| 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" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "content/browser/browser_child_process_host_impl.h" | 24 #include "content/browser/browser_child_process_host_impl.h" |
| 25 #include "content/browser/mojo/mojo_application_host.h" | 25 #include "content/browser/mojo/mojo_application_host.h" |
| 26 #include "content/browser/renderer_host/render_process_host_impl.h" | 26 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 27 #include "content/common/child_process_host_impl.h" | 27 #include "content/common/child_process_host_impl.h" |
| 28 #include "content/common/in_process_child_thread_params.h" | 28 #include "content/common/in_process_child_thread_params.h" |
| 29 #include "content/common/utility_messages.h" | 29 #include "content/common/utility_messages.h" |
| 30 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
| 31 #include "content/public/browser/content_browser_client.h" | 31 #include "content/public/browser/content_browser_client.h" |
| 32 #include "content/public/browser/utility_process_host_client.h" | 32 #include "content/public/browser/utility_process_host_client.h" |
| 33 #include "content/public/common/content_switches.h" | 33 #include "content/public/common/content_switches.h" |
| 34 #include "content/public/common/mojo_channel_switches.h" |
| 34 #include "content/public/common/process_type.h" | 35 #include "content/public/common/process_type.h" |
| 35 #include "content/public/common/sandbox_type.h" | 36 #include "content/public/common/sandbox_type.h" |
| 36 #include "content/public/common/sandboxed_process_launcher_delegate.h" | 37 #include "content/public/common/sandboxed_process_launcher_delegate.h" |
| 37 #include "ipc/ipc_switches.h" | 38 #include "ipc/ipc_switches.h" |
| 38 #include "ui/base/ui_base_switches.h" | 39 #include "ui/base/ui_base_switches.h" |
| 39 | 40 |
| 40 #if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) | 41 #if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) |
| 41 #include "content/public/browser/zygote_handle_linux.h" | 42 #include "content/public/browser/zygote_handle_linux.h" |
| 42 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) | 43 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) |
| 43 | 44 |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) | 241 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) |
| 241 | 242 |
| 242 bool UtilityProcessHostImpl::StartProcess() { | 243 bool UtilityProcessHostImpl::StartProcess() { |
| 243 if (started_) | 244 if (started_) |
| 244 return true; | 245 return true; |
| 245 started_ = true; | 246 started_ = true; |
| 246 | 247 |
| 247 if (is_batch_mode_) | 248 if (is_batch_mode_) |
| 248 return true; | 249 return true; |
| 249 | 250 |
| 250 bool mojo_result = mojo_application_host_->Init(); | |
| 251 if (!mojo_result) { | |
| 252 NotifyAndDelete(); | |
| 253 return false; | |
| 254 } | |
| 255 | |
| 256 // Name must be set or metrics_service will crash in any test which | 251 // Name must be set or metrics_service will crash in any test which |
| 257 // launches a UtilityProcessHost. | 252 // launches a UtilityProcessHost. |
| 258 process_.reset(new BrowserChildProcessHostImpl(PROCESS_TYPE_UTILITY, this)); | 253 process_.reset(new BrowserChildProcessHostImpl(PROCESS_TYPE_UTILITY, this)); |
| 259 process_->SetName(name_); | 254 process_->SetName(name_); |
| 260 | 255 |
| 261 std::string channel_id = process_->GetHost()->CreateChannel(); | 256 std::string channel_id = process_->GetHost()->CreateChannel(); |
| 262 if (channel_id.empty()) { | 257 if (channel_id.empty()) { |
| 263 NotifyAndDelete(); | 258 NotifyAndDelete(); |
| 264 return false; | 259 return false; |
| 265 } | 260 } |
| 266 | 261 |
| 267 if (RenderProcessHost::run_renderer_in_process()) { | 262 if (RenderProcessHost::run_renderer_in_process()) { |
| 268 DCHECK(g_utility_main_thread_factory); | 263 DCHECK(g_utility_main_thread_factory); |
| 264 mojo::ScopedMessagePipeHandle mojo_handle = |
| 265 mojo_application_host_->InitWithPipe(); |
| 266 if (!mojo_handle.is_valid()) { |
| 267 NotifyAndDelete(); |
| 268 return false; |
| 269 } |
| 270 |
| 269 // See comment in RenderProcessHostImpl::Init() for the background on why we | 271 // See comment in RenderProcessHostImpl::Init() for the background on why we |
| 270 // support single process mode this way. | 272 // support single process mode this way. |
| 271 in_process_thread_.reset( | 273 in_process_thread_.reset( |
| 272 g_utility_main_thread_factory(InProcessChildThreadParams( | 274 g_utility_main_thread_factory(InProcessChildThreadParams( |
| 273 channel_id, BrowserThread::UnsafeGetMessageLoopForThread( | 275 channel_id, BrowserThread::UnsafeGetMessageLoopForThread( |
| 274 BrowserThread::IO)->task_runner()))); | 276 BrowserThread::IO)->task_runner(), |
| 277 mojo::MessagePipeHandle(), mojo_handle.release()))); |
| 275 in_process_thread_->Start(); | 278 in_process_thread_->Start(); |
| 276 OnProcessLaunched(); | |
| 277 } else { | 279 } else { |
| 278 const base::CommandLine& browser_command_line = | 280 const base::CommandLine& browser_command_line = |
| 279 *base::CommandLine::ForCurrentProcess(); | 281 *base::CommandLine::ForCurrentProcess(); |
| 280 | 282 |
| 281 bool has_cmd_prefix = browser_command_line.HasSwitch( | 283 bool has_cmd_prefix = browser_command_line.HasSwitch( |
| 282 switches::kUtilityCmdPrefix); | 284 switches::kUtilityCmdPrefix); |
| 283 | 285 |
| 284 #if defined(OS_ANDROID) | 286 #if defined(OS_ANDROID) |
| 285 // readlink("/prof/self/exe") sometimes fails on Android at startup. | 287 // readlink("/prof/self/exe") sometimes fails on Android at startup. |
| 286 // As a workaround skip calling it here, since the executable name is | 288 // As a workaround skip calling it here, since the executable name is |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir, | 345 cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir, |
| 344 exposed_dir_); | 346 exposed_dir_); |
| 345 } | 347 } |
| 346 | 348 |
| 347 #if defined(OS_WIN) | 349 #if defined(OS_WIN) |
| 348 // Let the utility process know if it is intended to be elevated. | 350 // Let the utility process know if it is intended to be elevated. |
| 349 if (run_elevated_) | 351 if (run_elevated_) |
| 350 cmd_line->AppendSwitch(switches::kUtilityProcessRunningElevated); | 352 cmd_line->AppendSwitch(switches::kUtilityProcessRunningElevated); |
| 351 #endif | 353 #endif |
| 352 | 354 |
| 355 std::string mojo_token = mojo_application_host_->InitWithToken(); |
| 356 cmd_line->AppendSwitchASCII(switches::kMojoApplicationChannelToken, |
| 357 mojo_token); |
| 358 |
| 353 process_->Launch( | 359 process_->Launch( |
| 354 new UtilitySandboxedProcessLauncherDelegate(exposed_dir_, | 360 new UtilitySandboxedProcessLauncherDelegate(exposed_dir_, |
| 355 run_elevated_, | 361 run_elevated_, |
| 356 no_sandbox_, env_, | 362 no_sandbox_, env_, |
| 357 process_->GetHost()), | 363 process_->GetHost()), |
| 358 cmd_line, | 364 cmd_line, |
| 359 true); | 365 true); |
| 360 } | 366 } |
| 361 | 367 |
| 362 return true; | 368 return true; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 389 void UtilityProcessHostImpl::OnProcessCrashed(int exit_code) { | 395 void UtilityProcessHostImpl::OnProcessCrashed(int exit_code) { |
| 390 if (!client_.get()) | 396 if (!client_.get()) |
| 391 return; | 397 return; |
| 392 | 398 |
| 393 client_task_runner_->PostTask( | 399 client_task_runner_->PostTask( |
| 394 FROM_HERE, | 400 FROM_HERE, |
| 395 base::Bind(&UtilityProcessHostClient::OnProcessCrashed, client_.get(), | 401 base::Bind(&UtilityProcessHostClient::OnProcessCrashed, client_.get(), |
| 396 exit_code)); | 402 exit_code)); |
| 397 } | 403 } |
| 398 | 404 |
| 399 void UtilityProcessHostImpl::OnProcessLaunched() { | |
| 400 DCHECK(mojo_application_host_); | |
| 401 base::ProcessHandle handle; | |
| 402 if (RenderProcessHost::run_renderer_in_process()) | |
| 403 handle = base::GetCurrentProcessHandle(); | |
| 404 else | |
| 405 handle = process_->GetData().handle; | |
| 406 | |
| 407 mojo_application_host_->Activate(this, handle); | |
| 408 } | |
| 409 | |
| 410 void UtilityProcessHostImpl::NotifyAndDelete() { | 405 void UtilityProcessHostImpl::NotifyAndDelete() { |
| 411 BrowserThread::PostTask( | 406 BrowserThread::PostTask( |
| 412 BrowserThread::IO, FROM_HERE, | 407 BrowserThread::IO, FROM_HERE, |
| 413 base::Bind(&UtilityProcessHostImpl::NotifyLaunchFailedAndDelete, | 408 base::Bind(&UtilityProcessHostImpl::NotifyLaunchFailedAndDelete, |
| 414 weak_ptr_factory_.GetWeakPtr())); | 409 weak_ptr_factory_.GetWeakPtr())); |
| 415 } | 410 } |
| 416 | 411 |
| 417 // static | 412 // static |
| 418 void UtilityProcessHostImpl::NotifyLaunchFailedAndDelete( | 413 void UtilityProcessHostImpl::NotifyLaunchFailedAndDelete( |
| 419 base::WeakPtr<UtilityProcessHostImpl> host) { | 414 base::WeakPtr<UtilityProcessHostImpl> host) { |
| 420 if (!host) | 415 if (!host) |
| 421 return; | 416 return; |
| 422 | 417 |
| 423 host->OnProcessLaunchFailed(); | 418 host->OnProcessLaunchFailed(); |
| 424 delete host.get(); | 419 delete host.get(); |
| 425 } | 420 } |
| 426 | 421 |
| 427 } // namespace content | 422 } // namespace content |
| OLD | NEW |