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> |
| 11 #include <memory> |
11 #include <set> | 12 #include <set> |
12 #include <string> | 13 #include <string> |
13 #include <utility> | 14 #include <utility> |
14 #include <vector> | 15 #include <vector> |
15 | 16 |
16 #include "base/files/file_enumerator.h" | 17 #include "base/files/file_enumerator.h" |
17 #include "base/files/file_path.h" | 18 #include "base/files/file_path.h" |
18 #include "base/files/file_util.h" | 19 #include "base/files/file_util.h" |
19 #include "base/files/scoped_temp_dir.h" | 20 #include "base/files/scoped_temp_dir.h" |
20 #include "base/json/json_file_value_serializer.h" | 21 #include "base/json/json_file_value_serializer.h" |
21 #include "base/logging.h" | 22 #include "base/logging.h" |
22 #include "base/macros.h" | 23 #include "base/macros.h" |
23 #include "base/memory/scoped_ptr.h" | |
24 #include "base/metrics/field_trial.h" | 24 #include "base/metrics/field_trial.h" |
25 #include "base/metrics/histogram.h" | 25 #include "base/metrics/histogram.h" |
26 #include "base/strings/stringprintf.h" | 26 #include "base/strings/stringprintf.h" |
27 #include "base/strings/utf_string_conversions.h" | 27 #include "base/strings/utf_string_conversions.h" |
28 #include "base/threading/thread_restrictions.h" | 28 #include "base/threading/thread_restrictions.h" |
29 #include "base/time/time.h" | 29 #include "base/time/time.h" |
30 #include "extensions/common/constants.h" | 30 #include "extensions/common/constants.h" |
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" |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 int flags, | 200 int flags, |
201 std::string* error) { | 201 std::string* error) { |
202 return LoadExtension(extension_path, std::string(), location, flags, error); | 202 return LoadExtension(extension_path, std::string(), location, flags, error); |
203 } | 203 } |
204 | 204 |
205 scoped_refptr<Extension> LoadExtension(const base::FilePath& extension_path, | 205 scoped_refptr<Extension> LoadExtension(const base::FilePath& extension_path, |
206 const std::string& extension_id, | 206 const std::string& extension_id, |
207 Manifest::Location location, | 207 Manifest::Location location, |
208 int flags, | 208 int flags, |
209 std::string* error) { | 209 std::string* error) { |
210 scoped_ptr<base::DictionaryValue> manifest = | 210 std::unique_ptr<base::DictionaryValue> manifest = |
211 LoadManifest(extension_path, error); | 211 LoadManifest(extension_path, error); |
212 if (!manifest.get()) | 212 if (!manifest.get()) |
213 return NULL; | 213 return NULL; |
214 if (!extension_l10n_util::LocalizeExtension( | 214 if (!extension_l10n_util::LocalizeExtension( |
215 extension_path, manifest.get(), error)) { | 215 extension_path, manifest.get(), error)) { |
216 return NULL; | 216 return NULL; |
217 } | 217 } |
218 | 218 |
219 scoped_refptr<Extension> extension(Extension::Create( | 219 scoped_refptr<Extension> extension(Extension::Create( |
220 extension_path, location, *manifest, flags, extension_id, error)); | 220 extension_path, location, *manifest, flags, extension_id, error)); |
221 if (!extension.get()) | 221 if (!extension.get()) |
222 return NULL; | 222 return NULL; |
223 | 223 |
224 std::vector<InstallWarning> warnings; | 224 std::vector<InstallWarning> warnings; |
225 if (!ValidateExtension(extension.get(), error, &warnings)) | 225 if (!ValidateExtension(extension.get(), error, &warnings)) |
226 return NULL; | 226 return NULL; |
227 extension->AddInstallWarnings(warnings); | 227 extension->AddInstallWarnings(warnings); |
228 | 228 |
229 return extension; | 229 return extension; |
230 } | 230 } |
231 | 231 |
232 scoped_ptr<base::DictionaryValue> LoadManifest( | 232 std::unique_ptr<base::DictionaryValue> LoadManifest( |
233 const base::FilePath& extension_path, | 233 const base::FilePath& extension_path, |
234 std::string* error) { | 234 std::string* error) { |
235 return LoadManifest(extension_path, kManifestFilename, error); | 235 return LoadManifest(extension_path, kManifestFilename, error); |
236 } | 236 } |
237 | 237 |
238 scoped_ptr<base::DictionaryValue> LoadManifest( | 238 std::unique_ptr<base::DictionaryValue> LoadManifest( |
239 const base::FilePath& extension_path, | 239 const base::FilePath& extension_path, |
240 const base::FilePath::CharType* manifest_filename, | 240 const base::FilePath::CharType* manifest_filename, |
241 std::string* error) { | 241 std::string* error) { |
242 base::FilePath manifest_path = extension_path.Append(manifest_filename); | 242 base::FilePath manifest_path = extension_path.Append(manifest_filename); |
243 if (!base::PathExists(manifest_path)) { | 243 if (!base::PathExists(manifest_path)) { |
244 *error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE); | 244 *error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE); |
245 return NULL; | 245 return NULL; |
246 } | 246 } |
247 | 247 |
248 JSONFileValueDeserializer deserializer(manifest_path); | 248 JSONFileValueDeserializer deserializer(manifest_path); |
249 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, error)); | 249 std::unique_ptr<base::Value> root(deserializer.Deserialize(NULL, error)); |
250 if (!root.get()) { | 250 if (!root.get()) { |
251 if (error->empty()) { | 251 if (error->empty()) { |
252 // If |error| is empty, than the file could not be read. | 252 // If |error| is empty, than the file could not be read. |
253 // It would be cleaner to have the JSON reader give a specific error | 253 // It would be cleaner to have the JSON reader give a specific error |
254 // in this case, but other code tests for a file error with | 254 // in this case, but other code tests for a file error with |
255 // error->empty(). For now, be consistent. | 255 // error->empty(). For now, be consistent. |
256 *error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE); | 256 *error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE); |
257 } else { | 257 } else { |
258 *error = base::StringPrintf( | 258 *error = base::StringPrintf( |
259 "%s %s", manifest_errors::kManifestParseError, error->c_str()); | 259 "%s %s", manifest_errors::kManifestParseError, error->c_str()); |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 | 534 |
535 MessageBundle::SubstitutionMap* LoadMessageBundleSubstitutionMap( | 535 MessageBundle::SubstitutionMap* LoadMessageBundleSubstitutionMap( |
536 const base::FilePath& extension_path, | 536 const base::FilePath& extension_path, |
537 const std::string& extension_id, | 537 const std::string& extension_id, |
538 const std::string& default_locale) { | 538 const std::string& default_locale) { |
539 MessageBundle::SubstitutionMap* return_value = | 539 MessageBundle::SubstitutionMap* return_value = |
540 new MessageBundle::SubstitutionMap(); | 540 new MessageBundle::SubstitutionMap(); |
541 if (!default_locale.empty()) { | 541 if (!default_locale.empty()) { |
542 // Touch disk only if extension is localized. | 542 // Touch disk only if extension is localized. |
543 std::string error; | 543 std::string error; |
544 scoped_ptr<MessageBundle> bundle( | 544 std::unique_ptr<MessageBundle> bundle( |
545 LoadMessageBundle(extension_path, default_locale, &error)); | 545 LoadMessageBundle(extension_path, default_locale, &error)); |
546 | 546 |
547 if (bundle.get()) | 547 if (bundle.get()) |
548 *return_value = *bundle->dictionary(); | 548 *return_value = *bundle->dictionary(); |
549 } | 549 } |
550 | 550 |
551 // Add @@extension_id reserved message here, so it's available to | 551 // Add @@extension_id reserved message here, so it's available to |
552 // non-localized extensions too. | 552 // non-localized extensions too. |
553 return_value->insert( | 553 return_value->insert( |
554 std::make_pair(MessageBundle::kExtensionIdKey, extension_id)); | 554 std::make_pair(MessageBundle::kExtensionIdKey, extension_id)); |
(...skipping 20 matching lines...) Expand all Loading... |
575 return return_value; | 575 return return_value; |
576 } | 576 } |
577 | 577 |
578 // Touch disk only if extension is localized. | 578 // Touch disk only if extension is localized. |
579 default_locale = LocaleInfo::GetDefaultLocale(extension); | 579 default_locale = LocaleInfo::GetDefaultLocale(extension); |
580 if (default_locale.empty()) { | 580 if (default_locale.empty()) { |
581 return return_value; | 581 return return_value; |
582 } | 582 } |
583 | 583 |
584 std::string error; | 584 std::string error; |
585 scoped_ptr<MessageBundle> bundle( | 585 std::unique_ptr<MessageBundle> bundle( |
586 LoadMessageBundle(extension->path(), default_locale, &error)); | 586 LoadMessageBundle(extension->path(), default_locale, &error)); |
587 | 587 |
588 if (bundle.get()) { | 588 if (bundle.get()) { |
589 for (auto iter : *bundle->dictionary()) { | 589 for (auto iter : *bundle->dictionary()) { |
590 return_value->insert(std::make_pair(iter.first, iter.second)); | 590 return_value->insert(std::make_pair(iter.first, iter.second)); |
591 } | 591 } |
592 } | 592 } |
593 | 593 |
594 auto imports = extensions::SharedModuleInfo::GetImports(extension); | 594 auto imports = extensions::SharedModuleInfo::GetImports(extension); |
595 // Iterate through the imports in reverse. This will allow later imported | 595 // Iterate through the imports in reverse. This will allow later imported |
596 // modules to override earlier imported modules, as the list order is | 596 // modules to override earlier imported modules, as the list order is |
597 // maintained from the definition in manifest.json of the imports. | 597 // maintained from the definition in manifest.json of the imports. |
598 for (auto it = imports.rbegin(); it != imports.rend(); ++it) { | 598 for (auto it = imports.rbegin(); it != imports.rend(); ++it) { |
599 const extensions::Extension* imported_extension = | 599 const extensions::Extension* imported_extension = |
600 extension_set.GetByID(it->extension_id); | 600 extension_set.GetByID(it->extension_id); |
601 if (!imported_extension) { | 601 if (!imported_extension) { |
602 NOTREACHED() << "Missing shared module " << it->extension_id; | 602 NOTREACHED() << "Missing shared module " << it->extension_id; |
603 continue; | 603 continue; |
604 } | 604 } |
605 scoped_ptr<MessageBundle> imported_bundle( | 605 std::unique_ptr<MessageBundle> imported_bundle( |
606 LoadMessageBundle(imported_extension->path(), default_locale, &error)); | 606 LoadMessageBundle(imported_extension->path(), default_locale, &error)); |
607 | 607 |
608 if (imported_bundle.get()) { | 608 if (imported_bundle.get()) { |
609 for (auto iter : *imported_bundle->dictionary()) { | 609 for (auto iter : *imported_bundle->dictionary()) { |
610 // |insert| only adds new entries, and does not replace entries in | 610 // |insert| only adds new entries, and does not replace entries in |
611 // the main extension or previously processed imports. | 611 // the main extension or previously processed imports. |
612 return_value->insert(std::make_pair(iter.first, iter.second)); | 612 return_value->insert(std::make_pair(iter.first, iter.second)); |
613 } | 613 } |
614 } | 614 } |
615 } | 615 } |
616 | 616 |
617 return return_value; | 617 return return_value; |
618 } | 618 } |
619 | 619 |
620 base::FilePath GetVerifiedContentsPath(const base::FilePath& extension_path) { | 620 base::FilePath GetVerifiedContentsPath(const base::FilePath& extension_path) { |
621 return extension_path.Append(kMetadataFolder) | 621 return extension_path.Append(kMetadataFolder) |
622 .Append(kVerifiedContentsFilename); | 622 .Append(kVerifiedContentsFilename); |
623 } | 623 } |
624 base::FilePath GetComputedHashesPath(const base::FilePath& extension_path) { | 624 base::FilePath GetComputedHashesPath(const base::FilePath& extension_path) { |
625 return extension_path.Append(kMetadataFolder).Append(kComputedHashesFilename); | 625 return extension_path.Append(kMetadataFolder).Append(kComputedHashesFilename); |
626 } | 626 } |
627 | 627 |
628 } // namespace file_util | 628 } // namespace file_util |
629 } // namespace extensions | 629 } // namespace extensions |
OLD | NEW |