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

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

Issue 2403533003: Mojo C++ Bindings: Support custom impl ref types (Closed)
Patch Set: nit, move Created 4 years, 2 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
« no previous file with comments | « mojo/public/cpp/bindings/binding.h ('k') | mojo/public/cpp/bindings/raw_ptr_impl_ref_traits.h » ('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>
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 std::unique_ptr<MessageReceiver> request_validator, 85 std::unique_ptr<MessageReceiver> request_validator,
86 bool has_sync_methods, 86 bool has_sync_methods,
87 MessageReceiverWithResponderStatus* stub, 87 MessageReceiverWithResponderStatus* stub,
88 uint32_t interface_version); 88 uint32_t interface_version);
89 89
90 void DestroyRouter(); 90 void DestroyRouter();
91 91
92 internal::Router* router_ = nullptr; 92 internal::Router* router_ = nullptr;
93 }; 93 };
94 94
95 template <typename Interface, bool use_multiplex_router> 95 template <typename Interface, bool use_multiplex_router, typename ImplRefTraits>
96 class BindingState; 96 class BindingState;
97 97
98 // Uses a single-threaded, dedicated router. If |Interface| doesn't have any 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 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 100 // multiple interfaces running on the underlying message pipe. In that case, we
101 // can use this specialization to reduce cost. 101 // can use this specialization to reduce cost.
102 template <typename Interface> 102 template <typename Interface, typename ImplRefTraits>
103 class BindingState<Interface, false> : public SimpleBindingState { 103 class BindingState<Interface, false, ImplRefTraits>
104 : public SimpleBindingState {
104 public: 105 public:
105 explicit BindingState(Interface* impl) : impl_(impl) { 106 using ImplPointerType = typename ImplRefTraits::PointerType;
106 stub_.set_sink(impl_); 107
108 explicit BindingState(ImplPointerType impl) {
109 stub_.set_sink(std::move(impl));
107 } 110 }
108 111
109 ~BindingState() { Close(); } 112 ~BindingState() { Close(); }
110 113
111 void Bind(ScopedMessagePipeHandle handle, 114 void Bind(ScopedMessagePipeHandle handle,
112 scoped_refptr<base::SingleThreadTaskRunner> runner) { 115 scoped_refptr<base::SingleThreadTaskRunner> runner) {
113 DCHECK(!router_); 116 DCHECK(!router_);
114 SimpleBindingState::BindInternal( 117 SimpleBindingState::BindInternal(
115 std::move(handle), runner, Interface::Name_, 118 std::move(handle), runner, Interface::Name_,
116 base::MakeUnique<typename Interface::RequestValidator_>(), 119 base::MakeUnique<typename Interface::RequestValidator_>(),
117 Interface::HasSyncMethods_, &stub_, Interface::Version_); 120 Interface::HasSyncMethods_, &stub_, Interface::Version_);
118 } 121 }
119 122
120 InterfaceRequest<Interface> Unbind() { 123 InterfaceRequest<Interface> Unbind() {
121 InterfaceRequest<Interface> request = 124 InterfaceRequest<Interface> request =
122 MakeRequest<Interface>(router_->PassMessagePipe()); 125 MakeRequest<Interface>(router_->PassMessagePipe());
123 DestroyRouter(); 126 DestroyRouter();
124 return std::move(request); 127 return std::move(request);
125 } 128 }
126 129
127 Interface* impl() { return impl_; } 130 Interface* impl() { return ImplRefTraits::GetRawPointer(&stub_.sink()); }
128 131
129 private: 132 private:
130 typename Interface::Stub_ stub_; 133 typename Interface::template Stub_<ImplRefTraits> stub_;
131 Interface* impl_;
132 134
133 DISALLOW_COPY_AND_ASSIGN(BindingState); 135 DISALLOW_COPY_AND_ASSIGN(BindingState);
134 }; 136 };
135 137
136 // Base class used for templated binding primitives which may bind a pipe to 138 // Base class used for templated binding primitives which may bind a pipe to
137 // multiple interfaces. 139 // multiple interfaces.
138 class MOJO_CPP_BINDINGS_EXPORT MultiplexedBindingState { 140 class MOJO_CPP_BINDINGS_EXPORT MultiplexedBindingState {
139 public: 141 public:
140 MultiplexedBindingState(); 142 MultiplexedBindingState();
141 ~MultiplexedBindingState(); 143 ~MultiplexedBindingState();
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 bool has_sync_methods, 190 bool has_sync_methods,
189 MessageReceiverWithResponderStatus* stub, 191 MessageReceiverWithResponderStatus* stub,
190 uint32_t interface_version); 192 uint32_t interface_version);
191 193
192 scoped_refptr<internal::MultiplexRouter> router_; 194 scoped_refptr<internal::MultiplexRouter> router_;
193 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; 195 std::unique_ptr<InterfaceEndpointClient> endpoint_client_;
194 }; 196 };
195 197
196 // Uses a multiplexing router. If |Interface| has methods to pass associated 198 // Uses a multiplexing router. If |Interface| has methods to pass associated
197 // interface pointers or requests, this specialization should be used. 199 // interface pointers or requests, this specialization should be used.
198 template <typename Interface> 200 template <typename Interface, typename ImplRefTraits>
199 class BindingState<Interface, true> : public MultiplexedBindingState { 201 class BindingState<Interface, true, ImplRefTraits>
202 : public MultiplexedBindingState {
200 public: 203 public:
201 explicit BindingState(Interface* impl) : impl_(impl) { 204 using ImplPointerType = typename ImplRefTraits::PointerType;
202 stub_.set_sink(impl_); 205
206 explicit BindingState(ImplPointerType impl) {
207 stub_.set_sink(std::move(impl));
203 } 208 }
204 209
205 ~BindingState() { Close(); } 210 ~BindingState() { Close(); }
206 211
207 void Bind(ScopedMessagePipeHandle handle, 212 void Bind(ScopedMessagePipeHandle handle,
208 scoped_refptr<base::SingleThreadTaskRunner> runner) { 213 scoped_refptr<base::SingleThreadTaskRunner> runner) {
209 MultiplexedBindingState::BindInternal( 214 MultiplexedBindingState::BindInternal(
210 std::move(handle), runner, Interface::Name_, 215 std::move(handle), runner, Interface::Name_,
211 base::MakeUnique<typename Interface::RequestValidator_>(), 216 base::MakeUnique<typename Interface::RequestValidator_>(),
212 Interface::PassesAssociatedKinds_, Interface::HasSyncMethods_, &stub_, 217 Interface::PassesAssociatedKinds_, Interface::HasSyncMethods_, &stub_,
213 Interface::Version_); 218 Interface::Version_);
214 stub_.serialization_context()->group_controller = router_; 219 stub_.serialization_context()->group_controller = router_;
215 } 220 }
216 221
217 InterfaceRequest<Interface> Unbind() { 222 InterfaceRequest<Interface> Unbind() {
218 endpoint_client_.reset(); 223 endpoint_client_.reset();
219 InterfaceRequest<Interface> request = 224 InterfaceRequest<Interface> request =
220 MakeRequest<Interface>(router_->PassMessagePipe()); 225 MakeRequest<Interface>(router_->PassMessagePipe());
221 router_ = nullptr; 226 router_ = nullptr;
222 return request; 227 return request;
223 } 228 }
224 229
225 Interface* impl() { return impl_; } 230 Interface* impl() { return ImplRefTraits::GetRawPointer(&stub_.sink()); }
226 231
227 private: 232 private:
228 typename Interface::Stub_ stub_; 233 typename Interface::template Stub_<ImplRefTraits> stub_;
229 Interface* impl_;
230 234
231 DISALLOW_COPY_AND_ASSIGN(BindingState); 235 DISALLOW_COPY_AND_ASSIGN(BindingState);
232 }; 236 };
233 237
234 } // namesapce internal 238 } // namesapce internal
235 } // namespace mojo 239 } // namespace mojo
236 240
237 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_ 241 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/binding.h ('k') | mojo/public/cpp/bindings/raw_ptr_impl_ref_traits.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698