| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/policy/core/browser/policy_error_map.h" | 5 #include "components/policy/core/browser/policy_error_map.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "grit/component_strings.h" | 12 #include "grit/component_strings.h" |
| 13 #include "ui/base/l10n/l10n_util.h" | 13 #include "ui/base/l10n/l10n_util.h" |
| 14 #include "ui/base/resource/resource_bundle.h" | 14 #include "ui/base/resource/resource_bundle.h" |
| 15 | 15 |
| 16 namespace policy { | 16 namespace policy { |
| 17 | 17 |
| 18 struct PolicyErrorMap::PendingError { | 18 class PolicyErrorMap::PendingError { |
| 19 PendingError(const std::string& policy, | 19 public: |
| 20 const std::string& subkey, | 20 explicit PendingError(const std::string& policy_name) |
| 21 int index, | 21 : policy_name_(policy_name) {} |
| 22 int message_id, | 22 virtual ~PendingError() {} |
| 23 const std::string& replacement) | |
| 24 : policy(policy), | |
| 25 subkey(subkey), | |
| 26 index(index), | |
| 27 message_id(message_id), | |
| 28 has_replacement(true), | |
| 29 replacement(replacement) {} | |
| 30 | 23 |
| 31 PendingError(const std::string& policy, | 24 const std::string& policy_name() const { return policy_name_; } |
| 32 const std::string& subkey, | |
| 33 int index, | |
| 34 int message_id) | |
| 35 : policy(policy), | |
| 36 subkey(subkey), | |
| 37 index(index), | |
| 38 message_id(message_id), | |
| 39 has_replacement(false) {} | |
| 40 | 25 |
| 41 std::string policy; | 26 virtual base::string16 GetMessage() const = 0; |
| 42 std::string subkey; | 27 |
| 43 int index; | 28 private: |
| 44 int message_id; | 29 std::string policy_name_; |
| 45 bool has_replacement; | 30 |
| 46 std::string replacement; | 31 DISALLOW_COPY_AND_ASSIGN(PendingError); |
| 47 }; | 32 }; |
| 48 | 33 |
| 34 namespace { |
| 35 |
| 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() {} |
| 45 |
| 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_)); |
| 52 } |
| 53 return base::ASCIIToUTF16(replacement_); |
| 54 } |
| 55 |
| 56 private: |
| 57 int message_id_; |
| 58 std::string replacement_; |
| 59 |
| 60 DISALLOW_COPY_AND_ASSIGN(SimplePendingError); |
| 61 }; |
| 62 |
| 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() {} |
| 72 |
| 73 virtual base::string16 GetMessage() const OVERRIDE { |
| 74 return l10n_util::GetStringFUTF16(IDS_POLICY_SUBKEY_ERROR, |
| 75 base::ASCIIToUTF16(subkey_), |
| 76 SimplePendingError::GetMessage()); |
| 77 } |
| 78 |
| 79 private: |
| 80 std::string subkey_; |
| 81 |
| 82 DISALLOW_COPY_AND_ASSIGN(DictSubkeyPendingError); |
| 83 }; |
| 84 |
| 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() {} |
| 94 |
| 95 virtual base::string16 GetMessage() const OVERRIDE { |
| 96 return l10n_util::GetStringFUTF16(IDS_POLICY_LIST_ENTRY_ERROR, |
| 97 base::IntToString16(index_), |
| 98 SimplePendingError::GetMessage()); |
| 99 } |
| 100 |
| 101 private: |
| 102 int index_; |
| 103 |
| 104 DISALLOW_COPY_AND_ASSIGN(ListItemPendingError); |
| 105 }; |
| 106 |
| 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() {} |
| 115 |
| 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()); |
| 120 } |
| 121 |
| 122 private: |
| 123 std::string error_path_; |
| 124 |
| 125 DISALLOW_COPY_AND_ASSIGN(SchemaValidatingPendingError); |
| 126 }; |
| 127 |
| 128 } // namespace |
| 129 |
| 49 PolicyErrorMap::PolicyErrorMap() { | 130 PolicyErrorMap::PolicyErrorMap() { |
| 50 } | 131 } |
| 51 | 132 |
| 52 PolicyErrorMap::~PolicyErrorMap() { | 133 PolicyErrorMap::~PolicyErrorMap() { |
| 53 } | 134 } |
| 54 | 135 |
| 55 bool PolicyErrorMap::IsReady() const { | 136 bool PolicyErrorMap::IsReady() const { |
| 56 return ui::ResourceBundle::HasSharedInstance(); | 137 return ui::ResourceBundle::HasSharedInstance(); |
| 57 } | 138 } |
| 58 | 139 |
| 59 void PolicyErrorMap::AddError(const std::string& policy, int message_id) { | 140 void PolicyErrorMap::AddError(const std::string& policy, int message_id) { |
| 60 AddError(PendingError(policy, std::string(), -1, message_id)); | 141 AddError(new SimplePendingError(policy, message_id, std::string())); |
| 61 } | 142 } |
| 62 | 143 |
| 63 void PolicyErrorMap::AddError(const std::string& policy, | 144 void PolicyErrorMap::AddError(const std::string& policy, |
| 64 const std::string& subkey, | 145 const std::string& subkey, |
| 65 int message_id) { | 146 int message_id) { |
| 66 AddError(PendingError(policy, subkey, -1, message_id)); | 147 AddError( |
| 148 new DictSubkeyPendingError(policy, subkey, message_id, std::string())); |
| 67 } | 149 } |
| 68 | 150 |
| 69 void PolicyErrorMap::AddError(const std::string& policy, | 151 void PolicyErrorMap::AddError(const std::string& policy, |
| 70 int index, | 152 int index, |
| 71 int message_id) { | 153 int message_id) { |
| 72 AddError(PendingError(policy, std::string(), index, message_id)); | 154 AddError(new ListItemPendingError(policy, index, message_id, std::string())); |
| 73 } | 155 } |
| 74 | 156 |
| 75 void PolicyErrorMap::AddError(const std::string& policy, | 157 void PolicyErrorMap::AddError(const std::string& policy, |
| 76 int message_id, | 158 int message_id, |
| 77 const std::string& replacement) { | 159 const std::string& replacement) { |
| 78 AddError(PendingError(policy, std::string(), -1, message_id, replacement)); | 160 AddError(new SimplePendingError(policy, message_id, replacement)); |
| 79 } | 161 } |
| 80 | 162 |
| 81 void PolicyErrorMap::AddError(const std::string& policy, | 163 void PolicyErrorMap::AddError(const std::string& policy, |
| 82 const std::string& subkey, | 164 const std::string& subkey, |
| 83 int message_id, | 165 int message_id, |
| 84 const std::string& replacement) { | 166 const std::string& replacement) { |
| 85 AddError(PendingError(policy, subkey, -1, message_id, replacement)); | 167 AddError(new DictSubkeyPendingError(policy, subkey, message_id, replacement)); |
| 86 } | 168 } |
| 87 | 169 |
| 88 void PolicyErrorMap::AddError(const std::string& policy, | 170 void PolicyErrorMap::AddError(const std::string& policy, |
| 89 int index, | 171 int index, |
| 90 int message_id, | 172 int message_id, |
| 91 const std::string& replacement) { | 173 const std::string& replacement) { |
| 92 AddError(PendingError(policy, std::string(), index, message_id, replacement)); | 174 AddError(new ListItemPendingError(policy, index, message_id, replacement)); |
| 175 } |
| 176 |
| 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)); |
| 93 } | 181 } |
| 94 | 182 |
| 95 base::string16 PolicyErrorMap::GetErrors(const std::string& policy) { | 183 base::string16 PolicyErrorMap::GetErrors(const std::string& policy) { |
| 96 CheckReadyAndConvert(); | 184 CheckReadyAndConvert(); |
| 97 std::pair<const_iterator, const_iterator> range = map_.equal_range(policy); | 185 std::pair<const_iterator, const_iterator> range = map_.equal_range(policy); |
| 98 std::vector<base::string16> list; | 186 std::vector<base::string16> list; |
| 99 for (const_iterator it = range.first; it != range.second; ++it) | 187 for (const_iterator it = range.first; it != range.second; ++it) |
| 100 list.push_back(it->second); | 188 list.push_back(it->second); |
| 101 return JoinString(list, '\n'); | 189 return JoinString(list, '\n'); |
| 102 } | 190 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 119 PolicyErrorMap::const_iterator PolicyErrorMap::end() { | 207 PolicyErrorMap::const_iterator PolicyErrorMap::end() { |
| 120 CheckReadyAndConvert(); | 208 CheckReadyAndConvert(); |
| 121 return map_.end(); | 209 return map_.end(); |
| 122 } | 210 } |
| 123 | 211 |
| 124 void PolicyErrorMap::Clear() { | 212 void PolicyErrorMap::Clear() { |
| 125 CheckReadyAndConvert(); | 213 CheckReadyAndConvert(); |
| 126 map_.clear(); | 214 map_.clear(); |
| 127 } | 215 } |
| 128 | 216 |
| 129 void PolicyErrorMap::AddError(const PendingError& error) { | 217 void PolicyErrorMap::AddError(PendingError* error) { |
| 130 if (IsReady()) { | 218 if (IsReady()) { |
| 131 Convert(error); | 219 Convert(error); |
| 220 delete error; |
| 132 } else { | 221 } else { |
| 133 pending_.push_back(error); | 222 pending_.push_back(error); |
| 134 } | 223 } |
| 135 } | 224 } |
| 136 | 225 |
| 137 void PolicyErrorMap::Convert(const PendingError& error) { | 226 void PolicyErrorMap::Convert(PendingError* error) { |
| 138 base::string16 submessage; | 227 map_.insert(std::make_pair(error->policy_name(), error->GetMessage())); |
| 139 if (error.has_replacement) { | |
| 140 submessage = l10n_util::GetStringFUTF16(error.message_id, | |
| 141 base::ASCIIToUTF16( | |
| 142 error.replacement)); | |
| 143 } else { | |
| 144 submessage = l10n_util::GetStringUTF16(error.message_id); | |
| 145 } | |
| 146 base::string16 message; | |
| 147 if (!error.subkey.empty()) { | |
| 148 message = l10n_util::GetStringFUTF16(IDS_POLICY_SUBKEY_ERROR, | |
| 149 base::ASCIIToUTF16(error.subkey), | |
| 150 submessage); | |
| 151 } else if (error.index >= 0) { | |
| 152 message = l10n_util::GetStringFUTF16(IDS_POLICY_LIST_ENTRY_ERROR, | |
| 153 base::IntToString16(error.index), | |
| 154 submessage); | |
| 155 } else { | |
| 156 message = submessage; | |
| 157 } | |
| 158 map_.insert(std::make_pair(error.policy, message)); | |
| 159 } | 228 } |
| 160 | 229 |
| 161 void PolicyErrorMap::CheckReadyAndConvert() { | 230 void PolicyErrorMap::CheckReadyAndConvert() { |
| 162 DCHECK(IsReady()); | 231 DCHECK(IsReady()); |
| 163 for (size_t i = 0; i < pending_.size(); ++i) { | 232 for (size_t i = 0; i < pending_.size(); ++i) { |
| 164 Convert(pending_[i]); | 233 Convert(pending_[i]); |
| 165 } | 234 } |
| 166 pending_.clear(); | 235 pending_.clear(); |
| 167 } | 236 } |
| 168 | 237 |
| 169 } // namespace policy | 238 } // namespace policy |
| OLD | NEW |