Commit da0622b2 authored by Julie Jeongeun Kim's avatar Julie Jeongeun Kim Committed by Commit Bot

Convert connector.mojom of chromecast to new Mojo types

This CL converts ExternalService and ExternalConnector
to new Mojo types.

It also updates RegisterServiceInstance and Clone from
connector.mojom and members and methods which implement
them.

Bug: 955171
Change-Id: I8b9c6ab70a65b8c5661b294ec0ccde742123522a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1803940
Commit-Queue: Julie Kim <jkim@igalia.com>
Reviewed-by: default avatarKenneth MacKay <kmackay@chromium.org>
Reviewed-by: default avatarOksana Zhuravlova <oksamyt@chromium.org>
Reviewed-by: default avatarSam McNally <sammc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#697446}
parent 8dab8bcb
......@@ -40,14 +40,12 @@ ChromiumServiceWrapper::ChromiumServiceWrapper(
std::unique_ptr<service_manager::Service> chromium_service,
const std::string& service_name)
: service_ptr_(std::move(service_ptr)),
chromium_service_(std::move(chromium_service)),
service_binding_(this) {
chromium_service_(std::move(chromium_service)) {
DCHECK(connector);
DCHECK(chromium_service_);
external_mojo::mojom::ExternalServicePtr ptr;
service_binding_.Bind(mojo::MakeRequest(&ptr));
connector->RegisterService(service_name, std::move(ptr));
connector->RegisterService(service_name,
service_receiver_.BindNewPipeAndPassRemote());
}
ChromiumServiceWrapper::~ChromiumServiceWrapper() = default;
......
......@@ -10,7 +10,7 @@
#include "base/macros.h"
#include "chromecast/external_mojo/public/mojom/connector.mojom.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "services/service_manager/public/cpp/identity.h"
#include "services/service_manager/public/cpp/service.h"
......@@ -44,7 +44,7 @@ class ChromiumServiceWrapper : public external_mojo::mojom::ExternalService {
const service_manager::mojom::ServicePtr service_ptr_;
const std::unique_ptr<service_manager::Service> chromium_service_;
mojo::Binding<external_mojo::mojom::ExternalService> service_binding_;
mojo::Receiver<external_mojo::mojom::ExternalService> service_receiver_{this};
DISALLOW_COPY_AND_ASSIGN(ChromiumServiceWrapper);
};
......
......@@ -14,6 +14,7 @@
#include "chromecast/external_mojo/public/mojom/connector.mojom.h"
#include "mojo/public/cpp/bindings/interface_ptr.h"
#include "mojo/public/cpp/bindings/interface_request.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/system/message_pipe.h"
namespace chromecast {
......@@ -47,7 +48,8 @@ class ExternalConnector {
ExternalService* service) = 0;
virtual void RegisterService(
const std::string& service_name,
external_mojo::mojom::ExternalServicePtr service_ptr) = 0;
mojo::PendingRemote<external_mojo::mojom::ExternalService>
service_remote) = 0;
// Asks the Mojo broker to bind to a matching interface on the service with
// the given |service_name|. If the service does not yet exist, the binding
......
......@@ -50,21 +50,22 @@ void ExternalConnector::Connect(
kConnectRetryDelay);
return;
}
external_mojo::mojom::ExternalConnectorPtr connector(
external_mojo::mojom::ExternalConnectorPtrInfo(std::move(pipe), 0));
mojo::Remote<external_mojo::mojom::ExternalConnector> connector(
mojo::PendingRemote<external_mojo::mojom::ExternalConnector>(
std::move(pipe), 0));
std::move(callback).Run(
std::make_unique<ExternalConnectorImpl>(std::move(connector)));
}
ExternalConnectorImpl::ExternalConnectorImpl(
external_mojo::mojom::ExternalConnectorPtr connector)
mojo::Remote<external_mojo::mojom::ExternalConnector> connector)
: connector_(std::move(connector)) {
connector_.set_connection_error_handler(base::BindOnce(
&ExternalConnectorImpl::OnConnectionError, base::Unretained(this)));
connector_.set_disconnect_handler(base::BindOnce(
&ExternalConnectorImpl::OnMojoDisconnect, base::Unretained(this)));
}
ExternalConnectorImpl::ExternalConnectorImpl(
external_mojo::mojom::ExternalConnectorPtrInfo unbound_state)
mojo::PendingRemote<external_mojo::mojom::ExternalConnector> unbound_state)
: unbound_state_(std::move(unbound_state)) {
DETACH_FROM_SEQUENCE(sequence_checker_);
}
......@@ -78,14 +79,15 @@ void ExternalConnectorImpl::SetConnectionErrorCallback(
void ExternalConnectorImpl::RegisterService(const std::string& service_name,
ExternalService* service) {
RegisterService(service_name, service->GetBinding());
RegisterService(service_name, service->GetReceiver());
}
void ExternalConnectorImpl::RegisterService(
const std::string& service_name,
external_mojo::mojom::ExternalServicePtr service_ptr) {
mojo::PendingRemote<external_mojo::mojom::ExternalService> service_remote) {
if (BindConnectorIfNecessary()) {
connector_->RegisterServiceInstance(service_name, std::move(service_ptr));
connector_->RegisterServiceInstance(service_name,
std::move(service_remote));
}
}
......@@ -109,12 +111,12 @@ void ExternalConnectorImpl::BindInterface(
}
std::unique_ptr<ExternalConnector> ExternalConnectorImpl::Clone() {
external_mojo::mojom::ExternalConnectorPtrInfo connector_info;
auto request = mojo::MakeRequest(&connector_info);
mojo::PendingRemote<external_mojo::mojom::ExternalConnector> connector_remote;
auto receiver = connector_remote.InitWithNewPipeAndPassReceiver();
if (BindConnectorIfNecessary()) {
connector_->Clone(std::move(request));
connector_->Clone(std::move(receiver));
}
return std::make_unique<ExternalConnectorImpl>(std::move(connector_info));
return std::make_unique<ExternalConnectorImpl>(std::move(connector_remote));
}
void ExternalConnectorImpl::SendChromiumConnectorRequest(
......@@ -124,7 +126,7 @@ void ExternalConnectorImpl::SendChromiumConnectorRequest(
}
}
void ExternalConnectorImpl::OnConnectionError() {
void ExternalConnectorImpl::OnMojoDisconnect() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
connector_.reset();
if (connection_error_callback_) {
......@@ -141,13 +143,13 @@ bool ExternalConnectorImpl::BindConnectorIfNecessary() {
}
if (!unbound_state_.is_valid()) {
// OnConnectionError was already called, but |this| was not destroyed.
// OnMojoDisconnect was already called, but |this| was not destroyed.
return false;
}
connector_.Bind(std::move(unbound_state_));
connector_.set_connection_error_handler(base::BindOnce(
&ExternalConnectorImpl::OnConnectionError, base::Unretained(this)));
connector_.set_disconnect_handler(base::BindOnce(
&ExternalConnectorImpl::OnMojoDisconnect, base::Unretained(this)));
return true;
}
......
......@@ -13,6 +13,8 @@
#include "base/macros.h"
#include "base/sequence_checker.h"
#include "chromecast/external_mojo/external_service_support/external_connector.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
namespace chromecast {
namespace external_service_support {
......@@ -20,9 +22,10 @@ namespace external_service_support {
class ExternalConnectorImpl : public ExternalConnector {
public:
explicit ExternalConnectorImpl(
external_mojo::mojom::ExternalConnectorPtr connector);
mojo::Remote<external_mojo::mojom::ExternalConnector> connector);
explicit ExternalConnectorImpl(
external_mojo::mojom::ExternalConnectorPtrInfo unbound_state);
mojo::PendingRemote<external_mojo::mojom::ExternalConnector>
unbound_state);
~ExternalConnectorImpl() override;
// ExternalConnector implementation:
......@@ -31,7 +34,8 @@ class ExternalConnectorImpl : public ExternalConnector {
ExternalService* service) override;
void RegisterService(
const std::string& service_name,
external_mojo::mojom::ExternalServicePtr service_ptr) override;
mojo::PendingRemote<external_mojo::mojom::ExternalService> service_remote)
override;
void BindInterface(const std::string& service_name,
const std::string& interface_name,
mojo::ScopedMessagePipeHandle interface_pipe) override;
......@@ -45,11 +49,11 @@ class ExternalConnectorImpl : public ExternalConnector {
callback) override;
private:
void OnConnectionError();
void OnMojoDisconnect();
bool BindConnectorIfNecessary();
external_mojo::mojom::ExternalConnectorPtr connector_;
external_mojo::mojom::ExternalConnectorPtrInfo unbound_state_;
mojo::Remote<external_mojo::mojom::ExternalConnector> connector_;
mojo::PendingRemote<external_mojo::mojom::ExternalConnector> unbound_state_;
base::OnceClosure connection_error_callback_;
SEQUENCE_CHECKER(sequence_checker_);
......
......@@ -9,18 +9,17 @@
namespace chromecast {
namespace external_service_support {
ExternalService::ExternalService() : service_binding_(this) {}
ExternalService::ExternalService() = default;
ExternalService::~ExternalService() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
external_mojo::mojom::ExternalServicePtr ExternalService::GetBinding() {
mojo::PendingRemote<external_mojo::mojom::ExternalService>
ExternalService::GetReceiver() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
service_binding_.Close();
external_mojo::mojom::ExternalServicePtr ptr;
service_binding_.Bind(mojo::MakeRequest(&ptr));
return ptr;
service_receiver_.reset();
return service_receiver_.BindNewPipeAndPassRemote();
}
void ExternalService::AddInterface(const std::string& interface_name,
......
......@@ -14,8 +14,8 @@
#include "base/macros.h"
#include "base/sequence_checker.h"
#include "chromecast/external_mojo/public/mojom/connector.mojom.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/public/cpp/bindings/interface_request.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/system/message_pipe.h"
namespace chromecast {
......@@ -27,8 +27,8 @@ class ExternalService : public external_mojo::mojom::ExternalService {
ExternalService();
~ExternalService() override;
// Returns the Mojo binding for this service.
external_mojo::mojom::ExternalServicePtr GetBinding();
// Returns the Mojo receiver for this service.
mojo::PendingRemote<external_mojo::mojom::ExternalService> GetReceiver();
// Adds an interface that users of this service may bind to. To avoid races
// where the service is registered but interfaces cannot be bound by other
......@@ -94,7 +94,7 @@ class ExternalService : public external_mojo::mojom::ExternalService {
mojo::ScopedMessagePipeHandle interface_pipe) override;
std::map<std::string, std::unique_ptr<Binder>> binders_;
mojo::Binding<external_mojo::mojom::ExternalService> service_binding_;
mojo::Receiver<external_mojo::mojom::ExternalService> service_receiver_{this};
SEQUENCE_CHECKER(sequence_checker_);
......
......@@ -20,6 +20,8 @@
#include "chromecast/external_mojo/public/mojom/connector.mojom.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/platform/named_platform_channel.h"
#include "mojo/public/cpp/platform/platform_channel_endpoint.h"
#include "mojo/public/cpp/platform/platform_handle.h"
......@@ -73,8 +75,8 @@ class ExternalMojoBroker::ConnectorImpl : public mojom::ExternalConnector {
RegisterExternalServices(external_services_to_proxy);
}
void AddBinding(mojom::ExternalConnectorRequest request) {
bindings_.AddBinding(this, std::move(request));
void AddReceiver(mojo::PendingReceiver<mojom::ExternalConnector> receiver) {
receivers_.Add(this, std::move(receiver));
}
private:
......@@ -217,14 +219,16 @@ class ExternalMojoBroker::ConnectorImpl : public mojom::ExternalConnector {
}
// standalone::mojom::Connector implementation:
void RegisterServiceInstance(const std::string& service_name,
mojom::ExternalServicePtr service) override {
void RegisterServiceInstance(
const std::string& service_name,
mojo::PendingRemote<mojom::ExternalService> service_remote) override {
if (services_.find(service_name) != services_.end()) {
LOG(ERROR) << "Duplicate service " << service_name;
return;
}
LOG(INFO) << "Register service " << service_name;
service.set_connection_error_handler(base::BindOnce(
mojo::Remote<mojom::ExternalService> service(std::move(service_remote));
service.set_disconnect_handler(base::BindOnce(
&ConnectorImpl::OnServiceLost, base::Unretained(this), service_name));
auto it = services_.emplace(service_name, std::move(service)).first;
......@@ -266,8 +270,9 @@ class ExternalMojoBroker::ConnectorImpl : public mojom::ExternalConnector {
std::move(interface_pipe)));
}
void Clone(mojom::ExternalConnectorRequest request) override {
AddBinding(std::move(request));
void Clone(
mojo::PendingReceiver<mojom::ExternalConnector> receiver) override {
AddReceiver(std::move(receiver));
}
void BindChromiumConnector(
......@@ -325,11 +330,11 @@ class ExternalMojoBroker::ConnectorImpl : public mojom::ExternalConnector {
ServiceManagerConnectorFacade connector_facade_;
std::unique_ptr<service_manager::Connector> connector_;
mojo::BindingSet<mojom::ExternalConnector> bindings_;
mojo::ReceiverSet<mojom::ExternalConnector> receivers_;
std::map<std::string, std::unique_ptr<ExternalServiceProxy>>
registered_external_services_;
std::map<std::string, mojom::ExternalServicePtr> services_;
std::map<std::string, mojo::Remote<mojom::ExternalService>> services_;
std::map<std::string, std::vector<PendingBindRequest>> pending_bind_requests_;
std::map<std::string, mojom::ExternalServiceInfo> services_info_;
......@@ -361,7 +366,8 @@ class ExternalMojoBroker::ReadWatcher
std::move(invitation), base::kNullProcessHandle,
mojo::PlatformChannelEndpoint(
mojo::PlatformHandle(std::move(accepted_fd))));
connector_->AddBinding(mojom::ExternalConnectorRequest(std::move(pipe)));
connector_->AddReceiver(
mojo::PendingReceiver<mojom::ExternalConnector>(std::move(pipe)));
}
}
......@@ -398,10 +404,11 @@ void ExternalMojoBroker::InitializeChromium(
external_services_to_proxy);
}
mojom::ExternalConnectorPtr ExternalMojoBroker::CreateConnector() {
mojom::ExternalConnectorPtrInfo info;
connector_->AddBinding(mojo::MakeRequest(&info));
return mojom::ExternalConnectorPtr(std::move(info));
mojo::PendingRemote<mojom::ExternalConnector>
ExternalMojoBroker::CreateConnector() {
mojo::PendingRemote<mojom::ExternalConnector> remote;
connector_->AddReceiver(remote.InitWithNewPipeAndPassReceiver());
return remote;
}
ExternalMojoBroker::~ExternalMojoBroker() = default;
......
......@@ -11,6 +11,7 @@
#include "base/macros.h"
#include "chromecast/external_mojo/public/mojom/connector.mojom.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
namespace service_manager {
class Connector;
......@@ -35,7 +36,7 @@ class ExternalMojoBroker {
std::unique_ptr<service_manager::Connector> connector,
const std::vector<std::string>& external_services_to_proxy);
mojom::ExternalConnectorPtr CreateConnector();
mojo::PendingRemote<mojom::ExternalConnector> CreateConnector();
private:
class ConnectorImpl;
......
......@@ -33,7 +33,7 @@ interface ExternalService {
interface ExternalConnector {
// Registers a service with the broker.
RegisterServiceInstance(string service_name,
ExternalService service);
pending_remote<ExternalService> service);
// Asks the broker to pass the |interface_pipe| to the registered service with
// the given |service_name| to be bound to the appropriate interface. If the
......@@ -43,9 +43,9 @@ interface ExternalConnector {
string interface_name,
handle<message_pipe> interface_pipe);
// Creates a new binding to the Connector implementation, to allow clients to
// Binds a new receiver to the Connector implementation, to allow clients to
// clone Connector pointers for use on other threads.
Clone(ExternalConnector& request);
Clone(pending_receiver<ExternalConnector> receiver);
// Binds to a Chromium service_manager::Connector instance, if possible.
BindChromiumConnector(handle<message_pipe> interface_pipe);
......
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