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

Migrate ash to ServiceBinding

Replaces usage of the deprecated ServiceContext with ServiceBinding.

Also adds support for a C++ ServiceMain which is passed a proper
ServiceRequest instead of a raw Mojo handle. This makes writing
ServiceMain slightly less awkward.

Also cleans up service.gni using forward_variables_from, which was
unsupported when it was originally written.

Finally, removes the unused RegisterInProcessServices method from
BrowserProcessPlatformPart/Base.

Bug: 891780
Change-Id: I99734414586b6b3a355fa22afa85930249f8e62b
Reviewed-on: https://chromium-review.googlesource.com/c/1355336
Commit-Queue: Ken Rockot <rockot@google.com>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#612503}
parent bc585ae9
...@@ -2307,6 +2307,7 @@ static_library("interactive_ui_test_support") { ...@@ -2307,6 +2307,7 @@ static_library("interactive_ui_test_support") {
service("ash_service") { service("ash_service") {
output_name = "ash" output_name = "ash"
use_cpp_main = true
sources = [ sources = [
"main.cc", "main.cc",
......
...@@ -47,10 +47,6 @@ ...@@ -47,10 +47,6 @@
namespace ash { namespace ash {
namespace { namespace {
std::unique_ptr<service_manager::Service> CreateAshService() {
return std::make_unique<AshService>();
}
class AshViewsDelegate : public views::ViewsDelegate { class AshViewsDelegate : public views::ViewsDelegate {
public: public:
AshViewsDelegate() = default; AshViewsDelegate() = default;
...@@ -71,7 +67,8 @@ class AshViewsDelegate : public views::ViewsDelegate { ...@@ -71,7 +67,8 @@ class AshViewsDelegate : public views::ViewsDelegate {
} // namespace } // namespace
AshService::AshService() = default; AshService::AshService(service_manager::mojom::ServiceRequest request)
: service_binding_(this, std::move(request)) {}
AshService::~AshService() { AshService::~AshService() {
if (!base::FeatureList::IsEnabled(features::kMash)) if (!base::FeatureList::IsEnabled(features::kMash))
...@@ -104,14 +101,6 @@ AshService::~AshService() { ...@@ -104,14 +101,6 @@ AshService::~AshService() {
gpu_host_.reset(); gpu_host_.reset();
} }
// static
service_manager::EmbeddedServiceInfo AshService::CreateEmbeddedServiceInfo() {
service_manager::EmbeddedServiceInfo info;
info.factory = base::BindRepeating(&CreateAshService);
info.task_runner = base::ThreadTaskRunnerHandle::Get();
return info;
}
void AshService::InitForMash() { void AshService::InitForMash() {
wm_state_ = std::make_unique<::wm::WMState>(); wm_state_ = std::make_unique<::wm::WMState>();
...@@ -119,7 +108,8 @@ void AshService::InitForMash() { ...@@ -119,7 +108,8 @@ void AshService::InitForMash() {
std::make_unique<discardable_memory::DiscardableSharedMemoryManager>(); std::make_unique<discardable_memory::DiscardableSharedMemoryManager>();
gpu_host_ = std::make_unique<ws::gpu_host::GpuHost>( gpu_host_ = std::make_unique<ws::gpu_host::GpuHost>(
this, context()->connector(), discardable_shared_memory_manager_.get()); this, service_binding_.GetConnector(),
discardable_shared_memory_manager_.get());
host_frame_sink_manager_ = std::make_unique<viz::HostFrameSinkManager>(); host_frame_sink_manager_ = std::make_unique<viz::HostFrameSinkManager>();
CreateFrameSinkManager(); CreateFrameSinkManager();
...@@ -127,13 +117,13 @@ void AshService::InitForMash() { ...@@ -127,13 +117,13 @@ void AshService::InitForMash() {
base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0); base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0);
thread_options.priority = base::ThreadPriority::NORMAL; thread_options.priority = base::ThreadPriority::NORMAL;
CHECK(io_thread_->StartWithOptions(thread_options)); CHECK(io_thread_->StartWithOptions(thread_options));
gpu_ = ws::Gpu::Create(context()->connector(), ws::mojom::kServiceName, gpu_ = ws::Gpu::Create(service_binding_.GetConnector(),
io_thread_->task_runner()); ws::mojom::kServiceName, io_thread_->task_runner());
context_factory_ = std::make_unique<ws::HostContextFactory>( context_factory_ = std::make_unique<ws::HostContextFactory>(
gpu_.get(), host_frame_sink_manager_.get()); gpu_.get(), host_frame_sink_manager_.get());
env_ = aura::Env::CreateInstanceToHostViz(context()->connector()); env_ = aura::Env::CreateInstanceToHostViz(service_binding_.GetConnector());
views_delegate_ = std::make_unique<AshViewsDelegate>(); views_delegate_ = std::make_unique<AshViewsDelegate>();
...@@ -173,7 +163,7 @@ void AshService::InitForMash() { ...@@ -173,7 +163,7 @@ void AshService::InitForMash() {
shell_init_params.context_factory = context_factory_.get(); shell_init_params.context_factory = context_factory_.get();
shell_init_params.context_factory_private = shell_init_params.context_factory_private =
context_factory_->GetContextFactoryPrivate(); context_factory_->GetContextFactoryPrivate();
shell_init_params.connector = context()->connector(); shell_init_params.connector = service_binding_.GetConnector();
shell_init_params.gpu_interface_provider = shell_init_params.gpu_interface_provider =
std::make_unique<AshGpuInterfaceProvider>( std::make_unique<AshGpuInterfaceProvider>(
gpu_host_.get(), discardable_shared_memory_manager_.get()); gpu_host_.get(), discardable_shared_memory_manager_.get());
......
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
#include "mojo/public/cpp/bindings/binding_set.h" #include "mojo/public/cpp/bindings/binding_set.h"
#include "services/service_manager/public/cpp/binder_registry.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.h"
#include "services/service_manager/public/cpp/service_binding.h"
#include "services/service_manager/public/mojom/service.mojom.h"
#include "services/service_manager/public/mojom/service_factory.mojom.h" #include "services/service_manager/public/mojom/service_factory.mojom.h"
#include "services/ws/gpu_host/gpu_host_delegate.h" #include "services/ws/gpu_host/gpu_host_delegate.h"
#include "services/ws/public/mojom/gpu.mojom.h" #include "services/ws/public/mojom/gpu.mojom.h"
...@@ -32,10 +34,6 @@ namespace discardable_memory { ...@@ -32,10 +34,6 @@ namespace discardable_memory {
class DiscardableSharedMemoryManager; class DiscardableSharedMemoryManager;
} }
namespace service_manager {
struct EmbeddedServiceInfo;
}
namespace views { namespace views {
class ViewsDelegate; class ViewsDelegate;
} }
...@@ -68,12 +66,9 @@ class ASH_EXPORT AshService : public service_manager::Service, ...@@ -68,12 +66,9 @@ class ASH_EXPORT AshService : public service_manager::Service,
public service_manager::mojom::ServiceFactory, public service_manager::mojom::ServiceFactory,
public ws::gpu_host::GpuHostDelegate { public ws::gpu_host::GpuHostDelegate {
public: public:
AshService(); explicit AshService(service_manager::mojom::ServiceRequest request);
~AshService() override; ~AshService() override;
// Returns an appropriate EmbeddedServiceInfo that creates AshService.
static service_manager::EmbeddedServiceInfo CreateEmbeddedServiceInfo();
// service_manager::Service: // service_manager::Service:
void OnStart() override; void OnStart() override;
void OnBindInterface(const service_manager::BindSourceInfo& remote_info, void OnBindInterface(const service_manager::BindSourceInfo& remote_info,
...@@ -99,6 +94,7 @@ class ASH_EXPORT AshService : public service_manager::Service, ...@@ -99,6 +94,7 @@ class ASH_EXPORT AshService : public service_manager::Service,
// ui::ws::GpuHostDelegate: // ui::ws::GpuHostDelegate:
void OnGpuServiceInitialized() override; void OnGpuServiceInitialized() override;
service_manager::ServiceBinding service_binding_;
service_manager::BinderRegistry registry_; service_manager::BinderRegistry registry_;
mojo::BindingSet<service_manager::mojom::ServiceFactory> mojo::BindingSet<service_manager::mojom::ServiceFactory>
service_factory_bindings_; service_factory_bindings_;
......
...@@ -2,20 +2,20 @@ ...@@ -2,20 +2,20 @@
// 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/c/main.h"
#include "ash/ash_service.h" #include "ash/ash_service.h"
#include "base/feature_list.h" #include "base/feature_list.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "services/service_manager/public/cpp/service_runner.h" #include "base/run_loop.h"
#include "ui/base/material_design/material_design_controller.h" #include "ui/base/material_design/material_design_controller.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_features.h" #include "ui/base/ui_base_features.h"
// This path is only hit in testing, not production. Production launches ash by // This path is only hit in testing, not production. Production launches ash by
// way of the utility process, which does not use this. // way of the utility process, which does not use this.
MojoResult ServiceMain(MojoHandle service_request_handle) { void ServiceMain(service_manager::mojom::ServiceRequest request) {
logging::SetLogPrefix("ash"); logging::SetLogPrefix("ash");
// Load ash resources and strings. // Load ash resources and strings.
// TODO: investigate nuking ash_service_resources and use the same resources // TODO: investigate nuking ash_service_resources and use the same resources
...@@ -54,7 +54,6 @@ MojoResult ServiceMain(MojoHandle service_request_handle) { ...@@ -54,7 +54,6 @@ MojoResult ServiceMain(MojoHandle service_request_handle) {
ui::MaterialDesignController::Initialize(); ui::MaterialDesignController::Initialize();
service_manager::ServiceRunner runner(new ash::AshService); base::MessageLoop message_loop(base::MessageLoop::TYPE_UI);
runner.set_message_loop_type(base::MessageLoop::TYPE_UI); ash::AshService(std::move(request)).RunUntilTermination();
return runner.Run(service_request_handle);
} }
...@@ -101,11 +101,11 @@ void ShellContentBrowserClient::RegisterOutOfProcessServices( ...@@ -101,11 +101,11 @@ void ShellContentBrowserClient::RegisterOutOfProcessServices(
&base::ASCIIToUTF16, test_ime_driver::mojom::kServiceName); &base::ASCIIToUTF16, test_ime_driver::mojom::kServiceName);
} }
void ShellContentBrowserClient::RegisterInProcessServices( void ShellContentBrowserClient::HandleServiceRequest(
StaticServiceMap* services, const std::string& service_name,
content::ServiceManagerConnection* connection) { service_manager::mojom::ServiceRequest request) {
services->insert(std::make_pair(mojom::kServiceName, service_manager::Service::RunAsyncUntilTermination(
AshService::CreateEmbeddedServiceInfo())); std::make_unique<AshService>(std::move(request)));
} }
} // namespace shell } // namespace shell
......
...@@ -36,9 +36,9 @@ class ShellContentBrowserClient : public content::ContentBrowserClient { ...@@ -36,9 +36,9 @@ class ShellContentBrowserClient : public content::ContentBrowserClient {
base::StringPiece name) override; base::StringPiece name) override;
std::vector<ServiceManifestInfo> GetExtraServiceManifests() override; std::vector<ServiceManifestInfo> GetExtraServiceManifests() override;
void RegisterOutOfProcessServices(OutOfProcessServiceMap* services) override; void RegisterOutOfProcessServices(OutOfProcessServiceMap* services) override;
void RegisterInProcessServices( void HandleServiceRequest(
StaticServiceMap* services, const std::string& service_name,
content::ServiceManagerConnection* connection) override; service_manager::mojom::ServiceRequest request) override;
private: private:
ShellBrowserMainParts* shell_browser_main_parts_; ShellBrowserMainParts* shell_browser_main_parts_;
......
...@@ -94,12 +94,15 @@ void RegisterInProcessServices( ...@@ -94,12 +94,15 @@ void RegisterInProcessServices(
}); });
(*services)[ax::mojom::kAXHostServiceName] = info; (*services)[ax::mojom::kAXHostServiceName] = info;
} }
}
if (features::IsMultiProcessMash()) void HandleServiceRequest(const std::string& service_name,
return; service_manager::mojom::ServiceRequest request) {
if (!features::IsMultiProcessMash() &&
(*services)[ash::mojom::kServiceName] = service_name == ash::mojom::kServiceName) {
ash::AshService::CreateEmbeddedServiceInfo(); service_manager::Service::RunAsyncUntilTermination(
std::make_unique<ash::AshService>(std::move(request)));
}
} }
bool IsAshRelatedServiceName(const std::string& name) { bool IsAshRelatedServiceName(const std::string& name) {
......
...@@ -20,6 +20,11 @@ void RegisterInProcessServices( ...@@ -20,6 +20,11 @@ void RegisterInProcessServices(
content::ContentBrowserClient::StaticServiceMap* services, content::ContentBrowserClient::StaticServiceMap* services,
content::ServiceManagerConnection* connection); content::ServiceManagerConnection* connection);
// Handles an incoming ServiceRequest. Newly added in-process services should be
// handled here instead of being registered by RegisterInProcessServices.
void HandleServiceRequest(const std::string& service_name,
service_manager::mojom::ServiceRequest request);
// Returns true if |name| identifies an Ash related service. // Returns true if |name| identifies an Ash related service.
bool IsAshRelatedServiceName(const std::string& name); bool IsAshRelatedServiceName(const std::string& name);
......
...@@ -41,7 +41,3 @@ std::unique_ptr<policy::ChromeBrowserPolicyConnector> ...@@ -41,7 +41,3 @@ std::unique_ptr<policy::ChromeBrowserPolicyConnector>
BrowserProcessPlatformPartBase::CreateBrowserPolicyConnector() { BrowserProcessPlatformPartBase::CreateBrowserPolicyConnector() {
return std::make_unique<policy::ChromeBrowserPolicyConnector>(); return std::make_unique<policy::ChromeBrowserPolicyConnector>();
} }
void BrowserProcessPlatformPartBase::RegisterInProcessServices(
content::ContentBrowserClient::StaticServiceMap* services,
content::ServiceManagerConnection* connection) {}
...@@ -42,11 +42,6 @@ class BrowserProcessPlatformPartBase { ...@@ -42,11 +42,6 @@ class BrowserProcessPlatformPartBase {
virtual std::unique_ptr<policy::ChromeBrowserPolicyConnector> virtual std::unique_ptr<policy::ChromeBrowserPolicyConnector>
CreateBrowserPolicyConnector(); CreateBrowserPolicyConnector();
// Called from ChromeContentBrowserClient::RegisterInProcessServices().
virtual void RegisterInProcessServices(
content::ContentBrowserClient::StaticServiceMap* services,
content::ServiceManagerConnection* connection);
private: private:
DISALLOW_COPY_AND_ASSIGN(BrowserProcessPlatformPartBase); DISALLOW_COPY_AND_ASSIGN(BrowserProcessPlatformPartBase);
}; };
......
...@@ -189,12 +189,6 @@ BrowserProcessPlatformPart::CreateBrowserPolicyConnector() { ...@@ -189,12 +189,6 @@ BrowserProcessPlatformPart::CreateBrowserPolicyConnector() {
new policy::BrowserPolicyConnectorChromeOS()); new policy::BrowserPolicyConnectorChromeOS());
} }
void BrowserProcessPlatformPart::RegisterInProcessServices(
content::ContentBrowserClient::StaticServiceMap* services,
content::ServiceManagerConnection* connection) {
ash_service_registry::RegisterInProcessServices(services, connection);
}
chromeos::system::SystemClock* BrowserProcessPlatformPart::GetSystemClock() { chromeos::system::SystemClock* BrowserProcessPlatformPart::GetSystemClock() {
if (!system_clock_.get()) if (!system_clock_.get())
system_clock_.reset(new chromeos::system::SystemClock()); system_clock_.reset(new chromeos::system::SystemClock());
......
...@@ -108,9 +108,6 @@ class BrowserProcessPlatformPart : public BrowserProcessPlatformPartBase { ...@@ -108,9 +108,6 @@ class BrowserProcessPlatformPart : public BrowserProcessPlatformPartBase {
void StartTearDown() override; void StartTearDown() override;
std::unique_ptr<policy::ChromeBrowserPolicyConnector> std::unique_ptr<policy::ChromeBrowserPolicyConnector>
CreateBrowserPolicyConnector() override; CreateBrowserPolicyConnector() override;
void RegisterInProcessServices(
content::ContentBrowserClient::StaticServiceMap* services,
content::ServiceManagerConnection* connection) override;
chromeos::system::SystemClock* GetSystemClock(); chromeos::system::SystemClock* GetSystemClock();
void DestroySystemClock(); void DestroySystemClock();
......
...@@ -3898,6 +3898,10 @@ void ChromeContentBrowserClient::HandleServiceRequest( ...@@ -3898,6 +3898,10 @@ void ChromeContentBrowserClient::HandleServiceRequest(
media::CreateMediaService(std::move(request))); media::CreateMediaService(std::move(request)));
} }
#endif #endif
#if defined(OS_CHROMEOS)
ash_service_registry::HandleServiceRequest(service_name, std::move(request));
#endif
} }
bool ChromeContentBrowserClient::ShouldTerminateOnServiceQuit( bool ChromeContentBrowserClient::ShouldTerminateOnServiceQuit(
......
...@@ -51,10 +51,11 @@ void RecordMashServiceLaunch(MashService service) { ...@@ -51,10 +51,11 @@ void RecordMashServiceLaunch(MashService service) {
UMA_HISTOGRAM_ENUMERATION("Launch.MashService", service); UMA_HISTOGRAM_ENUMERATION("Launch.MashService", service);
} }
std::unique_ptr<service_manager::Service> CreateAshService() { std::unique_ptr<service_manager::Service> CreateAshService(
service_manager::mojom::ServiceRequest request) {
RecordMashServiceLaunch(MashService::kAsh); RecordMashServiceLaunch(MashService::kAsh);
logging::SetLogPrefix("ash"); logging::SetLogPrefix("ash");
return std::make_unique<ash::AshService>(); return std::make_unique<ash::AshService>(std::move(request));
} }
std::unique_ptr<service_manager::Service> CreateQuickLaunchService( std::unique_ptr<service_manager::Service> CreateQuickLaunchService(
...@@ -86,7 +87,6 @@ MashServiceFactory::~MashServiceFactory() = default; ...@@ -86,7 +87,6 @@ MashServiceFactory::~MashServiceFactory() = default;
void MashServiceFactory::RegisterOutOfProcessServices( void MashServiceFactory::RegisterOutOfProcessServices(
content::ContentUtilityClient::StaticServiceMap* services) { content::ContentUtilityClient::StaticServiceMap* services) {
RegisterMashService(services, ash::mojom::kServiceName, &CreateAshService);
RegisterMashService(services, shortcut_viewer::mojom::kServiceName, RegisterMashService(services, shortcut_viewer::mojom::kServiceName,
&CreateShortcutViewerApp); &CreateShortcutViewerApp);
RegisterMashService(services, tap_visualizer::mojom::kServiceName, RegisterMashService(services, tap_visualizer::mojom::kServiceName,
...@@ -99,6 +99,8 @@ std::unique_ptr<service_manager::Service> ...@@ -99,6 +99,8 @@ std::unique_ptr<service_manager::Service>
MashServiceFactory::HandleServiceRequest( MashServiceFactory::HandleServiceRequest(
const std::string& service_name, const std::string& service_name,
service_manager::mojom::ServiceRequest request) { service_manager::mojom::ServiceRequest request) {
if (service_name == ash::mojom::kServiceName)
return CreateAshService(std::move(request));
if (service_name == quick_launch::mojom::kServiceName) if (service_name == quick_launch::mojom::kServiceName)
return CreateQuickLaunchService(std::move(request)); return CreateQuickLaunchService(std::move(request));
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "services/service_manager/public/cpp/service.h" #include "services/service_manager/public/cpp/service.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/run_loop.h"
#include "services/service_manager/public/cpp/service_context.h" #include "services/service_manager/public/cpp/service_context.h"
namespace service_manager { namespace service_manager {
...@@ -34,6 +35,12 @@ bool Service::OnServiceManagerConnectionLost() { ...@@ -34,6 +35,12 @@ bool Service::OnServiceManagerConnectionLost() {
return true; return true;
} }
void Service::RunUntilTermination() {
base::RunLoop loop;
set_termination_closure(loop.QuitClosure());
loop.Run();
}
void Service::Terminate() { void Service::Terminate() {
if (termination_closure_) if (termination_closure_)
std::move(termination_closure_).Run(); std::move(termination_closure_).Run();
......
...@@ -2,33 +2,14 @@ ...@@ -2,33 +2,14 @@
# 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.
import("//build/toolchain/toolchain.gni")
if (is_android) {
import("//build/config/android/rules.gni")
import("//build/config/zip.gni")
}
# Generates a Service binary. The parameters of this template are those of an # Generates a Service binary. The parameters of this template are those of an
# executable. # executable.
#
# TODO: We should consider removing this. At this point all it does is bring a
# single dep for an entry point, and change the executable extension. Both are
# of marginal value, and having another special target type may be unnecessarily
# confusing for developers when these are really just executables.
template("service") { template("service") {
base_target_name = target_name
if (defined(invoker.output_name)) {
base_target_name = invoker.output_name
}
service_deps = []
if (defined(invoker.deps)) {
service_deps += invoker.deps
}
service_data_deps =
[ "//services/service_manager/public/cpp/standalone_service:main" ]
if (defined(invoker.data_deps)) {
service_data_deps += invoker.data_deps
}
if (defined(invoker.resources)) { if (defined(invoker.resources)) {
# TODO(rockot): Remove this once all existing service targets have stopped # TODO(rockot): Remove this once all existing service targets have stopped
# setting |resources|. This target serves no purpose other than to ensure # setting |resources|. This target serves no purpose other than to ensure
...@@ -40,83 +21,32 @@ template("service") { ...@@ -40,83 +21,32 @@ template("service") {
} }
} }
if (is_win) {
executable_extension = "service.exe"
} else {
executable_extension = "service"
}
executable(target_name) { executable(target_name) {
output_name = base_target_name forward_variables_from(invoker, "*", [ "configs" ])
output_extension = executable_extension
if (defined(invoker.cflags)) {
cflags = invoker.cflags
}
if (defined(invoker.cflags_c)) {
cflags_c = invoker.cflags_c
}
if (defined(invoker.cflags_cc)) {
cflags_cc = invoker.cflags_cc
}
if (defined(invoker.cflags_objc)) {
cflags_objc = invoker.cflags_objc
}
if (defined(invoker.cflags_objcc)) {
cflags_objcc = invoker.cflags_objcc
}
if (defined(invoker.defines)) {
defines = invoker.defines
}
if (defined(invoker.include_dirs)) {
include_dirs = invoker.include_dirs
}
if (defined(invoker.ldflags)) {
ldflags = invoker.ldflags
}
if (defined(invoker.lib_dirs)) {
lib_dirs = invoker.lib_dirs
}
if (defined(invoker.libs)) {
libs = invoker.libs
}
data_deps = service_data_deps
deps = [ # Executable targets already have a default set of configs which we should
"//services/service_manager/public/cpp/standalone_service:main", # not overwrite. Hence we do not forward but manually append.
]
deps += service_deps
if (defined(invoker.public_deps)) {
public_deps = invoker.public_deps
}
if (defined(invoker.all_dependent_configs)) {
all_dependent_configs = invoker.all_dependent_configs
}
if (defined(invoker.public_configs)) {
public_configs = invoker.public_configs
}
if (defined(invoker.check_includes)) {
check_includes = invoker.check_includes
}
if (defined(invoker.configs)) { if (defined(invoker.configs)) {
configs += invoker.configs configs += invoker.configs
} }
if (defined(invoker.data)) {
data = invoker.data if (is_win) {
} output_extension = "service.exe"
if (defined(invoker.inputs)) { } else {
inputs = invoker.inputs output_extension = "service"
}
if (defined(invoker.public)) {
public = invoker.public
} }
if (defined(invoker.sources)) {
sources = invoker.sources if (!defined(deps)) {
deps = []
} }
if (defined(invoker.testonly)) {
testonly = invoker.testonly if (defined(use_cpp_main) && use_cpp_main) {
deps += [
"//services/service_manager/public/cpp/standalone_service:main_cpp",
]
} else {
deps +=
[ "//services/service_manager/public/cpp/standalone_service:main" ]
} }
} }
} }
...@@ -80,6 +80,11 @@ class COMPONENT_EXPORT(SERVICE_MANAGER_CPP) Service { ...@@ -80,6 +80,11 @@ class COMPONENT_EXPORT(SERVICE_MANAGER_CPP) Service {
// deprecated. // deprecated.
virtual bool OnServiceManagerConnectionLost(); virtual bool OnServiceManagerConnectionLost();
// Runs a RunLoop until this service self-terminates. This is intended for use
// in environments where the service is the only thing running, e.g. as a
// standalone executable.
void RunUntilTermination();
protected: protected:
// Subclasses should always invoke |Terminate()| when they want to // Subclasses should always invoke |Terminate()| when they want to
// self-terminate. This should generally only be done once the service is // self-terminate. This should generally only be done once the service is
......
...@@ -39,6 +39,11 @@ source_set("standalone_service") { ...@@ -39,6 +39,11 @@ source_set("standalone_service") {
# to the Service Manager. In order to link properly, dependents must ensure that # to the Service Manager. In order to link properly, dependents must ensure that
# they define a ServiceMain() symbol which matches the signature in # they define a ServiceMain() symbol which matches the signature in
# //services/service_manager/public/c/main.h. # //services/service_manager/public/c/main.h.
#
# NOTE: This target should be considered deprecated, as there is no longer a
# need for service entry points to use C linkage. service() targets
# automatically link against this, but can set |use_cpp_main| to |true| to link
# against the "main_cpp" target below instead.
source_set("main") { source_set("main") {
sources = [ sources = [
"main.cc", "main.cc",
...@@ -51,3 +56,22 @@ source_set("main") { ...@@ -51,3 +56,22 @@ source_set("main") {
"//services/service_manager/runner:init", "//services/service_manager/runner:init",
] ]
} }
# Same as above but uses a nicer signature for |ServiceMain()|, with a type-safe
# ServiceRequest handle instead of a raw C MojoHandle.
source_set("main_cpp") {
public = [
"service_main.h",
]
sources = [
"main_cpp.cc",
]
deps = [
":standalone_service",
"//base",
"//base:i18n",
"//services/service_manager/runner:init",
]
}
...@@ -61,7 +61,7 @@ int main(int argc, char** argv) { ...@@ -61,7 +61,7 @@ int main(int argc, char** argv) {
command_line->GetSwitchValueASCII(switches::kDisableFeatures)); command_line->GetSwitchValueASCII(switches::kDisableFeatures));
service_manager::WaitForDebuggerIfNecessary(); service_manager::WaitForDebuggerIfNecessary();
service_manager::RunStandaloneService(base::Bind(&RunServiceMain)); service_manager::RunStandaloneService(base::BindOnce(&RunServiceMain));
base::TaskScheduler::GetInstance()->Shutdown(); base::TaskScheduler::GetInstance()->Shutdown();
......
// Copyright 2018 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.
#include "base/at_exit.h"
#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/debug/stack_trace.h"
#include "base/feature_list.h"
#include "base/i18n/icu_util.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/process/launch.h"
#include "base/task/task_scheduler/task_scheduler.h"
#include "build/build_config.h"
#include "services/service_manager/public/cpp/standalone_service/service_main.h"
#include "services/service_manager/public/cpp/standalone_service/standalone_service.h"
#include "services/service_manager/public/cpp/standalone_service/switches.h"
#include "services/service_manager/public/mojom/service.mojom.h"
#include "services/service_manager/runner/init.h"
#if defined(OS_MACOSX)
#include "base/mac/bundle_locations.h"
#endif
int main(int argc, char** argv) {
base::AtExitManager at_exit;
base::CommandLine::Init(argc, argv);
#if !defined(OFFICIAL_BUILD) && defined(OS_WIN)
base::RouteStdioToConsole(false);
#endif
service_manager::InitializeLogging();
base::i18n::InitializeICU();
#if !defined(OFFICIAL_BUILD)
// Initialize stack dumping before initializing sandbox to make sure symbol
// names in all loaded libraries will be cached.
base::debug::EnableInProcessStackDumping();
#endif
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
base::FeatureList::InitializeInstance(
command_line->GetSwitchValueASCII(switches::kEnableFeatures),
command_line->GetSwitchValueASCII(switches::kDisableFeatures));
service_manager::WaitForDebuggerIfNecessary();
service_manager::RunStandaloneService(base::BindOnce(&ServiceMain));
base::TaskScheduler::GetInstance()->Shutdown();
return 0;
}
// Copyright 2018 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_STANDALONE_SERVICE_SERVICE_MAIN_H_
#define SERVICES_SERVICE_MANAGER_PUBLIC_CPP_STANDALONE_SERVICE_SERVICE_MAIN_H_
#include "services/service_manager/public/mojom/service.mojom.h"
// Service executables linking against the
// "//services/service_manager/public/cpp/standalone_service:main_cpp" target
// must implement this function as their entry point.
void ServiceMain(service_manager::mojom::ServiceRequest request);
#endif // SERVICES_SERVICE_MANAGER_PUBLIC_CPP_STANDALONE_SERVICE_SERVICE_MAIN_H_
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
namespace service_manager { namespace service_manager {
void RunStandaloneService(const StandaloneServiceCallback& callback) { void RunStandaloneService(StandaloneServiceCallback callback) {
DCHECK(!base::MessageLoopCurrent::Get()); DCHECK(!base::MessageLoopCurrent::Get());
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
...@@ -78,7 +78,7 @@ void RunStandaloneService(const StandaloneServiceCallback& callback) { ...@@ -78,7 +78,7 @@ void RunStandaloneService(const StandaloneServiceCallback& callback) {
auto invitation = mojo::IncomingInvitation::Accept( auto invitation = mojo::IncomingInvitation::Accept(
mojo::PlatformChannel::RecoverPassedEndpointFromCommandLine( mojo::PlatformChannel::RecoverPassedEndpointFromCommandLine(
command_line)); command_line));
callback.Run(GetServiceRequestFromCommandLine(&invitation)); std::move(callback).Run(GetServiceRequestFromCommandLine(&invitation));
} }
} // namespace service_manager } // namespace service_manager
...@@ -10,7 +10,8 @@ ...@@ -10,7 +10,8 @@
namespace service_manager { namespace service_manager {
using StandaloneServiceCallback = base::Callback<void(mojom::ServiceRequest)>; using StandaloneServiceCallback =
base::OnceCallback<void(mojom::ServiceRequest)>;
// Runs a standalone service in the current process. This takes care of setting // Runs a standalone service in the current process. This takes care of setting
// up a boilerplate environment, including initializing //base objects, Mojo // up a boilerplate environment, including initializing //base objects, Mojo
...@@ -23,7 +24,7 @@ using StandaloneServiceCallback = base::Callback<void(mojom::ServiceRequest)>; ...@@ -23,7 +24,7 @@ using StandaloneServiceCallback = base::Callback<void(mojom::ServiceRequest)>;
// NOTE: A typical service should also link against the main() defined in // NOTE: A typical service should also link against the main() defined in
// main.cc (next to this header) and thus have no need to call this function // main.cc (next to this header) and thus have no need to call this function
// directly. // directly.
void RunStandaloneService(const StandaloneServiceCallback& callback); void RunStandaloneService(StandaloneServiceCallback callback);
} // 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