OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "shell/child_process_host.h" | 5 #include "shell/child_process_host.h" |
6 | 6 |
7 #include "base/base_switches.h" | 7 #include "base/base_switches.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/process/kill.h" | 13 #include "base/process/kill.h" |
14 #include "base/process/launch.h" | 14 #include "base/process/launch.h" |
15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
16 #include "base/task_runner.h" | 16 #include "base/task_runner.h" |
17 #include "base/task_runner_util.h" | 17 #include "base/task_runner_util.h" |
18 #include "shell/context.h" | 18 #include "shell/context.h" |
19 #include "shell/switches.h" | 19 #include "shell/switches.h" |
20 | 20 |
21 namespace mojo { | 21 namespace mojo { |
22 namespace shell { | 22 namespace shell { |
23 | 23 |
24 ChildProcessHost::ChildProcessHost(Context* context, | 24 ChildProcessHost::ChildProcessHost(Context* context, |
25 Delegate* delegate, | 25 Delegate* delegate, |
26 ChildProcess::Type type) | 26 ChildProcess::Type type) |
27 : context_(context), | 27 : context_(context), delegate_(delegate), type_(type) { |
28 delegate_(delegate), | |
29 type_(type), | |
30 child_process_handle_(base::kNullProcessHandle) { | |
31 DCHECK(delegate); | 28 DCHECK(delegate); |
32 platform_channel_ = platform_channel_pair_.PassServerHandle(); | 29 platform_channel_ = platform_channel_pair_.PassServerHandle(); |
33 CHECK(platform_channel_.is_valid()); | 30 CHECK(platform_channel_.is_valid()); |
34 } | 31 } |
35 | 32 |
36 ChildProcessHost::~ChildProcessHost() { | 33 ChildProcessHost::~ChildProcessHost() { |
37 if (child_process_handle_ != base::kNullProcessHandle) { | 34 if (child_process_.IsValid()) { |
38 LOG(WARNING) << "Destroying ChildProcessHost with unjoined child"; | 35 LOG(WARNING) << "Destroying ChildProcessHost with unjoined child"; |
39 base::CloseProcessHandle(child_process_handle_); | 36 child_process_.Close(); |
40 child_process_handle_ = base::kNullProcessHandle; | |
41 } | 37 } |
42 } | 38 } |
43 | 39 |
44 void ChildProcessHost::Start() { | 40 void ChildProcessHost::Start() { |
45 DCHECK_EQ(child_process_handle_, base::kNullProcessHandle); | 41 DCHECK(!child_process_.IsValid()); |
46 | 42 |
47 delegate_->WillStart(); | 43 delegate_->WillStart(); |
48 | 44 |
49 CHECK(base::PostTaskAndReplyWithResult( | 45 CHECK(base::PostTaskAndReplyWithResult( |
50 context_->task_runners()->blocking_pool(), FROM_HERE, | 46 context_->task_runners()->blocking_pool(), FROM_HERE, |
51 base::Bind(&ChildProcessHost::DoLaunch, base::Unretained(this)), | 47 base::Bind(&ChildProcessHost::DoLaunch, base::Unretained(this)), |
52 base::Bind(&ChildProcessHost::DidLaunch, base::Unretained(this)))); | 48 base::Bind(&ChildProcessHost::DidLaunch, base::Unretained(this)))); |
53 } | 49 } |
54 | 50 |
55 int ChildProcessHost::Join() { | 51 int ChildProcessHost::Join() { |
56 DCHECK_NE(child_process_handle_, base::kNullProcessHandle); | 52 DCHECK(child_process_.IsValid()); |
57 int rv = -1; | 53 int rv = -1; |
58 // Note: |WaitForExitCode()| closes the process handle. | 54 LOG_IF(ERROR, child_process_.WaitForExit(&rv)) |
59 LOG_IF(ERROR, !base::WaitForExitCode(child_process_handle_, &rv)) | |
60 << "Failed to wait for child process"; | 55 << "Failed to wait for child process"; |
61 child_process_handle_ = base::kNullProcessHandle; | 56 child_process_.Close(); |
62 return rv; | 57 return rv; |
63 } | 58 } |
64 | 59 |
65 bool ChildProcessHost::DoLaunch() { | 60 bool ChildProcessHost::DoLaunch() { |
66 static const char* kForwardSwitches[] = { | 61 static const char* kForwardSwitches[] = { |
67 switches::kTraceToConsole, switches::kV, switches::kVModule, | 62 switches::kTraceToConsole, switches::kV, switches::kVModule, |
68 }; | 63 }; |
69 | 64 |
70 const base::CommandLine* parent_command_line = | 65 const base::CommandLine* parent_command_line = |
71 base::CommandLine::ForCurrentProcess(); | 66 base::CommandLine::ForCurrentProcess(); |
72 base::CommandLine child_command_line(parent_command_line->GetProgram()); | 67 base::CommandLine child_command_line(parent_command_line->GetProgram()); |
73 child_command_line.CopySwitchesFrom(*parent_command_line, kForwardSwitches, | 68 child_command_line.CopySwitchesFrom(*parent_command_line, kForwardSwitches, |
74 arraysize(kForwardSwitches)); | 69 arraysize(kForwardSwitches)); |
75 child_command_line.AppendSwitchASCII( | 70 child_command_line.AppendSwitchASCII( |
76 switches::kChildProcessType, base::IntToString(static_cast<int>(type_))); | 71 switches::kChildProcessType, base::IntToString(static_cast<int>(type_))); |
77 | 72 |
78 embedder::HandlePassingInformation handle_passing_info; | 73 embedder::HandlePassingInformation handle_passing_info; |
79 platform_channel_pair_.PrepareToPassClientHandleToChildProcess( | 74 platform_channel_pair_.PrepareToPassClientHandleToChildProcess( |
80 &child_command_line, &handle_passing_info); | 75 &child_command_line, &handle_passing_info); |
81 | 76 |
82 base::LaunchOptions options; | 77 base::LaunchOptions options; |
83 #if defined(OS_WIN) | 78 #if defined(OS_WIN) |
84 options.start_hidden = true; | 79 options.start_hidden = true; |
85 options.handles_to_inherit = &handle_passing_info; | 80 options.handles_to_inherit = &handle_passing_info; |
86 #elif defined(OS_POSIX) | 81 #elif defined(OS_POSIX) |
87 options.fds_to_remap = &handle_passing_info; | 82 options.fds_to_remap = &handle_passing_info; |
88 #endif | 83 #endif |
89 | 84 child_process_ = base::LaunchProcess(child_command_line, options); |
90 if (!base::LaunchProcess(child_command_line, options, &child_process_handle_)) | 85 if (!child_process_.IsValid()) |
91 return false; | 86 return false; |
92 | 87 |
93 platform_channel_pair_.ChildProcessLaunched(); | 88 platform_channel_pair_.ChildProcessLaunched(); |
94 return true; | 89 return true; |
95 } | 90 } |
96 | 91 |
97 void ChildProcessHost::DidLaunch(bool success) { | 92 void ChildProcessHost::DidLaunch(bool success) { |
98 delegate_->DidStart(success); | 93 delegate_->DidStart(success); |
99 } | 94 } |
100 | 95 |
101 } // namespace shell | 96 } // namespace shell |
102 } // namespace mojo | 97 } // namespace mojo |
OLD | NEW |