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 <utility> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/callback.h" | 10 #include "base/callback.h" |
9 #include "base/command_line.h" | 11 #include "base/command_line.h" |
10 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
12 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
13 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
14 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
15 #include "mojo/edk/embedder/embedder.h" | 17 #include "mojo/edk/embedder/embedder.h" |
16 #include "mojo/edk/embedder/platform_channel_pair.h" | 18 #include "mojo/edk/embedder/platform_channel_pair.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 base::Closure run_after_; | 69 base::Closure run_after_; |
68 | 70 |
69 DISALLOW_COPY_AND_ASSIGN(Blocker); | 71 DISALLOW_COPY_AND_ASSIGN(Blocker); |
70 }; | 72 }; |
71 | 73 |
72 using GotApplicationRequestCallback = | 74 using GotApplicationRequestCallback = |
73 base::Callback<void(InterfaceRequest<Application>)>; | 75 base::Callback<void(InterfaceRequest<Application>)>; |
74 | 76 |
75 void OnGotApplicationRequest(InterfaceRequest<Application>* out_request, | 77 void OnGotApplicationRequest(InterfaceRequest<Application>* out_request, |
76 InterfaceRequest<Application> request) { | 78 InterfaceRequest<Application> request) { |
77 *out_request = request.Pass(); | 79 *out_request = std::move(request); |
78 } | 80 } |
79 | 81 |
80 class ChildControllerImpl; | 82 class ChildControllerImpl; |
81 | 83 |
82 class RunnerConnectionImpl : public RunnerConnection { | 84 class RunnerConnectionImpl : public RunnerConnection { |
83 public: | 85 public: |
84 RunnerConnectionImpl() : controller_thread_("controller_thread") { | 86 RunnerConnectionImpl() : controller_thread_("controller_thread") { |
85 StartControllerThread(); | 87 StartControllerThread(); |
86 } | 88 } |
87 ~RunnerConnectionImpl() override { | 89 ~RunnerConnectionImpl() override { |
88 controller_runner_->PostTask( | 90 controller_runner_->PostTask( |
89 FROM_HERE, base::Bind(&RunnerConnectionImpl::ShutdownOnControllerThread, | 91 FROM_HERE, base::Bind(&RunnerConnectionImpl::ShutdownOnControllerThread, |
90 base::Unretained(this))); | 92 base::Unretained(this))); |
91 controller_thread_.Stop(); | 93 controller_thread_.Stop(); |
92 } | 94 } |
93 | 95 |
94 // Returns true if a connection to the runner has been established and | 96 // Returns true if a connection to the runner has been established and |
95 // |request| has been modified, false if no connection was established. | 97 // |request| has been modified, false if no connection was established. |
96 bool WaitForApplicationRequest(InterfaceRequest<Application>* request, | 98 bool WaitForApplicationRequest(InterfaceRequest<Application>* request, |
97 ScopedMessagePipeHandle handle); | 99 ScopedMessagePipeHandle handle); |
98 | 100 |
99 ChildControllerImpl* controller() const { return controller_.get(); } | 101 ChildControllerImpl* controller() const { return controller_.get(); } |
100 | 102 |
101 void set_controller(scoped_ptr<ChildControllerImpl> controller) { | 103 void set_controller(scoped_ptr<ChildControllerImpl> controller) { |
102 controller_ = controller.Pass(); | 104 controller_ = std::move(controller); |
103 } | 105 } |
104 | 106 |
105 private: | 107 private: |
106 void StartControllerThread() { | 108 void StartControllerThread() { |
107 base::Thread::Options controller_thread_options; | 109 base::Thread::Options controller_thread_options; |
108 controller_thread_options.message_loop_type = | 110 controller_thread_options.message_loop_type = |
109 base::MessageLoop::TYPE_CUSTOM; | 111 base::MessageLoop::TYPE_CUSTOM; |
110 controller_thread_options.message_pump_factory = | 112 controller_thread_options.message_pump_factory = |
111 base::Bind(&common::MessagePumpMojo::Create); | 113 base::Bind(&common::MessagePumpMojo::Create); |
112 CHECK(controller_thread_.StartWithOptions(controller_thread_options)); | 114 CHECK(controller_thread_.StartWithOptions(controller_thread_options)); |
(...skipping 26 matching lines...) Expand all Loading... |
139 static void Create(RunnerConnectionImpl* connection, | 141 static void Create(RunnerConnectionImpl* connection, |
140 const GotApplicationRequestCallback& callback, | 142 const GotApplicationRequestCallback& callback, |
141 ScopedMessagePipeHandle runner_handle, | 143 ScopedMessagePipeHandle runner_handle, |
142 const Blocker::Unblocker& unblocker) { | 144 const Blocker::Unblocker& unblocker) { |
143 DCHECK(connection); | 145 DCHECK(connection); |
144 DCHECK(!connection->controller()); | 146 DCHECK(!connection->controller()); |
145 | 147 |
146 scoped_ptr<ChildControllerImpl> impl( | 148 scoped_ptr<ChildControllerImpl> impl( |
147 new ChildControllerImpl(connection, callback, unblocker)); | 149 new ChildControllerImpl(connection, callback, unblocker)); |
148 | 150 |
149 impl->Bind(runner_handle.Pass()); | 151 impl->Bind(std::move(runner_handle)); |
150 | 152 |
151 connection->set_controller(impl.Pass()); | 153 connection->set_controller(std::move(impl)); |
152 } | 154 } |
153 | 155 |
154 void Bind(ScopedMessagePipeHandle handle) { | 156 void Bind(ScopedMessagePipeHandle handle) { |
155 binding_.Bind(handle.Pass()); | 157 binding_.Bind(std::move(handle)); |
156 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); | 158 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); |
157 } | 159 } |
158 | 160 |
159 void OnConnectionError() { | 161 void OnConnectionError() { |
160 // A connection error means the connection to the shell is lost. This is not | 162 // A connection error means the connection to the shell is lost. This is not |
161 // recoverable. | 163 // recoverable. |
162 LOG(ERROR) << "Connection error to the shell."; | 164 LOG(ERROR) << "Connection error to the shell."; |
163 _exit(1); | 165 _exit(1); |
164 } | 166 } |
165 | 167 |
(...skipping 19 matching lines...) Expand all Loading... |
185 const Blocker::Unblocker& unblocker) | 187 const Blocker::Unblocker& unblocker) |
186 : connection_(connection), | 188 : connection_(connection), |
187 callback_(callback), | 189 callback_(callback), |
188 unblocker_(unblocker), | 190 unblocker_(unblocker), |
189 channel_info_(nullptr), | 191 channel_info_(nullptr), |
190 binding_(this) {} | 192 binding_(this) {} |
191 | 193 |
192 static void ReturnApplicationRequestOnMainThread( | 194 static void ReturnApplicationRequestOnMainThread( |
193 const GotApplicationRequestCallback& callback, | 195 const GotApplicationRequestCallback& callback, |
194 InterfaceRequest<Application> application_request) { | 196 InterfaceRequest<Application> application_request) { |
195 callback.Run(application_request.Pass()); | 197 callback.Run(std::move(application_request)); |
196 } | 198 } |
197 | 199 |
198 base::ThreadChecker thread_checker_; | 200 base::ThreadChecker thread_checker_; |
199 RunnerConnectionImpl* const connection_; | 201 RunnerConnectionImpl* const connection_; |
200 GotApplicationRequestCallback callback_; | 202 GotApplicationRequestCallback callback_; |
201 Blocker::Unblocker unblocker_; | 203 Blocker::Unblocker unblocker_; |
202 StartAppCallback on_app_complete_; | 204 StartAppCallback on_app_complete_; |
203 | 205 |
204 embedder::ChannelInfo* channel_info_; | 206 embedder::ChannelInfo* channel_info_; |
205 Binding<ChildController> binding_; | 207 Binding<ChildController> binding_; |
206 | 208 |
207 DISALLOW_COPY_AND_ASSIGN(ChildControllerImpl); | 209 DISALLOW_COPY_AND_ASSIGN(ChildControllerImpl); |
208 }; | 210 }; |
209 | 211 |
210 bool RunnerConnectionImpl::WaitForApplicationRequest( | 212 bool RunnerConnectionImpl::WaitForApplicationRequest( |
211 InterfaceRequest<Application>* request, | 213 InterfaceRequest<Application>* request, |
212 ScopedMessagePipeHandle handle) { | 214 ScopedMessagePipeHandle handle) { |
213 // If a valid message pipe to the runner was not provided, look for one on the | 215 // If a valid message pipe to the runner was not provided, look for one on the |
214 // command line. | 216 // command line. |
215 if (!handle.is_valid()) { | 217 if (!handle.is_valid()) { |
216 embedder::ScopedPlatformHandle platform_channel = | 218 embedder::ScopedPlatformHandle platform_channel = |
217 embedder::PlatformChannelPair::PassClientHandleFromParentProcess( | 219 embedder::PlatformChannelPair::PassClientHandleFromParentProcess( |
218 *base::CommandLine::ForCurrentProcess()); | 220 *base::CommandLine::ForCurrentProcess()); |
219 if (!platform_channel.is_valid()) | 221 if (!platform_channel.is_valid()) |
220 return false; | 222 return false; |
221 scoped_refptr<base::TaskRunner> task_runner; | 223 scoped_refptr<base::TaskRunner> task_runner; |
222 if (!base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) | 224 if (!base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) |
223 task_runner = base::ThreadTaskRunnerHandle::Get(); | 225 task_runner = base::ThreadTaskRunnerHandle::Get(); |
224 handle = embedder::CreateChannel(platform_channel.Pass(), | 226 handle = |
225 base::Bind(&DidCreateChannel), | 227 embedder::CreateChannel(std::move(platform_channel), |
226 task_runner); | 228 base::Bind(&DidCreateChannel), task_runner); |
227 // Copy of code in child_process.cc | 229 // Copy of code in child_process.cc |
228 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) { | 230 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) { |
229 // When using the new Mojo EDK, each message pipe is backed by a platform | 231 // When using the new Mojo EDK, each message pipe is backed by a platform |
230 // handle. The one platform handle that comes on the command line is used | 232 // handle. The one platform handle that comes on the command line is used |
231 // to bind to the ChildController interface. However we also want a | 233 // to bind to the ChildController interface. However we also want a |
232 // platform handle to setup the communication channel by which we exchange | 234 // platform handle to setup the communication channel by which we exchange |
233 // handles to/from tokens, which is needed for sandboxed Windows | 235 // handles to/from tokens, which is needed for sandboxed Windows |
234 // processes. | 236 // processes. |
235 char broker_handle[10]; | 237 char broker_handle[10]; |
236 MojoHandleSignalsState state; | 238 MojoHandleSignalsState state; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 | 270 |
269 } // namespace | 271 } // namespace |
270 | 272 |
271 RunnerConnection::~RunnerConnection() {} | 273 RunnerConnection::~RunnerConnection() {} |
272 | 274 |
273 // static | 275 // static |
274 RunnerConnection* RunnerConnection::ConnectToRunner( | 276 RunnerConnection* RunnerConnection::ConnectToRunner( |
275 InterfaceRequest<Application>* request, | 277 InterfaceRequest<Application>* request, |
276 ScopedMessagePipeHandle handle) { | 278 ScopedMessagePipeHandle handle) { |
277 RunnerConnectionImpl* connection = new RunnerConnectionImpl; | 279 RunnerConnectionImpl* connection = new RunnerConnectionImpl; |
278 if (!connection->WaitForApplicationRequest(request, handle.Pass())) { | 280 if (!connection->WaitForApplicationRequest(request, std::move(handle))) { |
279 delete connection; | 281 delete connection; |
280 return nullptr; | 282 return nullptr; |
281 } | 283 } |
282 return connection; | 284 return connection; |
283 } | 285 } |
284 | 286 |
285 RunnerConnection::RunnerConnection() {} | 287 RunnerConnection::RunnerConnection() {} |
286 | 288 |
287 } // namespace runner | 289 } // namespace runner |
288 } // namespace mojo | 290 } // namespace mojo |
OLD | NEW |