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

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: . 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
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
107 } 108 explicit BindingState(ImplPointerType impl) { stub_.set_sink(impl); }
108 109
109 ~BindingState() { Close(); } 110 ~BindingState() { Close(); }
110 111
111 void Bind(ScopedMessagePipeHandle handle, 112 void Bind(ScopedMessagePipeHandle handle,
112 scoped_refptr<base::SingleThreadTaskRunner> runner) { 113 scoped_refptr<base::SingleThreadTaskRunner> runner) {
113 DCHECK(!router_); 114 DCHECK(!router_);
114 SimpleBindingState::BindInternal( 115 SimpleBindingState::BindInternal(
115 std::move(handle), runner, Interface::Name_, 116 std::move(handle), runner, Interface::Name_,
116 base::MakeUnique<typename Interface::RequestValidator_>(), 117 base::MakeUnique<typename Interface::RequestValidator_>(),
117 Interface::HasSyncMethods_, &stub_, Interface::Version_); 118 Interface::HasSyncMethods_, &stub_, Interface::Version_);
118 } 119 }
119 120
120 InterfaceRequest<Interface> Unbind() { 121 InterfaceRequest<Interface> Unbind() {
121 InterfaceRequest<Interface> request = 122 InterfaceRequest<Interface> request =
122 MakeRequest<Interface>(router_->PassMessagePipe()); 123 MakeRequest<Interface>(router_->PassMessagePipe());
123 DestroyRouter(); 124 DestroyRouter();
124 return std::move(request); 125 return std::move(request);
125 } 126 }
126 127
127 Interface* impl() { return impl_; } 128 Interface* impl() { return ImplRefTraits::GetRawPointer(stub_.sink()); }
128 129
129 private: 130 private:
130 typename Interface::Stub_ stub_; 131 typename Interface::template Stub_<ImplRefTraits> stub_;
131 Interface* impl_;
132 132
133 DISALLOW_COPY_AND_ASSIGN(BindingState); 133 DISALLOW_COPY_AND_ASSIGN(BindingState);
134 }; 134 };
135 135
136 // Base class used for templated binding primitives which may bind a pipe to 136 // Base class used for templated binding primitives which may bind a pipe to
137 // multiple interfaces. 137 // multiple interfaces.
138 class MOJO_CPP_BINDINGS_EXPORT MultiplexedBindingState { 138 class MOJO_CPP_BINDINGS_EXPORT MultiplexedBindingState {
139 public: 139 public:
140 MultiplexedBindingState(); 140 MultiplexedBindingState();
141 ~MultiplexedBindingState(); 141 ~MultiplexedBindingState();
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 bool has_sync_methods, 188 bool has_sync_methods,
189 MessageReceiverWithResponderStatus* stub, 189 MessageReceiverWithResponderStatus* stub,
190 uint32_t interface_version); 190 uint32_t interface_version);
191 191
192 scoped_refptr<internal::MultiplexRouter> router_; 192 scoped_refptr<internal::MultiplexRouter> router_;
193 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; 193 std::unique_ptr<InterfaceEndpointClient> endpoint_client_;
194 }; 194 };
195 195
196 // Uses a multiplexing router. If |Interface| has methods to pass associated 196 // Uses a multiplexing router. If |Interface| has methods to pass associated
197 // interface pointers or requests, this specialization should be used. 197 // interface pointers or requests, this specialization should be used.
198 template <typename Interface> 198 template <typename Interface, typename ImplRefTraits>
199 class BindingState<Interface, true> : public MultiplexedBindingState { 199 class BindingState<Interface, true, ImplRefTraits>
200 : public MultiplexedBindingState {
200 public: 201 public:
201 explicit BindingState(Interface* impl) : impl_(impl) { 202 using ImplPointerType = typename ImplRefTraits::PointerType;
202 stub_.set_sink(impl_); 203
203 } 204 explicit BindingState(ImplPointerType impl) { stub_.set_sink(impl); }
204 205
205 ~BindingState() { Close(); } 206 ~BindingState() { Close(); }
206 207
207 void Bind(ScopedMessagePipeHandle handle, 208 void Bind(ScopedMessagePipeHandle handle,
208 scoped_refptr<base::SingleThreadTaskRunner> runner) { 209 scoped_refptr<base::SingleThreadTaskRunner> runner) {
209 MultiplexedBindingState::BindInternal( 210 MultiplexedBindingState::BindInternal(
210 std::move(handle), runner, Interface::Name_, 211 std::move(handle), runner, Interface::Name_,
211 base::MakeUnique<typename Interface::RequestValidator_>(), 212 base::MakeUnique<typename Interface::RequestValidator_>(),
212 Interface::PassesAssociatedKinds_, Interface::HasSyncMethods_, &stub_, 213 Interface::PassesAssociatedKinds_, Interface::HasSyncMethods_, &stub_,
213 Interface::Version_); 214 Interface::Version_);
214 stub_.serialization_context()->group_controller = router_; 215 stub_.serialization_context()->group_controller = router_;
215 } 216 }
216 217
217 InterfaceRequest<Interface> Unbind() { 218 InterfaceRequest<Interface> Unbind() {
218 endpoint_client_.reset(); 219 endpoint_client_.reset();
219 InterfaceRequest<Interface> request = 220 InterfaceRequest<Interface> request =
220 MakeRequest<Interface>(router_->PassMessagePipe()); 221 MakeRequest<Interface>(router_->PassMessagePipe());
221 router_ = nullptr; 222 router_ = nullptr;
222 return request; 223 return request;
223 } 224 }
224 225
225 Interface* impl() { return impl_; } 226 Interface* impl() { return ImplRefTraits::GetRawPointer(stub_.sink()); }
226 227
227 private: 228 private:
228 typename Interface::Stub_ stub_; 229 typename Interface::template Stub_<ImplRefTraits> stub_;
229 Interface* impl_;
230 230
231 DISALLOW_COPY_AND_ASSIGN(BindingState); 231 DISALLOW_COPY_AND_ASSIGN(BindingState);
232 }; 232 };
233 233
234 } // namesapce internal 234 } // namesapce internal
235 } // namespace mojo 235 } // namespace mojo
236 236
237 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_ 237 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDING_STATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698