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