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 "content/public/utility/utility_thread.h"
10 #include "extensions/common/extension.h"
11 #include "extensions/common/extension_l10n_util.h"
12 #include "extensions/common/extension_utility_messages.h"
13 #include "extensions/common/extensions_client.h"
14 #include "extensions/common/manifest.h"
15 #include "extensions/common/update_manifest.h"
16 #include "extensions/utility/unpacker.h"
17 #include "ipc/ipc_message.h"
18 #include "ipc/ipc_message_macros.h"
19 #include "third_party/zlib/google/zip.h"
20 #include "ui/base/ui_base_switches.h"
22 namespace extensions
{
26 bool Send(IPC::Message
* message
) {
27 return content::UtilityThread::Get()->Send(message
);
30 void ReleaseProcessIfNeeded() {
31 content::UtilityThread::Get()->ReleaseProcessIfNeeded();
34 const char kExtensionHandlerUnzipError
[] =
35 "Could not unzip extension for install.";
39 UtilityHandler::UtilityHandler() {
42 UtilityHandler::~UtilityHandler() {
46 void UtilityHandler::UtilityThreadStarted() {
47 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
48 std::string lang
= command_line
->GetSwitchValueASCII(switches::kLang
);
50 extension_l10n_util::SetProcessLocale(lang
);
53 bool UtilityHandler::OnMessageReceived(const IPC::Message
& message
) {
55 IPC_BEGIN_MESSAGE_MAP(UtilityHandler
, message
)
56 IPC_MESSAGE_HANDLER(ExtensionUtilityMsg_ParseUpdateManifest
,
57 OnParseUpdateManifest
)
58 IPC_MESSAGE_HANDLER(ExtensionUtilityMsg_UnzipToDir
, OnUnzipToDir
)
59 IPC_MESSAGE_HANDLER(ExtensionUtilityMsg_UnpackExtension
, OnUnpackExtension
)
60 IPC_MESSAGE_UNHANDLED(handled
= false)
65 void UtilityHandler::OnParseUpdateManifest(const std::string
& xml
) {
66 UpdateManifest manifest
;
67 if (!manifest
.Parse(xml
)) {
68 Send(new ExtensionUtilityHostMsg_ParseUpdateManifest_Failed(
71 Send(new ExtensionUtilityHostMsg_ParseUpdateManifest_Succeeded(
74 ReleaseProcessIfNeeded();
77 void UtilityHandler::OnUnzipToDir(const base::FilePath
& zip_path
,
78 const base::FilePath
& dir
) {
79 if (!zip::Unzip(zip_path
, dir
)) {
80 Send(new ExtensionUtilityHostMsg_UnzipToDir_Failed(
81 std::string(kExtensionHandlerUnzipError
)));
83 Send(new ExtensionUtilityHostMsg_UnzipToDir_Succeeded(dir
));
86 ReleaseProcessIfNeeded();
89 void UtilityHandler::OnUnpackExtension(
90 const base::FilePath
& extension_path
,
91 const std::string
& extension_id
,
94 CHECK_GT(location
, Manifest::INVALID_LOCATION
);
95 CHECK_LT(location
, Manifest::NUM_LOCATIONS
);
96 DCHECK(ExtensionsClient::Get());
97 Unpacker
unpacker(extension_path
,
99 static_cast<Manifest::Location
>(location
),
101 if (unpacker
.Run() && unpacker
.DumpImagesToFile() &&
102 unpacker
.DumpMessageCatalogsToFile()) {
103 Send(new ExtensionUtilityHostMsg_UnpackExtension_Succeeded(
104 *unpacker
.parsed_manifest()));
106 Send(new ExtensionUtilityHostMsg_UnpackExtension_Failed(
107 unpacker
.error_message()));
110 ReleaseProcessIfNeeded();
114 } // namespace extensions