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