| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_ASSOCIATED_INTERFACE_PTR_STATE_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ASSOCIATED_INTERFACE_PTR_STATE_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ASSOCIATED_INTERFACE_PTR_STATE_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ASSOCIATED_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/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 20 #include "mojo/public/cpp/bindings/associated_group.h" | 20 #include "mojo/public/cpp/bindings/associated_group.h" |
| 21 #include "mojo/public/cpp/bindings/associated_group_controller.h" | 21 #include "mojo/public/cpp/bindings/associated_group_controller.h" |
| 22 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" | 22 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.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/lib/control_message_handler.h" |
| 25 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" | 26 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" |
| 26 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" | 27 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
| 27 #include "mojo/public/cpp/system/message_pipe.h" | 28 #include "mojo/public/cpp/system/message_pipe.h" |
| 28 | 29 |
| 29 namespace mojo { | 30 namespace mojo { |
| 30 namespace internal { | 31 namespace internal { |
| 31 | 32 |
| 32 template <typename Interface> | 33 template <typename Interface> |
| 33 class AssociatedInterfacePtrState { | 34 class AssociatedInterfacePtrState { |
| 34 public: | 35 public: |
| (...skipping 10 matching lines...) Expand all Loading... |
| 45 } | 46 } |
| 46 | 47 |
| 47 uint32_t version() const { return version_; } | 48 uint32_t version() const { return version_; } |
| 48 | 49 |
| 49 uint32_t interface_id() const { | 50 uint32_t interface_id() const { |
| 50 DCHECK(is_bound()); | 51 DCHECK(is_bound()); |
| 51 return endpoint_client_->interface_id(); | 52 return endpoint_client_->interface_id(); |
| 52 } | 53 } |
| 53 | 54 |
| 54 void QueryVersion(const base::Callback<void(uint32_t)>& callback) { | 55 void QueryVersion(const base::Callback<void(uint32_t)>& callback) { |
| 55 // Do a static cast in case the interface contains methods with the same | 56 // It is safe to capture |this| because the callback won't be run after this |
| 56 // name. It is safe to capture |this| because the callback won't be run | 57 // object goes away. |
| 57 // after this object goes away. | 58 endpoint_client_->control_message_proxy()->QueryVersion( |
| 58 static_cast<ControlMessageProxy*>(proxy_.get()) | 59 base::Bind(&AssociatedInterfacePtrState::OnQueryVersion, |
| 59 ->QueryVersion(base::Bind(&AssociatedInterfacePtrState::OnQueryVersion, | 60 base::Unretained(this), callback)); |
| 60 base::Unretained(this), callback)); | |
| 61 } | 61 } |
| 62 | 62 |
| 63 void RequireVersion(uint32_t version) { | 63 void RequireVersion(uint32_t version) { |
| 64 if (version <= version_) | 64 if (version <= version_) |
| 65 return; | 65 return; |
| 66 | 66 |
| 67 version_ = version; | 67 version_ = version; |
| 68 // Do a static cast in case the interface contains methods with the same | 68 endpoint_client_->control_message_proxy()->RequireVersion(version); |
| 69 // name. | 69 } |
| 70 static_cast<ControlMessageProxy*>(proxy_.get())->RequireVersion(version); | 70 |
| 71 void FlushForTesting() { |
| 72 endpoint_client_->control_message_proxy()->FlushForTesting(); |
| 71 } | 73 } |
| 72 | 74 |
| 73 void Swap(AssociatedInterfacePtrState* other) { | 75 void Swap(AssociatedInterfacePtrState* other) { |
| 74 using std::swap; | 76 using std::swap; |
| 75 swap(other->endpoint_client_, endpoint_client_); | 77 swap(other->endpoint_client_, endpoint_client_); |
| 76 swap(other->proxy_, proxy_); | 78 swap(other->proxy_, proxy_); |
| 77 swap(other->version_, version_); | 79 swap(other->version_, version_); |
| 78 } | 80 } |
| 79 | 81 |
| 80 void Bind(AssociatedInterfacePtrInfo<Interface> info, | 82 void Bind(AssociatedInterfacePtrInfo<Interface> info, |
| 81 scoped_refptr<base::SingleThreadTaskRunner> runner) { | 83 scoped_refptr<base::SingleThreadTaskRunner> runner) { |
| 82 DCHECK(!endpoint_client_); | 84 DCHECK(!endpoint_client_); |
| 83 DCHECK(!proxy_); | 85 DCHECK(!proxy_); |
| 84 DCHECK_EQ(0u, version_); | 86 DCHECK_EQ(0u, version_); |
| 85 DCHECK(info.is_valid()); | 87 DCHECK(info.is_valid()); |
| 86 | 88 |
| 87 version_ = info.version(); | 89 version_ = info.version(); |
| 90 // The version is only queried from the client so the value passed here |
| 91 // will not be used. |
| 88 endpoint_client_.reset(new InterfaceEndpointClient( | 92 endpoint_client_.reset(new InterfaceEndpointClient( |
| 89 info.PassHandle(), nullptr, | 93 info.PassHandle(), nullptr, |
| 90 base::WrapUnique(new typename Interface::ResponseValidator_()), false, | 94 base::WrapUnique(new typename Interface::ResponseValidator_()), false, |
| 91 std::move(runner))); | 95 std::move(runner), 0u)); |
| 92 proxy_.reset(new Proxy(endpoint_client_.get())); | 96 proxy_.reset(new Proxy(endpoint_client_.get())); |
| 93 proxy_->serialization_context()->group_controller = | 97 proxy_->serialization_context()->group_controller = |
| 94 endpoint_client_->group_controller(); | 98 endpoint_client_->group_controller(); |
| 95 } | 99 } |
| 96 | 100 |
| 97 // After this method is called, the object is in an invalid state and | 101 // After this method is called, the object is in an invalid state and |
| 98 // shouldn't be reused. | 102 // shouldn't be reused. |
| 99 AssociatedInterfacePtrInfo<Interface> PassInterface() { | 103 AssociatedInterfacePtrInfo<Interface> PassInterface() { |
| 100 ScopedInterfaceEndpointHandle handle = endpoint_client_->PassHandle(); | 104 ScopedInterfaceEndpointHandle handle = endpoint_client_->PassHandle(); |
| 101 endpoint_client_.reset(); | 105 endpoint_client_.reset(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 | 141 |
| 138 uint32_t version_; | 142 uint32_t version_; |
| 139 | 143 |
| 140 DISALLOW_COPY_AND_ASSIGN(AssociatedInterfacePtrState); | 144 DISALLOW_COPY_AND_ASSIGN(AssociatedInterfacePtrState); |
| 141 }; | 145 }; |
| 142 | 146 |
| 143 } // namespace internal | 147 } // namespace internal |
| 144 } // namespace mojo | 148 } // namespace mojo |
| 145 | 149 |
| 146 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ASSOCIATED_INTERFACE_PTR_STATE_H_ | 150 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ASSOCIATED_INTERFACE_PTR_STATE_H_ |
| OLD | NEW |