| 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_STRONG_BINDING_H_ | |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ | |
| 7 | |
| 8 #include <assert.h> | |
| 9 | |
| 10 #include "mojo/public/c/environment/async_waiter.h" | |
| 11 #include "mojo/public/cpp/bindings/binding.h" | |
| 12 #include "mojo/public/cpp/bindings/error_handler.h" | |
| 13 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
| 14 #include "mojo/public/cpp/bindings/interface_request.h" | |
| 15 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | |
| 16 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | |
| 17 #include "mojo/public/cpp/bindings/lib/router.h" | |
| 18 #include "mojo/public/cpp/system/core.h" | |
| 19 | |
| 20 namespace mojo { | |
| 21 | |
| 22 // This connects an interface implementation strongly to a pipe. When a | |
| 23 // connection error is detected the implementation is deleted. Deleting the | |
| 24 // connector also closes the pipe. | |
| 25 // | |
| 26 // Example of an implementation that is always bound strongly to a pipe | |
| 27 // | |
| 28 // class StronglyBound : public Foo { | |
| 29 // public: | |
| 30 // explicit StronglyBound(InterfaceRequest<Foo> request) | |
| 31 // : binding_(this, request.Pass()) {} | |
| 32 // | |
| 33 // // Foo implementation here | |
| 34 // | |
| 35 // private: | |
| 36 // StrongBinding<Foo> binding_; | |
| 37 // }; | |
| 38 // | |
| 39 // class MyFooFactory : public InterfaceFactory<Foo> { | |
| 40 // public: | |
| 41 // void Create(..., InterfaceRequest<Foo> request) override { | |
| 42 // new StronglyBound(request.Pass()); // The binding now owns the | |
| 43 // // instance of StronglyBound. | |
| 44 // } | |
| 45 // }; | |
| 46 template <typename Interface> | |
| 47 class StrongBinding : public ErrorHandler { | |
| 48 public: | |
| 49 explicit StrongBinding(Interface* impl) : binding_(impl) { | |
| 50 binding_.set_error_handler(this); | |
| 51 } | |
| 52 | |
| 53 StrongBinding( | |
| 54 Interface* impl, | |
| 55 ScopedMessagePipeHandle handle, | |
| 56 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | |
| 57 : StrongBinding(impl) { | |
| 58 binding_.Bind(handle.Pass(), waiter); | |
| 59 } | |
| 60 | |
| 61 StrongBinding( | |
| 62 Interface* impl, | |
| 63 InterfacePtr<Interface>* ptr, | |
| 64 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | |
| 65 : StrongBinding(impl) { | |
| 66 binding_.Bind(ptr, waiter); | |
| 67 } | |
| 68 | |
| 69 StrongBinding( | |
| 70 Interface* impl, | |
| 71 InterfaceRequest<Interface> request, | |
| 72 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | |
| 73 : StrongBinding(impl) { | |
| 74 binding_.Bind(request.Pass(), waiter); | |
| 75 } | |
| 76 | |
| 77 ~StrongBinding() override {} | |
| 78 | |
| 79 void Bind( | |
| 80 ScopedMessagePipeHandle handle, | |
| 81 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
| 82 assert(!binding_.is_bound()); | |
| 83 binding_.Bind(handle.Pass(), waiter); | |
| 84 } | |
| 85 | |
| 86 void Bind( | |
| 87 InterfacePtr<Interface>* ptr, | |
| 88 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
| 89 assert(!binding_.is_bound()); | |
| 90 binding_.Bind(ptr, waiter); | |
| 91 } | |
| 92 | |
| 93 void Bind( | |
| 94 InterfaceRequest<Interface> request, | |
| 95 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | |
| 96 assert(!binding_.is_bound()); | |
| 97 binding_.Bind(request.Pass(), waiter); | |
| 98 } | |
| 99 | |
| 100 bool WaitForIncomingMethodCall() { | |
| 101 return binding_.WaitForIncomingMethodCall(); | |
| 102 } | |
| 103 | |
| 104 void set_error_handler(ErrorHandler* error_handler) { | |
| 105 error_handler_ = error_handler; | |
| 106 } | |
| 107 | |
| 108 Interface* impl() { return binding_.impl(); } | |
| 109 typename Interface::Client* client() { return binding_.client(); } | |
| 110 // Exposed for testing, should not generally be used. | |
| 111 internal::Router* internal_router() { return binding_.internal_router(); } | |
| 112 | |
| 113 // ErrorHandler implementation | |
| 114 void OnConnectionError() override { | |
| 115 if (error_handler_) | |
| 116 error_handler_->OnConnectionError(); | |
| 117 delete binding_.impl(); | |
| 118 } | |
| 119 | |
| 120 private: | |
| 121 ErrorHandler* error_handler_ = nullptr; | |
| 122 Binding<Interface> binding_; | |
| 123 }; | |
| 124 | |
| 125 } // namespace mojo | |
| 126 | |
| 127 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ | |
| OLD | NEW |