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