Roll src/third_party/skia a6ae14e:85693c1
[chromium-blink-merge.git] / extensions / utility / utility_handler.cc
blob65cf509dd0d715acb594668532a8325595084205
1 // Copyright 2014 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/utility_handler.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/i18n/rtl.h"
11 #include "content/public/utility/utility_thread.h"
12 #include "extensions/common/constants.h"
13 #include "extensions/common/extension.h"
14 #include "extensions/common/extension_l10n_util.h"
15 #include "extensions/common/extension_utility_messages.h"
16 #include "extensions/common/extensions_client.h"
17 #include "extensions/common/manifest.h"
18 #include "extensions/common/update_manifest.h"
19 #include "extensions/strings/grit/extensions_strings.h"
20 #include "extensions/utility/unpacker.h"
21 #include "ipc/ipc_message.h"
22 #include "ipc/ipc_message_macros.h"
23 #include "third_party/zlib/google/zip.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/base/ui_base_switches.h"
27 namespace extensions {
29 namespace {
31 bool Send(IPC::Message* message) {
32 return content::UtilityThread::Get()->Send(message);
35 void ReleaseProcessIfNeeded() {
36 content::UtilityThread::Get()->ReleaseProcessIfNeeded();
39 const char kExtensionHandlerUnzipError[] =
40 "Could not unzip extension for install.";
42 } // namespace
44 UtilityHandler::UtilityHandler() {
47 UtilityHandler::~UtilityHandler() {
50 // static
51 void UtilityHandler::UtilityThreadStarted() {
52 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
53 std::string lang = command_line->GetSwitchValueASCII(switches::kLang);
54 if (!lang.empty())
55 extension_l10n_util::SetProcessLocale(lang);
58 bool UtilityHandler::OnMessageReceived(const IPC::Message& message) {
59 bool handled = true;
60 IPC_BEGIN_MESSAGE_MAP(UtilityHandler, message)
61 IPC_MESSAGE_HANDLER(ExtensionUtilityMsg_ParseUpdateManifest,
62 OnParseUpdateManifest)
63 IPC_MESSAGE_HANDLER(ExtensionUtilityMsg_UnzipToDir, OnUnzipToDir)
64 IPC_MESSAGE_HANDLER(ExtensionUtilityMsg_UnpackExtension, OnUnpackExtension)
65 IPC_MESSAGE_UNHANDLED(handled = false)
66 IPC_END_MESSAGE_MAP()
67 return handled;
70 void UtilityHandler::OnParseUpdateManifest(const std::string& xml) {
71 UpdateManifest manifest;
72 if (!manifest.Parse(xml)) {
73 Send(new ExtensionUtilityHostMsg_ParseUpdateManifest_Failed(
74 manifest.errors()));
75 } else {
76 Send(new ExtensionUtilityHostMsg_ParseUpdateManifest_Succeeded(
77 manifest.results()));
79 ReleaseProcessIfNeeded();
82 void UtilityHandler::OnUnzipToDir(const base::FilePath& zip_path,
83 const base::FilePath& dir) {
84 if (!zip::Unzip(zip_path, dir)) {
85 Send(new ExtensionUtilityHostMsg_UnzipToDir_Failed(
86 std::string(kExtensionHandlerUnzipError)));
87 } else {
88 Send(new ExtensionUtilityHostMsg_UnzipToDir_Succeeded(dir));
91 ReleaseProcessIfNeeded();
94 void UtilityHandler::OnUnpackExtension(
95 const base::FilePath& extension_path,
96 const std::string& extension_id,
97 int location,
98 int creation_flags) {
99 CHECK_GT(location, Manifest::INVALID_LOCATION);
100 CHECK_LT(location, Manifest::NUM_LOCATIONS);
101 DCHECK(ExtensionsClient::Get());
102 base::FilePath working_dir = extension_path.DirName();
103 base::FilePath unzipped_dir = working_dir.AppendASCII(kTempExtensionName);
104 base::string16 error;
105 if (!base::CreateDirectory(unzipped_dir)) {
106 Send(new ExtensionUtilityHostMsg_UnpackExtension_Failed(
107 l10n_util::GetStringFUTF16(
108 IDS_EXTENSION_PACKAGE_DIRECTORY_ERROR,
109 base::i18n::GetDisplayStringInLTRDirectionality(
110 unzipped_dir.LossyDisplayName()))));
111 } else if (!zip::Unzip(extension_path, unzipped_dir)) {
112 Send(new ExtensionUtilityHostMsg_UnpackExtension_Failed(
113 l10n_util::GetStringUTF16(IDS_EXTENSION_PACKAGE_UNZIP_ERROR)));
114 } else {
115 Unpacker unpacker(working_dir, unzipped_dir, extension_id,
116 static_cast<Manifest::Location>(location),
117 creation_flags);
118 if (unpacker.Run()) {
119 Send(new ExtensionUtilityHostMsg_UnpackExtension_Succeeded(
120 *unpacker.parsed_manifest()));
121 } else {
122 Send(new ExtensionUtilityHostMsg_UnpackExtension_Failed(
123 unpacker.error_message()));
126 ReleaseProcessIfNeeded();
130 } // namespace extensions