| 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 "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "mojo/public/c/environment/async_waiter.h" | 9 #include "mojo/public/c/environment/async_waiter.h" |
| 10 #include "mojo/public/cpp/bindings/callback.h" | 10 #include "mojo/public/cpp/bindings/callback.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 // However, the caller may provide their own implementation if needed. The | 52 // 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 | 53 // |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 | 54 // 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 | 55 // 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 | 56 // implementation runs on. If writing library code that has to work on different |
| 57 // types of threads callers may need to provide different waiter | 57 // types of threads callers may need to provide different waiter |
| 58 // implementations. | 58 // implementations. |
| 59 template <typename Interface> | 59 template <typename Interface> |
| 60 class Binding { | 60 class Binding { |
| 61 public: | 61 public: |
| 62 using GenericInterface = typename Interface::GenericInterface; |
| 63 |
| 62 // Constructs an incomplete binding that will use the implementation |impl|. | 64 // Constructs an incomplete binding that will use the implementation |impl|. |
| 63 // The binding may be completed with a subsequent call to the |Bind| method. | 65 // 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. | 66 // Does not take ownership of |impl|, which must outlive the binding. |
| 65 explicit Binding(Interface* impl) : internal_state_(impl) {} | 67 explicit Binding(Interface* impl) : internal_state_(impl) {} |
| 66 | 68 |
| 67 // Constructs a completed binding of message pipe |handle| to implementation | 69 // Constructs a completed binding of message pipe |handle| to implementation |
| 68 // |impl|. Does not take ownership of |impl|, which must outlive the binding. | 70 // |impl|. Does not take ownership of |impl|, which must outlive the binding. |
| 69 // See class comment for definition of |waiter|. | 71 // See class comment for definition of |waiter|. |
| 70 Binding(Interface* impl, | 72 Binding(Interface* impl, |
| 71 ScopedMessagePipeHandle handle, | 73 ScopedMessagePipeHandle handle, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 85 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | 87 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) |
| 86 : Binding(impl) { | 88 : Binding(impl) { |
| 87 Bind(ptr, waiter); | 89 Bind(ptr, waiter); |
| 88 } | 90 } |
| 89 | 91 |
| 90 // Constructs a completed binding of |impl| to the message pipe endpoint in | 92 // Constructs a completed binding of |impl| to the message pipe endpoint in |
| 91 // |request|, taking ownership of the endpoint. Does not take ownership of | 93 // |request|, taking ownership of the endpoint. Does not take ownership of |
| 92 // |impl|, which must outlive the binding. See class comment for definition of | 94 // |impl|, which must outlive the binding. See class comment for definition of |
| 93 // |waiter|. | 95 // |waiter|. |
| 94 Binding(Interface* impl, | 96 Binding(Interface* impl, |
| 95 InterfaceRequest<Interface> request, | 97 InterfaceRequest<GenericInterface> request, |
| 96 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | 98 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) |
| 97 : Binding(impl) { | 99 : Binding(impl) { |
| 98 Bind(request.PassMessagePipe(), waiter); | 100 Bind(request.PassMessagePipe(), waiter); |
| 99 } | 101 } |
| 100 | 102 |
| 101 // Tears down the binding, closing the message pipe and leaving the interface | 103 // Tears down the binding, closing the message pipe and leaving the interface |
| 102 // implementation unbound. | 104 // implementation unbound. |
| 103 ~Binding() {} | 105 ~Binding() {} |
| 104 | 106 |
| 105 // Completes a binding that was constructed with only an interface | 107 // Completes a binding that was constructed with only an interface |
| (...skipping 19 matching lines...) Expand all Loading... |
| 125 InterfacePtrInfo<Interface>(pipe.handle0.Pass(), Interface::Version_), | 127 InterfacePtrInfo<Interface>(pipe.handle0.Pass(), Interface::Version_), |
| 126 waiter); | 128 waiter); |
| 127 Bind(pipe.handle1.Pass(), waiter); | 129 Bind(pipe.handle1.Pass(), waiter); |
| 128 } | 130 } |
| 129 | 131 |
| 130 // Completes a binding that was constructed with only an interface | 132 // Completes a binding that was constructed with only an interface |
| 131 // implementation by removing the message pipe endpoint from |request| and | 133 // implementation by removing the message pipe endpoint from |request| and |
| 132 // binding it to the previously specified implementation. See class comment | 134 // binding it to the previously specified implementation. See class comment |
| 133 // for definition of |waiter|. | 135 // for definition of |waiter|. |
| 134 void Bind( | 136 void Bind( |
| 135 InterfaceRequest<Interface> request, | 137 InterfaceRequest<GenericInterface> request, |
| 136 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 138 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 137 Bind(request.PassMessagePipe(), waiter); | 139 Bind(request.PassMessagePipe(), waiter); |
| 138 } | 140 } |
| 139 | 141 |
| 140 // Whether there are any associated interfaces running on the pipe currently. | 142 // Whether there are any associated interfaces running on the pipe currently. |
| 141 bool HasAssociatedInterfaces() const { | 143 bool HasAssociatedInterfaces() const { |
| 142 return internal_state_.HasAssociatedInterfaces(); | 144 return internal_state_.HasAssociatedInterfaces(); |
| 143 } | 145 } |
| 144 | 146 |
| 145 // Stops processing incoming messages until | 147 // Stops processing incoming messages until |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 // implementation. Put this object into a state where it can be rebound to a | 181 // implementation. Put this object into a state where it can be rebound to a |
| 180 // new pipe. | 182 // new pipe. |
| 181 // | 183 // |
| 182 // This method may only be called if the object has been bound to a message | 184 // This method may only be called if the object has been bound to a message |
| 183 // pipe and there are no associated interfaces running. | 185 // pipe and there are no associated interfaces running. |
| 184 // | 186 // |
| 185 // TODO(yzshen): For now, users need to make sure there is no one holding | 187 // TODO(yzshen): For now, users need to make sure there is no one holding |
| 186 // on to associated interface endpoint handles at both sides of the | 188 // on to associated interface endpoint handles at both sides of the |
| 187 // message pipe in order to call this method. We need a way to forcefully | 189 // message pipe in order to call this method. We need a way to forcefully |
| 188 // invalidate associated interface endpoint handles. | 190 // invalidate associated interface endpoint handles. |
| 189 InterfaceRequest<Interface> Unbind() { | 191 InterfaceRequest<GenericInterface> Unbind() { |
| 190 CHECK(!HasAssociatedInterfaces()); | 192 CHECK(!HasAssociatedInterfaces()); |
| 191 return internal_state_.Unbind(); | 193 return internal_state_.Unbind(); |
| 192 } | 194 } |
| 193 | 195 |
| 194 // Sets an error handler that will be called if a connection error occurs on | 196 // Sets an error handler that will be called if a connection error occurs on |
| 195 // the bound message pipe. | 197 // the bound message pipe. |
| 196 void set_connection_error_handler(const Closure& error_handler) { | 198 void set_connection_error_handler(const Closure& error_handler) { |
| 197 internal_state_.set_connection_error_handler(error_handler); | 199 internal_state_.set_connection_error_handler(error_handler); |
| 198 } | 200 } |
| 199 | 201 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 225 private: | 227 private: |
| 226 internal::BindingState<Interface, Interface::PassesAssociatedKinds_> | 228 internal::BindingState<Interface, Interface::PassesAssociatedKinds_> |
| 227 internal_state_; | 229 internal_state_; |
| 228 | 230 |
| 229 DISALLOW_COPY_AND_ASSIGN(Binding); | 231 DISALLOW_COPY_AND_ASSIGN(Binding); |
| 230 }; | 232 }; |
| 231 | 233 |
| 232 } // namespace mojo | 234 } // namespace mojo |
| 233 | 235 |
| 234 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ | 236 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ |
| OLD | NEW |