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(); } |
96 | 86 |
| 87 // Queries the max version that the remote side supports. On completion, the |
| 88 // result will be returned as the input of |callback|. The version number of |
| 89 // this interface pointer will also be updated. |
| 90 void QueryVersion(const Callback<void(uint32_t)>& callback) { |
| 91 internal_state_.QueryVersion(callback); |
| 92 } |
| 93 |
| 94 // If the remote side doesn't support the specified version, it will close its |
| 95 // end of the message pipe asynchronously. This does nothing if it's already |
| 96 // known that the remote side supports the specified version, i.e., if |
| 97 // |version <= this->version()|. |
| 98 // |
| 99 // After calling RequireVersion() with a version not supported by the remote |
| 100 // side, all subsequent calls to interface methods will be ignored. |
| 101 void RequireVersion(uint32_t version) { |
| 102 internal_state_.RequireVersion(version); |
| 103 } |
| 104 |
97 // Closes the bound message pipe (if any) and returns the pointer to the | 105 // Closes the bound message pipe (if any) and returns the pointer to the |
98 // unbound state. | 106 // unbound state. |
99 void reset() { | 107 void reset() { |
100 State doomed; | 108 State doomed; |
101 internal_state_.Swap(&doomed); | 109 internal_state_.Swap(&doomed); |
102 } | 110 } |
103 | 111 |
104 // Blocks the current thread until the next incoming response callback arrives | 112 // Blocks the current thread until the next incoming response callback arrives |
105 // or an error occurs. Returns |true| if a response arrived, or |false| in | 113 // or an error occurs. Returns |true| if a response arrived, or |false| in |
106 // case of error. | 114 // case of error. |
(...skipping 23 matching lines...) Expand all Loading... |
130 // Unbinds the InterfacePtr and returns the information which could be used | 138 // Unbinds the InterfacePtr and returns the information which could be used |
131 // 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 |
132 // to a different thread (see class comments for details). | 140 // to a different thread (see class comments for details). |
133 InterfacePtrInfo<Interface> PassInterface() { | 141 InterfacePtrInfo<Interface> PassInterface() { |
134 State state; | 142 State state; |
135 internal_state_.Swap(&state); | 143 internal_state_.Swap(&state); |
136 | 144 |
137 return state.PassInterface(); | 145 return state.PassInterface(); |
138 } | 146 } |
139 | 147 |
140 // Similar to the previous method but returns the previously bound message | |
141 // pipe (if any). | |
142 // | |
143 // TODO(yzshen): Remove this method and change call sites to use | |
144 // PassInterface(). | |
145 ScopedMessagePipeHandle PassMessagePipe() { | |
146 return PassInterface().PassHandle(); | |
147 } | |
148 | |
149 // DO NOT USE. Exposed only for internal use and for testing. | 148 // DO NOT USE. Exposed only for internal use and for testing. |
150 internal::InterfacePtrState<Interface>* internal_state() { | 149 internal::InterfacePtrState<Interface>* internal_state() { |
151 return &internal_state_; | 150 return &internal_state_; |
152 } | 151 } |
153 | 152 |
154 // Allow InterfacePtr<> to be used in boolean expressions, but not | 153 // Allow InterfacePtr<> to be used in boolean expressions, but not |
155 // implicitly convertible to a real bool (which is dangerous). | 154 // implicitly convertible to a real bool (which is dangerous). |
156 private: | 155 private: |
157 typedef internal::InterfacePtrState<Interface> InterfacePtr::*Testable; | 156 typedef internal::InterfacePtrState<Interface> InterfacePtr::*Testable; |
158 | 157 |
159 public: | 158 public: |
160 operator Testable() const { | 159 operator Testable() const { |
161 return internal_state_.is_bound() ? &InterfacePtr::internal_state_ | 160 return internal_state_.is_bound() ? &InterfacePtr::internal_state_ |
162 : nullptr; | 161 : nullptr; |
163 } | 162 } |
164 | 163 |
165 private: | 164 private: |
166 typedef internal::InterfacePtrState<Interface> State; | 165 typedef internal::InterfacePtrState<Interface> State; |
167 mutable State internal_state_; | 166 mutable State internal_state_; |
168 }; | 167 }; |
169 | 168 |
170 // 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 |
171 // to it. Otherwise, returns an unbound InterfacePtr. The specified |waiter| | 170 // InterfacePtr bound to it. Otherwise, returns an unbound InterfacePtr. The |
172 // will be used as in the InterfacePtr::Bind() method. | 171 // specified |waiter| will be used as in the InterfacePtr::Bind() method. |
173 // | |
174 // TODO(yzshen): Either remove it or change to use InterfacePtrInfo as the first | |
175 // parameter. | |
176 template <typename Interface> | 172 template <typename Interface> |
177 InterfacePtr<Interface> MakeProxy( | 173 InterfacePtr<Interface> MakeProxy( |
178 ScopedMessagePipeHandle handle, | 174 InterfacePtrInfo<Interface> info, |
179 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 175 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
180 InterfacePtr<Interface> ptr; | 176 InterfacePtr<Interface> ptr; |
181 if (handle.is_valid()) | 177 if (info.is_valid()) |
182 ptr.Bind(handle.Pass(), waiter); | 178 ptr.Bind(info.Pass(), waiter); |
183 return ptr.Pass(); | 179 return ptr.Pass(); |
184 } | 180 } |
185 | 181 |
186 } // namespace mojo | 182 } // namespace mojo |
187 | 183 |
188 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ | 184 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
OLD | NEW |