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 <string> |
9 #include <utility> | 10 #include <utility> |
10 | 11 |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
12 #include "base/callback.h" | 13 #include "base/callback.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/macros.h" | 15 #include "base/macros.h" |
15 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
17 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
18 #include "mojo/public/cpp/bindings/associated_group.h" | 19 #include "mojo/public/cpp/bindings/associated_group.h" |
| 20 #include "mojo/public/cpp/bindings/connection_error_callback.h" |
19 #include "mojo/public/cpp/bindings/filter_chain.h" | 21 #include "mojo/public/cpp/bindings/filter_chain.h" |
20 #include "mojo/public/cpp/bindings/interface_endpoint_client.h" | 22 #include "mojo/public/cpp/bindings/interface_endpoint_client.h" |
21 #include "mojo/public/cpp/bindings/interface_id.h" | 23 #include "mojo/public/cpp/bindings/interface_id.h" |
22 #include "mojo/public/cpp/bindings/interface_ptr.h" | 24 #include "mojo/public/cpp/bindings/interface_ptr.h" |
23 #include "mojo/public/cpp/bindings/interface_ptr_info.h" | 25 #include "mojo/public/cpp/bindings/interface_ptr_info.h" |
24 #include "mojo/public/cpp/bindings/interface_request.h" | 26 #include "mojo/public/cpp/bindings/interface_request.h" |
25 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" | 27 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" |
26 #include "mojo/public/cpp/bindings/lib/router.h" | 28 #include "mojo/public/cpp/bindings/lib/router.h" |
27 #include "mojo/public/cpp/bindings/message_header_validator.h" | 29 #include "mojo/public/cpp/bindings/message_header_validator.h" |
28 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" | 30 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
(...skipping 13 matching lines...) Expand all Loading... |
42 | 44 |
43 bool HasAssociatedInterfaces() const { return false; } | 45 bool HasAssociatedInterfaces() const { return false; } |
44 | 46 |
45 void PauseIncomingMethodCallProcessing(); | 47 void PauseIncomingMethodCallProcessing(); |
46 void ResumeIncomingMethodCallProcessing(); | 48 void ResumeIncomingMethodCallProcessing(); |
47 | 49 |
48 bool WaitForIncomingMethodCall( | 50 bool WaitForIncomingMethodCall( |
49 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE); | 51 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE); |
50 | 52 |
51 void Close(); | 53 void Close(); |
| 54 void CloseWithReason(uint32_t custom_reason, const std::string& description); |
52 | 55 |
53 void set_connection_error_handler(const base::Closure& error_handler) { | 56 void set_connection_error_handler(const base::Closure& error_handler) { |
54 DCHECK(is_bound()); | 57 DCHECK(is_bound()); |
55 connection_error_handler_ = error_handler; | 58 router_->set_connection_error_handler(error_handler); |
| 59 } |
| 60 |
| 61 void set_connection_error_with_reason_handler( |
| 62 const ConnectionErrorWithReasonCallback& error_handler) { |
| 63 DCHECK(is_bound()); |
| 64 router_->set_connection_error_with_reason_handler(error_handler); |
56 } | 65 } |
57 | 66 |
58 bool is_bound() const { return !!router_; } | 67 bool is_bound() const { return !!router_; } |
59 | 68 |
60 MessagePipeHandle handle() const { | 69 MessagePipeHandle handle() const { |
61 DCHECK(is_bound()); | 70 DCHECK(is_bound()); |
62 return router_->handle(); | 71 return router_->handle(); |
63 } | 72 } |
64 | 73 |
65 AssociatedGroup* associated_group() { return nullptr; } | 74 AssociatedGroup* associated_group() { return nullptr; } |
66 | 75 |
67 void FlushForTesting(); | 76 void FlushForTesting(); |
68 | 77 |
69 void EnableTestingMode(); | 78 void EnableTestingMode(); |
70 | 79 |
71 protected: | 80 protected: |
72 void BindInternal(ScopedMessagePipeHandle handle, | 81 void BindInternal(ScopedMessagePipeHandle handle, |
73 scoped_refptr<base::SingleThreadTaskRunner> runner, | 82 scoped_refptr<base::SingleThreadTaskRunner> runner, |
74 const char* interface_name, | 83 const char* interface_name, |
75 std::unique_ptr<MessageReceiver> request_validator, | 84 std::unique_ptr<MessageReceiver> request_validator, |
76 bool has_sync_methods, | 85 bool has_sync_methods, |
77 MessageReceiverWithResponderStatus* stub, | 86 MessageReceiverWithResponderStatus* stub, |
78 uint32_t interface_version); | 87 uint32_t interface_version); |
79 | 88 |
80 void DestroyRouter(); | 89 void DestroyRouter(); |
81 | 90 |
82 internal::Router* router_ = nullptr; | 91 internal::Router* router_ = nullptr; |
83 base::Closure connection_error_handler_; | |
84 | |
85 private: | |
86 void RunConnectionErrorHandler(); | |
87 }; | 92 }; |
88 | 93 |
89 template <typename Interface, bool use_multiplex_router> | 94 template <typename Interface, bool use_multiplex_router> |
90 class BindingState; | 95 class BindingState; |
91 | 96 |
92 // Uses a single-threaded, dedicated router. If |Interface| doesn't have any | 97 // Uses a single-threaded, dedicated router. If |Interface| doesn't have any |
93 // methods to pass associated interface pointers or requests, there won't be | 98 // methods to pass associated interface pointers or requests, there won't be |
94 // multiple interfaces running on the underlying message pipe. In that case, we | 99 // multiple interfaces running on the underlying message pipe. In that case, we |
95 // can use this specialization to reduce cost. | 100 // can use this specialization to reduce cost. |
96 template <typename Interface> | 101 template <typename Interface> |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 | 143 |
139 bool HasAssociatedInterfaces() const; | 144 bool HasAssociatedInterfaces() const; |
140 | 145 |
141 void PauseIncomingMethodCallProcessing(); | 146 void PauseIncomingMethodCallProcessing(); |
142 void ResumeIncomingMethodCallProcessing(); | 147 void ResumeIncomingMethodCallProcessing(); |
143 | 148 |
144 bool WaitForIncomingMethodCall( | 149 bool WaitForIncomingMethodCall( |
145 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE); | 150 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE); |
146 | 151 |
147 void Close(); | 152 void Close(); |
| 153 void CloseWithReason(uint32_t custom_reason, const std::string& description); |
148 | 154 |
149 void set_connection_error_handler(const base::Closure& error_handler) { | 155 void set_connection_error_handler(const base::Closure& error_handler) { |
150 DCHECK(is_bound()); | 156 DCHECK(is_bound()); |
151 connection_error_handler_ = error_handler; | 157 endpoint_client_->set_connection_error_handler(error_handler); |
| 158 } |
| 159 |
| 160 void set_connection_error_with_reason_handler( |
| 161 const ConnectionErrorWithReasonCallback& error_handler) { |
| 162 DCHECK(is_bound()); |
| 163 endpoint_client_->set_connection_error_with_reason_handler(error_handler); |
152 } | 164 } |
153 | 165 |
154 bool is_bound() const { return !!router_; } | 166 bool is_bound() const { return !!router_; } |
155 | 167 |
156 MessagePipeHandle handle() const { | 168 MessagePipeHandle handle() const { |
157 DCHECK(is_bound()); | 169 DCHECK(is_bound()); |
158 return router_->handle(); | 170 return router_->handle(); |
159 } | 171 } |
160 | 172 |
161 AssociatedGroup* associated_group() { | 173 AssociatedGroup* associated_group() { |
162 return endpoint_client_ ? endpoint_client_->associated_group() : nullptr; | 174 return endpoint_client_ ? endpoint_client_->associated_group() : nullptr; |
163 } | 175 } |
164 | 176 |
165 void FlushForTesting(); | 177 void FlushForTesting(); |
166 | 178 |
167 void EnableTestingMode(); | 179 void EnableTestingMode(); |
168 | 180 |
169 protected: | 181 protected: |
170 void BindInternal(ScopedMessagePipeHandle handle, | 182 void BindInternal(ScopedMessagePipeHandle handle, |
171 scoped_refptr<base::SingleThreadTaskRunner> runner, | 183 scoped_refptr<base::SingleThreadTaskRunner> runner, |
172 const char* interface_name, | 184 const char* interface_name, |
173 std::unique_ptr<MessageReceiver> request_validator, | 185 std::unique_ptr<MessageReceiver> request_validator, |
174 bool has_sync_methods, | 186 bool has_sync_methods, |
175 MessageReceiverWithResponderStatus* stub, | 187 MessageReceiverWithResponderStatus* stub, |
176 uint32_t interface_version); | 188 uint32_t interface_version); |
177 | 189 |
178 scoped_refptr<internal::MultiplexRouter> router_; | 190 scoped_refptr<internal::MultiplexRouter> router_; |
179 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; | 191 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; |
180 base::Closure connection_error_handler_; | |
181 | |
182 private: | |
183 void RunConnectionErrorHandler(); | |
184 }; | 192 }; |
185 | 193 |
186 // Uses a multiplexing router. If |Interface| has methods to pass associated | 194 // Uses a multiplexing router. If |Interface| has methods to pass associated |
187 // interface pointers or requests, this specialization should be used. | 195 // interface pointers or requests, this specialization should be used. |
188 template <typename Interface> | 196 template <typename Interface> |
189 class BindingState<Interface, true> : public MultiplexedBindingState { | 197 class BindingState<Interface, true> : public MultiplexedBindingState { |
190 public: | 198 public: |
191 explicit BindingState(Interface* impl) : impl_(impl) { | 199 explicit BindingState(Interface* impl) : impl_(impl) { |
192 stub_.set_sink(impl_); | 200 stub_.set_sink(impl_); |
193 } | 201 } |
194 | 202 |
195 ~BindingState() { Close(); } | 203 ~BindingState() { Close(); } |
196 | 204 |
197 void Bind(ScopedMessagePipeHandle handle, | 205 void Bind(ScopedMessagePipeHandle handle, |
198 scoped_refptr<base::SingleThreadTaskRunner> runner) { | 206 scoped_refptr<base::SingleThreadTaskRunner> runner) { |
199 MultiplexedBindingState::BindInternal( | 207 MultiplexedBindingState::BindInternal( |
200 std::move(handle), runner, Interface::Name_, | 208 std::move(handle), runner, Interface::Name_, |
201 base::MakeUnique<typename Interface::RequestValidator_>(), | 209 base::MakeUnique<typename Interface::RequestValidator_>(), |
202 Interface::HasSyncMethods_, &stub_, Interface::Version_); | 210 Interface::HasSyncMethods_, &stub_, Interface::Version_); |
203 stub_.serialization_context()->group_controller = router_; | 211 stub_.serialization_context()->group_controller = router_; |
204 } | 212 } |
205 | 213 |
206 InterfaceRequest<Interface> Unbind() { | 214 InterfaceRequest<Interface> Unbind() { |
207 endpoint_client_.reset(); | 215 endpoint_client_.reset(); |
208 InterfaceRequest<Interface> request = | 216 InterfaceRequest<Interface> request = |
209 MakeRequest<Interface>(router_->PassMessagePipe()); | 217 MakeRequest<Interface>(router_->PassMessagePipe()); |
210 router_ = nullptr; | 218 router_ = nullptr; |
211 connection_error_handler_.Reset(); | |
212 return request; | 219 return request; |
213 } | 220 } |
214 | 221 |
215 Interface* impl() { return impl_; } | 222 Interface* impl() { return impl_; } |
216 | 223 |
217 private: | 224 private: |
218 typename Interface::Stub_ stub_; | 225 typename Interface::Stub_ stub_; |
219 Interface* impl_; | 226 Interface* impl_; |
220 | 227 |
221 DISALLOW_COPY_AND_ASSIGN(BindingState); | 228 DISALLOW_COPY_AND_ASSIGN(BindingState); |
222 }; | 229 }; |
223 | 230 |
224 } // namesapce internal | 231 } // namesapce internal |
225 } // namespace mojo | 232 } // namespace mojo |
226 | 233 |
227 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_ | 234 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_ |
OLD | NEW |