diff options
author | asargent <asargent@chromium.org> | 2015-01-14 17:07:02 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-01-15 01:08:11 +0000 |
commit | 9156f029885bb4c54eeb5599a22b7aba0f7dbd3e (patch) | |
tree | bc232e28c29e03d0b3bd7fd145050c247128c732 /extensions/utility | |
parent | f8346eaaa8271f81474f1daf9cc5fcb436d358ac (diff) | |
download | chromium_src-9156f029885bb4c54eeb5599a22b7aba0f7dbd3e.zip chromium_src-9156f029885bb4c54eeb5599a22b7aba0f7dbd3e.tar.gz chromium_src-9156f029885bb4c54eeb5599a22b7aba0f7dbd3e.tar.bz2 |
Refactoring: move extension unpacker from chrome/ to extensions/
This is mostly a series of mechanical changes to move
unpacker.{h,cc} from chrome/utility to extensions/utility.
The unpacker_unittests.cc and associated data files will be moved
in a follow-up CL to keep this one easy to review, because there
are a lot of test data files to move.
BUG=447014
Review URL: https://codereview.chromium.org/818943004
Cr-Commit-Position: refs/heads/master@{#311598}
Diffstat (limited to 'extensions/utility')
-rw-r--r-- | extensions/utility/BUILD.gn | 3 | ||||
-rw-r--r-- | extensions/utility/DEPS | 3 | ||||
-rw-r--r-- | extensions/utility/unpacker.cc | 314 | ||||
-rw-r--r-- | extensions/utility/unpacker.h | 108 |
4 files changed, 428 insertions, 0 deletions
diff --git a/extensions/utility/BUILD.gn b/extensions/utility/BUILD.gn index a88421c..c954746 100644 --- a/extensions/utility/BUILD.gn +++ b/extensions/utility/BUILD.gn @@ -9,6 +9,8 @@ assert(enable_extensions) # GYP version: extensions/extensions.gyp:extensions_utility source_set("utility") { sources = [ + "unpacker.cc", + "unpacker.h", "utility_handler.cc", "utility_handler.h", ] @@ -16,6 +18,7 @@ source_set("utility") { deps = [ "//content/public/utility", "//extensions/common", + "//skia", ] if (is_win) { diff --git a/extensions/utility/DEPS b/extensions/utility/DEPS index 8ad521e..48d2f8d 100644 --- a/extensions/utility/DEPS +++ b/extensions/utility/DEPS @@ -1,3 +1,6 @@ include_rules = [ "+content/public/utility", + "+content/public/child", + "+net", + "+third_party/zlib/google", ] diff --git a/extensions/utility/unpacker.cc b/extensions/utility/unpacker.cc new file mode 100644 index 0000000..d244ba5 --- /dev/null +++ b/extensions/utility/unpacker.cc @@ -0,0 +1,314 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "extensions/utility/unpacker.h" + +#include <set> + +#include "base/files/file_enumerator.h" +#include "base/files/file_util.h" +#include "base/files/scoped_temp_dir.h" +#include "base/i18n/rtl.h" +#include "base/json/json_file_value_serializer.h" +#include "base/numerics/safe_conversions.h" +#include "base/strings/string_util.h" +#include "base/strings/utf_string_conversions.h" +#include "base/threading/thread.h" +#include "base/values.h" +#include "content/public/child/image_decoder_utils.h" +#include "content/public/common/common_param_traits.h" +#include "extensions/common/constants.h" +#include "extensions/common/extension.h" +#include "extensions/common/extension_l10n_util.h" +#include "extensions/common/extension_utility_messages.h" +#include "extensions/common/extensions_client.h" +#include "extensions/common/file_util.h" +#include "extensions/common/manifest.h" +#include "extensions/common/manifest_constants.h" +#include "extensions/common/manifest_handlers/default_locale_handler.h" +#include "extensions/strings/grit/extensions_strings.h" +#include "ipc/ipc_message_utils.h" +#include "net/base/file_stream.h" +#include "third_party/skia/include/core/SkBitmap.h" +#include "third_party/zlib/google/zip.h" +#include "ui/base/l10n/l10n_util.h" +#include "ui/gfx/geometry/size.h" + +namespace extensions { + +namespace { + +namespace errors = manifest_errors; +namespace keys = manifest_keys; + +// A limit to stop us passing dangerously large canvases to the browser. +const int kMaxImageCanvas = 4096 * 4096; + +SkBitmap DecodeImage(const base::FilePath& path) { + // Read the file from disk. + std::string file_contents; + if (!base::PathExists(path) || + !base::ReadFileToString(path, &file_contents)) { + return SkBitmap(); + } + + // Decode the image using WebKit's image decoder. + const unsigned char* data = + reinterpret_cast<const unsigned char*>(file_contents.data()); + SkBitmap bitmap = + content::DecodeImage(data, gfx::Size(), file_contents.length()); + if (bitmap.computeSize64() > kMaxImageCanvas) + return SkBitmap(); + return bitmap; +} + +bool PathContainsParentDirectory(const base::FilePath& path) { + const base::FilePath::StringType kSeparators(base::FilePath::kSeparators); + const base::FilePath::StringType kParentDirectory( + base::FilePath::kParentDirectory); + const size_t npos = base::FilePath::StringType::npos; + const base::FilePath::StringType& value = path.value(); + + for (size_t i = 0; i < value.length();) { + i = value.find(kParentDirectory, i); + if (i != npos) { + if ((i == 0 || kSeparators.find(value[i - 1]) == npos) && + (i + 1 < value.length() || kSeparators.find(value[i + 1]) == npos)) { + return true; + } + ++i; + } + } + + return false; +} + +bool WritePickle(const IPC::Message& pickle, const base::FilePath& dest_path) { + int size = base::checked_cast<int>(pickle.size()); + const char* data = static_cast<const char*>(pickle.data()); + int bytes_written = base::WriteFile(dest_path, data, size); + return (bytes_written == size); +} + +} // namespace + +struct Unpacker::InternalData { + DecodedImages decoded_images; +}; + +Unpacker::Unpacker(const base::FilePath& extension_path, + const std::string& extension_id, + Manifest::Location location, + int creation_flags) + : extension_path_(extension_path), + extension_id_(extension_id), + location_(location), + creation_flags_(creation_flags) { + internal_data_.reset(new InternalData()); +} + +Unpacker::~Unpacker() { +} + +base::DictionaryValue* Unpacker::ReadManifest() { + base::FilePath manifest_path = temp_install_dir_.Append(kManifestFilename); + if (!base::PathExists(manifest_path)) { + SetError(errors::kInvalidManifest); + return NULL; + } + + JSONFileValueSerializer serializer(manifest_path); + std::string error; + scoped_ptr<base::Value> root(serializer.Deserialize(NULL, &error)); + if (!root.get()) { + SetError(error); + return NULL; + } + + if (!root->IsType(base::Value::TYPE_DICTIONARY)) { + SetError(errors::kInvalidManifest); + return NULL; + } + + return static_cast<base::DictionaryValue*>(root.release()); +} + +bool Unpacker::ReadAllMessageCatalogs(const std::string& default_locale) { + base::FilePath locales_path = temp_install_dir_.Append(kLocaleFolder); + + // Not all folders under _locales have to be valid locales. + base::FileEnumerator locales(locales_path, false, + base::FileEnumerator::DIRECTORIES); + + std::set<std::string> all_locales; + extension_l10n_util::GetAllLocales(&all_locales); + base::FilePath locale_path; + while (!(locale_path = locales.Next()).empty()) { + if (extension_l10n_util::ShouldSkipValidation(locales_path, locale_path, + all_locales)) + continue; + + base::FilePath messages_path = locale_path.Append(kMessagesFilename); + + if (!ReadMessageCatalog(messages_path)) + return false; + } + + return true; +} + +bool Unpacker::Run() { + DVLOG(1) << "Installing extension " << extension_path_.value(); + + // <profile>/Extensions/CRX_INSTALL + temp_install_dir_ = extension_path_.DirName().AppendASCII(kTempExtensionName); + + if (!base::CreateDirectory(temp_install_dir_)) { + SetUTF16Error(l10n_util::GetStringFUTF16( + IDS_EXTENSION_PACKAGE_DIRECTORY_ERROR, + base::i18n::GetDisplayStringInLTRDirectionality( + temp_install_dir_.LossyDisplayName()))); + return false; + } + + if (!zip::Unzip(extension_path_, temp_install_dir_)) { + SetUTF16Error(l10n_util::GetStringUTF16(IDS_EXTENSION_PACKAGE_UNZIP_ERROR)); + return false; + } + + // Parse the manifest. + parsed_manifest_.reset(ReadManifest()); + if (!parsed_manifest_.get()) + return false; // Error was already reported. + + std::string error; + scoped_refptr<Extension> extension( + Extension::Create(temp_install_dir_, location_, *parsed_manifest_, + creation_flags_, extension_id_, &error)); + if (!extension.get()) { + SetError(error); + return false; + } + + std::vector<InstallWarning> warnings; + if (!file_util::ValidateExtension(extension.get(), &error, &warnings)) { + SetError(error); + return false; + } + extension->AddInstallWarnings(warnings); + + // Decode any images that the browser needs to display. + std::set<base::FilePath> image_paths = + ExtensionsClient::Get()->GetBrowserImagePaths(extension.get()); + for (std::set<base::FilePath>::iterator it = image_paths.begin(); + it != image_paths.end(); ++it) { + if (!AddDecodedImage(*it)) + return false; // Error was already reported. + } + + // Parse all message catalogs (if any). + parsed_catalogs_.reset(new base::DictionaryValue); + if (!LocaleInfo::GetDefaultLocale(extension.get()).empty()) { + if (!ReadAllMessageCatalogs(LocaleInfo::GetDefaultLocale(extension.get()))) + return false; // Error was already reported. + } + + return true; +} + +bool Unpacker::DumpImagesToFile() { + IPC::Message pickle; // We use a Message so we can use WriteParam. + IPC::WriteParam(&pickle, internal_data_->decoded_images); + + base::FilePath path = + extension_path_.DirName().AppendASCII(kDecodedImagesFilename); + if (!WritePickle(pickle, path)) { + SetError("Could not write image data to disk."); + return false; + } + + return true; +} + +bool Unpacker::DumpMessageCatalogsToFile() { + IPC::Message pickle; + IPC::WriteParam(&pickle, *parsed_catalogs_.get()); + + base::FilePath path = + extension_path_.DirName().AppendASCII(kDecodedMessageCatalogsFilename); + if (!WritePickle(pickle, path)) { + SetError("Could not write message catalogs to disk."); + return false; + } + + return true; +} + +bool Unpacker::AddDecodedImage(const base::FilePath& path) { + // Make sure it's not referencing a file outside the extension's subdir. + if (path.IsAbsolute() || PathContainsParentDirectory(path)) { + SetUTF16Error(l10n_util::GetStringFUTF16( + IDS_EXTENSION_PACKAGE_IMAGE_PATH_ERROR, + base::i18n::GetDisplayStringInLTRDirectionality( + path.LossyDisplayName()))); + return false; + } + + SkBitmap image_bitmap = DecodeImage(temp_install_dir_.Append(path)); + if (image_bitmap.isNull()) { + SetUTF16Error(l10n_util::GetStringFUTF16( + IDS_EXTENSION_PACKAGE_IMAGE_ERROR, + base::i18n::GetDisplayStringInLTRDirectionality( + path.BaseName().LossyDisplayName()))); + return false; + } + + internal_data_->decoded_images.push_back(MakeTuple(image_bitmap, path)); + return true; +} + +bool Unpacker::ReadMessageCatalog(const base::FilePath& message_path) { + std::string error; + JSONFileValueSerializer serializer(message_path); + scoped_ptr<base::DictionaryValue> root(static_cast<base::DictionaryValue*>( + serializer.Deserialize(NULL, &error))); + if (!root.get()) { + base::string16 messages_file = message_path.LossyDisplayName(); + if (error.empty()) { + // If file is missing, Deserialize will fail with empty error. + SetError(base::StringPrintf("%s %s", errors::kLocalesMessagesFileMissing, + base::UTF16ToUTF8(messages_file).c_str())); + } else { + SetError(base::StringPrintf( + "%s: %s", base::UTF16ToUTF8(messages_file).c_str(), error.c_str())); + } + return false; + } + + base::FilePath relative_path; + // message_path was created from temp_install_dir. This should never fail. + if (!temp_install_dir_.AppendRelativePath(message_path, &relative_path)) { + NOTREACHED(); + return false; + } + + std::string dir_name = relative_path.DirName().MaybeAsASCII(); + if (dir_name.empty()) { + NOTREACHED(); + return false; + } + parsed_catalogs_->Set(dir_name, root.release()); + + return true; +} + +void Unpacker::SetError(const std::string& error) { + SetUTF16Error(base::UTF8ToUTF16(error)); +} + +void Unpacker::SetUTF16Error(const base::string16& error) { + error_message_ = error; +} + +} // namespace extensions diff --git a/extensions/utility/unpacker.h b/extensions/utility/unpacker.h new file mode 100644 index 0000000..240172e --- /dev/null +++ b/extensions/utility/unpacker.h @@ -0,0 +1,108 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef EXTENSIONS_UTILITY_UNPACKER_H_ +#define EXTENSIONS_UTILITY_UNPACKER_H_ + +#include <string> +#include <vector> + +#include "base/files/file_path.h" +#include "base/memory/scoped_ptr.h" +#include "extensions/common/manifest.h" + +class SkBitmap; + +namespace base { +class DictionaryValue; +} + +namespace extensions { + +// This class unpacks an extension. It is designed to be used in a sandboxed +// child process. We unpack and parse various bits of the extension, then +// report back to the browser process, who then transcodes the pre-parsed bits +// and writes them back out to disk for later use. +class Unpacker { + public: + Unpacker(const base::FilePath& extension_path, + const std::string& extension_id, + Manifest::Location location, + int creation_flags); + ~Unpacker(); + + // Install the extension file at |extension_path|. Returns true on success. + // Otherwise, error_message will contain a string explaining what went wrong. + bool Run(); + + // Write the decoded images to kDecodedImagesFilename. We do this instead + // of sending them over IPC, since they are so large. Returns true on + // success. + bool DumpImagesToFile(); + + // Write the decoded messages to kDecodedMessageCatalogsFilename. We do this + // instead of sending them over IPC, since they are so large. Returns true on + // success. + bool DumpMessageCatalogsToFile(); + + const base::string16& error_message() { return error_message_; } + base::DictionaryValue* parsed_manifest() { return parsed_manifest_.get(); } + base::DictionaryValue* parsed_catalogs() { return parsed_catalogs_.get(); } + + private: + // Parse the manifest.json file inside the extension (not in the header). + // Caller takes ownership of return value. + base::DictionaryValue* ReadManifest(); + + // Parse all _locales/*/messages.json files inside the extension. + bool ReadAllMessageCatalogs(const std::string& default_locale); + + // Decodes the image at the given path and puts it in our list of decoded + // images. + bool AddDecodedImage(const base::FilePath& path); + + // Parses the catalog at the given path and puts it in our list of parsed + // catalogs. + bool ReadMessageCatalog(const base::FilePath& message_path); + + // Set the error message. + void SetError(const std::string& error); + void SetUTF16Error(const base::string16& error); + + // The extension to unpack. + base::FilePath extension_path_; + + // The extension ID if known. + std::string extension_id_; + + // The location to use for the created extension. + Manifest::Location location_; + + // The creation flags to use with the created extension. + int creation_flags_; + + // The place we unpacked the extension to. + base::FilePath temp_install_dir_; + + // The parsed version of the manifest JSON contained in the extension. + scoped_ptr<base::DictionaryValue> parsed_manifest_; + + // A list of decoded images and the paths where those images came from. Paths + // are relative to the manifest file. + struct InternalData; + scoped_ptr<InternalData> internal_data_; + + // Dictionary of relative paths and catalogs per path. Paths are in the form + // of _locales/locale, without messages.json base part. + scoped_ptr<base::DictionaryValue> parsed_catalogs_; + + // The last error message that was set. Empty if there were no errors. + base::string16 error_message_; + + DISALLOW_COPY_AND_ASSIGN(Unpacker); +}; + +} // namespace extensions + +#endif // EXTENSIONS_UTILITY_UNPACKER_H_ |