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_ASSOCIATED_INTERFACE_PTR_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_INTERFACE_PTR_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_INTERFACE_PTR_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_INTERFACE_PTR_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
16 #include "mojo/public/cpp/bindings/associated_group.h" | 16 #include "mojo/public/cpp/bindings/associated_group.h" |
17 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" | 17 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" |
18 #include "mojo/public/cpp/bindings/associated_interface_request.h" | 18 #include "mojo/public/cpp/bindings/associated_interface_request.h" |
19 #include "mojo/public/cpp/bindings/callback.h" | 19 #include "mojo/public/cpp/bindings/callback.h" |
20 #include "mojo/public/cpp/bindings/lib/associated_interface_ptr_state.h" | 20 #include "mojo/public/cpp/bindings/lib/associated_interface_ptr_state.h" |
21 | 21 |
22 namespace mojo { | 22 namespace mojo { |
23 | 23 |
24 // Represents the client side of an associated interface. It is similar to | 24 // Represents the client side of an associated interface. It is similar to |
25 // InterfacePtr, except that it doesn't own a message pipe handle. | 25 // InterfacePtr, except that it doesn't own a message pipe handle. |
26 template <typename Interface> | 26 template <typename Interface> |
27 class AssociatedInterfacePtr { | 27 class AssociatedInterfacePtr { |
| 28 DISALLOW_COPY_AND_ASSIGN_WITH_MOVE_FOR_BIND(AssociatedInterfacePtr) |
| 29 |
28 public: | 30 public: |
29 // Constructs an unbound AssociatedInterfacePtr. | 31 // Constructs an unbound AssociatedInterfacePtr. |
30 AssociatedInterfacePtr() {} | 32 AssociatedInterfacePtr() {} |
31 AssociatedInterfacePtr(decltype(nullptr)) {} | 33 AssociatedInterfacePtr(decltype(nullptr)) {} |
32 | 34 |
33 AssociatedInterfacePtr(AssociatedInterfacePtr&& other) { | 35 AssociatedInterfacePtr(AssociatedInterfacePtr&& other) { |
34 internal_state_.Swap(&other.internal_state_); | 36 internal_state_.Swap(&other.internal_state_); |
35 } | 37 } |
36 | 38 |
37 AssociatedInterfacePtr& operator=(AssociatedInterfacePtr&& other) { | 39 AssociatedInterfacePtr& operator=(AssociatedInterfacePtr&& other) { |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 } | 150 } |
149 | 151 |
150 // DO NOT USE. Exposed only for internal use and for testing. | 152 // DO NOT USE. Exposed only for internal use and for testing. |
151 internal::AssociatedInterfacePtrState<Interface>* internal_state() { | 153 internal::AssociatedInterfacePtrState<Interface>* internal_state() { |
152 return &internal_state_; | 154 return &internal_state_; |
153 } | 155 } |
154 | 156 |
155 // Allow AssociatedInterfacePtr<> to be used in boolean expressions, but not | 157 // Allow AssociatedInterfacePtr<> to be used in boolean expressions, but not |
156 // implicitly convertible to a real bool (which is dangerous). | 158 // implicitly convertible to a real bool (which is dangerous). |
157 private: | 159 private: |
158 // TODO(dcheng): Use an explicit conversion operator. | |
159 typedef internal::AssociatedInterfacePtrState<Interface> | 160 typedef internal::AssociatedInterfacePtrState<Interface> |
160 AssociatedInterfacePtr::*Testable; | 161 AssociatedInterfacePtr::*Testable; |
161 | 162 |
162 public: | 163 public: |
163 operator Testable() const { | 164 operator Testable() const { |
164 return internal_state_.is_bound() ? &AssociatedInterfacePtr::internal_state_ | 165 return internal_state_.is_bound() ? &AssociatedInterfacePtr::internal_state_ |
165 : nullptr; | 166 : nullptr; |
166 } | 167 } |
167 | 168 |
168 private: | 169 private: |
169 // Forbid the == and != operators explicitly, otherwise AssociatedInterfacePtr | 170 // Forbid the == and != operators explicitly, otherwise AssociatedInterfacePtr |
170 // will be converted to Testable to do == or != comparison. | 171 // will be converted to Testable to do == or != comparison. |
171 template <typename T> | 172 template <typename T> |
172 bool operator==(const AssociatedInterfacePtr<T>& other) const = delete; | 173 bool operator==(const AssociatedInterfacePtr<T>& other) const = delete; |
173 template <typename T> | 174 template <typename T> |
174 bool operator!=(const AssociatedInterfacePtr<T>& other) const = delete; | 175 bool operator!=(const AssociatedInterfacePtr<T>& other) const = delete; |
175 | 176 |
176 typedef internal::AssociatedInterfacePtrState<Interface> State; | 177 typedef internal::AssociatedInterfacePtrState<Interface> State; |
177 mutable State internal_state_; | 178 mutable State internal_state_; |
178 | |
179 DISALLOW_COPY_AND_ASSIGN(AssociatedInterfacePtr); | |
180 }; | 179 }; |
181 | 180 |
182 // Creates an associated interface. The output |ptr| should be used locally | 181 // Creates an associated interface. The output |ptr| should be used locally |
183 // while the returned request should be passed through the message pipe endpoint | 182 // while the returned request should be passed through the message pipe endpoint |
184 // referred to by |associated_group| to setup the corresponding asssociated | 183 // referred to by |associated_group| to setup the corresponding asssociated |
185 // interface implementation at the remote side. | 184 // interface implementation at the remote side. |
186 // | 185 // |
187 // NOTE: |ptr| should NOT be used to make calls before the request is sent. | 186 // NOTE: |ptr| should NOT be used to make calls before the request is sent. |
188 // Violating that will cause the message pipe to be closed. On the other hand, | 187 // Violating that will cause the message pipe to be closed. On the other hand, |
189 // as soon as the request is sent, |ptr| is usable. There is no need to wait | 188 // as soon as the request is sent, |ptr| is usable. There is no need to wait |
190 // until the request is bound to an implementation at the remote side. | 189 // until the request is bound to an implementation at the remote side. |
191 template <typename Interface> | 190 template <typename Interface> |
192 AssociatedInterfaceRequest<Interface> GetProxy( | 191 AssociatedInterfaceRequest<Interface> GetProxy( |
193 AssociatedInterfacePtr<Interface>* ptr, | 192 AssociatedInterfacePtr<Interface>* ptr, |
194 AssociatedGroup* group, | 193 AssociatedGroup* group, |
195 scoped_refptr<base::SingleThreadTaskRunner> runner = | 194 scoped_refptr<base::SingleThreadTaskRunner> runner = |
196 base::ThreadTaskRunnerHandle::Get()) { | 195 base::ThreadTaskRunnerHandle::Get()) { |
197 AssociatedInterfaceRequest<Interface> request; | 196 AssociatedInterfaceRequest<Interface> request; |
198 AssociatedInterfacePtrInfo<Interface> ptr_info; | 197 AssociatedInterfacePtrInfo<Interface> ptr_info; |
199 group->CreateAssociatedInterface(AssociatedGroup::WILL_PASS_REQUEST, | 198 group->CreateAssociatedInterface(AssociatedGroup::WILL_PASS_REQUEST, |
200 &ptr_info, &request); | 199 &ptr_info, &request); |
201 | 200 |
202 ptr->Bind(std::move(ptr_info), std::move(runner)); | 201 ptr->Bind(std::move(ptr_info), std::move(runner)); |
203 return request; | 202 return request; |
204 } | 203 } |
205 | 204 |
206 } // namespace mojo | 205 } // namespace mojo |
207 | 206 |
208 #endif // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_INTERFACE_PTR_H_ | 207 #endif // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_INTERFACE_PTR_H_ |
OLD | NEW |