| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
| 15 #include "mojo/message_pump/message_pump_mojo.h" | 15 #include "mojo/message_pump/message_pump_mojo.h" |
| 16 #include "mojo/public/cpp/bindings/binding.h" | 16 #include "mojo/public/cpp/bindings/binding.h" |
| 17 #include "mojo/runner/child/child_controller.mojom.h" | 17 #include "mojo/runner/child/child_controller.mojom.h" |
| 18 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" | 18 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" |
| 19 #include "third_party/mojo/src/mojo/edk/embedder/platform_channel_pair.h" | 19 #include "third_party/mojo/src/mojo/edk/embedder/platform_channel_pair.h" |
| 20 #include "third_party/mojo/src/mojo/edk/embedder/scoped_platform_handle.h" | 20 #include "third_party/mojo/src/mojo/edk/embedder/scoped_platform_handle.h" |
| 21 | 21 |
| 22 namespace mojo { | 22 namespace mojo { |
| 23 namespace runner { | 23 namespace runner { |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 void DidCreateChannel(embedder::ChannelInfo* channel_info) {} |
| 27 |
| 26 // Blocks a thread until another thread unblocks it, at which point it unblocks | 28 // Blocks a thread until another thread unblocks it, at which point it unblocks |
| 27 // and runs a closure provided by that thread. | 29 // and runs a closure provided by that thread. |
| 28 class Blocker { | 30 class Blocker { |
| 29 public: | 31 public: |
| 30 class Unblocker { | 32 class Unblocker { |
| 31 public: | 33 public: |
| 32 explicit Unblocker(Blocker* blocker = nullptr) : blocker_(blocker) {} | 34 explicit Unblocker(Blocker* blocker = nullptr) : blocker_(blocker) {} |
| 33 ~Unblocker() {} | 35 ~Unblocker() {} |
| 34 | 36 |
| 35 void Unblock(base::Closure run_after) { | 37 void Unblock(base::Closure run_after) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 } | 84 } |
| 83 ~RunnerConnectionImpl() override { | 85 ~RunnerConnectionImpl() override { |
| 84 controller_runner_->PostTask( | 86 controller_runner_->PostTask( |
| 85 FROM_HERE, base::Bind(&RunnerConnectionImpl::ShutdownOnControllerThread, | 87 FROM_HERE, base::Bind(&RunnerConnectionImpl::ShutdownOnControllerThread, |
| 86 base::Unretained(this))); | 88 base::Unretained(this))); |
| 87 controller_thread_.Stop(); | 89 controller_thread_.Stop(); |
| 88 } | 90 } |
| 89 | 91 |
| 90 // Returns true if a connection to the runner has been established and | 92 // Returns true if a connection to the runner has been established and |
| 91 // |request| has been modified, false if no connection was established. | 93 // |request| has been modified, false if no connection was established. |
| 92 bool WaitForApplicationRequest(InterfaceRequest<Application>* request); | 94 bool WaitForApplicationRequest(InterfaceRequest<Application>* request, |
| 95 ScopedMessagePipeHandle handle); |
| 93 | 96 |
| 94 ChildControllerImpl* controller() const { return controller_.get(); } | 97 ChildControllerImpl* controller() const { return controller_.get(); } |
| 95 | 98 |
| 96 void set_controller(scoped_ptr<ChildControllerImpl> controller) { | 99 void set_controller(scoped_ptr<ChildControllerImpl> controller) { |
| 97 controller_ = controller.Pass(); | 100 controller_ = controller.Pass(); |
| 98 } | 101 } |
| 99 | 102 |
| 100 private: | 103 private: |
| 101 void StartControllerThread() { | 104 void StartControllerThread() { |
| 102 base::Thread::Options controller_thread_options; | 105 base::Thread::Options controller_thread_options; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 126 DCHECK(thread_checker_.CalledOnValidThread()); | 129 DCHECK(thread_checker_.CalledOnValidThread()); |
| 127 | 130 |
| 128 // TODO(vtl): Pass in the result from |MainMain()|. | 131 // TODO(vtl): Pass in the result from |MainMain()|. |
| 129 on_app_complete_.Run(MOJO_RESULT_UNIMPLEMENTED); | 132 on_app_complete_.Run(MOJO_RESULT_UNIMPLEMENTED); |
| 130 } | 133 } |
| 131 | 134 |
| 132 // To be executed on the controller thread. Creates the |ChildController|, | 135 // To be executed on the controller thread. Creates the |ChildController|, |
| 133 // etc. | 136 // etc. |
| 134 static void Create(RunnerConnectionImpl* connection, | 137 static void Create(RunnerConnectionImpl* connection, |
| 135 const GotApplicationRequestCallback& callback, | 138 const GotApplicationRequestCallback& callback, |
| 136 embedder::ScopedPlatformHandle platform_channel, | 139 ScopedMessagePipeHandle runner_handle, |
| 137 const Blocker::Unblocker& unblocker) { | 140 const Blocker::Unblocker& unblocker) { |
| 138 DCHECK(connection); | 141 DCHECK(connection); |
| 139 DCHECK(platform_channel.is_valid()); | |
| 140 | |
| 141 DCHECK(!connection->controller()); | 142 DCHECK(!connection->controller()); |
| 142 | 143 |
| 143 scoped_ptr<ChildControllerImpl> impl( | 144 scoped_ptr<ChildControllerImpl> impl( |
| 144 new ChildControllerImpl(connection, callback, unblocker)); | 145 new ChildControllerImpl(connection, callback, unblocker)); |
| 145 | 146 |
| 146 ScopedMessagePipeHandle host_message_pipe(embedder::CreateChannel( | 147 impl->Bind(runner_handle.Pass()); |
| 147 platform_channel.Pass(), | |
| 148 base::Bind(&ChildControllerImpl::DidCreateChannel, | |
| 149 base::Unretained(impl.get())), | |
| 150 base::ThreadTaskRunnerHandle::Get())); | |
| 151 | |
| 152 impl->Bind(host_message_pipe.Pass()); | |
| 153 | 148 |
| 154 connection->set_controller(impl.Pass()); | 149 connection->set_controller(impl.Pass()); |
| 155 } | 150 } |
| 156 | 151 |
| 157 void Bind(ScopedMessagePipeHandle handle) { binding_.Bind(handle.Pass()); } | 152 void Bind(ScopedMessagePipeHandle handle) { binding_.Bind(handle.Pass()); } |
| 158 | 153 |
| 159 void OnConnectionError() { | 154 void OnConnectionError() { |
| 160 // A connection error means the connection to the shell is lost. This is not | 155 // A connection error means the connection to the shell is lost. This is not |
| 161 // recoverable. | 156 // recoverable. |
| 162 LOG(ERROR) << "Connection error to the shell."; | 157 LOG(ERROR) << "Connection error to the shell."; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 184 const GotApplicationRequestCallback& callback, | 179 const GotApplicationRequestCallback& callback, |
| 185 const Blocker::Unblocker& unblocker) | 180 const Blocker::Unblocker& unblocker) |
| 186 : connection_(connection), | 181 : connection_(connection), |
| 187 callback_(callback), | 182 callback_(callback), |
| 188 unblocker_(unblocker), | 183 unblocker_(unblocker), |
| 189 channel_info_(nullptr), | 184 channel_info_(nullptr), |
| 190 binding_(this) { | 185 binding_(this) { |
| 191 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); | 186 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); |
| 192 } | 187 } |
| 193 | 188 |
| 194 // Callback for |embedder::CreateChannel()|. | |
| 195 void DidCreateChannel(embedder::ChannelInfo* channel_info) { | |
| 196 DVLOG(2) << "ChildControllerImpl::DidCreateChannel()"; | |
| 197 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 198 channel_info_ = channel_info; | |
| 199 } | |
| 200 | |
| 201 static void ReturnApplicationRequestOnMainThread( | 189 static void ReturnApplicationRequestOnMainThread( |
| 202 const GotApplicationRequestCallback& callback, | 190 const GotApplicationRequestCallback& callback, |
| 203 InterfaceRequest<Application> application_request) { | 191 InterfaceRequest<Application> application_request) { |
| 204 callback.Run(application_request.Pass()); | 192 callback.Run(application_request.Pass()); |
| 205 } | 193 } |
| 206 | 194 |
| 207 base::ThreadChecker thread_checker_; | 195 base::ThreadChecker thread_checker_; |
| 208 RunnerConnectionImpl* const connection_; | 196 RunnerConnectionImpl* const connection_; |
| 209 GotApplicationRequestCallback callback_; | 197 GotApplicationRequestCallback callback_; |
| 210 Blocker::Unblocker unblocker_; | 198 Blocker::Unblocker unblocker_; |
| 211 StartAppCallback on_app_complete_; | 199 StartAppCallback on_app_complete_; |
| 212 | 200 |
| 213 embedder::ChannelInfo* channel_info_; | 201 embedder::ChannelInfo* channel_info_; |
| 214 Binding<ChildController> binding_; | 202 Binding<ChildController> binding_; |
| 215 | 203 |
| 216 DISALLOW_COPY_AND_ASSIGN(ChildControllerImpl); | 204 DISALLOW_COPY_AND_ASSIGN(ChildControllerImpl); |
| 217 }; | 205 }; |
| 218 | 206 |
| 219 bool RunnerConnectionImpl::WaitForApplicationRequest( | 207 bool RunnerConnectionImpl::WaitForApplicationRequest( |
| 220 InterfaceRequest<Application>* request) { | 208 InterfaceRequest<Application>* request, |
| 221 embedder::ScopedPlatformHandle platform_channel = | 209 ScopedMessagePipeHandle handle) { |
| 222 embedder::PlatformChannelPair::PassClientHandleFromParentProcess( | 210 // If a valid message pipe to the runner was not provided, look for one on the |
| 223 *base::CommandLine::ForCurrentProcess()); | 211 // command line. |
| 224 if (!platform_channel.is_valid()) | 212 if (!handle.is_valid()) { |
| 225 return false; | 213 embedder::ScopedPlatformHandle platform_channel = |
| 214 embedder::PlatformChannelPair::PassClientHandleFromParentProcess( |
| 215 *base::CommandLine::ForCurrentProcess()); |
| 216 if (!platform_channel.is_valid()) |
| 217 return false; |
| 218 handle = embedder::CreateChannel(platform_channel.Pass(), |
| 219 base::Bind(&DidCreateChannel), |
| 220 base::ThreadTaskRunnerHandle::Get()); |
| 221 } |
| 226 | 222 |
| 227 Blocker blocker; | 223 Blocker blocker; |
| 228 controller_runner_->PostTask( | 224 controller_runner_->PostTask( |
| 229 FROM_HERE, | 225 FROM_HERE, |
| 230 base::Bind( | 226 base::Bind( |
| 231 &ChildControllerImpl::Create, base::Unretained(this), | 227 &ChildControllerImpl::Create, base::Unretained(this), |
| 232 base::Bind(&OnGotApplicationRequest, base::Unretained(request)), | 228 base::Bind(&OnGotApplicationRequest, base::Unretained(request)), |
| 233 base::Passed(&platform_channel), blocker.GetUnblocker())); | 229 base::Passed(&handle), blocker.GetUnblocker())); |
| 234 blocker.Block(); | 230 blocker.Block(); |
| 235 | 231 |
| 236 return true; | 232 return true; |
| 237 } | 233 } |
| 238 | 234 |
| 239 } // namespace | 235 } // namespace |
| 240 | 236 |
| 241 RunnerConnection::~RunnerConnection() {} | 237 RunnerConnection::~RunnerConnection() {} |
| 242 | 238 |
| 243 // static | 239 // static |
| 244 RunnerConnection* RunnerConnection::ConnectToRunner( | 240 RunnerConnection* RunnerConnection::ConnectToRunner( |
| 245 InterfaceRequest<Application>* request) { | 241 InterfaceRequest<Application>* request, |
| 242 ScopedMessagePipeHandle handle) { |
| 246 RunnerConnectionImpl* connection = new RunnerConnectionImpl; | 243 RunnerConnectionImpl* connection = new RunnerConnectionImpl; |
| 247 if (!connection->WaitForApplicationRequest(request)) { | 244 if (!connection->WaitForApplicationRequest(request, handle.Pass())) { |
| 248 delete connection; | 245 delete connection; |
| 249 return nullptr; | 246 return nullptr; |
| 250 } | 247 } |
| 251 return connection; | 248 return connection; |
| 252 } | 249 } |
| 253 | 250 |
| 254 RunnerConnection::RunnerConnection() {} | 251 RunnerConnection::RunnerConnection() {} |
| 255 | 252 |
| 256 } // namespace runner | 253 } // namespace runner |
| 257 } // namespace mojo | 254 } // namespace mojo |
| OLD | NEW |