[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 | |
asargent | 9156f029 | 2015-01-15 01:07:02 | [diff] [blame] | 5 | #include "extensions/utility/unpacker.h" |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 6 | |
avi | 2d124c0 | 2015-12-23 06:36:42 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
meacer | bd73506 | 2016-09-28 19:58:37 | [diff] [blame] | 9 | #include <algorithm> |
[email protected] | ccea03c | 2010-12-17 03:31:50 | [diff] [blame] | 10 | #include <set> |
tzik | b2f765b | 2016-03-09 00:51:45 | [diff] [blame] | 11 | #include <tuple> |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 12 | #include <utility> |
[email protected] | ccea03c | 2010-12-17 03:31:50 | [diff] [blame] | 13 | |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 14 | #include "base/files/file_enumerator.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 15 | #include "base/files/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 16 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 3367f3a | 2012-09-01 02:40:06 | [diff] [blame] | 17 | #include "base/i18n/rtl.h" |
[email protected] | ffbec69 | 2012-02-26 20:26:42 | [diff] [blame] | 18 | #include "base/json/json_file_value_serializer.h" |
[email protected] | cb15406 | 2014-01-17 03:32:40 | [diff] [blame] | 19 | #include "base/numerics/safe_conversions.h" |
[email protected] | 3c8a6b0 | 2013-06-11 00:49:49 | [diff] [blame] | 20 | #include "base/strings/string_util.h" |
[email protected] | 12bfb61 | 2013-06-07 19:54:02 | [diff] [blame] | 21 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 22 | #include "base/threading/thread.h" |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 23 | #include "base/values.h" |
[email protected] | e689cf25 | 2013-06-26 18:21:14 | [diff] [blame] | 24 | #include "content/public/child/image_decoder_utils.h" |
Karan Bhatia | 71f6a62 | 2017-10-02 19:39:10 | [diff] [blame] | 25 | #include "extensions/common/api/declarative_net_request/dnr_manifest_data.h" |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 26 | #include "extensions/common/constants.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 27 | #include "extensions/common/extension.h" |
[email protected] | 6668e5d | 2014-04-08 23:32:52 | [diff] [blame] | 28 | #include "extensions/common/extension_l10n_util.h" |
noel | c8702c4 | 2017-03-16 08:51:19 | [diff] [blame] | 29 | #include "extensions/common/extension_utility_types.h" |
rockot | ec1e64b | 2014-11-13 22:06:51 | [diff] [blame] | 30 | #include "extensions/common/extensions_client.h" |
[email protected] | 85df9d1 | 2014-04-15 17:02:14 | [diff] [blame] | 31 | #include "extensions/common/file_util.h" |
[email protected] | d42c111 | 2013-08-22 19:36:32 | [diff] [blame] | 32 | #include "extensions/common/manifest.h" |
[email protected] | 0c3c973 | 2013-09-16 08:53:41 | [diff] [blame] | 33 | #include "extensions/common/manifest_constants.h" |
rockot | e5fd3d9 | 2014-11-13 00:21:22 | [diff] [blame] | 34 | #include "extensions/common/manifest_handlers/default_locale_handler.h" |
asargent | 9156f029 | 2015-01-15 01:07:02 | [diff] [blame] | 35 | #include "extensions/strings/grit/extensions_strings.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 36 | #include "ipc/ipc_message_utils.h" |
[email protected] | e078590 | 2011-05-19 23:34:17 | [diff] [blame] | 37 | #include "net/base/file_stream.h" |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 38 | #include "third_party/skia/include/core/SkBitmap.h" |
[email protected] | 3367f3a | 2012-09-01 02:40:06 | [diff] [blame] | 39 | #include "ui/base/l10n/l10n_util.h" |
tfarina | ebe974f0 | 2015-01-03 04:25:32 | [diff] [blame] | 40 | #include "ui/gfx/geometry/size.h" |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 41 | |
[email protected] | 0c3c973 | 2013-09-16 08:53:41 | [diff] [blame] | 42 | namespace extensions { |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 43 | |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 44 | namespace { |
[email protected] | 6d37714 | 2010-03-17 20:36:05 | [diff] [blame] | 45 | |
[email protected] | 0c3c973 | 2013-09-16 08:53:41 | [diff] [blame] | 46 | namespace errors = manifest_errors; |
| 47 | namespace keys = manifest_keys; |
| 48 | |
[email protected] | fcfd12f | 2009-08-14 22:20:46 | [diff] [blame] | 49 | // A limit to stop us passing dangerously large canvases to the browser. |
| 50 | const int kMaxImageCanvas = 4096 * 4096; |
| 51 | |
Lei Zhang | eb9b341 | 2017-09-25 20:57:38 | [diff] [blame] | 52 | constexpr const base::FilePath::CharType* kAllowedThemeFiletypes[] = { |
meacer | bd73506 | 2016-09-28 19:58:37 | [diff] [blame] | 53 | FILE_PATH_LITERAL(".bmp"), FILE_PATH_LITERAL(".gif"), |
| 54 | FILE_PATH_LITERAL(".jpeg"), FILE_PATH_LITERAL(".jpg"), |
| 55 | FILE_PATH_LITERAL(".json"), FILE_PATH_LITERAL(".png"), |
| 56 | FILE_PATH_LITERAL(".webp")}; |
| 57 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 58 | SkBitmap DecodeImage(const base::FilePath& path) { |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 59 | // Read the file from disk. |
| 60 | std::string file_contents; |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 61 | if (!base::PathExists(path) || |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 62 | !base::ReadFileToString(path, &file_contents)) { |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 63 | return SkBitmap(); |
| 64 | } |
| 65 | |
| 66 | // Decode the image using WebKit's image decoder. |
| 67 | const unsigned char* data = |
| 68 | reinterpret_cast<const unsigned char*>(file_contents.data()); |
asargent | 9156f029 | 2015-01-15 01:07:02 | [diff] [blame] | 69 | SkBitmap bitmap = |
| 70 | content::DecodeImage(data, gfx::Size(), file_contents.length()); |
Mike Reed | 8eac88c4 | 2017-10-12 21:24:38 | [diff] [blame^] | 71 | if (bitmap.computeByteSize() > kMaxImageCanvas) |
[email protected] | fcfd12f | 2009-08-14 22:20:46 | [diff] [blame] | 72 | return SkBitmap(); |
| 73 | return bitmap; |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 74 | } |
| 75 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 76 | bool PathContainsParentDirectory(const base::FilePath& path) { |
| 77 | const base::FilePath::StringType kSeparators(base::FilePath::kSeparators); |
| 78 | const base::FilePath::StringType kParentDirectory( |
| 79 | base::FilePath::kParentDirectory); |
| 80 | const size_t npos = base::FilePath::StringType::npos; |
| 81 | const base::FilePath::StringType& value = path.value(); |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 82 | |
asargent | 9156f029 | 2015-01-15 01:07:02 | [diff] [blame] | 83 | for (size_t i = 0; i < value.length();) { |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 84 | i = value.find(kParentDirectory, i); |
| 85 | if (i != npos) { |
asargent | 9156f029 | 2015-01-15 01:07:02 | [diff] [blame] | 86 | if ((i == 0 || kSeparators.find(value[i - 1]) == npos) && |
| 87 | (i + 1 < value.length() || kSeparators.find(value[i + 1]) == npos)) { |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 88 | return true; |
| 89 | } |
| 90 | ++i; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return false; |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | fc006cac | 2013-09-17 22:43:31 | [diff] [blame] | 97 | bool WritePickle(const IPC::Message& pickle, const base::FilePath& dest_path) { |
[email protected] | cb15406 | 2014-01-17 03:32:40 | [diff] [blame] | 98 | int size = base::checked_cast<int>(pickle.size()); |
[email protected] | fc006cac | 2013-09-17 22:43:31 | [diff] [blame] | 99 | const char* data = static_cast<const char*>(pickle.data()); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 100 | int bytes_written = base::WriteFile(dest_path, data, size); |
[email protected] | fc006cac | 2013-09-17 22:43:31 | [diff] [blame] | 101 | return (bytes_written == size); |
| 102 | } |
| 103 | |
[email protected] | 3bb8499 | 2010-08-26 17:23:46 | [diff] [blame] | 104 | } // namespace |
| 105 | |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 106 | struct Unpacker::InternalData { |
| 107 | DecodedImages decoded_images; |
| 108 | }; |
| 109 | |
asargent | cac81511 | 2015-06-08 18:52:50 | [diff] [blame] | 110 | Unpacker::Unpacker(const base::FilePath& working_dir, |
| 111 | const base::FilePath& extension_dir, |
[email protected] | b3fe68d | 2012-07-16 19:14:39 | [diff] [blame] | 112 | const std::string& extension_id, |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 113 | Manifest::Location location, |
[email protected] | b3fe68d | 2012-07-16 19:14:39 | [diff] [blame] | 114 | int creation_flags) |
asargent | cac81511 | 2015-06-08 18:52:50 | [diff] [blame] | 115 | : working_dir_(working_dir), |
| 116 | extension_dir_(extension_dir), |
[email protected] | f5bf184 | 2012-02-15 02:52:26 | [diff] [blame] | 117 | extension_id_(extension_id), |
| 118 | location_(location), |
[email protected] | fc38935a | 2011-10-31 23:53:28 | [diff] [blame] | 119 | creation_flags_(creation_flags) { |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 120 | internal_data_.reset(new InternalData()); |
[email protected] | 3bb8499 | 2010-08-26 17:23:46 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | b3fe68d | 2012-07-16 19:14:39 | [diff] [blame] | 123 | Unpacker::~Unpacker() { |
[email protected] | 3bb8499 | 2010-08-26 17:23:46 | [diff] [blame] | 124 | } |
| 125 | |
meacer | b040d3a | 2016-09-09 03:20:20 | [diff] [blame] | 126 | // static |
meacer | bd73506 | 2016-09-28 19:58:37 | [diff] [blame] | 127 | bool Unpacker::ShouldExtractFile(bool is_theme, |
| 128 | const base::FilePath& file_path) { |
| 129 | if (is_theme) { |
| 130 | const base::FilePath::StringType extension = |
| 131 | base::ToLowerASCII(file_path.FinalExtension()); |
| 132 | // Allow filenames with no extension. |
| 133 | if (extension.empty()) |
| 134 | return true; |
| 135 | return std::find(kAllowedThemeFiletypes, |
| 136 | kAllowedThemeFiletypes + arraysize(kAllowedThemeFiletypes), |
| 137 | extension) != |
| 138 | (kAllowedThemeFiletypes + arraysize(kAllowedThemeFiletypes)); |
| 139 | } |
meacer | b040d3a | 2016-09-09 03:20:20 | [diff] [blame] | 140 | return !base::FilePath::CompareEqualIgnoreCase(file_path.FinalExtension(), |
| 141 | FILE_PATH_LITERAL(".exe")); |
| 142 | } |
| 143 | |
meacer | bd73506 | 2016-09-28 19:58:37 | [diff] [blame] | 144 | // static |
| 145 | bool Unpacker::IsManifestFile(const base::FilePath& file_path) { |
| 146 | CHECK(!file_path.IsAbsolute()); |
| 147 | return base::FilePath::CompareEqualIgnoreCase(file_path.value(), |
| 148 | kManifestFilename); |
| 149 | } |
| 150 | |
| 151 | // static |
| 152 | std::unique_ptr<base::DictionaryValue> Unpacker::ReadManifest( |
| 153 | const base::FilePath& extension_dir, |
| 154 | std::string* error) { |
| 155 | DCHECK(error); |
| 156 | base::FilePath manifest_path = extension_dir.Append(kManifestFilename); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 157 | if (!base::PathExists(manifest_path)) { |
meacer | bd73506 | 2016-09-28 19:58:37 | [diff] [blame] | 158 | *error = errors::kInvalidManifest; |
| 159 | return nullptr; |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 160 | } |
| 161 | |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 162 | JSONFileValueDeserializer deserializer(manifest_path); |
meacer | bd73506 | 2016-09-28 19:58:37 | [diff] [blame] | 163 | std::unique_ptr<base::Value> root = deserializer.Deserialize(NULL, error); |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 164 | if (!root.get()) { |
meacer | bd73506 | 2016-09-28 19:58:37 | [diff] [blame] | 165 | return nullptr; |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 166 | } |
| 167 | |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 168 | if (!root->IsType(base::Value::Type::DICTIONARY)) { |
meacer | bd73506 | 2016-09-28 19:58:37 | [diff] [blame] | 169 | *error = errors::kInvalidManifest; |
| 170 | return nullptr; |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 171 | } |
| 172 | |
dcheng | e59eca160 | 2015-12-18 17:48:00 | [diff] [blame] | 173 | return base::DictionaryValue::From(std::move(root)); |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 174 | } |
| 175 | |
lazyboy | 4c9991d | 2016-12-16 23:54:03 | [diff] [blame] | 176 | bool Unpacker::ReadAllMessageCatalogs() { |
asargent | cac81511 | 2015-06-08 18:52:50 | [diff] [blame] | 177 | base::FilePath locales_path = extension_dir_.Append(kLocaleFolder); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 178 | |
[email protected] | 1acbb4b6 | 2010-03-09 17:52:29 | [diff] [blame] | 179 | // Not all folders under _locales have to be valid locales. |
asargent | 9156f029 | 2015-01-15 01:07:02 | [diff] [blame] | 180 | base::FileEnumerator locales(locales_path, false, |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 181 | base::FileEnumerator::DIRECTORIES); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 182 | |
[email protected] | 1acbb4b6 | 2010-03-09 17:52:29 | [diff] [blame] | 183 | std::set<std::string> all_locales; |
| 184 | extension_l10n_util::GetAllLocales(&all_locales); |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 185 | base::FilePath locale_path; |
[email protected] | 1acbb4b6 | 2010-03-09 17:52:29 | [diff] [blame] | 186 | while (!(locale_path = locales.Next()).empty()) { |
| 187 | if (extension_l10n_util::ShouldSkipValidation(locales_path, locale_path, |
| 188 | all_locales)) |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 189 | continue; |
| 190 | |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 191 | base::FilePath messages_path = locale_path.Append(kMessagesFilename); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 192 | |
| 193 | if (!ReadMessageCatalog(messages_path)) |
| 194 | return false; |
[email protected] | 1acbb4b6 | 2010-03-09 17:52:29 | [diff] [blame] | 195 | } |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 196 | |
| 197 | return true; |
| 198 | } |
| 199 | |
[email protected] | b3fe68d | 2012-07-16 19:14:39 | [diff] [blame] | 200 | bool Unpacker::Run() { |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 201 | // Parse the manifest. |
dschuyler | 3fe4bfc | 2016-09-28 01:11:29 | [diff] [blame] | 202 | std::string error; |
meacer | bd73506 | 2016-09-28 19:58:37 | [diff] [blame] | 203 | parsed_manifest_ = ReadManifest(extension_dir_, &error); |
| 204 | if (!parsed_manifest_.get()) { |
| 205 | SetError(error); |
| 206 | return false; |
| 207 | } |
| 208 | |
asargent | 9156f029 | 2015-01-15 01:07:02 | [diff] [blame] | 209 | scoped_refptr<Extension> extension( |
asargent | cac81511 | 2015-06-08 18:52:50 | [diff] [blame] | 210 | Extension::Create(extension_dir_, location_, *parsed_manifest_, |
asargent | 9156f029 | 2015-01-15 01:07:02 | [diff] [blame] | 211 | creation_flags_, extension_id_, &error)); |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 212 | if (!extension.get()) { |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 213 | SetError(error); |
| 214 | return false; |
| 215 | } |
[email protected] | 99872e3 | 2009-09-25 22:02:49 | [diff] [blame] | 216 | |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 217 | std::vector<InstallWarning> warnings; |
[email protected] | 85df9d1 | 2014-04-15 17:02:14 | [diff] [blame] | 218 | if (!file_util::ValidateExtension(extension.get(), &error, &warnings)) { |
[email protected] | 99872e3 | 2009-09-25 22:02:49 | [diff] [blame] | 219 | SetError(error); |
| 220 | return false; |
| 221 | } |
[email protected] | ab55c2b | 2012-06-01 23:55:03 | [diff] [blame] | 222 | extension->AddInstallWarnings(warnings); |
[email protected] | 99872e3 | 2009-09-25 22:02:49 | [diff] [blame] | 223 | |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 224 | // Decode any images that the browser needs to display. |
[email protected] | 72b49d4 | 2013-04-19 12:47:31 | [diff] [blame] | 225 | std::set<base::FilePath> image_paths = |
rockot | ec1e64b | 2014-11-13 22:06:51 | [diff] [blame] | 226 | ExtensionsClient::Get()->GetBrowserImagePaths(extension.get()); |
estade | 82eaf58 | 2015-12-29 17:51:31 | [diff] [blame] | 227 | for (const base::FilePath& path : image_paths) { |
| 228 | if (!AddDecodedImage(path)) |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 229 | return false; // Error was already reported. |
| 230 | } |
| 231 | |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 232 | // Parse all message catalogs (if any). |
[email protected] | a371b4b | 2013-06-18 20:29:27 | [diff] [blame] | 233 | parsed_catalogs_.reset(new base::DictionaryValue); |
[email protected] | 5c6ac84 | 2013-06-02 23:37:03 | [diff] [blame] | 234 | if (!LocaleInfo::GetDefaultLocale(extension.get()).empty()) { |
lazyboy | 4c9991d | 2016-12-16 23:54:03 | [diff] [blame] | 235 | if (!ReadAllMessageCatalogs()) |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 236 | return false; // Error was already reported. |
| 237 | } |
| 238 | |
Karan Bhatia | 71f6a62 | 2017-10-02 19:39:10 | [diff] [blame] | 239 | return ReadJSONRulesetIfNeeded(extension.get()) && DumpImagesToFile() && |
| 240 | DumpMessageCatalogsToFile(); |
| 241 | } |
| 242 | |
| 243 | bool Unpacker::ReadJSONRulesetIfNeeded(const Extension* extension) { |
| 244 | const ExtensionResource* resource = |
| 245 | declarative_net_request::DNRManifestData::GetRulesetResource(extension); |
| 246 | // The extension did not provide a ruleset. |
| 247 | if (!resource) |
| 248 | return true; |
| 249 | |
| 250 | std::string error; |
| 251 | JSONFileValueDeserializer deserializer(resource->GetFilePath()); |
| 252 | std::unique_ptr<base::Value> root = deserializer.Deserialize(nullptr, &error); |
| 253 | if (!root) { |
| 254 | SetError(error); |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | if (!root->is_list()) { |
| 259 | SetError(errors::kDeclarativeNetRequestListNotPassed); |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | parsed_json_ruleset_ = base::ListValue::From(std::move(root)); |
| 264 | return true; |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 265 | } |
| 266 | |
[email protected] | b3fe68d | 2012-07-16 19:14:39 | [diff] [blame] | 267 | bool Unpacker::DumpImagesToFile() { |
[email protected] | facd7a765 | 2009-06-05 23:15:02 | [diff] [blame] | 268 | IPC::Message pickle; // We use a Message so we can use WriteParam. |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 269 | IPC::WriteParam(&pickle, internal_data_->decoded_images); |
[email protected] | facd7a765 | 2009-06-05 23:15:02 | [diff] [blame] | 270 | |
asargent | cac81511 | 2015-06-08 18:52:50 | [diff] [blame] | 271 | base::FilePath path = working_dir_.AppendASCII(kDecodedImagesFilename); |
[email protected] | fc006cac | 2013-09-17 22:43:31 | [diff] [blame] | 272 | if (!WritePickle(pickle, path)) { |
[email protected] | facd7a765 | 2009-06-05 23:15:02 | [diff] [blame] | 273 | SetError("Could not write image data to disk."); |
| 274 | return false; |
| 275 | } |
| 276 | |
| 277 | return true; |
| 278 | } |
| 279 | |
[email protected] | b3fe68d | 2012-07-16 19:14:39 | [diff] [blame] | 280 | bool Unpacker::DumpMessageCatalogsToFile() { |
[email protected] | 6d37714 | 2010-03-17 20:36:05 | [diff] [blame] | 281 | IPC::Message pickle; |
| 282 | IPC::WriteParam(&pickle, *parsed_catalogs_.get()); |
| 283 | |
asargent | 9156f029 | 2015-01-15 01:07:02 | [diff] [blame] | 284 | base::FilePath path = |
asargent | cac81511 | 2015-06-08 18:52:50 | [diff] [blame] | 285 | working_dir_.AppendASCII(kDecodedMessageCatalogsFilename); |
[email protected] | fc006cac | 2013-09-17 22:43:31 | [diff] [blame] | 286 | if (!WritePickle(pickle, path)) { |
[email protected] | 6d37714 | 2010-03-17 20:36:05 | [diff] [blame] | 287 | SetError("Could not write message catalogs to disk."); |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | return true; |
| 292 | } |
| 293 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 294 | bool Unpacker::AddDecodedImage(const base::FilePath& path) { |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 295 | // Make sure it's not referencing a file outside the extension's subdir. |
| 296 | if (path.IsAbsolute() || PathContainsParentDirectory(path)) { |
asargent | 9156f029 | 2015-01-15 01:07:02 | [diff] [blame] | 297 | SetUTF16Error(l10n_util::GetStringFUTF16( |
| 298 | IDS_EXTENSION_PACKAGE_IMAGE_PATH_ERROR, |
| 299 | base::i18n::GetDisplayStringInLTRDirectionality( |
| 300 | path.LossyDisplayName()))); |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 301 | return false; |
| 302 | } |
| 303 | |
asargent | cac81511 | 2015-06-08 18:52:50 | [diff] [blame] | 304 | SkBitmap image_bitmap = DecodeImage(extension_dir_.Append(path)); |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 305 | if (image_bitmap.isNull()) { |
asargent | 9156f029 | 2015-01-15 01:07:02 | [diff] [blame] | 306 | SetUTF16Error(l10n_util::GetStringFUTF16( |
| 307 | IDS_EXTENSION_PACKAGE_IMAGE_ERROR, |
| 308 | base::i18n::GetDisplayStringInLTRDirectionality( |
| 309 | path.BaseName().LossyDisplayName()))); |
[email protected] | 902f7cd | 2009-05-22 19:02:19 | [diff] [blame] | 310 | return false; |
| 311 | } |
| 312 | |
tzik | b2f765b | 2016-03-09 00:51:45 | [diff] [blame] | 313 | internal_data_->decoded_images.push_back(std::make_tuple(image_bitmap, path)); |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 314 | return true; |
| 315 | } |
| 316 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 317 | bool Unpacker::ReadMessageCatalog(const base::FilePath& message_path) { |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 318 | std::string error; |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 319 | JSONFileValueDeserializer deserializer(message_path); |
dcheng | 4a7e93b | 2016-04-23 00:27:16 | [diff] [blame] | 320 | std::unique_ptr<base::DictionaryValue> root = |
olli.raula | ba04525 | 2015-10-16 06:16:40 | [diff] [blame] | 321 | base::DictionaryValue::From(deserializer.Deserialize(NULL, &error)); |
[email protected] | 6d37714 | 2010-03-17 20:36:05 | [diff] [blame] | 322 | if (!root.get()) { |
[email protected] | 05aab99c | 2013-12-20 09:03:15 | [diff] [blame] | 323 | base::string16 messages_file = message_path.LossyDisplayName(); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 324 | if (error.empty()) { |
| 325 | // If file is missing, Deserialize will fail with empty error. |
[email protected] | 93f1052 | 2010-10-31 16:27:48 | [diff] [blame] | 326 | SetError(base::StringPrintf("%s %s", errors::kLocalesMessagesFileMissing, |
[email protected] | c91b9d4 | 2013-12-25 00:53:42 | [diff] [blame] | 327 | base::UTF16ToUTF8(messages_file).c_str())); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 328 | } else { |
asargent | 9156f029 | 2015-01-15 01:07:02 | [diff] [blame] | 329 | SetError(base::StringPrintf( |
| 330 | "%s: %s", base::UTF16ToUTF8(messages_file).c_str(), error.c_str())); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 331 | } |
| 332 | return false; |
| 333 | } |
| 334 | |
[email protected] | a732916 | 2013-02-07 19:21:48 | [diff] [blame] | 335 | base::FilePath relative_path; |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 336 | // message_path was created from temp_install_dir. This should never fail. |
asargent | cac81511 | 2015-06-08 18:52:50 | [diff] [blame] | 337 | if (!extension_dir_.AppendRelativePath(message_path, &relative_path)) { |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 338 | NOTREACHED(); |
[email protected] | 967d18b | 2011-03-02 22:22:07 | [diff] [blame] | 339 | return false; |
| 340 | } |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 341 | |
[email protected] | 967d18b | 2011-03-02 22:22:07 | [diff] [blame] | 342 | std::string dir_name = relative_path.DirName().MaybeAsASCII(); |
| 343 | if (dir_name.empty()) { |
| 344 | NOTREACHED(); |
| 345 | return false; |
| 346 | } |
jdoerrie | 6ff270ca | 2017-06-07 10:31:45 | [diff] [blame] | 347 | parsed_catalogs_->Set(dir_name, std::move(root)); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 348 | |
| 349 | return true; |
| 350 | } |
| 351 | |
asargent | 9156f029 | 2015-01-15 01:07:02 | [diff] [blame] | 352 | void Unpacker::SetError(const std::string& error) { |
[email protected] | c91b9d4 | 2013-12-25 00:53:42 | [diff] [blame] | 353 | SetUTF16Error(base::UTF8ToUTF16(error)); |
[email protected] | 3367f3a | 2012-09-01 02:40:06 | [diff] [blame] | 354 | } |
| 355 | |
[email protected] | 05aab99c | 2013-12-20 09:03:15 | [diff] [blame] | 356 | void Unpacker::SetUTF16Error(const base::string16& error) { |
[email protected] | 3367f3a | 2012-09-01 02:40:06 | [diff] [blame] | 357 | error_message_ = error; |
[email protected] | 1fca149 | 2009-05-15 22:23:43 | [diff] [blame] | 358 | } |
[email protected] | b3fe68d | 2012-07-16 19:14:39 | [diff] [blame] | 359 | |
| 360 | } // namespace extensions |