OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/common/extension_l10n_util.h" | 5 #include "extensions/common/extension_l10n_util.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/files/file_enumerator.h" | 14 #include "base/files/file_enumerator.h" |
15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
16 #include "base/json/json_file_value_serializer.h" | 16 #include "base/json/json_file_value_serializer.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/memory/linked_ptr.h" | |
19 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
20 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
21 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
22 #include "base/values.h" | 21 #include "base/values.h" |
23 #include "extensions/common/constants.h" | 22 #include "extensions/common/constants.h" |
24 #include "extensions/common/error_utils.h" | 23 #include "extensions/common/error_utils.h" |
25 #include "extensions/common/file_util.h" | 24 #include "extensions/common/file_util.h" |
26 #include "extensions/common/manifest_constants.h" | 25 #include "extensions/common/manifest_constants.h" |
27 #include "extensions/common/message_bundle.h" | 26 #include "extensions/common/message_bundle.h" |
28 #include "third_party/icu/source/common/unicode/uloc.h" | 27 #include "third_party/icu/source/common/unicode/uloc.h" |
29 #include "ui/base/l10n/l10n_util.h" | 28 #include "ui/base/l10n/l10n_util.h" |
30 | 29 |
31 namespace errors = extensions::manifest_errors; | 30 namespace errors = extensions::manifest_errors; |
32 namespace keys = extensions::manifest_keys; | 31 namespace keys = extensions::manifest_keys; |
33 | 32 |
34 namespace { | 33 namespace { |
35 | 34 |
36 // Loads contents of the messages file for given locale. If file is not found, | 35 // Loads contents of the messages file for given locale. If file is not found, |
37 // or there was parsing error we return NULL and set |error|. | 36 // or there was parsing error we return NULL and set |error|. |
38 // Caller owns the returned object. | 37 // Caller owns the returned object. |
39 base::DictionaryValue* LoadMessageFile(const base::FilePath& locale_path, | 38 std::unique_ptr<base::DictionaryValue> LoadMessageFile( |
40 const std::string& locale, | 39 const base::FilePath& locale_path, |
41 std::string* error) { | 40 const std::string& locale, |
| 41 std::string* error) { |
42 base::FilePath file = | 42 base::FilePath file = |
43 locale_path.AppendASCII(locale).Append(extensions::kMessagesFilename); | 43 locale_path.AppendASCII(locale).Append(extensions::kMessagesFilename); |
44 JSONFileValueDeserializer messages_deserializer(file); | 44 JSONFileValueDeserializer messages_deserializer(file); |
45 std::unique_ptr<base::DictionaryValue> dictionary = | 45 std::unique_ptr<base::DictionaryValue> dictionary = |
46 base::DictionaryValue::From( | 46 base::DictionaryValue::From( |
47 messages_deserializer.Deserialize(NULL, error)); | 47 messages_deserializer.Deserialize(NULL, error)); |
48 if (!dictionary) { | 48 if (!dictionary) { |
49 if (error->empty()) { | 49 if (error->empty()) { |
50 // JSONFileValueSerializer just returns NULL if file cannot be found. It | 50 // JSONFileValueSerializer just returns NULL if file cannot be found. It |
51 // doesn't set the error, so we have to do it. | 51 // doesn't set the error, so we have to do it. |
52 *error = base::StringPrintf("Catalog file is missing for locale %s.", | 52 *error = base::StringPrintf("Catalog file is missing for locale %s.", |
53 locale.c_str()); | 53 locale.c_str()); |
54 } else { | 54 } else { |
55 *error = extensions::ErrorUtils::FormatErrorMessage( | 55 *error = extensions::ErrorUtils::FormatErrorMessage( |
56 errors::kLocalesInvalidLocale, | 56 errors::kLocalesInvalidLocale, |
57 base::UTF16ToUTF8(file.LossyDisplayName()), | 57 base::UTF16ToUTF8(file.LossyDisplayName()), |
58 *error); | 58 *error); |
59 } | 59 } |
60 } | 60 } |
61 | 61 |
62 return dictionary.release(); | 62 return dictionary; |
63 } | 63 } |
64 | 64 |
65 // Localizes manifest value of string type for a given key. | 65 // Localizes manifest value of string type for a given key. |
66 bool LocalizeManifestValue(const std::string& key, | 66 bool LocalizeManifestValue(const std::string& key, |
67 const extensions::MessageBundle& messages, | 67 const extensions::MessageBundle& messages, |
68 base::DictionaryValue* manifest, | 68 base::DictionaryValue* manifest, |
69 std::string* error) { | 69 std::string* error) { |
70 std::string result; | 70 std::string result; |
71 if (!manifest->GetString(key, &result)) | 71 if (!manifest->GetString(key, &result)) |
72 return true; | 72 return true; |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 | 369 |
370 extensions::MessageBundle* LoadMessageCatalogs( | 370 extensions::MessageBundle* LoadMessageCatalogs( |
371 const base::FilePath& locale_path, | 371 const base::FilePath& locale_path, |
372 const std::string& default_locale, | 372 const std::string& default_locale, |
373 const std::string& application_locale, | 373 const std::string& application_locale, |
374 std::string* error) { | 374 std::string* error) { |
375 std::vector<std::string> all_fallback_locales; | 375 std::vector<std::string> all_fallback_locales; |
376 GetAllFallbackLocales( | 376 GetAllFallbackLocales( |
377 application_locale, default_locale, &all_fallback_locales); | 377 application_locale, default_locale, &all_fallback_locales); |
378 | 378 |
379 std::vector<linked_ptr<base::DictionaryValue> > catalogs; | 379 std::vector<std::unique_ptr<base::DictionaryValue>> catalogs; |
380 for (size_t i = 0; i < all_fallback_locales.size(); ++i) { | 380 for (size_t i = 0; i < all_fallback_locales.size(); ++i) { |
381 // Skip all parent locales that are not supplied. | 381 // Skip all parent locales that are not supplied. |
382 base::FilePath this_locale_path = | 382 base::FilePath this_locale_path = |
383 locale_path.AppendASCII(all_fallback_locales[i]); | 383 locale_path.AppendASCII(all_fallback_locales[i]); |
384 if (!base::PathExists(this_locale_path)) | 384 if (!base::PathExists(this_locale_path)) |
385 continue; | 385 continue; |
386 linked_ptr<base::DictionaryValue> catalog( | 386 std::unique_ptr<base::DictionaryValue> catalog = |
387 LoadMessageFile(locale_path, all_fallback_locales[i], error)); | 387 LoadMessageFile(locale_path, all_fallback_locales[i], error); |
388 if (!catalog.get()) { | 388 if (!catalog.get()) { |
389 // If locale is valid, but messages.json is corrupted or missing, return | 389 // If locale is valid, but messages.json is corrupted or missing, return |
390 // an error. | 390 // an error. |
391 return NULL; | 391 return nullptr; |
392 } else { | 392 } else { |
393 catalogs.push_back(catalog); | 393 catalogs.push_back(std::move(catalog)); |
394 } | 394 } |
395 } | 395 } |
396 | 396 |
397 return extensions::MessageBundle::Create(catalogs, error); | 397 return extensions::MessageBundle::Create(catalogs, error); |
398 } | 398 } |
399 | 399 |
400 bool ValidateExtensionLocales(const base::FilePath& extension_path, | 400 bool ValidateExtensionLocales(const base::FilePath& extension_path, |
401 const base::DictionaryValue* manifest, | 401 const base::DictionaryValue* manifest, |
402 std::string* error) { | 402 std::string* error) { |
403 std::string default_locale = GetDefaultLocaleFromManifest(*manifest, error); | 403 std::string default_locale = GetDefaultLocaleFromManifest(*manifest, error); |
404 | 404 |
405 if (default_locale.empty()) | 405 if (default_locale.empty()) |
406 return true; | 406 return true; |
407 | 407 |
408 base::FilePath locale_path = extension_path.Append(extensions::kLocaleFolder); | 408 base::FilePath locale_path = extension_path.Append(extensions::kLocaleFolder); |
409 | 409 |
410 std::set<std::string> valid_locales; | 410 std::set<std::string> valid_locales; |
411 if (!GetValidLocales(locale_path, &valid_locales, error)) | 411 if (!GetValidLocales(locale_path, &valid_locales, error)) |
412 return false; | 412 return false; |
413 | 413 |
414 for (std::set<std::string>::const_iterator locale = valid_locales.begin(); | 414 for (std::set<std::string>::const_iterator locale = valid_locales.begin(); |
415 locale != valid_locales.end(); | 415 locale != valid_locales.end(); |
416 ++locale) { | 416 ++locale) { |
417 std::string locale_error; | 417 std::string locale_error; |
418 std::unique_ptr<base::DictionaryValue> catalog( | 418 std::unique_ptr<base::DictionaryValue> catalog = |
419 LoadMessageFile(locale_path, *locale, &locale_error)); | 419 LoadMessageFile(locale_path, *locale, &locale_error); |
420 | 420 |
421 if (!locale_error.empty()) { | 421 if (!locale_error.empty()) { |
422 if (!error->empty()) | 422 if (!error->empty()) |
423 error->append(" "); | 423 error->append(" "); |
424 error->append(locale_error); | 424 error->append(locale_error); |
425 } | 425 } |
426 } | 426 } |
427 | 427 |
428 return error->empty(); | 428 return error->empty(); |
429 } | 429 } |
(...skipping 28 matching lines...) Expand all Loading... |
458 ScopedLocaleForTest::ScopedLocaleForTest(const std::string& locale) | 458 ScopedLocaleForTest::ScopedLocaleForTest(const std::string& locale) |
459 : locale_(extension_l10n_util::CurrentLocaleOrDefault()) { | 459 : locale_(extension_l10n_util::CurrentLocaleOrDefault()) { |
460 extension_l10n_util::SetProcessLocale(locale); | 460 extension_l10n_util::SetProcessLocale(locale); |
461 } | 461 } |
462 | 462 |
463 ScopedLocaleForTest::~ScopedLocaleForTest() { | 463 ScopedLocaleForTest::~ScopedLocaleForTest() { |
464 extension_l10n_util::SetProcessLocale(locale_); | 464 extension_l10n_util::SetProcessLocale(locale_); |
465 } | 465 } |
466 | 466 |
467 } // namespace extension_l10n_util | 467 } // namespace extension_l10n_util |
OLD | NEW |