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() { | |
63 return binding_.WaitForIncomingMethodCall(); | |
64 } | |
65 | |
66 void set_error_handler(ErrorHandler* error_handler) { | |
67 error_handler_ = error_handler; | |
68 } | |
69 | |
70 typename Interface::Client* client() { return binding_.client(); } | |
71 // Exposed for testing, should not generally be used. | |
72 internal::Router* internal_router() { return binding_.internal_router(); } | |
73 | |
74 // ErrorHandler implementation | |
75 void OnConnectionError() override { | |
76 if (error_handler_) | |
77 error_handler_->OnConnectionError(); | |
78 delete binding_.impl(); | |
79 } | |
80 | |
81 private: | |
82 ErrorHandler* error_handler_ = nullptr; | |
83 Binding<Interface> binding_; | |
84 }; | |
85 | |
86 } // namespace mojo | |
87 | |
88 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ | |
OLD | NEW |