1 // Copyright (c) 2012 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/strings/string_number_conversions.h" 10 #include "base/strings/string_util.h" 11 #include "base/strings/utf_string_conversions.h" 12 #include "grit/generated_resources.h" 13 #include "ui/base/l10n/l10n_util.h" 14 #include "ui/base/resource/resource_bundle.h" 15 16 namespace policy { 17 18 struct PolicyErrorMap::PendingError { 19 PendingError(const std::string& policy, 20 const std::string& subkey, 21 int index, 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 31 PendingError(const std::string& policy, 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 41 std::string policy; 42 std::string subkey; 43 int index; 44 int message_id; 45 bool has_replacement; 46 std::string replacement; 47 }; 48 49 PolicyErrorMap::PolicyErrorMap() { 50 } 51 52 PolicyErrorMap::~PolicyErrorMap() { 53 } 54 55 bool PolicyErrorMap::IsReady() const { 56 return ui::ResourceBundle::HasSharedInstance(); 57 } 58 59 void PolicyErrorMap::AddError(const std::string& policy, int message_id) { 60 AddError(PendingError(policy, std::string(), -1, message_id)); 61 } 62 63 void PolicyErrorMap::AddError(const std::string& policy, 64 const std::string& subkey, 65 int message_id) { 66 AddError(PendingError(policy, subkey, -1, message_id)); 67 } 68 69 void PolicyErrorMap::AddError(const std::string& policy, 70 int index, 71 int message_id) { 72 AddError(PendingError(policy, std::string(), index, message_id)); 73 } 74 75 void PolicyErrorMap::AddError(const std::string& policy, 76 int message_id, 77 const std::string& replacement) { 78 AddError(PendingError(policy, std::string(), -1, message_id, replacement)); 79 } 80 81 void PolicyErrorMap::AddError(const std::string& policy, 82 const std::string& subkey, 83 int message_id, 84 const std::string& replacement) { 85 AddError(PendingError(policy, subkey, -1, message_id, replacement)); 86 } 87 88 void PolicyErrorMap::AddError(const std::string& policy, 89 int index, 90 int message_id, 91 const std::string& replacement) { 92 AddError(PendingError(policy, std::string(), index, message_id, replacement)); 93 } 94 95 string16 PolicyErrorMap::GetErrors(const std::string& policy) { 96 CheckReadyAndConvert(); 97 std::pair<const_iterator, const_iterator> range = map_.equal_range(policy); 98 std::vector<string16> list; 99 for (const_iterator it = range.first; it != range.second; ++it) 100 list.push_back(it->second); 101 return JoinString(list, '\n'); 102 } 103 104 bool PolicyErrorMap::empty() { 105 CheckReadyAndConvert(); 106 return map_.empty(); 107 } 108 109 size_t PolicyErrorMap::size() { 110 CheckReadyAndConvert(); 111 return map_.size(); 112 } 113 114 PolicyErrorMap::const_iterator PolicyErrorMap::begin() { 115 CheckReadyAndConvert(); 116 return map_.begin(); 117 } 118 119 PolicyErrorMap::const_iterator PolicyErrorMap::end() { 120 CheckReadyAndConvert(); 121 return map_.end(); 122 } 123 124 void PolicyErrorMap::Clear() { 125 CheckReadyAndConvert(); 126 map_.clear(); 127 } 128 129 void PolicyErrorMap::AddError(const PendingError& error) { 130 if (IsReady()) { 131 Convert(error); 132 } else { 133 pending_.push_back(error); 134 } 135 } 136 137 void PolicyErrorMap::Convert(const PendingError& error) { 138 string16 submessage; 139 if (error.has_replacement) { 140 submessage = l10n_util::GetStringFUTF16(error.message_id, 141 ASCIIToUTF16(error.replacement)); 142 } else { 143 submessage = l10n_util::GetStringUTF16(error.message_id); 144 } 145 string16 message; 146 if (!error.subkey.empty()) { 147 message = l10n_util::GetStringFUTF16(IDS_POLICY_SUBKEY_ERROR, 148 ASCIIToUTF16(error.subkey), 149 submessage); 150 } else if (error.index >= 0) { 151 message = l10n_util::GetStringFUTF16(IDS_POLICY_LIST_ENTRY_ERROR, 152 base::IntToString16(error.index), 153 submessage); 154 } else { 155 message = submessage; 156 } 157 map_.insert(std::make_pair(error.policy, message)); 158 } 159 160 void PolicyErrorMap::CheckReadyAndConvert() { 161 DCHECK(IsReady()); 162 for (size_t i = 0; i < pending_.size(); ++i) { 163 Convert(pending_[i]); 164 } 165 pending_.clear(); 166 } 167 168 } // namespace policy 169