Commit 1f1f6b2a authored by ben's avatar ben Committed by Commit bot

Flatten Connector & ConnectorImpl

R=rockot@chromium.org

Review-Url: https://codereview.chromium.org/2857963004
Cr-Commit-Position: refs/heads/master@{#469271}
parent 93c0968c
...@@ -14,6 +14,7 @@ static_library("sources") { ...@@ -14,6 +14,7 @@ static_library("sources") {
"bind_source_info.h", "bind_source_info.h",
"binder_registry.h", "binder_registry.h",
"connect.h", "connect.h",
"connector.cc",
"connector.h", "connector.h",
"identity.h", "identity.h",
"interface_binder.h", "interface_binder.h",
...@@ -23,8 +24,6 @@ static_library("sources") { ...@@ -23,8 +24,6 @@ static_library("sources") {
"lib/binder_registry.cc", "lib/binder_registry.cc",
"lib/callback_binder.cc", "lib/callback_binder.cc",
"lib/callback_binder.h", "lib/callback_binder.h",
"lib/connector_impl.cc",
"lib/connector_impl.h",
"lib/identity.cc", "lib/identity.cc",
"lib/interface_provider.cc", "lib/interface_provider.cc",
"lib/interface_provider_spec.cc", "lib/interface_provider_spec.cc",
......
// Copyright 2016 The Chromium Authors. All rights reserved. // Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "services/service_manager/public/cpp/lib/connector_impl.h" #include "services/service_manager/public/cpp/connector.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "services/service_manager/public/cpp/identity.h" #include "services/service_manager/public/cpp/identity.h"
namespace service_manager { namespace service_manager {
ConnectorImpl::ConnectorImpl(mojom::ConnectorPtrInfo unbound_state) ////////////////////////////////////////////////////////////////////////////////
// Connector, public:
Connector::Connector(mojom::ConnectorPtrInfo unbound_state)
: unbound_state_(std::move(unbound_state)), weak_factory_(this) { : unbound_state_(std::move(unbound_state)), weak_factory_(this) {
thread_checker_.DetachFromThread(); thread_checker_.DetachFromThread();
} }
ConnectorImpl::ConnectorImpl(mojom::ConnectorPtr connector) Connector::Connector(mojom::ConnectorPtr connector)
: connector_(std::move(connector)), weak_factory_(this) { : connector_(std::move(connector)), weak_factory_(this) {
connector_.set_connection_error_handler( connector_.set_connection_error_handler(
base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); base::Bind(&Connector::OnConnectionError, base::Unretained(this)));
} }
ConnectorImpl::~ConnectorImpl() {} Connector::~Connector() = default;
void ConnectorImpl::OnConnectionError() { std::unique_ptr<Connector> Connector::Create(mojom::ConnectorRequest* request) {
DCHECK(thread_checker_.CalledOnValidThread()); mojom::ConnectorPtr proxy;
connector_.reset(); *request = mojo::MakeRequest(&proxy);
return base::MakeUnique<Connector>(proxy.PassInterface());
} }
void ConnectorImpl::StartService(const Identity& identity) { void Connector::StartService(const Identity& identity) {
if (BindConnectorIfNecessary()) if (BindConnectorIfNecessary())
connector_->StartService(identity, connector_->StartService(identity,
base::Bind(&ConnectorImpl::StartServiceCallback, base::Bind(&Connector::RunStartServiceCallback,
weak_factory_.GetWeakPtr())); weak_factory_.GetWeakPtr()));
} }
void ConnectorImpl::StartService(const std::string& name) { void Connector::StartService(const std::string& name) {
StartService(Identity(name, mojom::kInheritUserID)); StartService(Identity(name, mojom::kInheritUserID));
} }
void ConnectorImpl::StartService( void Connector::StartService(const Identity& identity,
const Identity& identity, mojom::ServicePtr service,
mojom::ServicePtr service, mojom::PIDReceiverRequest pid_receiver_request) {
mojom::PIDReceiverRequest pid_receiver_request) {
if (!BindConnectorIfNecessary()) if (!BindConnectorIfNecessary())
return; return;
...@@ -49,14 +52,13 @@ void ConnectorImpl::StartService( ...@@ -49,14 +52,13 @@ void ConnectorImpl::StartService(
connector_->StartServiceWithProcess( connector_->StartServiceWithProcess(
identity, service.PassInterface().PassHandle(), identity, service.PassInterface().PassHandle(),
std::move(pid_receiver_request), std::move(pid_receiver_request),
base::Bind(&ConnectorImpl::StartServiceCallback, base::Bind(&Connector::RunStartServiceCallback,
weak_factory_.GetWeakPtr())); weak_factory_.GetWeakPtr()));
} }
void ConnectorImpl::BindInterface( void Connector::BindInterface(const Identity& target,
const Identity& target, const std::string& interface_name,
const std::string& interface_name, mojo::ScopedMessagePipeHandle interface_pipe) {
mojo::ScopedMessagePipeHandle interface_pipe) {
if (!BindConnectorIfNecessary()) if (!BindConnectorIfNecessary())
return; return;
...@@ -70,60 +72,68 @@ void ConnectorImpl::BindInterface( ...@@ -70,60 +72,68 @@ void ConnectorImpl::BindInterface(
} }
connector_->BindInterface(target, interface_name, std::move(interface_pipe), connector_->BindInterface(target, interface_name, std::move(interface_pipe),
base::Bind(&ConnectorImpl::StartServiceCallback, base::Bind(&Connector::RunStartServiceCallback,
weak_factory_.GetWeakPtr())); weak_factory_.GetWeakPtr()));
} }
std::unique_ptr<Connector> ConnectorImpl::Clone() { std::unique_ptr<Connector> Connector::Clone() {
if (!BindConnectorIfNecessary()) if (!BindConnectorIfNecessary())
return nullptr; return nullptr;
mojom::ConnectorPtr connector; mojom::ConnectorPtr connector;
mojom::ConnectorRequest request(&connector); mojom::ConnectorRequest request(&connector);
connector_->Clone(std::move(request)); connector_->Clone(std::move(request));
return base::MakeUnique<ConnectorImpl>(connector.PassInterface()); return base::MakeUnique<Connector>(connector.PassInterface());
} }
void ConnectorImpl::FilterInterfaces(const std::string& spec, void Connector::FilterInterfaces(const std::string& spec,
const Identity& source_identity, const Identity& source_identity,
mojom::InterfaceProviderRequest request, mojom::InterfaceProviderRequest request,
mojom::InterfaceProviderPtr target) { mojom::InterfaceProviderPtr target) {
if (!BindConnectorIfNecessary()) if (!BindConnectorIfNecessary())
return; return;
connector_->FilterInterfaces(spec, source_identity, std::move(request), connector_->FilterInterfaces(spec, source_identity, std::move(request),
std::move(target)); std::move(target));
} }
void ConnectorImpl::BindConnectorRequest(mojom::ConnectorRequest request) { void Connector::BindConnectorRequest(mojom::ConnectorRequest request) {
if (!BindConnectorIfNecessary()) if (!BindConnectorIfNecessary())
return; return;
connector_->Clone(std::move(request)); connector_->Clone(std::move(request));
} }
base::WeakPtr<Connector> ConnectorImpl::GetWeakPtr() { base::WeakPtr<Connector> Connector::GetWeakPtr() {
return weak_factory_.GetWeakPtr(); return weak_factory_.GetWeakPtr();
} }
void ConnectorImpl::OverrideBinderForTesting(const std::string& service_name, ////////////////////////////////////////////////////////////////////////////////
const std::string& interface_name, // Connector, private:
const TestApi::Binder& binder) {
void Connector::OnConnectionError() {
DCHECK(thread_checker_.CalledOnValidThread());
connector_.reset();
}
void Connector::OverrideBinderForTesting(const std::string& service_name,
const std::string& interface_name,
const TestApi::Binder& binder) {
local_binder_overrides_[service_name][interface_name] = binder; local_binder_overrides_[service_name][interface_name] = binder;
} }
void ConnectorImpl::ClearBinderOverrides() { void Connector::ClearBinderOverrides() {
local_binder_overrides_.clear(); local_binder_overrides_.clear();
} }
void ConnectorImpl::SetStartServiceCallback( void Connector::SetStartServiceCallback(
const Connector::StartServiceCallback& callback) { const Connector::StartServiceCallback& callback) {
start_service_callback_ = callback; start_service_callback_ = callback;
} }
void ConnectorImpl::ResetStartServiceCallback() { void Connector::ResetStartServiceCallback() {
start_service_callback_.Reset(); start_service_callback_.Reset();
} }
bool ConnectorImpl::BindConnectorIfNecessary() { bool Connector::BindConnectorIfNecessary() {
// Bind this object to the current thread the first time it is used to // Bind this object to the current thread the first time it is used to
// connect. // connect.
if (!connector_.is_bound()) { if (!connector_.is_bound()) {
...@@ -140,22 +150,16 @@ bool ConnectorImpl::BindConnectorIfNecessary() { ...@@ -140,22 +150,16 @@ bool ConnectorImpl::BindConnectorIfNecessary() {
connector_.Bind(std::move(unbound_state_)); connector_.Bind(std::move(unbound_state_));
connector_.set_connection_error_handler( connector_.set_connection_error_handler(
base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); base::Bind(&Connector::OnConnectionError, base::Unretained(this)));
} }
return true; return true;
} }
void ConnectorImpl::StartServiceCallback(mojom::ConnectResult result, void Connector::RunStartServiceCallback(mojom::ConnectResult result,
const Identity& user_id) { const Identity& user_id) {
if (!start_service_callback_.is_null()) if (!start_service_callback_.is_null())
start_service_callback_.Run(result, user_id); start_service_callback_.Run(result, user_id);
} }
std::unique_ptr<Connector> Connector::Create(mojom::ConnectorRequest* request) {
mojom::ConnectorPtr proxy;
*request = mojo::MakeRequest(&proxy);
return base::MakeUnique<ConnectorImpl>(proxy.PassInterface());
}
} // namespace service_manager } // namespace service_manager
// Copyright 2016 The Chromium Authors. All rights reserved. // Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef SERVICES_SERVICE_MANAGER_PUBLIC_CPP_CONNECTOR_H_ #ifndef SERVICES_SERVICE_MANAGER_PUBLIC_CPP_CONNECTOR_H_
#define SERVICES_SERVICE_MANAGER_PUBLIC_CPP_CONNECTOR_H_ #define SERVICES_SERVICE_MANAGER_PUBLIC_CPP_CONNECTOR_H_
#include <map>
#include <memory> #include <memory>
#include "base/callback.h"
#include "base/threading/thread_checker.h"
#include "services/service_manager/public/cpp/identity.h" #include "services/service_manager/public/cpp/identity.h"
#include "services/service_manager/public/interfaces/connector.mojom.h" #include "services/service_manager/public/interfaces/connector.mojom.h"
#include "services/service_manager/public/interfaces/service.mojom.h" #include "services/service_manager/public/interfaces/service.mojom.h"
...@@ -64,25 +67,26 @@ class Connector { ...@@ -64,25 +67,26 @@ class Connector {
Connector* connector_; Connector* connector_;
}; };
virtual ~Connector() {} explicit Connector(mojom::ConnectorPtrInfo unbound_state);
explicit Connector(mojom::ConnectorPtr connector);
~Connector();
// Creates a new Connector instance and fills in |*request| with a request // Creates a new Connector instance and fills in |*request| with a request
// for the other end the Connector's interface. // for the other end the Connector's interface.
static std::unique_ptr<Connector> Create(mojom::ConnectorRequest* request); static std::unique_ptr<Connector> Create(mojom::ConnectorRequest* request);
// Creates an instance of a service for |identity|. // Creates an instance of a service for |identity|.
virtual void StartService(const Identity& identity) = 0; void StartService(const Identity& identity);
// Creates an instance of the service |name| inheriting the caller's identity. // Creates an instance of the service |name| inheriting the caller's identity.
virtual void StartService(const std::string& name) = 0; void StartService(const std::string& name);
// Creates an instance of a service for |identity| in a process started by the // Creates an instance of a service for |identity| in a process started by the
// client (or someone else). Must be called before BindInterface() may be // client (or someone else). Must be called before BindInterface() may be
// called to |identity|. // called to |identity|.
virtual void StartService( void StartService(const Identity& identity,
const Identity& identity, mojom::ServicePtr service,
mojom::ServicePtr service, mojom::PIDReceiverRequest pid_receiver_request);
mojom::PIDReceiverRequest pid_receiver_request) = 0;
// Connect to |target| & request to bind |Interface|. // Connect to |target| & request to bind |Interface|.
template <typename Interface> template <typename Interface>
...@@ -103,34 +107,55 @@ class Connector { ...@@ -103,34 +107,55 @@ class Connector {
return BindInterface(Identity(name, mojom::kInheritUserID), return BindInterface(Identity(name, mojom::kInheritUserID),
Interface::Name_, request.PassMessagePipe()); Interface::Name_, request.PassMessagePipe());
} }
virtual void BindInterface(const Identity& target, void BindInterface(const Identity& target,
const std::string& interface_name, const std::string& interface_name,
mojo::ScopedMessagePipeHandle interface_pipe) = 0; mojo::ScopedMessagePipeHandle interface_pipe);
// Creates a new instance of this class which may be passed to another thread. // Creates a new instance of this class which may be passed to another thread.
// The returned object may be passed multiple times until StartService() or // The returned object may be passed multiple times until StartService() or
// BindInterface() is called, at which point this method must be called again // BindInterface() is called, at which point this method must be called again
// to pass again. // to pass again.
virtual std::unique_ptr<Connector> Clone() = 0; std::unique_ptr<Connector> Clone();
virtual void FilterInterfaces(const std::string& spec, void FilterInterfaces(const std::string& spec,
const Identity& source_identity, const Identity& source_identity,
mojom::InterfaceProviderRequest request, mojom::InterfaceProviderRequest request,
mojom::InterfaceProviderPtr target) = 0; mojom::InterfaceProviderPtr target);
// Binds a Connector request to the other end of this Connector. // Binds a Connector request to the other end of this Connector.
virtual void BindConnectorRequest(mojom::ConnectorRequest request) = 0; void BindConnectorRequest(mojom::ConnectorRequest request);
virtual base::WeakPtr<Connector> GetWeakPtr() = 0; base::WeakPtr<Connector> GetWeakPtr();
protected: private:
virtual void OverrideBinderForTesting(const std::string& service_name, using BinderOverrideMap = std::map<std::string, TestApi::Binder>;
const std::string& interface_name,
const TestApi::Binder& binder) = 0; void OnConnectionError();
virtual void ClearBinderOverrides() = 0;
virtual void SetStartServiceCallback( void OverrideBinderForTesting(const std::string& service_name,
const StartServiceCallback& callback) = 0; const std::string& interface_name,
virtual void ResetStartServiceCallback() = 0; const TestApi::Binder& binder);
void ClearBinderOverrides();
void SetStartServiceCallback(const StartServiceCallback& callback);
void ResetStartServiceCallback();
bool BindConnectorIfNecessary();
// Callback passed to mojom methods StartService()/BindInterface().
void RunStartServiceCallback(mojom::ConnectResult result,
const Identity& user_id);
mojom::ConnectorPtrInfo unbound_state_;
mojom::ConnectorPtr connector_;
base::ThreadChecker thread_checker_;
std::map<std::string, BinderOverrideMap> local_binder_overrides_;
StartServiceCallback start_service_callback_;
base::WeakPtrFactory<Connector> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(Connector);
}; };
} // namespace service_manager } // namespace service_manager
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SERVICES_SERVICE_MANAGER_PUBLIC_CPP_LIB_CONNECTOR_IMPL_H_
#define SERVICES_SERVICE_MANAGER_PUBLIC_CPP_LIB_CONNECTOR_IMPL_H_
#include <map>
#include <memory>
#include "base/callback.h"
#include "base/threading/thread_checker.h"
#include "services/service_manager/public/cpp/connector.h"
#include "services/service_manager/public/interfaces/connector.mojom.h"
namespace service_manager {
class ConnectorImpl : public Connector {
public:
explicit ConnectorImpl(mojom::ConnectorPtrInfo unbound_state);
explicit ConnectorImpl(mojom::ConnectorPtr connector);
~ConnectorImpl() override;
private:
void OnConnectionError();
// Connector:
void StartService(const Identity& identity) override;
void StartService(const std::string& name) override;
void StartService(const Identity& identity,
mojom::ServicePtr service,
mojom::PIDReceiverRequest pid_receiver_request) override;
void BindInterface(const Identity& target,
const std::string& interface_name,
mojo::ScopedMessagePipeHandle interface_pipe) override;
std::unique_ptr<Connector> Clone() override;
void FilterInterfaces(const std::string& spec,
const Identity& source_identity,
mojom::InterfaceProviderRequest request,
mojom::InterfaceProviderPtr target) override;
void BindConnectorRequest(mojom::ConnectorRequest request) override;
base::WeakPtr<Connector> GetWeakPtr() override;
void OverrideBinderForTesting(const std::string& service_name,
const std::string& interface_name,
const TestApi::Binder& binder) override;
void ClearBinderOverrides() override;
void SetStartServiceCallback(const StartServiceCallback& callback) override;
void ResetStartServiceCallback() override;
bool BindConnectorIfNecessary();
// Callback passed to mojom methods StartService()/BindInterface().
void StartServiceCallback(mojom::ConnectResult result,
const Identity& user_id);
using BinderOverrideMap = std::map<std::string, TestApi::Binder>;
mojom::ConnectorPtrInfo unbound_state_;
mojom::ConnectorPtr connector_;
base::ThreadChecker thread_checker_;
std::map<std::string, BinderOverrideMap> local_binder_overrides_;
Connector::StartServiceCallback start_service_callback_;
base::WeakPtrFactory<ConnectorImpl> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(ConnectorImpl);
};
} // namespace service_manager
#endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_LIB_CONNECTOR_IMPL_H_
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/logging.h" #include "base/logging.h"
#include "mojo/public/cpp/bindings/interface_request.h" #include "mojo/public/cpp/bindings/interface_request.h"
#include "services/service_manager/public/cpp/lib/connector_impl.h" #include "services/service_manager/public/cpp/connector.h"
#include "services/service_manager/public/cpp/service.h" #include "services/service_manager/public/cpp/service.h"
namespace service_manager { namespace service_manager {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment