| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/child/runner_connection.h" | 5 #include "mojo/runner/child/runner_connection.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/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
| 18 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
| 19 #include "base/threading/thread_checker.h" | 19 #include "base/threading/thread_checker.h" |
| 20 #include "mojo/edk/embedder/embedder.h" | 20 #include "mojo/edk/embedder/embedder.h" |
| 21 #include "mojo/edk/embedder/platform_channel_pair.h" | 21 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 22 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 22 #include "mojo/message_pump/message_pump_mojo.h" | 23 #include "mojo/message_pump/message_pump_mojo.h" |
| 23 #include "mojo/public/cpp/bindings/binding.h" | 24 #include "mojo/public/cpp/bindings/binding.h" |
| 24 #include "mojo/runner/child/child_controller.mojom.h" | 25 #include "mojo/runner/child/child_controller.mojom.h" |
| 25 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" | |
| 26 #include "third_party/mojo/src/mojo/edk/embedder/platform_channel_pair.h" | |
| 27 #include "third_party/mojo/src/mojo/edk/embedder/scoped_platform_handle.h" | |
| 28 | 26 |
| 29 namespace mojo { | 27 namespace mojo { |
| 30 namespace runner { | 28 namespace runner { |
| 31 namespace { | 29 namespace { |
| 32 | 30 |
| 33 void DidCreateChannel(embedder::ChannelInfo* channel_info) {} | |
| 34 | |
| 35 // Blocks a thread until another thread unblocks it, at which point it unblocks | 31 // Blocks a thread until another thread unblocks it, at which point it unblocks |
| 36 // and runs a closure provided by that thread. | 32 // and runs a closure provided by that thread. |
| 37 class Blocker { | 33 class Blocker { |
| 38 public: | 34 public: |
| 39 class Unblocker { | 35 class Unblocker { |
| 40 public: | 36 public: |
| 41 explicit Unblocker(Blocker* blocker = nullptr) : blocker_(blocker) {} | 37 explicit Unblocker(Blocker* blocker = nullptr) : blocker_(blocker) {} |
| 42 ~Unblocker() {} | 38 ~Unblocker() {} |
| 43 | 39 |
| 44 void Unblock(base::Closure run_after) { | 40 void Unblock(base::Closure run_after) { |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 _exit(exit_code); | 180 _exit(exit_code); |
| 185 } | 181 } |
| 186 | 182 |
| 187 private: | 183 private: |
| 188 ChildControllerImpl(RunnerConnectionImpl* connection, | 184 ChildControllerImpl(RunnerConnectionImpl* connection, |
| 189 const GotApplicationRequestCallback& callback, | 185 const GotApplicationRequestCallback& callback, |
| 190 const Blocker::Unblocker& unblocker) | 186 const Blocker::Unblocker& unblocker) |
| 191 : connection_(connection), | 187 : connection_(connection), |
| 192 callback_(callback), | 188 callback_(callback), |
| 193 unblocker_(unblocker), | 189 unblocker_(unblocker), |
| 194 channel_info_(nullptr), | |
| 195 binding_(this) {} | 190 binding_(this) {} |
| 196 | 191 |
| 197 static void ReturnApplicationRequestOnMainThread( | 192 static void ReturnApplicationRequestOnMainThread( |
| 198 const GotApplicationRequestCallback& callback, | 193 const GotApplicationRequestCallback& callback, |
| 199 InterfaceRequest<Application> application_request) { | 194 InterfaceRequest<Application> application_request) { |
| 200 callback.Run(std::move(application_request)); | 195 callback.Run(std::move(application_request)); |
| 201 } | 196 } |
| 202 | 197 |
| 203 base::ThreadChecker thread_checker_; | 198 base::ThreadChecker thread_checker_; |
| 204 RunnerConnectionImpl* const connection_; | 199 RunnerConnectionImpl* const connection_; |
| 205 GotApplicationRequestCallback callback_; | 200 GotApplicationRequestCallback callback_; |
| 206 Blocker::Unblocker unblocker_; | 201 Blocker::Unblocker unblocker_; |
| 207 StartAppCallback on_app_complete_; | 202 StartAppCallback on_app_complete_; |
| 208 | 203 |
| 209 embedder::ChannelInfo* channel_info_; | |
| 210 Binding<ChildController> binding_; | 204 Binding<ChildController> binding_; |
| 211 | 205 |
| 212 DISALLOW_COPY_AND_ASSIGN(ChildControllerImpl); | 206 DISALLOW_COPY_AND_ASSIGN(ChildControllerImpl); |
| 213 }; | 207 }; |
| 214 | 208 |
| 215 bool RunnerConnectionImpl::WaitForApplicationRequest( | 209 bool RunnerConnectionImpl::WaitForApplicationRequest( |
| 216 InterfaceRequest<Application>* request, | 210 InterfaceRequest<Application>* request, |
| 217 ScopedMessagePipeHandle handle) { | 211 ScopedMessagePipeHandle handle) { |
| 218 // If a valid message pipe to the runner was not provided, look for one on the | 212 // If a valid message pipe to the runner was not provided, look for one on the |
| 219 // command line. | 213 // command line. |
| 220 if (!handle.is_valid()) { | 214 if (!handle.is_valid()) { |
| 221 embedder::ScopedPlatformHandle platform_channel = | 215 edk::ScopedPlatformHandle platform_channel = |
| 222 embedder::PlatformChannelPair::PassClientHandleFromParentProcess( | 216 edk::PlatformChannelPair::PassClientHandleFromParentProcess( |
| 223 *base::CommandLine::ForCurrentProcess()); | 217 *base::CommandLine::ForCurrentProcess()); |
| 224 if (!platform_channel.is_valid()) | 218 if (!platform_channel.is_valid()) |
| 225 return false; | 219 return false; |
| 226 scoped_refptr<base::TaskRunner> task_runner; | 220 handle = edk::CreateMessagePipe(std::move(platform_channel)); |
| 227 if (!base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) | |
| 228 task_runner = base::ThreadTaskRunnerHandle::Get(); | |
| 229 handle = | |
| 230 embedder::CreateChannel(std::move(platform_channel), | |
| 231 base::Bind(&DidCreateChannel), task_runner); | |
| 232 // Copy of code in child_process.cc | 221 // Copy of code in child_process.cc |
| 233 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) { | |
| 234 // When using the new Mojo EDK, each message pipe is backed by a platform | |
| 235 // handle. The one platform handle that comes on the command line is used | |
| 236 // to bind to the ChildController interface. However we also want a | |
| 237 // platform handle to setup the communication channel by which we exchange | |
| 238 // handles to/from tokens, which is needed for sandboxed Windows | |
| 239 // processes. | |
| 240 char broker_handle[10]; | |
| 241 MojoHandleSignalsState state; | |
| 242 MojoResult rv = | |
| 243 MojoWait(handle.get().value(), MOJO_HANDLE_SIGNAL_READABLE, | |
| 244 MOJO_DEADLINE_INDEFINITE, &state); | |
| 245 CHECK_EQ(MOJO_RESULT_OK, rv); | |
| 246 uint32_t num_bytes = arraysize(broker_handle); | |
| 247 rv = MojoReadMessage(handle.get().value(), | |
| 248 broker_handle, &num_bytes, nullptr, 0, | |
| 249 MOJO_READ_MESSAGE_FLAG_NONE); | |
| 250 CHECK_EQ(MOJO_RESULT_OK, rv); | |
| 251 | 222 |
| 252 edk::ScopedPlatformHandle broker_channel = | 223 // When using the new Mojo EDK, each message pipe is backed by a platform |
| 253 edk::PlatformChannelPair::PassClientHandleFromParentProcessFromString( | 224 // handle. The one platform handle that comes on the command line is used |
| 254 std::string(broker_handle, num_bytes)); | 225 // to bind to the ChildController interface. However we also want a |
| 255 CHECK(broker_channel.is_valid()); | 226 // platform handle to setup the communication channel by which we exchange |
| 256 embedder::SetParentPipeHandle( | 227 // handles to/from tokens, which is needed for sandboxed Windows |
| 257 mojo::embedder::ScopedPlatformHandle(mojo::embedder::PlatformHandle( | 228 // processes. |
| 258 broker_channel.release().handle))); | 229 char broker_handle[10]; |
| 259 } | 230 MojoHandleSignalsState state; |
| 231 MojoResult rv = |
| 232 MojoWait(handle.get().value(), MOJO_HANDLE_SIGNAL_READABLE, |
| 233 MOJO_DEADLINE_INDEFINITE, &state); |
| 234 CHECK_EQ(MOJO_RESULT_OK, rv); |
| 235 uint32_t num_bytes = arraysize(broker_handle); |
| 236 rv = MojoReadMessage(handle.get().value(), |
| 237 broker_handle, &num_bytes, nullptr, 0, |
| 238 MOJO_READ_MESSAGE_FLAG_NONE); |
| 239 CHECK_EQ(MOJO_RESULT_OK, rv); |
| 240 |
| 241 edk::ScopedPlatformHandle broker_channel = |
| 242 edk::PlatformChannelPair::PassClientHandleFromParentProcessFromString( |
| 243 std::string(broker_handle, num_bytes)); |
| 244 CHECK(broker_channel.is_valid()); |
| 245 edk::SetParentPipeHandle(std::move(broker_channel)); |
| 260 } | 246 } |
| 261 | 247 |
| 262 Blocker blocker; | 248 Blocker blocker; |
| 263 controller_runner_->PostTask( | 249 controller_runner_->PostTask( |
| 264 FROM_HERE, | 250 FROM_HERE, |
| 265 base::Bind( | 251 base::Bind( |
| 266 &ChildControllerImpl::Create, base::Unretained(this), | 252 &ChildControllerImpl::Create, base::Unretained(this), |
| 267 base::Bind(&OnGotApplicationRequest, base::Unretained(request)), | 253 base::Bind(&OnGotApplicationRequest, base::Unretained(request)), |
| 268 base::Passed(&handle), blocker.GetUnblocker())); | 254 base::Passed(&handle), blocker.GetUnblocker())); |
| 269 blocker.Block(); | 255 blocker.Block(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 284 delete connection; | 270 delete connection; |
| 285 return nullptr; | 271 return nullptr; |
| 286 } | 272 } |
| 287 return connection; | 273 return connection; |
| 288 } | 274 } |
| 289 | 275 |
| 290 RunnerConnection::RunnerConnection() {} | 276 RunnerConnection::RunnerConnection() {} |
| 291 | 277 |
| 292 } // namespace runner | 278 } // namespace runner |
| 293 } // namespace mojo | 279 } // namespace mojo |
| OLD | NEW |