OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/file_util.h" | 5 #include "extensions/common/file_util.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 20 matching lines...) Expand all Loading... |
31 #include "extensions/common/extension.h" | 31 #include "extensions/common/extension.h" |
32 #include "extensions/common/extension_icon_set.h" | 32 #include "extensions/common/extension_icon_set.h" |
33 #include "extensions/common/extension_l10n_util.h" | 33 #include "extensions/common/extension_l10n_util.h" |
34 #include "extensions/common/extension_set.h" | 34 #include "extensions/common/extension_set.h" |
35 #include "extensions/common/install_warning.h" | 35 #include "extensions/common/install_warning.h" |
36 #include "extensions/common/manifest.h" | 36 #include "extensions/common/manifest.h" |
37 #include "extensions/common/manifest_constants.h" | 37 #include "extensions/common/manifest_constants.h" |
38 #include "extensions/common/manifest_handler.h" | 38 #include "extensions/common/manifest_handler.h" |
39 #include "extensions/common/manifest_handlers/default_locale_handler.h" | 39 #include "extensions/common/manifest_handlers/default_locale_handler.h" |
40 #include "extensions/common/manifest_handlers/icons_handler.h" | 40 #include "extensions/common/manifest_handlers/icons_handler.h" |
41 #include "extensions/common/manifest_handlers/shared_module_info.h" | |
42 #include "grit/extensions_strings.h" | 41 #include "grit/extensions_strings.h" |
43 #include "net/base/escape.h" | 42 #include "net/base/escape.h" |
44 #include "net/base/filename_util.h" | 43 #include "net/base/filename_util.h" |
45 #include "ui/base/l10n/l10n_util.h" | 44 #include "ui/base/l10n/l10n_util.h" |
46 #include "url/gurl.h" | 45 #include "url/gurl.h" |
47 | 46 |
48 namespace extensions { | 47 namespace extensions { |
49 namespace file_util { | 48 namespace file_util { |
50 namespace { | 49 namespace { |
51 | 50 |
(...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
507 extension_l10n_util::CurrentLocaleOrDefault(), | 506 extension_l10n_util::CurrentLocaleOrDefault(), |
508 error); | 507 error); |
509 | 508 |
510 return message_bundle; | 509 return message_bundle; |
511 } | 510 } |
512 | 511 |
513 MessageBundle::SubstitutionMap* LoadMessageBundleSubstitutionMap( | 512 MessageBundle::SubstitutionMap* LoadMessageBundleSubstitutionMap( |
514 const base::FilePath& extension_path, | 513 const base::FilePath& extension_path, |
515 const std::string& extension_id, | 514 const std::string& extension_id, |
516 const std::string& default_locale) { | 515 const std::string& default_locale) { |
| 516 return LoadMessageBundleSubstitutionMapFromPaths( |
| 517 {extension_path}, extension_id, default_locale); |
| 518 } |
| 519 |
| 520 MessageBundle::SubstitutionMap* LoadNonLocalizedMessageBundleSubstitutionMap( |
| 521 const std::string& extension_id) { |
517 MessageBundle::SubstitutionMap* return_value = | 522 MessageBundle::SubstitutionMap* return_value = |
518 new MessageBundle::SubstitutionMap(); | 523 new MessageBundle::SubstitutionMap(); |
519 if (!default_locale.empty()) { | |
520 // Touch disk only if extension is localized. | |
521 std::string error; | |
522 std::unique_ptr<MessageBundle> bundle( | |
523 LoadMessageBundle(extension_path, default_locale, &error)); | |
524 | 524 |
525 if (bundle.get()) | 525 // Add @@extension_id reserved message here. |
526 *return_value = *bundle->dictionary(); | |
527 } | |
528 | |
529 // Add @@extension_id reserved message here, so it's available to | |
530 // non-localized extensions too. | |
531 return_value->insert( | 526 return_value->insert( |
532 std::make_pair(MessageBundle::kExtensionIdKey, extension_id)); | 527 std::make_pair(MessageBundle::kExtensionIdKey, extension_id)); |
533 | 528 |
534 return return_value; | 529 return return_value; |
535 } | 530 } |
536 | 531 |
537 MessageBundle::SubstitutionMap* LoadMessageBundleSubstitutionMapWithImports( | 532 MessageBundle::SubstitutionMap* LoadMessageBundleSubstitutionMapFromPaths( |
| 533 const std::vector<base::FilePath>& paths, |
538 const std::string& extension_id, | 534 const std::string& extension_id, |
539 const ExtensionSet& extension_set) { | 535 const std::string& default_locale) { |
540 const Extension* extension = extension_set.GetByID(extension_id); | |
541 MessageBundle::SubstitutionMap* return_value = | 536 MessageBundle::SubstitutionMap* return_value = |
542 new MessageBundle::SubstitutionMap(); | 537 LoadNonLocalizedMessageBundleSubstitutionMap(extension_id); |
543 | |
544 // Add @@extension_id reserved message here, so it's available to | |
545 // non-localized extensions too. | |
546 return_value->insert( | |
547 std::make_pair(MessageBundle::kExtensionIdKey, extension_id)); | |
548 | |
549 base::FilePath extension_path; | |
550 std::string default_locale; | |
551 if (!extension) { | |
552 NOTREACHED() << "Missing extension " << extension_id; | |
553 return return_value; | |
554 } | |
555 | 538 |
556 // Touch disk only if extension is localized. | 539 // Touch disk only if extension is localized. |
557 default_locale = LocaleInfo::GetDefaultLocale(extension); | 540 if (default_locale.empty()) |
558 if (default_locale.empty()) { | |
559 return return_value; | 541 return return_value; |
560 } | |
561 | 542 |
562 std::string error; | 543 std::string error; |
563 std::unique_ptr<MessageBundle> bundle( | 544 for (const base::FilePath& path : paths) { |
564 LoadMessageBundle(extension->path(), default_locale, &error)); | 545 std::unique_ptr<MessageBundle> bundle( |
| 546 LoadMessageBundle(path, default_locale, &error)); |
565 | 547 |
566 if (bundle.get()) { | 548 if (bundle) { |
567 for (auto iter : *bundle->dictionary()) { | 549 for (const auto& iter : *bundle->dictionary()) { |
568 return_value->insert(std::make_pair(iter.first, iter.second)); | |
569 } | |
570 } | |
571 | |
572 auto imports = extensions::SharedModuleInfo::GetImports(extension); | |
573 // Iterate through the imports in reverse. This will allow later imported | |
574 // modules to override earlier imported modules, as the list order is | |
575 // maintained from the definition in manifest.json of the imports. | |
576 for (auto it = imports.rbegin(); it != imports.rend(); ++it) { | |
577 const extensions::Extension* imported_extension = | |
578 extension_set.GetByID(it->extension_id); | |
579 if (!imported_extension) { | |
580 NOTREACHED() << "Missing shared module " << it->extension_id; | |
581 continue; | |
582 } | |
583 std::unique_ptr<MessageBundle> imported_bundle( | |
584 LoadMessageBundle(imported_extension->path(), default_locale, &error)); | |
585 | |
586 if (imported_bundle.get()) { | |
587 for (auto iter : *imported_bundle->dictionary()) { | |
588 // |insert| only adds new entries, and does not replace entries in | 550 // |insert| only adds new entries, and does not replace entries in |
589 // the main extension or previously processed imports. | 551 // the main extension or previously processed imports. |
590 return_value->insert(std::make_pair(iter.first, iter.second)); | 552 return_value->insert(std::make_pair(iter.first, iter.second)); |
591 } | 553 } |
592 } | 554 } |
593 } | 555 } |
594 | 556 |
595 return return_value; | 557 return return_value; |
596 } | 558 } |
597 | 559 |
598 base::FilePath GetVerifiedContentsPath(const base::FilePath& extension_path) { | 560 base::FilePath GetVerifiedContentsPath(const base::FilePath& extension_path) { |
599 return extension_path.Append(kMetadataFolder) | 561 return extension_path.Append(kMetadataFolder) |
600 .Append(kVerifiedContentsFilename); | 562 .Append(kVerifiedContentsFilename); |
601 } | 563 } |
602 base::FilePath GetComputedHashesPath(const base::FilePath& extension_path) { | 564 base::FilePath GetComputedHashesPath(const base::FilePath& extension_path) { |
603 return extension_path.Append(kMetadataFolder).Append(kComputedHashesFilename); | 565 return extension_path.Append(kMetadataFolder).Append(kComputedHashesFilename); |
604 } | 566 } |
605 | 567 |
606 } // namespace file_util | 568 } // namespace file_util |
607 } // namespace extensions | 569 } // namespace extensions |
OLD | NEW |