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 | 9 |
10 #include "mojo/public/cpp/bindings/error_handler.h" | 10 #include "mojo/public/cpp/bindings/error_handler.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 InterfacePtrInfo<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 // Similar to the previous method, but takes a message pipe handle as input. | |
77 // | |
78 // TODO(yzshen): Remove this method and change call sites to use the other | |
79 // Bind(). | |
80 void Bind( | |
81 ScopedMessagePipeHandle handle, | |
82 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
83 Bind(InterfacePtrInfo<Interface>(handle.Pass(), 0u), waiter); | |
84 } | |
85 | |
86 // Returns a raw pointer to the local proxy. Caller does not take ownership. | 76 // Returns a raw pointer to the local proxy. Caller does not take ownership. |
87 // Note that the local proxy is thread hostile, as stated above. | 77 // Note that the local proxy is thread hostile, as stated above. |
88 Interface* get() const { return internal_state_.instance(); } | 78 Interface* get() const { return internal_state_.instance(); } |
89 | 79 |
90 // Functions like a pointer to Interface. Must already be bound. | 80 // Functions like a pointer to Interface. Must already be bound. |
91 Interface* operator->() const { return get(); } | 81 Interface* operator->() const { return get(); } |
92 Interface& operator*() const { return *get(); } | 82 Interface& operator*() const { return *get(); } |
93 | 83 |
94 // Returns the version number of the interface that the remote side supports. | 84 // Returns the version number of the interface that the remote side supports. |
95 uint32_t version() const { return internal_state_.version(); } | 85 uint32_t version() const { return internal_state_.version(); } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 // Unbinds the InterfacePtr and returns the information which could be used | 138 // Unbinds the InterfacePtr and returns the information which could be used |
149 // to setup an InterfacePtr again. This method may be used to move the proxy | 139 // to setup an InterfacePtr again. This method may be used to move the proxy |
150 // to a different thread (see class comments for details). | 140 // to a different thread (see class comments for details). |
151 InterfacePtrInfo<Interface> PassInterface() { | 141 InterfacePtrInfo<Interface> PassInterface() { |
152 State state; | 142 State state; |
153 internal_state_.Swap(&state); | 143 internal_state_.Swap(&state); |
154 | 144 |
155 return state.PassInterface(); | 145 return state.PassInterface(); |
156 } | 146 } |
157 | 147 |
158 // Similar to the previous method but returns the previously bound message | |
159 // pipe (if any). | |
160 // | |
161 // TODO(yzshen): Remove this method and change call sites to use | |
162 // PassInterface(). | |
163 ScopedMessagePipeHandle PassMessagePipe() { | |
164 return PassInterface().PassHandle(); | |
165 } | |
166 | |
167 // DO NOT USE. Exposed only for internal use and for testing. | 148 // DO NOT USE. Exposed only for internal use and for testing. |
168 internal::InterfacePtrState<Interface>* internal_state() { | 149 internal::InterfacePtrState<Interface>* internal_state() { |
169 return &internal_state_; | 150 return &internal_state_; |
170 } | 151 } |
171 | 152 |
172 // Allow InterfacePtr<> to be used in boolean expressions, but not | 153 // Allow InterfacePtr<> to be used in boolean expressions, but not |
173 // implicitly convertible to a real bool (which is dangerous). | 154 // implicitly convertible to a real bool (which is dangerous). |
174 private: | 155 private: |
175 typedef internal::InterfacePtrState<Interface> InterfacePtr::*Testable; | 156 typedef internal::InterfacePtrState<Interface> InterfacePtr::*Testable; |
176 | 157 |
177 public: | 158 public: |
178 operator Testable() const { | 159 operator Testable() const { |
179 return internal_state_.is_bound() ? &InterfacePtr::internal_state_ | 160 return internal_state_.is_bound() ? &InterfacePtr::internal_state_ |
180 : nullptr; | 161 : nullptr; |
181 } | 162 } |
182 | 163 |
183 private: | 164 private: |
184 typedef internal::InterfacePtrState<Interface> State; | 165 typedef internal::InterfacePtrState<Interface> State; |
185 mutable State internal_state_; | 166 mutable State internal_state_; |
186 }; | 167 }; |
187 | 168 |
188 // If the specified message pipe handle is valid, returns an InterfacePtr bound | 169 // If |info| is valid (containing a valid message pipe handle), returns an |
189 // to it. Otherwise, returns an unbound InterfacePtr. The specified |waiter| | 170 // InterfacePtr bound to it. Otherwise, returns an unbound InterfacePtr. The |
190 // will be used as in the InterfacePtr::Bind() method. | 171 // specified |waiter| will be used as in the InterfacePtr::Bind() method. |
191 // | |
192 // TODO(yzshen): Either remove it or change to use InterfacePtrInfo as the first | |
193 // parameter. | |
194 template <typename Interface> | 172 template <typename Interface> |
195 InterfacePtr<Interface> MakeProxy( | 173 InterfacePtr<Interface> MakeProxy( |
196 ScopedMessagePipeHandle handle, | 174 InterfacePtrInfo<Interface> info, |
197 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 175 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
198 InterfacePtr<Interface> ptr; | 176 InterfacePtr<Interface> ptr; |
199 if (handle.is_valid()) | 177 if (info.is_valid()) |
200 ptr.Bind(handle.Pass(), waiter); | 178 ptr.Bind(info.Pass(), waiter); |
201 return ptr.Pass(); | 179 return ptr.Pass(); |
202 } | 180 } |
203 | 181 |
204 } // namespace mojo | 182 } // namespace mojo |
205 | 183 |
206 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ | 184 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
OLD | NEW |