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