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

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: Fix Android compile 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"
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 uint32_t version_; 203 uint32_t version_;
203 204
204 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); 205 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState);
205 }; 206 };
206 207
207 // Uses a multiplexing router. If |Interface| has methods to pass associated 208 // Uses a multiplexing router. If |Interface| has methods to pass associated
208 // interface pointers or requests, this specialization should be used. 209 // interface pointers or requests, this specialization should be used.
209 template <typename Interface> 210 template <typename Interface>
210 class InterfacePtrState<Interface, true> { 211 class InterfacePtrState<Interface, true> {
211 public: 212 public:
212 InterfacePtrState() : version_(0u) {} 213 InterfacePtrState() : version_(0u), weak_ptr_factory_(this) {}
213 214
214 ~InterfacePtrState() { 215 ~InterfacePtrState() {
215 endpoint_client_.reset(); 216 endpoint_client_.reset();
216 proxy_.reset(); 217 proxy_.reset();
217 if (router_) 218 if (router_)
218 router_->CloseMessagePipe(); 219 router_->CloseMessagePipe();
219 } 220 }
220 221
221 Interface* instance() { 222 Interface* instance() {
222 ConfigureProxyIfNecessary(); 223 ConfigureProxyIfNecessary();
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 AssociatedGroup* associated_group() { 325 AssociatedGroup* associated_group() {
325 ConfigureProxyIfNecessary(); 326 ConfigureProxyIfNecessary();
326 return endpoint_client_->associated_group(); 327 return endpoint_client_->associated_group();
327 } 328 }
328 329
329 void EnableTestingMode() { 330 void EnableTestingMode() {
330 ConfigureProxyIfNecessary(); 331 ConfigureProxyIfNecessary();
331 router_->EnableTestingMode(); 332 router_->EnableTestingMode();
332 } 333 }
333 334
335 base::Callback<void(Message)> GetThreadSafePtrAcceptCallback() {
336 return base::Bind(&InterfacePtrState::ForwardMessage,
337 weak_ptr_factory_.GetWeakPtr());
338 }
339
340 base::Callback<void(Message, std::unique_ptr<MessageReceiver>)>
341 GetThreadSafePtrAcceptWithResponderCallback() {
342 return 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 10 matching lines...) Expand all
354 router_ = new MultiplexRouter(std::move(handle_), config, true, runner_); 366 router_ = new MultiplexRouter(std::move(handle_), config, true, runner_);
355 router_->SetMasterInterfaceName(Interface::Name_); 367 router_->SetMasterInterfaceName(Interface::Name_);
356 endpoint_client_.reset(new InterfaceEndpointClient( 368 endpoint_client_.reset(new InterfaceEndpointClient(
357 router_->CreateLocalEndpointHandle(kMasterInterfaceId), nullptr, 369 router_->CreateLocalEndpointHandle(kMasterInterfaceId), nullptr,
358 base::WrapUnique(new typename Interface::ResponseValidator_()), false, 370 base::WrapUnique(new typename Interface::ResponseValidator_()), false,
359 std::move(runner_), 371 std::move(runner_),
360 // The version is only queried from the client so the value passed here 372 // The version is only queried from the client so the value passed here
361 // will not be used. 373 // will not be used.
362 0u)); 374 0u));
363 proxy_.reset(new Proxy(endpoint_client_.get())); 375 proxy_.reset(new Proxy(endpoint_client_.get()));
364 if (Interface::PassesAssociatedKinds_) { 376 if (Interface::PassesAssociatedKinds_)
365 proxy_->serialization_context()->group_controller = 377 proxy_->set_group_controller(endpoint_client_->group_controller());
366 endpoint_client_->group_controller();
367 }
368 } 378 }
369 379
370 void OnQueryVersion(const base::Callback<void(uint32_t)>& callback, 380 void OnQueryVersion(const base::Callback<void(uint32_t)>& callback,
371 uint32_t version) { 381 uint32_t version) {
372 version_ = version; 382 version_ = version;
373 callback.Run(version); 383 callback.Run(version);
374 } 384 }
375 385
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
376 scoped_refptr<MultiplexRouter> router_; 397 scoped_refptr<MultiplexRouter> router_;
377 398
378 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; 399 std::unique_ptr<InterfaceEndpointClient> endpoint_client_;
379 std::unique_ptr<Proxy> proxy_; 400 std::unique_ptr<Proxy> proxy_;
380 401
381 // |router_| (as well as other members above) is not initialized until 402 // |router_| (as well as other members above) is not initialized until
382 // read/write with the message pipe handle is needed. |handle_| is valid 403 // read/write with the message pipe handle is needed. |handle_| is valid
383 // between the Bind() call and the initialization of |router_|. 404 // between the Bind() call and the initialization of |router_|.
384 ScopedMessagePipeHandle handle_; 405 ScopedMessagePipeHandle handle_;
385 scoped_refptr<base::SingleThreadTaskRunner> runner_; 406 scoped_refptr<base::SingleThreadTaskRunner> runner_;
386 407
387 uint32_t version_; 408 uint32_t version_;
388 409
410 base::WeakPtrFactory<InterfacePtrState> weak_ptr_factory_;
yzshen1 2016/11/15 18:53:46 It is not needed in the majority case. Does it mak
Jay Civelli 2016/11/15 19:36:38 On Linux the size of a WeakPtrFactory is 16 bytes
yzshen1 2016/11/15 20:26:15 Just wanted to make sure that the size/init cost h
411
389 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); 412 DISALLOW_COPY_AND_ASSIGN(InterfacePtrState);
390 }; 413 };
391 414
392 } // namespace internal 415 } // namespace internal
393 } // namespace mojo 416 } // namespace mojo
394 417
395 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_ 418 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_STATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698