| 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 { |
| 14 void EmptyBindCallback(mojom::ConnectResult, const std::string&) {} |
| 15 } |
| 16 |
| 13 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtrInfo unbound_state) | 17 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtrInfo unbound_state) |
| 14 : unbound_state_(std::move(unbound_state)) { | 18 : unbound_state_(std::move(unbound_state)) { |
| 15 thread_checker_.DetachFromThread(); | 19 thread_checker_.DetachFromThread(); |
| 16 } | 20 } |
| 17 | 21 |
| 18 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtr connector) | 22 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtr connector) |
| 19 : connector_(std::move(connector)) { | 23 : connector_(std::move(connector)) { |
| 20 connector_.set_connection_error_handler( | 24 connector_.set_connection_error_handler( |
| 21 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); | 25 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); |
| 22 } | 26 } |
| 23 | 27 |
| 24 ConnectorImpl::~ConnectorImpl() {} | 28 ConnectorImpl::~ConnectorImpl() {} |
| 25 | 29 |
| 26 void ConnectorImpl::OnConnectionError() { | 30 void ConnectorImpl::OnConnectionError() { |
| 27 DCHECK(thread_checker_.CalledOnValidThread()); | 31 DCHECK(thread_checker_.CalledOnValidThread()); |
| 28 connector_.reset(); | 32 connector_.reset(); |
| 29 } | 33 } |
| 30 | 34 |
| 31 void ConnectorImpl::Start( | 35 void ConnectorImpl::StartService( |
| 32 const Identity& identity, | 36 const Identity& identity, |
| 33 mojom::ServicePtr service, | 37 mojom::ServicePtr service, |
| 34 mojom::PIDReceiverRequest pid_receiver_request) { | 38 mojom::PIDReceiverRequest pid_receiver_request) { |
| 35 if (!BindIfNecessary()) | 39 if (!BindConnectorIfNecessary()) |
| 36 return; | 40 return; |
| 37 | 41 |
| 38 DCHECK(service.is_bound() && pid_receiver_request.is_pending()); | 42 DCHECK(service.is_bound() && pid_receiver_request.is_pending()); |
| 39 connector_->Start(identity, | 43 connector_->StartService(identity, |
| 40 service.PassInterface().PassHandle(), | 44 service.PassInterface().PassHandle(), |
| 41 std::move(pid_receiver_request)); | 45 std::move(pid_receiver_request)); |
| 42 } | 46 } |
| 43 | 47 |
| 44 std::unique_ptr<Connection> ConnectorImpl::Connect(const std::string& name) { | 48 std::unique_ptr<Connection> ConnectorImpl::Connect(const std::string& name) { |
| 45 return Connect(Identity(name, mojom::kInheritUserID)); | 49 return Connect(Identity(name, mojom::kInheritUserID)); |
| 46 } | 50 } |
| 47 | 51 |
| 48 std::unique_ptr<Connection> ConnectorImpl::Connect(const Identity& target) { | 52 std::unique_ptr<Connection> ConnectorImpl::Connect(const Identity& target) { |
| 49 if (!BindIfNecessary()) | 53 if (!BindConnectorIfNecessary()) |
| 50 return nullptr; | 54 return nullptr; |
| 51 | 55 |
| 52 DCHECK(thread_checker_.CalledOnValidThread()); | 56 DCHECK(thread_checker_.CalledOnValidThread()); |
| 53 | 57 |
| 54 mojom::InterfaceProviderPtr remote_interfaces; | 58 mojom::InterfaceProviderPtr remote_interfaces; |
| 55 mojom::InterfaceProviderRequest remote_request(&remote_interfaces); | 59 mojom::InterfaceProviderRequest remote_request(&remote_interfaces); |
| 56 std::unique_ptr<internal::ConnectionImpl> connection( | 60 std::unique_ptr<internal::ConnectionImpl> connection( |
| 57 new internal::ConnectionImpl(target, Connection::State::PENDING)); | 61 new internal::ConnectionImpl(target, Connection::State::PENDING)); |
| 58 std::unique_ptr<InterfaceProvider> remote_interface_provider( | 62 std::unique_ptr<InterfaceProvider> remote_interface_provider( |
| 59 new InterfaceProvider); | 63 new InterfaceProvider); |
| 60 remote_interface_provider->Bind(std::move(remote_interfaces)); | 64 remote_interface_provider->Bind(std::move(remote_interfaces)); |
| 61 connection->SetRemoteInterfaces(std::move(remote_interface_provider)); | 65 connection->SetRemoteInterfaces(std::move(remote_interface_provider)); |
| 62 | 66 |
| 63 connector_->Connect(target, std::move(remote_request), | 67 connector_->Connect(target, std::move(remote_request), |
| 64 connection->GetConnectCallback()); | 68 connection->GetConnectCallback()); |
| 65 return std::move(connection); | 69 return std::move(connection); |
| 66 } | 70 } |
| 67 | 71 |
| 72 void ConnectorImpl::BindInterface( |
| 73 const Identity& target, |
| 74 const std::string& interface_name, |
| 75 mojo::ScopedMessagePipeHandle interface_pipe) { |
| 76 if (!BindConnectorIfNecessary()) |
| 77 return; |
| 78 |
| 79 connector_->BindInterface(target, interface_name, std::move(interface_pipe), |
| 80 base::Bind(&EmptyBindCallback)); |
| 81 } |
| 82 |
| 68 std::unique_ptr<Connector> ConnectorImpl::Clone() { | 83 std::unique_ptr<Connector> ConnectorImpl::Clone() { |
| 69 if (!BindIfNecessary()) | 84 if (!BindConnectorIfNecessary()) |
| 70 return nullptr; | 85 return nullptr; |
| 71 | 86 |
| 72 mojom::ConnectorPtr connector; | 87 mojom::ConnectorPtr connector; |
| 73 mojom::ConnectorRequest request(&connector); | 88 mojom::ConnectorRequest request(&connector); |
| 74 connector_->Clone(std::move(request)); | 89 connector_->Clone(std::move(request)); |
| 75 return base::MakeUnique<ConnectorImpl>(connector.PassInterface()); | 90 return base::MakeUnique<ConnectorImpl>(connector.PassInterface()); |
| 76 } | 91 } |
| 77 | 92 |
| 78 void ConnectorImpl::BindRequest(mojom::ConnectorRequest request) { | 93 void ConnectorImpl::BindConnectorRequest(mojom::ConnectorRequest request) { |
| 79 if (!BindIfNecessary()) | 94 if (!BindConnectorIfNecessary()) |
| 80 return; | 95 return; |
| 81 connector_->Clone(std::move(request)); | 96 connector_->Clone(std::move(request)); |
| 82 } | 97 } |
| 83 | 98 |
| 84 bool ConnectorImpl::BindIfNecessary() { | 99 bool ConnectorImpl::BindConnectorIfNecessary() { |
| 85 // Bind this object to the current thread the first time it is used to | 100 // Bind this object to the current thread the first time it is used to |
| 86 // connect. | 101 // connect. |
| 87 if (!connector_.is_bound()) { | 102 if (!connector_.is_bound()) { |
| 88 if (!unbound_state_.is_valid()) { | 103 if (!unbound_state_.is_valid()) { |
| 89 // It's possible to get here when the link to the service manager has been | 104 // It's possible to get here when the link to the service manager has been |
| 90 // severed | 105 // severed |
| 91 // (and so the connector pipe has been closed) but the app has chosen not | 106 // (and so the connector pipe has been closed) but the app has chosen not |
| 92 // to quit. | 107 // to quit. |
| 93 return false; | 108 return false; |
| 94 } | 109 } |
| 95 | 110 |
| 96 // Bind the ThreadChecker to this thread. | 111 // Bind the ThreadChecker to this thread. |
| 97 DCHECK(thread_checker_.CalledOnValidThread()); | 112 DCHECK(thread_checker_.CalledOnValidThread()); |
| 98 | 113 |
| 99 connector_.Bind(std::move(unbound_state_)); | 114 connector_.Bind(std::move(unbound_state_)); |
| 100 connector_.set_connection_error_handler( | 115 connector_.set_connection_error_handler( |
| 101 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); | 116 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); |
| 102 } | 117 } |
| 103 | 118 |
| 104 return true; | 119 return true; |
| 105 } | 120 } |
| 106 | 121 |
| 107 std::unique_ptr<Connector> Connector::Create(mojom::ConnectorRequest* request) { | 122 std::unique_ptr<Connector> Connector::Create(mojom::ConnectorRequest* request) { |
| 108 mojom::ConnectorPtr proxy; | 123 mojom::ConnectorPtr proxy; |
| 109 *request = mojo::MakeRequest(&proxy); | 124 *request = mojo::MakeRequest(&proxy); |
| 110 return base::MakeUnique<ConnectorImpl>(proxy.PassInterface()); | 125 return base::MakeUnique<ConnectorImpl>(proxy.PassInterface()); |
| 111 } | 126 } |
| 112 | 127 |
| 113 } // namespace service_manager | 128 } // namespace service_manager |
| OLD | NEW |