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/logging.h" | 15 #include "base/logging.h" |
15 #include "base/macros.h" | 16 #include "base/macros.h" |
16 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
17 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
18 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
19 #include "mojo/public/cpp/bindings/associated_group.h" | 20 #include "mojo/public/cpp/bindings/associated_group.h" |
20 #include "mojo/public/cpp/bindings/callback.h" | 21 #include "mojo/public/cpp/bindings/callback.h" |
21 #include "mojo/public/cpp/bindings/interface_ptr_info.h" | 22 #include "mojo/public/cpp/bindings/interface_ptr_info.h" |
22 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" | 23 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" |
23 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | 24 #include "mojo/public/cpp/bindings/lib/filter_chain.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 | 57 |
57 // This will be null if the object is not bound. | 58 // This will be null if the object is not bound. |
58 return proxy_; | 59 return proxy_; |
59 } | 60 } |
60 | 61 |
61 uint32_t version() const { return version_; } | 62 uint32_t version() const { return version_; } |
62 | 63 |
63 void QueryVersion(const Callback<void(uint32_t)>& callback) { | 64 void QueryVersion(const Callback<void(uint32_t)>& callback) { |
64 ConfigureProxyIfNecessary(); | 65 ConfigureProxyIfNecessary(); |
65 | 66 |
66 // It is safe to capture |this| because the callback won't be run after this | |
67 // object goes away. | |
68 auto callback_wrapper = [this, callback](uint32_t version) { | |
69 this->version_ = version; | |
70 callback.Run(version); | |
71 }; | |
72 | |
73 // 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 |
74 // name. | 68 // name. It is safe to capture |this| because the callback won't be run |
75 static_cast<ControlMessageProxy*>(proxy_)->QueryVersion(callback_wrapper); | 69 // after this object goes away. |
| 70 static_cast<ControlMessageProxy*>(proxy_)->QueryVersion( |
| 71 base::Bind(&InterfacePtrState::OnQueryVersion, base::Unretained(this), |
| 72 callback)); |
76 } | 73 } |
77 | 74 |
78 void RequireVersion(uint32_t version) { | 75 void RequireVersion(uint32_t version) { |
79 ConfigureProxyIfNecessary(); | 76 ConfigureProxyIfNecessary(); |
80 | 77 |
81 if (version <= version_) | 78 if (version <= version_) |
82 return; | 79 return; |
83 | 80 |
84 version_ = version; | 81 version_ = version; |
85 // Do a static cast in case the interface contains methods with the same | 82 // Do a static cast in case the interface contains methods with the same |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 FilterChain filters; | 163 FilterChain filters; |
167 filters.Append<MessageHeaderValidator>(Interface::Name_); | 164 filters.Append<MessageHeaderValidator>(Interface::Name_); |
168 filters.Append<typename Interface::ResponseValidator_>(); | 165 filters.Append<typename Interface::ResponseValidator_>(); |
169 | 166 |
170 router_ = new Router(std::move(handle_), std::move(filters), false, | 167 router_ = new Router(std::move(handle_), std::move(filters), false, |
171 std::move(runner_)); | 168 std::move(runner_)); |
172 | 169 |
173 proxy_ = new Proxy(router_); | 170 proxy_ = new Proxy(router_); |
174 } | 171 } |
175 | 172 |
| 173 void OnQueryVersion(const Callback<void(uint32_t)>& callback, |
| 174 uint32_t version) { |
| 175 version_ = version; |
| 176 callback.Run(version); |
| 177 } |
| 178 |
176 Proxy* proxy_; | 179 Proxy* proxy_; |
177 Router* router_; | 180 Router* router_; |
178 | 181 |
179 // |proxy_| and |router_| are not initialized until read/write with the | 182 // |proxy_| and |router_| are not initialized until read/write with the |
180 // 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 |
181 // and the initialization of |proxy_| and |router_|. | 184 // and the initialization of |proxy_| and |router_|. |
182 ScopedMessagePipeHandle handle_; | 185 ScopedMessagePipeHandle handle_; |
183 scoped_refptr<base::SingleThreadTaskRunner> runner_; | 186 scoped_refptr<base::SingleThreadTaskRunner> runner_; |
184 | 187 |
185 uint32_t version_; | 188 uint32_t version_; |
(...skipping 20 matching lines...) Expand all Loading... |
206 | 209 |
207 // This will be null if the object is not bound. | 210 // This will be null if the object is not bound. |
208 return proxy_.get(); | 211 return proxy_.get(); |
209 } | 212 } |
210 | 213 |
211 uint32_t version() const { return version_; } | 214 uint32_t version() const { return version_; } |
212 | 215 |
213 void QueryVersion(const Callback<void(uint32_t)>& callback) { | 216 void QueryVersion(const Callback<void(uint32_t)>& callback) { |
214 ConfigureProxyIfNecessary(); | 217 ConfigureProxyIfNecessary(); |
215 | 218 |
216 // It is safe to capture |this| because the callback won't be run after this | |
217 // object goes away. | |
218 auto callback_wrapper = [this, callback](uint32_t version) { | |
219 this->version_ = version; | |
220 callback.Run(version); | |
221 }; | |
222 | 219 |
223 // 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 |
224 // name. | 221 // name. It is safe to capture |this| because the callback won't be run |
225 static_cast<ControlMessageProxy*>(proxy_.get()) | 222 // after this object goes away. |
226 ->QueryVersion(callback_wrapper); | 223 static_cast<ControlMessageProxy*>(proxy_.get())->QueryVersion( |
| 224 base::Bind(&InterfacePtrState::OnQueryVersion, base::Unretained(this), |
| 225 callback)); |
227 } | 226 } |
228 | 227 |
229 void RequireVersion(uint32_t version) { | 228 void RequireVersion(uint32_t version) { |
230 ConfigureProxyIfNecessary(); | 229 ConfigureProxyIfNecessary(); |
231 | 230 |
232 if (version <= version_) | 231 if (version <= version_) |
233 return; | 232 return; |
234 | 233 |
235 version_ = version; | 234 version_ = version; |
236 // Do a static cast in case the interface contains methods with the same | 235 // Do a static cast in case the interface contains methods with the same |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 router_ = new MultiplexRouter(true, std::move(handle_), runner_); | 326 router_ = new MultiplexRouter(true, std::move(handle_), runner_); |
328 router_->SetMasterInterfaceName(Interface::Name_); | 327 router_->SetMasterInterfaceName(Interface::Name_); |
329 endpoint_client_.reset(new InterfaceEndpointClient( | 328 endpoint_client_.reset(new InterfaceEndpointClient( |
330 router_->CreateLocalEndpointHandle(kMasterInterfaceId), nullptr, | 329 router_->CreateLocalEndpointHandle(kMasterInterfaceId), nullptr, |
331 base::WrapUnique(new typename Interface::ResponseValidator_()), false, | 330 base::WrapUnique(new typename Interface::ResponseValidator_()), false, |
332 std::move(runner_))); | 331 std::move(runner_))); |
333 proxy_.reset(new Proxy(endpoint_client_.get())); | 332 proxy_.reset(new Proxy(endpoint_client_.get())); |
334 proxy_->serialization_context()->router = endpoint_client_->router(); | 333 proxy_->serialization_context()->router = endpoint_client_->router(); |
335 } | 334 } |
336 | 335 |
| 336 void OnQueryVersion(const Callback<void(uint32_t)>& callback, |
| 337 uint32_t version) { |
| 338 version_ = version; |
| 339 callback.Run(version); |
| 340 } |
| 341 |
337 scoped_refptr<MultiplexRouter> router_; | 342 scoped_refptr<MultiplexRouter> router_; |
338 | 343 |
339 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; | 344 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; |
340 std::unique_ptr<Proxy> proxy_; | 345 std::unique_ptr<Proxy> proxy_; |
341 | 346 |
342 // |router_| (as well as other members above) is not initialized until | 347 // |router_| (as well as other members above) is not initialized until |
343 // 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 |
344 // between the Bind() call and the initialization of |router_|. | 349 // between the Bind() call and the initialization of |router_|. |
345 ScopedMessagePipeHandle handle_; | 350 ScopedMessagePipeHandle handle_; |
346 scoped_refptr<base::SingleThreadTaskRunner> runner_; | 351 scoped_refptr<base::SingleThreadTaskRunner> runner_; |
347 | 352 |
348 uint32_t version_; | 353 uint32_t version_; |
349 | 354 |
350 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); | 355 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); |
351 }; | 356 }; |
352 | 357 |
353 } // namespace internal | 358 } // namespace internal |
354 } // namespace mojo | 359 } // namespace mojo |
355 | 360 |
356 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_ | 361 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_ |
OLD | NEW |