OLD | NEW |
(Empty) | |
| 1 #ifndef MOJO_PUBLIC_CPP_BINDINGS_WEAK_CONNECTOR_H_ |
| 2 #define MOJO_PUBLIC_CPP_BINDINGS_WEAK_CONNECTOR_H_ |
| 3 |
| 4 #include "mojo/public/c/environment/async_waiter.h" |
| 5 #include "mojo/public/cpp/bindings/error_handler.h" |
| 6 #include "mojo/public/cpp/bindings/lib/filter_chain.h" |
| 7 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" |
| 8 #include "mojo/public/cpp/bindings/lib/router.h" |
| 9 #include "mojo/public/cpp/system/core.h" |
| 10 |
| 11 namespace mojo { |
| 12 |
| 13 // This connects an interface implementation strongly to a pipe. When a |
| 14 // connection error is detected the implementation is deleted. Deleting the |
| 15 // connector also closes the pipe. |
| 16 // |
| 17 // Example of an implementation that is always bound strongly to a pipe |
| 18 // |
| 19 // class WeaklyBound : public Foo { |
| 20 // public: |
| 21 // explicit WeaklyBound(ScopedMessagePipeHandle handle) |
| 22 // : connector_(this, handle.Pass()) {} |
| 23 // |
| 24 // // Foo implementation here |
| 25 // |
| 26 // private: |
| 27 // WeakConnector<Foo> connector_; |
| 28 // }; |
| 29 // |
| 30 template <typename Interface> |
| 31 class WeakConnector : public ErrorHandler { |
| 32 typedef typename Interface::Client Client; |
| 33 |
| 34 public: |
| 35 explicit WeakConnector(Interface* impl) : impl_(impl) { |
| 36 stub_.set_sink(impl_); |
| 37 } |
| 38 |
| 39 WeakConnector( |
| 40 Interface* impl, |
| 41 ScopedMessagePipeHandle handle, |
| 42 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) |
| 43 : WeakConnector(impl) { |
| 44 stub_.set_sink(impl_); |
| 45 Bind(handle.Pass(), waiter); |
| 46 } |
| 47 |
| 48 virtual ~WeakConnector() { |
| 49 delete proxy_; |
| 50 if (router_) { |
| 51 router_->set_error_handler(nullptr); |
| 52 delete router_; |
| 53 } |
| 54 } |
| 55 |
| 56 void Bind( |
| 57 ScopedMessagePipeHandle handle, |
| 58 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
| 59 internal::FilterChain filters; |
| 60 filters.Append<internal::MessageHeaderValidator>(); |
| 61 filters.Append<typename Interface::RequestValidator_>(); |
| 62 filters.Append<typename Interface::Client::ResponseValidator_>(); |
| 63 |
| 64 router_ = new internal::Router(handle.Pass(), filters.Pass(), waiter); |
| 65 router_->set_incoming_receiver(&stub_); |
| 66 router_->set_error_handler(this); |
| 67 |
| 68 proxy_ = new typename Client::Proxy_(router_); |
| 69 } |
| 70 |
| 71 void set_error_handler(ErrorHandler* error_handler) { |
| 72 error_handler_ = error_handler; |
| 73 } |
| 74 |
| 75 // ErrorHandler implementation |
| 76 void OnConnectionError() override { |
| 77 if (error_handler_) |
| 78 error_handler_->OnConnectionError(); |
| 79 } |
| 80 |
| 81 typename Interface::Client* client() { return proxy_; } |
| 82 |
| 83 private: |
| 84 internal::Router* router_ = nullptr; |
| 85 typename Client::Proxy_* proxy_ = nullptr; |
| 86 typename Interface::Stub_ stub_; |
| 87 Interface* impl_; |
| 88 ErrorHandler* error_handler_ = nullptr; |
| 89 }; |
| 90 |
| 91 } // namespace mojo |
| 92 |
| 93 #endif // MOJO_PUBLIC_CPP_BINDINGS_WEAK_CONNECTOR_H_ |
OLD | NEW |