OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/child_process_launcher.h" | 5 #include "content/browser/child_process_launcher.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/i18n/icu_util.h" | 13 #include "base/i18n/icu_util.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/metrics/field_trial.h" |
15 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
16 #include "base/process/launch.h" | 17 #include "base/process/launch.h" |
17 #include "base/process/process.h" | 18 #include "base/process/process.h" |
18 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
19 #include "base/synchronization/lock.h" | 20 #include "base/synchronization/lock.h" |
20 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
21 #include "build/build_config.h" | 22 #include "build/build_config.h" |
22 #include "content/public/browser/content_browser_client.h" | 23 #include "content/public/browser/content_browser_client.h" |
23 #include "content/public/common/content_descriptors.h" | 24 #include "content/public/common/content_descriptors.h" |
24 #include "content/public/common/content_switches.h" | 25 #include "content/public/common/content_switches.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 } | 115 } |
115 #endif | 116 #endif |
116 | 117 |
117 void LaunchOnLauncherThread(const NotifyCallback& callback, | 118 void LaunchOnLauncherThread(const NotifyCallback& callback, |
118 BrowserThread::ID client_thread_id, | 119 BrowserThread::ID client_thread_id, |
119 int child_process_id, | 120 int child_process_id, |
120 SandboxedProcessLauncherDelegate* delegate, | 121 SandboxedProcessLauncherDelegate* delegate, |
121 #if defined(OS_ANDROID) | 122 #if defined(OS_ANDROID) |
122 base::ScopedFD ipcfd, | 123 base::ScopedFD ipcfd, |
123 #endif | 124 #endif |
124 const base::SharedMemory* field_trial_state, | |
125 mojo::edk::ScopedPlatformHandle client_handle, | 125 mojo::edk::ScopedPlatformHandle client_handle, |
126 base::CommandLine* cmd_line) { | 126 base::CommandLine* cmd_line) { |
127 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); | 127 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); |
128 std::unique_ptr<SandboxedProcessLauncherDelegate> delegate_deleter(delegate); | 128 std::unique_ptr<SandboxedProcessLauncherDelegate> delegate_deleter(delegate); |
129 #if !defined(OS_ANDROID) | 129 #if !defined(OS_ANDROID) |
130 ZygoteHandle zygote = nullptr; | 130 ZygoteHandle zygote = nullptr; |
131 int launch_result = LAUNCH_RESULT_FAILURE; | 131 int launch_result = LAUNCH_RESULT_FAILURE; |
132 #endif | 132 #endif |
133 #if defined(OS_WIN) | 133 #if defined(OS_WIN) |
134 bool launch_elevated = delegate->ShouldLaunchElevated(); | 134 bool launch_elevated = delegate->ShouldLaunchElevated(); |
(...skipping 11 matching lines...) Expand all Loading... |
146 #if defined(OS_WIN) | 146 #if defined(OS_WIN) |
147 if (launch_elevated) { | 147 if (launch_elevated) { |
148 // When establishing a Mojo connection, the pipe path has already been added | 148 // When establishing a Mojo connection, the pipe path has already been added |
149 // to the command line. | 149 // to the command line. |
150 base::LaunchOptions options; | 150 base::LaunchOptions options; |
151 options.start_hidden = true; | 151 options.start_hidden = true; |
152 process = base::LaunchElevatedProcess(*cmd_line, options); | 152 process = base::LaunchElevatedProcess(*cmd_line, options); |
153 } else { | 153 } else { |
154 base::HandlesToInheritVector handles; | 154 base::HandlesToInheritVector handles; |
155 handles.push_back(client_handle.get().handle); | 155 handles.push_back(client_handle.get().handle); |
156 if (field_trial_state) | 156 base::FieldTrialList::AppendFieldTrialHandleIfNeeded(&handles); |
157 handles.push_back(field_trial_state->handle().GetHandle()); | |
158 cmd_line->AppendSwitchASCII( | 157 cmd_line->AppendSwitchASCII( |
159 mojo::edk::PlatformChannelPair::kMojoPlatformChannelHandleSwitch, | 158 mojo::edk::PlatformChannelPair::kMojoPlatformChannelHandleSwitch, |
160 base::UintToString(base::win::HandleToUint32(handles[0]))); | 159 base::UintToString(base::win::HandleToUint32(handles[0]))); |
161 launch_result = | 160 launch_result = |
162 StartSandboxedProcess(delegate, cmd_line, handles, &process); | 161 StartSandboxedProcess(delegate, cmd_line, handles, &process); |
163 } | 162 } |
164 #elif defined(OS_POSIX) | 163 #elif defined(OS_POSIX) |
165 std::string process_type = | 164 std::string process_type = |
166 cmd_line->GetSwitchValueASCII(switches::kProcessType); | 165 cmd_line->GetSwitchValueASCII(switches::kProcessType); |
167 std::unique_ptr<FileDescriptorInfo> files_to_register( | 166 std::unique_ptr<FileDescriptorInfo> files_to_register( |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 #endif | 392 #endif |
394 } | 393 } |
395 | 394 |
396 } // namespace | 395 } // namespace |
397 | 396 |
398 ChildProcessLauncher::ChildProcessLauncher( | 397 ChildProcessLauncher::ChildProcessLauncher( |
399 SandboxedProcessLauncherDelegate* delegate, | 398 SandboxedProcessLauncherDelegate* delegate, |
400 base::CommandLine* cmd_line, | 399 base::CommandLine* cmd_line, |
401 int child_process_id, | 400 int child_process_id, |
402 Client* client, | 401 Client* client, |
403 const base::SharedMemory* field_trial_state, | |
404 const std::string& mojo_child_token, | 402 const std::string& mojo_child_token, |
405 const mojo::edk::ProcessErrorCallback& process_error_callback, | 403 const mojo::edk::ProcessErrorCallback& process_error_callback, |
406 bool terminate_on_shutdown) | 404 bool terminate_on_shutdown) |
407 : client_(client), | 405 : client_(client), |
408 termination_status_(base::TERMINATION_STATUS_NORMAL_TERMINATION), | 406 termination_status_(base::TERMINATION_STATUS_NORMAL_TERMINATION), |
409 exit_code_(RESULT_CODE_NORMAL_EXIT), | 407 exit_code_(RESULT_CODE_NORMAL_EXIT), |
410 zygote_(nullptr), | 408 zygote_(nullptr), |
411 starting_(true), | 409 starting_(true), |
412 process_error_callback_(process_error_callback), | 410 process_error_callback_(process_error_callback), |
413 #if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \ | 411 #if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \ |
414 defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \ | 412 defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \ |
415 defined(UNDEFINED_SANITIZER) | 413 defined(UNDEFINED_SANITIZER) |
416 terminate_child_on_shutdown_(false), | 414 terminate_child_on_shutdown_(false), |
417 #else | 415 #else |
418 terminate_child_on_shutdown_(terminate_on_shutdown), | 416 terminate_child_on_shutdown_(terminate_on_shutdown), |
419 #endif | 417 #endif |
420 mojo_child_token_(mojo_child_token), | 418 mojo_child_token_(mojo_child_token), |
421 weak_factory_(this) { | 419 weak_factory_(this) { |
422 DCHECK(CalledOnValidThread()); | 420 DCHECK(CalledOnValidThread()); |
423 CHECK(BrowserThread::GetCurrentThreadIdentifier(&client_thread_id_)); | 421 CHECK(BrowserThread::GetCurrentThreadIdentifier(&client_thread_id_)); |
424 Launch(delegate, cmd_line, child_process_id, field_trial_state); | 422 Launch(delegate, cmd_line, child_process_id); |
425 } | 423 } |
426 | 424 |
427 ChildProcessLauncher::~ChildProcessLauncher() { | 425 ChildProcessLauncher::~ChildProcessLauncher() { |
428 DCHECK(CalledOnValidThread()); | 426 DCHECK(CalledOnValidThread()); |
429 if (process_.IsValid() && terminate_child_on_shutdown_) { | 427 if (process_.IsValid() && terminate_child_on_shutdown_) { |
430 // On Posix, EnsureProcessTerminated can lead to 2 seconds of sleep! So | 428 // On Posix, EnsureProcessTerminated can lead to 2 seconds of sleep! So |
431 // don't this on the UI/IO threads. | 429 // don't this on the UI/IO threads. |
432 BrowserThread::PostTask(BrowserThread::PROCESS_LAUNCHER, FROM_HERE, | 430 BrowserThread::PostTask(BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
433 base::Bind(&TerminateOnLauncherThread, zygote_, | 431 base::Bind(&TerminateOnLauncherThread, zygote_, |
434 base::Passed(&process_))); | 432 base::Passed(&process_))); |
435 } | 433 } |
436 } | 434 } |
437 | 435 |
438 void ChildProcessLauncher::Launch(SandboxedProcessLauncherDelegate* delegate, | 436 void ChildProcessLauncher::Launch(SandboxedProcessLauncherDelegate* delegate, |
439 base::CommandLine* cmd_line, | 437 base::CommandLine* cmd_line, |
440 int child_process_id, | 438 int child_process_id) { |
441 const base::SharedMemory* field_trial_state) { | |
442 DCHECK(CalledOnValidThread()); | 439 DCHECK(CalledOnValidThread()); |
443 | 440 |
444 #if defined(OS_ANDROID) | 441 #if defined(OS_ANDROID) |
445 // Android only supports renderer, sandboxed utility and gpu. | 442 // Android only supports renderer, sandboxed utility and gpu. |
446 std::string process_type = | 443 std::string process_type = |
447 cmd_line->GetSwitchValueASCII(switches::kProcessType); | 444 cmd_line->GetSwitchValueASCII(switches::kProcessType); |
448 CHECK(process_type == switches::kGpuProcess || | 445 CHECK(process_type == switches::kGpuProcess || |
449 process_type == switches::kRendererProcess || | 446 process_type == switches::kRendererProcess || |
450 #if defined(ENABLE_PLUGINS) | 447 #if defined(ENABLE_PLUGINS) |
451 process_type == switches::kPpapiPluginProcess || | 448 process_type == switches::kPpapiPluginProcess || |
(...skipping 28 matching lines...) Expand all Loading... |
480 weak_factory_.GetWeakPtr(), | 477 weak_factory_.GetWeakPtr(), |
481 terminate_child_on_shutdown_, | 478 terminate_child_on_shutdown_, |
482 base::Passed(&server_handle))); | 479 base::Passed(&server_handle))); |
483 BrowserThread::PostTask( | 480 BrowserThread::PostTask( |
484 BrowserThread::PROCESS_LAUNCHER, FROM_HERE, | 481 BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
485 base::Bind(&LaunchOnLauncherThread, reply_callback, client_thread_id_, | 482 base::Bind(&LaunchOnLauncherThread, reply_callback, client_thread_id_, |
486 child_process_id, delegate, | 483 child_process_id, delegate, |
487 #if defined(OS_ANDROID) | 484 #if defined(OS_ANDROID) |
488 base::Passed(&ipcfd), | 485 base::Passed(&ipcfd), |
489 #endif | 486 #endif |
490 field_trial_state, base::Passed(&client_handle), cmd_line)); | 487 base::Passed(&client_handle), cmd_line)); |
491 } | 488 } |
492 | 489 |
493 void ChildProcessLauncher::UpdateTerminationStatus(bool known_dead) { | 490 void ChildProcessLauncher::UpdateTerminationStatus(bool known_dead) { |
494 DCHECK(CalledOnValidThread()); | 491 DCHECK(CalledOnValidThread()); |
495 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) | 492 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
496 if (zygote_) { | 493 if (zygote_) { |
497 termination_status_ = zygote_->GetTerminationStatus( | 494 termination_status_ = zygote_->GetTerminationStatus( |
498 process_.Handle(), known_dead, &exit_code_); | 495 process_.Handle(), known_dead, &exit_code_); |
499 } else if (known_dead) { | 496 } else if (known_dead) { |
500 termination_status_ = | 497 termination_status_ = |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
625 } | 622 } |
626 | 623 |
627 ChildProcessLauncher::Client* ChildProcessLauncher::ReplaceClientForTest( | 624 ChildProcessLauncher::Client* ChildProcessLauncher::ReplaceClientForTest( |
628 Client* client) { | 625 Client* client) { |
629 Client* ret = client_; | 626 Client* ret = client_; |
630 client_ = client; | 627 client_ = client; |
631 return ret; | 628 return ret; |
632 } | 629 } |
633 | 630 |
634 } // namespace content | 631 } // namespace content |
OLD | NEW |