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
{
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.";
44 UtilityHandler::UtilityHandler() {
47 UtilityHandler::~UtilityHandler() {
51 void UtilityHandler::UtilityThreadStarted() {
52 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
53 std::string lang
= command_line
->GetSwitchValueASCII(switches::kLang
);
55 extension_l10n_util::SetProcessLocale(lang
);
58 bool UtilityHandler::OnMessageReceived(const IPC::Message
& message
) {
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)
70 void UtilityHandler::OnParseUpdateManifest(const std::string
& xml
) {
71 UpdateManifest manifest
;
72 if (!manifest
.Parse(xml
)) {
73 Send(new ExtensionUtilityHostMsg_ParseUpdateManifest_Failed(
76 Send(new ExtensionUtilityHostMsg_ParseUpdateManifest_Succeeded(
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
)));
88 Send(new ExtensionUtilityHostMsg_UnzipToDir_Succeeded(dir
));
91 ReleaseProcessIfNeeded();
94 void UtilityHandler::OnUnpackExtension(const base::FilePath
& directory_path
,
95 const std::string
& extension_id
,
98 CHECK_GT(location
, Manifest::INVALID_LOCATION
);
99 CHECK_LT(location
, Manifest::NUM_LOCATIONS
);
100 DCHECK(ExtensionsClient::Get());
101 content::UtilityThread::Get()->EnsureBlinkInitialized();
102 Unpacker
unpacker(directory_path
.DirName(), directory_path
, extension_id
,
103 static_cast<Manifest::Location
>(location
), creation_flags
);
104 if (unpacker
.Run()) {
105 Send(new ExtensionUtilityHostMsg_UnpackExtension_Succeeded(
106 *unpacker
.parsed_manifest()));
108 Send(new ExtensionUtilityHostMsg_UnpackExtension_Failed(
109 unpacker
.error_message()));
111 ReleaseProcessIfNeeded();
115 } // namespace extensions