Update Crashpad to 1baff4ff92fe1a1ead6b88b5f01633a4f0b6b51c.
[chromium-blink-merge.git] / extensions / utility / unpacker.cc
blobc61faa24f53e749d570ae5f0b5b701e1cbd6da02
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/utility/unpacker.h"
7 #include <set>
9 #include "base/files/file_enumerator.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/i18n/rtl.h"
13 #include "base/json/json_file_value_serializer.h"
14 #include "base/numerics/safe_conversions.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/threading/thread.h"
18 #include "base/values.h"
19 #include "content/public/child/image_decoder_utils.h"
20 #include "content/public/common/common_param_traits.h"
21 #include "extensions/common/constants.h"
22 #include "extensions/common/extension.h"
23 #include "extensions/common/extension_l10n_util.h"
24 #include "extensions/common/extension_utility_messages.h"
25 #include "extensions/common/extensions_client.h"
26 #include "extensions/common/file_util.h"
27 #include "extensions/common/manifest.h"
28 #include "extensions/common/manifest_constants.h"
29 #include "extensions/common/manifest_handlers/default_locale_handler.h"
30 #include "extensions/strings/grit/extensions_strings.h"
31 #include "ipc/ipc_message_utils.h"
32 #include "net/base/file_stream.h"
33 #include "third_party/skia/include/core/SkBitmap.h"
34 #include "third_party/zlib/google/zip.h"
35 #include "ui/base/l10n/l10n_util.h"
36 #include "ui/gfx/geometry/size.h"
38 namespace extensions {
40 namespace {
42 namespace errors = manifest_errors;
43 namespace keys = manifest_keys;
45 // A limit to stop us passing dangerously large canvases to the browser.
46 const int kMaxImageCanvas = 4096 * 4096;
48 SkBitmap DecodeImage(const base::FilePath& path) {
49 // Read the file from disk.
50 std::string file_contents;
51 if (!base::PathExists(path) ||
52 !base::ReadFileToString(path, &file_contents)) {
53 return SkBitmap();
56 // Decode the image using WebKit's image decoder.
57 const unsigned char* data =
58 reinterpret_cast<const unsigned char*>(file_contents.data());
59 SkBitmap bitmap =
60 content::DecodeImage(data, gfx::Size(), file_contents.length());
61 if (bitmap.computeSize64() > kMaxImageCanvas)
62 return SkBitmap();
63 return bitmap;
66 bool PathContainsParentDirectory(const base::FilePath& path) {
67 const base::FilePath::StringType kSeparators(base::FilePath::kSeparators);
68 const base::FilePath::StringType kParentDirectory(
69 base::FilePath::kParentDirectory);
70 const size_t npos = base::FilePath::StringType::npos;
71 const base::FilePath::StringType& value = path.value();
73 for (size_t i = 0; i < value.length();) {
74 i = value.find(kParentDirectory, i);
75 if (i != npos) {
76 if ((i == 0 || kSeparators.find(value[i - 1]) == npos) &&
77 (i + 1 < value.length() || kSeparators.find(value[i + 1]) == npos)) {
78 return true;
80 ++i;
84 return false;
87 bool WritePickle(const IPC::Message& pickle, const base::FilePath& dest_path) {
88 int size = base::checked_cast<int>(pickle.size());
89 const char* data = static_cast<const char*>(pickle.data());
90 int bytes_written = base::WriteFile(dest_path, data, size);
91 return (bytes_written == size);
94 } // namespace
96 struct Unpacker::InternalData {
97 DecodedImages decoded_images;
100 Unpacker::Unpacker(const base::FilePath& extension_path,
101 const std::string& extension_id,
102 Manifest::Location location,
103 int creation_flags)
104 : extension_path_(extension_path),
105 extension_id_(extension_id),
106 location_(location),
107 creation_flags_(creation_flags) {
108 internal_data_.reset(new InternalData());
111 Unpacker::~Unpacker() {
114 base::DictionaryValue* Unpacker::ReadManifest() {
115 base::FilePath manifest_path = temp_install_dir_.Append(kManifestFilename);
116 if (!base::PathExists(manifest_path)) {
117 SetError(errors::kInvalidManifest);
118 return NULL;
121 JSONFileValueDeserializer deserializer(manifest_path);
122 std::string error;
123 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, &error));
124 if (!root.get()) {
125 SetError(error);
126 return NULL;
129 if (!root->IsType(base::Value::TYPE_DICTIONARY)) {
130 SetError(errors::kInvalidManifest);
131 return NULL;
134 return static_cast<base::DictionaryValue*>(root.release());
137 bool Unpacker::ReadAllMessageCatalogs(const std::string& default_locale) {
138 base::FilePath locales_path = temp_install_dir_.Append(kLocaleFolder);
140 // Not all folders under _locales have to be valid locales.
141 base::FileEnumerator locales(locales_path, false,
142 base::FileEnumerator::DIRECTORIES);
144 std::set<std::string> all_locales;
145 extension_l10n_util::GetAllLocales(&all_locales);
146 base::FilePath locale_path;
147 while (!(locale_path = locales.Next()).empty()) {
148 if (extension_l10n_util::ShouldSkipValidation(locales_path, locale_path,
149 all_locales))
150 continue;
152 base::FilePath messages_path = locale_path.Append(kMessagesFilename);
154 if (!ReadMessageCatalog(messages_path))
155 return false;
158 return true;
161 bool Unpacker::Run() {
162 DVLOG(1) << "Installing extension " << extension_path_.value();
164 // <profile>/Extensions/CRX_INSTALL
165 temp_install_dir_ = extension_path_.DirName().AppendASCII(kTempExtensionName);
167 if (!base::CreateDirectory(temp_install_dir_)) {
168 SetUTF16Error(l10n_util::GetStringFUTF16(
169 IDS_EXTENSION_PACKAGE_DIRECTORY_ERROR,
170 base::i18n::GetDisplayStringInLTRDirectionality(
171 temp_install_dir_.LossyDisplayName())));
172 return false;
175 if (!zip::Unzip(extension_path_, temp_install_dir_)) {
176 SetUTF16Error(l10n_util::GetStringUTF16(IDS_EXTENSION_PACKAGE_UNZIP_ERROR));
177 return false;
180 // Parse the manifest.
181 parsed_manifest_.reset(ReadManifest());
182 if (!parsed_manifest_.get())
183 return false; // Error was already reported.
185 std::string error;
186 scoped_refptr<Extension> extension(
187 Extension::Create(temp_install_dir_, location_, *parsed_manifest_,
188 creation_flags_, extension_id_, &error));
189 if (!extension.get()) {
190 SetError(error);
191 return false;
194 std::vector<InstallWarning> warnings;
195 if (!file_util::ValidateExtension(extension.get(), &error, &warnings)) {
196 SetError(error);
197 return false;
199 extension->AddInstallWarnings(warnings);
201 // Decode any images that the browser needs to display.
202 std::set<base::FilePath> image_paths =
203 ExtensionsClient::Get()->GetBrowserImagePaths(extension.get());
204 for (std::set<base::FilePath>::iterator it = image_paths.begin();
205 it != image_paths.end(); ++it) {
206 if (!AddDecodedImage(*it))
207 return false; // Error was already reported.
210 // Parse all message catalogs (if any).
211 parsed_catalogs_.reset(new base::DictionaryValue);
212 if (!LocaleInfo::GetDefaultLocale(extension.get()).empty()) {
213 if (!ReadAllMessageCatalogs(LocaleInfo::GetDefaultLocale(extension.get())))
214 return false; // Error was already reported.
217 return true;
220 bool Unpacker::DumpImagesToFile() {
221 IPC::Message pickle; // We use a Message so we can use WriteParam.
222 IPC::WriteParam(&pickle, internal_data_->decoded_images);
224 base::FilePath path =
225 extension_path_.DirName().AppendASCII(kDecodedImagesFilename);
226 if (!WritePickle(pickle, path)) {
227 SetError("Could not write image data to disk.");
228 return false;
231 return true;
234 bool Unpacker::DumpMessageCatalogsToFile() {
235 IPC::Message pickle;
236 IPC::WriteParam(&pickle, *parsed_catalogs_.get());
238 base::FilePath path =
239 extension_path_.DirName().AppendASCII(kDecodedMessageCatalogsFilename);
240 if (!WritePickle(pickle, path)) {
241 SetError("Could not write message catalogs to disk.");
242 return false;
245 return true;
248 bool Unpacker::AddDecodedImage(const base::FilePath& path) {
249 // Make sure it's not referencing a file outside the extension's subdir.
250 if (path.IsAbsolute() || PathContainsParentDirectory(path)) {
251 SetUTF16Error(l10n_util::GetStringFUTF16(
252 IDS_EXTENSION_PACKAGE_IMAGE_PATH_ERROR,
253 base::i18n::GetDisplayStringInLTRDirectionality(
254 path.LossyDisplayName())));
255 return false;
258 SkBitmap image_bitmap = DecodeImage(temp_install_dir_.Append(path));
259 if (image_bitmap.isNull()) {
260 SetUTF16Error(l10n_util::GetStringFUTF16(
261 IDS_EXTENSION_PACKAGE_IMAGE_ERROR,
262 base::i18n::GetDisplayStringInLTRDirectionality(
263 path.BaseName().LossyDisplayName())));
264 return false;
267 internal_data_->decoded_images.push_back(MakeTuple(image_bitmap, path));
268 return true;
271 bool Unpacker::ReadMessageCatalog(const base::FilePath& message_path) {
272 std::string error;
273 JSONFileValueDeserializer deserializer(message_path);
274 scoped_ptr<base::DictionaryValue> root(static_cast<base::DictionaryValue*>(
275 deserializer.Deserialize(NULL, &error)));
276 if (!root.get()) {
277 base::string16 messages_file = message_path.LossyDisplayName();
278 if (error.empty()) {
279 // If file is missing, Deserialize will fail with empty error.
280 SetError(base::StringPrintf("%s %s", errors::kLocalesMessagesFileMissing,
281 base::UTF16ToUTF8(messages_file).c_str()));
282 } else {
283 SetError(base::StringPrintf(
284 "%s: %s", base::UTF16ToUTF8(messages_file).c_str(), error.c_str()));
286 return false;
289 base::FilePath relative_path;
290 // message_path was created from temp_install_dir. This should never fail.
291 if (!temp_install_dir_.AppendRelativePath(message_path, &relative_path)) {
292 NOTREACHED();
293 return false;
296 std::string dir_name = relative_path.DirName().MaybeAsASCII();
297 if (dir_name.empty()) {
298 NOTREACHED();
299 return false;
301 parsed_catalogs_->Set(dir_name, root.release());
303 return true;
306 void Unpacker::SetError(const std::string& error) {
307 SetUTF16Error(base::UTF8ToUTF16(error));
310 void Unpacker::SetUTF16Error(const base::string16& error) {
311 error_message_ = error;
314 } // namespace extensions