| OLD | NEW |
| (Empty) |
| 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/common/extensions/extension_l10n_util.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <set> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/file_util.h" | |
| 13 #include "base/files/file_enumerator.h" | |
| 14 #include "base/json/json_file_value_serializer.h" | |
| 15 #include "base/logging.h" | |
| 16 #include "base/memory/linked_ptr.h" | |
| 17 #include "base/strings/stringprintf.h" | |
| 18 #include "base/strings/utf_string_conversions.h" | |
| 19 #include "base/values.h" | |
| 20 #include "chrome/common/extensions/extension_file_util.h" | |
| 21 #include "chrome/common/extensions/message_bundle.h" | |
| 22 #include "chrome/common/url_constants.h" | |
| 23 #include "extensions/common/constants.h" | |
| 24 #include "extensions/common/error_utils.h" | |
| 25 #include "extensions/common/manifest_constants.h" | |
| 26 #include "third_party/icu/source/common/unicode/uloc.h" | |
| 27 #include "ui/base/l10n/l10n_util.h" | |
| 28 | |
| 29 namespace errors = extensions::manifest_errors; | |
| 30 namespace keys = extensions::manifest_keys; | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 // Loads contents of the messages file for given locale. If file is not found, | |
| 35 // or there was parsing error we return NULL and set |error|. | |
| 36 // Caller owns the returned object. | |
| 37 base::DictionaryValue* LoadMessageFile(const base::FilePath& locale_path, | |
| 38 const std::string& locale, | |
| 39 std::string* error) { | |
| 40 base::FilePath file = locale_path.AppendASCII(locale) | |
| 41 .Append(extensions::kMessagesFilename); | |
| 42 JSONFileValueSerializer messages_serializer(file); | |
| 43 base::Value* dictionary = messages_serializer.Deserialize(NULL, error); | |
| 44 if (!dictionary) { | |
| 45 if (error->empty()) { | |
| 46 // JSONFileValueSerializer just returns NULL if file cannot be found. It | |
| 47 // doesn't set the error, so we have to do it. | |
| 48 *error = base::StringPrintf("Catalog file is missing for locale %s.", | |
| 49 locale.c_str()); | |
| 50 } else { | |
| 51 *error = extensions::ErrorUtils::FormatErrorMessage( | |
| 52 errors::kLocalesInvalidLocale, | |
| 53 base::UTF16ToUTF8(file.LossyDisplayName()), | |
| 54 *error); | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 return static_cast<base::DictionaryValue*>(dictionary); | |
| 59 } | |
| 60 | |
| 61 // Localizes manifest value of string type for a given key. | |
| 62 bool LocalizeManifestValue(const std::string& key, | |
| 63 const extensions::MessageBundle& messages, | |
| 64 base::DictionaryValue* manifest, | |
| 65 std::string* error) { | |
| 66 std::string result; | |
| 67 if (!manifest->GetString(key, &result)) | |
| 68 return true; | |
| 69 | |
| 70 if (!messages.ReplaceMessages(&result, error)) | |
| 71 return false; | |
| 72 | |
| 73 manifest->SetString(key, result); | |
| 74 return true; | |
| 75 } | |
| 76 | |
| 77 // Localizes manifest value of list type for a given key. | |
| 78 bool LocalizeManifestListValue(const std::string& key, | |
| 79 const extensions::MessageBundle& messages, | |
| 80 base::DictionaryValue* manifest, | |
| 81 std::string* error) { | |
| 82 base::ListValue* list = NULL; | |
| 83 if (!manifest->GetList(key, &list)) | |
| 84 return true; | |
| 85 | |
| 86 bool ret = true; | |
| 87 for (size_t i = 0; i < list->GetSize(); ++i) { | |
| 88 std::string result; | |
| 89 if (list->GetString(i, &result)) { | |
| 90 if (messages.ReplaceMessages(&result, error)) | |
| 91 list->Set(i, new base::StringValue(result)); | |
| 92 else | |
| 93 ret = false; | |
| 94 } | |
| 95 } | |
| 96 return ret; | |
| 97 } | |
| 98 | |
| 99 std::string& GetProcessLocale() { | |
| 100 CR_DEFINE_STATIC_LOCAL(std::string, locale, ()); | |
| 101 return locale; | |
| 102 } | |
| 103 | |
| 104 } // namespace | |
| 105 | |
| 106 namespace extension_l10n_util { | |
| 107 | |
| 108 void SetProcessLocale(const std::string& locale) { | |
| 109 GetProcessLocale() = locale; | |
| 110 } | |
| 111 | |
| 112 std::string GetDefaultLocaleFromManifest(const base::DictionaryValue& manifest, | |
| 113 std::string* error) { | |
| 114 std::string default_locale; | |
| 115 if (manifest.GetString(keys::kDefaultLocale, &default_locale)) | |
| 116 return default_locale; | |
| 117 | |
| 118 *error = errors::kInvalidDefaultLocale; | |
| 119 return std::string(); | |
| 120 } | |
| 121 | |
| 122 bool ShouldRelocalizeManifest(const base::DictionaryValue* manifest) { | |
| 123 if (!manifest) | |
| 124 return false; | |
| 125 | |
| 126 if (!manifest->HasKey(keys::kDefaultLocale)) | |
| 127 return false; | |
| 128 | |
| 129 std::string manifest_current_locale; | |
| 130 manifest->GetString(keys::kCurrentLocale, &manifest_current_locale); | |
| 131 return manifest_current_locale != CurrentLocaleOrDefault(); | |
| 132 } | |
| 133 | |
| 134 bool LocalizeManifest(const extensions::MessageBundle& messages, | |
| 135 base::DictionaryValue* manifest, | |
| 136 std::string* error) { | |
| 137 // Initialize name. | |
| 138 std::string result; | |
| 139 if (!manifest->GetString(keys::kName, &result)) { | |
| 140 *error = errors::kInvalidName; | |
| 141 return false; | |
| 142 } | |
| 143 if (!LocalizeManifestValue(keys::kName, messages, manifest, error)) { | |
| 144 return false; | |
| 145 } | |
| 146 | |
| 147 // Initialize short name. | |
| 148 if (!LocalizeManifestValue(keys::kShortName, messages, manifest, error)) | |
| 149 return false; | |
| 150 | |
| 151 // Initialize description. | |
| 152 if (!LocalizeManifestValue(keys::kDescription, messages, manifest, error)) | |
| 153 return false; | |
| 154 | |
| 155 // Initialize browser_action.default_title | |
| 156 std::string key(keys::kBrowserAction); | |
| 157 key.append("."); | |
| 158 key.append(keys::kPageActionDefaultTitle); | |
| 159 if (!LocalizeManifestValue(key, messages, manifest, error)) | |
| 160 return false; | |
| 161 | |
| 162 // Initialize page_action.default_title | |
| 163 key.assign(keys::kPageAction); | |
| 164 key.append("."); | |
| 165 key.append(keys::kPageActionDefaultTitle); | |
| 166 if (!LocalizeManifestValue(key, messages, manifest, error)) | |
| 167 return false; | |
| 168 | |
| 169 // Initialize omnibox.keyword. | |
| 170 if (!LocalizeManifestValue(keys::kOmniboxKeyword, messages, manifest, error)) | |
| 171 return false; | |
| 172 | |
| 173 base::ListValue* file_handlers = NULL; | |
| 174 if (manifest->GetList(keys::kFileBrowserHandlers, &file_handlers)) { | |
| 175 key.assign(keys::kFileBrowserHandlers); | |
| 176 for (size_t i = 0; i < file_handlers->GetSize(); i++) { | |
| 177 base::DictionaryValue* handler = NULL; | |
| 178 if (!file_handlers->GetDictionary(i, &handler)) { | |
| 179 *error = errors::kInvalidFileBrowserHandler; | |
| 180 return false; | |
| 181 } | |
| 182 if (!LocalizeManifestValue(keys::kPageActionDefaultTitle, messages, | |
| 183 handler, error)) | |
| 184 return false; | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 base::ListValue* media_galleries_handlers = NULL; | |
| 189 if (manifest->GetList(keys::kMediaGalleriesHandlers, | |
| 190 &media_galleries_handlers)) { | |
| 191 key.assign(keys::kMediaGalleriesHandlers); | |
| 192 for (size_t i = 0; i < media_galleries_handlers->GetSize(); i++) { | |
| 193 base::DictionaryValue* handler = NULL; | |
| 194 if (!media_galleries_handlers->GetDictionary(i, &handler)) { | |
| 195 *error = errors::kInvalidMediaGalleriesHandler; | |
| 196 return false; | |
| 197 } | |
| 198 if (!LocalizeManifestValue(keys::kPageActionDefaultTitle, messages, | |
| 199 handler, error)) | |
| 200 return false; | |
| 201 } | |
| 202 } | |
| 203 | |
| 204 // Initialize all input_components | |
| 205 base::ListValue* input_components = NULL; | |
| 206 if (manifest->GetList(keys::kInputComponents, &input_components)) { | |
| 207 for (size_t i = 0; i < input_components->GetSize(); ++i) { | |
| 208 base::DictionaryValue* module = NULL; | |
| 209 if (!input_components->GetDictionary(i, &module)) { | |
| 210 *error = errors::kInvalidInputComponents; | |
| 211 return false; | |
| 212 } | |
| 213 if (!LocalizeManifestValue(keys::kName, messages, module, error)) | |
| 214 return false; | |
| 215 if (!LocalizeManifestValue(keys::kDescription, messages, module, error)) | |
| 216 return false; | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 // Initialize app.launch.local_path. | |
| 221 if (!LocalizeManifestValue(keys::kLaunchLocalPath, messages, manifest, error)) | |
| 222 return false; | |
| 223 | |
| 224 // Initialize app.launch.web_url. | |
| 225 if (!LocalizeManifestValue(keys::kLaunchWebURL, messages, manifest, error)) | |
| 226 return false; | |
| 227 | |
| 228 // Initialize description of commmands. | |
| 229 base::DictionaryValue* commands_handler = NULL; | |
| 230 if (manifest->GetDictionary(keys::kCommands, &commands_handler)) { | |
| 231 for (base::DictionaryValue::Iterator iter(*commands_handler); | |
| 232 !iter.IsAtEnd(); | |
| 233 iter.Advance()) { | |
| 234 key.assign(base::StringPrintf("commands.%s.description", | |
| 235 iter.key().c_str())); | |
| 236 if (!LocalizeManifestValue(key, messages, manifest, error)) | |
| 237 return false; | |
| 238 } | |
| 239 } | |
| 240 | |
| 241 // Initialize search_provider fields. | |
| 242 base::DictionaryValue* search_provider = NULL; | |
| 243 if (manifest->GetDictionary(keys::kOverrideSearchProvider, | |
| 244 &search_provider)) { | |
| 245 for (base::DictionaryValue::Iterator iter(*search_provider); | |
| 246 !iter.IsAtEnd(); | |
| 247 iter.Advance()) { | |
| 248 key.assign(base::StringPrintf( | |
| 249 "%s.%s", keys::kOverrideSearchProvider, iter.key().c_str())); | |
| 250 bool success = (key == keys::kSettingsOverrideAlternateUrls) ? | |
| 251 LocalizeManifestListValue(key, messages, manifest, error) : | |
| 252 LocalizeManifestValue(key, messages, manifest, error); | |
| 253 if (!success) | |
| 254 return false; | |
| 255 } | |
| 256 } | |
| 257 | |
| 258 // Initialize chrome_settings_overrides.homepage. | |
| 259 if (!LocalizeManifestValue( | |
| 260 keys::kOverrideHomepage, messages, manifest, error)) | |
| 261 return false; | |
| 262 | |
| 263 // Initialize chrome_settings_overrides.startup_pages. | |
| 264 if (!LocalizeManifestListValue( | |
| 265 keys::kOverrideStartupPage, messages, manifest, error)) | |
| 266 return false; | |
| 267 | |
| 268 // Add current locale key to the manifest, so we can overwrite prefs | |
| 269 // with new manifest when chrome locale changes. | |
| 270 manifest->SetString(keys::kCurrentLocale, CurrentLocaleOrDefault()); | |
| 271 return true; | |
| 272 } | |
| 273 | |
| 274 bool LocalizeExtension(const base::FilePath& extension_path, | |
| 275 base::DictionaryValue* manifest, | |
| 276 std::string* error) { | |
| 277 DCHECK(manifest); | |
| 278 | |
| 279 std::string default_locale = GetDefaultLocaleFromManifest(*manifest, error); | |
| 280 | |
| 281 scoped_ptr<extensions::MessageBundle> message_bundle( | |
| 282 extension_file_util::LoadMessageBundle( | |
| 283 extension_path, default_locale, error)); | |
| 284 | |
| 285 if (!message_bundle.get() && !error->empty()) | |
| 286 return false; | |
| 287 | |
| 288 if (message_bundle.get() && | |
| 289 !LocalizeManifest(*message_bundle, manifest, error)) | |
| 290 return false; | |
| 291 | |
| 292 return true; | |
| 293 } | |
| 294 | |
| 295 bool AddLocale(const std::set<std::string>& chrome_locales, | |
| 296 const base::FilePath& locale_folder, | |
| 297 const std::string& locale_name, | |
| 298 std::set<std::string>* valid_locales, | |
| 299 std::string* error) { | |
| 300 // Accept name that starts with a . but don't add it to the list of supported | |
| 301 // locales. | |
| 302 if (locale_name.find(".") == 0) | |
| 303 return true; | |
| 304 if (chrome_locales.find(locale_name) == chrome_locales.end()) { | |
| 305 // Warn if there is an extension locale that's not in the Chrome list, | |
| 306 // but don't fail. | |
| 307 DLOG(WARNING) << base::StringPrintf("Supplied locale %s is not supported.", | |
| 308 locale_name.c_str()); | |
| 309 return true; | |
| 310 } | |
| 311 // Check if messages file is actually present (but don't check content). | |
| 312 if (base::PathExists( | |
| 313 locale_folder.Append(extensions::kMessagesFilename))) { | |
| 314 valid_locales->insert(locale_name); | |
| 315 } else { | |
| 316 *error = base::StringPrintf("Catalog file is missing for locale %s.", | |
| 317 locale_name.c_str()); | |
| 318 return false; | |
| 319 } | |
| 320 | |
| 321 return true; | |
| 322 } | |
| 323 | |
| 324 std::string CurrentLocaleOrDefault() { | |
| 325 std::string current_locale = l10n_util::NormalizeLocale(GetProcessLocale()); | |
| 326 if (current_locale.empty()) | |
| 327 current_locale = "en"; | |
| 328 | |
| 329 return current_locale; | |
| 330 } | |
| 331 | |
| 332 void GetAllLocales(std::set<std::string>* all_locales) { | |
| 333 const std::vector<std::string>& available_locales = | |
| 334 l10n_util::GetAvailableLocales(); | |
| 335 // Add all parents of the current locale to the available locales set. | |
| 336 // I.e. for sr_Cyrl_RS we add sr_Cyrl_RS, sr_Cyrl and sr. | |
| 337 for (size_t i = 0; i < available_locales.size(); ++i) { | |
| 338 std::vector<std::string> result; | |
| 339 l10n_util::GetParentLocales(available_locales[i], &result); | |
| 340 all_locales->insert(result.begin(), result.end()); | |
| 341 } | |
| 342 } | |
| 343 | |
| 344 void GetAllFallbackLocales(const std::string& application_locale, | |
| 345 const std::string& default_locale, | |
| 346 std::vector<std::string>* all_fallback_locales) { | |
| 347 DCHECK(all_fallback_locales); | |
| 348 if (!application_locale.empty() && application_locale != default_locale) | |
| 349 l10n_util::GetParentLocales(application_locale, all_fallback_locales); | |
| 350 all_fallback_locales->push_back(default_locale); | |
| 351 } | |
| 352 | |
| 353 bool GetValidLocales(const base::FilePath& locale_path, | |
| 354 std::set<std::string>* valid_locales, | |
| 355 std::string* error) { | |
| 356 std::set<std::string> chrome_locales; | |
| 357 GetAllLocales(&chrome_locales); | |
| 358 | |
| 359 // Enumerate all supplied locales in the extension. | |
| 360 base::FileEnumerator locales(locale_path, | |
| 361 false, | |
| 362 base::FileEnumerator::DIRECTORIES); | |
| 363 base::FilePath locale_folder; | |
| 364 while (!(locale_folder = locales.Next()).empty()) { | |
| 365 std::string locale_name = locale_folder.BaseName().MaybeAsASCII(); | |
| 366 if (locale_name.empty()) { | |
| 367 NOTREACHED(); | |
| 368 continue; // Not ASCII. | |
| 369 } | |
| 370 if (!AddLocale(chrome_locales, | |
| 371 locale_folder, | |
| 372 locale_name, | |
| 373 valid_locales, | |
| 374 error)) { | |
| 375 return false; | |
| 376 } | |
| 377 } | |
| 378 | |
| 379 if (valid_locales->empty()) { | |
| 380 *error = errors::kLocalesNoValidLocaleNamesListed; | |
| 381 return false; | |
| 382 } | |
| 383 | |
| 384 return true; | |
| 385 } | |
| 386 | |
| 387 extensions::MessageBundle* LoadMessageCatalogs( | |
| 388 const base::FilePath& locale_path, | |
| 389 const std::string& default_locale, | |
| 390 const std::string& application_locale, | |
| 391 const std::set<std::string>& valid_locales, | |
| 392 std::string* error) { | |
| 393 std::vector<std::string> all_fallback_locales; | |
| 394 GetAllFallbackLocales(application_locale, default_locale, | |
| 395 &all_fallback_locales); | |
| 396 | |
| 397 std::vector<linked_ptr<base::DictionaryValue> > catalogs; | |
| 398 for (size_t i = 0; i < all_fallback_locales.size(); ++i) { | |
| 399 // Skip all parent locales that are not supplied. | |
| 400 if (valid_locales.find(all_fallback_locales[i]) == valid_locales.end()) | |
| 401 continue; | |
| 402 linked_ptr<base::DictionaryValue> catalog( | |
| 403 LoadMessageFile(locale_path, all_fallback_locales[i], error)); | |
| 404 if (!catalog.get()) { | |
| 405 // If locale is valid, but messages.json is corrupted or missing, return | |
| 406 // an error. | |
| 407 return NULL; | |
| 408 } else { | |
| 409 catalogs.push_back(catalog); | |
| 410 } | |
| 411 } | |
| 412 | |
| 413 return extensions::MessageBundle::Create(catalogs, error); | |
| 414 } | |
| 415 | |
| 416 bool ValidateExtensionLocales(const base::FilePath& extension_path, | |
| 417 const base::DictionaryValue* manifest, | |
| 418 std::string* error) { | |
| 419 std::string default_locale = GetDefaultLocaleFromManifest(*manifest, error); | |
| 420 | |
| 421 if (default_locale.empty()) | |
| 422 return true; | |
| 423 | |
| 424 base::FilePath locale_path = | |
| 425 extension_path.Append(extensions::kLocaleFolder); | |
| 426 | |
| 427 std::set<std::string> valid_locales; | |
| 428 if (!GetValidLocales(locale_path, &valid_locales, error)) | |
| 429 return false; | |
| 430 | |
| 431 for (std::set<std::string>::const_iterator locale = valid_locales.begin(); | |
| 432 locale != valid_locales.end(); ++locale) { | |
| 433 std::string locale_error; | |
| 434 scoped_ptr<base::DictionaryValue> catalog( | |
| 435 LoadMessageFile(locale_path, *locale, &locale_error)); | |
| 436 | |
| 437 if (!locale_error.empty()) { | |
| 438 if (!error->empty()) | |
| 439 error->append(" "); | |
| 440 error->append(locale_error); | |
| 441 } | |
| 442 } | |
| 443 | |
| 444 return error->empty(); | |
| 445 } | |
| 446 | |
| 447 bool ShouldSkipValidation(const base::FilePath& locales_path, | |
| 448 const base::FilePath& locale_path, | |
| 449 const std::set<std::string>& all_locales) { | |
| 450 // Since we use this string as a key in a DictionaryValue, be paranoid about | |
| 451 // skipping any strings with '.'. This happens sometimes, for example with | |
| 452 // '.svn' directories. | |
| 453 base::FilePath relative_path; | |
| 454 if (!locales_path.AppendRelativePath(locale_path, &relative_path)) { | |
| 455 NOTREACHED(); | |
| 456 return true; | |
| 457 } | |
| 458 std::string subdir = relative_path.MaybeAsASCII(); | |
| 459 if (subdir.empty()) | |
| 460 return true; // Non-ASCII. | |
| 461 | |
| 462 if (std::find(subdir.begin(), subdir.end(), '.') != subdir.end()) | |
| 463 return true; | |
| 464 | |
| 465 if (all_locales.find(subdir) == all_locales.end()) | |
| 466 return true; | |
| 467 | |
| 468 return false; | |
| 469 } | |
| 470 | |
| 471 ScopedLocaleForTest::ScopedLocaleForTest() | |
| 472 : locale_(extension_l10n_util::CurrentLocaleOrDefault()) {} | |
| 473 | |
| 474 ScopedLocaleForTest::ScopedLocaleForTest(const std::string& locale) | |
| 475 : locale_(extension_l10n_util::CurrentLocaleOrDefault()) { | |
| 476 extension_l10n_util::SetProcessLocale(locale); | |
| 477 } | |
| 478 | |
| 479 ScopedLocaleForTest::~ScopedLocaleForTest() { | |
| 480 extension_l10n_util::SetProcessLocale(locale_); | |
| 481 } | |
| 482 | |
| 483 } // namespace extension_l10n_util | |
| OLD | NEW |