Chromium Code Reviews

Side by Side Diff: services/service_manager/public/cpp/lib/connector_impl.cc

Issue 2804373002: Eliminate Connector::Connect(), Connection, etc. (Closed)
Patch Set: . Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
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"
10 9
11 namespace service_manager { 10 namespace service_manager {
12 11
13 namespace {
14 void EmptyBindCallback(mojom::ConnectResult, const std::string&) {}
15 }
16
17 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtrInfo unbound_state) 12 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtrInfo unbound_state)
18 : unbound_state_(std::move(unbound_state)), weak_factory_(this) { 13 : unbound_state_(std::move(unbound_state)), weak_factory_(this) {
19 thread_checker_.DetachFromThread(); 14 thread_checker_.DetachFromThread();
20 } 15 }
21 16
22 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtr connector) 17 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtr connector)
23 : connector_(std::move(connector)), weak_factory_(this) { 18 : connector_(std::move(connector)), weak_factory_(this) {
24 connector_.set_connection_error_handler( 19 connector_.set_connection_error_handler(
25 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); 20 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this)));
26 } 21 }
27 22
28 ConnectorImpl::~ConnectorImpl() {} 23 ConnectorImpl::~ConnectorImpl() {}
29 24
30 void ConnectorImpl::OnConnectionError() { 25 void ConnectorImpl::OnConnectionError() {
31 DCHECK(thread_checker_.CalledOnValidThread()); 26 DCHECK(thread_checker_.CalledOnValidThread());
32 connector_.reset(); 27 connector_.reset();
33 } 28 }
34 29
30 void ConnectorImpl::StartService(const Identity& identity) {
31 if (BindConnectorIfNecessary())
32 connector_->StartService(identity,
33 base::Bind(&ConnectorImpl::StartServiceCallback,
34 weak_factory_.GetWeakPtr()));
35 }
36
37 void ConnectorImpl::StartService(const std::string& name) {
38 StartService(Identity(name, mojom::kInheritUserID));
39 }
40
35 void ConnectorImpl::StartService( 41 void ConnectorImpl::StartService(
36 const Identity& identity, 42 const Identity& identity,
37 mojom::ServicePtr service, 43 mojom::ServicePtr service,
38 mojom::PIDReceiverRequest pid_receiver_request) { 44 mojom::PIDReceiverRequest pid_receiver_request) {
39 if (!BindConnectorIfNecessary()) 45 if (!BindConnectorIfNecessary())
40 return; 46 return;
41 47
42 DCHECK(service.is_bound() && pid_receiver_request.is_pending()); 48 DCHECK(service.is_bound() && pid_receiver_request.is_pending());
43 connector_->StartService(identity, 49 connector_->StartServiceWithProcess(
44 service.PassInterface().PassHandle(), 50 identity, service.PassInterface().PassHandle(),
45 std::move(pid_receiver_request)); 51 std::move(pid_receiver_request),
46 } 52 base::Bind(&ConnectorImpl::StartServiceCallback,
47 53 weak_factory_.GetWeakPtr()));
48 std::unique_ptr<Connection> ConnectorImpl::Connect(const std::string& name) {
49 return Connect(Identity(name, mojom::kInheritUserID));
50 }
51
52 std::unique_ptr<Connection> ConnectorImpl::Connect(const Identity& target) {
53 if (!BindConnectorIfNecessary())
54 return nullptr;
55
56 DCHECK(thread_checker_.CalledOnValidThread());
57
58 mojom::InterfaceProviderPtr remote_interfaces;
59 mojom::InterfaceProviderRequest remote_request(&remote_interfaces);
60 std::unique_ptr<internal::ConnectionImpl> connection(
61 new internal::ConnectionImpl(target, Connection::State::PENDING));
62 std::unique_ptr<InterfaceProvider> remote_interface_provider(
63 new InterfaceProvider);
64 remote_interface_provider->Bind(std::move(remote_interfaces));
65 connection->SetRemoteInterfaces(std::move(remote_interface_provider));
66
67 connector_->Connect(target, std::move(remote_request),
68 connection->GetConnectCallback());
69 return std::move(connection);
70 } 54 }
71 55
72 void ConnectorImpl::BindInterface( 56 void ConnectorImpl::BindInterface(
73 const Identity& target, 57 const Identity& target,
74 const std::string& interface_name, 58 const std::string& interface_name,
75 mojo::ScopedMessagePipeHandle interface_pipe) { 59 mojo::ScopedMessagePipeHandle interface_pipe) {
76 if (!BindConnectorIfNecessary()) 60 if (!BindConnectorIfNecessary())
77 return; 61 return;
78 62
79 auto service_overrides_iter = local_binder_overrides_.find(target.name()); 63 auto service_overrides_iter = local_binder_overrides_.find(target.name());
80 if (service_overrides_iter != local_binder_overrides_.end()) { 64 if (service_overrides_iter != local_binder_overrides_.end()) {
81 auto override_iter = service_overrides_iter->second.find(interface_name); 65 auto override_iter = service_overrides_iter->second.find(interface_name);
82 if (override_iter != service_overrides_iter->second.end()) { 66 if (override_iter != service_overrides_iter->second.end()) {
83 override_iter->second.Run(std::move(interface_pipe)); 67 override_iter->second.Run(std::move(interface_pipe));
84 return; 68 return;
85 } 69 }
86 } 70 }
87 71
88 connector_->BindInterface(target, interface_name, std::move(interface_pipe), 72 connector_->BindInterface(target, interface_name, std::move(interface_pipe),
89 base::Bind(&EmptyBindCallback)); 73 base::Bind(&ConnectorImpl::StartServiceCallback,
74 weak_factory_.GetWeakPtr()));
90 } 75 }
91 76
92 std::unique_ptr<Connector> ConnectorImpl::Clone() { 77 std::unique_ptr<Connector> ConnectorImpl::Clone() {
93 if (!BindConnectorIfNecessary()) 78 if (!BindConnectorIfNecessary())
94 return nullptr; 79 return nullptr;
95 80
96 mojom::ConnectorPtr connector; 81 mojom::ConnectorPtr connector;
97 mojom::ConnectorRequest request(&connector); 82 mojom::ConnectorRequest request(&connector);
98 connector_->Clone(std::move(request)); 83 connector_->Clone(std::move(request));
99 return base::MakeUnique<ConnectorImpl>(connector.PassInterface()); 84 return base::MakeUnique<ConnectorImpl>(connector.PassInterface());
(...skipping 12 matching lines...)
112 void ConnectorImpl::OverrideBinderForTesting(const std::string& service_name, 97 void ConnectorImpl::OverrideBinderForTesting(const std::string& service_name,
113 const std::string& interface_name, 98 const std::string& interface_name,
114 const TestApi::Binder& binder) { 99 const TestApi::Binder& binder) {
115 local_binder_overrides_[service_name][interface_name] = binder; 100 local_binder_overrides_[service_name][interface_name] = binder;
116 } 101 }
117 102
118 void ConnectorImpl::ClearBinderOverrides() { 103 void ConnectorImpl::ClearBinderOverrides() {
119 local_binder_overrides_.clear(); 104 local_binder_overrides_.clear();
120 } 105 }
121 106
107 void ConnectorImpl::SetStartServiceCallback(
108 const Connector::StartServiceCallback& callback) {
109 start_service_callback_ = callback;
110 }
111
112 void ConnectorImpl::ResetStartServiceCallback() {
113 start_service_callback_.Reset();
114 }
115
122 bool ConnectorImpl::BindConnectorIfNecessary() { 116 bool ConnectorImpl::BindConnectorIfNecessary() {
123 // Bind this object to the current thread the first time it is used to 117 // Bind this object to the current thread the first time it is used to
124 // connect. 118 // connect.
125 if (!connector_.is_bound()) { 119 if (!connector_.is_bound()) {
126 if (!unbound_state_.is_valid()) { 120 if (!unbound_state_.is_valid()) {
127 // It's possible to get here when the link to the service manager has been 121 // It's possible to get here when the link to the service manager has been
128 // severed 122 // severed
129 // (and so the connector pipe has been closed) but the app has chosen not 123 // (and so the connector pipe has been closed) but the app has chosen not
130 // to quit. 124 // to quit.
131 return false; 125 return false;
132 } 126 }
133 127
134 // Bind the ThreadChecker to this thread. 128 // Bind the ThreadChecker to this thread.
135 DCHECK(thread_checker_.CalledOnValidThread()); 129 DCHECK(thread_checker_.CalledOnValidThread());
136 130
137 connector_.Bind(std::move(unbound_state_)); 131 connector_.Bind(std::move(unbound_state_));
138 connector_.set_connection_error_handler( 132 connector_.set_connection_error_handler(
139 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); 133 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this)));
140 } 134 }
141 135
142 return true; 136 return true;
143 } 137 }
144 138
139 void ConnectorImpl::StartServiceCallback(mojom::ConnectResult result,
140 const Identity& user_id) {
141 if (!start_service_callback_.is_null())
142 start_service_callback_.Run(result, user_id);
143 }
144
145 std::unique_ptr<Connector> Connector::Create(mojom::ConnectorRequest* request) { 145 std::unique_ptr<Connector> Connector::Create(mojom::ConnectorRequest* request) {
146 mojom::ConnectorPtr proxy; 146 mojom::ConnectorPtr proxy;
147 *request = mojo::MakeRequest(&proxy); 147 *request = mojo::MakeRequest(&proxy);
148 return base::MakeUnique<ConnectorImpl>(proxy.PassInterface()); 148 return base::MakeUnique<ConnectorImpl>(proxy.PassInterface());
149 } 149 }
150 150
151 } // namespace service_manager 151 } // namespace service_manager
OLDNEW
« no previous file with comments | « services/service_manager/public/cpp/lib/connector_impl.h ('k') | services/service_manager/public/cpp/lib/identity.cc » ('j') | no next file with comments »

Powered by Google App Engine