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