diff options
author | dconnelly@chromium.org <dconnelly@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-05 19:01:25 +0000 |
---|---|---|
committer | dconnelly@chromium.org <dconnelly@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-05 19:01:25 +0000 |
commit | f6c403b591bd31653b65d068a73e45e453db9d7f (patch) | |
tree | 8a41ec3a84d4fcf8424eb9685262b3f428944b12 /components/policy | |
parent | b8fdbd943bf4c18267488296841d5f672d8786e8 (diff) | |
download | chromium_src-f6c403b591bd31653b65d068a73e45e453db9d7f.zip chromium_src-f6c403b591bd31653b65d068a73e45e453db9d7f.tar.gz chromium_src-f6c403b591bd31653b65d068a73e45e453db9d7f.tar.bz2 |
Move PolicyErrorMap to components/policy/.
Facilitates policy componentization. Also add DEPS exceptions for ui
and grit includes.
BUG=271392
TBR=sky
Review URL: https://codereview.chromium.org/103263003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@239004 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components/policy')
-rw-r--r-- | components/policy/core/browser/DEPS | 5 | ||||
-rw-r--r-- | components/policy/core/browser/policy_error_map.cc | 168 | ||||
-rw-r--r-- | components/policy/core/browser/policy_error_map.h | 103 |
3 files changed, 276 insertions, 0 deletions
diff --git a/components/policy/core/browser/DEPS b/components/policy/core/browser/DEPS new file mode 100644 index 0000000..0410926 --- /dev/null +++ b/components/policy/core/browser/DEPS @@ -0,0 +1,5 @@ +include_rules = [ + "+grit/component_strings.h", # For generated headers + "+ui/base/l10n", + "+ui/base/resource", +] diff --git a/components/policy/core/browser/policy_error_map.cc b/components/policy/core/browser/policy_error_map.cc new file mode 100644 index 0000000..0c358c5 --- /dev/null +++ b/components/policy/core/browser/policy_error_map.cc @@ -0,0 +1,168 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/policy/core/browser/policy_error_map.h" + +#include <utility> + +#include "base/strings/string_number_conversions.h" +#include "base/strings/string_util.h" +#include "base/strings/utf_string_conversions.h" +#include "grit/component_strings.h" +#include "ui/base/l10n/l10n_util.h" +#include "ui/base/resource/resource_bundle.h" + +namespace policy { + +struct PolicyErrorMap::PendingError { + PendingError(const std::string& policy, + const std::string& subkey, + int index, + int message_id, + const std::string& replacement) + : policy(policy), + subkey(subkey), + index(index), + message_id(message_id), + has_replacement(true), + replacement(replacement) {} + + PendingError(const std::string& policy, + const std::string& subkey, + int index, + int message_id) + : policy(policy), + subkey(subkey), + index(index), + message_id(message_id), + has_replacement(false) {} + + std::string policy; + std::string subkey; + int index; + int message_id; + bool has_replacement; + std::string replacement; +}; + +PolicyErrorMap::PolicyErrorMap() { +} + +PolicyErrorMap::~PolicyErrorMap() { +} + +bool PolicyErrorMap::IsReady() const { + return ui::ResourceBundle::HasSharedInstance(); +} + +void PolicyErrorMap::AddError(const std::string& policy, int message_id) { + AddError(PendingError(policy, std::string(), -1, message_id)); +} + +void PolicyErrorMap::AddError(const std::string& policy, + const std::string& subkey, + int message_id) { + AddError(PendingError(policy, subkey, -1, message_id)); +} + +void PolicyErrorMap::AddError(const std::string& policy, + int index, + int message_id) { + AddError(PendingError(policy, std::string(), index, message_id)); +} + +void PolicyErrorMap::AddError(const std::string& policy, + int message_id, + const std::string& replacement) { + AddError(PendingError(policy, std::string(), -1, message_id, replacement)); +} + +void PolicyErrorMap::AddError(const std::string& policy, + const std::string& subkey, + int message_id, + const std::string& replacement) { + AddError(PendingError(policy, subkey, -1, message_id, replacement)); +} + +void PolicyErrorMap::AddError(const std::string& policy, + int index, + int message_id, + const std::string& replacement) { + AddError(PendingError(policy, std::string(), index, message_id, replacement)); +} + +string16 PolicyErrorMap::GetErrors(const std::string& policy) { + CheckReadyAndConvert(); + std::pair<const_iterator, const_iterator> range = map_.equal_range(policy); + std::vector<string16> list; + for (const_iterator it = range.first; it != range.second; ++it) + list.push_back(it->second); + return JoinString(list, '\n'); +} + +bool PolicyErrorMap::empty() { + CheckReadyAndConvert(); + return map_.empty(); +} + +size_t PolicyErrorMap::size() { + CheckReadyAndConvert(); + return map_.size(); +} + +PolicyErrorMap::const_iterator PolicyErrorMap::begin() { + CheckReadyAndConvert(); + return map_.begin(); +} + +PolicyErrorMap::const_iterator PolicyErrorMap::end() { + CheckReadyAndConvert(); + return map_.end(); +} + +void PolicyErrorMap::Clear() { + CheckReadyAndConvert(); + map_.clear(); +} + +void PolicyErrorMap::AddError(const PendingError& error) { + if (IsReady()) { + Convert(error); + } else { + pending_.push_back(error); + } +} + +void PolicyErrorMap::Convert(const PendingError& error) { + string16 submessage; + if (error.has_replacement) { + submessage = l10n_util::GetStringFUTF16(error.message_id, + ASCIIToUTF16(error.replacement)); + } else { + submessage = l10n_util::GetStringUTF16(error.message_id); + } + string16 message; + if (!error.subkey.empty()) { + message = l10n_util::GetStringFUTF16(IDS_POLICY_SUBKEY_ERROR, + ASCIIToUTF16(error.subkey), + submessage); + } else if (error.index >= 0) { + message = l10n_util::GetStringFUTF16(IDS_POLICY_LIST_ENTRY_ERROR, + base::IntToString16(error.index), + submessage); + } else { + message = submessage; + } + map_.insert(std::make_pair(error.policy, message)); +} + +void PolicyErrorMap::CheckReadyAndConvert() { + DCHECK(IsReady()); + for (size_t i = 0; i < pending_.size(); ++i) { + Convert(pending_[i]); + } + pending_.clear(); +} + +} // namespace policy diff --git a/components/policy/core/browser/policy_error_map.h b/components/policy/core/browser/policy_error_map.h new file mode 100644 index 0000000..afc9706 --- /dev/null +++ b/components/policy/core/browser/policy_error_map.h @@ -0,0 +1,103 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_POLICY_CORE_BROWSER_POLICY_ERROR_MAP_H_ +#define COMPONENTS_POLICY_CORE_BROWSER_POLICY_ERROR_MAP_H_ + +#include <map> +#include <string> +#include <vector> + +#include "base/basictypes.h" +#include "base/strings/string16.h" +#include "components/policy/policy_export.h" + +namespace policy { + +// Collects error messages and their associated policies. +class POLICY_EXPORT PolicyErrorMap { + public: + typedef std::multimap<std::string, string16> PolicyMapType; + typedef PolicyMapType::const_iterator const_iterator; + + PolicyErrorMap(); + virtual ~PolicyErrorMap(); + + // Returns true when the errors logged are ready to be retrieved. It is always + // safe to call AddError, but the other methods are only allowed once + // IsReady is true. IsReady will be true once the UI message loop has started. + bool IsReady() const; + + // Adds an entry with key |policy| and the error message corresponding to + // |message_id| in grit/generated_resources.h to the map. + void AddError(const std::string& policy, int message_id); + + // Adds an entry with key |policy|, subkey |subkey|, and the error message + // corresponding to |message_id| in grit/generated_resources.h to the map. + void AddError(const std::string& policy, + const std::string& subkey, + int message_id); + + // Adds an entry with key |policy|, list index |index|, and the error message + // corresponding to |message_id| in grit/generated_resources.h to the map. + void AddError(const std::string& policy, + int index, + int message_id); + + // Adds an entry with key |policy| and the error message corresponding to + // |message_id| in grit/generated_resources.h to the map and replaces the + // placeholder within the error message with |replacement_string|. + void AddError(const std::string& policy, + int message_id, + const std::string& replacement_string); + + // Adds an entry with key |policy|, subkey |subkey| and the error message + // corresponding to |message_id| in grit/generated_resources.h to the map. + // Replaces the placeholder in the error message with |replacement_string|. + void AddError(const std::string& policy, + const std::string& subkey, + int message_id, + const std::string& replacement_string); + + // Adds an entry with key |policy|, list index |index| and the error message + // corresponding to |message_id| in grit/generated_resources.h to the map. + // Replaces the placeholder in the error message with |replacement_string|. + void AddError(const std::string& policy, + int index, + int message_id, + const std::string& replacement_string); + + // Returns all the error messages stored for |policy|, separated by a white + // space. Returns an empty string if there are no errors for |policy|. + string16 GetErrors(const std::string& policy); + + bool empty(); + size_t size(); + + const_iterator begin(); + const_iterator end(); + + void Clear(); + + private: + struct PendingError; + + // Maps the error when ready, otherwise adds it to the pending errors list. + void AddError(const PendingError& error); + + // Converts a PendingError into a |map_| entry. + void Convert(const PendingError& error); + + // Converts all pending errors to |map_| entries. + void CheckReadyAndConvert(); + + std::vector<PendingError> pending_; + PolicyMapType map_; + + DISALLOW_COPY_AND_ASSIGN(PolicyErrorMap); +}; + +} // namespace policy + +#endif // COMPONENTS_POLICY_CORE_BROWSER_POLICY_ERROR_MAP_H_ |