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