| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ | |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ | |
| 7 | |
| 8 #include "mojo/public/c/environment/async_waiter.h" | |
| 9 #include "mojo/public/cpp/bindings/error_handler.h" | |
| 10 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
| 11 #include "mojo/public/cpp/bindings/interface_request.h" | |
| 12 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | |
| 13 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | |
| 14 #include "mojo/public/cpp/bindings/lib/router.h" | |
| 15 #include "mojo/public/cpp/environment/logging.h" | |
| 16 #include "mojo/public/cpp/system/core.h" | |
| 17 | |
| 18 namespace mojo { | |
| 19 | |
| 20 // This binds an interface implementation a pipe. Deleting the binding closes | |
| 21 // the pipe. | |
| 22 // | |
| 23 // Example: | |
| 24 // | |
| 25 // #include "foo.mojom.h" | |
| 26 // | |
| 27 // class FooImpl : public Foo { | |
| 28 // public: | |
| 29 // explicit FooImpl(InterfaceRequest<Foo> request) | |
| 30 // : binding_(this, request.Pass()) {} | |
| 31 // | |
| 32 // // Foo implementation here. | |
| 33 // | |
| 34 // private: | |
| 35 // Binding<Foo> binding_; | |
| 36 // }; | |
| 37 // | |
| 38 // class MyFooFactory : public InterfaceFactory<Foo> { | |
| 39 // public: | |
| 40 // void Create(..., InterfaceRequest<Foo> request) override { | |
| 41 // auto f = new FooImpl(request.Pass()); | |
| 42 // // Do something to manage the lifetime of |f|. Use StrongBinding<> to | |
| 43 // // delete FooImpl on connection errors. | |
| 44 // } | |
| 45 // }; | |
| 46 template <typename Interface> | |
| 47 class Binding : public ErrorHandler { | |
| 48 public: | |
| 49 using Client = typename Interface::Client; | |
| 50 | |
| 51 explicit Binding(Interface* impl) : impl_(impl) { stub_.set_sink(impl_); } | |
| 52 | |
| 53 Binding(Interface* impl, | |
| 54 ScopedMessagePipeHandle handle, | |
| 55 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | |
| 56 : Binding(impl) { | |
| 57 Bind(handle.Pass(), waiter); | |
| 58 } | |
| 59 | |
| 60 Binding(Interface* impl, | |
| 61 InterfacePtr<Interface>* ptr, | |
| 62 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | |
| 63 : Binding(impl) { | |
| 64 Bind(ptr, waiter); | |
| 65 } | |
| 66 | |
| 67 Binding(Interface* impl, | |
| 68 InterfaceRequest<Interface> request, | |
| 69 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | |
| 70 : Binding(impl) { | |
| 71 Bind(request.PassMessagePipe(), waiter); | |
| 72 } | |
| 73 | |
| 74 ~Binding() override { | |
| 75 delete proxy_; | |
| 76 if (internal_router_) { | |
| 77 internal_router_->set_error_handler(nullptr); | |
| 78 delete internal_router_; | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 void Bind( | |
| 83 ScopedMessagePipeHandle handle, | |
| 84 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
| 85 internal::FilterChain filters; | |
| 86 filters.Append<internal::MessageHeaderValidator>(); | |
| 87 filters.Append<typename Interface::RequestValidator_>(); | |
| 88 filters.Append<typename Client::ResponseValidator_>(); | |
| 89 | |
| 90 internal_router_ = | |
| 91 new internal::Router(handle.Pass(), filters.Pass(), waiter); | |
| 92 internal_router_->set_incoming_receiver(&stub_); | |
| 93 internal_router_->set_error_handler(this); | |
| 94 | |
| 95 proxy_ = new typename Client::Proxy_(internal_router_); | |
| 96 } | |
| 97 | |
| 98 void Bind( | |
| 99 InterfacePtr<Interface>* ptr, | |
| 100 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
| 101 MessagePipe pipe; | |
| 102 ptr->Bind(pipe.handle0.Pass(), waiter); | |
| 103 Bind(pipe.handle1.Pass(), waiter); | |
| 104 } | |
| 105 | |
| 106 void Bind( | |
| 107 InterfaceRequest<Interface> request, | |
| 108 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
| 109 Bind(request.PassMessagePipe(), waiter); | |
| 110 } | |
| 111 | |
| 112 bool WaitForIncomingMethodCall() { | |
| 113 MOJO_DCHECK(internal_router_); | |
| 114 return internal_router_->WaitForIncomingMessage(); | |
| 115 } | |
| 116 | |
| 117 void Close() { | |
| 118 MOJO_DCHECK(internal_router_); | |
| 119 internal_router_->CloseMessagePipe(); | |
| 120 } | |
| 121 | |
| 122 void set_error_handler(ErrorHandler* error_handler) { | |
| 123 error_handler_ = error_handler; | |
| 124 } | |
| 125 | |
| 126 // ErrorHandler implementation | |
| 127 void OnConnectionError() override { | |
| 128 if (error_handler_) | |
| 129 error_handler_->OnConnectionError(); | |
| 130 } | |
| 131 | |
| 132 Interface* impl() { return impl_; } | |
| 133 Client* client() { return proxy_; } | |
| 134 | |
| 135 bool is_bound() const { return !!internal_router_; } | |
| 136 | |
| 137 // Exposed for testing, should not generally be used. | |
| 138 internal::Router* internal_router() { return internal_router_; } | |
| 139 | |
| 140 private: | |
| 141 internal::Router* internal_router_ = nullptr; | |
| 142 typename Client::Proxy_* proxy_ = nullptr; | |
| 143 typename Interface::Stub_ stub_; | |
| 144 Interface* impl_; | |
| 145 ErrorHandler* error_handler_ = nullptr; | |
| 146 | |
| 147 MOJO_DISALLOW_COPY_AND_ASSIGN(Binding); | |
| 148 }; | |
| 149 | |
| 150 } // namespace mojo | |
| 151 | |
| 152 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ | |
| OLD | NEW |