| 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 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | 24 #include "mojo/public/cpp/bindings/lib/filter_chain.h" |
| 25 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" | 25 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" |
| 26 #include "mojo/public/cpp/bindings/lib/router.h" | 26 #include "mojo/public/cpp/bindings/lib/router.h" |
| 27 #include "mojo/public/cpp/bindings/message_header_validator.h" | 27 #include "mojo/public/cpp/bindings/message_header_validator.h" |
| 28 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" | 28 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
| 29 #include "mojo/public/cpp/system/core.h" | 29 #include "mojo/public/cpp/system/core.h" |
| 30 | 30 |
| 31 namespace mojo { | 31 namespace mojo { |
| 32 namespace internal { | 32 namespace internal { |
| 33 | 33 |
| 34 // Base class used for templated binding primitives which bind a pipe |
| 35 // exclusively to a single interface. |
| 36 class SimpleBindingState { |
| 37 public: |
| 38 SimpleBindingState(); |
| 39 ~SimpleBindingState(); |
| 40 |
| 41 bool HasAssociatedInterfaces() const { return false; } |
| 42 |
| 43 void PauseIncomingMethodCallProcessing(); |
| 44 void ResumeIncomingMethodCallProcessing(); |
| 45 |
| 46 bool WaitForIncomingMethodCall( |
| 47 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE); |
| 48 |
| 49 void Close(); |
| 50 |
| 51 void set_connection_error_handler(const base::Closure& error_handler) { |
| 52 DCHECK(is_bound()); |
| 53 connection_error_handler_ = error_handler; |
| 54 } |
| 55 |
| 56 bool is_bound() const { return !!router_; } |
| 57 |
| 58 MessagePipeHandle handle() const { |
| 59 DCHECK(is_bound()); |
| 60 return router_->handle(); |
| 61 } |
| 62 |
| 63 AssociatedGroup* associated_group() { return nullptr; } |
| 64 |
| 65 void EnableTestingMode(); |
| 66 |
| 67 protected: |
| 68 void BindInternal(ScopedMessagePipeHandle handle, |
| 69 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 70 const char* interface_name, |
| 71 MessageFilter* request_validator, |
| 72 bool has_sync_methods, |
| 73 MessageReceiverWithResponderStatus* stub); |
| 74 |
| 75 void DestroyRouter(); |
| 76 |
| 77 internal::Router* router_ = nullptr; |
| 78 base::Closure connection_error_handler_; |
| 79 |
| 80 private: |
| 81 void RunConnectionErrorHandler(); |
| 82 }; |
| 83 |
| 34 template <typename Interface, bool use_multiplex_router> | 84 template <typename Interface, bool use_multiplex_router> |
| 35 class BindingState; | 85 class BindingState; |
| 36 | 86 |
| 37 // Uses a single-threaded, dedicated router. If |Interface| doesn't have any | 87 // Uses a single-threaded, dedicated router. If |Interface| doesn't have any |
| 38 // methods to pass associated interface pointers or requests, there won't be | 88 // methods to pass associated interface pointers or requests, there won't be |
| 39 // multiple interfaces running on the underlying message pipe. In that case, we | 89 // multiple interfaces running on the underlying message pipe. In that case, we |
| 40 // can use this specialization to reduce cost. | 90 // can use this specialization to reduce cost. |
| 41 template <typename Interface> | 91 template <typename Interface> |
| 42 class BindingState<Interface, false> { | 92 class BindingState<Interface, false> : public SimpleBindingState { |
| 43 public: | 93 public: |
| 44 explicit BindingState(Interface* impl) : impl_(impl) { | 94 explicit BindingState(Interface* impl) : impl_(impl) { |
| 45 stub_.set_sink(impl_); | 95 stub_.set_sink(impl_); |
| 46 } | 96 } |
| 47 | 97 |
| 48 ~BindingState() { Close(); } | 98 ~BindingState() { Close(); } |
| 49 | 99 |
| 50 void Bind(ScopedMessagePipeHandle handle, | 100 void Bind(ScopedMessagePipeHandle handle, |
| 51 scoped_refptr<base::SingleThreadTaskRunner> runner) { | 101 scoped_refptr<base::SingleThreadTaskRunner> runner) { |
| 52 DCHECK(!router_); | 102 DCHECK(!router_); |
| 53 internal::FilterChain filters; | 103 SimpleBindingState::BindInternal(std::move(handle), runner, |
| 54 filters.Append<MessageHeaderValidator>(Interface::Name_); | 104 Interface::Name_, new |
| 55 filters.Append<typename Interface::RequestValidator_>(); | 105 typename Interface::RequestValidator_(), |
| 56 | 106 Interface::HasSyncMethods_, &stub_); |
| 57 router_ = | |
| 58 new internal::Router(std::move(handle), std::move(filters), | |
| 59 Interface::HasSyncMethods_, std::move(runner)); | |
| 60 router_->set_incoming_receiver(&stub_); | |
| 61 router_->set_connection_error_handler( | |
| 62 base::Bind(&BindingState::RunConnectionErrorHandler, | |
| 63 base::Unretained(this))); | |
| 64 } | |
| 65 | |
| 66 bool HasAssociatedInterfaces() const { return false; } | |
| 67 | |
| 68 void PauseIncomingMethodCallProcessing() { | |
| 69 DCHECK(router_); | |
| 70 router_->PauseIncomingMethodCallProcessing(); | |
| 71 } | |
| 72 void ResumeIncomingMethodCallProcessing() { | |
| 73 DCHECK(router_); | |
| 74 router_->ResumeIncomingMethodCallProcessing(); | |
| 75 } | |
| 76 | |
| 77 bool WaitForIncomingMethodCall( | |
| 78 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE) { | |
| 79 DCHECK(router_); | |
| 80 return router_->WaitForIncomingMessage(deadline); | |
| 81 } | |
| 82 | |
| 83 void Close() { | |
| 84 if (!router_) | |
| 85 return; | |
| 86 | |
| 87 router_->CloseMessagePipe(); | |
| 88 DestroyRouter(); | |
| 89 } | 107 } |
| 90 | 108 |
| 91 InterfaceRequest<Interface> Unbind() { | 109 InterfaceRequest<Interface> Unbind() { |
| 92 InterfaceRequest<Interface> request = | 110 InterfaceRequest<Interface> request = |
| 93 MakeRequest<Interface>(router_->PassMessagePipe()); | 111 MakeRequest<Interface>(router_->PassMessagePipe()); |
| 94 DestroyRouter(); | 112 DestroyRouter(); |
| 95 return std::move(request); | 113 return std::move(request); |
| 96 } | 114 } |
| 97 | 115 |
| 116 Interface* impl() { return impl_; } |
| 117 |
| 118 private: |
| 119 typename Interface::Stub_ stub_; |
| 120 Interface* impl_; |
| 121 |
| 122 DISALLOW_COPY_AND_ASSIGN(BindingState); |
| 123 }; |
| 124 |
| 125 // Base class used for templated binding primitives which may bind a pipe to |
| 126 // multiple interfaces. |
| 127 class MultiplexedBindingState { |
| 128 public: |
| 129 MultiplexedBindingState(); |
| 130 ~MultiplexedBindingState(); |
| 131 |
| 132 bool HasAssociatedInterfaces() const; |
| 133 |
| 134 void PauseIncomingMethodCallProcessing(); |
| 135 void ResumeIncomingMethodCallProcessing(); |
| 136 |
| 137 bool WaitForIncomingMethodCall( |
| 138 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE); |
| 139 |
| 140 void Close(); |
| 141 |
| 98 void set_connection_error_handler(const base::Closure& error_handler) { | 142 void set_connection_error_handler(const base::Closure& error_handler) { |
| 99 DCHECK(is_bound()); | 143 DCHECK(is_bound()); |
| 100 connection_error_handler_ = error_handler; | 144 connection_error_handler_ = error_handler; |
| 101 } | 145 } |
| 102 | 146 |
| 103 Interface* impl() { return impl_; } | |
| 104 | |
| 105 bool is_bound() const { return !!router_; } | 147 bool is_bound() const { return !!router_; } |
| 106 | 148 |
| 107 MessagePipeHandle handle() const { | 149 MessagePipeHandle handle() const { |
| 108 DCHECK(is_bound()); | 150 DCHECK(is_bound()); |
| 109 return router_->handle(); | 151 return router_->handle(); |
| 110 } | 152 } |
| 111 | 153 |
| 112 AssociatedGroup* associated_group() { return nullptr; } | 154 AssociatedGroup* associated_group() { |
| 113 | 155 return endpoint_client_ ? endpoint_client_->associated_group() : nullptr; |
| 114 void EnableTestingMode() { | |
| 115 DCHECK(is_bound()); | |
| 116 router_->EnableTestingMode(); | |
| 117 } | 156 } |
| 118 | 157 |
| 119 private: | 158 void EnableTestingMode(); |
| 120 void DestroyRouter() { | |
| 121 router_->set_connection_error_handler(base::Closure()); | |
| 122 delete router_; | |
| 123 router_ = nullptr; | |
| 124 connection_error_handler_.Reset(); | |
| 125 } | |
| 126 | 159 |
| 127 void RunConnectionErrorHandler() { | 160 protected: |
| 128 if (!connection_error_handler_.is_null()) | 161 void BindInternal(ScopedMessagePipeHandle handle, |
| 129 connection_error_handler_.Run(); | 162 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 130 } | 163 const char* interface_name, |
| 164 std::unique_ptr<MessageFilter> request_validator, |
| 165 bool has_sync_methods, |
| 166 MessageReceiverWithResponderStatus* stub); |
| 131 | 167 |
| 132 internal::Router* router_ = nullptr; | 168 scoped_refptr<internal::MultiplexRouter> router_; |
| 133 typename Interface::Stub_ stub_; | 169 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; |
| 134 Interface* impl_; | |
| 135 base::Closure connection_error_handler_; | 170 base::Closure connection_error_handler_; |
| 136 | 171 |
| 137 DISALLOW_COPY_AND_ASSIGN(BindingState); | 172 private: |
| 173 void RunConnectionErrorHandler(); |
| 138 }; | 174 }; |
| 139 | 175 |
| 140 // Uses a multiplexing router. If |Interface| has methods to pass associated | 176 // Uses a multiplexing router. If |Interface| has methods to pass associated |
| 141 // interface pointers or requests, this specialization should be used. | 177 // interface pointers or requests, this specialization should be used. |
| 142 template <typename Interface> | 178 template <typename Interface> |
| 143 class BindingState<Interface, true> { | 179 class BindingState<Interface, true> : public MultiplexedBindingState { |
| 144 public: | 180 public: |
| 145 explicit BindingState(Interface* impl) : impl_(impl) { | 181 explicit BindingState(Interface* impl) : impl_(impl) { |
| 146 stub_.set_sink(impl_); | 182 stub_.set_sink(impl_); |
| 147 } | 183 } |
| 148 | 184 |
| 149 ~BindingState() { Close(); } | 185 ~BindingState() { Close(); } |
| 150 | 186 |
| 151 void Bind(ScopedMessagePipeHandle handle, | 187 void Bind(ScopedMessagePipeHandle handle, |
| 152 scoped_refptr<base::SingleThreadTaskRunner> runner) { | 188 scoped_refptr<base::SingleThreadTaskRunner> runner) { |
| 153 DCHECK(!router_); | 189 MultiplexedBindingState::BindInternal( |
| 154 | 190 std::move(handle), runner, Interface::Name_, |
| 155 router_ = new internal::MultiplexRouter(false, std::move(handle), runner); | 191 base::WrapUnique(new typename Interface::RequestValidator_()), |
| 156 router_->SetMasterInterfaceName(Interface::Name_); | 192 Interface::HasSyncMethods_, &stub_); |
| 157 stub_.serialization_context()->group_controller = router_; | 193 stub_.serialization_context()->group_controller = router_; |
| 158 | |
| 159 endpoint_client_.reset(new InterfaceEndpointClient( | |
| 160 router_->CreateLocalEndpointHandle(kMasterInterfaceId), | |
| 161 &stub_, base::WrapUnique(new typename Interface::RequestValidator_()), | |
| 162 Interface::HasSyncMethods_, std::move(runner))); | |
| 163 | |
| 164 endpoint_client_->set_connection_error_handler( | |
| 165 base::Bind(&BindingState::RunConnectionErrorHandler, | |
| 166 base::Unretained(this))); | |
| 167 } | |
| 168 | |
| 169 bool HasAssociatedInterfaces() const { | |
| 170 return router_ ? router_->HasAssociatedEndpoints() : false; | |
| 171 } | |
| 172 | |
| 173 void PauseIncomingMethodCallProcessing() { | |
| 174 DCHECK(router_); | |
| 175 router_->PauseIncomingMethodCallProcessing(); | |
| 176 } | |
| 177 void ResumeIncomingMethodCallProcessing() { | |
| 178 DCHECK(router_); | |
| 179 router_->ResumeIncomingMethodCallProcessing(); | |
| 180 } | |
| 181 | |
| 182 bool WaitForIncomingMethodCall( | |
| 183 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE) { | |
| 184 DCHECK(router_); | |
| 185 return router_->WaitForIncomingMessage(deadline); | |
| 186 } | |
| 187 | |
| 188 void Close() { | |
| 189 if (!router_) | |
| 190 return; | |
| 191 | |
| 192 endpoint_client_.reset(); | |
| 193 router_->CloseMessagePipe(); | |
| 194 router_ = nullptr; | |
| 195 connection_error_handler_.Reset(); | |
| 196 } | 194 } |
| 197 | 195 |
| 198 InterfaceRequest<Interface> Unbind() { | 196 InterfaceRequest<Interface> Unbind() { |
| 199 endpoint_client_.reset(); | 197 endpoint_client_.reset(); |
| 200 InterfaceRequest<Interface> request = | 198 InterfaceRequest<Interface> request = |
| 201 MakeRequest<Interface>(router_->PassMessagePipe()); | 199 MakeRequest<Interface>(router_->PassMessagePipe()); |
| 202 router_ = nullptr; | 200 router_ = nullptr; |
| 203 connection_error_handler_.Reset(); | 201 connection_error_handler_.Reset(); |
| 204 return request; | 202 return request; |
| 205 } | 203 } |
| 206 | 204 |
| 207 void set_connection_error_handler(const base::Closure& error_handler) { | |
| 208 DCHECK(is_bound()); | |
| 209 connection_error_handler_ = error_handler; | |
| 210 } | |
| 211 | |
| 212 Interface* impl() { return impl_; } | 205 Interface* impl() { return impl_; } |
| 213 | 206 |
| 214 bool is_bound() const { return !!router_; } | |
| 215 | |
| 216 MessagePipeHandle handle() const { | |
| 217 DCHECK(is_bound()); | |
| 218 return router_->handle(); | |
| 219 } | |
| 220 | |
| 221 AssociatedGroup* associated_group() { | |
| 222 return endpoint_client_ ? endpoint_client_->associated_group() : nullptr; | |
| 223 } | |
| 224 | |
| 225 void EnableTestingMode() { | |
| 226 DCHECK(is_bound()); | |
| 227 router_->EnableTestingMode(); | |
| 228 } | |
| 229 | |
| 230 private: | 207 private: |
| 231 void RunConnectionErrorHandler() { | |
| 232 if (!connection_error_handler_.is_null()) | |
| 233 connection_error_handler_.Run(); | |
| 234 } | |
| 235 | |
| 236 scoped_refptr<internal::MultiplexRouter> router_; | |
| 237 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; | |
| 238 | |
| 239 typename Interface::Stub_ stub_; | 208 typename Interface::Stub_ stub_; |
| 240 Interface* impl_; | 209 Interface* impl_; |
| 241 base::Closure connection_error_handler_; | |
| 242 | 210 |
| 243 DISALLOW_COPY_AND_ASSIGN(BindingState); | 211 DISALLOW_COPY_AND_ASSIGN(BindingState); |
| 244 }; | 212 }; |
| 245 | 213 |
| 246 } // namesapce internal | 214 } // namesapce internal |
| 247 } // namespace mojo | 215 } // namespace mojo |
| 248 | 216 |
| 249 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_ | 217 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_ |
| OLD | NEW |