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" | 15 #include "base/callback_forward.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
19 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
20 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
21 #include "mojo/public/cpp/bindings/associated_group.h" | 21 #include "mojo/public/cpp/bindings/associated_group.h" |
22 #include "mojo/public/cpp/bindings/filter_chain.h" | 22 #include "mojo/public/cpp/bindings/filter_chain.h" |
23 #include "mojo/public/cpp/bindings/interface_endpoint_client.h" | 23 #include "mojo/public/cpp/bindings/interface_endpoint_client.h" |
24 #include "mojo/public/cpp/bindings/interface_id.h" | 24 #include "mojo/public/cpp/bindings/interface_id.h" |
25 #include "mojo/public/cpp/bindings/interface_ptr_info.h" | 25 #include "mojo/public/cpp/bindings/interface_ptr_info.h" |
26 #include "mojo/public/cpp/bindings/lib/control_message_handler.h" | |
26 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" | 27 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" |
27 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" | 28 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" |
28 #include "mojo/public/cpp/bindings/lib/router.h" | 29 #include "mojo/public/cpp/bindings/lib/router.h" |
29 #include "mojo/public/cpp/bindings/message_header_validator.h" | 30 #include "mojo/public/cpp/bindings/message_header_validator.h" |
30 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" | 31 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
31 | 32 |
32 namespace mojo { | 33 namespace mojo { |
33 namespace internal { | 34 namespace internal { |
34 | 35 |
35 template <typename Interface, bool use_multiplex_router> | 36 template <typename Interface, bool use_multiplex_router> |
(...skipping 21 matching lines...) Expand all Loading... | |
57 | 58 |
58 // This will be null if the object is not bound. | 59 // This will be null if the object is not bound. |
59 return proxy_; | 60 return proxy_; |
60 } | 61 } |
61 | 62 |
62 uint32_t version() const { return version_; } | 63 uint32_t version() const { return version_; } |
63 | 64 |
64 void QueryVersion(const base::Callback<void(uint32_t)>& callback) { | 65 void QueryVersion(const base::Callback<void(uint32_t)>& callback) { |
65 ConfigureProxyIfNecessary(); | 66 ConfigureProxyIfNecessary(); |
66 | 67 |
67 // Do a static cast in case the interface contains methods with the same | 68 // It is safe to capture |this| because the callback won't be run after this |
68 // name. It is safe to capture |this| because the callback won't be run | 69 // object goes away. |
69 // after this object goes away. | 70 router_->control_message_proxy()->QueryVersion(base::Bind( |
70 static_cast<ControlMessageProxy*>(proxy_)->QueryVersion( | 71 &InterfacePtrState::OnQueryVersion, base::Unretained(this), callback)); |
71 base::Bind(&InterfacePtrState::OnQueryVersion, base::Unretained(this), | |
72 callback)); | |
73 } | 72 } |
74 | 73 |
75 void RequireVersion(uint32_t version) { | 74 void RequireVersion(uint32_t version) { |
76 ConfigureProxyIfNecessary(); | 75 ConfigureProxyIfNecessary(); |
77 | 76 |
78 if (version <= version_) | 77 if (version <= version_) |
79 return; | 78 return; |
80 | 79 |
81 version_ = version; | 80 version_ = version; |
82 // Do a static cast in case the interface contains methods with the same | 81 router_->control_message_proxy()->RequireVersion(version); |
83 // name. | 82 } |
84 static_cast<ControlMessageProxy*>(proxy_)->RequireVersion(version); | 83 |
84 void FlushForTesting() { | |
85 ConfigureProxyIfNecessary(); | |
86 router_->control_message_proxy()->FlushForTesting(); | |
85 } | 87 } |
86 | 88 |
87 void Swap(InterfacePtrState* other) { | 89 void Swap(InterfacePtrState* other) { |
88 using std::swap; | 90 using std::swap; |
89 swap(other->proxy_, proxy_); | 91 swap(other->proxy_, proxy_); |
90 swap(other->router_, router_); | 92 swap(other->router_, router_); |
91 handle_.swap(other->handle_); | 93 handle_.swap(other->handle_); |
92 runner_.swap(other->runner_); | 94 runner_.swap(other->runner_); |
93 swap(other->version_, version_); | 95 swap(other->version_, version_); |
94 } | 96 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 return; | 152 return; |
151 } | 153 } |
152 // The object hasn't been bound. | 154 // The object hasn't been bound. |
153 if (!handle_.is_valid()) | 155 if (!handle_.is_valid()) |
154 return; | 156 return; |
155 | 157 |
156 FilterChain filters; | 158 FilterChain filters; |
157 filters.Append<MessageHeaderValidator>(Interface::Name_); | 159 filters.Append<MessageHeaderValidator>(Interface::Name_); |
158 filters.Append<typename Interface::ResponseValidator_>(); | 160 filters.Append<typename Interface::ResponseValidator_>(); |
159 | 161 |
160 router_ = new Router(std::move(handle_), std::move(filters), false, | 162 router_ = new Router( |
161 std::move(runner_)); | 163 std::move(handle_), std::move(filters), false, std::move(runner_), |
164 // The version is only queried from the client so the value passed here | |
yzshen1
2016/08/31 17:06:47
style nit: please move it out of the statement. (a
Sam McNally
2016/09/01 00:44:37
Done.
| |
165 // will not be used. | |
166 0u); | |
162 | 167 |
163 proxy_ = new Proxy(router_); | 168 proxy_ = new Proxy(router_); |
164 } | 169 } |
165 | 170 |
166 void OnQueryVersion(const base::Callback<void(uint32_t)>& callback, | 171 void OnQueryVersion(const base::Callback<void(uint32_t)>& callback, |
167 uint32_t version) { | 172 uint32_t version) { |
168 version_ = version; | 173 version_ = version; |
169 callback.Run(version); | 174 callback.Run(version); |
170 } | 175 } |
171 | 176 |
(...skipping 30 matching lines...) Expand all Loading... | |
202 | 207 |
203 // This will be null if the object is not bound. | 208 // This will be null if the object is not bound. |
204 return proxy_.get(); | 209 return proxy_.get(); |
205 } | 210 } |
206 | 211 |
207 uint32_t version() const { return version_; } | 212 uint32_t version() const { return version_; } |
208 | 213 |
209 void QueryVersion(const base::Callback<void(uint32_t)>& callback) { | 214 void QueryVersion(const base::Callback<void(uint32_t)>& callback) { |
210 ConfigureProxyIfNecessary(); | 215 ConfigureProxyIfNecessary(); |
211 | 216 |
212 | 217 // It is safe to capture |this| because the callback won't be run after this |
213 // Do a static cast in case the interface contains methods with the same | 218 // object goes away. |
214 // name. It is safe to capture |this| because the callback won't be run | 219 endpoint_client_->control_message_proxy()->QueryVersion(base::Bind( |
215 // after this object goes away. | 220 &InterfacePtrState::OnQueryVersion, base::Unretained(this), callback)); |
216 static_cast<ControlMessageProxy*>(proxy_.get())->QueryVersion( | |
217 base::Bind(&InterfacePtrState::OnQueryVersion, base::Unretained(this), | |
218 callback)); | |
219 } | 221 } |
220 | 222 |
221 void RequireVersion(uint32_t version) { | 223 void RequireVersion(uint32_t version) { |
222 ConfigureProxyIfNecessary(); | 224 ConfigureProxyIfNecessary(); |
223 | 225 |
224 if (version <= version_) | 226 if (version <= version_) |
225 return; | 227 return; |
226 | 228 |
227 version_ = version; | 229 version_ = version; |
228 // Do a static cast in case the interface contains methods with the same | 230 endpoint_client_->control_message_proxy()->RequireVersion(version); |
229 // name. | 231 } |
230 static_cast<ControlMessageProxy*>(proxy_.get())->RequireVersion(version); | 232 |
233 void FlushForTesting() { | |
234 ConfigureProxyIfNecessary(); | |
235 endpoint_client_->control_message_proxy()->FlushForTesting(); | |
231 } | 236 } |
232 | 237 |
233 void Swap(InterfacePtrState* other) { | 238 void Swap(InterfacePtrState* other) { |
234 using std::swap; | 239 using std::swap; |
235 swap(other->router_, router_); | 240 swap(other->router_, router_); |
236 swap(other->endpoint_client_, endpoint_client_); | 241 swap(other->endpoint_client_, endpoint_client_); |
237 swap(other->proxy_, proxy_); | 242 swap(other->proxy_, proxy_); |
238 handle_.swap(other->handle_); | 243 handle_.swap(other->handle_); |
239 runner_.swap(other->runner_); | 244 runner_.swap(other->runner_); |
240 swap(other->version_, version_); | 245 swap(other->version_, version_); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
307 } | 312 } |
308 // The object hasn't been bound. | 313 // The object hasn't been bound. |
309 if (!handle_.is_valid()) | 314 if (!handle_.is_valid()) |
310 return; | 315 return; |
311 | 316 |
312 router_ = new MultiplexRouter(true, std::move(handle_), runner_); | 317 router_ = new MultiplexRouter(true, std::move(handle_), runner_); |
313 router_->SetMasterInterfaceName(Interface::Name_); | 318 router_->SetMasterInterfaceName(Interface::Name_); |
314 endpoint_client_.reset(new InterfaceEndpointClient( | 319 endpoint_client_.reset(new InterfaceEndpointClient( |
315 router_->CreateLocalEndpointHandle(kMasterInterfaceId), nullptr, | 320 router_->CreateLocalEndpointHandle(kMasterInterfaceId), nullptr, |
316 base::WrapUnique(new typename Interface::ResponseValidator_()), false, | 321 base::WrapUnique(new typename Interface::ResponseValidator_()), false, |
317 std::move(runner_))); | 322 std::move(runner_), |
323 // The version is only queried from the client so the value passed here | |
324 // will not be used. | |
325 0u)); | |
318 proxy_.reset(new Proxy(endpoint_client_.get())); | 326 proxy_.reset(new Proxy(endpoint_client_.get())); |
319 proxy_->serialization_context()->group_controller = | 327 proxy_->serialization_context()->group_controller = |
320 endpoint_client_->group_controller(); | 328 endpoint_client_->group_controller(); |
321 } | 329 } |
322 | 330 |
323 void OnQueryVersion(const base::Callback<void(uint32_t)>& callback, | 331 void OnQueryVersion(const base::Callback<void(uint32_t)>& callback, |
324 uint32_t version) { | 332 uint32_t version) { |
325 version_ = version; | 333 version_ = version; |
326 callback.Run(version); | 334 callback.Run(version); |
327 } | 335 } |
(...skipping 11 matching lines...) Expand all Loading... | |
339 | 347 |
340 uint32_t version_; | 348 uint32_t version_; |
341 | 349 |
342 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); | 350 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); |
343 }; | 351 }; |
344 | 352 |
345 } // namespace internal | 353 } // namespace internal |
346 } // namespace mojo | 354 } // namespace mojo |
347 | 355 |
348 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_ | 356 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_ |
OLD | NEW |