| 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 <algorithm> |
| 9 #include <cstddef> | 9 #include <cstddef> |
| 10 | 10 |
| 11 #include "mojo/public/cpp/bindings/callback.h" | 11 #include "mojo/public/cpp/bindings/callback.h" |
| 12 #include "mojo/public/cpp/bindings/interface_ptr_info.h" | 12 #include "mojo/public/cpp/bindings/interface_handle.h" |
| 13 #include "mojo/public/cpp/bindings/lib/interface_ptr_internal.h" | 13 #include "mojo/public/cpp/bindings/lib/interface_ptr_internal.h" |
| 14 #include "mojo/public/cpp/environment/environment.h" | 14 #include "mojo/public/cpp/environment/environment.h" |
| 15 #include "mojo/public/cpp/system/macros.h" | 15 #include "mojo/public/cpp/system/macros.h" |
| 16 | 16 |
| 17 namespace mojo { | 17 namespace mojo { |
| 18 | 18 |
| 19 // 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 |
| 20 // message pipe to communicate with the remote implementation, and automatically | 20 // message pipe to communicate with the remote implementation, and automatically |
| 21 // 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 |
| 22 // 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. |
| 23 // | 23 // |
| 24 // 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 |
| 25 // 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 |
| 26 // 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 |
| 27 // InterfacePtrInfo (containing just the message pipe and any version | 27 // InterfaceHandle (containing just the message pipe and any version |
| 28 // information) using PassInterface(), pass it to a different thread, and | 28 // information) using PassInterface(), pass it to a different thread, and |
| 29 // create and bind a new InterfacePtr from that thread. | 29 // create and bind a new InterfacePtr from that thread. |
| 30 template <typename Interface> | 30 template <typename Interface> |
| 31 class InterfacePtr { | 31 class InterfacePtr { |
| 32 public: | 32 public: |
| 33 // Constructs an unbound InterfacePtr. | 33 // Constructs an unbound InterfacePtr. |
| 34 InterfacePtr() {} | 34 InterfacePtr() {} |
| 35 InterfacePtr(std::nullptr_t) {} | 35 InterfacePtr(std::nullptr_t) {} |
| 36 | 36 |
| 37 // Takes over the binding of another InterfacePtr. | 37 // Takes over the binding of another InterfacePtr. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 59 | 59 |
| 60 // Binds the InterfacePtr to a remote implementation of Interface. The | 60 // Binds the InterfacePtr to a remote implementation of Interface. The |
| 61 // |waiter| is used for receiving notifications when there is data to read | 61 // |waiter| is used for receiving notifications when there is data to read |
| 62 // from the message pipe. For most callers, the default |waiter| will be | 62 // from the message pipe. For most callers, the default |waiter| will be |
| 63 // sufficient. | 63 // sufficient. |
| 64 // | 64 // |
| 65 // Calling with an invalid |info| (containing an invalid message pipe handle) | 65 // Calling with an invalid |info| (containing an invalid message pipe handle) |
| 66 // has the same effect as reset(). In this case, the InterfacePtr is not | 66 // has the same effect as reset(). In this case, the InterfacePtr is not |
| 67 // considered as bound. | 67 // considered as bound. |
| 68 void Bind( | 68 void Bind( |
| 69 InterfacePtrInfo<Interface> info, | 69 InterfaceHandle<Interface> info, |
| 70 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 70 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 71 reset(); | 71 reset(); |
| 72 if (info.is_valid()) | 72 if (info.is_valid()) |
| 73 internal_state_.Bind(info.Pass(), waiter); | 73 internal_state_.Bind(info.Pass(), waiter); |
| 74 } | 74 } |
| 75 | 75 |
| 76 // Returns whether or not this InterfacePtr is bound to a message pipe. | 76 // Returns whether or not this InterfacePtr is bound to a message pipe. |
| 77 bool is_bound() const { return internal_state_.is_bound(); } | 77 bool is_bound() const { return internal_state_.is_bound(); } |
| 78 | 78 |
| 79 // Returns a raw pointer to the local proxy. Caller does not take ownership. | 79 // Returns a raw pointer to the local proxy. Caller does not take ownership. |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 // | 146 // |
| 147 // This method may only be called after the InterfacePtr has been bound to a | 147 // This method may only be called after the InterfacePtr has been bound to a |
| 148 // message pipe. | 148 // message pipe. |
| 149 void set_connection_error_handler(const Closure& error_handler) { | 149 void set_connection_error_handler(const Closure& error_handler) { |
| 150 internal_state_.set_connection_error_handler(error_handler); | 150 internal_state_.set_connection_error_handler(error_handler); |
| 151 } | 151 } |
| 152 | 152 |
| 153 // Unbinds the InterfacePtr and returns the information which could be used | 153 // Unbinds the InterfacePtr and returns the information which could be used |
| 154 // to setup an InterfacePtr again. This method may be used to move the proxy | 154 // to setup an InterfacePtr again. This method may be used to move the proxy |
| 155 // to a different thread (see class comments for details). | 155 // to a different thread (see class comments for details). |
| 156 InterfacePtrInfo<Interface> PassInterface() { | 156 InterfaceHandle<Interface> PassInterface() { |
| 157 State state; | 157 State state; |
| 158 internal_state_.Swap(&state); | 158 internal_state_.Swap(&state); |
| 159 | 159 |
| 160 return state.PassInterface(); | 160 return state.PassInterface(); |
| 161 } | 161 } |
| 162 | 162 |
| 163 // DO NOT USE. Exposed only for internal use and for testing. | 163 // DO NOT USE. Exposed only for internal use and for testing. |
| 164 internal::InterfacePtrState<Interface>* internal_state() { | 164 internal::InterfacePtrState<Interface>* internal_state() { |
| 165 return &internal_state_; | 165 return &internal_state_; |
| 166 } | 166 } |
| 167 | 167 |
| 168 private: | 168 private: |
| 169 typedef internal::InterfacePtrState<Interface> State; | 169 typedef internal::InterfacePtrState<Interface> State; |
| 170 mutable State internal_state_; | 170 mutable State internal_state_; |
| 171 | 171 |
| 172 MOJO_MOVE_ONLY_TYPE(InterfacePtr); | 172 MOJO_MOVE_ONLY_TYPE(InterfacePtr); |
| 173 }; | 173 }; |
| 174 | 174 |
| 175 // If |info| is valid (containing a valid message pipe handle), returns an | 175 // If |info| is valid (containing a valid message pipe handle), returns an |
| 176 // InterfacePtr bound to it. Otherwise, returns an unbound InterfacePtr. The | 176 // InterfacePtr bound to it. Otherwise, returns an unbound InterfacePtr. The |
| 177 // specified |waiter| will be used as in the InterfacePtr::Bind() method. | 177 // specified |waiter| will be used as in the InterfacePtr::Bind() method. |
| 178 template <typename Interface> | 178 template <typename Interface> |
| 179 InterfacePtr<Interface> MakeProxy( | 179 InterfacePtr<Interface> MakeProxy( |
| 180 InterfacePtrInfo<Interface> info, | 180 InterfaceHandle<Interface> info, |
| 181 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 181 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 182 InterfacePtr<Interface> ptr; | 182 InterfacePtr<Interface> ptr; |
| 183 if (info.is_valid()) | 183 if (info.is_valid()) |
| 184 ptr.Bind(info.Pass(), waiter); | 184 ptr.Bind(info.Pass(), waiter); |
| 185 return ptr; | 185 return ptr; |
| 186 } | 186 } |
| 187 | 187 |
| 188 } // namespace mojo | 188 } // namespace mojo |
| 189 | 189 |
| 190 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ | 190 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
| OLD | NEW |