OLD | NEW |
| (Empty) |
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 | |
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 | |
28 UtilityProcessHost::~UtilityProcessHost() { | |
29 DCHECK(!is_batch_mode_); | |
30 } | |
31 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
111 void UtilityProcessHost::EndBatchMode() { | |
112 CHECK(is_batch_mode_); | |
113 is_batch_mode_ = false; | |
114 Send(new UtilityMsg_BatchMode_Finished()); | |
115 } | |
116 | |
117 FilePath UtilityProcessHost::GetUtilityProcessCmd() { | |
118 return GetChildPath(true); | |
119 } | |
120 | |
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 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess(); | |
145 if (browser_command_line.HasSwitch(switches::kChromeFrame)) | |
146 cmd_line->AppendSwitch(switches::kChromeFrame); | |
147 if (browser_command_line.HasSwitch(switches::kNoSandbox)) | |
148 cmd_line->AppendSwitch(switches::kNoSandbox); | |
149 | |
150 if (browser_command_line.HasSwitch( | |
151 switches::kEnableExperimentalExtensionApis)) { | |
152 cmd_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); | |
153 } | |
154 | |
155 #if defined(OS_POSIX) | |
156 // TODO(port): Sandbox this on Linux. Also, zygote this to work with | |
157 // Linux updating. | |
158 bool has_cmd_prefix = browser_command_line.HasSwitch( | |
159 switches::kUtilityCmdPrefix); | |
160 if (has_cmd_prefix) { | |
161 // launch the utility child process with some prefix (usually "xterm -e gdb | |
162 // --args"). | |
163 cmd_line->PrependWrapper(browser_command_line.GetSwitchValueNative( | |
164 switches::kUtilityCmdPrefix)); | |
165 } | |
166 | |
167 cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir, exposed_dir); | |
168 #endif | |
169 | |
170 Launch( | |
171 #if defined(OS_WIN) | |
172 exposed_dir, | |
173 #elif defined(OS_POSIX) | |
174 false, | |
175 base::environment_vector(), | |
176 #endif | |
177 cmd_line); | |
178 | |
179 return true; | |
180 } | |
181 | |
182 bool UtilityProcessHost::OnMessageReceived(const IPC::Message& message) { | |
183 BrowserThread::PostTask( | |
184 client_thread_id_, FROM_HERE, | |
185 NewRunnableMethod(client_.get(), &Client::OnMessageReceived, message)); | |
186 return true; | |
187 } | |
188 | |
189 void UtilityProcessHost::OnProcessCrashed(int exit_code) { | |
190 BrowserThread::PostTask( | |
191 client_thread_id_, FROM_HERE, | |
192 NewRunnableMethod(client_.get(), &Client::OnProcessCrashed, exit_code)); | |
193 } | |
194 | |
195 bool UtilityProcessHost::CanShutdown() { | |
196 return true; | |
197 } | |
198 | |
199 bool UtilityProcessHost::Client::OnMessageReceived( | |
200 const IPC::Message& message) { | |
201 bool handled = true; | |
202 IPC_BEGIN_MESSAGE_MAP(UtilityProcessHost, message) | |
203 IPC_MESSAGE_HANDLER(UtilityHostMsg_UnpackExtension_Succeeded, | |
204 Client::OnUnpackExtensionSucceeded) | |
205 IPC_MESSAGE_HANDLER(UtilityHostMsg_UnpackExtension_Failed, | |
206 Client::OnUnpackExtensionFailed) | |
207 IPC_MESSAGE_HANDLER(UtilityHostMsg_UnpackWebResource_Succeeded, | |
208 Client::OnUnpackWebResourceSucceeded) | |
209 IPC_MESSAGE_HANDLER(UtilityHostMsg_UnpackWebResource_Failed, | |
210 Client::OnUnpackWebResourceFailed) | |
211 IPC_MESSAGE_HANDLER(UtilityHostMsg_ParseUpdateManifest_Succeeded, | |
212 Client::OnParseUpdateManifestSucceeded) | |
213 IPC_MESSAGE_HANDLER(UtilityHostMsg_ParseUpdateManifest_Failed, | |
214 Client::OnParseUpdateManifestFailed) | |
215 IPC_MESSAGE_HANDLER(UtilityHostMsg_DecodeImage_Succeeded, | |
216 Client::OnDecodeImageSucceeded) | |
217 IPC_MESSAGE_HANDLER(UtilityHostMsg_DecodeImage_Failed, | |
218 Client::OnDecodeImageFailed) | |
219 IPC_MESSAGE_HANDLER(UtilityHostMsg_IDBKeysFromValuesAndKeyPath_Succeeded, | |
220 Client::OnIDBKeysFromValuesAndKeyPathSucceeded) | |
221 IPC_MESSAGE_HANDLER(UtilityHostMsg_IDBKeysFromValuesAndKeyPath_Failed, | |
222 Client::OnIDBKeysFromValuesAndKeyPathFailed) | |
223 IPC_MESSAGE_HANDLER(UtilityHostMsg_InjectIDBKey_Finished, | |
224 Client::OnInjectIDBKeyFinished) | |
225 IPC_MESSAGE_HANDLER(UtilityHostMsg_ParseJSON_Succeeded, | |
226 Client::OnJSONParseSucceeded) | |
227 IPC_MESSAGE_HANDLER(UtilityHostMsg_ParseJSON_Failed, | |
228 Client::OnJSONParseFailed) | |
229 IPC_MESSAGE_UNHANDLED(handled = false) | |
230 IPC_END_MESSAGE_MAP_EX() | |
231 return handled; | |
232 } | |
OLD | NEW |