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

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

Issue 2498743002: Mojo: introducing a thread safe interface pointer. (Closed)
Patch Set: Clean-up Created 4 years, 1 month 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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_INTERFACE_PTR_STATE_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <algorithm> // For |std::swap()|. 10 #include <algorithm> // For |std::swap()|.
11 #include <memory> 11 #include <memory>
12 #include <string> 12 #include <string>
13 #include <utility> 13 #include <utility>
14 14
15 #include "base/bind.h" 15 #include "base/bind.h"
16 #include "base/callback_forward.h" 16 #include "base/callback_forward.h"
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/macros.h" 18 #include "base/macros.h"
19 #include "base/memory/ptr_util.h" 19 #include "base/memory/ptr_util.h"
20 #include "base/memory/ref_counted.h" 20 #include "base/memory/ref_counted.h"
21 #include "base/memory/weak_ptr.h"
21 #include "base/single_thread_task_runner.h" 22 #include "base/single_thread_task_runner.h"
22 #include "mojo/public/cpp/bindings/associated_group.h" 23 #include "mojo/public/cpp/bindings/associated_group.h"
23 #include "mojo/public/cpp/bindings/connection_error_callback.h" 24 #include "mojo/public/cpp/bindings/connection_error_callback.h"
24 #include "mojo/public/cpp/bindings/filter_chain.h" 25 #include "mojo/public/cpp/bindings/filter_chain.h"
25 #include "mojo/public/cpp/bindings/interface_endpoint_client.h" 26 #include "mojo/public/cpp/bindings/interface_endpoint_client.h"
26 #include "mojo/public/cpp/bindings/interface_id.h" 27 #include "mojo/public/cpp/bindings/interface_id.h"
27 #include "mojo/public/cpp/bindings/interface_ptr_info.h" 28 #include "mojo/public/cpp/bindings/interface_ptr_info.h"
28 #include "mojo/public/cpp/bindings/lib/control_message_handler.h" 29 #include "mojo/public/cpp/bindings/lib/control_message_handler.h"
29 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" 30 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h"
30 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" 31 #include "mojo/public/cpp/bindings/lib/multiplex_router.h"
31 #include "mojo/public/cpp/bindings/lib/router.h" 32 #include "mojo/public/cpp/bindings/lib/router.h"
32 #include "mojo/public/cpp/bindings/message_header_validator.h" 33 #include "mojo/public/cpp/bindings/message_header_validator.h"
33 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" 34 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
35 #include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h"
34 36
35 namespace mojo { 37 namespace mojo {
36 namespace internal { 38 namespace internal {
37 39
38 template <typename Interface, bool use_multiplex_router> 40 template <typename Interface, bool use_multiplex_router>
39 class InterfacePtrState; 41 class InterfacePtrState;
40 42
41 // Uses a single-threaded, dedicated router. If |Interface| doesn't have any 43 // Uses a single-threaded, dedicated router. If |Interface| doesn't have any
42 // methods to pass associated interface pointers or requests, there won't be 44 // methods to pass associated interface pointers or requests, there won't be
43 // multiple interfaces running on the underlying message pipe. In that case, we 45 // multiple interfaces running on the underlying message pipe. In that case, we
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 uint32_t version_; 204 uint32_t version_;
203 205
204 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); 206 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState);
205 }; 207 };
206 208
207 // Uses a multiplexing router. If |Interface| has methods to pass associated 209 // Uses a multiplexing router. If |Interface| has methods to pass associated
208 // interface pointers or requests, this specialization should be used. 210 // interface pointers or requests, this specialization should be used.
209 template <typename Interface> 211 template <typename Interface>
210 class InterfacePtrState<Interface, true> { 212 class InterfacePtrState<Interface, true> {
211 public: 213 public:
212 InterfacePtrState() : version_(0u) {} 214 InterfacePtrState() : version_(0u), weak_ptr_factory_(this) {}
213 215
214 ~InterfacePtrState() { 216 ~InterfacePtrState() {
215 endpoint_client_.reset(); 217 endpoint_client_.reset();
216 proxy_.reset(); 218 proxy_.reset();
217 if (router_) 219 if (router_)
218 router_->CloseMessagePipe(); 220 router_->CloseMessagePipe();
219 } 221 }
220 222
221 Interface* instance() { 223 Interface* instance() {
222 ConfigureProxyIfNecessary(); 224 ConfigureProxyIfNecessary();
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 AssociatedGroup* associated_group() { 326 AssociatedGroup* associated_group() {
325 ConfigureProxyIfNecessary(); 327 ConfigureProxyIfNecessary();
326 return endpoint_client_->associated_group(); 328 return endpoint_client_->associated_group();
327 } 329 }
328 330
329 void EnableTestingMode() { 331 void EnableTestingMode() {
330 ConfigureProxyIfNecessary(); 332 ConfigureProxyIfNecessary();
331 router_->EnableTestingMode(); 333 router_->EnableTestingMode();
332 } 334 }
333 335
336 scoped_refptr<ThreadSafeInterfacePtr<Interface>> GetThreadSafePtr() {
yzshen1 2016/11/11 22:32:10 One thing that seems a little weird about this is
Jay Civelli 2016/11/15 05:02:30 Per our discussion, the ThreadSafeInterfacePtr cl
337 ConfigureProxyIfNecessary();
338 return new ThreadSafeInterfacePtr<Interface>(
339 base::ThreadTaskRunnerHandle::Get(),
340 base::Bind(&InterfacePtrState::ForwardMessage,
341 weak_ptr_factory_.GetWeakPtr()),
342 base::Bind(&InterfacePtrState::ForwardMessageWithResponder,
343 weak_ptr_factory_.GetWeakPtr()));
344 }
345
334 private: 346 private:
335 using Proxy = typename Interface::Proxy_; 347 using Proxy = typename Interface::Proxy_;
336 348
337 void ConfigureProxyIfNecessary() { 349 void ConfigureProxyIfNecessary() {
338 // The proxy has been configured. 350 // The proxy has been configured.
339 if (proxy_) { 351 if (proxy_) {
340 DCHECK(router_); 352 DCHECK(router_);
341 DCHECK(endpoint_client_); 353 DCHECK(endpoint_client_);
342 return; 354 return;
343 } 355 }
(...skipping 20 matching lines...) Expand all
364 proxy_->serialization_context()->group_controller = 376 proxy_->serialization_context()->group_controller =
365 endpoint_client_->group_controller(); 377 endpoint_client_->group_controller();
366 } 378 }
367 379
368 void OnQueryVersion(const base::Callback<void(uint32_t)>& callback, 380 void OnQueryVersion(const base::Callback<void(uint32_t)>& callback,
369 uint32_t version) { 381 uint32_t version) {
370 version_ = version; 382 version_ = version;
371 callback.Run(version); 383 callback.Run(version);
372 } 384 }
373 385
386 void ForwardMessage(Message message) {
387 endpoint_client_->Accept(&message);
388 }
389
390 void ForwardMessageWithResponder(Message message,
391 std::unique_ptr<MessageReceiver> responder) {
392 endpoint_client_->AcceptWithResponder(&message, responder.release());
393 }
394
374 scoped_refptr<MultiplexRouter> router_; 395 scoped_refptr<MultiplexRouter> router_;
375 396
376 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; 397 std::unique_ptr<InterfaceEndpointClient> endpoint_client_;
377 std::unique_ptr<Proxy> proxy_; 398 std::unique_ptr<Proxy> proxy_;
378 399
379 // |router_| (as well as other members above) is not initialized until 400 // |router_| (as well as other members above) is not initialized until
380 // read/write with the message pipe handle is needed. |handle_| is valid 401 // read/write with the message pipe handle is needed. |handle_| is valid
381 // between the Bind() call and the initialization of |router_|. 402 // between the Bind() call and the initialization of |router_|.
382 ScopedMessagePipeHandle handle_; 403 ScopedMessagePipeHandle handle_;
383 scoped_refptr<base::SingleThreadTaskRunner> runner_; 404 scoped_refptr<base::SingleThreadTaskRunner> runner_;
384 405
385 uint32_t version_; 406 uint32_t version_;
386 407
408 base::WeakPtrFactory<InterfacePtrState> weak_ptr_factory_;
409
387 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); 410 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState);
388 }; 411 };
389 412
390 } // namespace internal 413 } // namespace internal
391 } // namespace mojo 414 } // namespace mojo
392 415
393 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_ 416 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698