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" |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "chrome/common/extensions/extension_l10n_util.h" | 22 #include "chrome/common/extensions/extension_l10n_util.h" |
23 #include "chrome/common/extensions/extension_manifest_constants.h" | 23 #include "chrome/common/extensions/extension_manifest_constants.h" |
24 #include "chrome/common/extensions/extension_message_bundle.h" | 24 #include "chrome/common/extensions/extension_message_bundle.h" |
25 #include "chrome/common/extensions/extension_messages.h" | 25 #include "chrome/common/extensions/extension_messages.h" |
26 #include "chrome/common/extensions/extension_resource.h" | 26 #include "chrome/common/extensions/extension_resource.h" |
27 #include "grit/generated_resources.h" | 27 #include "grit/generated_resources.h" |
28 #include "net/base/escape.h" | 28 #include "net/base/escape.h" |
29 #include "net/base/file_stream.h" | 29 #include "net/base/file_stream.h" |
30 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
31 | 31 |
| 32 using extensions::Extension; |
| 33 |
32 namespace errors = extension_manifest_errors; | 34 namespace errors = extension_manifest_errors; |
33 | 35 |
34 namespace extension_file_util { | 36 namespace extension_file_util { |
35 | 37 |
36 // Validates locale info. Doesn't check if messages.json files are valid. | 38 // Validates locale info. Doesn't check if messages.json files are valid. |
37 static bool ValidateLocaleInfo(const Extension& extension, std::string* error); | 39 static bool ValidateLocaleInfo(const Extension& extension, |
| 40 std::string* error); |
38 | 41 |
39 // Returns false and sets the error if script file can't be loaded, | 42 // Returns false and sets the error if script file can't be loaded, |
40 // or if it's not UTF-8 encoded. | 43 // or if it's not UTF-8 encoded. |
41 static bool IsScriptValid(const FilePath& path, const FilePath& relative_path, | 44 static bool IsScriptValid(const FilePath& path, const FilePath& relative_path, |
42 int message_id, std::string* error); | 45 int message_id, std::string* error); |
43 | 46 |
44 const char kInstallDirectoryName[] = "Extensions"; | 47 const char kInstallDirectoryName[] = "Extensions"; |
45 | 48 |
46 FilePath InstallExtension(const FilePath& unpacked_source_dir, | 49 FilePath InstallExtension(const FilePath& unpacked_source_dir, |
47 const std::string& id, | 50 const std::string& id, |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 // We don't care about the return value. If this fails (and it can, due to | 120 // We don't care about the return value. If this fails (and it can, due to |
118 // plugins that aren't unloaded yet, it will get cleaned up by | 121 // plugins that aren't unloaded yet, it will get cleaned up by |
119 // ExtensionService::GarbageCollectExtensions). | 122 // ExtensionService::GarbageCollectExtensions). |
120 file_util::Delete(extensions_dir.AppendASCII(id), true); // recursive. | 123 file_util::Delete(extensions_dir.AppendASCII(id), true); // recursive. |
121 } | 124 } |
122 | 125 |
123 scoped_refptr<Extension> LoadExtension(const FilePath& extension_path, | 126 scoped_refptr<Extension> LoadExtension(const FilePath& extension_path, |
124 Extension::Location location, | 127 Extension::Location location, |
125 int flags, | 128 int flags, |
126 std::string* error) { | 129 std::string* error) { |
127 return LoadExtension( | 130 return LoadExtension(extension_path, std::string(), location, flags, error); |
128 extension_path, std::string(), location, flags, error); | |
129 } | 131 } |
130 | 132 |
131 scoped_refptr<Extension> LoadExtension(const FilePath& extension_path, | 133 scoped_refptr<Extension> LoadExtension(const FilePath& extension_path, |
132 const std::string& extension_id, | 134 const std::string& extension_id, |
133 Extension::Location location, | 135 Extension::Location location, |
134 int flags, | 136 int flags, |
135 std::string* error) { | 137 std::string* error) { |
136 scoped_ptr<DictionaryValue> manifest(LoadManifest(extension_path, error)); | 138 scoped_ptr<DictionaryValue> manifest(LoadManifest(extension_path, error)); |
137 if (!manifest.get()) | 139 if (!manifest.get()) |
138 return NULL; | 140 return NULL; |
139 if (!extension_l10n_util::LocalizeExtension(extension_path, manifest.get(), | 141 if (!extension_l10n_util::LocalizeExtension(extension_path, manifest.get(), |
140 error)) | 142 error)) |
141 return NULL; | 143 return NULL; |
142 | 144 |
143 scoped_refptr<Extension> extension(Extension::Create( | 145 scoped_refptr<Extension> extension(Extension::Create(extension_path, |
144 extension_path, | 146 location, |
145 location, | 147 *manifest, |
146 *manifest, | 148 flags, |
147 flags, | 149 extension_id, |
148 extension_id, | 150 error)); |
149 error)); | |
150 if (!extension.get()) | 151 if (!extension.get()) |
151 return NULL; | 152 return NULL; |
152 | 153 |
153 if (!ValidateExtension(extension.get(), error)) | 154 if (!ValidateExtension(extension.get(), error)) |
154 return NULL; | 155 return NULL; |
155 | 156 |
156 return extension; | 157 return extension; |
157 } | 158 } |
158 | 159 |
159 DictionaryValue* LoadManifest(const FilePath& extension_path, | 160 DictionaryValue* LoadManifest(const FilePath& extension_path, |
(...skipping 23 matching lines...) Expand all Loading... |
183 } | 184 } |
184 | 185 |
185 if (!root->IsType(Value::TYPE_DICTIONARY)) { | 186 if (!root->IsType(Value::TYPE_DICTIONARY)) { |
186 *error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_INVALID); | 187 *error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_INVALID); |
187 return NULL; | 188 return NULL; |
188 } | 189 } |
189 | 190 |
190 return static_cast<DictionaryValue*>(root.release()); | 191 return static_cast<DictionaryValue*>(root.release()); |
191 } | 192 } |
192 | 193 |
193 bool ValidateExtension(const Extension* extension, std::string* error) { | 194 bool ValidateExtension(const Extension* extension, |
| 195 std::string* error) { |
194 // Validate icons exist. | 196 // Validate icons exist. |
195 for (ExtensionIconSet::IconMap::const_iterator iter = | 197 for (ExtensionIconSet::IconMap::const_iterator iter = |
196 extension->icons().map().begin(); | 198 extension->icons().map().begin(); |
197 iter != extension->icons().map().end(); | 199 iter != extension->icons().map().end(); |
198 ++iter) { | 200 ++iter) { |
199 const FilePath path = extension->GetResource(iter->second).GetFilePath(); | 201 const FilePath path = extension->GetResource(iter->second).GetFilePath(); |
200 if (!file_util::PathExists(path)) { | 202 if (!file_util::PathExists(path)) { |
201 *error = | 203 *error = |
202 l10n_util::GetStringFUTF8(IDS_EXTENSION_LOAD_ICON_FAILED, | 204 l10n_util::GetStringFUTF8(IDS_EXTENSION_LOAD_ICON_FAILED, |
203 UTF8ToUTF16(iter->second)); | 205 UTF8ToUTF16(iter->second)); |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 } | 430 } |
429 } | 431 } |
430 } | 432 } |
431 | 433 |
432 ExtensionMessageBundle* LoadExtensionMessageBundle( | 434 ExtensionMessageBundle* LoadExtensionMessageBundle( |
433 const FilePath& extension_path, | 435 const FilePath& extension_path, |
434 const std::string& default_locale, | 436 const std::string& default_locale, |
435 std::string* error) { | 437 std::string* error) { |
436 error->clear(); | 438 error->clear(); |
437 // Load locale information if available. | 439 // Load locale information if available. |
438 FilePath locale_path = extension_path.Append(Extension::kLocaleFolder); | 440 FilePath locale_path = extension_path.Append( |
| 441 Extension::kLocaleFolder); |
439 if (!file_util::PathExists(locale_path)) | 442 if (!file_util::PathExists(locale_path)) |
440 return NULL; | 443 return NULL; |
441 | 444 |
442 std::set<std::string> locales; | 445 std::set<std::string> locales; |
443 if (!extension_l10n_util::GetValidLocales(locale_path, &locales, error)) | 446 if (!extension_l10n_util::GetValidLocales(locale_path, &locales, error)) |
444 return NULL; | 447 return NULL; |
445 | 448 |
446 if (default_locale.empty() || | 449 if (default_locale.empty() || |
447 locales.find(default_locale) == locales.end()) { | 450 locales.find(default_locale) == locales.end()) { |
448 *error = l10n_util::GetStringUTF8( | 451 *error = l10n_util::GetStringUTF8( |
(...skipping 28 matching lines...) Expand all Loading... |
477 } | 480 } |
478 | 481 |
479 // Add @@extension_id reserved message here, so it's available to | 482 // Add @@extension_id reserved message here, so it's available to |
480 // non-localized extensions too. | 483 // non-localized extensions too. |
481 returnValue->insert( | 484 returnValue->insert( |
482 std::make_pair(ExtensionMessageBundle::kExtensionIdKey, extension_id)); | 485 std::make_pair(ExtensionMessageBundle::kExtensionIdKey, extension_id)); |
483 | 486 |
484 return returnValue; | 487 return returnValue; |
485 } | 488 } |
486 | 489 |
487 static bool ValidateLocaleInfo(const Extension& extension, std::string* error) { | 490 static bool ValidateLocaleInfo(const Extension& extension, |
| 491 std::string* error) { |
488 // default_locale and _locales have to be both present or both missing. | 492 // default_locale and _locales have to be both present or both missing. |
489 const FilePath path = extension.path().Append(Extension::kLocaleFolder); | 493 const FilePath path = extension.path().Append( |
| 494 Extension::kLocaleFolder); |
490 bool path_exists = file_util::PathExists(path); | 495 bool path_exists = file_util::PathExists(path); |
491 std::string default_locale = extension.default_locale(); | 496 std::string default_locale = extension.default_locale(); |
492 | 497 |
493 // If both default locale and _locales folder are empty, skip verification. | 498 // If both default locale and _locales folder are empty, skip verification. |
494 if (default_locale.empty() && !path_exists) | 499 if (default_locale.empty() && !path_exists) |
495 return true; | 500 return true; |
496 | 501 |
497 if (default_locale.empty() && path_exists) { | 502 if (default_locale.empty() && path_exists) { |
498 *error = l10n_util::GetStringUTF8( | 503 *error = l10n_util::GetStringUTF8( |
499 IDS_EXTENSION_LOCALES_NO_DEFAULT_LOCALE_SPECIFIED); | 504 IDS_EXTENSION_LOCALES_NO_DEFAULT_LOCALE_SPECIFIED); |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
716 return temp_path; | 721 return temp_path; |
717 | 722 |
718 return FilePath(); | 723 return FilePath(); |
719 } | 724 } |
720 | 725 |
721 void DeleteFile(const FilePath& path, bool recursive) { | 726 void DeleteFile(const FilePath& path, bool recursive) { |
722 file_util::Delete(path, recursive); | 727 file_util::Delete(path, recursive); |
723 } | 728 } |
724 | 729 |
725 } // namespace extension_file_util | 730 } // namespace extension_file_util |
OLD | NEW |