| 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_path.h" | 10 #include "base/file_path.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/json/json_file_value_serializer.h" | 13 #include "base/json/json_file_value_serializer.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
| 18 #include "base/threading/thread_restrictions.h" | 18 #include "base/threading/thread_restrictions.h" |
| 19 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
| 20 #include "chrome/common/chrome_constants.h" | 20 #include "chrome/common/chrome_constants.h" |
| 21 #include "chrome/common/chrome_paths.h" | 21 #include "chrome/common/chrome_paths.h" |
| 22 #include "chrome/common/extensions/api/extension_action/action_info.h" | 22 #include "chrome/common/extensions/api/extension_action/action_info.h" |
| 23 #include "chrome/common/extensions/api/extension_action/browser_action_handler.h
" | 23 #include "chrome/common/extensions/api/extension_action/browser_action_handler.h
" |
| 24 #include "chrome/common/extensions/api/i18n/default_locale_handler.h" | 24 #include "chrome/common/extensions/api/i18n/default_locale_handler.h" |
| 25 #include "chrome/common/extensions/extension.h" | 25 #include "chrome/common/extensions/extension.h" |
| 26 #include "chrome/common/extensions/extension_l10n_util.h" | 26 #include "chrome/common/extensions/extension_l10n_util.h" |
| 27 #include "chrome/common/extensions/extension_manifest_constants.h" | 27 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 28 #include "chrome/common/extensions/extension_messages.h" | 28 #include "chrome/common/extensions/extension_messages.h" |
| 29 #include "chrome/common/extensions/extension_resource.h" | 29 #include "chrome/common/extensions/extension_resource.h" |
| 30 #include "chrome/common/extensions/manifest.h" |
| 30 #include "chrome/common/extensions/manifest_url_handler.h" | 31 #include "chrome/common/extensions/manifest_url_handler.h" |
| 31 #include "chrome/common/extensions/message_bundle.h" | 32 #include "chrome/common/extensions/message_bundle.h" |
| 33 #include "extensions/common/install_warning.h" |
| 32 #include "grit/generated_resources.h" | 34 #include "grit/generated_resources.h" |
| 33 #include "net/base/escape.h" | 35 #include "net/base/escape.h" |
| 34 #include "net/base/file_stream.h" | 36 #include "net/base/file_stream.h" |
| 35 #include "ui/base/l10n/l10n_util.h" | 37 #include "ui/base/l10n/l10n_util.h" |
| 36 | 38 |
| 37 using extensions::Extension; | 39 using extensions::Extension; |
| 40 using extensions::Manifest; |
| 38 | 41 |
| 39 namespace errors = extension_manifest_errors; | 42 namespace errors = extension_manifest_errors; |
| 40 | 43 |
| 41 namespace { | 44 namespace { |
| 42 | 45 |
| 43 const FilePath::CharType kTempDirectoryName[] = FILE_PATH_LITERAL("Temp"); | 46 const FilePath::CharType kTempDirectoryName[] = FILE_PATH_LITERAL("Temp"); |
| 44 | 47 |
| 45 bool ValidateExtensionIconSet(const ExtensionIconSet& icon_set, | 48 bool ValidateExtensionIconSet(const ExtensionIconSet& icon_set, |
| 46 const Extension* extension, | 49 const Extension* extension, |
| 47 int error_message_id, | 50 int error_message_id, |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 | 133 |
| 131 void UninstallExtension(const FilePath& extensions_dir, | 134 void UninstallExtension(const FilePath& extensions_dir, |
| 132 const std::string& id) { | 135 const std::string& id) { |
| 133 // We don't care about the return value. If this fails (and it can, due to | 136 // We don't care about the return value. If this fails (and it can, due to |
| 134 // plugins that aren't unloaded yet, it will get cleaned up by | 137 // plugins that aren't unloaded yet, it will get cleaned up by |
| 135 // ExtensionService::GarbageCollectExtensions). | 138 // ExtensionService::GarbageCollectExtensions). |
| 136 file_util::Delete(extensions_dir.AppendASCII(id), true); // recursive. | 139 file_util::Delete(extensions_dir.AppendASCII(id), true); // recursive. |
| 137 } | 140 } |
| 138 | 141 |
| 139 scoped_refptr<Extension> LoadExtension(const FilePath& extension_path, | 142 scoped_refptr<Extension> LoadExtension(const FilePath& extension_path, |
| 140 Extension::Location location, | 143 Manifest::Location location, |
| 141 int flags, | 144 int flags, |
| 142 std::string* error) { | 145 std::string* error) { |
| 143 return LoadExtension(extension_path, std::string(), location, flags, error); | 146 return LoadExtension(extension_path, std::string(), location, flags, error); |
| 144 } | 147 } |
| 145 | 148 |
| 146 scoped_refptr<Extension> LoadExtension(const FilePath& extension_path, | 149 scoped_refptr<Extension> LoadExtension(const FilePath& extension_path, |
| 147 const std::string& extension_id, | 150 const std::string& extension_id, |
| 148 Extension::Location location, | 151 Manifest::Location location, |
| 149 int flags, | 152 int flags, |
| 150 std::string* error) { | 153 std::string* error) { |
| 151 scoped_ptr<DictionaryValue> manifest(LoadManifest(extension_path, error)); | 154 scoped_ptr<DictionaryValue> manifest(LoadManifest(extension_path, error)); |
| 152 if (!manifest.get()) | 155 if (!manifest.get()) |
| 153 return NULL; | 156 return NULL; |
| 154 if (!extension_l10n_util::LocalizeExtension(extension_path, manifest.get(), | 157 if (!extension_l10n_util::LocalizeExtension(extension_path, manifest.get(), |
| 155 error)) { | 158 error)) { |
| 156 return NULL; | 159 return NULL; |
| 157 } | 160 } |
| 158 | 161 |
| 159 scoped_refptr<Extension> extension(Extension::Create(extension_path, | 162 scoped_refptr<Extension> extension(Extension::Create(extension_path, |
| 160 location, | 163 location, |
| 161 *manifest, | 164 *manifest, |
| 162 flags, | 165 flags, |
| 163 extension_id, | 166 extension_id, |
| 164 error)); | 167 error)); |
| 165 if (!extension.get()) | 168 if (!extension.get()) |
| 166 return NULL; | 169 return NULL; |
| 167 | 170 |
| 168 Extension::InstallWarningVector warnings; | 171 std::vector<extensions::InstallWarning> warnings; |
| 169 if (!ValidateExtension(extension.get(), error, &warnings)) | 172 if (!ValidateExtension(extension.get(), error, &warnings)) |
| 170 return NULL; | 173 return NULL; |
| 171 extension->AddInstallWarnings(warnings); | 174 extension->AddInstallWarnings(warnings); |
| 172 | 175 |
| 173 return extension; | 176 return extension; |
| 174 } | 177 } |
| 175 | 178 |
| 176 DictionaryValue* LoadManifest(const FilePath& extension_path, | 179 DictionaryValue* LoadManifest(const FilePath& extension_path, |
| 177 std::string* error) { | 180 std::string* error) { |
| 178 FilePath manifest_path = | 181 FilePath manifest_path = |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 !file_util::GetFileSize(path, &size) || | 242 !file_util::GetFileSize(path, &size) || |
| 240 size == 0) { | 243 size == 0) { |
| 241 return false; | 244 return false; |
| 242 } | 245 } |
| 243 | 246 |
| 244 return true; | 247 return true; |
| 245 } | 248 } |
| 246 | 249 |
| 247 bool ValidateExtension(const Extension* extension, | 250 bool ValidateExtension(const Extension* extension, |
| 248 std::string* error, | 251 std::string* error, |
| 249 Extension::InstallWarningVector* warnings) { | 252 std::vector<extensions::InstallWarning>* warnings) { |
| 250 // Validate icons exist. | 253 // Validate icons exist. |
| 251 for (ExtensionIconSet::IconMap::const_iterator iter = | 254 for (ExtensionIconSet::IconMap::const_iterator iter = |
| 252 extension->icons().map().begin(); | 255 extension->icons().map().begin(); |
| 253 iter != extension->icons().map().end(); | 256 iter != extension->icons().map().end(); |
| 254 ++iter) { | 257 ++iter) { |
| 255 const FilePath path = extension->GetResource(iter->second).GetFilePath(); | 258 const FilePath path = extension->GetResource(iter->second).GetFilePath(); |
| 256 if (!ValidateFilePath(path)) { | 259 if (!ValidateFilePath(path)) { |
| 257 *error = | 260 *error = |
| 258 l10n_util::GetStringFUTF8(IDS_EXTENSION_LOAD_ICON_FAILED, | 261 l10n_util::GetStringFUTF8(IDS_EXTENSION_LOAD_ICON_FAILED, |
| 259 UTF8ToUTF16(iter->second)); | 262 UTF8ToUTF16(iter->second)); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 if (!private_keys.empty()) { | 410 if (!private_keys.empty()) { |
| 408 // Only print one of the private keys because l10n_util doesn't have a way | 411 // Only print one of the private keys because l10n_util doesn't have a way |
| 409 // to translate a list of strings. | 412 // to translate a list of strings. |
| 410 *error = l10n_util::GetStringFUTF8( | 413 *error = l10n_util::GetStringFUTF8( |
| 411 IDS_EXTENSION_CONTAINS_PRIVATE_KEY, | 414 IDS_EXTENSION_CONTAINS_PRIVATE_KEY, |
| 412 private_keys.front().LossyDisplayName()); | 415 private_keys.front().LossyDisplayName()); |
| 413 return false; | 416 return false; |
| 414 } | 417 } |
| 415 } else { | 418 } else { |
| 416 for (size_t i = 0; i < private_keys.size(); ++i) { | 419 for (size_t i = 0; i < private_keys.size(); ++i) { |
| 417 warnings->push_back(Extension::InstallWarning( | 420 warnings->push_back(extensions::InstallWarning( |
| 418 Extension::InstallWarning::FORMAT_TEXT, | 421 extensions::InstallWarning::FORMAT_TEXT, |
| 419 l10n_util::GetStringFUTF8( | 422 l10n_util::GetStringFUTF8( |
| 420 IDS_EXTENSION_CONTAINS_PRIVATE_KEY, | 423 IDS_EXTENSION_CONTAINS_PRIVATE_KEY, |
| 421 private_keys[i].LossyDisplayName()))); | 424 private_keys[i].LossyDisplayName()))); |
| 422 } | 425 } |
| 423 // Only warn; don't block loading the extension. | 426 // Only warn; don't block loading the extension. |
| 424 } | 427 } |
| 425 return true; | 428 return true; |
| 426 } | 429 } |
| 427 | 430 |
| 428 void GarbageCollectExtensions( | 431 void GarbageCollectExtensions( |
| (...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 759 return FilePath(); | 762 return FilePath(); |
| 760 } | 763 } |
| 761 return temp_path; | 764 return temp_path; |
| 762 } | 765 } |
| 763 | 766 |
| 764 void DeleteFile(const FilePath& path, bool recursive) { | 767 void DeleteFile(const FilePath& path, bool recursive) { |
| 765 file_util::Delete(path, recursive); | 768 file_util::Delete(path, recursive); |
| 766 } | 769 } |
| 767 | 770 |
| 768 } // namespace extension_file_util | 771 } // namespace extension_file_util |
| OLD | NEW |