OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include "chrome/browser/policy/policy_error_map.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/utf_string_conversions.h" |
| 10 #include "ui/base/l10n/l10n_util.h" |
| 11 #include "ui/base/resource/resource_bundle.h" |
| 12 |
| 13 namespace policy { |
| 14 |
| 15 struct PolicyErrorMap::PendingError { |
| 16 PendingError(ConfigurationPolicyType policy, |
| 17 int message_id, |
| 18 const string16& replacement) |
| 19 : policy(policy), |
| 20 message_id(message_id), |
| 21 has_replacement(true), |
| 22 replacement(replacement) {} |
| 23 |
| 24 PendingError(ConfigurationPolicyType policy, int message_id) |
| 25 : policy(policy), message_id(message_id), has_replacement(false) {} |
| 26 |
| 27 ConfigurationPolicyType policy; |
| 28 int message_id; |
| 29 bool has_replacement; |
| 30 string16 replacement; |
| 31 }; |
| 32 |
| 33 PolicyErrorMap::PolicyErrorMap() { |
| 34 } |
| 35 |
| 36 PolicyErrorMap::~PolicyErrorMap() { |
| 37 } |
| 38 |
| 39 bool PolicyErrorMap::IsReady() const { |
| 40 return ui::ResourceBundle::HasSharedInstance(); |
| 41 } |
| 42 |
| 43 void PolicyErrorMap::AddError(ConfigurationPolicyType policy, int message_id) { |
| 44 AddError(PendingError(policy, message_id)); |
| 45 } |
| 46 |
| 47 void PolicyErrorMap::AddError(ConfigurationPolicyType policy, |
| 48 int message_id, |
| 49 const std::string& replacement) { |
| 50 AddError(PendingError(policy, message_id, ASCIIToUTF16(replacement))); |
| 51 } |
| 52 |
| 53 ListValue* PolicyErrorMap::GetErrors(ConfigurationPolicyType policy) { |
| 54 CheckReadyAndConvert(); |
| 55 std::pair<const_iterator, const_iterator> range = map_.equal_range(policy); |
| 56 |
| 57 if (range.first == range.second) |
| 58 return NULL; |
| 59 |
| 60 ListValue* list = new ListValue(); |
| 61 for (const_iterator it = range.first; it != range.second; ++it) |
| 62 list->Append(Value::CreateStringValue(it->second)); |
| 63 |
| 64 return list; |
| 65 } |
| 66 |
| 67 bool PolicyErrorMap::empty() { |
| 68 CheckReadyAndConvert(); |
| 69 return map_.empty(); |
| 70 } |
| 71 |
| 72 size_t PolicyErrorMap::size() { |
| 73 CheckReadyAndConvert(); |
| 74 return map_.size(); |
| 75 } |
| 76 |
| 77 PolicyErrorMap::const_iterator PolicyErrorMap::begin() { |
| 78 CheckReadyAndConvert(); |
| 79 return map_.begin(); |
| 80 } |
| 81 |
| 82 PolicyErrorMap::const_iterator PolicyErrorMap::end() { |
| 83 CheckReadyAndConvert(); |
| 84 return map_.end(); |
| 85 } |
| 86 |
| 87 void PolicyErrorMap::Clear() { |
| 88 CheckReadyAndConvert(); |
| 89 map_.clear(); |
| 90 } |
| 91 |
| 92 void PolicyErrorMap::AddError(const PendingError& error) { |
| 93 if (IsReady()) { |
| 94 Convert(error); |
| 95 } else { |
| 96 pending_.push_back(error); |
| 97 } |
| 98 } |
| 99 |
| 100 void PolicyErrorMap::Convert(const PendingError& error) { |
| 101 string16 message; |
| 102 if (error.has_replacement) |
| 103 message = l10n_util::GetStringFUTF16(error.message_id, error.replacement); |
| 104 else |
| 105 message = l10n_util::GetStringUTF16(error.message_id); |
| 106 map_.insert(std::make_pair(error.policy, message)); |
| 107 } |
| 108 |
| 109 void PolicyErrorMap::CheckReadyAndConvert() { |
| 110 DCHECK(IsReady()); |
| 111 for (size_t i = 0; i < pending_.size(); ++i) { |
| 112 Convert(pending_[i]); |
| 113 } |
| 114 pending_.clear(); |
| 115 } |
| 116 |
| 117 } // namespace policy |
OLD | NEW |