OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/utility_process_host_impl.h" | 5 #include "content/browser/utility_process_host_impl.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/base_switches.h" | 9 #include "base/base_switches.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
17 #include "base/process/process_handle.h" | 17 #include "base/process/process_handle.h" |
18 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
19 #include "base/sequenced_task_runner.h" | 19 #include "base/sequenced_task_runner.h" |
20 #include "base/strings/stringprintf.h" | |
21 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
22 #include "base/synchronization/lock.h" | 21 #include "base/synchronization/lock.h" |
23 #include "base/synchronization/waitable_event.h" | 22 #include "base/synchronization/waitable_event.h" |
24 #include "build/build_config.h" | 23 #include "build/build_config.h" |
25 #include "content/browser/browser_child_process_host_impl.h" | 24 #include "content/browser/browser_child_process_host_impl.h" |
26 #include "content/browser/mojo/mojo_child_connection.h" | 25 #include "content/browser/mojo/mojo_child_connection.h" |
27 #include "content/browser/mojo/mojo_shell_context.h" | 26 #include "content/browser/mojo/mojo_shell_context.h" |
28 #include "content/browser/renderer_host/render_process_host_impl.h" | 27 #include "content/browser/renderer_host/render_process_host_impl.h" |
29 #include "content/common/child_process_host_impl.h" | 28 #include "content/common/child_process_host_impl.h" |
30 #include "content/common/in_process_child_thread_params.h" | 29 #include "content/common/in_process_child_thread_params.h" |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 is_batch_mode_(false), | 157 is_batch_mode_(false), |
159 no_sandbox_(false), | 158 no_sandbox_(false), |
160 run_elevated_(false), | 159 run_elevated_(false), |
161 #if defined(OS_LINUX) | 160 #if defined(OS_LINUX) |
162 child_flags_(ChildProcessHost::CHILD_ALLOW_SELF), | 161 child_flags_(ChildProcessHost::CHILD_ALLOW_SELF), |
163 #else | 162 #else |
164 child_flags_(ChildProcessHost::CHILD_NORMAL), | 163 child_flags_(ChildProcessHost::CHILD_NORMAL), |
165 #endif | 164 #endif |
166 started_(false), | 165 started_(false), |
167 name_(base::ASCIIToUTF16("utility process")), | 166 name_(base::ASCIIToUTF16("utility process")), |
168 child_token_(mojo::edk::GenerateRandomToken()), | |
169 weak_ptr_factory_(this) { | 167 weak_ptr_factory_(this) { |
170 process_.reset(new BrowserChildProcessHostImpl(PROCESS_TYPE_UTILITY, this, | 168 process_.reset(new BrowserChildProcessHostImpl( |
171 child_token_)); | 169 PROCESS_TYPE_UTILITY, this, kUtilityMojoApplicationName)); |
172 mojo_child_connection_.reset(new MojoChildConnection( | |
173 kUtilityMojoApplicationName, | |
174 base::StringPrintf("%d_0", process_->GetData().id), child_token_, | |
175 MojoShellContext::GetConnectorForIOThread(), | |
176 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))); | |
177 } | 170 } |
178 | 171 |
179 UtilityProcessHostImpl::~UtilityProcessHostImpl() { | 172 UtilityProcessHostImpl::~UtilityProcessHostImpl() { |
180 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 173 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
181 if (is_batch_mode_) | 174 if (is_batch_mode_) |
182 EndBatchMode(); | 175 EndBatchMode(); |
183 } | 176 } |
184 | 177 |
185 base::WeakPtr<UtilityProcessHost> UtilityProcessHostImpl::AsWeakPtr() { | 178 base::WeakPtr<UtilityProcessHost> UtilityProcessHostImpl::AsWeakPtr() { |
186 return weak_ptr_factory_.GetWeakPtr(); | 179 return weak_ptr_factory_.GetWeakPtr(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 env_ = env; | 224 env_ = env; |
232 } | 225 } |
233 | 226 |
234 #endif // OS_POSIX | 227 #endif // OS_POSIX |
235 | 228 |
236 bool UtilityProcessHostImpl::Start() { | 229 bool UtilityProcessHostImpl::Start() { |
237 return StartProcess(); | 230 return StartProcess(); |
238 } | 231 } |
239 | 232 |
240 shell::InterfaceProvider* UtilityProcessHostImpl::GetRemoteInterfaces() { | 233 shell::InterfaceProvider* UtilityProcessHostImpl::GetRemoteInterfaces() { |
241 return mojo_child_connection_->GetRemoteInterfaces(); | 234 return process_->child_connection()->GetRemoteInterfaces(); |
242 } | 235 } |
243 | 236 |
244 void UtilityProcessHostImpl::SetName(const base::string16& name) { | 237 void UtilityProcessHostImpl::SetName(const base::string16& name) { |
245 name_ = name; | 238 name_ = name; |
246 } | 239 } |
247 | 240 |
248 #if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) | 241 #if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) |
249 // static | 242 // static |
250 void UtilityProcessHostImpl::EarlyZygoteLaunch() { | 243 void UtilityProcessHostImpl::EarlyZygoteLaunch() { |
251 DCHECK(!g_utility_zygote); | 244 DCHECK(!g_utility_zygote); |
252 g_utility_zygote = CreateZygote(); | 245 g_utility_zygote = CreateZygote(); |
253 } | 246 } |
254 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) | 247 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) |
255 | 248 |
256 bool UtilityProcessHostImpl::StartProcess() { | 249 bool UtilityProcessHostImpl::StartProcess() { |
257 if (started_) | 250 if (started_) |
258 return true; | 251 return true; |
259 started_ = true; | 252 started_ = true; |
260 | 253 |
261 if (is_batch_mode_) | 254 if (is_batch_mode_) |
262 return true; | 255 return true; |
263 | 256 |
264 process_->SetName(name_); | 257 process_->SetName(name_); |
265 | 258 process_->GetHost()->CreateChannelMojo(); |
266 std::string mojo_channel_token = | |
267 process_->GetHost()->CreateChannelMojo(child_token_); | |
268 if (mojo_channel_token.empty()) { | |
269 NotifyAndDelete(LAUNCH_RESULT_FAILURE); | |
270 return false; | |
271 } | |
272 | 259 |
273 if (RenderProcessHost::run_renderer_in_process()) { | 260 if (RenderProcessHost::run_renderer_in_process()) { |
274 DCHECK(g_utility_main_thread_factory); | 261 DCHECK(g_utility_main_thread_factory); |
275 // See comment in RenderProcessHostImpl::Init() for the background on why we | 262 // See comment in RenderProcessHostImpl::Init() for the background on why we |
276 // support single process mode this way. | 263 // support single process mode this way. |
277 in_process_thread_.reset( | 264 in_process_thread_.reset( |
278 g_utility_main_thread_factory(InProcessChildThreadParams( | 265 g_utility_main_thread_factory(InProcessChildThreadParams( |
279 std::string(), BrowserThread::UnsafeGetMessageLoopForThread( | 266 std::string(), BrowserThread::UnsafeGetMessageLoopForThread( |
280 BrowserThread::IO)->task_runner(), | 267 BrowserThread::IO)->task_runner(), |
281 mojo_channel_token, mojo_child_connection_->service_token()))); | 268 std::string(), |
| 269 process_->child_connection()->service_token()))); |
282 in_process_thread_->Start(); | 270 in_process_thread_->Start(); |
283 } else { | 271 } else { |
284 const base::CommandLine& browser_command_line = | 272 const base::CommandLine& browser_command_line = |
285 *base::CommandLine::ForCurrentProcess(); | 273 *base::CommandLine::ForCurrentProcess(); |
286 | 274 |
287 bool has_cmd_prefix = browser_command_line.HasSwitch( | 275 bool has_cmd_prefix = browser_command_line.HasSwitch( |
288 switches::kUtilityCmdPrefix); | 276 switches::kUtilityCmdPrefix); |
289 | 277 |
290 #if defined(OS_ANDROID) | 278 #if defined(OS_ANDROID) |
291 // readlink("/prof/self/exe") sometimes fails on Android at startup. | 279 // readlink("/prof/self/exe") sometimes fails on Android at startup. |
(...skipping 16 matching lines...) Expand all Loading... |
308 if (exe_path.empty()) { | 296 if (exe_path.empty()) { |
309 NOTREACHED() << "Unable to get utility process binary name."; | 297 NOTREACHED() << "Unable to get utility process binary name."; |
310 return false; | 298 return false; |
311 } | 299 } |
312 | 300 |
313 base::CommandLine* cmd_line = new base::CommandLine(exe_path); | 301 base::CommandLine* cmd_line = new base::CommandLine(exe_path); |
314 #endif | 302 #endif |
315 | 303 |
316 cmd_line->AppendSwitchASCII(switches::kProcessType, | 304 cmd_line->AppendSwitchASCII(switches::kProcessType, |
317 switches::kUtilityProcess); | 305 switches::kUtilityProcess); |
318 cmd_line->AppendSwitchASCII(switches::kMojoChannelToken, | |
319 mojo_channel_token); | |
320 std::string locale = GetContentClient()->browser()->GetApplicationLocale(); | 306 std::string locale = GetContentClient()->browser()->GetApplicationLocale(); |
321 cmd_line->AppendSwitchASCII(switches::kLang, locale); | 307 cmd_line->AppendSwitchASCII(switches::kLang, locale); |
322 | 308 |
323 #if defined(OS_WIN) | 309 #if defined(OS_WIN) |
324 cmd_line->AppendArg(switches::kPrefetchArgumentOther); | 310 cmd_line->AppendArg(switches::kPrefetchArgumentOther); |
325 #endif // defined(OS_WIN) | 311 #endif // defined(OS_WIN) |
326 | 312 |
327 if (no_sandbox_) | 313 if (no_sandbox_) |
328 cmd_line->AppendSwitch(switches::kNoSandbox); | 314 cmd_line->AppendSwitch(switches::kNoSandbox); |
329 | 315 |
(...skipping 19 matching lines...) Expand all Loading... |
349 cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir, | 335 cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir, |
350 exposed_dir_); | 336 exposed_dir_); |
351 } | 337 } |
352 | 338 |
353 #if defined(OS_WIN) | 339 #if defined(OS_WIN) |
354 // Let the utility process know if it is intended to be elevated. | 340 // Let the utility process know if it is intended to be elevated. |
355 if (run_elevated_) | 341 if (run_elevated_) |
356 cmd_line->AppendSwitch(switches::kUtilityProcessRunningElevated); | 342 cmd_line->AppendSwitch(switches::kUtilityProcessRunningElevated); |
357 #endif | 343 #endif |
358 | 344 |
359 cmd_line->AppendSwitchASCII(switches::kMojoApplicationChannelToken, | |
360 mojo_child_connection_->service_token()); | |
361 | |
362 process_->Launch( | 345 process_->Launch( |
363 new UtilitySandboxedProcessLauncherDelegate(exposed_dir_, | 346 new UtilitySandboxedProcessLauncherDelegate(exposed_dir_, |
364 run_elevated_, | 347 run_elevated_, |
365 no_sandbox_, env_, | 348 no_sandbox_, env_, |
366 process_->GetHost()), | 349 process_->GetHost()), |
367 cmd_line, | 350 cmd_line, |
368 true); | 351 true); |
369 } | 352 } |
370 | 353 |
371 return true; | 354 return true; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 base::WeakPtr<UtilityProcessHostImpl> host, | 402 base::WeakPtr<UtilityProcessHostImpl> host, |
420 int error_code) { | 403 int error_code) { |
421 if (!host) | 404 if (!host) |
422 return; | 405 return; |
423 | 406 |
424 host->OnProcessLaunchFailed(error_code); | 407 host->OnProcessLaunchFailed(error_code); |
425 delete host.get(); | 408 delete host.get(); |
426 } | 409 } |
427 | 410 |
428 } // namespace content | 411 } // namespace content |
OLD | NEW |