| 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 |
| 162 // The version is only queried from the client so the value passed here |
| 163 // will not be used. |
| 160 router_ = new Router(std::move(handle_), std::move(filters), false, | 164 router_ = new Router(std::move(handle_), std::move(filters), false, |
| 161 std::move(runner_)); | 165 std::move(runner_), 0u); |
| 162 | 166 |
| 163 proxy_ = new Proxy(router_); | 167 proxy_ = new Proxy(router_); |
| 164 } | 168 } |
| 165 | 169 |
| 166 void OnQueryVersion(const base::Callback<void(uint32_t)>& callback, | 170 void OnQueryVersion(const base::Callback<void(uint32_t)>& callback, |
| 167 uint32_t version) { | 171 uint32_t version) { |
| 168 version_ = version; | 172 version_ = version; |
| 169 callback.Run(version); | 173 callback.Run(version); |
| 170 } | 174 } |
| 171 | 175 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 202 | 206 |
| 203 // This will be null if the object is not bound. | 207 // This will be null if the object is not bound. |
| 204 return proxy_.get(); | 208 return proxy_.get(); |
| 205 } | 209 } |
| 206 | 210 |
| 207 uint32_t version() const { return version_; } | 211 uint32_t version() const { return version_; } |
| 208 | 212 |
| 209 void QueryVersion(const base::Callback<void(uint32_t)>& callback) { | 213 void QueryVersion(const base::Callback<void(uint32_t)>& callback) { |
| 210 ConfigureProxyIfNecessary(); | 214 ConfigureProxyIfNecessary(); |
| 211 | 215 |
| 212 | 216 // 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 | 217 // object goes away. |
| 214 // name. It is safe to capture |this| because the callback won't be run | 218 endpoint_client_->control_message_proxy()->QueryVersion(base::Bind( |
| 215 // after this object goes away. | 219 &InterfacePtrState::OnQueryVersion, base::Unretained(this), callback)); |
| 216 static_cast<ControlMessageProxy*>(proxy_.get())->QueryVersion( | |
| 217 base::Bind(&InterfacePtrState::OnQueryVersion, base::Unretained(this), | |
| 218 callback)); | |
| 219 } | 220 } |
| 220 | 221 |
| 221 void RequireVersion(uint32_t version) { | 222 void RequireVersion(uint32_t version) { |
| 222 ConfigureProxyIfNecessary(); | 223 ConfigureProxyIfNecessary(); |
| 223 | 224 |
| 224 if (version <= version_) | 225 if (version <= version_) |
| 225 return; | 226 return; |
| 226 | 227 |
| 227 version_ = version; | 228 version_ = version; |
| 228 // Do a static cast in case the interface contains methods with the same | 229 endpoint_client_->control_message_proxy()->RequireVersion(version); |
| 229 // name. | 230 } |
| 230 static_cast<ControlMessageProxy*>(proxy_.get())->RequireVersion(version); | 231 |
| 232 void FlushForTesting() { |
| 233 ConfigureProxyIfNecessary(); |
| 234 endpoint_client_->control_message_proxy()->FlushForTesting(); |
| 231 } | 235 } |
| 232 | 236 |
| 233 void Swap(InterfacePtrState* other) { | 237 void Swap(InterfacePtrState* other) { |
| 234 using std::swap; | 238 using std::swap; |
| 235 swap(other->router_, router_); | 239 swap(other->router_, router_); |
| 236 swap(other->endpoint_client_, endpoint_client_); | 240 swap(other->endpoint_client_, endpoint_client_); |
| 237 swap(other->proxy_, proxy_); | 241 swap(other->proxy_, proxy_); |
| 238 handle_.swap(other->handle_); | 242 handle_.swap(other->handle_); |
| 239 runner_.swap(other->runner_); | 243 runner_.swap(other->runner_); |
| 240 swap(other->version_, version_); | 244 swap(other->version_, version_); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 } | 311 } |
| 308 // The object hasn't been bound. | 312 // The object hasn't been bound. |
| 309 if (!handle_.is_valid()) | 313 if (!handle_.is_valid()) |
| 310 return; | 314 return; |
| 311 | 315 |
| 312 router_ = new MultiplexRouter(true, std::move(handle_), runner_); | 316 router_ = new MultiplexRouter(true, std::move(handle_), runner_); |
| 313 router_->SetMasterInterfaceName(Interface::Name_); | 317 router_->SetMasterInterfaceName(Interface::Name_); |
| 314 endpoint_client_.reset(new InterfaceEndpointClient( | 318 endpoint_client_.reset(new InterfaceEndpointClient( |
| 315 router_->CreateLocalEndpointHandle(kMasterInterfaceId), nullptr, | 319 router_->CreateLocalEndpointHandle(kMasterInterfaceId), nullptr, |
| 316 base::WrapUnique(new typename Interface::ResponseValidator_()), false, | 320 base::WrapUnique(new typename Interface::ResponseValidator_()), false, |
| 317 std::move(runner_))); | 321 std::move(runner_), |
| 322 // The version is only queried from the client so the value passed here |
| 323 // will not be used. |
| 324 0u)); |
| 318 proxy_.reset(new Proxy(endpoint_client_.get())); | 325 proxy_.reset(new Proxy(endpoint_client_.get())); |
| 319 proxy_->serialization_context()->group_controller = | 326 proxy_->serialization_context()->group_controller = |
| 320 endpoint_client_->group_controller(); | 327 endpoint_client_->group_controller(); |
| 321 } | 328 } |
| 322 | 329 |
| 323 void OnQueryVersion(const base::Callback<void(uint32_t)>& callback, | 330 void OnQueryVersion(const base::Callback<void(uint32_t)>& callback, |
| 324 uint32_t version) { | 331 uint32_t version) { |
| 325 version_ = version; | 332 version_ = version; |
| 326 callback.Run(version); | 333 callback.Run(version); |
| 327 } | 334 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 339 | 346 |
| 340 uint32_t version_; | 347 uint32_t version_; |
| 341 | 348 |
| 342 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); | 349 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); |
| 343 }; | 350 }; |
| 344 | 351 |
| 345 } // namespace internal | 352 } // namespace internal |
| 346 } // namespace mojo | 353 } // namespace mojo |
| 347 | 354 |
| 348 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_ | 355 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_ |
| OLD | NEW |