Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(889)

Side by Side Diff: mojo/public/cpp/bindings/lib/binding_state.h

Issue 2203243002: Reduce code size of BindingState (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@before-sharing
Patch Set: --similarity=10 Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 13 matching lines...) Expand all
24 #include "mojo/public/cpp/bindings/lib/filter_chain.h" 24 #include "mojo/public/cpp/bindings/lib/filter_chain.h"
25 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" 25 #include "mojo/public/cpp/bindings/lib/multiplex_router.h"
26 #include "mojo/public/cpp/bindings/lib/router.h" 26 #include "mojo/public/cpp/bindings/lib/router.h"
27 #include "mojo/public/cpp/bindings/message_header_validator.h" 27 #include "mojo/public/cpp/bindings/message_header_validator.h"
28 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" 28 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
29 #include "mojo/public/cpp/system/core.h" 29 #include "mojo/public/cpp/system/core.h"
30 30
31 namespace mojo { 31 namespace mojo {
32 namespace internal { 32 namespace internal {
33 33
34 // Non-templated base class to reduce the amount of code generated for every
35 // mojom file.
Ken Rockot(use gerrit already) 2016/08/03 14:55:27 nit: Maybe document that it's a base class used fo
36 class SimpleBindingStateBase {
Ken Rockot(use gerrit already) 2016/08/03 14:55:28 nit: Maybe drop the "Base" suffix? Seems superfluo
tibell 2016/08/03 23:42:51 Done.
37 public:
38 SimpleBindingStateBase();
39 ~SimpleBindingStateBase();
40
41 void Bind(
Ken Rockot(use gerrit already) 2016/08/03 14:55:28 nitty nit: I'd prefer we make this protected and c
tibell 2016/08/03 23:42:51 Done.
42 ScopedMessagePipeHandle handle,
43 scoped_refptr<base::SingleThreadTaskRunner> runner,
44 const char* interface_name,
45 MessageFilter* request_validator,
46 bool has_sync_methods,
47 MessageReceiverWithResponderStatus* stub);
48
49 bool HasAssociatedInterfaces() const { return false; }
50
51 void PauseIncomingMethodCallProcessing();
52 void ResumeIncomingMethodCallProcessing();
53
54 bool WaitForIncomingMethodCall(
55 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE);
56
57 void Close();
58
59 void set_connection_error_handler(const base::Closure& error_handler) {
60 DCHECK(is_bound());
61 connection_error_handler_ = error_handler;
62 }
63
64 bool is_bound() const { return !!router_; }
65
66 MessagePipeHandle handle() const {
67 DCHECK(is_bound());
68 return router_->handle();
69 }
70
71 AssociatedGroup* associated_group() { return nullptr; }
72
73 void EnableTestingMode();
74
75 protected:
76 void DestroyRouter();
77
78 internal::Router* router_ = nullptr;
79 base::Closure connection_error_handler_;
80
81 private:
82 void RunConnectionErrorHandler();
83 };
84
34 template <typename Interface, bool use_multiplex_router> 85 template <typename Interface, bool use_multiplex_router>
35 class BindingState; 86 class BindingState;
36 87
37 // Uses a single-threaded, dedicated router. If |Interface| doesn't have any 88 // Uses a single-threaded, dedicated router. If |Interface| doesn't have any
38 // methods to pass associated interface pointers or requests, there won't be 89 // methods to pass associated interface pointers or requests, there won't be
39 // multiple interfaces running on the underlying message pipe. In that case, we 90 // multiple interfaces running on the underlying message pipe. In that case, we
40 // can use this specialization to reduce cost. 91 // can use this specialization to reduce cost.
41 template <typename Interface> 92 template <typename Interface>
42 class BindingState<Interface, false> { 93 class BindingState<Interface, false> : public SimpleBindingStateBase {
43 public: 94 public:
44 explicit BindingState(Interface* impl) : impl_(impl) { 95 explicit BindingState(Interface* impl) : impl_(impl) {
45 stub_.set_sink(impl_); 96 stub_.set_sink(impl_);
46 } 97 }
47 98
48 ~BindingState() { Close(); } 99 ~BindingState() { Close(); }
49 100
50 void Bind(ScopedMessagePipeHandle handle, 101 void Bind(ScopedMessagePipeHandle handle,
51 scoped_refptr<base::SingleThreadTaskRunner> runner) { 102 scoped_refptr<base::SingleThreadTaskRunner> runner) {
52 DCHECK(!router_); 103 DCHECK(!router_);
53 internal::FilterChain filters; 104 SimpleBindingStateBase::Bind(
54 filters.Append<MessageHeaderValidator>(Interface::Name_); 105 std::move(handle), runner, Interface::Name_,
55 filters.Append<typename Interface::RequestValidator_>(); 106 new typename Interface::RequestValidator_(), Interface::HasSyncMethods_,
56 107 &stub_);
57 router_ =
58 new internal::Router(std::move(handle), std::move(filters),
59 Interface::HasSyncMethods_, std::move(runner));
60 router_->set_incoming_receiver(&stub_);
61 router_->set_connection_error_handler(
62 base::Bind(&BindingState::RunConnectionErrorHandler,
63 base::Unretained(this)));
64 }
65
66 bool HasAssociatedInterfaces() const { return false; }
67
68 void PauseIncomingMethodCallProcessing() {
69 DCHECK(router_);
70 router_->PauseIncomingMethodCallProcessing();
71 }
72 void ResumeIncomingMethodCallProcessing() {
73 DCHECK(router_);
74 router_->ResumeIncomingMethodCallProcessing();
75 }
76
77 bool WaitForIncomingMethodCall(
78 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE) {
79 DCHECK(router_);
80 return router_->WaitForIncomingMessage(deadline);
81 }
82
83 void Close() {
84 if (!router_)
85 return;
86
87 router_->CloseMessagePipe();
88 DestroyRouter();
89 } 108 }
90 109
91 InterfaceRequest<Interface> Unbind() { 110 InterfaceRequest<Interface> Unbind() {
92 InterfaceRequest<Interface> request = 111 InterfaceRequest<Interface> request =
93 MakeRequest<Interface>(router_->PassMessagePipe()); 112 MakeRequest<Interface>(router_->PassMessagePipe());
94 DestroyRouter(); 113 DestroyRouter();
95 return std::move(request); 114 return std::move(request);
96 } 115 }
97 116
117 Interface* impl() { return impl_; }
118
119 private:
120 typename Interface::Stub_ stub_;
121 Interface* impl_;
122
123 DISALLOW_COPY_AND_ASSIGN(BindingState);
124 };
125
126 // Non-templated base class to reduce the amount of code generated for every
127 // mojom file.
128 class MultiplexedBindingStateBase {
Ken Rockot(use gerrit already) 2016/08/03 14:55:28 nit: Roughly the same comments about "Base" and do
tibell 2016/08/03 23:42:51 Done.
129 public:
130 MultiplexedBindingStateBase();
131 ~MultiplexedBindingStateBase();
132
133 void Bind(ScopedMessagePipeHandle handle,
134 scoped_refptr<base::SingleThreadTaskRunner> runner,
135 const char* interface_name,
136 std::unique_ptr<MessageFilter> request_validator,
137 bool has_sync_methods,
138 MessageReceiverWithResponderStatus* stub);
139
140 bool HasAssociatedInterfaces() const;
141
142 void PauseIncomingMethodCallProcessing();
143 void ResumeIncomingMethodCallProcessing();
144
145 bool WaitForIncomingMethodCall(
146 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE);
147
148 void Close();
149
98 void set_connection_error_handler(const base::Closure& error_handler) { 150 void set_connection_error_handler(const base::Closure& error_handler) {
99 DCHECK(is_bound()); 151 DCHECK(is_bound());
100 connection_error_handler_ = error_handler; 152 connection_error_handler_ = error_handler;
101 } 153 }
102 154
103 Interface* impl() { return impl_; }
104
105 bool is_bound() const { return !!router_; } 155 bool is_bound() const { return !!router_; }
106 156
107 MessagePipeHandle handle() const { 157 MessagePipeHandle handle() const {
108 DCHECK(is_bound()); 158 DCHECK(is_bound());
109 return router_->handle(); 159 return router_->handle();
110 } 160 }
111 161
112 AssociatedGroup* associated_group() { return nullptr; } 162 AssociatedGroup* associated_group() {
113 163 return endpoint_client_ ? endpoint_client_->associated_group() : nullptr;
114 void EnableTestingMode() {
115 DCHECK(is_bound());
116 router_->EnableTestingMode();
117 } 164 }
118 165
119 private: 166 void EnableTestingMode();
120 void DestroyRouter() {
121 router_->set_connection_error_handler(base::Closure());
122 delete router_;
123 router_ = nullptr;
124 connection_error_handler_.Reset();
125 }
126 167
127 void RunConnectionErrorHandler() { 168 protected:
128 if (!connection_error_handler_.is_null()) 169 scoped_refptr<internal::MultiplexRouter> router_;
129 connection_error_handler_.Run(); 170 std::unique_ptr<InterfaceEndpointClient> endpoint_client_;
130 }
131
132 internal::Router* router_ = nullptr;
133 typename Interface::Stub_ stub_;
134 Interface* impl_;
135 base::Closure connection_error_handler_; 171 base::Closure connection_error_handler_;
136 172
137 DISALLOW_COPY_AND_ASSIGN(BindingState); 173 private:
174 void RunConnectionErrorHandler();
138 }; 175 };
139 176
140 // Uses a multiplexing router. If |Interface| has methods to pass associated 177 // Uses a multiplexing router. If |Interface| has methods to pass associated
141 // interface pointers or requests, this specialization should be used. 178 // interface pointers or requests, this specialization should be used.
142 template <typename Interface> 179 template <typename Interface>
143 class BindingState<Interface, true> { 180 class BindingState<Interface, true> : public MultiplexedBindingStateBase {
144 public: 181 public:
145 explicit BindingState(Interface* impl) : impl_(impl) { 182 explicit BindingState(Interface* impl) : impl_(impl) {
146 stub_.set_sink(impl_); 183 stub_.set_sink(impl_);
147 } 184 }
148 185
149 ~BindingState() { Close(); } 186 ~BindingState() { Close(); }
150 187
151 void Bind(ScopedMessagePipeHandle handle, 188 void Bind(ScopedMessagePipeHandle handle,
152 scoped_refptr<base::SingleThreadTaskRunner> runner) { 189 scoped_refptr<base::SingleThreadTaskRunner> runner) {
153 DCHECK(!router_); 190 MultiplexedBindingStateBase::Bind(
154 191 std::move(handle), runner, Interface::Name_,
155 router_ = new internal::MultiplexRouter(false, std::move(handle), runner); 192 base::WrapUnique(new typename Interface::RequestValidator_()),
156 router_->SetMasterInterfaceName(Interface::Name_); 193 Interface::HasSyncMethods_, &stub_);
157 stub_.serialization_context()->group_controller = router_; 194 stub_.serialization_context()->group_controller = router_;
158
159 endpoint_client_.reset(new InterfaceEndpointClient(
160 router_->CreateLocalEndpointHandle(kMasterInterfaceId),
161 &stub_, base::WrapUnique(new typename Interface::RequestValidator_()),
162 Interface::HasSyncMethods_, std::move(runner)));
163
164 endpoint_client_->set_connection_error_handler(
165 base::Bind(&BindingState::RunConnectionErrorHandler,
166 base::Unretained(this)));
167 }
168
169 bool HasAssociatedInterfaces() const {
170 return router_ ? router_->HasAssociatedEndpoints() : false;
171 }
172
173 void PauseIncomingMethodCallProcessing() {
174 DCHECK(router_);
175 router_->PauseIncomingMethodCallProcessing();
176 }
177 void ResumeIncomingMethodCallProcessing() {
178 DCHECK(router_);
179 router_->ResumeIncomingMethodCallProcessing();
180 }
181
182 bool WaitForIncomingMethodCall(
183 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE) {
184 DCHECK(router_);
185 return router_->WaitForIncomingMessage(deadline);
186 }
187
188 void Close() {
189 if (!router_)
190 return;
191
192 endpoint_client_.reset();
193 router_->CloseMessagePipe();
194 router_ = nullptr;
195 connection_error_handler_.Reset();
196 } 195 }
197 196
198 InterfaceRequest<Interface> Unbind() { 197 InterfaceRequest<Interface> Unbind() {
199 endpoint_client_.reset(); 198 endpoint_client_.reset();
200 InterfaceRequest<Interface> request = 199 InterfaceRequest<Interface> request =
201 MakeRequest<Interface>(router_->PassMessagePipe()); 200 MakeRequest<Interface>(router_->PassMessagePipe());
202 router_ = nullptr; 201 router_ = nullptr;
203 connection_error_handler_.Reset(); 202 connection_error_handler_.Reset();
204 return request; 203 return request;
205 } 204 }
206 205
207 void set_connection_error_handler(const base::Closure& error_handler) {
208 DCHECK(is_bound());
209 connection_error_handler_ = error_handler;
210 }
211
212 Interface* impl() { return impl_; } 206 Interface* impl() { return impl_; }
213 207
214 bool is_bound() const { return !!router_; }
215
216 MessagePipeHandle handle() const {
217 DCHECK(is_bound());
218 return router_->handle();
219 }
220
221 AssociatedGroup* associated_group() {
222 return endpoint_client_ ? endpoint_client_->associated_group() : nullptr;
223 }
224
225 void EnableTestingMode() {
226 DCHECK(is_bound());
227 router_->EnableTestingMode();
228 }
229
230 private: 208 private:
231 void RunConnectionErrorHandler() {
232 if (!connection_error_handler_.is_null())
233 connection_error_handler_.Run();
234 }
235
236 scoped_refptr<internal::MultiplexRouter> router_;
237 std::unique_ptr<InterfaceEndpointClient> endpoint_client_;
238
239 typename Interface::Stub_ stub_; 209 typename Interface::Stub_ stub_;
240 Interface* impl_; 210 Interface* impl_;
241 base::Closure connection_error_handler_;
242 211
243 DISALLOW_COPY_AND_ASSIGN(BindingState); 212 DISALLOW_COPY_AND_ASSIGN(BindingState);
244 }; 213 };
245 214
246 } // namesapce internal 215 } // namesapce internal
247 } // namespace mojo 216 } // namespace mojo
248 217
249 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_ 218 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698