| 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_INTERFACE_PTR_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include "base/logging.h" |
| 9 | 9 #include "base/macros.h" |
| 10 #include "mojo/public/cpp/bindings/callback.h" | 10 #include "mojo/public/cpp/bindings/callback.h" |
| 11 #include "mojo/public/cpp/bindings/interface_ptr_info.h" | 11 #include "mojo/public/cpp/bindings/interface_ptr_info.h" |
| 12 #include "mojo/public/cpp/bindings/lib/interface_ptr_internal.h" | 12 #include "mojo/public/cpp/bindings/lib/interface_ptr_state.h" |
| 13 #include "mojo/public/cpp/environment/environment.h" | 13 #include "mojo/public/cpp/environment/environment.h" |
| 14 #include "mojo/public/cpp/system/macros.h" | |
| 15 | 14 |
| 16 namespace mojo { | 15 namespace mojo { |
| 17 | 16 |
| 17 class AssociatedGroup; |
| 18 |
| 18 // A pointer to a local proxy of a remote Interface implementation. Uses a | 19 // A pointer to a local proxy of a remote Interface implementation. Uses a |
| 19 // message pipe to communicate with the remote implementation, and automatically | 20 // message pipe to communicate with the remote implementation, and automatically |
| 20 // closes the pipe and deletes the proxy on destruction. The pointer must be | 21 // closes the pipe and deletes the proxy on destruction. The pointer must be |
| 21 // bound to a message pipe before the interface methods can be called. | 22 // bound to a message pipe before the interface methods can be called. |
| 22 // | 23 // |
| 23 // This class is thread hostile, as is the local proxy it manages. All calls to | 24 // This class is thread hostile, as is the local proxy it manages. All calls to |
| 24 // this class or the proxy should be from the same thread that created it. If | 25 // this class or the proxy should be from the same thread that created it. If |
| 25 // you need to move the proxy to a different thread, extract the | 26 // you need to move the proxy to a different thread, extract the |
| 26 // InterfacePtrInfo (containing just the message pipe and any version | 27 // InterfacePtrInfo (containing just the message pipe and any version |
| 27 // information) using PassInterface(), pass it to a different thread, and | 28 // information) using PassInterface(), pass it to a different thread, and |
| 28 // create and bind a new InterfacePtr from that thread. | 29 // create and bind a new InterfacePtr from that thread. |
| 29 template <typename Interface> | 30 template <typename Interface> |
| 30 class InterfacePtr { | 31 class InterfacePtr { |
| 31 MOJO_MOVE_ONLY_TYPE(InterfacePtr) | 32 MOVE_ONLY_TYPE_WITH_MOVE_CONSTRUCTOR_FOR_CPP_03(InterfacePtr) |
| 33 |
| 32 public: | 34 public: |
| 33 // Constructs an unbound InterfacePtr. | 35 // Constructs an unbound InterfacePtr. |
| 34 InterfacePtr() {} | 36 InterfacePtr() {} |
| 35 InterfacePtr(decltype(nullptr)) {} | 37 InterfacePtr(decltype(nullptr)) {} |
| 36 | 38 |
| 37 // Takes over the binding of another InterfacePtr. | 39 // Takes over the binding of another InterfacePtr. |
| 38 InterfacePtr(InterfacePtr&& other) { | 40 InterfacePtr(InterfacePtr&& other) { |
| 39 internal_state_.Swap(&other.internal_state_); | 41 internal_state_.Swap(&other.internal_state_); |
| 40 } | 42 } |
| 41 | 43 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 } | 139 } |
| 138 | 140 |
| 139 // Unbinds the InterfacePtr and returns the information which could be used | 141 // Unbinds the InterfacePtr and returns the information which could be used |
| 140 // to setup an InterfacePtr again. This method may be used to move the proxy | 142 // to setup an InterfacePtr again. This method may be used to move the proxy |
| 141 // to a different thread (see class comments for details). | 143 // to a different thread (see class comments for details). |
| 142 // | 144 // |
| 143 // It is an error to call PassInterface() while there are pending responses. | 145 // It is an error to call PassInterface() while there are pending responses. |
| 144 // TODO: fix this restriction, it's not always obvious when there is a | 146 // TODO: fix this restriction, it's not always obvious when there is a |
| 145 // pending response. | 147 // pending response. |
| 146 InterfacePtrInfo<Interface> PassInterface() { | 148 InterfacePtrInfo<Interface> PassInterface() { |
| 147 MOJO_DCHECK(!internal_state_.has_pending_callbacks()); | 149 DCHECK(!internal_state_.has_pending_callbacks()); |
| 148 State state; | 150 State state; |
| 149 internal_state_.Swap(&state); | 151 internal_state_.Swap(&state); |
| 150 | 152 |
| 151 return state.PassInterface(); | 153 return state.PassInterface(); |
| 152 } | 154 } |
| 153 | 155 |
| 156 // Returns the associated group that this object belongs to. Returns null if: |
| 157 // - this object is not bound; or |
| 158 // - the interface doesn't have methods to pass associated interface |
| 159 // pointers or requests. |
| 160 AssociatedGroup* associated_group() { |
| 161 return internal_state_.associated_group(); |
| 162 } |
| 163 |
| 154 // DO NOT USE. Exposed only for internal use and for testing. | 164 // DO NOT USE. Exposed only for internal use and for testing. |
| 155 internal::InterfacePtrState<Interface>* internal_state() { | 165 internal::InterfacePtrState<Interface, Interface::PassesAssociatedKinds_>* |
| 166 internal_state() { |
| 156 return &internal_state_; | 167 return &internal_state_; |
| 157 } | 168 } |
| 158 | 169 |
| 159 // Allow InterfacePtr<> to be used in boolean expressions, but not | 170 // Allow InterfacePtr<> to be used in boolean expressions, but not |
| 160 // implicitly convertible to a real bool (which is dangerous). | 171 // implicitly convertible to a real bool (which is dangerous). |
| 161 private: | 172 private: |
| 162 typedef internal::InterfacePtrState<Interface> InterfacePtr::*Testable; | 173 typedef internal::InterfacePtrState<Interface, |
| 174 Interface::PassesAssociatedKinds_> |
| 175 InterfacePtr::*Testable; |
| 163 | 176 |
| 164 public: | 177 public: |
| 165 operator Testable() const { | 178 operator Testable() const { |
| 166 return internal_state_.is_bound() ? &InterfacePtr::internal_state_ | 179 return internal_state_.is_bound() ? &InterfacePtr::internal_state_ |
| 167 : nullptr; | 180 : nullptr; |
| 168 } | 181 } |
| 169 | 182 |
| 170 private: | 183 private: |
| 171 // Forbid the == and != operators explicitly, otherwise InterfacePtr will be | 184 // Forbid the == and != operators explicitly, otherwise InterfacePtr will be |
| 172 // converted to Testable to do == or != comparison. | 185 // converted to Testable to do == or != comparison. |
| 173 template <typename T> | 186 template <typename T> |
| 174 bool operator==(const InterfacePtr<T>& other) const = delete; | 187 bool operator==(const InterfacePtr<T>& other) const = delete; |
| 175 template <typename T> | 188 template <typename T> |
| 176 bool operator!=(const InterfacePtr<T>& other) const = delete; | 189 bool operator!=(const InterfacePtr<T>& other) const = delete; |
| 177 | 190 |
| 178 typedef internal::InterfacePtrState<Interface> State; | 191 typedef internal::InterfacePtrState<Interface, |
| 192 Interface::PassesAssociatedKinds_> State; |
| 179 mutable State internal_state_; | 193 mutable State internal_state_; |
| 180 }; | 194 }; |
| 181 | 195 |
| 182 // If |info| is valid (containing a valid message pipe handle), returns an | 196 // If |info| is valid (containing a valid message pipe handle), returns an |
| 183 // InterfacePtr bound to it. Otherwise, returns an unbound InterfacePtr. The | 197 // InterfacePtr bound to it. Otherwise, returns an unbound InterfacePtr. The |
| 184 // specified |waiter| will be used as in the InterfacePtr::Bind() method. | 198 // specified |waiter| will be used as in the InterfacePtr::Bind() method. |
| 185 template <typename Interface> | 199 template <typename Interface> |
| 186 InterfacePtr<Interface> MakeProxy( | 200 InterfacePtr<Interface> MakeProxy( |
| 187 InterfacePtrInfo<Interface> info, | 201 InterfacePtrInfo<Interface> info, |
| 188 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 202 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 189 InterfacePtr<Interface> ptr; | 203 InterfacePtr<Interface> ptr; |
| 190 if (info.is_valid()) | 204 if (info.is_valid()) |
| 191 ptr.Bind(info.Pass(), waiter); | 205 ptr.Bind(info.Pass(), waiter); |
| 192 return ptr.Pass(); | 206 return ptr.Pass(); |
| 193 } | 207 } |
| 194 | 208 |
| 195 } // namespace mojo | 209 } // namespace mojo |
| 196 | 210 |
| 197 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ | 211 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
| OLD | NEW |