• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "mojo/public/cpp/bindings/lib/interface_ptr_state.h"
6 
7 #include "mojo/public/cpp/bindings/lib/task_runner_helper.h"
8 
9 namespace mojo {
10 namespace internal {
11 
12 InterfacePtrStateBase::InterfacePtrStateBase() = default;
13 
~InterfacePtrStateBase()14 InterfacePtrStateBase::~InterfacePtrStateBase() {
15   endpoint_client_.reset();
16   if (router_)
17     router_->CloseMessagePipe();
18 }
19 
QueryVersion(const base::Callback<void (uint32_t)> & callback)20 void InterfacePtrStateBase::QueryVersion(
21     const base::Callback<void(uint32_t)>& callback) {
22   // It is safe to capture |this| because the callback won't be run after this
23   // object goes away.
24   endpoint_client_->QueryVersion(
25       base::Bind(&InterfacePtrStateBase::OnQueryVersion, base::Unretained(this),
26                  callback));
27 }
28 
RequireVersion(uint32_t version)29 void InterfacePtrStateBase::RequireVersion(uint32_t version) {
30   if (version <= version_)
31     return;
32 
33   version_ = version;
34   endpoint_client_->RequireVersion(version);
35 }
36 
Swap(InterfacePtrStateBase * other)37 void InterfacePtrStateBase::Swap(InterfacePtrStateBase* other) {
38   using std::swap;
39   swap(other->router_, router_);
40   swap(other->endpoint_client_, endpoint_client_);
41   handle_.swap(other->handle_);
42   runner_.swap(other->runner_);
43   swap(other->version_, version_);
44 }
45 
Bind(ScopedMessagePipeHandle handle,uint32_t version,scoped_refptr<base::SequencedTaskRunner> task_runner)46 void InterfacePtrStateBase::Bind(
47     ScopedMessagePipeHandle handle,
48     uint32_t version,
49     scoped_refptr<base::SequencedTaskRunner> task_runner) {
50   DCHECK(!router_);
51   DCHECK(!endpoint_client_);
52   DCHECK(!handle_.is_valid());
53   DCHECK_EQ(0u, version_);
54   DCHECK(handle.is_valid());
55 
56   handle_ = std::move(handle);
57   version_ = version;
58   runner_ =
59       GetTaskRunnerToUseFromUserProvidedTaskRunner(std::move(task_runner));
60 }
61 
OnQueryVersion(const base::Callback<void (uint32_t)> & callback,uint32_t version)62 void InterfacePtrStateBase::OnQueryVersion(
63     const base::Callback<void(uint32_t)>& callback,
64     uint32_t version) {
65   version_ = version;
66   callback.Run(version);
67 }
68 
InitializeEndpointClient(bool passes_associated_kinds,bool has_sync_methods,std::unique_ptr<MessageReceiver> payload_validator)69 bool InterfacePtrStateBase::InitializeEndpointClient(
70     bool passes_associated_kinds,
71     bool has_sync_methods,
72     std::unique_ptr<MessageReceiver> payload_validator) {
73   // The object hasn't been bound.
74   if (!handle_.is_valid())
75     return false;
76 
77   MultiplexRouter::Config config =
78       passes_associated_kinds
79           ? MultiplexRouter::MULTI_INTERFACE
80           : (has_sync_methods
81                  ? MultiplexRouter::SINGLE_INTERFACE_WITH_SYNC_METHODS
82                  : MultiplexRouter::SINGLE_INTERFACE);
83   router_ = new MultiplexRouter(std::move(handle_), config, true, runner_);
84   endpoint_client_.reset(new InterfaceEndpointClient(
85       router_->CreateLocalEndpointHandle(kMasterInterfaceId), nullptr,
86       std::move(payload_validator), false, std::move(runner_),
87       // The version is only queried from the client so the value passed here
88       // will not be used.
89       0u));
90   return true;
91 }
92 
93 }  // namespace internal
94 }  // namespace mojo
95