OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_file_util.h" | 5 #include "chrome/common/extensions/extension_file_util.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/json/json_file_value_serializer.h" | 11 #include "base/json/json_file_value_serializer.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
15 #include "base/scoped_temp_dir.h" | 15 #include "base/scoped_temp_dir.h" |
16 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
17 #include "base/threading/thread_restrictions.h" | 17 #include "base/threading/thread_restrictions.h" |
18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
19 #include "chrome/common/chrome_constants.h" | 19 #include "chrome/common/chrome_constants.h" |
20 #include "chrome/common/chrome_paths.h" | 20 #include "chrome/common/chrome_paths.h" |
21 #include "chrome/common/extensions/extension.h" | 21 #include "chrome/common/extensions/extension.h" |
22 #include "chrome/common/extensions/extension_action.h" | 22 #include "chrome/common/extensions/extension_action.h" |
23 #include "chrome/common/extensions/extension_l10n_util.h" | 23 #include "chrome/common/extensions/extension_l10n_util.h" |
24 #include "chrome/common/extensions/extension_manifest_constants.h" | 24 #include "chrome/common/extensions/extension_manifest_constants.h" |
25 #include "chrome/common/extensions/extension_message_bundle.h" | |
26 #include "chrome/common/extensions/extension_messages.h" | 25 #include "chrome/common/extensions/extension_messages.h" |
27 #include "chrome/common/extensions/extension_resource.h" | 26 #include "chrome/common/extensions/extension_resource.h" |
| 27 #include "chrome/common/extensions/message_bundle.h" |
28 #include "grit/generated_resources.h" | 28 #include "grit/generated_resources.h" |
29 #include "net/base/escape.h" | 29 #include "net/base/escape.h" |
30 #include "net/base/file_stream.h" | 30 #include "net/base/file_stream.h" |
31 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
32 | 32 |
33 using extensions::Extension; | 33 using extensions::Extension; |
34 | 34 |
35 namespace errors = extension_manifest_errors; | 35 namespace errors = extension_manifest_errors; |
36 | 36 |
37 namespace extension_file_util { | 37 namespace extension_file_util { |
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 version_dir = versions_enumerator.Next()) { | 475 version_dir = versions_enumerator.Next()) { |
476 if (version_dir.BaseName() != iter->second.BaseName()) { | 476 if (version_dir.BaseName() != iter->second.BaseName()) { |
477 DVLOG(1) << "Deleting old version for directory " | 477 DVLOG(1) << "Deleting old version for directory " |
478 << version_dir.LossyDisplayName() << "."; | 478 << version_dir.LossyDisplayName() << "."; |
479 file_util::Delete(version_dir, true); // Recursive. | 479 file_util::Delete(version_dir, true); // Recursive. |
480 } | 480 } |
481 } | 481 } |
482 } | 482 } |
483 } | 483 } |
484 | 484 |
485 ExtensionMessageBundle* LoadExtensionMessageBundle( | 485 extensions::MessageBundle* LoadMessageBundle( |
486 const FilePath& extension_path, | 486 const FilePath& extension_path, |
487 const std::string& default_locale, | 487 const std::string& default_locale, |
488 std::string* error) { | 488 std::string* error) { |
489 error->clear(); | 489 error->clear(); |
490 // Load locale information if available. | 490 // Load locale information if available. |
491 FilePath locale_path = extension_path.Append( | 491 FilePath locale_path = extension_path.Append( |
492 Extension::kLocaleFolder); | 492 Extension::kLocaleFolder); |
493 if (!file_util::PathExists(locale_path)) | 493 if (!file_util::PathExists(locale_path)) |
494 return NULL; | 494 return NULL; |
495 | 495 |
496 std::set<std::string> locales; | 496 std::set<std::string> locales; |
497 if (!extension_l10n_util::GetValidLocales(locale_path, &locales, error)) | 497 if (!extension_l10n_util::GetValidLocales(locale_path, &locales, error)) |
498 return NULL; | 498 return NULL; |
499 | 499 |
500 if (default_locale.empty() || | 500 if (default_locale.empty() || |
501 locales.find(default_locale) == locales.end()) { | 501 locales.find(default_locale) == locales.end()) { |
502 *error = l10n_util::GetStringUTF8( | 502 *error = l10n_util::GetStringUTF8( |
503 IDS_EXTENSION_LOCALES_NO_DEFAULT_LOCALE_SPECIFIED); | 503 IDS_EXTENSION_LOCALES_NO_DEFAULT_LOCALE_SPECIFIED); |
504 return NULL; | 504 return NULL; |
505 } | 505 } |
506 | 506 |
507 ExtensionMessageBundle* message_bundle = | 507 extensions::MessageBundle* message_bundle = |
508 extension_l10n_util::LoadMessageCatalogs( | 508 extension_l10n_util::LoadMessageCatalogs( |
509 locale_path, | 509 locale_path, |
510 default_locale, | 510 default_locale, |
511 extension_l10n_util::CurrentLocaleOrDefault(), | 511 extension_l10n_util::CurrentLocaleOrDefault(), |
512 locales, | 512 locales, |
513 error); | 513 error); |
514 | 514 |
515 return message_bundle; | 515 return message_bundle; |
516 } | 516 } |
517 | 517 |
518 SubstitutionMap* LoadExtensionMessageBundleSubstitutionMap( | 518 SubstitutionMap* LoadMessageBundleSubstitutionMap( |
519 const FilePath& extension_path, | 519 const FilePath& extension_path, |
520 const std::string& extension_id, | 520 const std::string& extension_id, |
521 const std::string& default_locale) { | 521 const std::string& default_locale) { |
522 SubstitutionMap* returnValue = new SubstitutionMap(); | 522 SubstitutionMap* returnValue = new SubstitutionMap(); |
523 if (!default_locale.empty()) { | 523 if (!default_locale.empty()) { |
524 // Touch disk only if extension is localized. | 524 // Touch disk only if extension is localized. |
525 std::string error; | 525 std::string error; |
526 scoped_ptr<ExtensionMessageBundle> bundle( | 526 scoped_ptr<extensions::MessageBundle> bundle( |
527 LoadExtensionMessageBundle(extension_path, default_locale, &error)); | 527 LoadMessageBundle(extension_path, default_locale, &error)); |
528 | 528 |
529 if (bundle.get()) | 529 if (bundle.get()) |
530 *returnValue = *bundle->dictionary(); | 530 *returnValue = *bundle->dictionary(); |
531 } | 531 } |
532 | 532 |
533 // Add @@extension_id reserved message here, so it's available to | 533 // Add @@extension_id reserved message here, so it's available to |
534 // non-localized extensions too. | 534 // non-localized extensions too. |
535 returnValue->insert( | 535 returnValue->insert( |
536 std::make_pair(ExtensionMessageBundle::kExtensionIdKey, extension_id)); | 536 std::make_pair(extensions::MessageBundle::kExtensionIdKey, extension_id)); |
537 | 537 |
538 return returnValue; | 538 return returnValue; |
539 } | 539 } |
540 | 540 |
541 static bool ValidateLocaleInfo(const Extension& extension, | 541 static bool ValidateLocaleInfo(const Extension& extension, |
542 std::string* error) { | 542 std::string* error) { |
543 // default_locale and _locales have to be both present or both missing. | 543 // default_locale and _locales have to be both present or both missing. |
544 const FilePath path = extension.path().Append( | 544 const FilePath path = extension.path().Append( |
545 Extension::kLocaleFolder); | 545 Extension::kLocaleFolder); |
546 bool path_exists = file_util::PathExists(path); | 546 bool path_exists = file_util::PathExists(path); |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
772 return temp_path; | 772 return temp_path; |
773 | 773 |
774 return FilePath(); | 774 return FilePath(); |
775 } | 775 } |
776 | 776 |
777 void DeleteFile(const FilePath& path, bool recursive) { | 777 void DeleteFile(const FilePath& path, bool recursive) { |
778 file_util::Delete(path, recursive); | 778 file_util::Delete(path, recursive); |
779 } | 779 } |
780 | 780 |
781 } // namespace extension_file_util | 781 } // namespace extension_file_util |
OLD | NEW |