| 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 State doomed; | 113 State doomed; |
| 112 internal_state_.Swap(&doomed); | 114 internal_state_.Swap(&doomed); |
| 113 } | 115 } |
| 114 | 116 |
| 115 // Blocks the current thread until the next incoming response callback arrives | 117 // Blocks the current thread until the next incoming response callback arrives |
| 116 // or an error occurs. Returns |true| if a response arrived, or |false| in | 118 // or an error occurs. Returns |true| if a response arrived, or |false| in |
| 117 // case of error. | 119 // case of error. |
| 118 // | 120 // |
| 119 // This method may only be called after the InterfacePtr has been bound to a | 121 // This method may only be called after the InterfacePtr has been bound to a |
| 120 // message pipe. | 122 // message pipe. |
| 123 // |
| 124 // NOTE: Not supported (yet) if |Interface| has methods to pass associated |
| 125 // interface pointers/requests. |
| 121 bool WaitForIncomingResponse() { | 126 bool WaitForIncomingResponse() { |
| 122 return internal_state_.WaitForIncomingResponse(); | 127 return internal_state_.WaitForIncomingResponse(); |
| 123 } | 128 } |
| 124 | 129 |
| 125 // Indicates whether the message pipe has encountered an error. If true, | 130 // Indicates whether the message pipe has encountered an error. If true, |
| 126 // method calls made on this interface will be dropped (and may already have | 131 // method calls made on this interface will be dropped (and may already have |
| 127 // been dropped). | 132 // been dropped). |
| 128 bool encountered_error() const { return internal_state_.encountered_error(); } | 133 bool encountered_error() const { return internal_state_.encountered_error(); } |
| 129 | 134 |
| 130 // Registers a handler to receive error notifications. The handler will be | 135 // Registers a handler to receive error notifications. The handler will be |
| 131 // called from the thread that owns this InterfacePtr. | 136 // called from the thread that owns this InterfacePtr. |
| 132 // | 137 // |
| 133 // This method may only be called after the InterfacePtr has been bound to a | 138 // This method may only be called after the InterfacePtr has been bound to a |
| 134 // message pipe. | 139 // message pipe. |
| 135 void set_connection_error_handler(const Closure& error_handler) { | 140 void set_connection_error_handler(const Closure& error_handler) { |
| 136 internal_state_.set_connection_error_handler(error_handler); | 141 internal_state_.set_connection_error_handler(error_handler); |
| 137 } | 142 } |
| 138 | 143 |
| 139 // Unbinds the InterfacePtr and returns the information which could be used | 144 // 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 | 145 // to setup an InterfacePtr again. This method may be used to move the proxy |
| 141 // to a different thread (see class comments for details). | 146 // to a different thread (see class comments for details). |
| 142 // | 147 // |
| 143 // It is an error to call PassInterface() while there are pending responses. | 148 // 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 | 149 // TODO: fix this restriction, it's not always obvious when there is a |
| 145 // pending response. | 150 // pending response. |
| 146 InterfacePtrInfo<Interface> PassInterface() { | 151 InterfacePtrInfo<Interface> PassInterface() { |
| 147 MOJO_DCHECK(!internal_state_.has_pending_callbacks()); | 152 DCHECK(!internal_state_.has_pending_callbacks()); |
| 148 State state; | 153 State state; |
| 149 internal_state_.Swap(&state); | 154 internal_state_.Swap(&state); |
| 150 | 155 |
| 151 return state.PassInterface(); | 156 return state.PassInterface(); |
| 152 } | 157 } |
| 153 | 158 |
| 159 // Returns the associated group that this object belongs to. Returns null if: |
| 160 // - this object is not bound; or |
| 161 // - the interface doesn't have methods to pass associated interface |
| 162 // pointers or requests. |
| 163 AssociatedGroup* associated_group() { |
| 164 return internal_state_.associated_group(); |
| 165 } |
| 166 |
| 154 // DO NOT USE. Exposed only for internal use and for testing. | 167 // DO NOT USE. Exposed only for internal use and for testing. |
| 155 internal::InterfacePtrState<Interface>* internal_state() { | 168 internal::InterfacePtrState<Interface, Interface::PassesAssociatedKinds_>* |
| 169 internal_state() { |
| 156 return &internal_state_; | 170 return &internal_state_; |
| 157 } | 171 } |
| 158 | 172 |
| 159 // Allow InterfacePtr<> to be used in boolean expressions, but not | 173 // Allow InterfacePtr<> to be used in boolean expressions, but not |
| 160 // implicitly convertible to a real bool (which is dangerous). | 174 // implicitly convertible to a real bool (which is dangerous). |
| 161 private: | 175 private: |
| 162 typedef internal::InterfacePtrState<Interface> InterfacePtr::*Testable; | 176 typedef internal::InterfacePtrState<Interface, |
| 177 Interface::PassesAssociatedKinds_> |
| 178 InterfacePtr::*Testable; |
| 163 | 179 |
| 164 public: | 180 public: |
| 165 operator Testable() const { | 181 operator Testable() const { |
| 166 return internal_state_.is_bound() ? &InterfacePtr::internal_state_ | 182 return internal_state_.is_bound() ? &InterfacePtr::internal_state_ |
| 167 : nullptr; | 183 : nullptr; |
| 168 } | 184 } |
| 169 | 185 |
| 170 private: | 186 private: |
| 171 // Forbid the == and != operators explicitly, otherwise InterfacePtr will be | 187 // Forbid the == and != operators explicitly, otherwise InterfacePtr will be |
| 172 // converted to Testable to do == or != comparison. | 188 // converted to Testable to do == or != comparison. |
| 173 template <typename T> | 189 template <typename T> |
| 174 bool operator==(const InterfacePtr<T>& other) const = delete; | 190 bool operator==(const InterfacePtr<T>& other) const = delete; |
| 175 template <typename T> | 191 template <typename T> |
| 176 bool operator!=(const InterfacePtr<T>& other) const = delete; | 192 bool operator!=(const InterfacePtr<T>& other) const = delete; |
| 177 | 193 |
| 178 typedef internal::InterfacePtrState<Interface> State; | 194 typedef internal::InterfacePtrState<Interface, |
| 195 Interface::PassesAssociatedKinds_> State; |
| 179 mutable State internal_state_; | 196 mutable State internal_state_; |
| 180 }; | 197 }; |
| 181 | 198 |
| 182 // If |info| is valid (containing a valid message pipe handle), returns an | 199 // If |info| is valid (containing a valid message pipe handle), returns an |
| 183 // InterfacePtr bound to it. Otherwise, returns an unbound InterfacePtr. The | 200 // InterfacePtr bound to it. Otherwise, returns an unbound InterfacePtr. The |
| 184 // specified |waiter| will be used as in the InterfacePtr::Bind() method. | 201 // specified |waiter| will be used as in the InterfacePtr::Bind() method. |
| 185 template <typename Interface> | 202 template <typename Interface> |
| 186 InterfacePtr<Interface> MakeProxy( | 203 InterfacePtr<Interface> MakeProxy( |
| 187 InterfacePtrInfo<Interface> info, | 204 InterfacePtrInfo<Interface> info, |
| 188 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 205 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 189 InterfacePtr<Interface> ptr; | 206 InterfacePtr<Interface> ptr; |
| 190 if (info.is_valid()) | 207 if (info.is_valid()) |
| 191 ptr.Bind(info.Pass(), waiter); | 208 ptr.Bind(info.Pass(), waiter); |
| 192 return ptr.Pass(); | 209 return ptr.Pass(); |
| 193 } | 210 } |
| 194 | 211 |
| 195 } // namespace mojo | 212 } // namespace mojo |
| 196 | 213 |
| 197 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ | 214 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
| OLD | NEW |