| 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" |
| 20 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 21 #include "base/synchronization/lock.h" | 22 #include "base/synchronization/lock.h" |
| 22 #include "base/synchronization/waitable_event.h" | 23 #include "base/synchronization/waitable_event.h" |
| 23 #include "build/build_config.h" | 24 #include "build/build_config.h" |
| 24 #include "content/browser/browser_child_process_host_impl.h" | 25 #include "content/browser/browser_child_process_host_impl.h" |
| 25 #include "content/browser/mojo/mojo_application_host.h" | 26 #include "content/browser/mojo/constants.h" |
| 27 #include "content/browser/mojo/mojo_child_connection.h" |
| 28 #include "content/browser/mojo/mojo_shell_context.h" |
| 26 #include "content/browser/renderer_host/render_process_host_impl.h" | 29 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 27 #include "content/common/child_process_host_impl.h" | 30 #include "content/common/child_process_host_impl.h" |
| 28 #include "content/common/in_process_child_thread_params.h" | 31 #include "content/common/in_process_child_thread_params.h" |
| 29 #include "content/common/utility_messages.h" | 32 #include "content/common/utility_messages.h" |
| 30 #include "content/public/browser/browser_thread.h" | 33 #include "content/public/browser/browser_thread.h" |
| 31 #include "content/public/browser/content_browser_client.h" | 34 #include "content/public/browser/content_browser_client.h" |
| 32 #include "content/public/browser/utility_process_host_client.h" | 35 #include "content/public/browser/utility_process_host_client.h" |
| 33 #include "content/public/common/content_switches.h" | 36 #include "content/public/common/content_switches.h" |
| 34 #include "content/public/common/mojo_channel_switches.h" | 37 #include "content/public/common/mojo_channel_switches.h" |
| 38 #include "content/public/common/mojo_shell_connection.h" |
| 35 #include "content/public/common/process_type.h" | 39 #include "content/public/common/process_type.h" |
| 36 #include "content/public/common/sandbox_type.h" | 40 #include "content/public/common/sandbox_type.h" |
| 37 #include "content/public/common/sandboxed_process_launcher_delegate.h" | 41 #include "content/public/common/sandboxed_process_launcher_delegate.h" |
| 38 #include "ipc/ipc_switches.h" | 42 #include "ipc/ipc_switches.h" |
| 39 #include "mojo/edk/embedder/embedder.h" | 43 #include "mojo/edk/embedder/embedder.h" |
| 44 #include "services/shell/public/cpp/connection.h" |
| 40 #include "services/shell/public/cpp/interface_provider.h" | 45 #include "services/shell/public/cpp/interface_provider.h" |
| 41 #include "services/shell/public/cpp/interface_registry.h" | 46 #include "services/shell/public/cpp/interface_registry.h" |
| 42 #include "ui/base/ui_base_switches.h" | 47 #include "ui/base/ui_base_switches.h" |
| 43 | 48 |
| 44 #if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) | 49 #if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) |
| 45 #include "content/public/browser/zygote_handle_linux.h" | 50 #include "content/public/browser/zygote_handle_linux.h" |
| 46 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) | 51 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) |
| 47 | 52 |
| 48 #if defined(OS_WIN) | 53 #if defined(OS_WIN) |
| 49 #include "sandbox/win/src/sandbox_policy.h" | 54 #include "sandbox/win/src/sandbox_policy.h" |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 no_sandbox_(false), | 160 no_sandbox_(false), |
| 156 run_elevated_(false), | 161 run_elevated_(false), |
| 157 #if defined(OS_LINUX) | 162 #if defined(OS_LINUX) |
| 158 child_flags_(ChildProcessHost::CHILD_ALLOW_SELF), | 163 child_flags_(ChildProcessHost::CHILD_ALLOW_SELF), |
| 159 #else | 164 #else |
| 160 child_flags_(ChildProcessHost::CHILD_NORMAL), | 165 child_flags_(ChildProcessHost::CHILD_NORMAL), |
| 161 #endif | 166 #endif |
| 162 started_(false), | 167 started_(false), |
| 163 name_(base::ASCIIToUTF16("utility process")), | 168 name_(base::ASCIIToUTF16("utility process")), |
| 164 child_token_(mojo::edk::GenerateRandomToken()), | 169 child_token_(mojo::edk::GenerateRandomToken()), |
| 165 mojo_application_host_(new MojoApplicationHost(child_token_)), | |
| 166 weak_ptr_factory_(this) { | 170 weak_ptr_factory_(this) { |
| 171 process_.reset(new BrowserChildProcessHostImpl(PROCESS_TYPE_UTILITY, this, |
| 172 child_token_)); |
| 173 mojo_child_connection_.reset(new MojoChildConnection( |
| 174 kUtilityMojoApplicationName, |
| 175 base::StringPrintf("%d_0", process_->GetData().id), |
| 176 child_token_, |
| 177 MojoShellContext::GetConnectorForIOThread())); |
| 167 } | 178 } |
| 168 | 179 |
| 169 UtilityProcessHostImpl::~UtilityProcessHostImpl() { | 180 UtilityProcessHostImpl::~UtilityProcessHostImpl() { |
| 170 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 181 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 171 if (is_batch_mode_) | 182 if (is_batch_mode_) |
| 172 EndBatchMode(); | 183 EndBatchMode(); |
| 173 } | 184 } |
| 174 | 185 |
| 175 base::WeakPtr<UtilityProcessHost> UtilityProcessHostImpl::AsWeakPtr() { | 186 base::WeakPtr<UtilityProcessHost> UtilityProcessHostImpl::AsWeakPtr() { |
| 176 return weak_ptr_factory_.GetWeakPtr(); | 187 return weak_ptr_factory_.GetWeakPtr(); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 env_ = env; | 232 env_ = env; |
| 222 } | 233 } |
| 223 | 234 |
| 224 #endif // OS_POSIX | 235 #endif // OS_POSIX |
| 225 | 236 |
| 226 bool UtilityProcessHostImpl::Start() { | 237 bool UtilityProcessHostImpl::Start() { |
| 227 return StartProcess(); | 238 return StartProcess(); |
| 228 } | 239 } |
| 229 | 240 |
| 230 shell::InterfaceRegistry* UtilityProcessHostImpl::GetInterfaceRegistry() { | 241 shell::InterfaceRegistry* UtilityProcessHostImpl::GetInterfaceRegistry() { |
| 231 DCHECK(mojo_application_host_); | 242 return mojo_child_connection_->connection()->GetInterfaceRegistry(); |
| 232 return mojo_application_host_->interface_registry(); | |
| 233 } | 243 } |
| 234 | 244 |
| 235 shell::InterfaceProvider* UtilityProcessHostImpl::GetRemoteInterfaces() { | 245 shell::InterfaceProvider* UtilityProcessHostImpl::GetRemoteInterfaces() { |
| 236 DCHECK(mojo_application_host_); | 246 return mojo_child_connection_->connection()->GetRemoteInterfaces(); |
| 237 return mojo_application_host_->remote_interfaces(); | |
| 238 } | 247 } |
| 239 | 248 |
| 240 void UtilityProcessHostImpl::SetName(const base::string16& name) { | 249 void UtilityProcessHostImpl::SetName(const base::string16& name) { |
| 241 name_ = name; | 250 name_ = name; |
| 242 } | 251 } |
| 243 | 252 |
| 244 #if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) | 253 #if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) |
| 245 // static | 254 // static |
| 246 void UtilityProcessHostImpl::EarlyZygoteLaunch() { | 255 void UtilityProcessHostImpl::EarlyZygoteLaunch() { |
| 247 DCHECK(!g_utility_zygote); | 256 DCHECK(!g_utility_zygote); |
| 248 g_utility_zygote = CreateZygote(); | 257 g_utility_zygote = CreateZygote(); |
| 249 } | 258 } |
| 250 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) | 259 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) |
| 251 | 260 |
| 252 bool UtilityProcessHostImpl::StartProcess() { | 261 bool UtilityProcessHostImpl::StartProcess() { |
| 253 if (started_) | 262 if (started_) |
| 254 return true; | 263 return true; |
| 255 started_ = true; | 264 started_ = true; |
| 256 | 265 |
| 257 if (is_batch_mode_) | 266 if (is_batch_mode_) |
| 258 return true; | 267 return true; |
| 259 | 268 |
| 260 // Name must be set or metrics_service will crash in any test which | |
| 261 // launches a UtilityProcessHost. | |
| 262 process_.reset(new BrowserChildProcessHostImpl(PROCESS_TYPE_UTILITY, this, | |
| 263 child_token_)); | |
| 264 process_->SetName(name_); | 269 process_->SetName(name_); |
| 265 | 270 |
| 266 std::string mojo_channel_token = | 271 std::string mojo_channel_token = |
| 267 process_->GetHost()->CreateChannelMojo(child_token_); | 272 process_->GetHost()->CreateChannelMojo(child_token_); |
| 268 if (mojo_channel_token.empty()) { | 273 if (mojo_channel_token.empty()) { |
| 269 NotifyAndDelete(LAUNCH_RESULT_FAILURE); | 274 NotifyAndDelete(LAUNCH_RESULT_FAILURE); |
| 270 return false; | 275 return false; |
| 271 } | 276 } |
| 272 | 277 |
| 273 if (RenderProcessHost::run_renderer_in_process()) { | 278 if (RenderProcessHost::run_renderer_in_process()) { |
| 274 DCHECK(g_utility_main_thread_factory); | 279 DCHECK(g_utility_main_thread_factory); |
| 275 // See comment in RenderProcessHostImpl::Init() for the background on why we | 280 // See comment in RenderProcessHostImpl::Init() for the background on why we |
| 276 // support single process mode this way. | 281 // support single process mode this way. |
| 277 in_process_thread_.reset( | 282 in_process_thread_.reset( |
| 278 g_utility_main_thread_factory(InProcessChildThreadParams( | 283 g_utility_main_thread_factory(InProcessChildThreadParams( |
| 279 std::string(), BrowserThread::UnsafeGetMessageLoopForThread( | 284 std::string(), BrowserThread::UnsafeGetMessageLoopForThread( |
| 280 BrowserThread::IO)->task_runner(), | 285 BrowserThread::IO)->task_runner(), |
| 281 mojo_channel_token, mojo_application_host_->GetToken()))); | 286 mojo_channel_token, mojo_child_connection_->shell_client_token()))); |
| 282 in_process_thread_->Start(); | 287 in_process_thread_->Start(); |
| 283 } else { | 288 } else { |
| 284 const base::CommandLine& browser_command_line = | 289 const base::CommandLine& browser_command_line = |
| 285 *base::CommandLine::ForCurrentProcess(); | 290 *base::CommandLine::ForCurrentProcess(); |
| 286 | 291 |
| 287 bool has_cmd_prefix = browser_command_line.HasSwitch( | 292 bool has_cmd_prefix = browser_command_line.HasSwitch( |
| 288 switches::kUtilityCmdPrefix); | 293 switches::kUtilityCmdPrefix); |
| 289 | 294 |
| 290 #if defined(OS_ANDROID) | 295 #if defined(OS_ANDROID) |
| 291 // readlink("/prof/self/exe") sometimes fails on Android at startup. | 296 // readlink("/prof/self/exe") sometimes fails on Android at startup. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 exposed_dir_); | 355 exposed_dir_); |
| 351 } | 356 } |
| 352 | 357 |
| 353 #if defined(OS_WIN) | 358 #if defined(OS_WIN) |
| 354 // Let the utility process know if it is intended to be elevated. | 359 // Let the utility process know if it is intended to be elevated. |
| 355 if (run_elevated_) | 360 if (run_elevated_) |
| 356 cmd_line->AppendSwitch(switches::kUtilityProcessRunningElevated); | 361 cmd_line->AppendSwitch(switches::kUtilityProcessRunningElevated); |
| 357 #endif | 362 #endif |
| 358 | 363 |
| 359 cmd_line->AppendSwitchASCII(switches::kMojoApplicationChannelToken, | 364 cmd_line->AppendSwitchASCII(switches::kMojoApplicationChannelToken, |
| 360 mojo_application_host_->GetToken()); | 365 mojo_child_connection_->shell_client_token()); |
| 361 | 366 |
| 362 process_->Launch( | 367 process_->Launch( |
| 363 new UtilitySandboxedProcessLauncherDelegate(exposed_dir_, | 368 new UtilitySandboxedProcessLauncherDelegate(exposed_dir_, |
| 364 run_elevated_, | 369 run_elevated_, |
| 365 no_sandbox_, env_, | 370 no_sandbox_, env_, |
| 366 process_->GetHost()), | 371 process_->GetHost()), |
| 367 cmd_line, | 372 cmd_line, |
| 368 true); | 373 true); |
| 369 } | 374 } |
| 370 | 375 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 base::WeakPtr<UtilityProcessHostImpl> host, | 424 base::WeakPtr<UtilityProcessHostImpl> host, |
| 420 int error_code) { | 425 int error_code) { |
| 421 if (!host) | 426 if (!host) |
| 422 return; | 427 return; |
| 423 | 428 |
| 424 host->OnProcessLaunchFailed(error_code); | 429 host->OnProcessLaunchFailed(error_code); |
| 425 delete host.get(); | 430 delete host.get(); |
| 426 } | 431 } |
| 427 | 432 |
| 428 } // namespace content | 433 } // namespace content |
| OLD | NEW |