utility_process_host.cc revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2009 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.
4
5#include "chrome/browser/utility_process_host.h"
6
7#include "app/app_switches.h"
8#include "base/command_line.h"
9#include "base/file_util.h"
10#include "base/message_loop.h"
11#include "base/values.h"
12#include "chrome/browser/browser_process.h"
13#include "chrome/common/chrome_switches.h"
14#include "chrome/common/indexed_db_key.h"
15#include "chrome/common/utility_messages.h"
16#include "ipc/ipc_switches.h"
17#include "third_party/skia/include/core/SkBitmap.h"
18
19UtilityProcessHost::UtilityProcessHost(ResourceDispatcherHost* rdh,
20                                       Client* client,
21                                       ChromeThread::ID client_thread_id)
22    : BrowserChildProcessHost(UTILITY_PROCESS, rdh),
23      client_(client),
24      client_thread_id_(client_thread_id),
25      is_batch_mode_(false) {
26}
27
28UtilityProcessHost::~UtilityProcessHost() {
29  DCHECK(!is_batch_mode_);
30}
31
32bool UtilityProcessHost::StartExtensionUnpacker(const FilePath& extension) {
33  // Grant the subprocess access to the entire subdir the extension file is
34  // in, so that it can unpack to that dir.
35  if (!StartProcess(extension.DirName()))
36    return false;
37
38  Send(new UtilityMsg_UnpackExtension(extension));
39  return true;
40}
41
42bool UtilityProcessHost::StartWebResourceUnpacker(const std::string& data) {
43  if (!StartProcess(FilePath()))
44    return false;
45
46  Send(new UtilityMsg_UnpackWebResource(data));
47  return true;
48}
49
50bool UtilityProcessHost::StartUpdateManifestParse(const std::string& xml) {
51  if (!StartProcess(FilePath()))
52    return false;
53
54  Send(new UtilityMsg_ParseUpdateManifest(xml));
55  return true;
56}
57
58bool UtilityProcessHost::StartImageDecoding(
59    const std::vector<unsigned char>& encoded_data) {
60  if (!StartProcess(FilePath()))
61    return false;
62
63  Send(new UtilityMsg_DecodeImage(encoded_data));
64  return true;
65}
66
67bool UtilityProcessHost::StartIDBKeysFromValuesAndKeyPath(
68    int id, const std::vector<SerializedScriptValue>& serialized_values,
69    const string16& key_path)  {
70  if (!StartProcess(FilePath()))
71    return false;
72
73  Send(new UtilityMsg_IDBKeysFromValuesAndKeyPath(
74      id, serialized_values, key_path));
75  return true;
76}
77
78bool UtilityProcessHost::StartBatchMode()  {
79  CHECK(!is_batch_mode_);
80  is_batch_mode_ = StartProcess(FilePath());
81  Send(new UtilityMsg_BatchMode_Started());
82  return is_batch_mode_;
83}
84
85void UtilityProcessHost::EndBatchMode()  {
86  CHECK(is_batch_mode_);
87  is_batch_mode_ = false;
88  Send(new UtilityMsg_BatchMode_Finished());
89}
90
91FilePath UtilityProcessHost::GetUtilityProcessCmd() {
92  return GetChildPath(true);
93}
94
95bool UtilityProcessHost::StartProcess(const FilePath& exposed_dir) {
96  if (is_batch_mode_)
97    return true;
98  // Name must be set or metrics_service will crash in any test which
99  // launches a UtilityProcessHost.
100  set_name(L"utility process");
101
102  if (!CreateChannel())
103    return false;
104
105  FilePath exe_path = GetUtilityProcessCmd();
106  if (exe_path.empty()) {
107    NOTREACHED() << "Unable to get utility process binary name.";
108    return false;
109  }
110
111  CommandLine* cmd_line = new CommandLine(exe_path);
112  cmd_line->AppendSwitchASCII(switches::kProcessType,
113                              switches::kUtilityProcess);
114  cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id());
115  std::string locale = g_browser_process->GetApplicationLocale();
116  cmd_line->AppendSwitchASCII(switches::kLang, locale);
117
118  SetCrashReporterCommandLine(cmd_line);
119
120  const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
121  if (browser_command_line.HasSwitch(switches::kChromeFrame))
122    cmd_line->AppendSwitch(switches::kChromeFrame);
123
124  if (browser_command_line.HasSwitch(switches::kDisableApps))
125    cmd_line->AppendSwitch(switches::kDisableApps);
126
127  if (browser_command_line.HasSwitch(
128      switches::kEnableExperimentalExtensionApis)) {
129    cmd_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
130  }
131
132#if defined(OS_POSIX)
133  // TODO(port): Sandbox this on Linux.  Also, zygote this to work with
134  // Linux updating.
135  bool has_cmd_prefix = browser_command_line.HasSwitch(
136      switches::kUtilityCmdPrefix);
137  if (has_cmd_prefix) {
138    // launch the utility child process with some prefix (usually "xterm -e gdb
139    // --args").
140    cmd_line->PrependWrapper(browser_command_line.GetSwitchValueNative(
141        switches::kUtilityCmdPrefix));
142  }
143
144  cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir, exposed_dir);
145#endif
146
147  Launch(
148#if defined(OS_WIN)
149      exposed_dir,
150#elif defined(OS_POSIX)
151      false,
152      base::environment_vector(),
153#endif
154      cmd_line);
155
156  return true;
157}
158
159void UtilityProcessHost::OnMessageReceived(const IPC::Message& message) {
160  ChromeThread::PostTask(
161      client_thread_id_, FROM_HERE,
162      NewRunnableMethod(client_.get(), &Client::OnMessageReceived, message));
163}
164
165void UtilityProcessHost::OnProcessCrashed() {
166  ChromeThread::PostTask(
167      client_thread_id_, FROM_HERE,
168      NewRunnableMethod(client_.get(), &Client::OnProcessCrashed));
169}
170
171void UtilityProcessHost::Client::OnMessageReceived(
172    const IPC::Message& message) {
173  IPC_BEGIN_MESSAGE_MAP(UtilityProcessHost, message)
174    IPC_MESSAGE_HANDLER(UtilityHostMsg_UnpackExtension_Succeeded,
175                        Client::OnUnpackExtensionSucceeded)
176    IPC_MESSAGE_HANDLER(UtilityHostMsg_UnpackExtension_Failed,
177                        Client::OnUnpackExtensionFailed)
178    IPC_MESSAGE_HANDLER(UtilityHostMsg_UnpackWebResource_Succeeded,
179                        Client::OnUnpackWebResourceSucceeded)
180    IPC_MESSAGE_HANDLER(UtilityHostMsg_UnpackWebResource_Failed,
181                        Client::OnUnpackWebResourceFailed)
182    IPC_MESSAGE_HANDLER(UtilityHostMsg_ParseUpdateManifest_Succeeded,
183                        Client::OnParseUpdateManifestSucceeded)
184    IPC_MESSAGE_HANDLER(UtilityHostMsg_ParseUpdateManifest_Failed,
185                        Client::OnParseUpdateManifestFailed)
186    IPC_MESSAGE_HANDLER(UtilityHostMsg_DecodeImage_Succeeded,
187                        Client::OnDecodeImageSucceeded)
188    IPC_MESSAGE_HANDLER(UtilityHostMsg_DecodeImage_Failed,
189                        Client::OnDecodeImageFailed)
190    IPC_MESSAGE_HANDLER(UtilityHostMsg_IDBKeysFromValuesAndKeyPath_Succeeded,
191                        Client::OnIDBKeysFromValuesAndKeyPathSucceeded)
192    IPC_MESSAGE_HANDLER(UtilityHostMsg_IDBKeysFromValuesAndKeyPath_Failed,
193                        Client::OnIDBKeysFromValuesAndKeyPathFailed)
194  IPC_END_MESSAGE_MAP_EX()
195}
196