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_child_connection.h" | 26 #include "content/browser/mojo/mojo_child_connection.h" |
26 #include "content/browser/mojo/mojo_shell_context.h" | 27 #include "content/browser/mojo/mojo_shell_context.h" |
27 #include "content/browser/renderer_host/render_process_host_impl.h" | 28 #include "content/browser/renderer_host/render_process_host_impl.h" |
28 #include "content/common/child_process_host_impl.h" | 29 #include "content/common/child_process_host_impl.h" |
29 #include "content/common/in_process_child_thread_params.h" | 30 #include "content/common/in_process_child_thread_params.h" |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 is_batch_mode_(false), | 158 is_batch_mode_(false), |
158 no_sandbox_(false), | 159 no_sandbox_(false), |
159 run_elevated_(false), | 160 run_elevated_(false), |
160 #if defined(OS_LINUX) | 161 #if defined(OS_LINUX) |
161 child_flags_(ChildProcessHost::CHILD_ALLOW_SELF), | 162 child_flags_(ChildProcessHost::CHILD_ALLOW_SELF), |
162 #else | 163 #else |
163 child_flags_(ChildProcessHost::CHILD_NORMAL), | 164 child_flags_(ChildProcessHost::CHILD_NORMAL), |
164 #endif | 165 #endif |
165 started_(false), | 166 started_(false), |
166 name_(base::ASCIIToUTF16("utility process")), | 167 name_(base::ASCIIToUTF16("utility process")), |
| 168 child_token_(mojo::edk::GenerateRandomToken()), |
167 weak_ptr_factory_(this) { | 169 weak_ptr_factory_(this) { |
168 process_.reset(new BrowserChildProcessHostImpl( | 170 process_.reset(new BrowserChildProcessHostImpl(PROCESS_TYPE_UTILITY, this, |
169 PROCESS_TYPE_UTILITY, this, kUtilityMojoApplicationName)); | 171 child_token_)); |
| 172 mojo_child_connection_.reset(new MojoChildConnection( |
| 173 kUtilityMojoApplicationName, |
| 174 base::StringPrintf("%d_0", process_->GetData().id), child_token_, |
| 175 MojoShellContext::GetConnectorForIOThread(), |
| 176 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))); |
170 } | 177 } |
171 | 178 |
172 UtilityProcessHostImpl::~UtilityProcessHostImpl() { | 179 UtilityProcessHostImpl::~UtilityProcessHostImpl() { |
173 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 180 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
174 if (is_batch_mode_) | 181 if (is_batch_mode_) |
175 EndBatchMode(); | 182 EndBatchMode(); |
176 } | 183 } |
177 | 184 |
178 base::WeakPtr<UtilityProcessHost> UtilityProcessHostImpl::AsWeakPtr() { | 185 base::WeakPtr<UtilityProcessHost> UtilityProcessHostImpl::AsWeakPtr() { |
179 return weak_ptr_factory_.GetWeakPtr(); | 186 return weak_ptr_factory_.GetWeakPtr(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 env_ = env; | 231 env_ = env; |
225 } | 232 } |
226 | 233 |
227 #endif // OS_POSIX | 234 #endif // OS_POSIX |
228 | 235 |
229 bool UtilityProcessHostImpl::Start() { | 236 bool UtilityProcessHostImpl::Start() { |
230 return StartProcess(); | 237 return StartProcess(); |
231 } | 238 } |
232 | 239 |
233 shell::InterfaceProvider* UtilityProcessHostImpl::GetRemoteInterfaces() { | 240 shell::InterfaceProvider* UtilityProcessHostImpl::GetRemoteInterfaces() { |
234 return process_->child_connection()->GetRemoteInterfaces(); | 241 return mojo_child_connection_->GetRemoteInterfaces(); |
235 } | 242 } |
236 | 243 |
237 void UtilityProcessHostImpl::SetName(const base::string16& name) { | 244 void UtilityProcessHostImpl::SetName(const base::string16& name) { |
238 name_ = name; | 245 name_ = name; |
239 } | 246 } |
240 | 247 |
241 #if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) | 248 #if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) |
242 // static | 249 // static |
243 void UtilityProcessHostImpl::EarlyZygoteLaunch() { | 250 void UtilityProcessHostImpl::EarlyZygoteLaunch() { |
244 DCHECK(!g_utility_zygote); | 251 DCHECK(!g_utility_zygote); |
245 g_utility_zygote = CreateZygote(); | 252 g_utility_zygote = CreateZygote(); |
246 } | 253 } |
247 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) | 254 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX) |
248 | 255 |
249 bool UtilityProcessHostImpl::StartProcess() { | 256 bool UtilityProcessHostImpl::StartProcess() { |
250 if (started_) | 257 if (started_) |
251 return true; | 258 return true; |
252 started_ = true; | 259 started_ = true; |
253 | 260 |
254 if (is_batch_mode_) | 261 if (is_batch_mode_) |
255 return true; | 262 return true; |
256 | 263 |
257 process_->SetName(name_); | 264 process_->SetName(name_); |
258 process_->GetHost()->CreateChannelMojo(); | 265 |
| 266 std::string mojo_channel_token = |
| 267 process_->GetHost()->CreateChannelMojo(child_token_); |
| 268 if (mojo_channel_token.empty()) { |
| 269 NotifyAndDelete(LAUNCH_RESULT_FAILURE); |
| 270 return false; |
| 271 } |
259 | 272 |
260 if (RenderProcessHost::run_renderer_in_process()) { | 273 if (RenderProcessHost::run_renderer_in_process()) { |
261 DCHECK(g_utility_main_thread_factory); | 274 DCHECK(g_utility_main_thread_factory); |
262 // See comment in RenderProcessHostImpl::Init() for the background on why we | 275 // See comment in RenderProcessHostImpl::Init() for the background on why we |
263 // support single process mode this way. | 276 // support single process mode this way. |
264 in_process_thread_.reset( | 277 in_process_thread_.reset( |
265 g_utility_main_thread_factory(InProcessChildThreadParams( | 278 g_utility_main_thread_factory(InProcessChildThreadParams( |
266 std::string(), BrowserThread::UnsafeGetMessageLoopForThread( | 279 std::string(), BrowserThread::UnsafeGetMessageLoopForThread( |
267 BrowserThread::IO)->task_runner(), | 280 BrowserThread::IO)->task_runner(), |
268 std::string(), | 281 mojo_channel_token, mojo_child_connection_->service_token()))); |
269 process_->child_connection()->service_token()))); | |
270 in_process_thread_->Start(); | 282 in_process_thread_->Start(); |
271 } else { | 283 } else { |
272 const base::CommandLine& browser_command_line = | 284 const base::CommandLine& browser_command_line = |
273 *base::CommandLine::ForCurrentProcess(); | 285 *base::CommandLine::ForCurrentProcess(); |
274 | 286 |
275 bool has_cmd_prefix = browser_command_line.HasSwitch( | 287 bool has_cmd_prefix = browser_command_line.HasSwitch( |
276 switches::kUtilityCmdPrefix); | 288 switches::kUtilityCmdPrefix); |
277 | 289 |
278 #if defined(OS_ANDROID) | 290 #if defined(OS_ANDROID) |
279 // readlink("/prof/self/exe") sometimes fails on Android at startup. | 291 // readlink("/prof/self/exe") sometimes fails on Android at startup. |
(...skipping 16 matching lines...) Expand all Loading... |
296 if (exe_path.empty()) { | 308 if (exe_path.empty()) { |
297 NOTREACHED() << "Unable to get utility process binary name."; | 309 NOTREACHED() << "Unable to get utility process binary name."; |
298 return false; | 310 return false; |
299 } | 311 } |
300 | 312 |
301 base::CommandLine* cmd_line = new base::CommandLine(exe_path); | 313 base::CommandLine* cmd_line = new base::CommandLine(exe_path); |
302 #endif | 314 #endif |
303 | 315 |
304 cmd_line->AppendSwitchASCII(switches::kProcessType, | 316 cmd_line->AppendSwitchASCII(switches::kProcessType, |
305 switches::kUtilityProcess); | 317 switches::kUtilityProcess); |
| 318 cmd_line->AppendSwitchASCII(switches::kMojoChannelToken, |
| 319 mojo_channel_token); |
306 std::string locale = GetContentClient()->browser()->GetApplicationLocale(); | 320 std::string locale = GetContentClient()->browser()->GetApplicationLocale(); |
307 cmd_line->AppendSwitchASCII(switches::kLang, locale); | 321 cmd_line->AppendSwitchASCII(switches::kLang, locale); |
308 | 322 |
309 #if defined(OS_WIN) | 323 #if defined(OS_WIN) |
310 cmd_line->AppendArg(switches::kPrefetchArgumentOther); | 324 cmd_line->AppendArg(switches::kPrefetchArgumentOther); |
311 #endif // defined(OS_WIN) | 325 #endif // defined(OS_WIN) |
312 | 326 |
313 if (no_sandbox_) | 327 if (no_sandbox_) |
314 cmd_line->AppendSwitch(switches::kNoSandbox); | 328 cmd_line->AppendSwitch(switches::kNoSandbox); |
315 | 329 |
(...skipping 19 matching lines...) Expand all Loading... |
335 cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir, | 349 cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir, |
336 exposed_dir_); | 350 exposed_dir_); |
337 } | 351 } |
338 | 352 |
339 #if defined(OS_WIN) | 353 #if defined(OS_WIN) |
340 // Let the utility process know if it is intended to be elevated. | 354 // Let the utility process know if it is intended to be elevated. |
341 if (run_elevated_) | 355 if (run_elevated_) |
342 cmd_line->AppendSwitch(switches::kUtilityProcessRunningElevated); | 356 cmd_line->AppendSwitch(switches::kUtilityProcessRunningElevated); |
343 #endif | 357 #endif |
344 | 358 |
| 359 cmd_line->AppendSwitchASCII(switches::kMojoApplicationChannelToken, |
| 360 mojo_child_connection_->service_token()); |
| 361 |
345 process_->Launch( | 362 process_->Launch( |
346 new UtilitySandboxedProcessLauncherDelegate(exposed_dir_, | 363 new UtilitySandboxedProcessLauncherDelegate(exposed_dir_, |
347 run_elevated_, | 364 run_elevated_, |
348 no_sandbox_, env_, | 365 no_sandbox_, env_, |
349 process_->GetHost()), | 366 process_->GetHost()), |
350 cmd_line, | 367 cmd_line, |
351 true); | 368 true); |
352 } | 369 } |
353 | 370 |
354 return true; | 371 return true; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 base::WeakPtr<UtilityProcessHostImpl> host, | 419 base::WeakPtr<UtilityProcessHostImpl> host, |
403 int error_code) { | 420 int error_code) { |
404 if (!host) | 421 if (!host) |
405 return; | 422 return; |
406 | 423 |
407 host->OnProcessLaunchFailed(error_code); | 424 host->OnProcessLaunchFailed(error_code); |
408 delete host.get(); | 425 delete host.get(); |
409 } | 426 } |
410 | 427 |
411 } // namespace content | 428 } // namespace content |
OLD | NEW |