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_BINDING_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ |
7 | 7 |
8 #include "mojo/public/c/environment/async_waiter.h" | 8 #include "mojo/public/c/environment/async_waiter.h" |
9 #include "mojo/public/cpp/bindings/callback.h" | |
9 #include "mojo/public/cpp/bindings/error_handler.h" | 10 #include "mojo/public/cpp/bindings/error_handler.h" |
10 #include "mojo/public/cpp/bindings/interface_ptr.h" | 11 #include "mojo/public/cpp/bindings/interface_ptr.h" |
11 #include "mojo/public/cpp/bindings/interface_ptr_info.h" | 12 #include "mojo/public/cpp/bindings/interface_ptr_info.h" |
12 #include "mojo/public/cpp/bindings/interface_request.h" | 13 #include "mojo/public/cpp/bindings/interface_request.h" |
13 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | 14 #include "mojo/public/cpp/bindings/lib/filter_chain.h" |
14 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | 15 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" |
15 #include "mojo/public/cpp/bindings/lib/router.h" | 16 #include "mojo/public/cpp/bindings/lib/router.h" |
16 #include "mojo/public/cpp/environment/logging.h" | 17 #include "mojo/public/cpp/environment/logging.h" |
17 #include "mojo/public/cpp/system/core.h" | 18 #include "mojo/public/cpp/system/core.h" |
18 | 19 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
50 // The caller may specify a |MojoAsyncWaiter| to be used by the connection when | 51 // The caller may specify a |MojoAsyncWaiter| to be used by the connection when |
51 // waiting for calls to arrive. Normally it is fine to use the default waiter. | 52 // waiting for calls to arrive. Normally it is fine to use the default waiter. |
52 // However, the caller may provide their own implementation if needed. The | 53 // However, the caller may provide their own implementation if needed. The |
53 // |Binding| will not take ownership of the waiter, and the waiter must outlive | 54 // |Binding| will not take ownership of the waiter, and the waiter must outlive |
54 // the |Binding|. The provided waiter must be able to signal the implementation | 55 // the |Binding|. The provided waiter must be able to signal the implementation |
55 // which generally means it needs to be able to schedule work on the thread the | 56 // which generally means it needs to be able to schedule work on the thread the |
56 // implementation runs on. If writing library code that has to work on different | 57 // implementation runs on. If writing library code that has to work on different |
57 // types of threads callers may need to provide different waiter | 58 // types of threads callers may need to provide different waiter |
58 // implementations. | 59 // implementations. |
59 template <typename Interface> | 60 template <typename Interface> |
60 class Binding : public ErrorHandler { | 61 class Binding { |
61 public: | 62 public: |
62 // Constructs an incomplete binding that will use the implementation |impl|. | 63 // Constructs an incomplete binding that will use the implementation |impl|. |
63 // The binding may be completed with a subsequent call to the |Bind| method. | 64 // The binding may be completed with a subsequent call to the |Bind| method. |
64 // Does not take ownership of |impl|, which must outlive the binding. | 65 // Does not take ownership of |impl|, which must outlive the binding. |
65 explicit Binding(Interface* impl) : impl_(impl) { stub_.set_sink(impl_); } | 66 explicit Binding(Interface* impl) : impl_(impl) { stub_.set_sink(impl_); } |
66 | 67 |
67 // Constructs a completed binding of message pipe |handle| to implementation | 68 // Constructs a completed binding of message pipe |handle| to implementation |
68 // |impl|. Does not take ownership of |impl|, which must outlive the binding. | 69 // |impl|. Does not take ownership of |impl|, which must outlive the binding. |
69 // See class comment for definition of |waiter|. | 70 // See class comment for definition of |waiter|. |
70 Binding(Interface* impl, | 71 Binding(Interface* impl, |
(...skipping 22 matching lines...) Expand all Loading... | |
93 // |waiter|. | 94 // |waiter|. |
94 Binding(Interface* impl, | 95 Binding(Interface* impl, |
95 InterfaceRequest<Interface> request, | 96 InterfaceRequest<Interface> request, |
96 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | 97 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) |
97 : Binding(impl) { | 98 : Binding(impl) { |
98 Bind(request.PassMessagePipe(), waiter); | 99 Bind(request.PassMessagePipe(), waiter); |
99 } | 100 } |
100 | 101 |
101 // Tears down the binding, closing the message pipe and leaving the interface | 102 // Tears down the binding, closing the message pipe and leaving the interface |
102 // implementation unbound. | 103 // implementation unbound. |
103 ~Binding() override { | 104 ~Binding() { |
104 if (internal_router_) { | 105 if (internal_router_) { |
105 Close(); | 106 Close(); |
106 } | 107 } |
107 } | 108 } |
108 | 109 |
109 // Completes a binding that was constructed with only an interface | 110 // Completes a binding that was constructed with only an interface |
110 // implementation. Takes ownership of |handle| and binds it to the previously | 111 // implementation. Takes ownership of |handle| and binds it to the previously |
111 // specified implementation. See class comment for definition of |waiter|. | 112 // specified implementation. See class comment for definition of |waiter|. |
112 void Bind( | 113 void Bind( |
113 ScopedMessagePipeHandle handle, | 114 ScopedMessagePipeHandle handle, |
114 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 115 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
115 MOJO_DCHECK(!internal_router_); | 116 MOJO_DCHECK(!internal_router_); |
116 internal::FilterChain filters; | 117 internal::FilterChain filters; |
117 filters.Append<internal::MessageHeaderValidator>(); | 118 filters.Append<internal::MessageHeaderValidator>(); |
118 filters.Append<typename Interface::RequestValidator_>(); | 119 filters.Append<typename Interface::RequestValidator_>(); |
119 | 120 |
120 internal_router_ = | 121 internal_router_ = |
121 new internal::Router(handle.Pass(), filters.Pass(), waiter); | 122 new internal::Router(handle.Pass(), filters.Pass(), waiter); |
122 internal_router_->set_incoming_receiver(&stub_); | 123 internal_router_->set_incoming_receiver(&stub_); |
123 internal_router_->set_error_handler(this); | 124 internal_router_->set_connection_error_handler( |
125 [this]() { connection_error_handler_.Run(); }); | |
qsr
2015/06/11 09:37:53
I'm missing something. Why do you need to do this,
yzshen1
2015/06/11 16:51:45
The current code matches the previous behavior:
*
| |
124 } | 126 } |
125 | 127 |
126 // Completes a binding that was constructed with only an interface | 128 // Completes a binding that was constructed with only an interface |
127 // implementation by creating a new message pipe, binding one end of it to the | 129 // implementation by creating a new message pipe, binding one end of it to the |
128 // previously specified implementation, and passing the other to |ptr|, which | 130 // previously specified implementation, and passing the other to |ptr|, which |
129 // takes ownership of it. The caller is expected to pass |ptr| on to the | 131 // takes ownership of it. The caller is expected to pass |ptr| on to the |
130 // eventual client of the service. Does not take ownership of |ptr|. See | 132 // eventual client of the service. Does not take ownership of |ptr|. See |
131 // class comment for definition of |waiter|. | 133 // class comment for definition of |waiter|. |
132 void Bind( | 134 void Bind( |
133 InterfacePtr<Interface>* ptr, | 135 InterfacePtr<Interface>* ptr, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 InterfaceRequest<Interface> request = | 176 InterfaceRequest<Interface> request = |
175 MakeRequest<Interface>(internal_router_->PassMessagePipe()); | 177 MakeRequest<Interface>(internal_router_->PassMessagePipe()); |
176 DestroyRouter(); | 178 DestroyRouter(); |
177 // TODO(vtl): The |.Pass()| below is only needed due to an MSVS bug; remove | 179 // TODO(vtl): The |.Pass()| below is only needed due to an MSVS bug; remove |
178 // it once that's fixed. | 180 // it once that's fixed. |
179 return request.Pass(); | 181 return request.Pass(); |
180 } | 182 } |
181 | 183 |
182 // Sets an error handler that will be called if a connection error occurs on | 184 // Sets an error handler that will be called if a connection error occurs on |
183 // the bound message pipe. | 185 // the bound message pipe. |
184 void set_error_handler(ErrorHandler* error_handler) { | 186 void set_connection_error_handler(const Closure& error_handler) { |
185 error_handler_ = error_handler; | 187 connection_error_handler_ = error_handler; |
186 } | 188 } |
187 | 189 |
188 // Implements the |Binding|'s response to a connection error. | 190 // Similar to the method above but uses the ErrorHandler interface instead of |
189 void OnConnectionError() override { | 191 // a callback. |
190 if (error_handler_) | 192 // NOTE: Deprecated. Please use the method above. |
191 error_handler_->OnConnectionError(); | 193 // TODO(yzshen): Remove this method once all callsites are converted. |
194 void set_error_handler(ErrorHandler* error_handler) { | |
195 set_connection_error_handler( | |
196 [error_handler]() { error_handler->OnConnectionError(); }); | |
192 } | 197 } |
193 | 198 |
194 // Returns the interface implementation that was previously specified. Caller | 199 // Returns the interface implementation that was previously specified. Caller |
195 // does not take ownership. | 200 // does not take ownership. |
196 Interface* impl() { return impl_; } | 201 Interface* impl() { return impl_; } |
197 | 202 |
198 // Indicates whether the binding has been completed (i.e., whether a message | 203 // Indicates whether the binding has been completed (i.e., whether a message |
199 // pipe has been bound to the implementation). | 204 // pipe has been bound to the implementation). |
200 bool is_bound() const { return !!internal_router_; } | 205 bool is_bound() const { return !!internal_router_; } |
201 | 206 |
202 // Returns the value of the handle currently bound to this Binding which can | 207 // Returns the value of the handle currently bound to this Binding which can |
203 // be used to make explicit Wait/WaitMany calls. Requires that the Binding be | 208 // be used to make explicit Wait/WaitMany calls. Requires that the Binding be |
204 // bound. Ownership of the handle is retained by the Binding, it is not | 209 // bound. Ownership of the handle is retained by the Binding, it is not |
205 // transferred to the caller. | 210 // transferred to the caller. |
206 MessagePipeHandle handle() const { | 211 MessagePipeHandle handle() const { |
207 MOJO_DCHECK(is_bound()); | 212 MOJO_DCHECK(is_bound()); |
208 return internal_router_->handle(); | 213 return internal_router_->handle(); |
209 } | 214 } |
210 | 215 |
211 // Exposed for testing, should not generally be used. | 216 // Exposed for testing, should not generally be used. |
212 internal::Router* internal_router() { return internal_router_; } | 217 internal::Router* internal_router() { return internal_router_; } |
213 | 218 |
214 private: | 219 private: |
215 void DestroyRouter() { | 220 void DestroyRouter() { |
216 internal_router_->set_error_handler(nullptr); | 221 internal_router_->set_connection_error_handler(Closure()); |
217 delete internal_router_; | 222 delete internal_router_; |
218 internal_router_ = nullptr; | 223 internal_router_ = nullptr; |
219 } | 224 } |
220 | 225 |
221 internal::Router* internal_router_ = nullptr; | 226 internal::Router* internal_router_ = nullptr; |
222 typename Interface::Stub_ stub_; | 227 typename Interface::Stub_ stub_; |
223 Interface* impl_; | 228 Interface* impl_; |
224 ErrorHandler* error_handler_ = nullptr; | 229 Closure connection_error_handler_; |
225 | 230 |
226 MOJO_DISALLOW_COPY_AND_ASSIGN(Binding); | 231 MOJO_DISALLOW_COPY_AND_ASSIGN(Binding); |
227 }; | 232 }; |
228 | 233 |
229 } // namespace mojo | 234 } // namespace mojo |
230 | 235 |
231 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ | 236 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ |
OLD | NEW |