| 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 "mojo/runner/host/child_process_host.h" | 5 #include "mojo/runner/host/child_process_host.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.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/kill.h" | 17 #include "base/process/kill.h" |
| 18 #include "base/process/launch.h" | 18 #include "base/process/launch.h" |
| 19 #include "base/task_runner.h" | 19 #include "base/task_runner.h" |
| 20 #include "base/thread_task_runner_handle.h" | 20 #include "base/thread_task_runner_handle.h" |
| 21 #include "mojo/edk/embedder/embedder.h" |
| 21 #include "mojo/public/cpp/bindings/interface_ptr_info.h" | 22 #include "mojo/public/cpp/bindings/interface_ptr_info.h" |
| 22 #include "mojo/public/cpp/system/core.h" | 23 #include "mojo/public/cpp/system/core.h" |
| 23 #include "mojo/runner/host/switches.h" | 24 #include "mojo/runner/host/switches.h" |
| 24 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" | |
| 25 | 25 |
| 26 #if defined(OS_LINUX) && !defined(OS_ANDROID) | 26 #if defined(OS_LINUX) && !defined(OS_ANDROID) |
| 27 #include "sandbox/linux/services/namespace_sandbox.h" | 27 #include "sandbox/linux/services/namespace_sandbox.h" |
| 28 #endif | 28 #endif |
| 29 | 29 |
| 30 #if defined(OS_WIN) | 30 #if defined(OS_WIN) |
| 31 #include "base/win/windows_version.h" | 31 #include "base/win/windows_version.h" |
| 32 #endif | 32 #endif |
| 33 | 33 |
| 34 namespace mojo { | 34 namespace mojo { |
| 35 namespace runner { | 35 namespace runner { |
| 36 | 36 |
| 37 ChildProcessHost::ChildProcessHost(base::TaskRunner* launch_process_runner, | 37 ChildProcessHost::ChildProcessHost(base::TaskRunner* launch_process_runner, |
| 38 bool start_sandboxed, | 38 bool start_sandboxed, |
| 39 const base::FilePath& app_path) | 39 const base::FilePath& app_path) |
| 40 : launch_process_runner_(launch_process_runner), | 40 : launch_process_runner_(launch_process_runner), |
| 41 start_sandboxed_(start_sandboxed), | 41 start_sandboxed_(start_sandboxed), |
| 42 app_path_(app_path), | 42 app_path_(app_path), |
| 43 channel_info_(nullptr), | |
| 44 start_child_process_event_(false, false), | 43 start_child_process_event_(false, false), |
| 45 weak_factory_(this) { | 44 weak_factory_(this) { |
| 46 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) | 45 serializer_platform_channel_pair_.reset(new edk::PlatformChannelPair(true)); |
| 47 serializer_platform_channel_pair_.reset(new edk::PlatformChannelPair(true)); | |
| 48 | 46 |
| 49 child_message_pipe_ = embedder::CreateChannel( | 47 child_message_pipe_ = edk::CreateMessagePipe( |
| 50 platform_channel_pair_.PassServerHandle(), | 48 platform_channel_pair_.PassServerHandle()); |
| 51 base::Bind(&ChildProcessHost::DidCreateChannel, base::Unretained(this)), | |
| 52 base::ThreadTaskRunnerHandle::Get()); | |
| 53 } | 49 } |
| 54 | 50 |
| 55 ChildProcessHost::ChildProcessHost(ScopedHandle channel) | 51 ChildProcessHost::ChildProcessHost(ScopedHandle channel) |
| 56 : launch_process_runner_(nullptr), | 52 : launch_process_runner_(nullptr), |
| 57 start_sandboxed_(false), | 53 start_sandboxed_(false), |
| 58 channel_info_(nullptr), | |
| 59 start_child_process_event_(false, false), | 54 start_child_process_event_(false, false), |
| 60 weak_factory_(this) { | 55 weak_factory_(this) { |
| 61 CHECK(channel.is_valid()); | 56 CHECK(channel.is_valid()); |
| 62 ScopedMessagePipeHandle handle(MessagePipeHandle(channel.release().value())); | 57 ScopedMessagePipeHandle handle(MessagePipeHandle(channel.release().value())); |
| 63 controller_.Bind(InterfacePtrInfo<ChildController>(std::move(handle), 0u)); | 58 controller_.Bind(InterfacePtrInfo<ChildController>(std::move(handle), 0u)); |
| 64 } | 59 } |
| 65 | 60 |
| 66 ChildProcessHost::~ChildProcessHost() { | 61 ChildProcessHost::~ChildProcessHost() { |
| 67 if (!app_path_.empty()) | 62 if (!app_path_.empty()) |
| 68 CHECK(!controller_) << "Destroying ChildProcessHost before calling Join"; | 63 CHECK(!controller_) << "Destroying ChildProcessHost before calling Join"; |
| 69 } | 64 } |
| 70 | 65 |
| 71 void ChildProcessHost::Start( | 66 void ChildProcessHost::Start( |
| 72 const base::Callback<void(base::ProcessId)>& pid_available_callback) { | 67 const base::Callback<void(base::ProcessId)>& pid_available_callback) { |
| 73 DCHECK(!child_process_.IsValid()); | 68 DCHECK(!child_process_.IsValid()); |
| 74 DCHECK(child_message_pipe_.is_valid()); | 69 DCHECK(child_message_pipe_.is_valid()); |
| 75 | 70 |
| 76 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) { | 71 std::string client_handle_as_string = |
| 77 std::string client_handle_as_string = | 72 serializer_platform_channel_pair_ |
| 78 serializer_platform_channel_pair_ | 73 ->PrepareToPassClientHandleToChildProcessAsString( |
| 79 ->PrepareToPassClientHandleToChildProcessAsString( | 74 &handle_passing_info_); |
| 80 &handle_passing_info_); | 75 // We can't send the MP for the token serializer implementation as a |
| 81 // We can't send the MP for the token serializer implementation as a | 76 // platform handle, because that would require the other side to use the |
| 82 // platform handle, because that would require the other side to use the | 77 // token initializer itself! So instead we send it as a string. |
| 83 // token initializer itself! So instead we send it as a string. | 78 MojoResult rv = MojoWriteMessage( |
| 84 MojoResult rv = MojoWriteMessage( | 79 child_message_pipe_.get().value(), client_handle_as_string.c_str(), |
| 85 child_message_pipe_.get().value(), client_handle_as_string.c_str(), | 80 static_cast<uint32_t>(client_handle_as_string.size()), nullptr, 0, |
| 86 static_cast<uint32_t>(client_handle_as_string.size()), nullptr, 0, | 81 MOJO_WRITE_MESSAGE_FLAG_NONE); |
| 87 MOJO_WRITE_MESSAGE_FLAG_NONE); | 82 DCHECK_EQ(rv, MOJO_RESULT_OK); |
| 88 DCHECK_EQ(rv, MOJO_RESULT_OK); | |
| 89 } | |
| 90 | 83 |
| 91 controller_.Bind( | 84 controller_.Bind( |
| 92 InterfacePtrInfo<ChildController>(std::move(child_message_pipe_), 0u)); | 85 InterfacePtrInfo<ChildController>(std::move(child_message_pipe_), 0u)); |
| 93 | 86 |
| 94 launch_process_runner_->PostTaskAndReply( | 87 launch_process_runner_->PostTaskAndReply( |
| 95 FROM_HERE, | 88 FROM_HERE, |
| 96 base::Bind(&ChildProcessHost::DoLaunch, base::Unretained(this)), | 89 base::Bind(&ChildProcessHost::DoLaunch, base::Unretained(this)), |
| 97 base::Bind(&ChildProcessHost::DidStart, weak_factory_.GetWeakPtr(), | 90 base::Bind(&ChildProcessHost::DidStart, weak_factory_.GetWeakPtr(), |
| 98 pid_available_callback)); | 91 pid_available_callback)); |
| 99 } | 92 } |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 << " support."; | 198 << " support."; |
| 206 } | 199 } |
| 207 } else | 200 } else |
| 208 #endif | 201 #endif |
| 209 child_process_ = base::LaunchProcess(child_command_line, options); | 202 child_process_ = base::LaunchProcess(child_command_line, options); |
| 210 | 203 |
| 211 if (child_process_.IsValid()) { | 204 if (child_process_.IsValid()) { |
| 212 platform_channel_pair_.ChildProcessLaunched(); | 205 platform_channel_pair_.ChildProcessLaunched(); |
| 213 if (serializer_platform_channel_pair_.get()) { | 206 if (serializer_platform_channel_pair_.get()) { |
| 214 serializer_platform_channel_pair_->ChildProcessLaunched(); | 207 serializer_platform_channel_pair_->ChildProcessLaunched(); |
| 215 mojo::embedder::ChildProcessLaunched( | 208 mojo::edk::ChildProcessLaunched( |
| 216 child_process_.Handle(), | 209 child_process_.Handle(), |
| 217 mojo::embedder::ScopedPlatformHandle(mojo::embedder::PlatformHandle( | 210 serializer_platform_channel_pair_->PassServerHandle()); |
| 218 serializer_platform_channel_pair_->PassServerHandle().release(). | |
| 219 handle))); | |
| 220 } | 211 } |
| 221 } | 212 } |
| 222 start_child_process_event_.Signal(); | 213 start_child_process_event_.Signal(); |
| 223 } | 214 } |
| 224 | 215 |
| 225 void ChildProcessHost::AppCompleted(int32_t result) { | 216 void ChildProcessHost::AppCompleted(int32_t result) { |
| 226 if (!on_app_complete_.is_null()) { | 217 if (!on_app_complete_.is_null()) { |
| 227 auto on_app_complete = on_app_complete_; | 218 auto on_app_complete = on_app_complete_; |
| 228 on_app_complete_.reset(); | 219 on_app_complete_.reset(); |
| 229 on_app_complete.Run(result); | 220 on_app_complete.Run(result); |
| 230 } | 221 } |
| 231 } | 222 } |
| 232 | 223 |
| 233 void ChildProcessHost::DidCreateChannel(embedder::ChannelInfo* channel_info) { | |
| 234 DVLOG(2) << "AppChildProcessHost::DidCreateChannel()"; | |
| 235 | |
| 236 DCHECK(channel_info || | |
| 237 base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")); | |
| 238 channel_info_ = channel_info; | |
| 239 } | |
| 240 | |
| 241 } // namespace runner | 224 } // namespace runner |
| 242 } // namespace mojo | 225 } // namespace mojo |
| OLD | NEW |