1 // Copyright (c) 2011 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 "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/message_loop.h"
10 #include "base/values.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/utility_messages.h"
14 #include "content/common/indexed_db_key.h"
15 #include "content/common/serialized_script_value.h"
16 #include "ipc/ipc_switches.h"
17 #include "third_party/skia/include/core/SkBitmap.h"
18 #include "ui/base/ui_base_switches.h"
19
UtilityProcessHost(Client * client,BrowserThread::ID client_thread_id)20 UtilityProcessHost::UtilityProcessHost(Client* client,
21 BrowserThread::ID client_thread_id)
22 : BrowserChildProcessHost(UTILITY_PROCESS),
23 client_(client),
24 client_thread_id_(client_thread_id),
25 is_batch_mode_(false) {
26 }
27
~UtilityProcessHost()28 UtilityProcessHost::~UtilityProcessHost() {
29 DCHECK(!is_batch_mode_);
30 }
31
StartExtensionUnpacker(const FilePath & extension)32 bool 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
StartWebResourceUnpacker(const std::string & data)42 bool 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
StartUpdateManifestParse(const std::string & xml)50 bool 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
StartImageDecoding(const std::vector<unsigned char> & encoded_data)58 bool 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
StartImageDecodingBase64(const std::string & base64_encoded_data)67 bool UtilityProcessHost::StartImageDecodingBase64(
68 const std::string& base64_encoded_data) {
69 if (!StartProcess(FilePath()))
70 return false;
71
72 Send(new UtilityMsg_DecodeImageBase64(base64_encoded_data));
73 return true;
74 }
75
StartIDBKeysFromValuesAndKeyPath(int id,const std::vector<SerializedScriptValue> & serialized_values,const string16 & key_path)76 bool UtilityProcessHost::StartIDBKeysFromValuesAndKeyPath(
77 int id, const std::vector<SerializedScriptValue>& serialized_values,
78 const string16& key_path) {
79 if (!StartProcess(FilePath()))
80 return false;
81
82 Send(new UtilityMsg_IDBKeysFromValuesAndKeyPath(
83 id, serialized_values, key_path));
84 return true;
85 }
86
StartInjectIDBKey(const IndexedDBKey & key,const SerializedScriptValue & value,const string16 & key_path)87 bool UtilityProcessHost::StartInjectIDBKey(
88 const IndexedDBKey& key, const SerializedScriptValue& value,
89 const string16& key_path) {
90 if (!StartProcess(FilePath()))
91 return false;
92
93 Send(new UtilityMsg_InjectIDBKey(key, value, key_path));
94 return true;
95 }
96
StartJSONParsing(const std::string & json)97 bool UtilityProcessHost::StartJSONParsing(const std::string& json) {
98 if (!StartProcess(FilePath()))
99 return false;
100 Send(new UtilityMsg_ParseJSON(json));
101 return true;
102 }
103
StartBatchMode()104 bool UtilityProcessHost::StartBatchMode() {
105 CHECK(!is_batch_mode_);
106 is_batch_mode_ = StartProcess(FilePath());
107 Send(new UtilityMsg_BatchMode_Started());
108 return is_batch_mode_;
109 }
110
EndBatchMode()111 void UtilityProcessHost::EndBatchMode() {
112 CHECK(is_batch_mode_);
113 is_batch_mode_ = false;
114 Send(new UtilityMsg_BatchMode_Finished());
115 }
116
GetUtilityProcessCmd()117 FilePath UtilityProcessHost::GetUtilityProcessCmd() {
118 return GetChildPath(true);
119 }
120
StartProcess(const FilePath & exposed_dir)121 bool UtilityProcessHost::StartProcess(const FilePath& exposed_dir) {
122 if (is_batch_mode_)
123 return true;
124 // Name must be set or metrics_service will crash in any test which
125 // launches a UtilityProcessHost.
126 set_name(L"utility process");
127
128 if (!CreateChannel())
129 return false;
130
131 FilePath exe_path = GetUtilityProcessCmd();
132 if (exe_path.empty()) {
133 NOTREACHED() << "Unable to get utility process binary name.";
134 return false;
135 }
136
137 CommandLine* cmd_line = new CommandLine(exe_path);
138 cmd_line->AppendSwitchASCII(switches::kProcessType,
139 switches::kUtilityProcess);
140 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id());
141 std::string locale = g_browser_process->GetApplicationLocale();
142 cmd_line->AppendSwitchASCII(switches::kLang, locale);
143
144 SetCrashReporterCommandLine(cmd_line);
145
146 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
147 if (browser_command_line.HasSwitch(switches::kChromeFrame))
148 cmd_line->AppendSwitch(switches::kChromeFrame);
149 if (browser_command_line.HasSwitch(switches::kNoSandbox))
150 cmd_line->AppendSwitch(switches::kNoSandbox);
151
152 if (browser_command_line.HasSwitch(
153 switches::kEnableExperimentalExtensionApis)) {
154 cmd_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
155 }
156
157 #if defined(OS_POSIX)
158 // TODO(port): Sandbox this on Linux. Also, zygote this to work with
159 // Linux updating.
160 bool has_cmd_prefix = browser_command_line.HasSwitch(
161 switches::kUtilityCmdPrefix);
162 if (has_cmd_prefix) {
163 // launch the utility child process with some prefix (usually "xterm -e gdb
164 // --args").
165 cmd_line->PrependWrapper(browser_command_line.GetSwitchValueNative(
166 switches::kUtilityCmdPrefix));
167 }
168
169 cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir, exposed_dir);
170 #endif
171
172 Launch(
173 #if defined(OS_WIN)
174 exposed_dir,
175 #elif defined(OS_POSIX)
176 false,
177 base::environment_vector(),
178 #endif
179 cmd_line);
180
181 return true;
182 }
183
OnMessageReceived(const IPC::Message & message)184 bool UtilityProcessHost::OnMessageReceived(const IPC::Message& message) {
185 BrowserThread::PostTask(
186 client_thread_id_, FROM_HERE,
187 NewRunnableMethod(client_.get(), &Client::OnMessageReceived, message));
188 return true;
189 }
190
OnProcessCrashed(int exit_code)191 void UtilityProcessHost::OnProcessCrashed(int exit_code) {
192 BrowserThread::PostTask(
193 client_thread_id_, FROM_HERE,
194 NewRunnableMethod(client_.get(), &Client::OnProcessCrashed, exit_code));
195 }
196
CanShutdown()197 bool UtilityProcessHost::CanShutdown() {
198 return true;
199 }
200
OnMessageReceived(const IPC::Message & message)201 bool UtilityProcessHost::Client::OnMessageReceived(
202 const IPC::Message& message) {
203 bool handled = true;
204 IPC_BEGIN_MESSAGE_MAP(UtilityProcessHost, message)
205 IPC_MESSAGE_HANDLER(UtilityHostMsg_UnpackExtension_Succeeded,
206 Client::OnUnpackExtensionSucceeded)
207 IPC_MESSAGE_HANDLER(UtilityHostMsg_UnpackExtension_Failed,
208 Client::OnUnpackExtensionFailed)
209 IPC_MESSAGE_HANDLER(UtilityHostMsg_UnpackWebResource_Succeeded,
210 Client::OnUnpackWebResourceSucceeded)
211 IPC_MESSAGE_HANDLER(UtilityHostMsg_UnpackWebResource_Failed,
212 Client::OnUnpackWebResourceFailed)
213 IPC_MESSAGE_HANDLER(UtilityHostMsg_ParseUpdateManifest_Succeeded,
214 Client::OnParseUpdateManifestSucceeded)
215 IPC_MESSAGE_HANDLER(UtilityHostMsg_ParseUpdateManifest_Failed,
216 Client::OnParseUpdateManifestFailed)
217 IPC_MESSAGE_HANDLER(UtilityHostMsg_DecodeImage_Succeeded,
218 Client::OnDecodeImageSucceeded)
219 IPC_MESSAGE_HANDLER(UtilityHostMsg_DecodeImage_Failed,
220 Client::OnDecodeImageFailed)
221 IPC_MESSAGE_HANDLER(UtilityHostMsg_IDBKeysFromValuesAndKeyPath_Succeeded,
222 Client::OnIDBKeysFromValuesAndKeyPathSucceeded)
223 IPC_MESSAGE_HANDLER(UtilityHostMsg_IDBKeysFromValuesAndKeyPath_Failed,
224 Client::OnIDBKeysFromValuesAndKeyPathFailed)
225 IPC_MESSAGE_HANDLER(UtilityHostMsg_InjectIDBKey_Finished,
226 Client::OnInjectIDBKeyFinished)
227 IPC_MESSAGE_HANDLER(UtilityHostMsg_ParseJSON_Succeeded,
228 Client::OnJSONParseSucceeded)
229 IPC_MESSAGE_HANDLER(UtilityHostMsg_ParseJSON_Failed,
230 Client::OnJSONParseFailed)
231 IPC_MESSAGE_UNHANDLED(handled = false)
232 IPC_END_MESSAGE_MAP_EX()
233 return handled;
234 }
235