OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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_LIB_BINDING_STATE_H_ | |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_ | |
7 | |
8 #include "base/logging.h" | |
9 #include "base/macros.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "mojo/public/c/environment/async_waiter.h" | |
13 #include "mojo/public/cpp/bindings/associated_group.h" | |
14 #include "mojo/public/cpp/bindings/callback.h" | |
15 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
16 #include "mojo/public/cpp/bindings/interface_ptr_info.h" | |
17 #include "mojo/public/cpp/bindings/interface_request.h" | |
18 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | |
19 #include "mojo/public/cpp/bindings/lib/interface_endpoint_client.h" | |
20 #include "mojo/public/cpp/bindings/lib/interface_id.h" | |
21 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | |
22 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" | |
23 #include "mojo/public/cpp/bindings/lib/router.h" | |
24 #include "mojo/public/cpp/bindings/lib/scoped_interface_endpoint_handle.h" | |
25 #include "mojo/public/cpp/system/core.h" | |
26 | |
27 namespace mojo { | |
28 namespace internal { | |
29 | |
30 template <typename Interface, bool kUsesMultiplexRouter> | |
sky
2015/11/24 22:20:45
kUsesMultiplexRouter isn't a constant. use_multipl
yzshen1
2015/11/25 00:26:36
Done.
| |
31 class BindingState; | |
32 | |
33 template <typename Interface> | |
34 class BindingState<Interface, false> { | |
sky
2015/11/24 22:20:45
Add a description.
yzshen1
2015/11/25 00:26:36
Done.
| |
35 public: | |
36 explicit BindingState(Interface* impl) : impl_(impl) { | |
37 stub_.set_sink(impl_); | |
38 } | |
39 | |
40 ~BindingState() { | |
41 if (router_) | |
42 Close(); | |
43 } | |
44 | |
45 void Bind(ScopedMessagePipeHandle handle, const MojoAsyncWaiter* waiter) { | |
46 DCHECK(!router_); | |
47 internal::FilterChain filters; | |
48 filters.Append<internal::MessageHeaderValidator>(); | |
49 filters.Append<typename Interface::RequestValidator_>(); | |
50 | |
51 router_ = new internal::Router(handle.Pass(), filters.Pass(), waiter); | |
52 router_->set_incoming_receiver(&stub_); | |
53 router_->set_connection_error_handler( | |
54 [this]() { connection_error_handler_.Run(); }); | |
55 } | |
56 | |
57 void PauseIncomingMethodCallProcessing() { | |
sky
2015/11/24 22:20:45
What will the semantics of pausing/resuming be wit
yzshen1
2015/11/25 00:26:36
Documented in binding.h and also interface_ptr.h t
| |
58 DCHECK(router_); | |
59 router_->PauseIncomingMethodCallProcessing(); | |
60 } | |
61 void ResumeIncomingMethodCallProcessing() { | |
62 DCHECK(router_); | |
63 router_->ResumeIncomingMethodCallProcessing(); | |
64 } | |
65 | |
66 bool WaitForIncomingMethodCall( | |
67 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE) { | |
68 DCHECK(router_); | |
69 return router_->WaitForIncomingMessage(deadline); | |
70 } | |
71 | |
72 void Close() { | |
73 DCHECK(router_); | |
74 router_->CloseMessagePipe(); | |
75 DestroyRouter(); | |
76 } | |
77 | |
78 InterfaceRequest<Interface> Unbind() { | |
79 InterfaceRequest<Interface> request = | |
80 MakeRequest<Interface>(router_->PassMessagePipe()); | |
81 DestroyRouter(); | |
82 return request.Pass(); | |
83 } | |
84 | |
85 void set_connection_error_handler(const Closure& error_handler) { | |
86 connection_error_handler_ = error_handler; | |
87 } | |
88 | |
89 Interface* impl() { return impl_; } | |
90 | |
91 bool is_bound() const { return !!router_; } | |
92 | |
93 MessagePipeHandle handle() const { | |
94 DCHECK(is_bound()); | |
95 return router_->handle(); | |
96 } | |
97 | |
98 AssociatedGroup* associated_group() { return nullptr; } | |
99 | |
100 void EnableTestingMode() { | |
101 DCHECK(is_bound()); | |
102 router_->EnableTestingMode(); | |
103 } | |
104 | |
105 private: | |
106 void DestroyRouter() { | |
107 router_->set_connection_error_handler(Closure()); | |
108 delete router_; | |
109 router_ = nullptr; | |
110 } | |
111 | |
112 internal::Router* router_ = nullptr; | |
113 typename Interface::Stub_ stub_; | |
114 Interface* impl_; | |
115 Closure connection_error_handler_; | |
116 | |
117 DISALLOW_COPY_AND_ASSIGN(BindingState); | |
118 }; | |
119 | |
120 template <typename Interface> | |
121 class BindingState<Interface, true> { | |
122 public: | |
123 explicit BindingState(Interface* impl) : impl_(impl) { | |
124 stub_.set_sink(impl_); | |
125 } | |
126 | |
127 ~BindingState() { | |
128 if (router_) | |
129 Close(); | |
130 } | |
131 | |
132 void Bind(ScopedMessagePipeHandle handle, const MojoAsyncWaiter* waiter) { | |
133 DCHECK(!router_); | |
134 | |
135 router_ = new internal::MultiplexRouter(false, handle.Pass(), waiter); | |
136 endpoint_client_.reset(new internal::InterfaceEndpointClient( | |
137 router_->CreateLocalEndpointHandle(internal::kMasterInterfaceId), | |
138 &stub_, make_scoped_ptr(new typename Interface::RequestValidator_()))); | |
139 | |
140 endpoint_client_->set_connection_error_handler( | |
141 [this]() { connection_error_handler_.Run(); }); | |
142 } | |
143 | |
144 void PauseIncomingMethodCallProcessing() { | |
145 DCHECK(router_); | |
146 router_->PauseIncomingMethodCallProcessing(); | |
147 } | |
148 void ResumeIncomingMethodCallProcessing() { | |
149 DCHECK(router_); | |
150 router_->ResumeIncomingMethodCallProcessing(); | |
151 } | |
152 | |
153 bool WaitForIncomingMethodCall(MojoDeadline deadline) { | |
154 DCHECK(router_); | |
155 return router_->WaitForIncomingMessage(deadline); | |
156 } | |
157 | |
158 void Close() { | |
159 DCHECK(router_); | |
160 endpoint_client_.reset(); | |
161 router_->CloseMessagePipe(); | |
162 router_ = nullptr; | |
163 } | |
164 | |
165 InterfaceRequest<Interface> Unbind() { | |
166 endpoint_client_.reset(); | |
167 InterfaceRequest<Interface> request = | |
168 MakeRequest<Interface>(router_->PassMessagePipe()); | |
169 router_ = nullptr; | |
170 return request.Pass(); | |
171 } | |
172 | |
173 void set_connection_error_handler(const Closure& error_handler) { | |
174 connection_error_handler_ = error_handler; | |
175 } | |
176 | |
177 Interface* impl() { return impl_; } | |
178 | |
179 bool is_bound() const { return !!router_; } | |
180 | |
181 MessagePipeHandle handle() const { | |
182 DCHECK(is_bound()); | |
183 return router_->handle(); | |
184 } | |
185 | |
186 AssociatedGroup* associated_group() { | |
187 return endpoint_client_ ? endpoint_client_->associated_group() : nullptr; | |
188 } | |
189 | |
190 void EnableTestingMode() { | |
191 DCHECK(is_bound()); | |
192 router_->EnableTestingMode(); | |
193 } | |
194 | |
195 private: | |
196 scoped_refptr<internal::MultiplexRouter> router_; | |
197 scoped_ptr<internal::InterfaceEndpointClient> endpoint_client_; | |
198 | |
199 typename Interface::Stub_ stub_; | |
200 Interface* impl_; | |
201 Closure connection_error_handler_; | |
202 | |
203 DISALLOW_COPY_AND_ASSIGN(BindingState); | |
204 }; | |
205 | |
206 } // namesapce internal | |
207 } // namespace mojo | |
208 | |
209 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_ | |
OLD | NEW |