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

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

Issue 2532053004: Mojo C++ bindings: remove the single-threaded Router. (Closed)
Patch Set: . Created 4 years 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
« no previous file with comments | « mojo/public/cpp/bindings/interface_ptr.h ('k') | mojo/public/cpp/bindings/lib/binding_state.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/callback.h" 13 #include "base/callback.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/memory/ptr_util.h" 16 #include "base/memory/ptr_util.h"
17 #include "base/memory/ref_counted.h" 17 #include "base/memory/ref_counted.h"
18 #include "base/single_thread_task_runner.h" 18 #include "base/single_thread_task_runner.h"
19 #include "mojo/public/cpp/bindings/associated_group.h" 19 #include "mojo/public/cpp/bindings/associated_group.h"
20 #include "mojo/public/cpp/bindings/bindings_export.h" 20 #include "mojo/public/cpp/bindings/bindings_export.h"
21 #include "mojo/public/cpp/bindings/connection_error_callback.h" 21 #include "mojo/public/cpp/bindings/connection_error_callback.h"
22 #include "mojo/public/cpp/bindings/filter_chain.h" 22 #include "mojo/public/cpp/bindings/filter_chain.h"
23 #include "mojo/public/cpp/bindings/interface_endpoint_client.h" 23 #include "mojo/public/cpp/bindings/interface_endpoint_client.h"
24 #include "mojo/public/cpp/bindings/interface_id.h" 24 #include "mojo/public/cpp/bindings/interface_id.h"
25 #include "mojo/public/cpp/bindings/interface_ptr.h" 25 #include "mojo/public/cpp/bindings/interface_ptr.h"
26 #include "mojo/public/cpp/bindings/interface_ptr_info.h" 26 #include "mojo/public/cpp/bindings/interface_ptr_info.h"
27 #include "mojo/public/cpp/bindings/interface_request.h" 27 #include "mojo/public/cpp/bindings/interface_request.h"
28 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" 28 #include "mojo/public/cpp/bindings/lib/multiplex_router.h"
29 #include "mojo/public/cpp/bindings/lib/router.h"
30 #include "mojo/public/cpp/bindings/message_header_validator.h" 29 #include "mojo/public/cpp/bindings/message_header_validator.h"
31 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" 30 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
32 #include "mojo/public/cpp/system/core.h" 31 #include "mojo/public/cpp/system/core.h"
33 32
34 namespace mojo { 33 namespace mojo {
35 namespace internal { 34 namespace internal {
36 35
37 // Base class used for templated binding primitives which bind a pipe 36 class MOJO_CPP_BINDINGS_EXPORT BindingStateBase {
38 // exclusively to a single interface.
39 class MOJO_CPP_BINDINGS_EXPORT SimpleBindingState {
40 public: 37 public:
41 SimpleBindingState(); 38 BindingStateBase();
42 ~SimpleBindingState(); 39 ~BindingStateBase();
43 40
44 void AddFilter(std::unique_ptr<MessageReceiver> filter); 41 void AddFilter(std::unique_ptr<MessageReceiver> filter);
45 42
46 bool HasAssociatedInterfaces() const { return false; }
47
48 void PauseIncomingMethodCallProcessing();
49 void ResumeIncomingMethodCallProcessing();
50
51 bool WaitForIncomingMethodCall(
52 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE);
53
54 void Close();
55 void CloseWithReason(uint32_t custom_reason, const std::string& description);
56
57 void set_connection_error_handler(const base::Closure& error_handler) {
58 DCHECK(is_bound());
59 router_->set_connection_error_handler(error_handler);
60 }
61
62 void set_connection_error_with_reason_handler(
63 const ConnectionErrorWithReasonCallback& error_handler) {
64 DCHECK(is_bound());
65 router_->set_connection_error_with_reason_handler(error_handler);
66 }
67
68 bool is_bound() const { return !!router_; }
69
70 MessagePipeHandle handle() const {
71 DCHECK(is_bound());
72 return router_->handle();
73 }
74
75 AssociatedGroup* associated_group() { return nullptr; }
76
77 void FlushForTesting();
78
79 void EnableTestingMode();
80
81 protected:
82 void BindInternal(ScopedMessagePipeHandle handle,
83 scoped_refptr<base::SingleThreadTaskRunner> runner,
84 const char* interface_name,
85 std::unique_ptr<MessageReceiver> request_validator,
86 bool has_sync_methods,
87 MessageReceiverWithResponderStatus* stub,
88 uint32_t interface_version);
89
90 void DestroyRouter();
91
92 internal::Router* router_ = nullptr;
93 };
94
95 template <typename Interface, bool use_multiplex_router, typename ImplRefTraits>
96 class BindingState;
97
98 // Uses a single-threaded, dedicated router. If |Interface| doesn't have any
99 // methods to pass associated interface pointers or requests, there won't be
100 // multiple interfaces running on the underlying message pipe. In that case, we
101 // can use this specialization to reduce cost.
102 template <typename Interface, typename ImplRefTraits>
103 class BindingState<Interface, false, ImplRefTraits>
104 : public SimpleBindingState {
105 public:
106 using ImplPointerType = typename ImplRefTraits::PointerType;
107
108 explicit BindingState(ImplPointerType impl) {
109 stub_.set_sink(std::move(impl));
110 }
111
112 ~BindingState() { Close(); }
113
114 void Bind(ScopedMessagePipeHandle handle,
115 scoped_refptr<base::SingleThreadTaskRunner> runner) {
116 DCHECK(!router_);
117 SimpleBindingState::BindInternal(
118 std::move(handle), runner, Interface::Name_,
119 base::MakeUnique<typename Interface::RequestValidator_>(),
120 Interface::HasSyncMethods_, &stub_, Interface::Version_);
121 }
122
123 InterfaceRequest<Interface> Unbind() {
124 InterfaceRequest<Interface> request =
125 MakeRequest<Interface>(router_->PassMessagePipe());
126 DestroyRouter();
127 return std::move(request);
128 }
129
130 Interface* impl() { return ImplRefTraits::GetRawPointer(&stub_.sink()); }
131
132 private:
133 typename Interface::template Stub_<ImplRefTraits> stub_;
134
135 DISALLOW_COPY_AND_ASSIGN(BindingState);
136 };
137
138 // Base class used for templated binding primitives which may bind a pipe to
139 // multiple interfaces.
140 class MOJO_CPP_BINDINGS_EXPORT MultiplexedBindingState {
141 public:
142 MultiplexedBindingState();
143 ~MultiplexedBindingState();
144
145 void AddFilter(std::unique_ptr<MessageReceiver> filter);
146
147 bool HasAssociatedInterfaces() const; 43 bool HasAssociatedInterfaces() const;
148 44
149 void PauseIncomingMethodCallProcessing(); 45 void PauseIncomingMethodCallProcessing();
150 void ResumeIncomingMethodCallProcessing(); 46 void ResumeIncomingMethodCallProcessing();
151 47
152 bool WaitForIncomingMethodCall( 48 bool WaitForIncomingMethodCall(
153 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE); 49 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE);
154 50
155 void Close(); 51 void Close();
156 void CloseWithReason(uint32_t custom_reason, const std::string& description); 52 void CloseWithReason(uint32_t custom_reason, const std::string& description);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 std::unique_ptr<MessageReceiver> request_validator, 84 std::unique_ptr<MessageReceiver> request_validator,
189 bool passes_associated_kinds, 85 bool passes_associated_kinds,
190 bool has_sync_methods, 86 bool has_sync_methods,
191 MessageReceiverWithResponderStatus* stub, 87 MessageReceiverWithResponderStatus* stub,
192 uint32_t interface_version); 88 uint32_t interface_version);
193 89
194 scoped_refptr<internal::MultiplexRouter> router_; 90 scoped_refptr<internal::MultiplexRouter> router_;
195 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; 91 std::unique_ptr<InterfaceEndpointClient> endpoint_client_;
196 }; 92 };
197 93
198 // Uses a multiplexing router. If |Interface| has methods to pass associated
199 // interface pointers or requests, this specialization should be used.
200 template <typename Interface, typename ImplRefTraits> 94 template <typename Interface, typename ImplRefTraits>
201 class BindingState<Interface, true, ImplRefTraits> 95 class BindingState : public BindingStateBase {
202 : public MultiplexedBindingState {
203 public: 96 public:
204 using ImplPointerType = typename ImplRefTraits::PointerType; 97 using ImplPointerType = typename ImplRefTraits::PointerType;
205 98
206 explicit BindingState(ImplPointerType impl) { 99 explicit BindingState(ImplPointerType impl) {
207 stub_.set_sink(std::move(impl)); 100 stub_.set_sink(std::move(impl));
208 } 101 }
209 102
210 ~BindingState() { Close(); } 103 ~BindingState() { Close(); }
211 104
212 void Bind(ScopedMessagePipeHandle handle, 105 void Bind(ScopedMessagePipeHandle handle,
213 scoped_refptr<base::SingleThreadTaskRunner> runner) { 106 scoped_refptr<base::SingleThreadTaskRunner> runner) {
214 MultiplexedBindingState::BindInternal( 107 BindingStateBase::BindInternal(
215 std::move(handle), runner, Interface::Name_, 108 std::move(handle), runner, Interface::Name_,
216 base::MakeUnique<typename Interface::RequestValidator_>(), 109 base::MakeUnique<typename Interface::RequestValidator_>(),
217 Interface::PassesAssociatedKinds_, Interface::HasSyncMethods_, &stub_, 110 Interface::PassesAssociatedKinds_, Interface::HasSyncMethods_, &stub_,
218 Interface::Version_); 111 Interface::Version_);
219 if (Interface::PassesAssociatedKinds_) 112 if (Interface::PassesAssociatedKinds_)
220 stub_.serialization_context()->group_controller = router_; 113 stub_.serialization_context()->group_controller = router_;
221 } 114 }
222 115
223 InterfaceRequest<Interface> Unbind() { 116 InterfaceRequest<Interface> Unbind() {
224 endpoint_client_.reset(); 117 endpoint_client_.reset();
225 InterfaceRequest<Interface> request = 118 InterfaceRequest<Interface> request =
226 MakeRequest<Interface>(router_->PassMessagePipe()); 119 MakeRequest<Interface>(router_->PassMessagePipe());
227 router_ = nullptr; 120 router_ = nullptr;
228 return request; 121 return request;
229 } 122 }
230 123
231 Interface* impl() { return ImplRefTraits::GetRawPointer(&stub_.sink()); } 124 Interface* impl() { return ImplRefTraits::GetRawPointer(&stub_.sink()); }
232 125
233 private: 126 private:
234 typename Interface::template Stub_<ImplRefTraits> stub_; 127 typename Interface::template Stub_<ImplRefTraits> stub_;
235 128
236 DISALLOW_COPY_AND_ASSIGN(BindingState); 129 DISALLOW_COPY_AND_ASSIGN(BindingState);
237 }; 130 };
238 131
239 } // namesapce internal 132 } // namesapce internal
240 } // namespace mojo 133 } // namespace mojo
241 134
242 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_ 135 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/interface_ptr.h ('k') | mojo/public/cpp/bindings/lib/binding_state.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698