| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/common/extensions/extension_message_bundle.h" | 5 #include "chrome/common/extensions/extension_message_bundle.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
| 11 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
| 12 #include "base/i18n/rtl.h" | 12 #include "base/i18n/rtl.h" |
| 13 #include "base/linked_ptr.h" | 13 #include "base/linked_ptr.h" |
| 14 #include "base/scoped_ptr.h" | 14 #include "base/scoped_ptr.h" |
| 15 #include "base/singleton.h" | 15 #include "base/singleton.h" |
| 16 #include "base/stl_util-inl.h" | 16 #include "base/stl_util-inl.h" |
| 17 #include "base/string_util.h" |
| 17 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 18 #include "base/values.h" | 19 #include "base/values.h" |
| 19 #include "chrome/common/extensions/extension_constants.h" | 20 #include "chrome/common/extensions/extension_constants.h" |
| 20 #include "chrome/common/extensions/extension_error_utils.h" | 21 #include "chrome/common/extensions/extension_error_utils.h" |
| 21 #include "chrome/common/extensions/extension_l10n_util.h" | 22 #include "chrome/common/extensions/extension_l10n_util.h" |
| 22 | 23 |
| 23 namespace errors = extension_manifest_errors; | 24 namespace errors = extension_manifest_errors; |
| 24 | 25 |
| 25 const char* ExtensionMessageBundle::kContentKey = "content"; | 26 const char* ExtensionMessageBundle::kContentKey = "content"; |
| 26 const char* ExtensionMessageBundle::kMessageKey = "message"; | 27 const char* ExtensionMessageBundle::kMessageKey = "message"; |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 kPlaceholderEnd, | 205 kPlaceholderEnd, |
| 205 message, | 206 message, |
| 206 error); | 207 error); |
| 207 } | 208 } |
| 208 | 209 |
| 209 bool ExtensionMessageBundle::ReplaceMessages(std::string* text, | 210 bool ExtensionMessageBundle::ReplaceMessages(std::string* text, |
| 210 std::string* error) const { | 211 std::string* error) const { |
| 211 return ReplaceMessagesWithExternalDictionary(dictionary_, text, error); | 212 return ReplaceMessagesWithExternalDictionary(dictionary_, text, error); |
| 212 } | 213 } |
| 213 | 214 |
| 215 ExtensionMessageBundle::~ExtensionMessageBundle() { |
| 216 } |
| 217 |
| 214 // static | 218 // static |
| 215 bool ExtensionMessageBundle::ReplaceMessagesWithExternalDictionary( | 219 bool ExtensionMessageBundle::ReplaceMessagesWithExternalDictionary( |
| 216 const SubstitutionMap& dictionary, std::string* text, std::string* error) { | 220 const SubstitutionMap& dictionary, std::string* text, std::string* error) { |
| 217 return ReplaceVariables(dictionary, kMessageBegin, kMessageEnd, text, error); | 221 return ReplaceVariables(dictionary, kMessageBegin, kMessageEnd, text, error); |
| 218 } | 222 } |
| 219 | 223 |
| 220 // static | 224 // static |
| 221 bool ExtensionMessageBundle::ReplaceVariables( | 225 bool ExtensionMessageBundle::ReplaceVariables( |
| 222 const SubstitutionMap& variables, | 226 const SubstitutionMap& variables, |
| 223 const std::string& var_begin_delimiter, | 227 const std::string& var_begin_delimiter, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 var_end_delimiter.size(), | 267 var_end_delimiter.size(), |
| 264 value); | 268 value); |
| 265 | 269 |
| 266 // And position pointer to after the replacement. | 270 // And position pointer to after the replacement. |
| 267 beg_index += value.size() - var_begin_delimiter_size; | 271 beg_index += value.size() - var_begin_delimiter_size; |
| 268 } | 272 } |
| 269 | 273 |
| 270 return true; | 274 return true; |
| 271 } | 275 } |
| 272 | 276 |
| 277 // static |
| 278 bool ExtensionMessageBundle::IsValidName(const std::string& name) { |
| 279 if (name.empty()) |
| 280 return false; |
| 281 |
| 282 std::string::const_iterator it = name.begin(); |
| 283 for (; it != name.end(); ++it) { |
| 284 // Allow only ascii 0-9, a-z, A-Z, and _ in the name. |
| 285 if (!IsAsciiAlpha(*it) && !IsAsciiDigit(*it) && *it != '_' && *it != '@') |
| 286 return false; |
| 287 } |
| 288 |
| 289 return true; |
| 290 } |
| 291 |
| 273 // Dictionary interface. | 292 // Dictionary interface. |
| 274 | 293 |
| 275 std::string ExtensionMessageBundle::GetL10nMessage( | 294 std::string ExtensionMessageBundle::GetL10nMessage( |
| 276 const std::string& name) const { | 295 const std::string& name) const { |
| 277 return GetL10nMessage(name, dictionary_); | 296 return GetL10nMessage(name, dictionary_); |
| 278 } | 297 } |
| 279 | 298 |
| 280 // static | 299 // static |
| 281 std::string ExtensionMessageBundle::GetL10nMessage( | 300 std::string ExtensionMessageBundle::GetL10nMessage( |
| 282 const std::string& name, const SubstitutionMap& dictionary) { | 301 const std::string& name, const SubstitutionMap& dictionary) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 300 } | 319 } |
| 301 | 320 |
| 302 L10nMessagesMap* GetL10nMessagesMap(const std::string extension_id) { | 321 L10nMessagesMap* GetL10nMessagesMap(const std::string extension_id) { |
| 303 ExtensionToL10nMessagesMap::iterator it = | 322 ExtensionToL10nMessagesMap::iterator it = |
| 304 Singleton<ExtensionToMessagesMap>()->messages_map.find(extension_id); | 323 Singleton<ExtensionToMessagesMap>()->messages_map.find(extension_id); |
| 305 if (it != Singleton<ExtensionToMessagesMap>()->messages_map.end()) | 324 if (it != Singleton<ExtensionToMessagesMap>()->messages_map.end()) |
| 306 return &(it->second); | 325 return &(it->second); |
| 307 | 326 |
| 308 return NULL; | 327 return NULL; |
| 309 } | 328 } |
| OLD | NEW |