OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "services/service_manager/public/cpp/lib/connector_impl.h" | 5 #include "services/service_manager/public/cpp/lib/connector_impl.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "services/service_manager/public/cpp/identity.h" | 8 #include "services/service_manager/public/cpp/identity.h" |
9 #include "services/service_manager/public/cpp/lib/connection_impl.h" | 9 #include "services/service_manager/public/cpp/lib/connection_impl.h" |
10 | 10 |
11 namespace service_manager { | 11 namespace service_manager { |
12 | 12 |
13 namespace { | 13 namespace { |
14 void EmptyBindCallback(mojom::ConnectResult, const std::string&) {} | 14 void EmptyBindCallback(mojom::ConnectResult, const std::string&) {} |
15 } | 15 } |
16 | 16 |
17 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtrInfo unbound_state) | 17 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtrInfo unbound_state) |
18 : unbound_state_(std::move(unbound_state)) { | 18 : unbound_state_(std::move(unbound_state)), weak_factory_(this) { |
19 thread_checker_.DetachFromThread(); | 19 thread_checker_.DetachFromThread(); |
20 } | 20 } |
21 | 21 |
22 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtr connector) | 22 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtr connector) |
23 : connector_(std::move(connector)) { | 23 : connector_(std::move(connector)), weak_factory_(this) { |
24 connector_.set_connection_error_handler( | 24 connector_.set_connection_error_handler( |
25 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); | 25 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); |
26 } | 26 } |
27 | 27 |
28 ConnectorImpl::~ConnectorImpl() {} | 28 ConnectorImpl::~ConnectorImpl() {} |
29 | 29 |
30 void ConnectorImpl::OnConnectionError() { | 30 void ConnectorImpl::OnConnectionError() { |
31 DCHECK(thread_checker_.CalledOnValidThread()); | 31 DCHECK(thread_checker_.CalledOnValidThread()); |
32 connector_.reset(); | 32 connector_.reset(); |
33 } | 33 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 return std::move(connection); | 69 return std::move(connection); |
70 } | 70 } |
71 | 71 |
72 void ConnectorImpl::BindInterface( | 72 void ConnectorImpl::BindInterface( |
73 const Identity& target, | 73 const Identity& target, |
74 const std::string& interface_name, | 74 const std::string& interface_name, |
75 mojo::ScopedMessagePipeHandle interface_pipe) { | 75 mojo::ScopedMessagePipeHandle interface_pipe) { |
76 if (!BindConnectorIfNecessary()) | 76 if (!BindConnectorIfNecessary()) |
77 return; | 77 return; |
78 | 78 |
| 79 if (!local_binder_overrides_.empty() && |
| 80 local_binder_overrides_.count(interface_name)) { |
| 81 local_binder_overrides_[interface_name].Run(std::move(interface_pipe)); |
| 82 return; |
| 83 } |
| 84 |
79 connector_->BindInterface(target, interface_name, std::move(interface_pipe), | 85 connector_->BindInterface(target, interface_name, std::move(interface_pipe), |
80 base::Bind(&EmptyBindCallback)); | 86 base::Bind(&EmptyBindCallback)); |
81 } | 87 } |
82 | 88 |
83 std::unique_ptr<Connector> ConnectorImpl::Clone() { | 89 std::unique_ptr<Connector> ConnectorImpl::Clone() { |
84 if (!BindConnectorIfNecessary()) | 90 if (!BindConnectorIfNecessary()) |
85 return nullptr; | 91 return nullptr; |
86 | 92 |
87 mojom::ConnectorPtr connector; | 93 mojom::ConnectorPtr connector; |
88 mojom::ConnectorRequest request(&connector); | 94 mojom::ConnectorRequest request(&connector); |
89 connector_->Clone(std::move(request)); | 95 connector_->Clone(std::move(request)); |
90 return base::MakeUnique<ConnectorImpl>(connector.PassInterface()); | 96 return base::MakeUnique<ConnectorImpl>(connector.PassInterface()); |
91 } | 97 } |
92 | 98 |
93 void ConnectorImpl::BindConnectorRequest(mojom::ConnectorRequest request) { | 99 void ConnectorImpl::BindConnectorRequest(mojom::ConnectorRequest request) { |
94 if (!BindConnectorIfNecessary()) | 100 if (!BindConnectorIfNecessary()) |
95 return; | 101 return; |
96 connector_->Clone(std::move(request)); | 102 connector_->Clone(std::move(request)); |
97 } | 103 } |
98 | 104 |
| 105 base::WeakPtr<Connector> ConnectorImpl::GetWeakPtr() { |
| 106 return weak_factory_.GetWeakPtr(); |
| 107 } |
| 108 |
| 109 void ConnectorImpl::OverrideBinderForTesting(const std::string& interface_name, |
| 110 const TestApi::Binder& binder) { |
| 111 local_binder_overrides_[interface_name] = binder; |
| 112 } |
| 113 |
| 114 void ConnectorImpl::ClearBinderOverrides() { |
| 115 local_binder_overrides_.clear(); |
| 116 } |
| 117 |
99 bool ConnectorImpl::BindConnectorIfNecessary() { | 118 bool ConnectorImpl::BindConnectorIfNecessary() { |
100 // Bind this object to the current thread the first time it is used to | 119 // Bind this object to the current thread the first time it is used to |
101 // connect. | 120 // connect. |
102 if (!connector_.is_bound()) { | 121 if (!connector_.is_bound()) { |
103 if (!unbound_state_.is_valid()) { | 122 if (!unbound_state_.is_valid()) { |
104 // It's possible to get here when the link to the service manager has been | 123 // It's possible to get here when the link to the service manager has been |
105 // severed | 124 // severed |
106 // (and so the connector pipe has been closed) but the app has chosen not | 125 // (and so the connector pipe has been closed) but the app has chosen not |
107 // to quit. | 126 // to quit. |
108 return false; | 127 return false; |
(...skipping 10 matching lines...) Expand all Loading... |
119 return true; | 138 return true; |
120 } | 139 } |
121 | 140 |
122 std::unique_ptr<Connector> Connector::Create(mojom::ConnectorRequest* request) { | 141 std::unique_ptr<Connector> Connector::Create(mojom::ConnectorRequest* request) { |
123 mojom::ConnectorPtr proxy; | 142 mojom::ConnectorPtr proxy; |
124 *request = mojo::MakeRequest(&proxy); | 143 *request = mojo::MakeRequest(&proxy); |
125 return base::MakeUnique<ConnectorImpl>(proxy.PassInterface()); | 144 return base::MakeUnique<ConnectorImpl>(proxy.PassInterface()); |
126 } | 145 } |
127 | 146 |
128 } // namespace service_manager | 147 } // namespace service_manager |
OLD | NEW |