OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/shell/child_process_host.h" | |
6 | |
7 #include "base/base_switches.h" | |
8 #include "base/bind.h" | |
9 #include "base/command_line.h" | |
10 #include "base/location.h" | |
11 #include "base/logging.h" | |
12 #include "base/macros.h" | |
13 #include "base/message_loop/message_loop.h" | |
14 #include "base/process/kill.h" | |
15 #include "base/process/launch.h" | |
16 #include "base/stl_util.h" | |
17 #include "base/strings/string_split.h" | |
18 #include "base/task_runner.h" | |
19 #include "base/task_runner_util.h" | |
20 #include "mojo/edk/embedder/embedder.h" | |
21 #include "mojo/public/cpp/system/core.h" | |
22 #include "mojo/shell/context.h" | |
23 #include "mojo/shell/switches.h" | |
24 #include "mojo/shell/task_runners.h" | |
25 #include "ui/gl/gl_switches.h" | |
26 | |
27 namespace mojo { | |
28 namespace shell { | |
29 | |
30 ChildProcessHost::ChildProcessHost(Context* context, const std::string& name) | |
31 : context_(context), name_(name), channel_info_(nullptr) { | |
32 platform_channel_ = platform_channel_pair_.PassServerHandle(); | |
33 DCHECK(!name.empty()); | |
34 CHECK(platform_channel_.is_valid()); | |
35 } | |
36 | |
37 ChildProcessHost::~ChildProcessHost() { | |
38 if (child_process_.IsValid()) { | |
39 LOG(WARNING) << "Destroying ChildProcessHost with unjoined child"; | |
40 child_process_.Close(); | |
41 } | |
42 } | |
43 | |
44 void ChildProcessHost::Start() { | |
45 DCHECK(!child_process_.IsValid()); | |
46 DCHECK(platform_channel_.is_valid()); | |
47 | |
48 ScopedMessagePipeHandle handle(embedder::CreateChannel( | |
49 platform_channel_.Pass(), context_->task_runners()->io_runner(), | |
50 base::Bind(&ChildProcessHost::DidCreateChannel, base::Unretained(this)), | |
51 base::MessageLoop::current()->message_loop_proxy())); | |
52 | |
53 controller_.Bind(handle.Pass()); | |
54 | |
55 CHECK(base::PostTaskAndReplyWithResult( | |
56 context_->task_runners()->blocking_pool(), FROM_HERE, | |
57 base::Bind(&ChildProcessHost::DoLaunch, base::Unretained(this)), | |
58 base::Bind(&ChildProcessHost::DidStart, base::Unretained(this)))); | |
59 } | |
60 | |
61 int ChildProcessHost::Join() { | |
62 DCHECK(child_process_.IsValid()); | |
63 int rv = -1; | |
64 LOG_IF(ERROR, !child_process_.WaitForExit(&rv)) | |
65 << "Failed to wait for child process"; | |
66 child_process_.Close(); | |
67 return rv; | |
68 } | |
69 | |
70 void ChildProcessHost::StartApp( | |
71 const String& app_path, | |
72 bool clean_app_path, | |
73 InterfaceRequest<Application> application_request, | |
74 const ChildController::StartAppCallback& on_app_complete) { | |
75 DCHECK(controller_); | |
76 | |
77 on_app_complete_ = on_app_complete; | |
78 controller_->StartApp( | |
79 app_path, clean_app_path, application_request.Pass(), | |
80 base::Bind(&ChildProcessHost::AppCompleted, base::Unretained(this))); | |
81 } | |
82 | |
83 void ChildProcessHost::ExitNow(int32_t exit_code) { | |
84 DCHECK(controller_); | |
85 | |
86 controller_->ExitNow(exit_code); | |
87 } | |
88 | |
89 void ChildProcessHost::DidStart(bool success) { | |
90 DVLOG(2) << "ChildProcessHost::DidStart()"; | |
91 | |
92 if (!success) { | |
93 LOG(ERROR) << "Failed to start child process"; | |
94 AppCompleted(MOJO_RESULT_UNKNOWN); | |
95 return; | |
96 } | |
97 } | |
98 | |
99 bool ChildProcessHost::DoLaunch() { | |
100 static const char* kForwardSwitches[] = { | |
101 switches::kOverrideUseGLWithOSMesaForTests, | |
102 switches::kTraceToConsole, | |
103 switches::kV, | |
104 switches::kVModule, | |
105 }; | |
106 | |
107 const base::CommandLine* parent_command_line = | |
108 base::CommandLine::ForCurrentProcess(); | |
109 base::CommandLine child_command_line(parent_command_line->GetProgram()); | |
110 child_command_line.CopySwitchesFrom(*parent_command_line, kForwardSwitches, | |
111 arraysize(kForwardSwitches)); | |
112 child_command_line.AppendSwitchASCII(switches::kApp, name_); | |
113 child_command_line.AppendSwitch(switches::kChildProcess); | |
114 if (parent_command_line->HasSwitch(switches::kWaitForDebugger)) { | |
115 std::vector<std::string> apps_to_debug; | |
116 base::SplitString( | |
117 parent_command_line->GetSwitchValueASCII(switches::kWaitForDebugger), | |
118 ',', &apps_to_debug); | |
119 if (apps_to_debug.empty() || ContainsValue(apps_to_debug, name_)) | |
120 child_command_line.AppendSwitch(switches::kWaitForDebugger); | |
121 } | |
122 | |
123 auto args = parent_command_line->GetArgs(); | |
124 for (const auto& arg : args) | |
125 child_command_line.AppendArgNative(arg); | |
126 | |
127 embedder::HandlePassingInformation handle_passing_info; | |
128 platform_channel_pair_.PrepareToPassClientHandleToChildProcess( | |
129 &child_command_line, &handle_passing_info); | |
130 | |
131 base::LaunchOptions options; | |
132 #if defined(OS_WIN) | |
133 options.handles_to_inherit = &handle_passing_info; | |
134 #elif defined(OS_POSIX) | |
135 options.fds_to_remap = &handle_passing_info; | |
136 #endif | |
137 DVLOG(2) << "Launching child with command line: " | |
138 << child_command_line.GetCommandLineString(); | |
139 child_process_ = base::LaunchProcess(child_command_line, options); | |
140 if (!child_process_.IsValid()) | |
141 return false; | |
142 | |
143 platform_channel_pair_.ChildProcessLaunched(); | |
144 return true; | |
145 } | |
146 | |
147 void ChildProcessHost::AppCompleted(int32_t result) { | |
148 if (!on_app_complete_.is_null()) { | |
149 auto on_app_complete = on_app_complete_; | |
150 on_app_complete_.reset(); | |
151 on_app_complete.Run(result); | |
152 } | |
153 } | |
154 | |
155 void ChildProcessHost::DidCreateChannel(embedder::ChannelInfo* channel_info) { | |
156 DVLOG(2) << "AppChildProcessHost::DidCreateChannel()"; | |
157 | |
158 CHECK(channel_info); | |
159 channel_info_ = channel_info; | |
160 } | |
161 | |
162 } // namespace shell | |
163 } // namespace mojo | |
OLD | NEW |