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/macros.h" | 15 #include "base/macros.h" |
15 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
17 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
18 #include "mojo/public/cpp/bindings/associated_group.h" | 19 #include "mojo/public/cpp/bindings/associated_group.h" |
19 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" | 20 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" |
20 #include "mojo/public/cpp/bindings/callback.h" | 21 #include "mojo/public/cpp/bindings/callback.h" |
21 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" | 22 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" |
22 #include "mojo/public/cpp/bindings/lib/interface_endpoint_client.h" | 23 #include "mojo/public/cpp/bindings/lib/interface_endpoint_client.h" |
23 #include "mojo/public/cpp/bindings/lib/interface_id.h" | 24 #include "mojo/public/cpp/bindings/lib/interface_id.h" |
(...skipping 20 matching lines...) Expand all Loading... |
44 } | 45 } |
45 | 46 |
46 uint32_t version() const { return version_; } | 47 uint32_t version() const { return version_; } |
47 | 48 |
48 uint32_t interface_id() const { | 49 uint32_t interface_id() const { |
49 DCHECK(is_bound()); | 50 DCHECK(is_bound()); |
50 return endpoint_client_->interface_id(); | 51 return endpoint_client_->interface_id(); |
51 } | 52 } |
52 | 53 |
53 void QueryVersion(const Callback<void(uint32_t)>& callback) { | 54 void QueryVersion(const Callback<void(uint32_t)>& callback) { |
54 // It is safe to capture |this| because the callback won't be run after this | |
55 // object goes away. | |
56 auto callback_wrapper = [this, callback](uint32_t version) { | |
57 this->version_ = version; | |
58 callback.Run(version); | |
59 }; | |
60 | |
61 // Do a static cast in case the interface contains methods with the same | 55 // Do a static cast in case the interface contains methods with the same |
62 // name. | 56 // name. It is safe to capture |this| because the callback won't be run |
| 57 // after this object goes away. |
63 static_cast<ControlMessageProxy*>(proxy_.get()) | 58 static_cast<ControlMessageProxy*>(proxy_.get()) |
64 ->QueryVersion(callback_wrapper); | 59 ->QueryVersion(base::Bind(&AssociatedInterfacePtrState::OnQueryVersion, |
| 60 base::Unretained(this), callback)); |
65 } | 61 } |
66 | 62 |
67 void RequireVersion(uint32_t version) { | 63 void RequireVersion(uint32_t version) { |
68 if (version <= version_) | 64 if (version <= version_) |
69 return; | 65 return; |
70 | 66 |
71 version_ = version; | 67 version_ = version; |
72 // Do a static cast in case the interface contains methods with the same | 68 // Do a static cast in case the interface contains methods with the same |
73 // name. | 69 // name. |
74 static_cast<ControlMessageProxy*>(proxy_.get())->RequireVersion(version); | 70 static_cast<ControlMessageProxy*>(proxy_.get())->RequireVersion(version); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 return endpoint_client_ && endpoint_client_->has_pending_responders(); | 118 return endpoint_client_ && endpoint_client_->has_pending_responders(); |
123 } | 119 } |
124 | 120 |
125 AssociatedGroup* associated_group() { | 121 AssociatedGroup* associated_group() { |
126 return endpoint_client_ ? endpoint_client_->associated_group() : nullptr; | 122 return endpoint_client_ ? endpoint_client_->associated_group() : nullptr; |
127 } | 123 } |
128 | 124 |
129 private: | 125 private: |
130 using Proxy = typename Interface::Proxy_; | 126 using Proxy = typename Interface::Proxy_; |
131 | 127 |
| 128 void OnQueryVersion(const Callback<void(uint32_t)>& callback, |
| 129 uint32_t version) { |
| 130 version_ = version; |
| 131 callback.Run(version); |
| 132 } |
| 133 |
132 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; | 134 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; |
133 std::unique_ptr<Proxy> proxy_; | 135 std::unique_ptr<Proxy> proxy_; |
134 | 136 |
135 uint32_t version_; | 137 uint32_t version_; |
136 | 138 |
137 DISALLOW_COPY_AND_ASSIGN(AssociatedInterfacePtrState); | 139 DISALLOW_COPY_AND_ASSIGN(AssociatedInterfacePtrState); |
138 }; | 140 }; |
139 | 141 |
140 } // namespace internal | 142 } // namespace internal |
141 } // namespace mojo | 143 } // namespace mojo |
142 | 144 |
143 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ASSOCIATED_INTERFACE_PTR_STATE_H_ | 145 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ASSOCIATED_INTERFACE_PTR_STATE_H_ |
OLD | NEW |