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