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 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_ |
7 | 7 |
8 #include <algorithm> // For |std::swap()|. | 8 #include <algorithm> // For |std::swap()|. |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 template <typename Interface, bool use_multiplex_router> | 31 template <typename Interface, bool use_multiplex_router> |
32 class InterfacePtrState; | 32 class InterfacePtrState; |
33 | 33 |
34 // Uses a single-threaded, dedicated router. If |Interface| doesn't have any | 34 // Uses a single-threaded, dedicated router. If |Interface| doesn't have any |
35 // methods to pass associated interface pointers or requests, there won't be | 35 // methods to pass associated interface pointers or requests, there won't be |
36 // multiple interfaces running on the underlying message pipe. In that case, we | 36 // multiple interfaces running on the underlying message pipe. In that case, we |
37 // can use this specialization to reduce cost. | 37 // can use this specialization to reduce cost. |
38 template <typename Interface> | 38 template <typename Interface> |
39 class InterfacePtrState<Interface, false> { | 39 class InterfacePtrState<Interface, false> { |
40 public: | 40 public: |
| 41 using GenericInterface = typename Interface::GenericInterface; |
| 42 |
41 InterfacePtrState() | 43 InterfacePtrState() |
42 : proxy_(nullptr), router_(nullptr), waiter_(nullptr), version_(0u) {} | 44 : proxy_(nullptr), router_(nullptr), waiter_(nullptr), version_(0u) {} |
43 | 45 |
44 ~InterfacePtrState() { | 46 ~InterfacePtrState() { |
45 // Destruction order matters here. We delete |proxy_| first, even though | 47 // Destruction order matters here. We delete |proxy_| first, even though |
46 // |router_| may have a reference to it, so that destructors for any request | 48 // |router_| may have a reference to it, so that destructors for any request |
47 // callbacks still pending can interact with the InterfacePtr. | 49 // callbacks still pending can interact with the InterfacePtr. |
48 delete proxy_; | 50 delete proxy_; |
49 delete router_; | 51 delete router_; |
50 } | 52 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 | 89 |
88 void Swap(InterfacePtrState* other) { | 90 void Swap(InterfacePtrState* other) { |
89 using std::swap; | 91 using std::swap; |
90 swap(other->proxy_, proxy_); | 92 swap(other->proxy_, proxy_); |
91 swap(other->router_, router_); | 93 swap(other->router_, router_); |
92 handle_.swap(other->handle_); | 94 handle_.swap(other->handle_); |
93 swap(other->waiter_, waiter_); | 95 swap(other->waiter_, waiter_); |
94 swap(other->version_, version_); | 96 swap(other->version_, version_); |
95 } | 97 } |
96 | 98 |
97 void Bind(InterfacePtrInfo<Interface> info, const MojoAsyncWaiter* waiter) { | 99 void Bind(InterfacePtrInfo<GenericInterface> info, |
| 100 const MojoAsyncWaiter* waiter) { |
98 DCHECK(!proxy_); | 101 DCHECK(!proxy_); |
99 DCHECK(!router_); | 102 DCHECK(!router_); |
100 DCHECK(!handle_.is_valid()); | 103 DCHECK(!handle_.is_valid()); |
101 DCHECK(!waiter_); | 104 DCHECK(!waiter_); |
102 DCHECK_EQ(0u, version_); | 105 DCHECK_EQ(0u, version_); |
103 DCHECK(info.is_valid()); | 106 DCHECK(info.is_valid()); |
104 | 107 |
105 handle_ = info.PassHandle(); | 108 handle_ = info.PassHandle(); |
106 waiter_ = waiter; | 109 waiter_ = waiter; |
107 version_ = info.version(); | 110 version_ = info.version(); |
108 } | 111 } |
109 | 112 |
110 bool HasAssociatedInterfaces() const { return false; } | 113 bool HasAssociatedInterfaces() const { return false; } |
111 | 114 |
112 bool WaitForIncomingResponse() { | 115 bool WaitForIncomingResponse() { |
113 ConfigureProxyIfNecessary(); | 116 ConfigureProxyIfNecessary(); |
114 | 117 |
115 DCHECK(router_); | 118 DCHECK(router_); |
116 return router_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 119 return router_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
117 } | 120 } |
118 | 121 |
119 // After this method is called, the object is in an invalid state and | 122 // After this method is called, the object is in an invalid state and |
120 // shouldn't be reused. | 123 // shouldn't be reused. |
121 InterfacePtrInfo<Interface> PassInterface() { | 124 InterfacePtrInfo<GenericInterface> PassInterface() { |
122 return InterfacePtrInfo<Interface>( | 125 return InterfacePtrInfo<GenericInterface>( |
123 router_ ? router_->PassMessagePipe() : handle_.Pass(), version_); | 126 router_ ? router_->PassMessagePipe() : handle_.Pass(), version_); |
124 } | 127 } |
125 | 128 |
126 bool is_bound() const { return handle_.is_valid() || router_; } | 129 bool is_bound() const { return handle_.is_valid() || router_; } |
127 | 130 |
128 bool encountered_error() const { | 131 bool encountered_error() const { |
129 return router_ ? router_->encountered_error() : false; | 132 return router_ ? router_->encountered_error() : false; |
130 } | 133 } |
131 | 134 |
132 void set_connection_error_handler(const Closure& error_handler) { | 135 void set_connection_error_handler(const Closure& error_handler) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 uint32_t version_; | 188 uint32_t version_; |
186 | 189 |
187 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); | 190 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); |
188 }; | 191 }; |
189 | 192 |
190 // Uses a multiplexing router. If |Interface| has methods to pass associated | 193 // Uses a multiplexing router. If |Interface| has methods to pass associated |
191 // interface pointers or requests, this specialization should be used. | 194 // interface pointers or requests, this specialization should be used. |
192 template <typename Interface> | 195 template <typename Interface> |
193 class InterfacePtrState<Interface, true> { | 196 class InterfacePtrState<Interface, true> { |
194 public: | 197 public: |
| 198 using GenericInterface = typename Interface::GenericInterface; |
| 199 |
195 InterfacePtrState() : waiter_(nullptr), version_(0u) {} | 200 InterfacePtrState() : waiter_(nullptr), version_(0u) {} |
196 | 201 |
197 ~InterfacePtrState() { | 202 ~InterfacePtrState() { |
198 endpoint_client_.reset(); | 203 endpoint_client_.reset(); |
199 proxy_.reset(); | 204 proxy_.reset(); |
200 if (router_) | 205 if (router_) |
201 router_->CloseMessagePipe(); | 206 router_->CloseMessagePipe(); |
202 } | 207 } |
203 | 208 |
204 Interface* instance() { | 209 Interface* instance() { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 void Swap(InterfacePtrState* other) { | 246 void Swap(InterfacePtrState* other) { |
242 using std::swap; | 247 using std::swap; |
243 swap(other->router_, router_); | 248 swap(other->router_, router_); |
244 swap(other->endpoint_client_, endpoint_client_); | 249 swap(other->endpoint_client_, endpoint_client_); |
245 swap(other->proxy_, proxy_); | 250 swap(other->proxy_, proxy_); |
246 handle_.swap(other->handle_); | 251 handle_.swap(other->handle_); |
247 swap(other->waiter_, waiter_); | 252 swap(other->waiter_, waiter_); |
248 swap(other->version_, version_); | 253 swap(other->version_, version_); |
249 } | 254 } |
250 | 255 |
251 void Bind(InterfacePtrInfo<Interface> info, const MojoAsyncWaiter* waiter) { | 256 void Bind(InterfacePtrInfo<GenericInterface> info, |
| 257 const MojoAsyncWaiter* waiter) { |
252 DCHECK(!router_); | 258 DCHECK(!router_); |
253 DCHECK(!endpoint_client_); | 259 DCHECK(!endpoint_client_); |
254 DCHECK(!proxy_); | 260 DCHECK(!proxy_); |
255 DCHECK(!handle_.is_valid()); | 261 DCHECK(!handle_.is_valid()); |
256 DCHECK(!waiter_); | 262 DCHECK(!waiter_); |
257 DCHECK_EQ(0u, version_); | 263 DCHECK_EQ(0u, version_); |
258 DCHECK(info.is_valid()); | 264 DCHECK(info.is_valid()); |
259 | 265 |
260 handle_ = info.PassHandle(); | 266 handle_ = info.PassHandle(); |
261 waiter_ = waiter; | 267 waiter_ = waiter; |
262 version_ = info.version(); | 268 version_ = info.version(); |
263 } | 269 } |
264 | 270 |
265 bool HasAssociatedInterfaces() const { | 271 bool HasAssociatedInterfaces() const { |
266 return router_ ? router_->HasAssociatedEndpoints() : false; | 272 return router_ ? router_->HasAssociatedEndpoints() : false; |
267 } | 273 } |
268 | 274 |
269 bool WaitForIncomingResponse() { | 275 bool WaitForIncomingResponse() { |
270 ConfigureProxyIfNecessary(); | 276 ConfigureProxyIfNecessary(); |
271 | 277 |
272 DCHECK(router_); | 278 DCHECK(router_); |
273 return router_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); | 279 return router_->WaitForIncomingMessage(MOJO_DEADLINE_INDEFINITE); |
274 } | 280 } |
275 | 281 |
276 // After this method is called, the object is in an invalid state and | 282 // After this method is called, the object is in an invalid state and |
277 // shouldn't be reused. | 283 // shouldn't be reused. |
278 InterfacePtrInfo<Interface> PassInterface() { | 284 InterfacePtrInfo<GenericInterface> PassInterface() { |
279 endpoint_client_.reset(); | 285 endpoint_client_.reset(); |
280 proxy_.reset(); | 286 proxy_.reset(); |
281 return InterfacePtrInfo<Interface>( | 287 return InterfacePtrInfo<GenericInterface>( |
282 router_ ? router_->PassMessagePipe() : handle_.Pass(), version_); | 288 router_ ? router_->PassMessagePipe() : handle_.Pass(), version_); |
283 } | 289 } |
284 | 290 |
285 bool is_bound() const { return handle_.is_valid() || endpoint_client_; } | 291 bool is_bound() const { return handle_.is_valid() || endpoint_client_; } |
286 | 292 |
287 bool encountered_error() const { | 293 bool encountered_error() const { |
288 return endpoint_client_ ? endpoint_client_->encountered_error() : false; | 294 return endpoint_client_ ? endpoint_client_->encountered_error() : false; |
289 } | 295 } |
290 | 296 |
291 void set_connection_error_handler(const Closure& error_handler) { | 297 void set_connection_error_handler(const Closure& error_handler) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 | 355 |
350 uint32_t version_; | 356 uint32_t version_; |
351 | 357 |
352 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); | 358 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); |
353 }; | 359 }; |
354 | 360 |
355 } // namespace internal | 361 } // namespace internal |
356 } // namespace mojo | 362 } // namespace mojo |
357 | 363 |
358 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_ | 364 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_ |
OLD | NEW |