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 "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "mojo/public/cpp/bindings/callback.h" | 10 #include "mojo/public/cpp/bindings/callback.h" |
(...skipping 14 matching lines...) Expand all Loading... |
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 // InterfacePtrInfo (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 DISALLOW_COPY_AND_ASSIGN_WITH_MOVE_FOR_BIND(InterfacePtr) | 32 DISALLOW_COPY_AND_ASSIGN_WITH_MOVE_FOR_BIND(InterfacePtr) |
33 | 33 |
34 public: | 34 public: |
| 35 using GenericInterface = typename Interface::GenericInterface; |
| 36 |
35 // Constructs an unbound InterfacePtr. | 37 // Constructs an unbound InterfacePtr. |
36 InterfacePtr() {} | 38 InterfacePtr() {} |
37 InterfacePtr(decltype(nullptr)) {} | 39 InterfacePtr(decltype(nullptr)) {} |
38 | 40 |
39 // Takes over the binding of another InterfacePtr. | 41 // Takes over the binding of another InterfacePtr. |
40 InterfacePtr(InterfacePtr&& other) { | 42 InterfacePtr(InterfacePtr&& other) { |
41 internal_state_.Swap(&other.internal_state_); | 43 internal_state_.Swap(&other.internal_state_); |
42 } | 44 } |
43 | 45 |
44 // Takes over the binding of another InterfacePtr, and closes any message pipe | 46 // Takes over the binding of another InterfacePtr, and closes any message pipe |
(...skipping 16 matching lines...) Expand all Loading... |
61 | 63 |
62 // Binds the InterfacePtr to a remote implementation of Interface. The | 64 // Binds the InterfacePtr to a remote implementation of Interface. The |
63 // |waiter| is used for receiving notifications when there is data to read | 65 // |waiter| is used for receiving notifications when there is data to read |
64 // from the message pipe. For most callers, the default |waiter| will be | 66 // from the message pipe. For most callers, the default |waiter| will be |
65 // sufficient. | 67 // sufficient. |
66 // | 68 // |
67 // Calling with an invalid |info| (containing an invalid message pipe handle) | 69 // Calling with an invalid |info| (containing an invalid message pipe handle) |
68 // has the same effect as reset(). In this case, the InterfacePtr is not | 70 // has the same effect as reset(). In this case, the InterfacePtr is not |
69 // considered as bound. | 71 // considered as bound. |
70 void Bind( | 72 void Bind( |
71 InterfacePtrInfo<Interface> info, | 73 InterfacePtrInfo<GenericInterface> info, |
72 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 74 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
73 reset(); | 75 reset(); |
74 if (info.is_valid()) | 76 if (info.is_valid()) |
75 internal_state_.Bind(info.Pass(), waiter); | 77 internal_state_.Bind(info.Pass(), waiter); |
76 } | 78 } |
77 | 79 |
78 // Returns whether or not this InterfacePtr is bound to a message pipe. | 80 // Returns whether or not this InterfacePtr is bound to a message pipe. |
79 bool is_bound() const { return internal_state_.is_bound(); } | 81 bool is_bound() const { return internal_state_.is_bound(); } |
80 | 82 |
81 // Returns a raw pointer to the local proxy. Caller does not take ownership. | 83 // Returns a raw pointer to the local proxy. Caller does not take ownership. |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 // | 152 // |
151 // It is an error to call PassInterface() while: | 153 // It is an error to call PassInterface() while: |
152 // - there are pending responses; or | 154 // - there are pending responses; or |
153 // TODO: fix this restriction, it's not always obvious when there is a | 155 // TODO: fix this restriction, it's not always obvious when there is a |
154 // pending response. | 156 // pending response. |
155 // - there are associated interfaces running. | 157 // - there are associated interfaces running. |
156 // TODO(yzshen): For now, users need to make sure there is no one holding | 158 // TODO(yzshen): For now, users need to make sure there is no one holding |
157 // on to associated interface endpoint handles at both sides of the | 159 // on to associated interface endpoint handles at both sides of the |
158 // message pipe in order to call this method. We need a way to forcefully | 160 // message pipe in order to call this method. We need a way to forcefully |
159 // invalidate associated interface endpoint handles. | 161 // invalidate associated interface endpoint handles. |
160 InterfacePtrInfo<Interface> PassInterface() { | 162 InterfacePtrInfo<GenericInterface> PassInterface() { |
161 CHECK(!HasAssociatedInterfaces()); | 163 CHECK(!HasAssociatedInterfaces()); |
162 CHECK(!internal_state_.has_pending_callbacks()); | 164 CHECK(!internal_state_.has_pending_callbacks()); |
163 State state; | 165 State state; |
164 internal_state_.Swap(&state); | 166 internal_state_.Swap(&state); |
165 | 167 |
166 return state.PassInterface(); | 168 return state.PassInterface(); |
167 } | 169 } |
168 | 170 |
169 // Returns the associated group that this object belongs to. Returns null if: | 171 // Returns the associated group that this object belongs to. Returns null if: |
170 // - this object is not bound; or | 172 // - this object is not bound; or |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 217 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
216 InterfacePtr<Interface> ptr; | 218 InterfacePtr<Interface> ptr; |
217 if (info.is_valid()) | 219 if (info.is_valid()) |
218 ptr.Bind(info.Pass(), waiter); | 220 ptr.Bind(info.Pass(), waiter); |
219 return ptr.Pass(); | 221 return ptr.Pass(); |
220 } | 222 } |
221 | 223 |
222 } // namespace mojo | 224 } // namespace mojo |
223 | 225 |
224 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ | 226 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
OLD | NEW |