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