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

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

Issue 2506383002: Mojo: adding a thread safe associated interface ptr. (Closed)
Patch Set: Addressed comment + sync 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"
22 #include "base/single_thread_task_runner.h" 21 #include "base/single_thread_task_runner.h"
23 #include "mojo/public/cpp/bindings/associated_group.h" 22 #include "mojo/public/cpp/bindings/associated_group.h"
24 #include "mojo/public/cpp/bindings/connection_error_callback.h" 23 #include "mojo/public/cpp/bindings/connection_error_callback.h"
25 #include "mojo/public/cpp/bindings/filter_chain.h" 24 #include "mojo/public/cpp/bindings/filter_chain.h"
26 #include "mojo/public/cpp/bindings/interface_endpoint_client.h" 25 #include "mojo/public/cpp/bindings/interface_endpoint_client.h"
27 #include "mojo/public/cpp/bindings/interface_id.h" 26 #include "mojo/public/cpp/bindings/interface_id.h"
28 #include "mojo/public/cpp/bindings/interface_ptr_info.h" 27 #include "mojo/public/cpp/bindings/interface_ptr_info.h"
29 #include "mojo/public/cpp/bindings/lib/control_message_handler.h" 28 #include "mojo/public/cpp/bindings/lib/control_message_handler.h"
30 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" 29 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h"
31 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" 30 #include "mojo/public/cpp/bindings/lib/multiplex_router.h"
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 uint32_t version_; 202 uint32_t version_;
204 203
205 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); 204 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState);
206 }; 205 };
207 206
208 // Uses a multiplexing router. If |Interface| has methods to pass associated 207 // Uses a multiplexing router. If |Interface| has methods to pass associated
209 // interface pointers or requests, this specialization should be used. 208 // interface pointers or requests, this specialization should be used.
210 template <typename Interface> 209 template <typename Interface>
211 class InterfacePtrState<Interface, true> { 210 class InterfacePtrState<Interface, true> {
212 public: 211 public:
213 InterfacePtrState() : version_(0u), weak_ptr_factory_(this) {} 212 InterfacePtrState() : version_(0u) {}
214 213
215 ~InterfacePtrState() { 214 ~InterfacePtrState() {
216 endpoint_client_.reset(); 215 endpoint_client_.reset();
217 proxy_.reset(); 216 proxy_.reset();
218 if (router_) 217 if (router_)
219 router_->CloseMessagePipe(); 218 router_->CloseMessagePipe();
220 } 219 }
221 220
222 Interface* instance() { 221 Interface* instance() {
223 ConfigureProxyIfNecessary(); 222 ConfigureProxyIfNecessary();
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 AssociatedGroup* associated_group() { 324 AssociatedGroup* associated_group() {
326 ConfigureProxyIfNecessary(); 325 ConfigureProxyIfNecessary();
327 return endpoint_client_->associated_group(); 326 return endpoint_client_->associated_group();
328 } 327 }
329 328
330 void EnableTestingMode() { 329 void EnableTestingMode() {
331 ConfigureProxyIfNecessary(); 330 ConfigureProxyIfNecessary();
332 router_->EnableTestingMode(); 331 router_->EnableTestingMode();
333 } 332 }
334 333
335 base::Callback<void(Message)> GetThreadSafePtrAcceptCallback() { 334 void ForwardMessage(Message message) {
336 return base::Bind(&InterfacePtrState::ForwardMessage, 335 ConfigureProxyIfNecessary();
337 weak_ptr_factory_.GetWeakPtr()); 336 endpoint_client_->Accept(&message);
338 } 337 }
339 338
340 base::Callback<void(Message, std::unique_ptr<MessageReceiver>)> 339 void ForwardMessageWithResponder(Message message,
341 GetThreadSafePtrAcceptWithResponderCallback() { 340 std::unique_ptr<MessageReceiver> responder) {
342 return base::Bind(&InterfacePtrState::ForwardMessageWithResponder, 341 ConfigureProxyIfNecessary();
343 weak_ptr_factory_.GetWeakPtr()); 342 endpoint_client_->AcceptWithResponder(&message, responder.release());
344 } 343 }
345 344
346 private: 345 private:
347 using Proxy = typename Interface::Proxy_; 346 using Proxy = typename Interface::Proxy_;
348 347
349 void ConfigureProxyIfNecessary() { 348 void ConfigureProxyIfNecessary() {
350 // The proxy has been configured. 349 // The proxy has been configured.
351 if (proxy_) { 350 if (proxy_) {
352 DCHECK(router_); 351 DCHECK(router_);
353 DCHECK(endpoint_client_); 352 DCHECK(endpoint_client_);
(...skipping 22 matching lines...) Expand all
376 if (Interface::PassesAssociatedKinds_) 375 if (Interface::PassesAssociatedKinds_)
377 proxy_->set_group_controller(endpoint_client_->group_controller()); 376 proxy_->set_group_controller(endpoint_client_->group_controller());
378 } 377 }
379 378
380 void OnQueryVersion(const base::Callback<void(uint32_t)>& callback, 379 void OnQueryVersion(const base::Callback<void(uint32_t)>& callback,
381 uint32_t version) { 380 uint32_t version) {
382 version_ = version; 381 version_ = version;
383 callback.Run(version); 382 callback.Run(version);
384 } 383 }
385 384
386 void ForwardMessage(Message message) {
387 ConfigureProxyIfNecessary();
388 endpoint_client_->Accept(&message);
389 }
390
391 void ForwardMessageWithResponder(Message message,
392 std::unique_ptr<MessageReceiver> responder) {
393 ConfigureProxyIfNecessary();
394 endpoint_client_->AcceptWithResponder(&message, responder.release());
395 }
396
397 scoped_refptr<MultiplexRouter> router_; 385 scoped_refptr<MultiplexRouter> router_;
398 386
399 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; 387 std::unique_ptr<InterfaceEndpointClient> endpoint_client_;
400 std::unique_ptr<Proxy> proxy_; 388 std::unique_ptr<Proxy> proxy_;
401 389
402 // |router_| (as well as other members above) is not initialized until 390 // |router_| (as well as other members above) is not initialized until
403 // read/write with the message pipe handle is needed. |handle_| is valid 391 // read/write with the message pipe handle is needed. |handle_| is valid
404 // between the Bind() call and the initialization of |router_|. 392 // between the Bind() call and the initialization of |router_|.
405 ScopedMessagePipeHandle handle_; 393 ScopedMessagePipeHandle handle_;
406 scoped_refptr<base::SingleThreadTaskRunner> runner_; 394 scoped_refptr<base::SingleThreadTaskRunner> runner_;
407 395
408 uint32_t version_; 396 uint32_t version_;
409 397
410 base::WeakPtrFactory<InterfacePtrState> weak_ptr_factory_;
411
412 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); 398 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState);
413 }; 399 };
414 400
415 } // namespace internal 401 } // namespace internal
416 } // namespace mojo 402 } // namespace mojo
417 403
418 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_ 404 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698