Chromium Code Reviews| 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 PendingError(const std::string& policy_name) : policy_name_(policy_name) {} |
|
Joao da Silva
2014/01/29 09:14:42
explicit
binjin
2014/02/03 14:40:19
Done.
| |
| 21 int index, | 21 virtual ~PendingError() {} |
| 22 int message_id, | |
| 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 | 22 |
| 31 PendingError(const std::string& policy, | 23 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 | 24 |
| 41 std::string policy; | 25 virtual base::string16 GetMessage() const = 0; |
| 42 std::string subkey; | 26 |
| 43 int index; | 27 private: |
| 44 int message_id; | 28 std::string policy_name_; |
| 45 bool has_replacement; | 29 |
| 46 std::string replacement; | 30 DISALLOW_COPY_AND_ASSIGN(PendingError); |
| 47 }; | 31 }; |
| 48 | 32 |
| 33 namespace { | |
| 34 | |
| 35 class SimplePendingError : public PolicyErrorMap::PendingError { | |
| 36 public: | |
| 37 SimplePendingError(const std::string& policy_name, | |
| 38 int message_id, | |
| 39 const std::string& replacement) | |
| 40 : PendingError(policy_name), | |
| 41 message_id_(message_id), | |
| 42 replacement_(replacement) {} | |
| 43 virtual ~SimplePendingError() {} | |
| 44 | |
| 45 virtual base::string16 GetMessage() const OVERRIDE { | |
| 46 if (message_id_ >= 0) { | |
| 47 if (replacement_.empty()) | |
| 48 return l10n_util::GetStringUTF16(message_id_); | |
| 49 return l10n_util::GetStringFUTF16(message_id_, | |
| 50 base::ASCIIToUTF16(replacement_)); | |
| 51 } | |
| 52 return base::ASCIIToUTF16(replacement_); | |
| 53 } | |
| 54 | |
| 55 private: | |
| 56 int message_id_; | |
| 57 std::string replacement_; | |
| 58 | |
| 59 DISALLOW_COPY_AND_ASSIGN(SimplePendingError); | |
| 60 }; | |
| 61 | |
| 62 class DictSubkeyPendingError : public SimplePendingError { | |
| 63 public: | |
| 64 DictSubkeyPendingError(const std::string& policy_name, | |
| 65 const std::string& subkey, | |
| 66 int message_id, | |
| 67 const std::string& replacement) | |
| 68 : SimplePendingError(policy_name, message_id, replacement), | |
| 69 subkey_(subkey) {} | |
| 70 virtual ~DictSubkeyPendingError() {} | |
| 71 | |
| 72 virtual base::string16 GetMessage() const OVERRIDE { | |
| 73 return l10n_util::GetStringFUTF16(IDS_POLICY_SUBKEY_ERROR, | |
| 74 base::ASCIIToUTF16(subkey_), | |
| 75 SimplePendingError::GetMessage()); | |
| 76 } | |
| 77 | |
| 78 private: | |
| 79 std::string subkey_; | |
| 80 | |
| 81 DISALLOW_COPY_AND_ASSIGN(DictSubkeyPendingError); | |
| 82 }; | |
| 83 | |
| 84 class ListItemPendingError : public SimplePendingError { | |
| 85 public: | |
| 86 ListItemPendingError(const std::string& policy_name, | |
| 87 int index, | |
| 88 int message_id, | |
| 89 const std::string& replacement) | |
| 90 : SimplePendingError(policy_name, message_id, replacement), | |
| 91 index_(index) {} | |
| 92 virtual ~ListItemPendingError() {} | |
| 93 | |
| 94 virtual base::string16 GetMessage() const OVERRIDE { | |
| 95 return l10n_util::GetStringFUTF16(IDS_POLICY_LIST_ENTRY_ERROR, | |
| 96 base::IntToString16(index_), | |
| 97 SimplePendingError::GetMessage()); | |
| 98 } | |
| 99 | |
| 100 private: | |
| 101 int index_; | |
| 102 | |
| 103 DISALLOW_COPY_AND_ASSIGN(ListItemPendingError); | |
| 104 }; | |
| 105 | |
| 106 class SchemaValidatePendingError : public SimplePendingError { | |
| 107 public: | |
| 108 SchemaValidatePendingError(const std::string& policy_name, | |
| 109 const std::string& error_path, | |
| 110 int message_id, | |
| 111 const std::string& replacement) | |
| 112 : SimplePendingError(policy_name, message_id, replacement), | |
|
Joao da Silva
2014/01/29 09:14:42
Pass -1 instead of message_id here, and remove the
binjin
2014/02/03 14:40:19
Done.
| |
| 113 error_path_(error_path) {}; | |
| 114 virtual ~SchemaValidatePendingError() {} | |
| 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(SchemaValidatePendingError); | |
| 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 SchemaValidatePendingError(policy, error_path, -1, message)); | |
|
Joao da Silva
2014/01/29 09:14:42
Remove the -1 argument to SchemaValidatePendingErr
binjin
2014/02/03 14:40:19
Done.
| |
| 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 |