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/public/cpp/bindings/lib/multiplex_router.h" | 5 #include "mojo/public/cpp/bindings/lib/multiplex_router.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/location.h" | 12 #include "base/location.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
18 #include "mojo/public/cpp/bindings/associated_group.h" | 18 #include "mojo/public/cpp/bindings/associated_group.h" |
19 #include "mojo/public/cpp/bindings/lib/interface_endpoint_client.h" | 19 #include "mojo/public/cpp/bindings/interface_endpoint_client.h" |
20 #include "mojo/public/cpp/bindings/lib/interface_endpoint_controller.h" | 20 #include "mojo/public/cpp/bindings/interface_endpoint_controller.h" |
21 #include "mojo/public/cpp/bindings/lib/sync_handle_watcher.h" | 21 #include "mojo/public/cpp/bindings/lib/sync_handle_watcher.h" |
22 | 22 |
23 namespace mojo { | 23 namespace mojo { |
24 namespace internal { | 24 namespace internal { |
25 | 25 |
26 // InterfaceEndpoint stores the information of an interface endpoint registered | 26 // InterfaceEndpoint stores the information of an interface endpoint registered |
27 // with the router. | 27 // with the router. |
28 // No one other than the router's |endpoints_| and |tasks_| should hold refs to | 28 // No one other than the router's |endpoints_| and |tasks_| should hold refs to |
29 // this object. | 29 // this object. |
30 class MultiplexRouter::InterfaceEndpoint | 30 class MultiplexRouter::InterfaceEndpoint |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 Type type; | 278 Type type; |
279 | 279 |
280 private: | 280 private: |
281 explicit Task(Type in_type) : type(in_type) {} | 281 explicit Task(Type in_type) : type(in_type) {} |
282 }; | 282 }; |
283 | 283 |
284 MultiplexRouter::MultiplexRouter( | 284 MultiplexRouter::MultiplexRouter( |
285 bool set_interface_id_namesapce_bit, | 285 bool set_interface_id_namesapce_bit, |
286 ScopedMessagePipeHandle message_pipe, | 286 ScopedMessagePipeHandle message_pipe, |
287 scoped_refptr<base::SingleThreadTaskRunner> runner) | 287 scoped_refptr<base::SingleThreadTaskRunner> runner) |
288 : RefCountedDeleteOnMessageLoop(base::ThreadTaskRunnerHandle::Get()), | 288 : AssociatedGroupController(base::ThreadTaskRunnerHandle::Get()), |
289 set_interface_id_namespace_bit_(set_interface_id_namesapce_bit), | 289 set_interface_id_namespace_bit_(set_interface_id_namesapce_bit), |
290 header_validator_(this), | 290 header_validator_(this), |
291 connector_(std::move(message_pipe), | 291 connector_(std::move(message_pipe), |
292 Connector::MULTI_THREADED_SEND, | 292 Connector::MULTI_THREADED_SEND, |
293 std::move(runner)), | 293 std::move(runner)), |
294 control_message_handler_(this), | 294 control_message_handler_(this), |
295 control_message_proxy_(&connector_), | 295 control_message_proxy_(&connector_), |
296 next_interface_id_value_(1), | 296 next_interface_id_value_(1), |
297 posted_to_process_tasks_(false), | 297 posted_to_process_tasks_(false), |
298 encountered_error_(false), | 298 encountered_error_(false), |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 id = next_interface_id_value_++; | 344 id = next_interface_id_value_++; |
345 if (set_interface_id_namespace_bit_) | 345 if (set_interface_id_namespace_bit_) |
346 id |= kInterfaceIdNamespaceMask; | 346 id |= kInterfaceIdNamespaceMask; |
347 } while (ContainsKey(endpoints_, id)); | 347 } while (ContainsKey(endpoints_, id)); |
348 | 348 |
349 InterfaceEndpoint* endpoint = new InterfaceEndpoint(this, id); | 349 InterfaceEndpoint* endpoint = new InterfaceEndpoint(this, id); |
350 endpoints_[id] = endpoint; | 350 endpoints_[id] = endpoint; |
351 if (encountered_error_) | 351 if (encountered_error_) |
352 UpdateEndpointStateMayRemove(endpoint, PEER_ENDPOINT_CLOSED); | 352 UpdateEndpointStateMayRemove(endpoint, PEER_ENDPOINT_CLOSED); |
353 | 353 |
354 *local_endpoint = ScopedInterfaceEndpointHandle(id, true, this); | 354 *local_endpoint = CreateScopedInterfaceEndpointHandle(id, true); |
355 *remote_endpoint = ScopedInterfaceEndpointHandle(id, false, this); | 355 *remote_endpoint = CreateScopedInterfaceEndpointHandle(id, false); |
356 } | 356 } |
357 | 357 |
358 ScopedInterfaceEndpointHandle MultiplexRouter::CreateLocalEndpointHandle( | 358 ScopedInterfaceEndpointHandle MultiplexRouter::CreateLocalEndpointHandle( |
359 InterfaceId id) { | 359 InterfaceId id) { |
360 if (!IsValidInterfaceId(id)) | 360 if (!IsValidInterfaceId(id)) |
361 return ScopedInterfaceEndpointHandle(); | 361 return ScopedInterfaceEndpointHandle(); |
362 | 362 |
363 base::AutoLock locker(lock_); | 363 base::AutoLock locker(lock_); |
364 bool inserted = false; | 364 bool inserted = false; |
365 InterfaceEndpoint* endpoint = FindOrInsertEndpoint(id, &inserted); | 365 InterfaceEndpoint* endpoint = FindOrInsertEndpoint(id, &inserted); |
366 if (inserted) { | 366 if (inserted) { |
367 if (encountered_error_) | 367 if (encountered_error_) |
368 UpdateEndpointStateMayRemove(endpoint, PEER_ENDPOINT_CLOSED); | 368 UpdateEndpointStateMayRemove(endpoint, PEER_ENDPOINT_CLOSED); |
369 } else { | 369 } else { |
370 // If the endpoint already exist, it is because we have received a | 370 // If the endpoint already exist, it is because we have received a |
371 // notification that the peer endpoint has closed. | 371 // notification that the peer endpoint has closed. |
372 CHECK(!endpoint->closed()); | 372 CHECK(!endpoint->closed()); |
373 CHECK(endpoint->peer_closed()); | 373 CHECK(endpoint->peer_closed()); |
374 } | 374 } |
375 return ScopedInterfaceEndpointHandle(id, true, this); | 375 return CreateScopedInterfaceEndpointHandle(id, true); |
376 } | 376 } |
377 | 377 |
378 void MultiplexRouter::CloseEndpointHandle(InterfaceId id, bool is_local) { | 378 void MultiplexRouter::CloseEndpointHandle(InterfaceId id, bool is_local) { |
379 if (!IsValidInterfaceId(id)) | 379 if (!IsValidInterfaceId(id)) |
380 return; | 380 return; |
381 | 381 |
382 base::AutoLock locker(lock_); | 382 base::AutoLock locker(lock_); |
383 | 383 |
384 if (!is_local) { | 384 if (!is_local) { |
385 DCHECK(ContainsKey(endpoints_, id)); | 385 DCHECK(ContainsKey(endpoints_, id)); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 | 440 |
441 void MultiplexRouter::RaiseError() { | 441 void MultiplexRouter::RaiseError() { |
442 if (task_runner_->BelongsToCurrentThread()) { | 442 if (task_runner_->BelongsToCurrentThread()) { |
443 connector_.RaiseError(); | 443 connector_.RaiseError(); |
444 } else { | 444 } else { |
445 task_runner_->PostTask(FROM_HERE, | 445 task_runner_->PostTask(FROM_HERE, |
446 base::Bind(&MultiplexRouter::RaiseError, this)); | 446 base::Bind(&MultiplexRouter::RaiseError, this)); |
447 } | 447 } |
448 } | 448 } |
449 | 449 |
450 std::unique_ptr<AssociatedGroup> MultiplexRouter::CreateAssociatedGroup() { | |
451 std::unique_ptr<AssociatedGroup> group(new AssociatedGroup); | |
452 group->router_ = this; | |
453 return group; | |
454 } | |
455 | |
456 // static | |
457 MultiplexRouter* MultiplexRouter::GetRouter(AssociatedGroup* associated_group) { | |
458 return associated_group->router_.get(); | |
459 } | |
460 | |
461 void MultiplexRouter::CloseMessagePipe() { | 450 void MultiplexRouter::CloseMessagePipe() { |
462 DCHECK(thread_checker_.CalledOnValidThread()); | 451 DCHECK(thread_checker_.CalledOnValidThread()); |
463 connector_.CloseMessagePipe(); | 452 connector_.CloseMessagePipe(); |
464 // CloseMessagePipe() above won't trigger connection error handler. | 453 // CloseMessagePipe() above won't trigger connection error handler. |
465 // Explicitly call OnPipeConnectionError() so that associated endpoints will | 454 // Explicitly call OnPipeConnectionError() so that associated endpoints will |
466 // get notified. | 455 // get notified. |
467 OnPipeConnectionError(); | 456 OnPipeConnectionError(); |
468 } | 457 } |
469 | 458 |
470 bool MultiplexRouter::HasAssociatedEndpoints() const { | 459 bool MultiplexRouter::HasAssociatedEndpoints() const { |
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
851 *inserted = true; | 840 *inserted = true; |
852 } else { | 841 } else { |
853 endpoint = iter->second.get(); | 842 endpoint = iter->second.get(); |
854 } | 843 } |
855 | 844 |
856 return endpoint; | 845 return endpoint; |
857 } | 846 } |
858 | 847 |
859 } // namespace internal | 848 } // namespace internal |
860 } // namespace mojo | 849 } // namespace mojo |
OLD | NEW |