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 <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> // For |std::swap()|. | 10 #include <algorithm> // For |std::swap()|. |
11 #include <memory> | 11 #include <memory> |
12 #include <utility> | 12 #include <utility> |
13 | 13 |
14 #include "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/callback_forward.h" | |
16 #include "base/logging.h" | 15 #include "base/logging.h" |
17 #include "base/macros.h" | 16 #include "base/macros.h" |
18 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
19 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
20 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
21 #include "mojo/public/cpp/bindings/associated_group.h" | 20 #include "mojo/public/cpp/bindings/associated_group.h" |
| 21 #include "mojo/public/cpp/bindings/callback.h" |
22 #include "mojo/public/cpp/bindings/interface_ptr_info.h" | 22 #include "mojo/public/cpp/bindings/interface_ptr_info.h" |
23 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" | 23 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" |
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/interface_endpoint_client.h" | 25 #include "mojo/public/cpp/bindings/lib/interface_endpoint_client.h" |
26 #include "mojo/public/cpp/bindings/lib/interface_id.h" | 26 #include "mojo/public/cpp/bindings/lib/interface_id.h" |
27 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | 27 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" |
28 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" | 28 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" |
29 #include "mojo/public/cpp/bindings/lib/router.h" | 29 #include "mojo/public/cpp/bindings/lib/router.h" |
30 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" | 30 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
31 | 31 |
(...skipping 22 matching lines...) Expand all Loading... |
54 | 54 |
55 Interface* instance() { | 55 Interface* instance() { |
56 ConfigureProxyIfNecessary(); | 56 ConfigureProxyIfNecessary(); |
57 | 57 |
58 // This will be null if the object is not bound. | 58 // This will be null if the object is not bound. |
59 return proxy_; | 59 return proxy_; |
60 } | 60 } |
61 | 61 |
62 uint32_t version() const { return version_; } | 62 uint32_t version() const { return version_; } |
63 | 63 |
64 void QueryVersion(const base::Callback<void(uint32_t)>& callback) { | 64 void QueryVersion(const Callback<void(uint32_t)>& callback) { |
65 ConfigureProxyIfNecessary(); | 65 ConfigureProxyIfNecessary(); |
66 | 66 |
67 // Do a static cast in case the interface contains methods with the same | 67 // Do a static cast in case the interface contains methods with the same |
68 // name. It is safe to capture |this| because the callback won't be run | 68 // name. It is safe to capture |this| because the callback won't be run |
69 // after this object goes away. | 69 // after this object goes away. |
70 static_cast<ControlMessageProxy*>(proxy_)->QueryVersion( | 70 static_cast<ControlMessageProxy*>(proxy_)->QueryVersion( |
71 base::Bind(&InterfacePtrState::OnQueryVersion, base::Unretained(this), | 71 base::Bind(&InterfacePtrState::OnQueryVersion, base::Unretained(this), |
72 callback)); | 72 callback)); |
73 } | 73 } |
74 | 74 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 return InterfacePtrInfo<Interface>( | 121 return InterfacePtrInfo<Interface>( |
122 router_ ? router_->PassMessagePipe() : std::move(handle_), version_); | 122 router_ ? router_->PassMessagePipe() : std::move(handle_), version_); |
123 } | 123 } |
124 | 124 |
125 bool is_bound() const { return handle_.is_valid() || router_; } | 125 bool is_bound() const { return handle_.is_valid() || router_; } |
126 | 126 |
127 bool encountered_error() const { | 127 bool encountered_error() const { |
128 return router_ ? router_->encountered_error() : false; | 128 return router_ ? router_->encountered_error() : false; |
129 } | 129 } |
130 | 130 |
131 void set_connection_error_handler(const base::Closure& error_handler) { | 131 void set_connection_error_handler(const Closure& error_handler) { |
132 ConfigureProxyIfNecessary(); | 132 ConfigureProxyIfNecessary(); |
133 | 133 |
134 DCHECK(router_); | 134 DCHECK(router_); |
135 router_->set_connection_error_handler(error_handler); | 135 router_->set_connection_error_handler(error_handler); |
136 } | 136 } |
137 | 137 |
138 // Returns true if bound and awaiting a response to a message. | 138 // Returns true if bound and awaiting a response to a message. |
139 bool has_pending_callbacks() const { | 139 bool has_pending_callbacks() const { |
140 return router_ && router_->has_pending_responders(); | 140 return router_ && router_->has_pending_responders(); |
141 } | 141 } |
(...skipping 21 matching lines...) Expand all Loading... |
163 FilterChain filters; | 163 FilterChain filters; |
164 filters.Append<MessageHeaderValidator>(Interface::Name_); | 164 filters.Append<MessageHeaderValidator>(Interface::Name_); |
165 filters.Append<typename Interface::ResponseValidator_>(); | 165 filters.Append<typename Interface::ResponseValidator_>(); |
166 | 166 |
167 router_ = new Router(std::move(handle_), std::move(filters), false, | 167 router_ = new Router(std::move(handle_), std::move(filters), false, |
168 std::move(runner_)); | 168 std::move(runner_)); |
169 | 169 |
170 proxy_ = new Proxy(router_); | 170 proxy_ = new Proxy(router_); |
171 } | 171 } |
172 | 172 |
173 void OnQueryVersion(const base::Callback<void(uint32_t)>& callback, | 173 void OnQueryVersion(const Callback<void(uint32_t)>& callback, |
174 uint32_t version) { | 174 uint32_t version) { |
175 version_ = version; | 175 version_ = version; |
176 callback.Run(version); | 176 callback.Run(version); |
177 } | 177 } |
178 | 178 |
179 Proxy* proxy_; | 179 Proxy* proxy_; |
180 Router* router_; | 180 Router* router_; |
181 | 181 |
182 // |proxy_| and |router_| are not initialized until read/write with the | 182 // |proxy_| and |router_| are not initialized until read/write with the |
183 // message pipe handle is needed. |handle_| is valid between the Bind() call | 183 // message pipe handle is needed. |handle_| is valid between the Bind() call |
(...skipping 22 matching lines...) Expand all Loading... |
206 | 206 |
207 Interface* instance() { | 207 Interface* instance() { |
208 ConfigureProxyIfNecessary(); | 208 ConfigureProxyIfNecessary(); |
209 | 209 |
210 // This will be null if the object is not bound. | 210 // This will be null if the object is not bound. |
211 return proxy_.get(); | 211 return proxy_.get(); |
212 } | 212 } |
213 | 213 |
214 uint32_t version() const { return version_; } | 214 uint32_t version() const { return version_; } |
215 | 215 |
216 void QueryVersion(const base::Callback<void(uint32_t)>& callback) { | 216 void QueryVersion(const Callback<void(uint32_t)>& callback) { |
217 ConfigureProxyIfNecessary(); | 217 ConfigureProxyIfNecessary(); |
218 | 218 |
219 | 219 |
220 // Do a static cast in case the interface contains methods with the same | 220 // Do a static cast in case the interface contains methods with the same |
221 // name. It is safe to capture |this| because the callback won't be run | 221 // name. It is safe to capture |this| because the callback won't be run |
222 // after this object goes away. | 222 // after this object goes away. |
223 static_cast<ControlMessageProxy*>(proxy_.get())->QueryVersion( | 223 static_cast<ControlMessageProxy*>(proxy_.get())->QueryVersion( |
224 base::Bind(&InterfacePtrState::OnQueryVersion, base::Unretained(this), | 224 base::Bind(&InterfacePtrState::OnQueryVersion, base::Unretained(this), |
225 callback)); | 225 callback)); |
226 } | 226 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 return InterfacePtrInfo<Interface>( | 280 return InterfacePtrInfo<Interface>( |
281 router_ ? router_->PassMessagePipe() : std::move(handle_), version_); | 281 router_ ? router_->PassMessagePipe() : std::move(handle_), version_); |
282 } | 282 } |
283 | 283 |
284 bool is_bound() const { return handle_.is_valid() || endpoint_client_; } | 284 bool is_bound() const { return handle_.is_valid() || endpoint_client_; } |
285 | 285 |
286 bool encountered_error() const { | 286 bool encountered_error() const { |
287 return endpoint_client_ ? endpoint_client_->encountered_error() : false; | 287 return endpoint_client_ ? endpoint_client_->encountered_error() : false; |
288 } | 288 } |
289 | 289 |
290 void set_connection_error_handler(const base::Closure& error_handler) { | 290 void set_connection_error_handler(const Closure& error_handler) { |
291 ConfigureProxyIfNecessary(); | 291 ConfigureProxyIfNecessary(); |
292 | 292 |
293 DCHECK(endpoint_client_); | 293 DCHECK(endpoint_client_); |
294 endpoint_client_->set_connection_error_handler(error_handler); | 294 endpoint_client_->set_connection_error_handler(error_handler); |
295 } | 295 } |
296 | 296 |
297 // Returns true if bound and awaiting a response to a message. | 297 // Returns true if bound and awaiting a response to a message. |
298 bool has_pending_callbacks() const { | 298 bool has_pending_callbacks() const { |
299 return endpoint_client_ && endpoint_client_->has_pending_responders(); | 299 return endpoint_client_ && endpoint_client_->has_pending_responders(); |
300 } | 300 } |
(...skipping 25 matching lines...) Expand all Loading... |
326 router_ = new MultiplexRouter(true, std::move(handle_), runner_); | 326 router_ = new MultiplexRouter(true, std::move(handle_), runner_); |
327 router_->SetMasterInterfaceName(Interface::Name_); | 327 router_->SetMasterInterfaceName(Interface::Name_); |
328 endpoint_client_.reset(new InterfaceEndpointClient( | 328 endpoint_client_.reset(new InterfaceEndpointClient( |
329 router_->CreateLocalEndpointHandle(kMasterInterfaceId), nullptr, | 329 router_->CreateLocalEndpointHandle(kMasterInterfaceId), nullptr, |
330 base::WrapUnique(new typename Interface::ResponseValidator_()), false, | 330 base::WrapUnique(new typename Interface::ResponseValidator_()), false, |
331 std::move(runner_))); | 331 std::move(runner_))); |
332 proxy_.reset(new Proxy(endpoint_client_.get())); | 332 proxy_.reset(new Proxy(endpoint_client_.get())); |
333 proxy_->serialization_context()->router = endpoint_client_->router(); | 333 proxy_->serialization_context()->router = endpoint_client_->router(); |
334 } | 334 } |
335 | 335 |
336 void OnQueryVersion(const base::Callback<void(uint32_t)>& callback, | 336 void OnQueryVersion(const Callback<void(uint32_t)>& callback, |
337 uint32_t version) { | 337 uint32_t version) { |
338 version_ = version; | 338 version_ = version; |
339 callback.Run(version); | 339 callback.Run(version); |
340 } | 340 } |
341 | 341 |
342 scoped_refptr<MultiplexRouter> router_; | 342 scoped_refptr<MultiplexRouter> router_; |
343 | 343 |
344 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; | 344 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; |
345 std::unique_ptr<Proxy> proxy_; | 345 std::unique_ptr<Proxy> proxy_; |
346 | 346 |
347 // |router_| (as well as other members above) is not initialized until | 347 // |router_| (as well as other members above) is not initialized until |
348 // read/write with the message pipe handle is needed. |handle_| is valid | 348 // read/write with the message pipe handle is needed. |handle_| is valid |
349 // between the Bind() call and the initialization of |router_|. | 349 // between the Bind() call and the initialization of |router_|. |
350 ScopedMessagePipeHandle handle_; | 350 ScopedMessagePipeHandle handle_; |
351 scoped_refptr<base::SingleThreadTaskRunner> runner_; | 351 scoped_refptr<base::SingleThreadTaskRunner> runner_; |
352 | 352 |
353 uint32_t version_; | 353 uint32_t version_; |
354 | 354 |
355 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); | 355 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); |
356 }; | 356 }; |
357 | 357 |
358 } // namespace internal | 358 } // namespace internal |
359 } // namespace mojo | 359 } // namespace mojo |
360 | 360 |
361 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_ | 361 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_ |
OLD | NEW |