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.
5 #include "mojo/runner/child_process_host.h"
8 #include "base/command_line.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/macros.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/process/kill.h"
14 #include "base/process/launch.h"
15 #include "base/task_runner.h"
16 #include "base/task_runner_util.h"
17 #include "base/thread_task_runner_handle.h"
18 #include "mojo/edk/embedder/embedder.h"
19 #include "mojo/public/cpp/bindings/interface_ptr_info.h"
20 #include "mojo/public/cpp/system/core.h"
21 #include "mojo/runner/context.h"
22 #include "mojo/runner/switches.h"
23 #include "mojo/runner/task_runners.h"
28 ChildProcessHost::ChildProcessHost(Context
* context
, const std::string
& name
)
29 : context_(context
), name_(name
), channel_info_(nullptr) {
30 platform_channel_
= platform_channel_pair_
.PassServerHandle();
31 DCHECK(!name
.empty());
32 CHECK(platform_channel_
.is_valid());
35 ChildProcessHost::~ChildProcessHost() {
36 if (child_process_
.IsValid()) {
37 LOG(WARNING
) << "Destroying ChildProcessHost with unjoined child";
38 child_process_
.Close();
42 void ChildProcessHost::Start() {
43 DCHECK(!child_process_
.IsValid());
44 DCHECK(platform_channel_
.is_valid());
46 ScopedMessagePipeHandle
handle(embedder::CreateChannel(
47 platform_channel_
.Pass(),
48 base::Bind(&ChildProcessHost::DidCreateChannel
, base::Unretained(this)),
49 base::ThreadTaskRunnerHandle::Get()));
51 controller_
.Bind(InterfacePtrInfo
<ChildController
>(handle
.Pass(), 0u));
53 CHECK(base::PostTaskAndReplyWithResult(
54 context_
->task_runners()->blocking_pool(), FROM_HERE
,
55 base::Bind(&ChildProcessHost::DoLaunch
, base::Unretained(this)),
56 base::Bind(&ChildProcessHost::DidStart
, base::Unretained(this))));
59 int ChildProcessHost::Join() {
60 DCHECK(child_process_
.IsValid());
62 LOG_IF(ERROR
, !child_process_
.WaitForExit(&rv
))
63 << "Failed to wait for child process";
64 child_process_
.Close();
68 void ChildProcessHost::StartApp(
69 const String
& app_path
,
71 InterfaceRequest
<Application
> application_request
,
72 const ChildController::StartAppCallback
& on_app_complete
) {
75 on_app_complete_
= on_app_complete
;
76 controller_
->StartApp(
77 app_path
, clean_app_path
, application_request
.Pass(),
78 base::Bind(&ChildProcessHost::AppCompleted
, base::Unretained(this)));
81 void ChildProcessHost::ExitNow(int32_t exit_code
) {
84 controller_
->ExitNow(exit_code
);
87 void ChildProcessHost::DidStart(bool success
) {
88 DVLOG(2) << "ChildProcessHost::DidStart()";
91 LOG(ERROR
) << "Failed to start child process";
92 AppCompleted(MOJO_RESULT_UNKNOWN
);
97 bool ChildProcessHost::DoLaunch() {
98 const base::CommandLine
* parent_command_line
=
99 base::CommandLine::ForCurrentProcess();
100 base::CommandLine
child_command_line(parent_command_line
->GetProgram());
101 child_command_line
.AppendArguments(*parent_command_line
, false);
102 child_command_line
.AppendSwitchASCII(switches::kApp
, name_
);
103 child_command_line
.AppendSwitch(switches::kChildProcess
);
105 embedder::HandlePassingInformation handle_passing_info
;
106 platform_channel_pair_
.PrepareToPassClientHandleToChildProcess(
107 &child_command_line
, &handle_passing_info
);
109 base::LaunchOptions options
;
111 options
.handles_to_inherit
= &handle_passing_info
;
112 #elif defined(OS_POSIX)
113 options
.fds_to_remap
= &handle_passing_info
;
115 DVLOG(2) << "Launching child with command line: "
116 << child_command_line
.GetCommandLineString();
117 child_process_
= base::LaunchProcess(child_command_line
, options
);
118 if (!child_process_
.IsValid())
121 platform_channel_pair_
.ChildProcessLaunched();
125 void ChildProcessHost::AppCompleted(int32_t result
) {
126 if (!on_app_complete_
.is_null()) {
127 auto on_app_complete
= on_app_complete_
;
128 on_app_complete_
.reset();
129 on_app_complete
.Run(result
);
133 void ChildProcessHost::DidCreateChannel(embedder::ChannelInfo
* channel_info
) {
134 DVLOG(2) << "AppChildProcessHost::DidCreateChannel()";
137 channel_info_
= channel_info
;
140 } // namespace runner