Chromium Code Reviews| 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/error_handler.h" | 9 #include "mojo/public/cpp/bindings/error_handler.h" |
| 10 #include "mojo/public/cpp/bindings/interface_ptr.h" | 10 #include "mojo/public/cpp/bindings/interface_ptr.h" |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 Binding(Interface* impl, | 93 Binding(Interface* impl, |
| 94 InterfaceRequest<Interface> request, | 94 InterfaceRequest<Interface> request, |
| 95 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | 95 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) |
| 96 : Binding(impl) { | 96 : Binding(impl) { |
| 97 Bind(request.PassMessagePipe(), waiter); | 97 Bind(request.PassMessagePipe(), waiter); |
| 98 } | 98 } |
| 99 | 99 |
| 100 // Tears down the binding, closing the message pipe and leaving the interface | 100 // Tears down the binding, closing the message pipe and leaving the interface |
| 101 // implementation unbound. | 101 // implementation unbound. |
| 102 ~Binding() override { | 102 ~Binding() override { |
| 103 stub_.Close(); | |
|
rudominer
2015/03/13 23:52:36
I was confused here. Closing the stub here makes l
| |
| 103 if (internal_router_) { | 104 if (internal_router_) { |
| 104 Close(); | 105 Close(); |
| 105 } | 106 } |
| 106 } | 107 } |
| 107 | 108 |
| 108 // Completes a binding that was constructed with only an interface | 109 // Completes a binding that was constructed with only an interface |
| 109 // implementation. Takes ownership of |handle| and binds it to the previously | 110 // implementation. Takes ownership of |handle| and binds it to the previously |
| 110 // specified implementation. See class comment for definition of |waiter|. | 111 // specified implementation. See class comment for definition of |waiter|. |
| 111 void Bind( | 112 void Bind( |
| 112 ScopedMessagePipeHandle handle, | 113 ScopedMessagePipeHandle handle, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 149 // Blocks the calling thread until either a call arrives on the previously | 150 // Blocks the calling thread until either a call arrives on the previously |
| 150 // bound message pipe, or an error occurs. | 151 // bound message pipe, or an error occurs. |
| 151 bool WaitForIncomingMethodCall() { | 152 bool WaitForIncomingMethodCall() { |
| 152 MOJO_DCHECK(internal_router_); | 153 MOJO_DCHECK(internal_router_); |
| 153 return internal_router_->WaitForIncomingMessage(); | 154 return internal_router_->WaitForIncomingMessage(); |
| 154 } | 155 } |
| 155 | 156 |
| 156 // Closes the message pipe that was previously bound. Put this object into a | 157 // Closes the message pipe that was previously bound. Put this object into a |
| 157 // state where it can be rebound to a new pipe. | 158 // state where it can be rebound to a new pipe. |
| 158 void Close() { | 159 void Close() { |
| 160 stub_.Close(); | |
| 159 MOJO_DCHECK(internal_router_); | 161 MOJO_DCHECK(internal_router_); |
| 160 internal_router_->CloseMessagePipe(); | 162 internal_router_->CloseMessagePipe(); |
| 161 DestroyRouter(); | 163 DestroyRouter(); |
| 162 } | 164 } |
| 163 | 165 |
| 164 // Unbinds the underlying pipe from this binding and returns it so it can be | 166 // Unbinds the underlying pipe from this binding and returns it so it can be |
| 165 // used in another context, such as on another thread or with a different | 167 // used in another context, such as on another thread or with a different |
| 166 // implementation. Put this object into a state where it can be rebound to a | 168 // implementation. Put this object into a state where it can be rebound to a |
| 167 // new pipe. | 169 // new pipe. |
| 168 InterfaceRequest<Interface> Unbind() { | 170 InterfaceRequest<Interface> Unbind() { |
| 169 InterfaceRequest<Interface> request = | 171 InterfaceRequest<Interface> request = |
| 170 MakeRequest<Interface>(internal_router_->PassMessagePipe()); | 172 MakeRequest<Interface>(internal_router_->PassMessagePipe()); |
| 171 DestroyRouter(); | 173 DestroyRouter(); |
| 172 // TODO(vtl): The |.Pass()| below is only needed due to an MSVS bug; remove | 174 // TODO(vtl): The |.Pass()| below is only needed due to an MSVS bug; remove |
| 173 // it once that's fixed. | 175 // it once that's fixed. |
| 174 return request.Pass(); | 176 return request.Pass(); |
| 175 } | 177 } |
| 176 | 178 |
| 177 // Sets an error handler that will be called if a connection error occurs on | 179 // Sets an error handler that will be called if a connection error occurs on |
| 178 // the bound message pipe. | 180 // the bound message pipe. |
| 179 void set_error_handler(ErrorHandler* error_handler) { | 181 void set_error_handler(ErrorHandler* error_handler) { |
| 180 error_handler_ = error_handler; | 182 error_handler_ = error_handler; |
| 181 } | 183 } |
| 182 | 184 |
| 183 // Implements the |Binding|'s response to a connection error. | 185 // Implements the |Binding|'s response to a connection error. |
| 184 void OnConnectionError() override { | 186 void OnConnectionError() override { |
| 187 // Note that it is important that we tell |stub_| that there was a | |
| 188 // connection error before we tell |error_handler_|. This is because | |
| 189 // in response |error_handler_| may destroy a Callback that hasn't been | |
| 190 // run and this will cause |stub_| to DCHECK unless it also knows that | |
| 191 // this happened for a legitimate reason, namely that a connection error | |
| 192 // occurred. | |
| 193 stub_.OnConnectionError(); | |
| 185 if (error_handler_) | 194 if (error_handler_) |
| 186 error_handler_->OnConnectionError(); | 195 error_handler_->OnConnectionError(); |
| 187 } | 196 } |
| 188 | 197 |
| 189 // Returns the interface implementation that was previously specified. Caller | 198 // Returns the interface implementation that was previously specified. Caller |
| 190 // does not take ownership. | 199 // does not take ownership. |
| 191 Interface* impl() { return impl_; } | 200 Interface* impl() { return impl_; } |
| 192 | 201 |
| 193 // Indicates whether the binding has been completed (i.e., whether a message | 202 // Indicates whether the binding has been completed (i.e., whether a message |
| 194 // pipe has been bound to the implementation). | 203 // pipe has been bound to the implementation). |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 208 typename Interface::Stub_ stub_; | 217 typename Interface::Stub_ stub_; |
| 209 Interface* impl_; | 218 Interface* impl_; |
| 210 ErrorHandler* error_handler_ = nullptr; | 219 ErrorHandler* error_handler_ = nullptr; |
| 211 | 220 |
| 212 MOJO_DISALLOW_COPY_AND_ASSIGN(Binding); | 221 MOJO_DISALLOW_COPY_AND_ASSIGN(Binding); |
| 213 }; | 222 }; |
| 214 | 223 |
| 215 } // namespace mojo | 224 } // namespace mojo |
| 216 | 225 |
| 217 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ | 226 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ |
| OLD | NEW |