Commit 75609819 authored by Ken Rockot's avatar Ken Rockot Committed by Commit Bot

Migrate Service Manager to public invitation API

Migrates Service Manager away from the EDK invitation API and over to
the new public API.

Mostly a PoC CL for porting the old stuff to the new stuff, but this
also means that Service Manager now only depends on EDK for
initialization.

Bug: 844763,844764
Change-Id: I10e40a95fdd4760cabd537b2a4ac4c56fb4e33e1
Reviewed-on: https://chromium-review.googlesource.com/1070533
Commit-Queue: Ken Rockot <rockot@chromium.org>
Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#561898}
parent eca8c747
......@@ -109,6 +109,8 @@ jumbo_source_set("browser") {
"//media/mojo/services",
"//mojo/edk",
"//mojo/public/cpp/bindings",
"//mojo/public/cpp/platform",
"//mojo/public/cpp/system",
"//mojo/public/js:resources",
"//net",
"//net:extras",
......
......@@ -45,8 +45,8 @@
#include "media/media_buildflags.h"
#include "media/mojo/buildflags.h"
#include "media/mojo/interfaces/constants.mojom.h"
#include "mojo/edk/embedder/embedder.h"
#include "mojo/edk/embedder/incoming_broker_client_invitation.h"
#include "mojo/public/cpp/platform/platform_channel.h"
#include "mojo/public/cpp/system/invitation.h"
#include "services/audio/public/mojom/constants.mojom.h"
#include "services/audio/service_factory.h"
#include "services/catalog/manifest_provider.h"
......@@ -405,11 +405,11 @@ ServiceManagerContext::ServiceManagerContext(
DCHECK(service_manager_thread_task_runner_);
service_manager::mojom::ServiceRequest packaged_services_request;
if (service_manager::ServiceManagerIsRemote()) {
auto invitation =
mojo::edk::IncomingBrokerClientInvitation::AcceptFromCommandLine(
mojo::edk::TransportProtocol::kLegacy);
auto endpoint = mojo::PlatformChannel::RecoverPassedEndpointFromCommandLine(
*base::CommandLine::ForCurrentProcess());
auto invitation = mojo::IncomingInvitation::Accept(std::move(endpoint));
packaged_services_request =
service_manager::GetServiceRequestFromCommandLine(invitation.get());
service_manager::GetServiceRequestFromCommandLine(&invitation);
} else {
std::unique_ptr<BuiltinManifestProvider> manifest_provider =
std::make_unique<BuiltinManifestProvider>();
......
......@@ -12,6 +12,7 @@ source_set("standalone_service") {
deps = [
"//mojo/edk",
"//mojo/public/cpp/platform",
"//mojo/public/cpp/system",
"//services/service_manager/public/cpp",
"//services/service_manager/runner:init",
......
......@@ -12,9 +12,11 @@
#include "base/synchronization/waitable_event.h"
#include "base/task_scheduler/task_scheduler.h"
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "mojo/edk/embedder/embedder.h"
#include "mojo/edk/embedder/incoming_broker_client_invitation.h"
#include "mojo/edk/embedder/scoped_ipc_support.h"
#include "mojo/public/cpp/platform/platform_channel.h"
#include "mojo/public/cpp/system/invitation.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "services/service_manager/public/cpp/service_context.h"
#include "services/service_manager/runner/common/client_util.h"
......@@ -42,9 +44,9 @@ void RunStandaloneService(const StandaloneServiceCallback& callback) {
MachBroker::SendTaskPortToParent();
#endif
#if defined(OS_LINUX)
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
#if defined(OS_LINUX)
if (command_line.HasSwitch(switches::kServiceSandboxType)) {
// Warm parts of base in the copy of base in the mojo runner.
base::RandUint64();
......@@ -73,10 +75,10 @@ void RunStandaloneService(const StandaloneServiceCallback& callback) {
io_thread.task_runner(),
mojo::edk::ScopedIPCSupport::ShutdownPolicy::CLEAN);
auto invitation =
mojo::edk::IncomingBrokerClientInvitation::AcceptFromCommandLine(
mojo::edk::TransportProtocol::kLegacy);
callback.Run(GetServiceRequestFromCommandLine(invitation.get()));
auto invitation = mojo::IncomingInvitation::Accept(
mojo::PlatformChannel::RecoverPassedEndpointFromCommandLine(
command_line));
callback.Run(GetServiceRequestFromCommandLine(&invitation));
}
} // namespace service_manager
......@@ -13,11 +13,10 @@ source_set("common") {
deps = [
"//base",
"//mojo/public/cpp/bindings",
"//mojo/public/cpp/system",
]
public_deps = [
"//mojo/edk",
"//mojo/public/cpp/system",
"//services/service_manager/public/mojom",
]
}
......@@ -7,32 +7,30 @@
#include <string>
#include "base/command_line.h"
#include "mojo/edk/embedder/embedder.h"
#include "mojo/edk/embedder/incoming_broker_client_invitation.h"
#include "mojo/edk/embedder/outgoing_broker_client_invitation.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "mojo/public/cpp/system/invitation.h"
#include "services/service_manager/runner/common/switches.h"
namespace service_manager {
mojom::ServicePtr PassServiceRequestOnCommandLine(
mojo::edk::OutgoingBrokerClientInvitation* invitation,
mojo::OutgoingInvitation* invitation,
base::CommandLine* command_line) {
mojom::ServicePtr client;
std::string token = mojo::edk::GenerateRandomToken();
client.Bind(mojom::ServicePtrInfo(invitation->AttachMessagePipe(token), 0));
command_line->AppendSwitchASCII(switches::kServicePipeToken, token);
auto pipe_name = base::NumberToString(base::RandUint64());
client.Bind(
mojom::ServicePtrInfo(invitation->AttachMessagePipe(pipe_name), 0));
command_line->AppendSwitchASCII(switches::kServicePipeToken, pipe_name);
return client;
}
mojom::ServiceRequest GetServiceRequestFromCommandLine(
mojo::edk::IncomingBrokerClientInvitation* invitation) {
std::string token =
mojo::IncomingInvitation* invitation) {
std::string pipe_name =
base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kServicePipeToken);
mojom::ServiceRequest request;
if (!token.empty())
request = mojom::ServiceRequest(invitation->ExtractMessagePipe(token));
return request;
return mojom::ServiceRequest(invitation->ExtractMessagePipe(pipe_name));
}
bool ServiceManagerIsRemote() {
......
......@@ -12,10 +12,8 @@ class CommandLine;
}
namespace mojo {
namespace edk {
class IncomingBrokerClientInvitation;
class OutgoingBrokerClientInvitation;
}
class IncomingInvitation;
class OutgoingInvitation;
}
namespace service_manager {
......@@ -25,14 +23,14 @@ namespace service_manager {
// |command_line|. The launched process may extract the corresponding
// ServiceRequest by calling GetServiceRequestFromCommandLine().
mojom::ServicePtr PassServiceRequestOnCommandLine(
mojo::edk::OutgoingBrokerClientInvitation* invitation,
mojo::OutgoingInvitation* invitation,
base::CommandLine* command_line);
// Extracts a ServiceRequest from the command line of the current process.
// The parent of this process should have passed a request using
// PassServiceRequestOnCommandLine().
mojom::ServiceRequest GetServiceRequestFromCommandLine(
mojo::edk::IncomingBrokerClientInvitation* invitation);
mojo::IncomingInvitation* invitation);
// Returns true if the ServiceRequest came via the command line from a service
// manager
......
......@@ -35,7 +35,7 @@ source_set("lib") {
public_deps = [
"//base",
"//mojo/edk",
"//mojo/public/cpp/platform",
"//mojo/public/cpp/system",
]
......@@ -61,7 +61,6 @@ if (!is_ios) {
":lib",
"//base",
"//base/test:test_support",
"//mojo/edk",
"//services/service_manager",
"//services/service_manager/runner:init",
"//services/service_manager/runner/common",
......
......@@ -18,7 +18,6 @@
#include "base/task_scheduler/post_task.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "mojo/edk/embedder/embedder.h"
#include "mojo/public/cpp/bindings/interface_ptr_info.h"
#include "mojo/public/cpp/system/core.h"
#include "services/service_manager/public/cpp/standalone_service/switches.h"
......@@ -82,12 +81,12 @@ mojom::ServicePtr ServiceProcessLauncher::Start(const Identity& target,
switches::kServiceSandboxType,
StringFromUtilitySandboxType(sandbox_type_));
}
mojo_ipc_channel_.reset(new mojo::edk::PlatformChannelPair);
mojo_ipc_channel_->PrepareToPassClientHandleToChildProcess(
child_command_line.get(), &handle_passing_info_);
channel_.emplace();
channel_->PrepareToPassRemoteEndpoint(&handle_passing_info_,
child_command_line.get());
mojom::ServicePtr client = PassServiceRequestOnCommandLine(
&broker_client_invitation_, child_command_line.get());
mojom::ServicePtr client =
PassServiceRequestOnCommandLine(&invitation_, child_command_line.get());
base::PostTaskWithTraitsAndReply(
FROM_HERE, {base::TaskPriority::USER_BLOCKING, base::MayBlock()},
......@@ -103,9 +102,9 @@ void ServiceProcessLauncher::Join() {
// TODO: This code runs on the IO thread where Wait() is not allowed. This
// needs to be fixed: https://crbug.com/844078.
base::ScopedAllowBaseSyncPrimitivesOutsideBlockingScope allow_sync;
if (mojo_ipc_channel_)
if (channel_)
start_child_process_event_.Wait();
mojo_ipc_channel_.reset();
channel_.reset();
if (child_process_.IsValid()) {
int rv = -1;
LOG_IF(ERROR, !child_process_.WaitForExit(&rv))
......@@ -119,7 +118,7 @@ void ServiceProcessLauncher::DidStart(ProcessReadyCallback callback) {
std::move(callback).Run(child_process_.Pid());
} else {
LOG(ERROR) << "Failed to start child process";
mojo_ipc_channel_.reset();
channel_.reset();
std::move(callback).Run(base::kNullProcessId);
}
}
......@@ -202,12 +201,11 @@ void ServiceProcessLauncher::DoLaunch(
<< ", instance=" << target_.instance() << ", name=" << target_.name()
<< ", user_id=" << target_.user_id();
if (mojo_ipc_channel_.get()) {
mojo_ipc_channel_->ChildProcessLaunched();
broker_client_invitation_.Send(
child_process_.Handle(),
mojo::edk::ConnectionParams(mojo::edk::TransportProtocol::kLegacy,
mojo_ipc_channel_->PassServerHandle()));
if (channel_) {
channel_->RemoteProcessLaunched();
mojo::OutgoingInvitation::Send(std::move(invitation_),
child_process_.Handle(),
channel_->TakeLocalEndpoint());
}
}
start_child_process_event_.Signal();
......
......@@ -12,10 +12,11 @@
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/optional.h"
#include "base/process/process.h"
#include "base/synchronization/waitable_event.h"
#include "mojo/edk/embedder/outgoing_broker_client_invitation.h"
#include "mojo/edk/embedder/platform_channel_pair.h"
#include "mojo/public/cpp/platform/platform_channel.h"
#include "mojo/public/cpp/system/invitation.h"
#include "services/service_manager/public/mojom/service_factory.mojom.h"
#include "services/service_manager/runner/host/service_process_launcher_delegate.h"
#include "services/service_manager/sandbox/sandbox_type.h"
......@@ -68,9 +69,9 @@ class ServiceProcessLauncher {
base::Process child_process_;
// Used to initialize the Mojo IPC channel between parent and child.
std::unique_ptr<mojo::edk::PlatformChannelPair> mojo_ipc_channel_;
mojo::edk::HandlePassingInformation handle_passing_info_;
mojo::edk::OutgoingBrokerClientInvitation broker_client_invitation_;
base::Optional<mojo::PlatformChannel> channel_;
mojo::PlatformChannel::HandlePassingInfo handle_passing_info_;
mojo::OutgoingInvitation invitation_;
// Since Start() calls a method on another thread, we use an event to block
// the main thread if it tries to destruct |this| while launching the process.
......
......@@ -15,7 +15,6 @@ source_set("standalone") {
deps = [
"//base",
"//base/third_party/dynamic_annotations",
"//mojo/edk",
"//mojo/public/cpp/system",
"//services/catalog:lib",
"//services/service_manager",
......
......@@ -26,7 +26,6 @@ service_test("service_manager_unittests") {
":interfaces",
"//base",
"//base/test:test_support",
"//mojo/edk",
"//mojo/public/cpp/bindings",
"//mojo/public/cpp/system",
"//services/catalog:lib",
......@@ -62,7 +61,8 @@ source_set("util") {
deps = [
"//base",
"//base:base_static",
"//mojo/edk",
"//mojo/public/cpp/platform",
"//mojo/public/cpp/system",
"//services/service_manager/public/cpp",
"//services/service_manager/public/mojom",
"//services/service_manager/runner/common",
......
......@@ -18,7 +18,7 @@ source_set("service_manager") {
":interfaces",
"//base",
"//base/test:test_config",
"//mojo/edk",
"//mojo/public/cpp/platform",
"//mojo/public/cpp/system",
"//services/service_manager/public/cpp",
"//services/service_manager/public/cpp:service_test_support",
......@@ -105,7 +105,6 @@ service("service_manager_unittest_embedder") {
":interfaces",
"//base",
"//build/win:default_exe_manifest",
"//mojo/edk",
"//services/service_manager/public/cpp",
"//services/service_manager/public/cpp/standalone_service:main",
"//services/service_manager/public/mojom",
......
......@@ -22,11 +22,10 @@
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "build/build_config.h"
#include "mojo/edk/embedder/embedder.h"
#include "mojo/edk/embedder/outgoing_broker_client_invitation.h"
#include "mojo/edk/embedder/platform_channel_pair.h"
#include "mojo/edk/embedder/scoped_platform_handle.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "mojo/public/cpp/platform/platform_channel.h"
#include "mojo/public/cpp/platform/platform_handle.h"
#include "mojo/public/cpp/system/invitation.h"
#include "services/service_manager/public/cpp/binder_registry.h"
#include "services/service_manager/public/cpp/service.h"
#include "services/service_manager/public/cpp/service_context.h"
......@@ -264,20 +263,20 @@ class ServiceManagerTest : public test::ServiceTest,
// Create the channel to be shared with the target process. Pass one end
// on the command line.
mojo::edk::PlatformChannelPair platform_channel_pair;
mojo::PlatformChannel channel;
base::LaunchOptions options;
#if defined(OS_WIN)
platform_channel_pair.PrepareToPassClientHandleToChildProcess(
&child_command_line, &options.handles_to_inherit);
channel.PrepareToPassRemoteEndpoint(&options.handles_to_inherit,
&child_command_line);
#elif defined(OS_FUCHSIA)
platform_channel_pair.PrepareToPassClientHandleToChildProcess(
&child_command_line, &options.handles_to_transfer);
channel.PrepareToPassRemoteEndpoint(&options.handles_to_transfer,
&child_command_line);
#else
platform_channel_pair.PrepareToPassClientHandleToChildProcess(
&child_command_line, &options.fds_to_remap);
channel.PrepareToPassRemoteEndpoint(&options.fds_to_remap,
&child_command_line);
#endif
mojo::edk::OutgoingBrokerClientInvitation invitation;
mojo::OutgoingInvitation invitation;
service_manager::mojom::ServicePtr client =
service_manager::PassServiceRequestOnCommandLine(&invitation,
&child_command_line);
......@@ -293,12 +292,10 @@ class ServiceManagerTest : public test::ServiceTest,
target_ = base::LaunchProcess(child_command_line, options);
DCHECK(target_.IsValid());
platform_channel_pair.ChildProcessLaunched();
channel.RemoteProcessLaunched();
receiver->SetPID(target_.Pid());
invitation.Send(
target_.Handle(),
mojo::edk::ConnectionParams(mojo::edk::TransportProtocol::kLegacy,
platform_channel_pair.PassServerHandle()));
mojo::OutgoingInvitation::Send(std::move(invitation), target_.Handle(),
channel.TakeLocalEndpoint());
}
void StartEmbedderService() {
......
......@@ -12,12 +12,12 @@
#include "base/macros.h"
#include "base/path_service.h"
#include "base/process/process.h"
#include "base/rand_util.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "build/build_config.h"
#include "mojo/edk/embedder/embedder.h"
#include "mojo/edk/embedder/outgoing_broker_client_invitation.h"
#include "mojo/edk/embedder/platform_channel_pair.h"
#include "mojo/edk/embedder/scoped_platform_handle.h"
#include "mojo/public/cpp/platform/platform_channel.h"
#include "mojo/public/cpp/system/invitation.h"
#include "services/service_manager/public/cpp/connector.h"
#include "services/service_manager/public/mojom/connector.mojom.h"
#include "services/service_manager/public/mojom/service_factory.mojom.h"
......@@ -57,15 +57,15 @@ mojom::ConnectResult LaunchAndConnectToProcess(
// Create the channel to be shared with the target process. Pass one end
// on the command line.
mojo::edk::PlatformChannelPair platform_channel_pair;
mojo::edk::HandlePassingInformation handle_passing_info;
platform_channel_pair.PrepareToPassClientHandleToChildProcess(
&child_command_line, &handle_passing_info);
mojo::PlatformChannel channel;
mojo::PlatformChannel::HandlePassingInfo handle_passing_info;
channel.PrepareToPassRemoteEndpoint(&handle_passing_info,
&child_command_line);
mojo::edk::OutgoingBrokerClientInvitation invitation;
std::string token = mojo::edk::GenerateRandomToken();
mojo::ScopedMessagePipeHandle pipe = invitation.AttachMessagePipe(token);
child_command_line.AppendSwitchASCII(switches::kServicePipeToken, token);
mojo::OutgoingInvitation invitation;
auto pipe_name = base::NumberToString(base::RandUint64());
mojo::ScopedMessagePipeHandle pipe = invitation.AttachMessagePipe(pipe_name);
child_command_line.AppendSwitchASCII(switches::kServicePipeToken, pipe_name);
service_manager::mojom::ServicePtr client;
client.Bind(mojo::InterfacePtrInfo<service_manager::mojom::Service>(
......@@ -86,18 +86,17 @@ mojom::ConnectResult LaunchAndConnectToProcess(
#if defined(OS_WIN)
options.handles_to_inherit = handle_passing_info;
#elif defined(OS_FUCHSIA)
options.handles_to_transfer = handle_passing_info;
#elif defined(OS_POSIX)
options.fds_to_remap = handle_passing_info;
#endif
*process = base::LaunchProcess(child_command_line, options);
DCHECK(process->IsValid());
platform_channel_pair.ChildProcessLaunched();
channel.RemoteProcessLaunched();
receiver->SetPID(process->Pid());
invitation.Send(
process->Handle(),
mojo::edk::ConnectionParams(mojo::edk::TransportProtocol::kLegacy,
platform_channel_pair.PassServerHandle()));
mojo::OutgoingInvitation::Send(std::move(invitation), process->Handle(),
channel.TakeLocalEndpoint());
return result;
}
......
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