blob: 734fdebd587cd6bff7c43b0644918fc0daa12f29 [file] [log] [blame]
// Copyright 2015 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 "content/utility/utility_service_factory.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "content/child/child_process.h"
#include "content/network/network_service.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/service_names.mojom.h"
#include "content/public/utility/content_utility_client.h"
#include "content/public/utility/utility_thread.h"
#include "content/utility/utility_thread_impl.h"
#include "services/data_decoder/data_decoder_service.h"
#include "services/data_decoder/public/interfaces/constants.mojom.h"
#include "services/shape_detection/public/interfaces/constants.mojom.h"
#include "services/shape_detection/shape_detection_service.h"
#if defined(ENABLE_MOJO_MEDIA_IN_UTILITY_PROCESS)
#include "media/mojo/services/media_service_factory.h" // nogncheck
#endif
namespace content {
namespace {
std::unique_ptr<service_manager::Service> CreateDataDecoderService() {
content::UtilityThread::Get()->EnsureBlinkInitialized();
return data_decoder::DataDecoderService::Create();
}
} // namespace
UtilityServiceFactory::UtilityServiceFactory()
: network_registry_(base::MakeUnique<service_manager::BinderRegistry>()) {}
UtilityServiceFactory::~UtilityServiceFactory() {}
void UtilityServiceFactory::RegisterServices(ServiceMap* services) {
GetContentClient()->utility()->RegisterServices(services);
#if defined(ENABLE_MOJO_MEDIA_IN_UTILITY_PROCESS)
ServiceInfo info;
info.factory = base::Bind(&media::CreateMediaService);
services->insert(std::make_pair("media", info));
#endif
ServiceInfo shape_detection_info;
shape_detection_info.factory =
base::Bind(&shape_detection::ShapeDetectionService::Create);
services->insert(std::make_pair(shape_detection::mojom::kServiceName,
shape_detection_info));
ServiceInfo data_decoder_info;
data_decoder_info.factory = base::Bind(&CreateDataDecoderService);
services->insert(
std::make_pair(data_decoder::mojom::kServiceName, data_decoder_info));
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableNetworkService)) {
GetContentClient()->utility()->RegisterNetworkBinders(
network_registry_.get());
ServiceInfo network_info;
network_info.factory = base::Bind(
&UtilityServiceFactory::CreateNetworkService, base::Unretained(this));
network_info.task_runner = ChildProcess::current()->io_task_runner();
services->insert(
std::make_pair(content::mojom::kNetworkServiceName, network_info));
}
}
void UtilityServiceFactory::OnServiceQuit() {
UtilityThread::Get()->ReleaseProcessIfNeeded();
}
void UtilityServiceFactory::OnLoadFailed() {
UtilityThreadImpl* utility_thread =
static_cast<UtilityThreadImpl*>(UtilityThread::Get());
utility_thread->Shutdown();
utility_thread->ReleaseProcessIfNeeded();
}
std::unique_ptr<service_manager::Service>
UtilityServiceFactory::CreateNetworkService() {
return base::MakeUnique<NetworkService>(std::move(network_registry_));
}
} // namespace content