| 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_unpacker.h" | 5 #include "chrome/common/extensions/extension_unpacker.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "content/public/common/common_param_traits.h" | 23 #include "content/public/common/common_param_traits.h" |
| 24 #include "ipc/ipc_message_utils.h" | 24 #include "ipc/ipc_message_utils.h" |
| 25 #include "net/base/file_stream.h" | 25 #include "net/base/file_stream.h" |
| 26 #include "third_party/skia/include/core/SkBitmap.h" | 26 #include "third_party/skia/include/core/SkBitmap.h" |
| 27 #include "webkit/glue/image_decoder.h" | 27 #include "webkit/glue/image_decoder.h" |
| 28 | 28 |
| 29 namespace errors = extension_manifest_errors; | 29 namespace errors = extension_manifest_errors; |
| 30 namespace keys = extension_manifest_keys; | 30 namespace keys = extension_manifest_keys; |
| 31 namespace filenames = extension_filenames; | 31 namespace filenames = extension_filenames; |
| 32 | 32 |
| 33 using extensions::Extension; |
| 34 |
| 33 namespace { | 35 namespace { |
| 34 | 36 |
| 35 // Errors | 37 // Errors |
| 36 const char* kCouldNotCreateDirectoryError = | 38 const char* kCouldNotCreateDirectoryError = |
| 37 "Could not create directory for unzipping: "; | 39 "Could not create directory for unzipping: "; |
| 38 const char* kCouldNotDecodeImageError = "Could not decode theme image."; | 40 const char* kCouldNotDecodeImageError = "Could not decode theme image."; |
| 39 const char* kCouldNotUnzipExtension = "Could not unzip extension."; | 41 const char* kCouldNotUnzipExtension = "Could not unzip extension."; |
| 40 const char* kPathNamesMustBeAbsoluteOrLocalError = | 42 const char* kPathNamesMustBeAbsoluteOrLocalError = |
| 41 "Path names must not be absolute or contain '..'."; | 43 "Path names must not be absolute or contain '..'."; |
| 42 | 44 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 } | 81 } |
| 80 } | 82 } |
| 81 | 83 |
| 82 return false; | 84 return false; |
| 83 } | 85 } |
| 84 | 86 |
| 85 } // namespace | 87 } // namespace |
| 86 | 88 |
| 87 ExtensionUnpacker::ExtensionUnpacker(const FilePath& extension_path, | 89 ExtensionUnpacker::ExtensionUnpacker(const FilePath& extension_path, |
| 88 const std::string& extension_id, | 90 const std::string& extension_id, |
| 89 extensions::Extension::Location location, | 91 Extension::Location location, |
| 90 int creation_flags) | 92 int creation_flags) |
| 91 : extension_path_(extension_path), | 93 : extension_path_(extension_path), |
| 92 extension_id_(extension_id), | 94 extension_id_(extension_id), |
| 93 location_(location), | 95 location_(location), |
| 94 creation_flags_(creation_flags) { | 96 creation_flags_(creation_flags) { |
| 95 } | 97 } |
| 96 | 98 |
| 97 ExtensionUnpacker::~ExtensionUnpacker() { | 99 ExtensionUnpacker::~ExtensionUnpacker() { |
| 98 } | 100 } |
| 99 | 101 |
| 100 DictionaryValue* ExtensionUnpacker::ReadManifest() { | 102 DictionaryValue* ExtensionUnpacker::ReadManifest() { |
| 101 FilePath manifest_path = | 103 FilePath manifest_path = |
| 102 temp_install_dir_.Append(extensions::Extension::kManifestFilename); | 104 temp_install_dir_.Append(Extension::kManifestFilename); |
| 103 if (!file_util::PathExists(manifest_path)) { | 105 if (!file_util::PathExists(manifest_path)) { |
| 104 SetError(errors::kInvalidManifest); | 106 SetError(errors::kInvalidManifest); |
| 105 return NULL; | 107 return NULL; |
| 106 } | 108 } |
| 107 | 109 |
| 108 JSONFileValueSerializer serializer(manifest_path); | 110 JSONFileValueSerializer serializer(manifest_path); |
| 109 std::string error; | 111 std::string error; |
| 110 scoped_ptr<Value> root(serializer.Deserialize(NULL, &error)); | 112 scoped_ptr<Value> root(serializer.Deserialize(NULL, &error)); |
| 111 if (!root.get()) { | 113 if (!root.get()) { |
| 112 SetError(error); | 114 SetError(error); |
| 113 return NULL; | 115 return NULL; |
| 114 } | 116 } |
| 115 | 117 |
| 116 if (!root->IsType(Value::TYPE_DICTIONARY)) { | 118 if (!root->IsType(Value::TYPE_DICTIONARY)) { |
| 117 SetError(errors::kInvalidManifest); | 119 SetError(errors::kInvalidManifest); |
| 118 return NULL; | 120 return NULL; |
| 119 } | 121 } |
| 120 | 122 |
| 121 return static_cast<DictionaryValue*>(root.release()); | 123 return static_cast<DictionaryValue*>(root.release()); |
| 122 } | 124 } |
| 123 | 125 |
| 124 bool ExtensionUnpacker::ReadAllMessageCatalogs( | 126 bool ExtensionUnpacker::ReadAllMessageCatalogs( |
| 125 const std::string& default_locale) { | 127 const std::string& default_locale) { |
| 126 FilePath locales_path = | 128 FilePath locales_path = |
| 127 temp_install_dir_.Append(extensions::Extension::kLocaleFolder); | 129 temp_install_dir_.Append(Extension::kLocaleFolder); |
| 128 | 130 |
| 129 // Not all folders under _locales have to be valid locales. | 131 // Not all folders under _locales have to be valid locales. |
| 130 file_util::FileEnumerator locales(locales_path, | 132 file_util::FileEnumerator locales(locales_path, |
| 131 false, | 133 false, |
| 132 file_util::FileEnumerator::DIRECTORIES); | 134 file_util::FileEnumerator::DIRECTORIES); |
| 133 | 135 |
| 134 std::set<std::string> all_locales; | 136 std::set<std::string> all_locales; |
| 135 extension_l10n_util::GetAllLocales(&all_locales); | 137 extension_l10n_util::GetAllLocales(&all_locales); |
| 136 FilePath locale_path; | 138 FilePath locale_path; |
| 137 while (!(locale_path = locales.Next()).empty()) { | 139 while (!(locale_path = locales.Next()).empty()) { |
| 138 if (extension_l10n_util::ShouldSkipValidation(locales_path, locale_path, | 140 if (extension_l10n_util::ShouldSkipValidation(locales_path, locale_path, |
| 139 all_locales)) | 141 all_locales)) |
| 140 continue; | 142 continue; |
| 141 | 143 |
| 142 FilePath messages_path = | 144 FilePath messages_path = |
| 143 locale_path.Append(extensions::Extension::kMessagesFilename); | 145 locale_path.Append(Extension::kMessagesFilename); |
| 144 | 146 |
| 145 if (!ReadMessageCatalog(messages_path)) | 147 if (!ReadMessageCatalog(messages_path)) |
| 146 return false; | 148 return false; |
| 147 } | 149 } |
| 148 | 150 |
| 149 return true; | 151 return true; |
| 150 } | 152 } |
| 151 | 153 |
| 152 bool ExtensionUnpacker::Run() { | 154 bool ExtensionUnpacker::Run() { |
| 153 DVLOG(1) << "Installing extension " << extension_path_.value(); | 155 DVLOG(1) << "Installing extension " << extension_path_.value(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 171 SetError(kCouldNotUnzipExtension); | 173 SetError(kCouldNotUnzipExtension); |
| 172 return false; | 174 return false; |
| 173 } | 175 } |
| 174 | 176 |
| 175 // Parse the manifest. | 177 // Parse the manifest. |
| 176 parsed_manifest_.reset(ReadManifest()); | 178 parsed_manifest_.reset(ReadManifest()); |
| 177 if (!parsed_manifest_.get()) | 179 if (!parsed_manifest_.get()) |
| 178 return false; // Error was already reported. | 180 return false; // Error was already reported. |
| 179 | 181 |
| 180 std::string error; | 182 std::string error; |
| 181 scoped_refptr<extensions::Extension> extension(extensions::Extension::Create( | 183 scoped_refptr<Extension> extension(Extension::Create( |
| 182 temp_install_dir_, | 184 temp_install_dir_, |
| 183 location_, | 185 location_, |
| 184 *parsed_manifest_, | 186 *parsed_manifest_, |
| 185 creation_flags_, | 187 creation_flags_, |
| 186 extension_id_, | 188 extension_id_, |
| 187 &error)); | 189 &error)); |
| 188 if (!extension.get()) { | 190 if (!extension.get()) { |
| 189 SetError(error); | 191 SetError(error); |
| 190 return false; | 192 return false; |
| 191 } | 193 } |
| 192 | 194 |
| 193 std::vector<std::string> warnings; | 195 Extension::InstallWarningVector warnings; |
| 194 if (!extension_file_util::ValidateExtension(extension.get(), | 196 if (!extension_file_util::ValidateExtension(extension.get(), |
| 195 &error, &warnings)) { | 197 &error, &warnings)) { |
| 196 SetError(error); | 198 SetError(error); |
| 197 return false; | 199 return false; |
| 198 } | 200 } |
| 199 extension->AddInstallWarnings(warnings); | 201 extension->AddInstallWarnings(warnings); |
| 200 | 202 |
| 201 // Decode any images that the browser needs to display. | 203 // Decode any images that the browser needs to display. |
| 202 std::set<FilePath> image_paths = extension->GetBrowserImages(); | 204 std::set<FilePath> image_paths = extension->GetBrowserImages(); |
| 203 for (std::set<FilePath>::iterator it = image_paths.begin(); | 205 for (std::set<FilePath>::iterator it = image_paths.begin(); |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 return false; | 324 return false; |
| 323 } | 325 } |
| 324 parsed_catalogs_->Set(dir_name, root.release()); | 326 parsed_catalogs_->Set(dir_name, root.release()); |
| 325 | 327 |
| 326 return true; | 328 return true; |
| 327 } | 329 } |
| 328 | 330 |
| 329 void ExtensionUnpacker::SetError(const std::string &error) { | 331 void ExtensionUnpacker::SetError(const std::string &error) { |
| 330 error_message_ = UTF8ToUTF16(error); | 332 error_message_ = UTF8ToUTF16(error); |
| 331 } | 333 } |
| OLD | NEW |