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 "ipc/ipc_mojo_bootstrap.h" | 5 #include "ipc/ipc_mojo_bootstrap.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | |
9 #include <map> | |
10 #include <memory> | |
8 #include <utility> | 11 #include <utility> |
9 | 12 |
10 #include "base/callback.h" | 13 #include "base/callback.h" |
11 #include "base/logging.h" | 14 #include "base/logging.h" |
12 #include "base/macros.h" | 15 #include "base/macros.h" |
13 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
14 #include "base/process/process_handle.h" | 17 #include "base/process/process_handle.h" |
18 #include "base/single_thread_task_runner.h" | |
19 #include "base/stl_util.h" | |
20 #include "base/synchronization/lock.h" | |
21 #include "base/threading/thread_task_runner_handle.h" | |
15 #include "build/build_config.h" | 22 #include "build/build_config.h" |
16 #include "ipc/ipc_message_utils.h" | 23 #include "ipc/ipc_message_utils.h" |
17 #include "ipc/ipc_platform_file.h" | 24 #include "ipc/ipc_platform_file.h" |
25 #include "mojo/public/cpp/bindings/associated_group.h" | |
26 #include "mojo/public/cpp/bindings/associated_group_controller.h" | |
18 #include "mojo/public/cpp/bindings/binding.h" | 27 #include "mojo/public/cpp/bindings/binding.h" |
28 #include "mojo/public/cpp/bindings/connector.h" | |
29 #include "mojo/public/cpp/bindings/interface_endpoint_client.h" | |
30 #include "mojo/public/cpp/bindings/interface_endpoint_controller.h" | |
31 #include "mojo/public/cpp/bindings/interface_id.h" | |
32 #include "mojo/public/cpp/bindings/message_header_validator.h" | |
33 #include "mojo/public/cpp/bindings/pipe_control_message_handler.h" | |
34 #include "mojo/public/cpp/bindings/pipe_control_message_handler_delegate.h" | |
35 #include "mojo/public/cpp/bindings/pipe_control_message_proxy.h" | |
19 | 36 |
20 namespace IPC { | 37 namespace IPC { |
21 | 38 |
22 namespace { | 39 namespace { |
23 | 40 |
41 class ChannelAssociatedGroupController | |
42 : public mojo::AssociatedGroupController, | |
43 public mojo::MessageReceiver, | |
44 public mojo::PipeControlMessageHandlerDelegate { | |
45 public: | |
46 ChannelAssociatedGroupController(bool for_proxy, | |
47 mojo::ScopedMessagePipeHandle handle) | |
48 : mojo::AssociatedGroupController(base::ThreadTaskRunnerHandle::Get()), | |
49 task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
50 id_namespace_mask_(for_proxy ? mojo::kInterfaceIdNamespaceMask : 0), | |
51 associated_group_(CreateAssociatedGroup()), | |
52 connector_(std::move(handle), mojo::Connector::SINGLE_THREADED_SEND, | |
53 base::ThreadTaskRunnerHandle::Get()), | |
54 header_validator_( | |
55 "IPC::mojom::Bootstrap [master] MessageHeaderValidator", this), | |
56 control_message_handler_(this), | |
57 control_message_proxy_(&connector_) { | |
58 connector_.set_incoming_receiver(&header_validator_); | |
59 connector_.set_connection_error_handler( | |
60 base::Bind(&ChannelAssociatedGroupController::OnPipeError, | |
61 base::Unretained(this))); | |
62 control_message_handler_.SetDescription( | |
63 "IPC::mojom::Bootstrap [master] PipeControlMessageHandler"); | |
64 } | |
65 | |
66 mojo::AssociatedGroup* associated_group() { return associated_group_.get(); } | |
67 | |
68 void SetProxyTaskRunner( | |
69 scoped_refptr<base::SingleThreadTaskRunner> proxy_task_runner) { | |
70 proxy_task_runner_ = proxy_task_runner; | |
71 } | |
72 | |
73 // mojo::AssociatedGroupController: | |
74 void CreateEndpointHandlePair( | |
75 mojo::ScopedInterfaceEndpointHandle* local_endpoint, | |
76 mojo::ScopedInterfaceEndpointHandle* remote_endpoint) override { | |
77 base::AutoLock locker(lock_); | |
78 uint32_t id = 0; | |
79 do { | |
80 if (next_interface_id_ >= mojo::kInterfaceIdNamespaceMask) | |
81 next_interface_id_ = 1; | |
82 id = (next_interface_id_++) | id_namespace_mask_; | |
83 } while (ContainsKey(endpoints_, id)); | |
84 | |
85 Endpoint* endpoint = new Endpoint(this, id); | |
86 if (encountered_error_) | |
87 endpoint->set_peer_closed(); | |
88 endpoints_.insert({ id, endpoint }); | |
89 | |
90 *local_endpoint = CreateScopedInterfaceEndpointHandle(id, true); | |
91 *remote_endpoint = CreateScopedInterfaceEndpointHandle(id, false); | |
92 } | |
93 | |
94 mojo::ScopedInterfaceEndpointHandle CreateLocalEndpointHandle( | |
95 mojo::InterfaceId id) override { | |
96 if (!mojo::IsValidInterfaceId(id)) | |
97 return mojo::ScopedInterfaceEndpointHandle(); | |
98 | |
99 base::AutoLock locker(lock_); | |
100 bool inserted = false; | |
101 Endpoint* endpoint = FindOrInsertEndpoint(id, &inserted); | |
102 if (inserted && encountered_error_) | |
103 endpoint->set_peer_closed(); | |
104 | |
105 return CreateScopedInterfaceEndpointHandle(id, true); | |
106 } | |
107 | |
108 void CloseEndpointHandle(mojo::InterfaceId id, bool is_local) override { | |
109 if (!mojo::IsValidInterfaceId(id)) | |
110 return; | |
111 | |
112 base::AutoLock locker(lock_); | |
113 if (!is_local) { | |
114 DCHECK(ContainsKey(endpoints_, id)); | |
115 DCHECK(!mojo::IsMasterInterfaceId(id)); | |
116 control_message_proxy_.NotifyEndpointClosedBeforeSent(id); | |
117 return; | |
118 } | |
119 | |
120 DCHECK(ContainsKey(endpoints_, id)); | |
121 Endpoint* endpoint = endpoints_[id].get(); | |
122 DCHECK(!endpoint->client()); | |
123 DCHECK(!endpoint->closed()); | |
124 MarkClosedAndMaybeRemove(endpoint); | |
125 | |
126 if (!mojo::IsMasterInterfaceId(id)) | |
127 control_message_proxy_.NotifyPeerEndpointClosed(id); | |
128 } | |
129 | |
130 mojo::InterfaceEndpointController* AttachEndpointClient( | |
131 const mojo::ScopedInterfaceEndpointHandle& handle, | |
132 mojo::InterfaceEndpointClient* client, | |
133 scoped_refptr<base::SingleThreadTaskRunner> runner) override { | |
134 const mojo::InterfaceId id = handle.id(); | |
135 | |
136 DCHECK(mojo::IsValidInterfaceId(id)); | |
137 DCHECK(client); | |
138 | |
139 base::AutoLock locker(lock_); | |
140 DCHECK(ContainsKey(endpoints_, id)); | |
141 | |
142 Endpoint* endpoint = endpoints_[id].get(); | |
143 endpoint->AttachClient(client, std::move(runner)); | |
144 | |
145 if (endpoint->peer_closed()) | |
146 NotifyEndpointOfError(endpoint, true /* force_async */); | |
147 | |
148 return endpoint; | |
149 } | |
150 | |
151 void DetachEndpointClient( | |
152 const mojo::ScopedInterfaceEndpointHandle& handle) override { | |
153 const mojo::InterfaceId id = handle.id(); | |
154 | |
155 DCHECK(mojo::IsValidInterfaceId(id)); | |
156 | |
157 base::AutoLock locker(lock_); | |
158 DCHECK(ContainsKey(endpoints_, id)); | |
159 | |
160 Endpoint* endpoint = endpoints_[id].get(); | |
161 endpoint->DetachClient(); | |
162 } | |
163 | |
164 void RaiseError() override { | |
165 if (task_runner_->BelongsToCurrentThread()) { | |
166 connector_.RaiseError(); | |
167 } else { | |
168 task_runner_->PostTask( | |
169 FROM_HERE, | |
170 base::Bind(&ChannelAssociatedGroupController::RaiseError, this)); | |
171 } | |
172 } | |
173 | |
174 private: | |
175 class Endpoint; | |
176 friend class Endpoint; | |
177 | |
178 class Endpoint : public base::RefCountedThreadSafe<Endpoint>, | |
179 public mojo::InterfaceEndpointController { | |
180 public: | |
181 Endpoint(ChannelAssociatedGroupController* controller, mojo::InterfaceId id) | |
182 : controller_(controller), id_(id) {} | |
183 | |
184 mojo::InterfaceId id() const { return id_; } | |
185 | |
186 bool closed() const { | |
187 controller_->lock_.AssertAcquired(); | |
188 return closed_; | |
189 } | |
190 | |
191 void set_closed() { | |
192 controller_->lock_.AssertAcquired(); | |
193 closed_ = true; | |
194 } | |
195 | |
196 bool peer_closed() const { | |
197 controller_->lock_.AssertAcquired(); | |
198 return peer_closed_; | |
199 } | |
200 | |
201 void set_peer_closed() { | |
202 controller_->lock_.AssertAcquired(); | |
203 peer_closed_ = true; | |
204 } | |
205 | |
206 base::SingleThreadTaskRunner* task_runner() const { | |
207 return task_runner_.get(); | |
208 } | |
209 | |
210 mojo::InterfaceEndpointClient* client() const { | |
211 controller_->lock_.AssertAcquired(); | |
212 return client_; | |
213 } | |
214 | |
215 void AttachClient(mojo::InterfaceEndpointClient* client, | |
216 scoped_refptr<base::SingleThreadTaskRunner> runner) { | |
217 controller_->lock_.AssertAcquired(); | |
218 DCHECK(!client_); | |
219 DCHECK(!closed_); | |
220 DCHECK(runner->BelongsToCurrentThread()); | |
221 | |
222 task_runner_ = std::move(runner); | |
223 client_ = client; | |
224 } | |
225 | |
226 void DetachClient() { | |
227 controller_->lock_.AssertAcquired(); | |
228 DCHECK(client_); | |
229 DCHECK(task_runner_->BelongsToCurrentThread()); | |
230 DCHECK(!closed_); | |
231 | |
232 task_runner_ = nullptr; | |
233 client_ = nullptr; | |
234 } | |
235 | |
236 // mojo::InterfaceEndpointController: | |
237 bool SendMessage(mojo::Message* message) override { | |
238 DCHECK(task_runner_->BelongsToCurrentThread()); | |
239 message->set_interface_id(id_); | |
240 return controller_->SendMessage(message); | |
241 } | |
242 | |
243 void AllowWokenUpBySyncWatchOnSameThread() override { | |
244 DCHECK(task_runner_->BelongsToCurrentThread()); | |
245 | |
246 // It's not legal to make sync calls from the master endpoint's thread, | |
yzshen1
2016/07/13 16:46:45
This method is used for the implementation side of
Ken Rockot(use gerrit already)
2016/07/13 17:36:24
SGTM DCHECKs removed
| |
247 // and in fact they must only happen from the proxy task runner. | |
248 DCHECK(!controller_->task_runner_->BelongsToCurrentThread()); | |
249 DCHECK(controller_->proxy_task_runner_->BelongsToCurrentThread()); | |
250 | |
251 // TODO(rockot): Implement sync waiting. | |
252 NOTREACHED(); | |
253 } | |
254 | |
255 bool SyncWatch(const bool* should_stop) override { | |
256 DCHECK(task_runner_->BelongsToCurrentThread()); | |
257 | |
258 // It's not legal to make sync calls from the master endpoint's thread, | |
259 // and in fact they must only happen from the proxy task runner. | |
260 DCHECK(!controller_->task_runner_->BelongsToCurrentThread()); | |
261 DCHECK(controller_->proxy_task_runner_->BelongsToCurrentThread()); | |
262 | |
263 // TODO(rockot): Implement sync waiting. | |
264 NOTREACHED(); | |
265 return false; | |
266 } | |
267 | |
268 private: | |
269 friend class base::RefCountedThreadSafe<Endpoint>; | |
270 | |
271 ~Endpoint() override {} | |
272 | |
273 ChannelAssociatedGroupController* const controller_; | |
274 const mojo::InterfaceId id_; | |
275 | |
276 bool closed_ = false; | |
277 bool peer_closed_ = false; | |
278 mojo::InterfaceEndpointClient* client_ = nullptr; | |
279 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
280 | |
281 DISALLOW_COPY_AND_ASSIGN(Endpoint); | |
282 }; | |
283 | |
284 ~ChannelAssociatedGroupController() override {} | |
285 | |
286 bool SendMessage(mojo::Message* message) { | |
287 if (task_runner_->BelongsToCurrentThread()) { | |
288 DCHECK(thread_checker_.CalledOnValidThread()); | |
289 return connector_.Accept(message); | |
290 } else { | |
291 // We always post tasks to the master endpoint thread when called from the | |
292 // proxy thread in order to simulate IPC::ChannelProxy::Send behavior. | |
293 DCHECK(proxy_task_runner_ && | |
294 proxy_task_runner_->BelongsToCurrentThread()); | |
295 std::unique_ptr<mojo::Message> passed_message(new mojo::Message); | |
296 message->MoveTo(passed_message.get()); | |
297 task_runner_->PostTask( | |
298 FROM_HERE, | |
299 base::Bind( | |
300 &ChannelAssociatedGroupController::SendMessageOnMasterThread, | |
301 this, base::Passed(&passed_message))); | |
302 return true; | |
303 } | |
304 } | |
305 | |
306 void SendMessageOnMasterThread(std::unique_ptr<mojo::Message> message) { | |
307 DCHECK(thread_checker_.CalledOnValidThread()); | |
308 if (!SendMessage(message.get())) | |
309 RaiseError(); | |
310 } | |
311 | |
312 void OnPipeError() { | |
313 DCHECK(thread_checker_.CalledOnValidThread()); | |
314 | |
315 base::AutoLock locker(lock_); | |
316 encountered_error_ = true; | |
317 | |
318 std::vector<scoped_refptr<Endpoint>> endpoints_to_notify; | |
319 for (auto iter = endpoints_.begin(); iter != endpoints_.end();) { | |
320 Endpoint* endpoint = iter->second.get(); | |
321 ++iter; | |
322 | |
323 if (endpoint->client()) | |
324 endpoints_to_notify.push_back(endpoint); | |
325 | |
326 MarkPeerClosedAndMaybeRemove(endpoint); | |
327 } | |
328 | |
329 // We keep |this| alive here because it's possible for the notifications | |
330 // below to release all other references. | |
331 scoped_refptr<ChannelAssociatedGroupController> keepalive(this); | |
332 for (auto& endpoint : endpoints_to_notify) { | |
333 // Because an notification may in turn detach any endpoint, we have to | |
334 // check each client again here. | |
335 if (endpoint->client()) | |
336 NotifyEndpointOfError(endpoint.get(), false /* force_async */); | |
337 } | |
338 } | |
339 | |
340 void NotifyEndpointOfError(Endpoint* endpoint, bool force_async) { | |
341 lock_.AssertAcquired(); | |
342 DCHECK(endpoint->task_runner() && endpoint->client()); | |
343 if (endpoint->task_runner()->BelongsToCurrentThread() && !force_async) { | |
344 mojo::InterfaceEndpointClient* client = endpoint->client(); | |
345 | |
346 base::AutoUnlock unlocker(lock_); | |
347 client->NotifyError(); | |
348 } else { | |
349 endpoint->task_runner()->PostTask( | |
350 FROM_HERE, | |
351 base::Bind(&ChannelAssociatedGroupController | |
352 ::NotifyEndpointOfErrorOnEndpointThread, this, | |
353 make_scoped_refptr(endpoint))); | |
354 } | |
355 } | |
356 | |
357 void NotifyEndpointOfErrorOnEndpointThread(scoped_refptr<Endpoint> endpoint) { | |
358 base::AutoLock locker(lock_); | |
359 if (!endpoint->client()) | |
360 return; | |
361 DCHECK(endpoint->task_runner()->BelongsToCurrentThread()); | |
362 NotifyEndpointOfError(endpoint.get(), false /* force_async */); | |
363 } | |
364 | |
365 bool MarkClosedAndMaybeRemove(Endpoint* endpoint) { | |
366 lock_.AssertAcquired(); | |
367 endpoint->set_closed(); | |
368 if (endpoint->closed() && endpoint->peer_closed()) { | |
369 endpoints_.erase(endpoint->id()); | |
370 return true; | |
371 } | |
372 return false; | |
373 } | |
374 | |
375 bool MarkPeerClosedAndMaybeRemove(Endpoint* endpoint) { | |
376 lock_.AssertAcquired(); | |
377 endpoint->set_peer_closed(); | |
378 if (endpoint->closed() && endpoint->peer_closed()) { | |
379 endpoints_.erase(endpoint->id()); | |
380 return true; | |
381 } | |
382 return false; | |
383 } | |
384 | |
385 Endpoint* FindOrInsertEndpoint(mojo::InterfaceId id, bool* inserted) { | |
386 lock_.AssertAcquired(); | |
387 DCHECK(!inserted || !*inserted); | |
388 | |
389 auto iter = endpoints_.find(id); | |
390 if (iter != endpoints_.end()) | |
391 return iter->second.get(); | |
392 | |
393 Endpoint* endpoint = new Endpoint(this, id); | |
394 endpoints_.insert({ id, endpoint }); | |
395 *inserted = true; | |
396 return endpoint; | |
397 } | |
398 | |
399 // mojo::MessageReceiver: | |
400 bool Accept(mojo::Message* message) override { | |
401 DCHECK(thread_checker_.CalledOnValidThread()); | |
402 | |
403 if (mojo::PipeControlMessageHandler::IsPipeControlMessage(message)) { | |
404 if (!control_message_handler_.Accept(message)) | |
405 RaiseError(); | |
406 return true; | |
407 } | |
408 | |
409 mojo::InterfaceId id = message->interface_id(); | |
410 DCHECK(mojo::IsValidInterfaceId(id)); | |
411 | |
412 base::AutoLock locker(lock_); | |
413 bool inserted = false; | |
414 Endpoint* endpoint = FindOrInsertEndpoint(id, &inserted); | |
415 if (inserted) { | |
416 MarkClosedAndMaybeRemove(endpoint); | |
417 if (!mojo::IsMasterInterfaceId(id)) | |
418 control_message_proxy_.NotifyPeerEndpointClosed(id); | |
419 return true; | |
420 } | |
421 | |
422 if (endpoint->closed()) | |
423 return true; | |
424 | |
425 mojo::InterfaceEndpointClient* client = endpoint->client(); | |
426 if (!client || !endpoint->task_runner()->BelongsToCurrentThread()) { | |
427 // No client has been bound yet or the client runs tasks on another | |
428 // thread. We assume the other thread must always be the one on which | |
429 // |proxy_task_runner_| runs tasks, since that's the only valid scenario. | |
430 // | |
431 // If the client is not yet bound, it must be bound by the time this task | |
432 // runs or else it's programmer error. | |
433 DCHECK(proxy_task_runner_); | |
434 CHECK(false); | |
435 std::unique_ptr<mojo::Message> passed_message(new mojo::Message); | |
436 message->MoveTo(passed_message.get()); | |
437 proxy_task_runner_->PostTask( | |
438 FROM_HERE, | |
439 base::Bind(&ChannelAssociatedGroupController::AcceptOnProxyThread, | |
440 this, base::Passed(&passed_message))); | |
441 return true; | |
442 } | |
443 | |
444 // We do not expect to receive sync responses on the master endpoint thread. | |
445 // If it's happening, it's a bug. | |
446 DCHECK(!message->has_flag(mojo::Message::kFlagIsSync)); | |
447 | |
448 bool result = false; | |
449 { | |
450 base::AutoUnlock unlocker(lock_); | |
451 result = client->HandleIncomingMessage(message); | |
452 } | |
453 | |
454 if (!result) | |
455 RaiseError(); | |
456 | |
457 return true; | |
458 } | |
459 | |
460 void AcceptOnProxyThread(std::unique_ptr<mojo::Message> message) { | |
461 DCHECK(proxy_task_runner_->BelongsToCurrentThread()); | |
462 | |
463 // TODO(rockot): Implement this. | |
464 NOTREACHED(); | |
465 } | |
466 | |
467 // mojo::PipeControlMessageHandlerDelegate: | |
468 bool OnPeerAssociatedEndpointClosed(mojo::InterfaceId id) override { | |
469 DCHECK(thread_checker_.CalledOnValidThread()); | |
470 | |
471 if (mojo::IsMasterInterfaceId(id)) | |
472 return false; | |
473 | |
474 base::AutoLock locker(lock_); | |
475 Endpoint* endpoint = FindOrInsertEndpoint(id, nullptr); | |
476 if (!endpoint->peer_closed()) { | |
477 if (endpoint->client()) | |
478 NotifyEndpointOfError(endpoint, false /* force_async */); | |
479 MarkPeerClosedAndMaybeRemove(endpoint); | |
480 } | |
481 | |
482 return true; | |
483 } | |
484 | |
485 bool OnAssociatedEndpointClosedBeforeSent(mojo::InterfaceId id) override { | |
486 DCHECK(thread_checker_.CalledOnValidThread()); | |
487 | |
488 if (mojo::IsMasterInterfaceId(id)) | |
489 return false; | |
490 | |
491 Endpoint* endpoint = FindOrInsertEndpoint(id, nullptr); | |
492 DCHECK(!endpoint->closed()); | |
493 MarkClosedAndMaybeRemove(endpoint); | |
494 control_message_proxy_.NotifyPeerEndpointClosed(id); | |
495 return true; | |
496 } | |
497 | |
498 // Checked in places which must be run on the master endpoint's thread. | |
499 base::ThreadChecker thread_checker_; | |
500 | |
501 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
502 scoped_refptr<base::SingleThreadTaskRunner> proxy_task_runner_; | |
503 const uint32_t id_namespace_mask_; | |
504 const std::unique_ptr<mojo::AssociatedGroup> associated_group_; | |
505 mojo::Connector connector_; | |
506 mojo::MessageHeaderValidator header_validator_; | |
507 mojo::PipeControlMessageHandler control_message_handler_; | |
508 | |
509 // Guards the fields below for thread-safe access. | |
510 base::Lock lock_; | |
511 | |
512 bool encountered_error_ = false; | |
513 uint32_t next_interface_id_ = 1; | |
514 std::map<uint32_t, scoped_refptr<Endpoint>> endpoints_; | |
515 mojo::PipeControlMessageProxy control_message_proxy_; | |
516 | |
517 DISALLOW_COPY_AND_ASSIGN(ChannelAssociatedGroupController); | |
518 }; | |
519 | |
520 class BootstrapMasterProxy { | |
yzshen1
2016/07/13 16:46:45
optional: does it make sense to move this into it
Ken Rockot(use gerrit already)
2016/07/13 17:36:24
I considered it, but this I like the idea of keepi
| |
521 public: | |
522 BootstrapMasterProxy() {} | |
523 ~BootstrapMasterProxy() {} | |
524 | |
525 void Bind(mojo::ScopedMessagePipeHandle handle) { | |
526 DCHECK(!controller_); | |
527 controller_ = new ChannelAssociatedGroupController(true, std::move(handle)); | |
528 endpoint_client_.reset(new mojo::InterfaceEndpointClient( | |
529 controller_->CreateLocalEndpointHandle(mojo::kMasterInterfaceId), | |
530 nullptr, | |
531 base::MakeUnique<typename mojom::Bootstrap::ResponseValidator_>(), | |
532 false, base::ThreadTaskRunnerHandle::Get())); | |
533 proxy_.reset(new mojom::BootstrapProxy(endpoint_client_.get())); | |
534 proxy_->serialization_context()->group_controller = controller_; | |
535 } | |
536 | |
537 void set_connection_error_handler(const base::Closure& handler) { | |
538 DCHECK(endpoint_client_); | |
539 endpoint_client_->set_connection_error_handler(handler); | |
540 } | |
541 | |
542 mojo::AssociatedGroup* associated_group() { | |
543 DCHECK(controller_); | |
544 return controller_->associated_group(); | |
545 } | |
546 | |
547 mojom::Bootstrap* operator->() { | |
548 DCHECK(proxy_); | |
549 return proxy_.get(); | |
550 } | |
551 | |
552 private: | |
553 std::unique_ptr<mojom::BootstrapProxy> proxy_; | |
554 scoped_refptr<ChannelAssociatedGroupController> controller_; | |
555 std::unique_ptr<mojo::InterfaceEndpointClient> endpoint_client_; | |
556 | |
557 DISALLOW_COPY_AND_ASSIGN(BootstrapMasterProxy); | |
558 }; | |
559 | |
560 class BootstrapMasterBinding { | |
561 public: | |
562 explicit BootstrapMasterBinding(mojom::Bootstrap* impl) { | |
563 stub_.set_sink(impl); | |
564 } | |
565 | |
566 ~BootstrapMasterBinding() {} | |
567 | |
568 void set_connection_error_handler(const base::Closure& handler) { | |
569 DCHECK(endpoint_client_); | |
570 endpoint_client_->set_connection_error_handler(handler); | |
571 } | |
572 | |
573 void Bind(mojo::ScopedMessagePipeHandle handle) { | |
574 DCHECK(!controller_); | |
575 controller_ = | |
576 new ChannelAssociatedGroupController(false, std::move(handle)); | |
577 stub_.serialization_context()->group_controller = controller_; | |
578 | |
579 endpoint_client_.reset(new mojo::InterfaceEndpointClient( | |
580 controller_->CreateLocalEndpointHandle(mojo::kMasterInterfaceId), | |
581 &stub_, | |
582 base::MakeUnique<typename mojom::Bootstrap::RequestValidator_>(), | |
583 false, base::ThreadTaskRunnerHandle::Get())); | |
584 } | |
585 | |
586 private: | |
587 mojom::BootstrapStub stub_; | |
588 scoped_refptr<ChannelAssociatedGroupController> controller_; | |
589 std::unique_ptr<mojo::InterfaceEndpointClient> endpoint_client_; | |
590 | |
591 DISALLOW_COPY_AND_ASSIGN(BootstrapMasterBinding); | |
592 }; | |
593 | |
24 // MojoBootstrap for the server process. You should create the instance | 594 // MojoBootstrap for the server process. You should create the instance |
25 // using MojoBootstrap::Create(). | 595 // using MojoBootstrap::Create(). |
26 class MojoServerBootstrap : public MojoBootstrap { | 596 class MojoServerBootstrap : public MojoBootstrap { |
27 public: | 597 public: |
28 MojoServerBootstrap(); | 598 MojoServerBootstrap(); |
29 | 599 |
30 private: | 600 private: |
31 // MojoBootstrap implementation. | 601 // MojoBootstrap implementation. |
32 void Connect() override; | 602 void Connect() override; |
33 | 603 |
34 void OnInitDone(int32_t peer_pid); | 604 void OnInitDone(int32_t peer_pid); |
35 | 605 |
36 mojom::BootstrapPtr bootstrap_; | 606 BootstrapMasterProxy bootstrap_; |
37 IPC::mojom::ChannelAssociatedPtrInfo send_channel_; | 607 IPC::mojom::ChannelAssociatedPtrInfo send_channel_; |
38 IPC::mojom::ChannelAssociatedRequest receive_channel_request_; | 608 IPC::mojom::ChannelAssociatedRequest receive_channel_request_; |
39 | 609 |
40 DISALLOW_COPY_AND_ASSIGN(MojoServerBootstrap); | 610 DISALLOW_COPY_AND_ASSIGN(MojoServerBootstrap); |
41 }; | 611 }; |
42 | 612 |
43 MojoServerBootstrap::MojoServerBootstrap() = default; | 613 MojoServerBootstrap::MojoServerBootstrap() = default; |
44 | 614 |
45 void MojoServerBootstrap::Connect() { | 615 void MojoServerBootstrap::Connect() { |
46 DCHECK_EQ(state(), STATE_INITIALIZED); | 616 DCHECK_EQ(state(), STATE_INITIALIZED); |
47 | 617 |
48 bootstrap_.Bind(mojom::BootstrapPtrInfo(TakeHandle(), 0)); | 618 bootstrap_.Bind(TakeHandle()); |
49 bootstrap_.set_connection_error_handler( | 619 bootstrap_.set_connection_error_handler( |
50 base::Bind(&MojoServerBootstrap::Fail, base::Unretained(this))); | 620 base::Bind(&MojoServerBootstrap::Fail, base::Unretained(this))); |
51 | 621 |
52 IPC::mojom::ChannelAssociatedRequest send_channel_request; | 622 IPC::mojom::ChannelAssociatedRequest send_channel_request; |
53 IPC::mojom::ChannelAssociatedPtrInfo receive_channel; | 623 IPC::mojom::ChannelAssociatedPtrInfo receive_channel; |
54 | 624 |
55 bootstrap_.associated_group()->CreateAssociatedInterface( | 625 bootstrap_.associated_group()->CreateAssociatedInterface( |
56 mojo::AssociatedGroup::WILL_PASS_REQUEST, &send_channel_, | 626 mojo::AssociatedGroup::WILL_PASS_REQUEST, &send_channel_, |
57 &send_channel_request); | 627 &send_channel_request); |
58 bootstrap_.associated_group()->CreateAssociatedInterface( | 628 bootstrap_.associated_group()->CreateAssociatedInterface( |
(...skipping 30 matching lines...) Expand all Loading... | |
89 private: | 659 private: |
90 // MojoBootstrap implementation. | 660 // MojoBootstrap implementation. |
91 void Connect() override; | 661 void Connect() override; |
92 | 662 |
93 // mojom::Bootstrap implementation. | 663 // mojom::Bootstrap implementation. |
94 void Init(mojom::ChannelAssociatedRequest receive_channel, | 664 void Init(mojom::ChannelAssociatedRequest receive_channel, |
95 mojom::ChannelAssociatedPtrInfo send_channel, | 665 mojom::ChannelAssociatedPtrInfo send_channel, |
96 int32_t peer_pid, | 666 int32_t peer_pid, |
97 const InitCallback& callback) override; | 667 const InitCallback& callback) override; |
98 | 668 |
99 mojo::Binding<mojom::Bootstrap> binding_; | 669 BootstrapMasterBinding binding_; |
100 | 670 |
101 DISALLOW_COPY_AND_ASSIGN(MojoClientBootstrap); | 671 DISALLOW_COPY_AND_ASSIGN(MojoClientBootstrap); |
102 }; | 672 }; |
103 | 673 |
104 MojoClientBootstrap::MojoClientBootstrap() : binding_(this) {} | 674 MojoClientBootstrap::MojoClientBootstrap() : binding_(this) {} |
105 | 675 |
106 void MojoClientBootstrap::Connect() { | 676 void MojoClientBootstrap::Connect() { |
107 binding_.Bind(TakeHandle()); | 677 binding_.Bind(TakeHandle()); |
108 binding_.set_connection_error_handler( | 678 binding_.set_connection_error_handler( |
109 base::Bind(&MojoClientBootstrap::Fail, base::Unretained(this))); | 679 base::Bind(&MojoClientBootstrap::Fail, base::Unretained(this))); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
169 | 739 |
170 bool MojoBootstrap::HasFailed() const { | 740 bool MojoBootstrap::HasFailed() const { |
171 return state() == STATE_ERROR; | 741 return state() == STATE_ERROR; |
172 } | 742 } |
173 | 743 |
174 mojo::ScopedMessagePipeHandle MojoBootstrap::TakeHandle() { | 744 mojo::ScopedMessagePipeHandle MojoBootstrap::TakeHandle() { |
175 return std::move(handle_); | 745 return std::move(handle_); |
176 } | 746 } |
177 | 747 |
178 } // namespace IPC | 748 } // namespace IPC |
OLD | NEW |