Chromium Code Reviews| 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_CONNECTOR_H_ | |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_CONNECTOR_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/lib/filter_chain.h" | |
| 12 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | |
| 13 #include "mojo/public/cpp/bindings/lib/router.h" | |
| 14 #include "mojo/public/cpp/environment/logging.h" | |
| 15 #include "mojo/public/cpp/system/core.h" | |
| 16 | |
| 17 namespace mojo { | |
| 18 | |
| 19 // This connects an interface implementation a pipe. Deleting the connector | |
|
DaveMoore
2014/11/11 17:18:45
nit: "to a pipe"
| |
| 20 // closes the pipe. | |
| 21 // | |
| 22 // Example: | |
| 23 // | |
| 24 // #include "foo.mojom.h" | |
| 25 // | |
| 26 // class FooImpl : public Foo { | |
| 27 // public: | |
| 28 // explicit FooImpl(ScopedMessagePipeHandle handle) | |
| 29 // : connector_(this, handle.Pass()) {} | |
| 30 // | |
| 31 // // Foo implementation here. | |
| 32 // | |
| 33 // private: | |
| 34 // Connector<Foo> connector_; | |
| 35 // }; | |
| 36 // | |
| 37 template <typename Interface> | |
| 38 class Connector : public ErrorHandler { | |
| 39 public: | |
| 40 using Client = typename Interface::Client; | |
| 41 | |
| 42 explicit Connector(Interface* impl) : impl_(impl) { stub_.set_sink(impl_); } | |
| 43 | |
| 44 Connector( | |
| 45 Interface* impl, | |
| 46 ScopedMessagePipeHandle handle, | |
| 47 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | |
| 48 : Connector(impl) { | |
| 49 stub_.set_sink(impl_); | |
| 50 Bind(handle.Pass(), waiter); | |
| 51 } | |
| 52 | |
| 53 virtual ~Connector() { | |
| 54 delete proxy_; | |
| 55 if (router_) { | |
| 56 router_->set_error_handler(nullptr); | |
| 57 delete router_; | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 void Bind( | |
| 62 ScopedMessagePipeHandle handle, | |
| 63 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
| 64 internal::FilterChain filters; | |
| 65 filters.Append<internal::MessageHeaderValidator>(); | |
| 66 filters.Append<typename Interface::RequestValidator_>(); | |
| 67 filters.Append<typename Client::ResponseValidator_>(); | |
| 68 | |
| 69 router_ = new internal::Router(handle.Pass(), filters.Pass(), waiter); | |
| 70 router_->set_incoming_receiver(&stub_); | |
| 71 router_->set_error_handler(this); | |
| 72 | |
| 73 proxy_ = new typename Client::Proxy_(router_); | |
| 74 } | |
| 75 | |
| 76 void Bind( | |
| 77 InterfacePtr<Interface>* ptr, | |
| 78 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
| 79 MessagePipe pipe; | |
| 80 ptr->Bind(pipe.handle0.Pass(), waiter); | |
| 81 Bind(pipe.handle1.Pass(), waiter); | |
| 82 } | |
| 83 | |
| 84 bool WaitForIncomingMethodCall() { | |
| 85 MOJO_DCHECK(router_); | |
| 86 return router_->WaitForIncomingMessage(); | |
| 87 } | |
| 88 | |
| 89 void set_error_handler(ErrorHandler* error_handler) { | |
| 90 error_handler_ = error_handler; | |
| 91 } | |
| 92 | |
| 93 // ErrorHandler implementation | |
| 94 void OnConnectionError() override { | |
| 95 if (error_handler_) | |
| 96 error_handler_->OnConnectionError(); | |
| 97 } | |
| 98 | |
| 99 Client* client() { return proxy_; } | |
| 100 | |
| 101 // Exposed for testing, should not generally be used. | |
| 102 internal::Router* router() { return router_; } | |
| 103 | |
| 104 protected: | |
| 105 Interface* impl() { return impl_; } | |
| 106 | |
| 107 private: | |
| 108 internal::Router* router_ = nullptr; | |
| 109 typename Client::Proxy_* proxy_ = nullptr; | |
| 110 typename Interface::Stub_ stub_; | |
| 111 Interface* impl_; | |
| 112 ErrorHandler* error_handler_ = nullptr; | |
| 113 | |
| 114 MOJO_DISALLOW_COPY_AND_ASSIGN(Connector); | |
| 115 }; | |
| 116 | |
| 117 } // namespace mojo | |
| 118 | |
| 119 #endif // MOJO_PUBLIC_CPP_BINDINGS_CONNECTOR_H_ | |
| OLD | NEW |