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_STRONG_BINDING_H_ | |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ | |
| 7 | |
| 8 #include "mojo/public/c/environment/async_waiter.h" | |
| 9 #include "mojo/public/cpp/bindings/binding.h" | |
| 10 #include "mojo/public/cpp/bindings/error_handler.h" | |
| 11 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
| 12 #include "mojo/public/cpp/bindings/interface_request.h" | |
| 13 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | |
| 14 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | |
| 15 #include "mojo/public/cpp/bindings/lib/router.h" | |
| 16 #include "mojo/public/cpp/system/core.h" | |
| 17 | |
| 18 namespace mojo { | |
| 19 | |
| 20 // This connects an interface implementation strongly to a pipe. When a | |
| 21 // connection error is detected the implementation is deleted. Deleting the | |
| 22 // connector also closes the pipe. | |
| 23 // | |
| 24 // Example of an implementation that is always bound strongly to a pipe | |
| 25 // | |
| 26 // class StronglyBound : public Foo { | |
| 27 // public: | |
| 28 // explicit StronglyBound(ScopedMessagePipeHandle handle) | |
| 29 // : binding_(this, handle.Pass()) {} | |
| 30 // | |
| 31 // // Foo implementation here | |
| 32 // | |
| 33 // private: | |
| 34 // StrongBinding<Foo> binding_; | |
| 35 // }; | |
| 36 // | |
| 37 template <typename Interface> | |
| 38 class StrongBinding : public ErrorHandler { | |
| 39 public: | |
| 40 explicit StrongBinding(Interface* impl) : binding_(impl) { | |
| 41 binding_.set_error_handler(this); | |
| 42 } | |
| 43 | |
| 44 StrongBinding( | |
| 45 Interface* impl, | |
| 46 ScopedMessagePipeHandle handle, | |
| 47 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | |
| 48 : StrongBinding(impl) { | |
| 49 binding_.Bind(handle.Pass(), waiter); | |
| 50 } | |
| 51 | |
| 52 StrongBinding( | |
| 53 Interface* impl, | |
| 54 InterfaceRequest<Interface> request, | |
| 55 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) | |
| 56 : StrongBinding(impl) { | |
| 57 binding_.Bind(request.Pass(), waiter); | |
| 58 } | |
| 59 | |
| 60 ~StrongBinding() override {} | |
| 61 | |
| 62 bool WaitForIncomingMethodCall() { binding_.WaitForIncomingMethodCall(); } | |
| 63 | |
| 64 void set_error_handler(ErrorHandler* error_handler) { | |
| 65 error_handler_ = error_handler; | |
| 66 } | |
| 67 | |
| 68 typename Interface::Client* client() { return binding_.client(); } | |
| 69 // Exposed for testing, should not generally be used. | |
|
DaveMoore
2014/11/13 00:00:36
Nit: name should match Binding::internal_router()
| |
| 70 internal::Router* router() { return binding_.router(); } | |
| 71 | |
| 72 // ErrorHandler implementation | |
| 73 void OnConnectionError() override { | |
| 74 if (error_handler_) | |
| 75 error_handler_->OnConnectionError(); | |
| 76 delete binding_.impl(); | |
| 77 } | |
| 78 | |
| 79 private: | |
| 80 ErrorHandler* error_handler_ = nullptr; | |
| 81 Binding<Interface> binding_; | |
| 82 }; | |
| 83 | |
| 84 } // namespace mojo | |
| 85 | |
| 86 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ | |
| OLD | NEW |