| 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" |
| 11 #include "mojo/public/cpp/bindings/interface_request.h" | 11 #include "mojo/public/cpp/bindings/interface_request.h" |
| 12 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | 12 #include "mojo/public/cpp/bindings/lib/filter_chain.h" |
| 13 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | 13 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" |
| 14 #include "mojo/public/cpp/bindings/lib/router.h" | 14 #include "mojo/public/cpp/bindings/lib/router.h" |
| 15 #include "mojo/public/cpp/environment/logging.h" | 15 #include "mojo/public/cpp/environment/logging.h" |
| 16 #include "mojo/public/cpp/system/core.h" | 16 #include "mojo/public/cpp/system/core.h" |
| 17 | 17 |
| 18 namespace mojo { | 18 namespace mojo { |
| 19 | 19 |
| 20 // Represents the binding of an interface implementation to a message pipe. | 20 // This binds an interface implementation a pipe. Deleting the binding closes |
| 21 // When the |Binding| object is destroyed, the binding between the message pipe | 21 // the pipe. |
| 22 // and the interface is torn down and the message pipe is closed, leaving the | |
| 23 // interface implementation in an unbound state. | |
| 24 // | 22 // |
| 25 // Example: | 23 // Example: |
| 26 // | 24 // |
| 27 // #include "foo.mojom.h" | 25 // #include "foo.mojom.h" |
| 28 // | 26 // |
| 29 // class FooImpl : public Foo { | 27 // class FooImpl : public Foo { |
| 30 // public: | 28 // public: |
| 31 // explicit FooImpl(InterfaceRequest<Foo> request) | 29 // explicit FooImpl(InterfaceRequest<Foo> request) |
| 32 // : binding_(this, request.Pass()) {} | 30 // : binding_(this, request.Pass()) {} |
| 33 // | 31 // |
| 34 // // Foo implementation here. | 32 // // Foo implementation here. |
| 35 // | 33 // |
| 36 // private: | 34 // private: |
| 37 // Binding<Foo> binding_; | 35 // Binding<Foo> binding_; |
| 38 // }; | 36 // }; |
| 39 // | 37 // |
| 40 // class MyFooFactory : public InterfaceFactory<Foo> { | 38 // class MyFooFactory : public InterfaceFactory<Foo> { |
| 41 // public: | 39 // public: |
| 42 // void Create(..., InterfaceRequest<Foo> request) override { | 40 // void Create(..., InterfaceRequest<Foo> request) override { |
| 43 // auto f = new FooImpl(request.Pass()); | 41 // auto f = new FooImpl(request.Pass()); |
| 44 // // Do something to manage the lifetime of |f|. Use StrongBinding<> to | 42 // // Do something to manage the lifetime of |f|. Use StrongBinding<> to |
| 45 // // delete FooImpl on connection errors. | 43 // // delete FooImpl on connection errors. |
| 46 // } | 44 // } |
| 47 // }; | 45 // }; |
| 48 // | |
| 49 // The caller may specify a |MojoAsyncWaiter| to be used by the connection when | |
| 50 // waiting for calls to arrive. Normally it is fine to use the default waiter. | |
| 51 // However, the caller may provide their own implementation if needed. The | |
| 52 // |Binding| will not take ownership of the waiter, and the waiter must outlive | |
| 53 // the |Binding|. | |
| 54 // | |
| 55 // TODO(ggowan): Find out under what circumstances the caller may need to | |
| 56 // provide their own implementation of MojoAsyncWaiter, and then describe those | |
| 57 // circumstances. | |
| 58 template <typename Interface> | 46 template <typename Interface> |
| 59 class Binding : public ErrorHandler { | 47 class Binding : public ErrorHandler { |
| 60 public: | 48 public: |
| 61 using Client = typename Interface::Client; | 49 using Client = typename Interface::Client; |
| 62 | 50 |
| 63 // Constructs an incomplete binding that will use the implementation |impl|. | |
| 64 // The binding may be completed with a subsequent call to the |Bind| method. | |
| 65 // Does not take ownership of |impl|, which must outlive the binding. | |
| 66 explicit Binding(Interface* impl) : impl_(impl) { stub_.set_sink(impl_); } | 51 explicit Binding(Interface* impl) : impl_(impl) { stub_.set_sink(impl_); } |
| 67 | 52 |
| 68 // Constructs a completed binding of message pipe |handle| to implementation | |
| 69 // |impl|. Does not take ownership of |impl|, which must outlive the binding. | |
| 70 // See class comment for definition of |waiter|. | |
| 71 Binding(Interface* impl, | 53 Binding(Interface* impl, |
| 72 ScopedMessagePipeHandle handle, | 54 ScopedMessagePipeHandle handle, |
| 73 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | 55 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) |
| 74 : Binding(impl) { | 56 : Binding(impl) { |
| 75 Bind(handle.Pass(), waiter); | 57 Bind(handle.Pass(), waiter); |
| 76 } | 58 } |
| 77 | 59 |
| 78 // Constructs a completed binding of |impl| to a new message pipe, passing the | |
| 79 // client end to |ptr|, which takes ownership of it. The caller is expected to | |
| 80 // pass |ptr| on to the client of the service. Does not take ownership of any | |
| 81 // of the parameters. |impl| must outlive the binding. |ptr| only needs to | |
| 82 // last until the constructor returns. See class comment for definition of | |
| 83 // |waiter|. | |
| 84 Binding(Interface* impl, | 60 Binding(Interface* impl, |
| 85 InterfacePtr<Interface>* ptr, | 61 InterfacePtr<Interface>* ptr, |
| 86 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | 62 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) |
| 87 : Binding(impl) { | 63 : Binding(impl) { |
| 88 Bind(ptr, waiter); | 64 Bind(ptr, waiter); |
| 89 } | 65 } |
| 90 | 66 |
| 91 // Constructs a completed binding of |impl| to the message pipe endpoint in | |
| 92 // |request|, taking ownership of the endpoint. Does not take ownership of | |
| 93 // |impl|, which must outlive the binding. See class comment for definition of | |
| 94 // |waiter|. | |
| 95 Binding(Interface* impl, | 67 Binding(Interface* impl, |
| 96 InterfaceRequest<Interface> request, | 68 InterfaceRequest<Interface> request, |
| 97 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | 69 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) |
| 98 : Binding(impl) { | 70 : Binding(impl) { |
| 99 Bind(request.PassMessagePipe(), waiter); | 71 Bind(request.PassMessagePipe(), waiter); |
| 100 } | 72 } |
| 101 | 73 |
| 102 // Tears down the binding, closing the message pipe and leaving the interface | |
| 103 // implementation unbound. | |
| 104 ~Binding() override { | 74 ~Binding() override { |
| 105 delete proxy_; | 75 delete proxy_; |
| 106 if (internal_router_) { | 76 if (internal_router_) { |
| 107 internal_router_->set_error_handler(nullptr); | 77 internal_router_->set_error_handler(nullptr); |
| 108 delete internal_router_; | 78 delete internal_router_; |
| 109 } | 79 } |
| 110 } | 80 } |
| 111 | 81 |
| 112 // Completes a binding that was constructed with only an interface | |
| 113 // implementation. Takes ownership of |handle| and binds it to the previously | |
| 114 // specified implementation. See class comment for definition of |waiter|. | |
| 115 void Bind( | 82 void Bind( |
| 116 ScopedMessagePipeHandle handle, | 83 ScopedMessagePipeHandle handle, |
| 117 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 84 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 118 internal::FilterChain filters; | 85 internal::FilterChain filters; |
| 119 filters.Append<internal::MessageHeaderValidator>(); | 86 filters.Append<internal::MessageHeaderValidator>(); |
| 120 filters.Append<typename Interface::RequestValidator_>(); | 87 filters.Append<typename Interface::RequestValidator_>(); |
| 121 filters.Append<typename Client::ResponseValidator_>(); | 88 filters.Append<typename Client::ResponseValidator_>(); |
| 122 | 89 |
| 123 internal_router_ = | 90 internal_router_ = |
| 124 new internal::Router(handle.Pass(), filters.Pass(), waiter); | 91 new internal::Router(handle.Pass(), filters.Pass(), waiter); |
| 125 internal_router_->set_incoming_receiver(&stub_); | 92 internal_router_->set_incoming_receiver(&stub_); |
| 126 internal_router_->set_error_handler(this); | 93 internal_router_->set_error_handler(this); |
| 127 | 94 |
| 128 proxy_ = new typename Client::Proxy_(internal_router_); | 95 proxy_ = new typename Client::Proxy_(internal_router_); |
| 129 } | 96 } |
| 130 | 97 |
| 131 // Completes a binding that was constructed with only an interface | |
| 132 // implementation by creating a new message pipe, binding one end of it to the | |
| 133 // previously specified implementation, and passing the other to |ptr|, which | |
| 134 // takes ownership of it. The caller is expected to pass |ptr| on to the | |
| 135 // eventual client of the service. Does not take ownership of |ptr|. See | |
| 136 // class comment for definition of |waiter|. | |
| 137 void Bind( | 98 void Bind( |
| 138 InterfacePtr<Interface>* ptr, | 99 InterfacePtr<Interface>* ptr, |
| 139 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 100 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 140 MessagePipe pipe; | 101 MessagePipe pipe; |
| 141 ptr->Bind(pipe.handle0.Pass(), waiter); | 102 ptr->Bind(pipe.handle0.Pass(), waiter); |
| 142 Bind(pipe.handle1.Pass(), waiter); | 103 Bind(pipe.handle1.Pass(), waiter); |
| 143 } | 104 } |
| 144 | 105 |
| 145 // Completes a binding that was constructed with only an interface | |
| 146 // implementation by removing the message pipe endpoint from |request| and | |
| 147 // binding it to the previously specified implementation. See class comment | |
| 148 // for definition of |waiter|. | |
| 149 void Bind( | 106 void Bind( |
| 150 InterfaceRequest<Interface> request, | 107 InterfaceRequest<Interface> request, |
| 151 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 108 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 152 Bind(request.PassMessagePipe(), waiter); | 109 Bind(request.PassMessagePipe(), waiter); |
| 153 } | 110 } |
| 154 | 111 |
| 155 // Blocks the calling thread until either a call arrives on the previously | |
| 156 // bound message pipe, or an error occurs. | |
| 157 bool WaitForIncomingMethodCall() { | 112 bool WaitForIncomingMethodCall() { |
| 158 MOJO_DCHECK(internal_router_); | 113 MOJO_DCHECK(internal_router_); |
| 159 return internal_router_->WaitForIncomingMessage(); | 114 return internal_router_->WaitForIncomingMessage(); |
| 160 } | 115 } |
| 161 | 116 |
| 162 // Closes the message pipe that was previously bound. | |
| 163 void Close() { | 117 void Close() { |
| 164 MOJO_DCHECK(internal_router_); | 118 MOJO_DCHECK(internal_router_); |
| 165 internal_router_->CloseMessagePipe(); | 119 internal_router_->CloseMessagePipe(); |
| 166 } | 120 } |
| 167 | 121 |
| 168 // Sets an error handler that will be called if a connection error occurs on | |
| 169 // the bound message pipe. | |
| 170 void set_error_handler(ErrorHandler* error_handler) { | 122 void set_error_handler(ErrorHandler* error_handler) { |
| 171 error_handler_ = error_handler; | 123 error_handler_ = error_handler; |
| 172 } | 124 } |
| 173 | 125 |
| 174 // Implements the |Binding|'s response to a connection error. | 126 // ErrorHandler implementation |
| 175 void OnConnectionError() override { | 127 void OnConnectionError() override { |
| 176 if (error_handler_) | 128 if (error_handler_) |
| 177 error_handler_->OnConnectionError(); | 129 error_handler_->OnConnectionError(); |
| 178 } | 130 } |
| 179 | 131 |
| 180 // Returns the interface implementation that was previously specified. Caller | |
| 181 // does not take ownership. | |
| 182 Interface* impl() { return impl_; } | 132 Interface* impl() { return impl_; } |
| 183 | |
| 184 // Returns the client's interface. | |
| 185 Client* client() { return proxy_; } | 133 Client* client() { return proxy_; } |
| 186 | 134 |
| 187 // Indicates whether the binding has been completed (i.e., whether a message | |
| 188 // pipe has been bound to the implementation). | |
| 189 bool is_bound() const { return !!internal_router_; } | 135 bool is_bound() const { return !!internal_router_; } |
| 190 | 136 |
| 191 // Exposed for testing, should not generally be used. | 137 // Exposed for testing, should not generally be used. |
| 192 internal::Router* internal_router() { return internal_router_; } | 138 internal::Router* internal_router() { return internal_router_; } |
| 193 | 139 |
| 194 private: | 140 private: |
| 195 internal::Router* internal_router_ = nullptr; | 141 internal::Router* internal_router_ = nullptr; |
| 196 typename Client::Proxy_* proxy_ = nullptr; | 142 typename Client::Proxy_* proxy_ = nullptr; |
| 197 typename Interface::Stub_ stub_; | 143 typename Interface::Stub_ stub_; |
| 198 Interface* impl_; | 144 Interface* impl_; |
| 199 ErrorHandler* error_handler_ = nullptr; | 145 ErrorHandler* error_handler_ = nullptr; |
| 200 | 146 |
| 201 MOJO_DISALLOW_COPY_AND_ASSIGN(Binding); | 147 MOJO_DISALLOW_COPY_AND_ASSIGN(Binding); |
| 202 }; | 148 }; |
| 203 | 149 |
| 204 } // namespace mojo | 150 } // namespace mojo |
| 205 | 151 |
| 206 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ | 152 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ |
| OLD | NEW |