| 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/message_loop/message_loop.h" |
| 13 #include "base/process/kill.h" | 14 #include "base/process/kill.h" |
| 14 #include "base/process/launch.h" | 15 #include "base/process/launch.h" |
| 15 #include "base/task_runner.h" | 16 #include "base/task_runner.h" |
| 16 #include "base/task_runner_util.h" | 17 #include "base/task_runner_util.h" |
| 18 #include "mojo/edk/embedder/embedder.h" |
| 19 #include "mojo/public/cpp/system/message_pipe.h" |
| 17 #include "shell/context.h" | 20 #include "shell/context.h" |
| 18 #include "shell/switches.h" | 21 #include "shell/switches.h" |
| 22 #include "shell/task_runners.h" |
| 19 | 23 |
| 20 namespace mojo { | 24 namespace mojo { |
| 21 namespace shell { | 25 namespace shell { |
| 22 | 26 |
| 23 ChildProcessHost::ChildProcessHost(Context* context) : context_(context) { | 27 ChildProcessHost::ChildProcessHost(Context* context) |
| 24 platform_channel_ = platform_channel_pair_.PassServerHandle(); | 28 : context_(context), channel_info_(nullptr) { |
| 25 CHECK(platform_channel_.is_valid()); | |
| 26 } | 29 } |
| 27 | 30 |
| 28 ChildProcessHost::~ChildProcessHost() { | 31 ChildProcessHost::~ChildProcessHost() { |
| 29 if (child_process_.IsValid()) { | 32 DCHECK(!child_process_.IsValid()); |
| 30 LOG(WARNING) << "Destroying ChildProcessHost with unjoined child"; | |
| 31 child_process_.Close(); | |
| 32 } | |
| 33 } | 33 } |
| 34 | 34 |
| 35 void ChildProcessHost::Start() { | 35 void ChildProcessHost::Start() { |
| 36 DCHECK(!child_process_.IsValid()); | 36 DCHECK(!child_process_.IsValid()); |
| 37 | 37 |
| 38 WillStart(); | 38 ScopedMessagePipeHandle handle(embedder::CreateChannel( |
| 39 platform_channel_pair_.PassServerHandle(), |
| 40 context_->task_runners()->io_runner(), |
| 41 base::Bind(&ChildProcessHost::DidCreateChannel, base::Unretained(this)), |
| 42 base::MessageLoop::current()->message_loop_proxy())); |
| 43 |
| 44 controller_.Bind(handle.Pass()); |
| 45 controller_.set_error_handler(this); |
| 39 | 46 |
| 40 CHECK(base::PostTaskAndReplyWithResult( | 47 CHECK(base::PostTaskAndReplyWithResult( |
| 41 context_->task_runners()->blocking_pool(), FROM_HERE, | 48 context_->task_runners()->blocking_pool(), FROM_HERE, |
| 42 base::Bind(&ChildProcessHost::DoLaunch, base::Unretained(this)), | 49 base::Bind(&ChildProcessHost::DoLaunch, base::Unretained(this)), |
| 43 base::Bind(&ChildProcessHost::DidStart, base::Unretained(this)))); | 50 base::Bind(&ChildProcessHost::DidStart, base::Unretained(this)))); |
| 44 } | 51 } |
| 45 | 52 |
| 46 int ChildProcessHost::Join() { | 53 int ChildProcessHost::Join() { |
| 47 DCHECK(child_process_.IsValid()); | 54 DCHECK(child_process_.IsValid()); |
| 48 int rv = -1; | 55 int rv = -1; |
| 49 LOG_IF(ERROR, !child_process_.WaitForExit(&rv)) | 56 LOG_IF(ERROR, !child_process_.WaitForExit(&rv)) |
| 50 << "Failed to wait for child process"; | 57 << "Failed to wait for child process"; |
| 51 child_process_.Close(); | 58 child_process_.Close(); |
| 52 return rv; | 59 return rv; |
| 53 } | 60 } |
| 54 | 61 |
| 62 void ChildProcessHost::StartApp( |
| 63 const String& app_path, |
| 64 bool clean_app_path, |
| 65 InterfaceRequest<Application> application_request, |
| 66 const AppChildController::StartAppCallback& on_app_complete) { |
| 67 DCHECK(controller_); |
| 68 |
| 69 on_app_complete_ = on_app_complete; |
| 70 controller_->StartApp( |
| 71 app_path, clean_app_path, application_request.Pass(), |
| 72 base::Bind(&ChildProcessHost::AppCompleted, base::Unretained(this))); |
| 73 } |
| 74 |
| 75 void ChildProcessHost::ExitNow(int32_t exit_code) { |
| 76 DCHECK(controller_); |
| 77 |
| 78 controller_->ExitNow(exit_code); |
| 79 } |
| 80 |
| 81 void ChildProcessHost::DidStart(bool success) { |
| 82 DVLOG(2) << "ChildProcessHost::DidStart()"; |
| 83 |
| 84 if (!success) { |
| 85 LOG(ERROR) << "Failed to start app child process"; |
| 86 AppCompleted(MOJO_RESULT_UNKNOWN); |
| 87 return; |
| 88 } |
| 89 } |
| 90 |
| 91 // Callback for |embedder::CreateChannel()|. |
| 92 void ChildProcessHost::DidCreateChannel(embedder::ChannelInfo* channel_info) { |
| 93 DVLOG(2) << "ChildProcessHost::DidCreateChannel()"; |
| 94 |
| 95 CHECK(channel_info); |
| 96 channel_info_ = channel_info; |
| 97 } |
| 98 |
| 55 bool ChildProcessHost::DoLaunch() { | 99 bool ChildProcessHost::DoLaunch() { |
| 56 static const char* kForwardSwitches[] = { | 100 static const char* kForwardSwitches[] = { |
| 57 switches::kTraceToConsole, switches::kV, switches::kVModule, | 101 switches::kTraceToConsole, switches::kV, switches::kVModule, |
| 58 }; | 102 }; |
| 59 | 103 |
| 60 const base::CommandLine* parent_command_line = | 104 const base::CommandLine* parent_command_line = |
| 61 base::CommandLine::ForCurrentProcess(); | 105 base::CommandLine::ForCurrentProcess(); |
| 62 base::CommandLine child_command_line(parent_command_line->GetProgram()); | 106 base::CommandLine child_command_line(parent_command_line->GetProgram()); |
| 63 child_command_line.CopySwitchesFrom(*parent_command_line, kForwardSwitches, | 107 child_command_line.CopySwitchesFrom(*parent_command_line, kForwardSwitches, |
| 64 arraysize(kForwardSwitches)); | 108 arraysize(kForwardSwitches)); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 78 DVLOG(2) << "Launching child with command line: " | 122 DVLOG(2) << "Launching child with command line: " |
| 79 << child_command_line.GetCommandLineString(); | 123 << child_command_line.GetCommandLineString(); |
| 80 child_process_ = base::LaunchProcess(child_command_line, options); | 124 child_process_ = base::LaunchProcess(child_command_line, options); |
| 81 if (!child_process_.IsValid()) | 125 if (!child_process_.IsValid()) |
| 82 return false; | 126 return false; |
| 83 | 127 |
| 84 platform_channel_pair_.ChildProcessLaunched(); | 128 platform_channel_pair_.ChildProcessLaunched(); |
| 85 return true; | 129 return true; |
| 86 } | 130 } |
| 87 | 131 |
| 132 void ChildProcessHost::AppCompleted(int32_t result) { |
| 133 if (!on_app_complete_.is_null()) { |
| 134 auto on_app_complete = on_app_complete_; |
| 135 on_app_complete_.reset(); |
| 136 on_app_complete.Run(result); |
| 137 } |
| 138 } |
| 139 |
| 140 void ChildProcessHost::OnConnectionError() { |
| 141 AppCompleted(MOJO_RESULT_UNKNOWN); |
| 142 } |
| 143 |
| 88 } // namespace shell | 144 } // namespace shell |
| 89 } // namespace mojo | 145 } // namespace mojo |
| OLD | NEW |