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 "ui/base/ui_base_switches.h"
21 namespace extensions
{
25 bool Send(IPC::Message
* message
) {
26 return content::UtilityThread::Get()->Send(message
);
29 void ReleaseProcessIfNeeded() {
30 content::UtilityThread::Get()->ReleaseProcessIfNeeded();
35 UtilityHandler::UtilityHandler() {
38 UtilityHandler::~UtilityHandler() {
42 void UtilityHandler::UtilityThreadStarted() {
43 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
44 std::string lang
= command_line
->GetSwitchValueASCII(switches::kLang
);
46 extension_l10n_util::SetProcessLocale(lang
);
49 bool UtilityHandler::OnMessageReceived(const IPC::Message
& message
) {
51 IPC_BEGIN_MESSAGE_MAP(UtilityHandler
, message
)
52 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_UnpackExtension
, OnUnpackExtension
)
53 IPC_MESSAGE_HANDLER(ExtensionUtilityMsg_ParseUpdateManifest
,
54 OnParseUpdateManifest
)
55 IPC_MESSAGE_UNHANDLED(handled
= false)
60 void UtilityHandler::OnParseUpdateManifest(const std::string
& xml
) {
61 UpdateManifest manifest
;
62 if (!manifest
.Parse(xml
)) {
63 Send(new ExtensionUtilityHostMsg_ParseUpdateManifest_Failed(
66 Send(new ExtensionUtilityHostMsg_ParseUpdateManifest_Succeeded(
69 ReleaseProcessIfNeeded();
72 void UtilityHandler::OnUnpackExtension(
73 const base::FilePath
& extension_path
,
74 const std::string
& extension_id
,
77 CHECK_GT(location
, Manifest::INVALID_LOCATION
);
78 CHECK_LT(location
, Manifest::NUM_LOCATIONS
);
79 DCHECK(ExtensionsClient::Get());
80 Unpacker
unpacker(extension_path
,
82 static_cast<Manifest::Location
>(location
),
84 if (unpacker
.Run() && unpacker
.DumpImagesToFile() &&
85 unpacker
.DumpMessageCatalogsToFile()) {
86 Send(new ChromeUtilityHostMsg_UnpackExtension_Succeeded(
87 *unpacker
.parsed_manifest()));
89 Send(new ChromeUtilityHostMsg_UnpackExtension_Failed(
90 unpacker
.error_message()));
93 ReleaseProcessIfNeeded();
97 } // namespace extensions