Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / components / policy / core / browser / policy_error_map.cc
blob89453040f44bfe2dbaa8ef7fb0c30b2af321090b
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/policy/core/browser/policy_error_map.h"
7 #include <utility>
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "grit/component_strings.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/resource/resource_bundle.h"
16 namespace policy {
18 class PolicyErrorMap::PendingError {
19 public:
20 explicit PendingError(const std::string& policy_name)
21 : policy_name_(policy_name) {}
22 virtual ~PendingError() {}
24 const std::string& policy_name() const { return policy_name_; }
26 virtual base::string16 GetMessage() const = 0;
28 private:
29 std::string policy_name_;
31 DISALLOW_COPY_AND_ASSIGN(PendingError);
34 namespace {
36 class SimplePendingError : public PolicyErrorMap::PendingError {
37 public:
38 SimplePendingError(const std::string& policy_name,
39 int message_id,
40 const std::string& replacement)
41 : PendingError(policy_name),
42 message_id_(message_id),
43 replacement_(replacement) {}
44 virtual ~SimplePendingError() {}
46 virtual base::string16 GetMessage() const OVERRIDE {
47 if (message_id_ >= 0) {
48 if (replacement_.empty())
49 return l10n_util::GetStringUTF16(message_id_);
50 return l10n_util::GetStringFUTF16(message_id_,
51 base::ASCIIToUTF16(replacement_));
53 return base::ASCIIToUTF16(replacement_);
56 private:
57 int message_id_;
58 std::string replacement_;
60 DISALLOW_COPY_AND_ASSIGN(SimplePendingError);
63 class DictSubkeyPendingError : public SimplePendingError {
64 public:
65 DictSubkeyPendingError(const std::string& policy_name,
66 const std::string& subkey,
67 int message_id,
68 const std::string& replacement)
69 : SimplePendingError(policy_name, message_id, replacement),
70 subkey_(subkey) {}
71 virtual ~DictSubkeyPendingError() {}
73 virtual base::string16 GetMessage() const OVERRIDE {
74 return l10n_util::GetStringFUTF16(IDS_POLICY_SUBKEY_ERROR,
75 base::ASCIIToUTF16(subkey_),
76 SimplePendingError::GetMessage());
79 private:
80 std::string subkey_;
82 DISALLOW_COPY_AND_ASSIGN(DictSubkeyPendingError);
85 class ListItemPendingError : public SimplePendingError {
86 public:
87 ListItemPendingError(const std::string& policy_name,
88 int index,
89 int message_id,
90 const std::string& replacement)
91 : SimplePendingError(policy_name, message_id, replacement),
92 index_(index) {}
93 virtual ~ListItemPendingError() {}
95 virtual base::string16 GetMessage() const OVERRIDE {
96 return l10n_util::GetStringFUTF16(IDS_POLICY_LIST_ENTRY_ERROR,
97 base::IntToString16(index_),
98 SimplePendingError::GetMessage());
101 private:
102 int index_;
104 DISALLOW_COPY_AND_ASSIGN(ListItemPendingError);
107 class SchemaValidatingPendingError : public SimplePendingError {
108 public:
109 SchemaValidatingPendingError(const std::string& policy_name,
110 const std::string& error_path,
111 const std::string& replacement)
112 : SimplePendingError(policy_name, -1, replacement),
113 error_path_(error_path) {};
114 virtual ~SchemaValidatingPendingError() {}
116 virtual base::string16 GetMessage() const OVERRIDE {
117 return l10n_util::GetStringFUTF16(IDS_POLICY_SCHEMA_VALIDATION_ERROR,
118 base::ASCIIToUTF16(error_path_),
119 SimplePendingError::GetMessage());
122 private:
123 std::string error_path_;
125 DISALLOW_COPY_AND_ASSIGN(SchemaValidatingPendingError);
128 } // namespace
130 PolicyErrorMap::PolicyErrorMap() {
133 PolicyErrorMap::~PolicyErrorMap() {
136 bool PolicyErrorMap::IsReady() const {
137 return ui::ResourceBundle::HasSharedInstance();
140 void PolicyErrorMap::AddError(const std::string& policy, int message_id) {
141 AddError(new SimplePendingError(policy, message_id, std::string()));
144 void PolicyErrorMap::AddError(const std::string& policy,
145 const std::string& subkey,
146 int message_id) {
147 AddError(
148 new DictSubkeyPendingError(policy, subkey, message_id, std::string()));
151 void PolicyErrorMap::AddError(const std::string& policy,
152 int index,
153 int message_id) {
154 AddError(new ListItemPendingError(policy, index, message_id, std::string()));
157 void PolicyErrorMap::AddError(const std::string& policy,
158 int message_id,
159 const std::string& replacement) {
160 AddError(new SimplePendingError(policy, message_id, replacement));
163 void PolicyErrorMap::AddError(const std::string& policy,
164 const std::string& subkey,
165 int message_id,
166 const std::string& replacement) {
167 AddError(new DictSubkeyPendingError(policy, subkey, message_id, replacement));
170 void PolicyErrorMap::AddError(const std::string& policy,
171 int index,
172 int message_id,
173 const std::string& replacement) {
174 AddError(new ListItemPendingError(policy, index, message_id, replacement));
177 void PolicyErrorMap::AddError(const std::string& policy,
178 const std::string& error_path,
179 const std::string& message) {
180 AddError(new SchemaValidatingPendingError(policy, error_path, message));
183 base::string16 PolicyErrorMap::GetErrors(const std::string& policy) {
184 CheckReadyAndConvert();
185 std::pair<const_iterator, const_iterator> range = map_.equal_range(policy);
186 std::vector<base::string16> list;
187 for (const_iterator it = range.first; it != range.second; ++it)
188 list.push_back(it->second);
189 return JoinString(list, '\n');
192 bool PolicyErrorMap::empty() {
193 CheckReadyAndConvert();
194 return map_.empty();
197 size_t PolicyErrorMap::size() {
198 CheckReadyAndConvert();
199 return map_.size();
202 PolicyErrorMap::const_iterator PolicyErrorMap::begin() {
203 CheckReadyAndConvert();
204 return map_.begin();
207 PolicyErrorMap::const_iterator PolicyErrorMap::end() {
208 CheckReadyAndConvert();
209 return map_.end();
212 void PolicyErrorMap::Clear() {
213 CheckReadyAndConvert();
214 map_.clear();
217 void PolicyErrorMap::AddError(PendingError* error) {
218 if (IsReady()) {
219 Convert(error);
220 delete error;
221 } else {
222 pending_.push_back(error);
226 void PolicyErrorMap::Convert(PendingError* error) {
227 map_.insert(std::make_pair(error->policy_name(), error->GetMessage()));
230 void PolicyErrorMap::CheckReadyAndConvert() {
231 DCHECK(IsReady());
232 for (size_t i = 0; i < pending_.size(); ++i) {
233 Convert(pending_[i]);
235 pending_.clear();
238 } // namespace policy