[email protected] | a999d67 | 2012-01-23 22:31:40 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | c34381df | 2013-07-11 16:14:42 | [diff] [blame] | 5 | #include "chrome/utility/extensions/unpacker.h" |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 6 | |
[email protected] | ccea03c | 2010-12-17 03:31:50 | [diff] [blame] | 7 | #include <set> |
| 8 | |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 9 | #include "base/files/file_enumerator.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 10 | #include "base/files/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 11 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 3367f3a | 2012-09-01 02:40:06 | [diff] [blame] | 12 | #include "base/i18n/rtl.h" |
[email protected] | ffbec69 | 2012-02-26 20:26:42 | [diff] [blame] | 13 | #include "base/json/json_file_value_serializer.h" |
[email protected] | cb15406 | 2014-01-17 03:32:40 | [diff] [blame] | 14 | #include "base/numerics/safe_conversions.h" |
[email protected] | 3c8a6b0 | 2013-06-11 00:49:49 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
[email protected] | 12bfb61 | 2013-06-07 19:54:02 | [diff] [blame] | 16 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 17 | #include "base/threading/thread.h" |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 18 | #include "base/values.h" |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 19 | #include "chrome/common/chrome_utility_messages.h" |
[email protected] | 5ec0d4f | 2014-08-13 08:28:30 | [diff] [blame] | 20 | #include "chrome/grit/generated_resources.h" |
[email protected] | e689cf25 | 2013-06-26 18:21:14 | [diff] [blame] | 21 | #include "content/public/child/image_decoder_utils.h" |
[email protected] | 19a5c744 | 2011-10-21 20:00:41 | [diff] [blame] | 22 | #include "content/public/common/common_param_traits.h" |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 23 | #include "extensions/common/constants.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 24 | #include "extensions/common/extension.h" |
[email protected] | 6668e5d | 2014-04-08 23:32:52 | [diff] [blame] | 25 | #include "extensions/common/extension_l10n_util.h" |
rockot | ec1e64b | 2014-11-13 22:06:51 | [diff] [blame] | 26 | #include "extensions/common/extensions_client.h" |
[email protected] | 85df9d1 | 2014-04-15 17:02:14 | [diff] [blame] | 27 | #include "extensions/common/file_util.h" |
[email protected] | d42c111 | 2013-08-22 19:36:32 | [diff] [blame] | 28 | #include "extensions/common/manifest.h" |
[email protected] | 0c3c973 | 2013-09-16 08:53:41 | [diff] [blame] | 29 | #include "extensions/common/manifest_constants.h" |
rockot | e5fd3d9 | 2014-11-13 00:21:22 | [diff] [blame] | 30 | #include "extensions/common/manifest_handlers/default_locale_handler.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 31 | #include "ipc/ipc_message_utils.h" |
[email protected] | e078590 | 2011-05-19 23:34:17 | [diff] [blame] | 32 | #include "net/base/file_stream.h" |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 33 | #include "third_party/skia/include/core/SkBitmap.h" |
[email protected] | 4170d3a | 2013-05-03 23:02:57 | [diff] [blame] | 34 | #include "third_party/zlib/google/zip.h" |
[email protected] | 3367f3a | 2012-09-01 02:40:06 | [diff] [blame] | 35 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | e689cf25 | 2013-06-26 18:21:14 | [diff] [blame] | 36 | #include "ui/gfx/size.h" |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 37 | |
[email protected] | 0c3c973 | 2013-09-16 08:53:41 | [diff] [blame] | 38 | namespace extensions { |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 39 | |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 40 | namespace { |
[email protected] | 6d37714 | 2010-03-17 20:36:05 | [diff] [blame] | 41 | |
[email protected] | 0c3c973 | 2013-09-16 08:53:41 | [diff] [blame] | 42 | namespace errors = manifest_errors; |
| 43 | namespace keys = manifest_keys; |
| 44 | |
[email protected] | fcfd12f | 2009-08-14 22:20:46 | [diff] [blame] | 45 | // A limit to stop us passing dangerously large canvases to the browser. |
| 46 | const int kMaxImageCanvas = 4096 * 4096; |
| 47 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 48 | SkBitmap DecodeImage(const base::FilePath& path) { |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 49 | // Read the file from disk. |
| 50 | std::string file_contents; |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 51 | if (!base::PathExists(path) || |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 52 | !base::ReadFileToString(path, &file_contents)) { |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 53 | return SkBitmap(); |
| 54 | } |
| 55 | |
| 56 | // Decode the image using WebKit's image decoder. |
| 57 | const unsigned char* data = |
| 58 | reinterpret_cast<const unsigned char*>(file_contents.data()); |
[email protected] | e689cf25 | 2013-06-26 18:21:14 | [diff] [blame] | 59 | SkBitmap bitmap = content::DecodeImage(data, |
| 60 | gfx::Size(), |
| 61 | file_contents.length()); |
[email protected] | 72ed0b850 | 2014-01-03 18:53:02 | [diff] [blame] | 62 | if (bitmap.computeSize64() > kMaxImageCanvas) |
[email protected] | fcfd12f | 2009-08-14 22:20:46 | [diff] [blame] | 63 | return SkBitmap(); |
| 64 | return bitmap; |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 65 | } |
| 66 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 67 | bool PathContainsParentDirectory(const base::FilePath& path) { |
| 68 | const base::FilePath::StringType kSeparators(base::FilePath::kSeparators); |
| 69 | const base::FilePath::StringType kParentDirectory( |
| 70 | base::FilePath::kParentDirectory); |
| 71 | const size_t npos = base::FilePath::StringType::npos; |
| 72 | const base::FilePath::StringType& value = path.value(); |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 73 | |
| 74 | for (size_t i = 0; i < value.length(); ) { |
| 75 | i = value.find(kParentDirectory, i); |
| 76 | if (i != npos) { |
| 77 | if ((i == 0 || kSeparators.find(value[i-1]) == npos) && |
| 78 | (i+1 < value.length() || kSeparators.find(value[i+1]) == npos)) { |
| 79 | return true; |
| 80 | } |
| 81 | ++i; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return false; |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 86 | } |
| 87 | |
[email protected] | fc006cac | 2013-09-17 22:43:31 | [diff] [blame] | 88 | bool WritePickle(const IPC::Message& pickle, const base::FilePath& dest_path) { |
[email protected] | cb15406 | 2014-01-17 03:32:40 | [diff] [blame] | 89 | int size = base::checked_cast<int>(pickle.size()); |
[email protected] | fc006cac | 2013-09-17 22:43:31 | [diff] [blame] | 90 | const char* data = static_cast<const char*>(pickle.data()); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 91 | int bytes_written = base::WriteFile(dest_path, data, size); |
[email protected] | fc006cac | 2013-09-17 22:43:31 | [diff] [blame] | 92 | return (bytes_written == size); |
| 93 | } |
| 94 | |
[email protected] | 3bb8499 | 2010-08-26 17:23:46 | [diff] [blame] | 95 | } // namespace |
| 96 | |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 97 | struct Unpacker::InternalData { |
| 98 | DecodedImages decoded_images; |
| 99 | }; |
| 100 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 101 | Unpacker::Unpacker(const base::FilePath& extension_path, |
[email protected] | b3fe68d | 2012-07-16 19:14:39 | [diff] [blame] | 102 | const std::string& extension_id, |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 103 | Manifest::Location location, |
[email protected] | b3fe68d | 2012-07-16 19:14:39 | [diff] [blame] | 104 | int creation_flags) |
[email protected] | f5bf184 | 2012-02-15 02:52:26 | [diff] [blame] | 105 | : extension_path_(extension_path), |
| 106 | extension_id_(extension_id), |
| 107 | location_(location), |
[email protected] | fc38935a | 2011-10-31 23:53:28 | [diff] [blame] | 108 | creation_flags_(creation_flags) { |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 109 | internal_data_.reset(new InternalData()); |
[email protected] | 3bb8499 | 2010-08-26 17:23:46 | [diff] [blame] | 110 | } |
| 111 | |
[email protected] | b3fe68d | 2012-07-16 19:14:39 | [diff] [blame] | 112 | Unpacker::~Unpacker() { |
[email protected] | 3bb8499 | 2010-08-26 17:23:46 | [diff] [blame] | 113 | } |
| 114 | |
[email protected] | a371b4b | 2013-06-18 20:29:27 | [diff] [blame] | 115 | base::DictionaryValue* Unpacker::ReadManifest() { |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 116 | base::FilePath manifest_path = |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 117 | temp_install_dir_.Append(kManifestFilename); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 118 | if (!base::PathExists(manifest_path)) { |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 119 | SetError(errors::kInvalidManifest); |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | JSONFileValueSerializer serializer(manifest_path); |
| 124 | std::string error; |
[email protected] | a371b4b | 2013-06-18 20:29:27 | [diff] [blame] | 125 | scoped_ptr<base::Value> root(serializer.Deserialize(NULL, &error)); |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 126 | if (!root.get()) { |
| 127 | SetError(error); |
| 128 | return NULL; |
| 129 | } |
| 130 | |
[email protected] | a371b4b | 2013-06-18 20:29:27 | [diff] [blame] | 131 | if (!root->IsType(base::Value::TYPE_DICTIONARY)) { |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 132 | SetError(errors::kInvalidManifest); |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 133 | return NULL; |
| 134 | } |
| 135 | |
[email protected] | a371b4b | 2013-06-18 20:29:27 | [diff] [blame] | 136 | return static_cast<base::DictionaryValue*>(root.release()); |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | b3fe68d | 2012-07-16 19:14:39 | [diff] [blame] | 139 | bool Unpacker::ReadAllMessageCatalogs(const std::string& default_locale) { |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 140 | base::FilePath locales_path = |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 141 | temp_install_dir_.Append(kLocaleFolder); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 142 | |
[email protected] | 1acbb4b6 | 2010-03-09 17:52:29 | [diff] [blame] | 143 | // Not all folders under _locales have to be valid locales. |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 144 | base::FileEnumerator locales(locales_path, |
| 145 | false, |
| 146 | base::FileEnumerator::DIRECTORIES); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 147 | |
[email protected] | 1acbb4b6 | 2010-03-09 17:52:29 | [diff] [blame] | 148 | std::set<std::string> all_locales; |
| 149 | extension_l10n_util::GetAllLocales(&all_locales); |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 150 | base::FilePath locale_path; |
[email protected] | 1acbb4b6 | 2010-03-09 17:52:29 | [diff] [blame] | 151 | while (!(locale_path = locales.Next()).empty()) { |
| 152 | if (extension_l10n_util::ShouldSkipValidation(locales_path, locale_path, |
| 153 | all_locales)) |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 154 | continue; |
| 155 | |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 156 | base::FilePath messages_path = locale_path.Append(kMessagesFilename); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 157 | |
| 158 | if (!ReadMessageCatalog(messages_path)) |
| 159 | return false; |
[email protected] | 1acbb4b6 | 2010-03-09 17:52:29 | [diff] [blame] | 160 | } |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 161 | |
| 162 | return true; |
| 163 | } |
| 164 | |
[email protected] | b3fe68d | 2012-07-16 19:14:39 | [diff] [blame] | 165 | bool Unpacker::Run() { |
[email protected] | 26f025e | 2011-10-28 22:49:27 | [diff] [blame] | 166 | DVLOG(1) << "Installing extension " << extension_path_.value(); |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 167 | |
[email protected] | 3367f3a | 2012-09-01 02:40:06 | [diff] [blame] | 168 | // <profile>/Extensions/CRX_INSTALL |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 169 | temp_install_dir_ = |
[email protected] | b22c8af6 | 2013-07-23 23:17:02 | [diff] [blame] | 170 | extension_path_.DirName().AppendASCII(kTempExtensionName); |
[email protected] | baedfdf | 2010-06-14 18:50:19 | [diff] [blame] | 171 | |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 172 | if (!base::CreateDirectory(temp_install_dir_)) { |
[email protected] | 3367f3a | 2012-09-01 02:40:06 | [diff] [blame] | 173 | SetUTF16Error( |
| 174 | l10n_util::GetStringFUTF16( |
| 175 | IDS_EXTENSION_PACKAGE_DIRECTORY_ERROR, |
| 176 | base::i18n::GetDisplayStringInLTRDirectionality( |
| 177 | temp_install_dir_.LossyDisplayName()))); |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 178 | return false; |
| 179 | } |
| 180 | |
[email protected] | b3eb3dd2 | 2011-10-26 06:59:34 | [diff] [blame] | 181 | if (!zip::Unzip(extension_path_, temp_install_dir_)) { |
[email protected] | 3367f3a | 2012-09-01 02:40:06 | [diff] [blame] | 182 | SetUTF16Error(l10n_util::GetStringUTF16(IDS_EXTENSION_PACKAGE_UNZIP_ERROR)); |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 183 | return false; |
| 184 | } |
| 185 | |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 186 | // Parse the manifest. |
| 187 | parsed_manifest_.reset(ReadManifest()); |
[email protected] | a999d67 | 2012-01-23 22:31:40 | [diff] [blame] | 188 | if (!parsed_manifest_.get()) |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 189 | return false; // Error was already reported. |
[email protected] | af1277b | 2009-07-28 00:47:53 | [diff] [blame] | 190 | |
[email protected] | fbcc4030 | 2009-06-12 20:45:45 | [diff] [blame] | 191 | std::string error; |
[email protected] | 1e0f45a | 2012-06-13 00:31:06 | [diff] [blame] | 192 | scoped_refptr<Extension> extension(Extension::Create( |
[email protected] | 542258c | 2011-03-04 21:25:31 | [diff] [blame] | 193 | temp_install_dir_, |
[email protected] | fc38935a | 2011-10-31 23:53:28 | [diff] [blame] | 194 | location_, |
[email protected] | 542258c | 2011-03-04 21:25:31 | [diff] [blame] | 195 | *parsed_manifest_, |
[email protected] | fc38935a | 2011-10-31 23:53:28 | [diff] [blame] | 196 | creation_flags_, |
[email protected] | f5bf184 | 2012-02-15 02:52:26 | [diff] [blame] | 197 | extension_id_, |
[email protected] | 542258c | 2011-03-04 21:25:31 | [diff] [blame] | 198 | &error)); |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 199 | if (!extension.get()) { |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 200 | SetError(error); |
| 201 | return false; |
| 202 | } |
[email protected] | 99872e3 | 2009-09-25 22:02:49 | [diff] [blame] | 203 | |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 204 | std::vector<InstallWarning> warnings; |
[email protected] | 85df9d1 | 2014-04-15 17:02:14 | [diff] [blame] | 205 | if (!file_util::ValidateExtension(extension.get(), &error, &warnings)) { |
[email protected] | 99872e3 | 2009-09-25 22:02:49 | [diff] [blame] | 206 | SetError(error); |
| 207 | return false; |
| 208 | } |
[email protected] | ab55c2b | 2012-06-01 23:55:03 | [diff] [blame] | 209 | extension->AddInstallWarnings(warnings); |
[email protected] | 99872e3 | 2009-09-25 22:02:49 | [diff] [blame] | 210 | |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 211 | // Decode any images that the browser needs to display. |
[email protected] | 72b49d4 | 2013-04-19 12:47:31 | [diff] [blame] | 212 | std::set<base::FilePath> image_paths = |
rockot | ec1e64b | 2014-11-13 22:06:51 | [diff] [blame] | 213 | ExtensionsClient::Get()->GetBrowserImagePaths(extension.get()); |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 214 | for (std::set<base::FilePath>::iterator it = image_paths.begin(); |
[email protected] | 5c6ac84 | 2013-06-02 23:37:03 | [diff] [blame] | 215 | it != image_paths.end(); |
| 216 | ++it) { |
[email protected] | facd7a765 | 2009-06-05 23:15:02 | [diff] [blame] | 217 | if (!AddDecodedImage(*it)) |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 218 | return false; // Error was already reported. |
| 219 | } |
| 220 | |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 221 | // Parse all message catalogs (if any). |
[email protected] | a371b4b | 2013-06-18 20:29:27 | [diff] [blame] | 222 | parsed_catalogs_.reset(new base::DictionaryValue); |
[email protected] | 5c6ac84 | 2013-06-02 23:37:03 | [diff] [blame] | 223 | if (!LocaleInfo::GetDefaultLocale(extension.get()).empty()) { |
| 224 | if (!ReadAllMessageCatalogs(LocaleInfo::GetDefaultLocale(extension.get()))) |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 225 | return false; // Error was already reported. |
| 226 | } |
| 227 | |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 228 | return true; |
| 229 | } |
| 230 | |
[email protected] | b3fe68d | 2012-07-16 19:14:39 | [diff] [blame] | 231 | bool Unpacker::DumpImagesToFile() { |
[email protected] | facd7a765 | 2009-06-05 23:15:02 | [diff] [blame] | 232 | IPC::Message pickle; // We use a Message so we can use WriteParam. |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 233 | IPC::WriteParam(&pickle, internal_data_->decoded_images); |
[email protected] | facd7a765 | 2009-06-05 23:15:02 | [diff] [blame] | 234 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 235 | base::FilePath path = extension_path_.DirName().AppendASCII( |
[email protected] | b22c8af6 | 2013-07-23 23:17:02 | [diff] [blame] | 236 | kDecodedImagesFilename); |
[email protected] | fc006cac | 2013-09-17 22:43:31 | [diff] [blame] | 237 | if (!WritePickle(pickle, path)) { |
[email protected] | facd7a765 | 2009-06-05 23:15:02 | [diff] [blame] | 238 | SetError("Could not write image data to disk."); |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | return true; |
| 243 | } |
| 244 | |
[email protected] | b3fe68d | 2012-07-16 19:14:39 | [diff] [blame] | 245 | bool Unpacker::DumpMessageCatalogsToFile() { |
[email protected] | 6d37714 | 2010-03-17 20:36:05 | [diff] [blame] | 246 | IPC::Message pickle; |
| 247 | IPC::WriteParam(&pickle, *parsed_catalogs_.get()); |
| 248 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 249 | base::FilePath path = extension_path_.DirName().AppendASCII( |
[email protected] | b22c8af6 | 2013-07-23 23:17:02 | [diff] [blame] | 250 | kDecodedMessageCatalogsFilename); |
[email protected] | fc006cac | 2013-09-17 22:43:31 | [diff] [blame] | 251 | if (!WritePickle(pickle, path)) { |
[email protected] | 6d37714 | 2010-03-17 20:36:05 | [diff] [blame] | 252 | SetError("Could not write message catalogs to disk."); |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | return true; |
| 257 | } |
| 258 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 259 | bool Unpacker::AddDecodedImage(const base::FilePath& path) { |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 260 | // Make sure it's not referencing a file outside the extension's subdir. |
| 261 | if (path.IsAbsolute() || PathContainsParentDirectory(path)) { |
[email protected] | 3367f3a | 2012-09-01 02:40:06 | [diff] [blame] | 262 | SetUTF16Error( |
| 263 | l10n_util::GetStringFUTF16( |
| 264 | IDS_EXTENSION_PACKAGE_IMAGE_PATH_ERROR, |
| 265 | base::i18n::GetDisplayStringInLTRDirectionality( |
| 266 | path.LossyDisplayName()))); |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 267 | return false; |
| 268 | } |
| 269 | |
| 270 | SkBitmap image_bitmap = DecodeImage(temp_install_dir_.Append(path)); |
| 271 | if (image_bitmap.isNull()) { |
[email protected] | 3367f3a | 2012-09-01 02:40:06 | [diff] [blame] | 272 | SetUTF16Error( |
| 273 | l10n_util::GetStringFUTF16( |
| 274 | IDS_EXTENSION_PACKAGE_IMAGE_ERROR, |
| 275 | base::i18n::GetDisplayStringInLTRDirectionality( |
| 276 | path.BaseName().LossyDisplayName()))); |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 277 | return false; |
| 278 | } |
| 279 | |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 280 | internal_data_->decoded_images.push_back(MakeTuple(image_bitmap, path)); |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 281 | return true; |
| 282 | } |
| 283 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 284 | bool Unpacker::ReadMessageCatalog(const base::FilePath& message_path) { |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 285 | std::string error; |
| 286 | JSONFileValueSerializer serializer(message_path); |
[email protected] | a371b4b | 2013-06-18 20:29:27 | [diff] [blame] | 287 | scoped_ptr<base::DictionaryValue> root(static_cast<base::DictionaryValue*>( |
| 288 | serializer.Deserialize(NULL, &error))); |
[email protected] | 6d37714 | 2010-03-17 20:36:05 | [diff] [blame] | 289 | if (!root.get()) { |
[email protected] | 05aab99c | 2013-12-20 09:03:15 | [diff] [blame] | 290 | base::string16 messages_file = message_path.LossyDisplayName(); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 291 | if (error.empty()) { |
| 292 | // If file is missing, Deserialize will fail with empty error. |
[email protected] | 93f1052 | 2010-10-31 16:27:48 | [diff] [blame] | 293 | SetError(base::StringPrintf("%s %s", errors::kLocalesMessagesFileMissing, |
[email protected] | c91b9d4 | 2013-12-25 00:53:42 | [diff] [blame] | 294 | base::UTF16ToUTF8(messages_file).c_str())); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 295 | } else { |
[email protected] | 967d18b | 2011-03-02 22:22:07 | [diff] [blame] | 296 | SetError(base::StringPrintf("%s: %s", |
[email protected] | c91b9d4 | 2013-12-25 00:53:42 | [diff] [blame] | 297 | base::UTF16ToUTF8(messages_file).c_str(), |
[email protected] | 93f1052 | 2010-10-31 16:27:48 | [diff] [blame] | 298 | error.c_str())); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 299 | } |
| 300 | return false; |
| 301 | } |
| 302 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 303 | base::FilePath relative_path; |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 304 | // message_path was created from temp_install_dir. This should never fail. |
[email protected] | 967d18b | 2011-03-02 22:22:07 | [diff] [blame] | 305 | if (!temp_install_dir_.AppendRelativePath(message_path, &relative_path)) { |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 306 | NOTREACHED(); |
[email protected] | 967d18b | 2011-03-02 22:22:07 | [diff] [blame] | 307 | return false; |
| 308 | } |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 309 | |
[email protected] | 967d18b | 2011-03-02 22:22:07 | [diff] [blame] | 310 | std::string dir_name = relative_path.DirName().MaybeAsASCII(); |
| 311 | if (dir_name.empty()) { |
| 312 | NOTREACHED(); |
| 313 | return false; |
| 314 | } |
| 315 | parsed_catalogs_->Set(dir_name, root.release()); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 316 | |
| 317 | return true; |
| 318 | } |
| 319 | |
[email protected] | b3fe68d | 2012-07-16 19:14:39 | [diff] [blame] | 320 | void Unpacker::SetError(const std::string &error) { |
[email protected] | c91b9d4 | 2013-12-25 00:53:42 | [diff] [blame] | 321 | SetUTF16Error(base::UTF8ToUTF16(error)); |
[email protected] | 3367f3a | 2012-09-01 02:40:06 | [diff] [blame] | 322 | } |
| 323 | |
[email protected] | 05aab99c | 2013-12-20 09:03:15 | [diff] [blame] | 324 | void Unpacker::SetUTF16Error(const base::string16& error) { |
[email protected] | 3367f3a | 2012-09-01 02:40:06 | [diff] [blame] | 325 | error_message_ = error; |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 326 | } |
[email protected] | b3fe68d | 2012-07-16 19:14:39 | [diff] [blame] | 327 | |
| 328 | } // namespace extensions |