| 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" |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 } | 114 } |
| 115 #endif | 115 #endif |
| 116 | 116 |
| 117 void LaunchOnLauncherThread(const NotifyCallback& callback, | 117 void LaunchOnLauncherThread(const NotifyCallback& callback, |
| 118 BrowserThread::ID client_thread_id, | 118 BrowserThread::ID client_thread_id, |
| 119 int child_process_id, | 119 int child_process_id, |
| 120 SandboxedProcessLauncherDelegate* delegate, | 120 SandboxedProcessLauncherDelegate* delegate, |
| 121 #if defined(OS_ANDROID) | 121 #if defined(OS_ANDROID) |
| 122 base::ScopedFD ipcfd, | 122 base::ScopedFD ipcfd, |
| 123 #endif | 123 #endif |
| 124 #if defined(OS_WIN) |
| 125 HANDLE field_trial_handle, |
| 126 #endif |
| 124 mojo::edk::ScopedPlatformHandle client_handle, | 127 mojo::edk::ScopedPlatformHandle client_handle, |
| 125 base::CommandLine* cmd_line) { | 128 base::CommandLine* cmd_line) { |
| 126 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); | 129 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); |
| 127 std::unique_ptr<SandboxedProcessLauncherDelegate> delegate_deleter(delegate); | 130 std::unique_ptr<SandboxedProcessLauncherDelegate> delegate_deleter(delegate); |
| 128 #if !defined(OS_ANDROID) | 131 #if !defined(OS_ANDROID) |
| 129 ZygoteHandle zygote = nullptr; | 132 ZygoteHandle zygote = nullptr; |
| 130 int launch_result = LAUNCH_RESULT_FAILURE; | 133 int launch_result = LAUNCH_RESULT_FAILURE; |
| 131 #endif | 134 #endif |
| 132 #if defined(OS_WIN) | 135 #if defined(OS_WIN) |
| 133 bool launch_elevated = delegate->ShouldLaunchElevated(); | 136 bool launch_elevated = delegate->ShouldLaunchElevated(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 145 #if defined(OS_WIN) | 148 #if defined(OS_WIN) |
| 146 if (launch_elevated) { | 149 if (launch_elevated) { |
| 147 // When establishing a Mojo connection, the pipe path has already been added | 150 // When establishing a Mojo connection, the pipe path has already been added |
| 148 // to the command line. | 151 // to the command line. |
| 149 base::LaunchOptions options; | 152 base::LaunchOptions options; |
| 150 options.start_hidden = true; | 153 options.start_hidden = true; |
| 151 process = base::LaunchElevatedProcess(*cmd_line, options); | 154 process = base::LaunchElevatedProcess(*cmd_line, options); |
| 152 } else { | 155 } else { |
| 153 base::HandlesToInheritVector handles; | 156 base::HandlesToInheritVector handles; |
| 154 handles.push_back(client_handle.get().handle); | 157 handles.push_back(client_handle.get().handle); |
| 158 if (field_trial_handle) |
| 159 handles.push_back(field_trial_handle); |
| 155 cmd_line->AppendSwitchASCII( | 160 cmd_line->AppendSwitchASCII( |
| 156 mojo::edk::PlatformChannelPair::kMojoPlatformChannelHandleSwitch, | 161 mojo::edk::PlatformChannelPair::kMojoPlatformChannelHandleSwitch, |
| 157 base::UintToString(base::win::HandleToUint32(handles[0]))); | 162 base::UintToString(base::win::HandleToUint32(handles[0]))); |
| 163 |
| 158 launch_result = | 164 launch_result = |
| 159 StartSandboxedProcess(delegate, cmd_line, handles, &process); | 165 StartSandboxedProcess(delegate, cmd_line, handles, &process); |
| 160 } | 166 } |
| 161 #elif defined(OS_POSIX) | 167 #elif defined(OS_POSIX) |
| 162 std::string process_type = | 168 std::string process_type = |
| 163 cmd_line->GetSwitchValueASCII(switches::kProcessType); | 169 cmd_line->GetSwitchValueASCII(switches::kProcessType); |
| 164 std::unique_ptr<FileDescriptorInfo> files_to_register( | 170 std::unique_ptr<FileDescriptorInfo> files_to_register( |
| 165 FileDescriptorInfoImpl::Create()); | 171 FileDescriptorInfoImpl::Create()); |
| 166 | 172 |
| 167 base::ScopedFD mojo_fd(client_handle.release().handle); | 173 base::ScopedFD mojo_fd(client_handle.release().handle); |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 } | 390 } |
| 385 #if defined(OS_ANDROID) | 391 #if defined(OS_ANDROID) |
| 386 SetChildProcessInForeground(process.Handle(), !background); | 392 SetChildProcessInForeground(process.Handle(), !background); |
| 387 #endif | 393 #endif |
| 388 } | 394 } |
| 389 | 395 |
| 390 } // namespace | 396 } // namespace |
| 391 | 397 |
| 392 ChildProcessLauncher::ChildProcessLauncher( | 398 ChildProcessLauncher::ChildProcessLauncher( |
| 393 SandboxedProcessLauncherDelegate* delegate, | 399 SandboxedProcessLauncherDelegate* delegate, |
| 400 #if defined(OS_WIN) |
| 401 HANDLE field_trial_handle, |
| 402 #endif |
| 394 base::CommandLine* cmd_line, | 403 base::CommandLine* cmd_line, |
| 395 int child_process_id, | 404 int child_process_id, |
| 396 Client* client, | 405 Client* client, |
| 397 const std::string& mojo_child_token, | 406 const std::string& mojo_child_token, |
| 398 const mojo::edk::ProcessErrorCallback& process_error_callback, | 407 const mojo::edk::ProcessErrorCallback& process_error_callback, |
| 399 bool terminate_on_shutdown) | 408 bool terminate_on_shutdown) |
| 400 : client_(client), | 409 : client_(client), |
| 401 termination_status_(base::TERMINATION_STATUS_NORMAL_TERMINATION), | 410 termination_status_(base::TERMINATION_STATUS_NORMAL_TERMINATION), |
| 402 exit_code_(RESULT_CODE_NORMAL_EXIT), | 411 exit_code_(RESULT_CODE_NORMAL_EXIT), |
| 403 zygote_(nullptr), | 412 zygote_(nullptr), |
| 404 starting_(true), | 413 starting_(true), |
| 405 process_error_callback_(process_error_callback), | 414 process_error_callback_(process_error_callback), |
| 406 #if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \ | 415 #if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \ |
| 407 defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \ | 416 defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \ |
| 408 defined(UNDEFINED_SANITIZER) | 417 defined(UNDEFINED_SANITIZER) |
| 409 terminate_child_on_shutdown_(false), | 418 terminate_child_on_shutdown_(false), |
| 410 #else | 419 #else |
| 411 terminate_child_on_shutdown_(terminate_on_shutdown), | 420 terminate_child_on_shutdown_(terminate_on_shutdown), |
| 412 #endif | 421 #endif |
| 413 mojo_child_token_(mojo_child_token), | 422 mojo_child_token_(mojo_child_token), |
| 414 weak_factory_(this) { | 423 weak_factory_(this) { |
| 415 DCHECK(CalledOnValidThread()); | 424 DCHECK(CalledOnValidThread()); |
| 416 CHECK(BrowserThread::GetCurrentThreadIdentifier(&client_thread_id_)); | 425 CHECK(BrowserThread::GetCurrentThreadIdentifier(&client_thread_id_)); |
| 426 #if defined(OS_WIN) |
| 427 Launch(delegate, field_trial_handle, cmd_line, child_process_id); |
| 428 #else |
| 417 Launch(delegate, cmd_line, child_process_id); | 429 Launch(delegate, cmd_line, child_process_id); |
| 430 #endif |
| 418 } | 431 } |
| 419 | 432 |
| 420 ChildProcessLauncher::~ChildProcessLauncher() { | 433 ChildProcessLauncher::~ChildProcessLauncher() { |
| 421 DCHECK(CalledOnValidThread()); | 434 DCHECK(CalledOnValidThread()); |
| 422 if (process_.IsValid() && terminate_child_on_shutdown_) { | 435 if (process_.IsValid() && terminate_child_on_shutdown_) { |
| 423 // On Posix, EnsureProcessTerminated can lead to 2 seconds of sleep! So | 436 // On Posix, EnsureProcessTerminated can lead to 2 seconds of sleep! So |
| 424 // don't this on the UI/IO threads. | 437 // don't this on the UI/IO threads. |
| 425 BrowserThread::PostTask(BrowserThread::PROCESS_LAUNCHER, FROM_HERE, | 438 BrowserThread::PostTask(BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
| 426 base::Bind(&TerminateOnLauncherThread, zygote_, | 439 base::Bind(&TerminateOnLauncherThread, zygote_, |
| 427 base::Passed(&process_))); | 440 base::Passed(&process_))); |
| 428 } | 441 } |
| 429 } | 442 } |
| 430 | 443 |
| 431 void ChildProcessLauncher::Launch( | 444 void ChildProcessLauncher::Launch( |
| 432 SandboxedProcessLauncherDelegate* delegate, | 445 SandboxedProcessLauncherDelegate* delegate, |
| 446 #if defined(OS_WIN) |
| 447 HANDLE field_trial_handle, |
| 448 #endif |
| 433 base::CommandLine* cmd_line, | 449 base::CommandLine* cmd_line, |
| 434 int child_process_id) { | 450 int child_process_id) { |
| 435 DCHECK(CalledOnValidThread()); | 451 DCHECK(CalledOnValidThread()); |
| 436 | 452 |
| 437 #if defined(OS_ANDROID) | 453 #if defined(OS_ANDROID) |
| 438 // Android only supports renderer, sandboxed utility and gpu. | 454 // Android only supports renderer, sandboxed utility and gpu. |
| 439 std::string process_type = | 455 std::string process_type = |
| 440 cmd_line->GetSwitchValueASCII(switches::kProcessType); | 456 cmd_line->GetSwitchValueASCII(switches::kProcessType); |
| 441 CHECK(process_type == switches::kGpuProcess || | 457 CHECK(process_type == switches::kGpuProcess || |
| 442 process_type == switches::kRendererProcess || | 458 process_type == switches::kRendererProcess || |
| (...skipping 30 matching lines...) Expand all Loading... |
| 473 weak_factory_.GetWeakPtr(), | 489 weak_factory_.GetWeakPtr(), |
| 474 terminate_child_on_shutdown_, | 490 terminate_child_on_shutdown_, |
| 475 base::Passed(&server_handle))); | 491 base::Passed(&server_handle))); |
| 476 BrowserThread::PostTask( | 492 BrowserThread::PostTask( |
| 477 BrowserThread::PROCESS_LAUNCHER, FROM_HERE, | 493 BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
| 478 base::Bind(&LaunchOnLauncherThread, reply_callback, client_thread_id_, | 494 base::Bind(&LaunchOnLauncherThread, reply_callback, client_thread_id_, |
| 479 child_process_id, delegate, | 495 child_process_id, delegate, |
| 480 #if defined(OS_ANDROID) | 496 #if defined(OS_ANDROID) |
| 481 base::Passed(&ipcfd), | 497 base::Passed(&ipcfd), |
| 482 #endif | 498 #endif |
| 499 #if defined(OS_WIN) |
| 500 field_trial_handle, |
| 501 #endif |
| 483 base::Passed(&client_handle), cmd_line)); | 502 base::Passed(&client_handle), cmd_line)); |
| 484 } | 503 } |
| 485 | 504 |
| 486 void ChildProcessLauncher::UpdateTerminationStatus(bool known_dead) { | 505 void ChildProcessLauncher::UpdateTerminationStatus(bool known_dead) { |
| 487 DCHECK(CalledOnValidThread()); | 506 DCHECK(CalledOnValidThread()); |
| 488 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) | 507 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 489 if (zygote_) { | 508 if (zygote_) { |
| 490 termination_status_ = zygote_->GetTerminationStatus( | 509 termination_status_ = zygote_->GetTerminationStatus( |
| 491 process_.Handle(), known_dead, &exit_code_); | 510 process_.Handle(), known_dead, &exit_code_); |
| 492 } else if (known_dead) { | 511 } else if (known_dead) { |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 618 } | 637 } |
| 619 | 638 |
| 620 ChildProcessLauncher::Client* ChildProcessLauncher::ReplaceClientForTest( | 639 ChildProcessLauncher::Client* ChildProcessLauncher::ReplaceClientForTest( |
| 621 Client* client) { | 640 Client* client) { |
| 622 Client* ret = client_; | 641 Client* ret = client_; |
| 623 client_ = client; | 642 client_ = client; |
| 624 return ret; | 643 return ret; |
| 625 } | 644 } |
| 626 | 645 |
| 627 } // namespace content | 646 } // namespace content |
| OLD | NEW |