tibell | ad0c0b3 | 2016-08-04 01:13:30 | [diff] [blame^] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "mojo/public/cpp/bindings/lib/binding_state.h" |
| 6 | |
| 7 | namespace mojo { |
| 8 | namespace internal { |
| 9 | |
| 10 | SimpleBindingState::SimpleBindingState() = default; |
| 11 | |
| 12 | SimpleBindingState::~SimpleBindingState() = default; |
| 13 | |
| 14 | void SimpleBindingState::PauseIncomingMethodCallProcessing() { |
| 15 | DCHECK(router_); |
| 16 | router_->PauseIncomingMethodCallProcessing(); |
| 17 | } |
| 18 | void SimpleBindingState::ResumeIncomingMethodCallProcessing() { |
| 19 | DCHECK(router_); |
| 20 | router_->ResumeIncomingMethodCallProcessing(); |
| 21 | } |
| 22 | |
| 23 | bool SimpleBindingState::WaitForIncomingMethodCall(MojoDeadline deadline) { |
| 24 | DCHECK(router_); |
| 25 | return router_->WaitForIncomingMessage(deadline); |
| 26 | } |
| 27 | |
| 28 | void SimpleBindingState::Close() { |
| 29 | if (!router_) |
| 30 | return; |
| 31 | |
| 32 | router_->CloseMessagePipe(); |
| 33 | DestroyRouter(); |
| 34 | } |
| 35 | |
| 36 | void SimpleBindingState::EnableTestingMode() { |
| 37 | DCHECK(is_bound()); |
| 38 | router_->EnableTestingMode(); |
| 39 | } |
| 40 | |
| 41 | void SimpleBindingState::BindInternal( |
| 42 | ScopedMessagePipeHandle handle, |
| 43 | scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 44 | const char* interface_name, |
| 45 | MessageFilter* request_validator, |
| 46 | bool has_sync_methods, |
| 47 | MessageReceiverWithResponderStatus* stub) { |
| 48 | internal::FilterChain filters; |
| 49 | filters.Append<MessageHeaderValidator>(interface_name); |
| 50 | filters.Append(request_validator); |
| 51 | |
| 52 | router_ = new internal::Router(std::move(handle), std::move(filters), |
| 53 | has_sync_methods, std::move(runner)); |
| 54 | router_->set_incoming_receiver(stub); |
| 55 | router_->set_connection_error_handler(base::Bind( |
| 56 | &SimpleBindingState::RunConnectionErrorHandler, base::Unretained(this))); |
| 57 | } |
| 58 | |
| 59 | void SimpleBindingState::DestroyRouter() { |
| 60 | router_->set_connection_error_handler(base::Closure()); |
| 61 | delete router_; |
| 62 | router_ = nullptr; |
| 63 | connection_error_handler_.Reset(); |
| 64 | } |
| 65 | |
| 66 | void SimpleBindingState::RunConnectionErrorHandler() { |
| 67 | if (!connection_error_handler_.is_null()) |
| 68 | connection_error_handler_.Run(); |
| 69 | } |
| 70 | |
| 71 | // ----------------------------------------------------------------------------- |
| 72 | |
| 73 | MultiplexedBindingState::MultiplexedBindingState() = default; |
| 74 | |
| 75 | MultiplexedBindingState::~MultiplexedBindingState() = default; |
| 76 | |
| 77 | bool MultiplexedBindingState::HasAssociatedInterfaces() const { |
| 78 | return router_ ? router_->HasAssociatedEndpoints() : false; |
| 79 | } |
| 80 | |
| 81 | void MultiplexedBindingState::PauseIncomingMethodCallProcessing() { |
| 82 | DCHECK(router_); |
| 83 | router_->PauseIncomingMethodCallProcessing(); |
| 84 | } |
| 85 | void MultiplexedBindingState::ResumeIncomingMethodCallProcessing() { |
| 86 | DCHECK(router_); |
| 87 | router_->ResumeIncomingMethodCallProcessing(); |
| 88 | } |
| 89 | |
| 90 | bool MultiplexedBindingState::WaitForIncomingMethodCall(MojoDeadline deadline) { |
| 91 | DCHECK(router_); |
| 92 | return router_->WaitForIncomingMessage(deadline); |
| 93 | } |
| 94 | |
| 95 | void MultiplexedBindingState::Close() { |
| 96 | if (!router_) |
| 97 | return; |
| 98 | |
| 99 | endpoint_client_.reset(); |
| 100 | router_->CloseMessagePipe(); |
| 101 | router_ = nullptr; |
| 102 | connection_error_handler_.Reset(); |
| 103 | } |
| 104 | |
| 105 | void MultiplexedBindingState::EnableTestingMode() { |
| 106 | DCHECK(is_bound()); |
| 107 | router_->EnableTestingMode(); |
| 108 | } |
| 109 | |
| 110 | void MultiplexedBindingState::BindInternal( |
| 111 | ScopedMessagePipeHandle handle, |
| 112 | scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 113 | const char* interface_name, |
| 114 | std::unique_ptr<MessageFilter> request_validator, |
| 115 | bool has_sync_methods, |
| 116 | MessageReceiverWithResponderStatus* stub) { |
| 117 | DCHECK(!router_); |
| 118 | |
| 119 | router_ = new internal::MultiplexRouter(false, std::move(handle), runner); |
| 120 | router_->SetMasterInterfaceName(interface_name); |
| 121 | |
| 122 | endpoint_client_.reset(new InterfaceEndpointClient( |
| 123 | router_->CreateLocalEndpointHandle(kMasterInterfaceId), stub, |
| 124 | std::move(request_validator), has_sync_methods, std::move(runner))); |
| 125 | |
| 126 | endpoint_client_->set_connection_error_handler( |
| 127 | base::Bind(&MultiplexedBindingState::RunConnectionErrorHandler, |
| 128 | base::Unretained(this))); |
| 129 | } |
| 130 | |
| 131 | void MultiplexedBindingState::RunConnectionErrorHandler() { |
| 132 | if (!connection_error_handler_.is_null()) |
| 133 | connection_error_handler_.Run(); |
| 134 | } |
| 135 | |
| 136 | } // namesapce internal |
| 137 | } // namespace mojo |