OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/connector.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 | 9 |
10 namespace service_manager { | 10 namespace service_manager { |
11 | 11 |
12 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtrInfo unbound_state) | 12 //////////////////////////////////////////////////////////////////////////////// |
| 13 // Connector, public: |
| 14 |
| 15 Connector::Connector(mojom::ConnectorPtrInfo unbound_state) |
13 : unbound_state_(std::move(unbound_state)), weak_factory_(this) { | 16 : unbound_state_(std::move(unbound_state)), weak_factory_(this) { |
14 thread_checker_.DetachFromThread(); | 17 thread_checker_.DetachFromThread(); |
15 } | 18 } |
16 | 19 |
17 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtr connector) | 20 Connector::Connector(mojom::ConnectorPtr connector) |
18 : connector_(std::move(connector)), weak_factory_(this) { | 21 : connector_(std::move(connector)), weak_factory_(this) { |
19 connector_.set_connection_error_handler( | 22 connector_.set_connection_error_handler( |
20 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); | 23 base::Bind(&Connector::OnConnectionError, base::Unretained(this))); |
21 } | 24 } |
22 | 25 |
23 ConnectorImpl::~ConnectorImpl() {} | 26 Connector::~Connector() = default; |
24 | 27 |
25 void ConnectorImpl::OnConnectionError() { | 28 std::unique_ptr<Connector> Connector::Create(mojom::ConnectorRequest* request) { |
26 DCHECK(thread_checker_.CalledOnValidThread()); | 29 mojom::ConnectorPtr proxy; |
27 connector_.reset(); | 30 *request = mojo::MakeRequest(&proxy); |
| 31 return base::MakeUnique<Connector>(proxy.PassInterface()); |
28 } | 32 } |
29 | 33 |
30 void ConnectorImpl::StartService(const Identity& identity) { | 34 void Connector::StartService(const Identity& identity) { |
31 if (BindConnectorIfNecessary()) | 35 if (BindConnectorIfNecessary()) |
32 connector_->StartService(identity, | 36 connector_->StartService(identity, |
33 base::Bind(&ConnectorImpl::StartServiceCallback, | 37 base::Bind(&Connector::RunStartServiceCallback, |
34 weak_factory_.GetWeakPtr())); | 38 weak_factory_.GetWeakPtr())); |
35 } | 39 } |
36 | 40 |
37 void ConnectorImpl::StartService(const std::string& name) { | 41 void Connector::StartService(const std::string& name) { |
38 StartService(Identity(name, mojom::kInheritUserID)); | 42 StartService(Identity(name, mojom::kInheritUserID)); |
39 } | 43 } |
40 | 44 |
41 void ConnectorImpl::StartService( | 45 void Connector::StartService(const Identity& identity, |
42 const Identity& identity, | 46 mojom::ServicePtr service, |
43 mojom::ServicePtr service, | 47 mojom::PIDReceiverRequest pid_receiver_request) { |
44 mojom::PIDReceiverRequest pid_receiver_request) { | |
45 if (!BindConnectorIfNecessary()) | 48 if (!BindConnectorIfNecessary()) |
46 return; | 49 return; |
47 | 50 |
48 DCHECK(service.is_bound() && pid_receiver_request.is_pending()); | 51 DCHECK(service.is_bound() && pid_receiver_request.is_pending()); |
49 connector_->StartServiceWithProcess( | 52 connector_->StartServiceWithProcess( |
50 identity, service.PassInterface().PassHandle(), | 53 identity, service.PassInterface().PassHandle(), |
51 std::move(pid_receiver_request), | 54 std::move(pid_receiver_request), |
52 base::Bind(&ConnectorImpl::StartServiceCallback, | 55 base::Bind(&Connector::RunStartServiceCallback, |
53 weak_factory_.GetWeakPtr())); | 56 weak_factory_.GetWeakPtr())); |
54 } | 57 } |
55 | 58 |
56 void ConnectorImpl::BindInterface( | 59 void Connector::BindInterface(const Identity& target, |
57 const Identity& target, | 60 const std::string& interface_name, |
58 const std::string& interface_name, | 61 mojo::ScopedMessagePipeHandle interface_pipe) { |
59 mojo::ScopedMessagePipeHandle interface_pipe) { | |
60 if (!BindConnectorIfNecessary()) | 62 if (!BindConnectorIfNecessary()) |
61 return; | 63 return; |
62 | 64 |
63 auto service_overrides_iter = local_binder_overrides_.find(target.name()); | 65 auto service_overrides_iter = local_binder_overrides_.find(target.name()); |
64 if (service_overrides_iter != local_binder_overrides_.end()) { | 66 if (service_overrides_iter != local_binder_overrides_.end()) { |
65 auto override_iter = service_overrides_iter->second.find(interface_name); | 67 auto override_iter = service_overrides_iter->second.find(interface_name); |
66 if (override_iter != service_overrides_iter->second.end()) { | 68 if (override_iter != service_overrides_iter->second.end()) { |
67 override_iter->second.Run(std::move(interface_pipe)); | 69 override_iter->second.Run(std::move(interface_pipe)); |
68 return; | 70 return; |
69 } | 71 } |
70 } | 72 } |
71 | 73 |
72 connector_->BindInterface(target, interface_name, std::move(interface_pipe), | 74 connector_->BindInterface(target, interface_name, std::move(interface_pipe), |
73 base::Bind(&ConnectorImpl::StartServiceCallback, | 75 base::Bind(&Connector::RunStartServiceCallback, |
74 weak_factory_.GetWeakPtr())); | 76 weak_factory_.GetWeakPtr())); |
75 } | 77 } |
76 | 78 |
77 std::unique_ptr<Connector> ConnectorImpl::Clone() { | 79 std::unique_ptr<Connector> Connector::Clone() { |
78 if (!BindConnectorIfNecessary()) | 80 if (!BindConnectorIfNecessary()) |
79 return nullptr; | 81 return nullptr; |
80 | 82 |
81 mojom::ConnectorPtr connector; | 83 mojom::ConnectorPtr connector; |
82 mojom::ConnectorRequest request(&connector); | 84 mojom::ConnectorRequest request(&connector); |
83 connector_->Clone(std::move(request)); | 85 connector_->Clone(std::move(request)); |
84 return base::MakeUnique<ConnectorImpl>(connector.PassInterface()); | 86 return base::MakeUnique<Connector>(connector.PassInterface()); |
85 } | 87 } |
86 | 88 |
87 void ConnectorImpl::FilterInterfaces(const std::string& spec, | 89 void Connector::FilterInterfaces(const std::string& spec, |
88 const Identity& source_identity, | 90 const Identity& source_identity, |
89 mojom::InterfaceProviderRequest request, | 91 mojom::InterfaceProviderRequest request, |
90 mojom::InterfaceProviderPtr target) { | 92 mojom::InterfaceProviderPtr target) { |
91 if (!BindConnectorIfNecessary()) | 93 if (!BindConnectorIfNecessary()) |
92 return; | 94 return; |
93 connector_->FilterInterfaces(spec, source_identity, std::move(request), | 95 connector_->FilterInterfaces(spec, source_identity, std::move(request), |
94 std::move(target)); | 96 std::move(target)); |
95 } | 97 } |
96 | 98 |
97 void ConnectorImpl::BindConnectorRequest(mojom::ConnectorRequest request) { | 99 void Connector::BindConnectorRequest(mojom::ConnectorRequest request) { |
98 if (!BindConnectorIfNecessary()) | 100 if (!BindConnectorIfNecessary()) |
99 return; | 101 return; |
100 connector_->Clone(std::move(request)); | 102 connector_->Clone(std::move(request)); |
101 } | 103 } |
102 | 104 |
103 base::WeakPtr<Connector> ConnectorImpl::GetWeakPtr() { | 105 base::WeakPtr<Connector> Connector::GetWeakPtr() { |
104 return weak_factory_.GetWeakPtr(); | 106 return weak_factory_.GetWeakPtr(); |
105 } | 107 } |
106 | 108 |
107 void ConnectorImpl::OverrideBinderForTesting(const std::string& service_name, | 109 //////////////////////////////////////////////////////////////////////////////// |
108 const std::string& interface_name, | 110 // Connector, private: |
109 const TestApi::Binder& binder) { | 111 |
| 112 void Connector::OnConnectionError() { |
| 113 DCHECK(thread_checker_.CalledOnValidThread()); |
| 114 connector_.reset(); |
| 115 } |
| 116 |
| 117 void Connector::OverrideBinderForTesting(const std::string& service_name, |
| 118 const std::string& interface_name, |
| 119 const TestApi::Binder& binder) { |
110 local_binder_overrides_[service_name][interface_name] = binder; | 120 local_binder_overrides_[service_name][interface_name] = binder; |
111 } | 121 } |
112 | 122 |
113 void ConnectorImpl::ClearBinderOverrides() { | 123 void Connector::ClearBinderOverrides() { |
114 local_binder_overrides_.clear(); | 124 local_binder_overrides_.clear(); |
115 } | 125 } |
116 | 126 |
117 void ConnectorImpl::SetStartServiceCallback( | 127 void Connector::SetStartServiceCallback( |
118 const Connector::StartServiceCallback& callback) { | 128 const Connector::StartServiceCallback& callback) { |
119 start_service_callback_ = callback; | 129 start_service_callback_ = callback; |
120 } | 130 } |
121 | 131 |
122 void ConnectorImpl::ResetStartServiceCallback() { | 132 void Connector::ResetStartServiceCallback() { |
123 start_service_callback_.Reset(); | 133 start_service_callback_.Reset(); |
124 } | 134 } |
125 | 135 |
126 bool ConnectorImpl::BindConnectorIfNecessary() { | 136 bool Connector::BindConnectorIfNecessary() { |
127 // Bind this object to the current thread the first time it is used to | 137 // Bind this object to the current thread the first time it is used to |
128 // connect. | 138 // connect. |
129 if (!connector_.is_bound()) { | 139 if (!connector_.is_bound()) { |
130 if (!unbound_state_.is_valid()) { | 140 if (!unbound_state_.is_valid()) { |
131 // It's possible to get here when the link to the service manager has been | 141 // It's possible to get here when the link to the service manager has been |
132 // severed | 142 // severed |
133 // (and so the connector pipe has been closed) but the app has chosen not | 143 // (and so the connector pipe has been closed) but the app has chosen not |
134 // to quit. | 144 // to quit. |
135 return false; | 145 return false; |
136 } | 146 } |
137 | 147 |
138 // Bind the ThreadChecker to this thread. | 148 // Bind the ThreadChecker to this thread. |
139 DCHECK(thread_checker_.CalledOnValidThread()); | 149 DCHECK(thread_checker_.CalledOnValidThread()); |
140 | 150 |
141 connector_.Bind(std::move(unbound_state_)); | 151 connector_.Bind(std::move(unbound_state_)); |
142 connector_.set_connection_error_handler( | 152 connector_.set_connection_error_handler( |
143 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); | 153 base::Bind(&Connector::OnConnectionError, base::Unretained(this))); |
144 } | 154 } |
145 | 155 |
146 return true; | 156 return true; |
147 } | 157 } |
148 | 158 |
149 void ConnectorImpl::StartServiceCallback(mojom::ConnectResult result, | 159 void Connector::RunStartServiceCallback(mojom::ConnectResult result, |
150 const Identity& user_id) { | 160 const Identity& user_id) { |
151 if (!start_service_callback_.is_null()) | 161 if (!start_service_callback_.is_null()) |
152 start_service_callback_.Run(result, user_id); | 162 start_service_callback_.Run(result, user_id); |
153 } | 163 } |
154 | 164 |
155 std::unique_ptr<Connector> Connector::Create(mojom::ConnectorRequest* request) { | |
156 mojom::ConnectorPtr proxy; | |
157 *request = mojo::MakeRequest(&proxy); | |
158 return base::MakeUnique<ConnectorImpl>(proxy.PassInterface()); | |
159 } | |
160 | |
161 } // namespace service_manager | 165 } // namespace service_manager |
OLD | NEW |