2014-07-03 20:34:58 +02:00
|
|
|
// Copyright (c) 2014 the Chromium Embedded Framework authors.
|
|
|
|
// Portions Copyright (c) 2012 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 "libcef/utility/content_utility_client.h"
|
|
|
|
|
2016-01-06 20:20:54 +01:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "build/build_config.h"
|
2018-02-15 01:12:09 +01:00
|
|
|
#include "chrome/services/printing/printing_service.h"
|
2018-03-20 21:15:08 +01:00
|
|
|
#include "chrome/services/printing/public/mojom/constants.mojom.h"
|
2018-06-01 21:16:26 +02:00
|
|
|
#include "components/services/pdf_compositor/public/cpp/pdf_compositor_service_factory.h"
|
|
|
|
#include "components/services/pdf_compositor/public/interfaces/pdf_compositor.mojom.h"
|
2017-05-31 17:33:30 +02:00
|
|
|
#include "content/public/child/child_thread.h"
|
|
|
|
#include "content/public/common/service_manager_connection.h"
|
|
|
|
#include "content/public/common/simple_connection_filter.h"
|
2019-01-17 10:56:52 +01:00
|
|
|
#include "content/public/utility/utility_thread.h"
|
2016-10-17 20:14:44 +02:00
|
|
|
#include "mojo/public/cpp/bindings/strong_binding.h"
|
2018-03-20 21:15:08 +01:00
|
|
|
#include "services/network/url_request_context_builder_mojo.h"
|
2017-10-20 19:45:20 +02:00
|
|
|
#include "services/proxy_resolver/proxy_resolver_service.h" // nogncheck
|
2018-03-20 21:15:08 +01:00
|
|
|
#include "services/proxy_resolver/public/mojom/proxy_resolver.mojom.h" // nogncheck
|
2017-05-31 17:33:30 +02:00
|
|
|
#include "services/service_manager/public/cpp/binder_registry.h"
|
2014-07-03 20:34:58 +02:00
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
#if defined(OS_WIN)
|
2016-11-23 21:54:29 +01:00
|
|
|
#include "chrome/utility/printing_handler.h"
|
2014-07-03 20:34:58 +02:00
|
|
|
#endif
|
|
|
|
|
2019-01-17 10:56:52 +01:00
|
|
|
namespace {
|
|
|
|
void RunServiceAsyncThenTerminateProcess(
|
|
|
|
std::unique_ptr<service_manager::Service> service) {
|
|
|
|
service_manager::Service::RunAsyncUntilTermination(
|
|
|
|
std::move(service),
|
|
|
|
base::BindOnce([] { content::UtilityThread::Get()->ReleaseProcess(); }));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(OS_ANDROID)
|
|
|
|
std::unique_ptr<service_manager::Service> CreateProxyResolverService(
|
|
|
|
service_manager::mojom::ServiceRequest request) {
|
|
|
|
return std::make_unique<proxy_resolver::ProxyResolverService>(
|
|
|
|
std::move(request));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using ServiceFactory =
|
|
|
|
base::OnceCallback<std::unique_ptr<service_manager::Service>()>;
|
|
|
|
void RunServiceOnIOThread(ServiceFactory factory) {
|
|
|
|
base::OnceClosure terminate_process = base::BindOnce(
|
|
|
|
base::IgnoreResult(&base::SequencedTaskRunner::PostTask),
|
|
|
|
base::SequencedTaskRunnerHandle::Get(), FROM_HERE,
|
|
|
|
base::BindOnce([] { content::UtilityThread::Get()->ReleaseProcess(); }));
|
|
|
|
content::ChildThread::Get()->GetIOTaskRunner()->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
base::BindOnce(
|
|
|
|
[](ServiceFactory factory, base::OnceClosure terminate_process) {
|
|
|
|
service_manager::Service::RunAsyncUntilTermination(
|
|
|
|
std::move(factory).Run(), std::move(terminate_process));
|
|
|
|
},
|
|
|
|
std::move(factory), std::move(terminate_process)));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-07-03 20:34:58 +02:00
|
|
|
CefContentUtilityClient::CefContentUtilityClient() {
|
2014-09-27 01:48:19 +02:00
|
|
|
#if defined(OS_WIN)
|
2018-03-20 21:15:08 +01:00
|
|
|
printing_handler_ = std::make_unique<printing::PrintingHandler>();
|
2014-07-03 20:34:58 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefContentUtilityClient::~CefContentUtilityClient() {}
|
2014-07-03 20:34:58 +02:00
|
|
|
|
2017-05-31 17:33:30 +02:00
|
|
|
void CefContentUtilityClient::UtilityThreadStarted() {
|
|
|
|
content::ServiceManagerConnection* connection =
|
|
|
|
content::ChildThread::Get()->GetServiceManagerConnection();
|
|
|
|
|
|
|
|
// NOTE: Some utility process instances are not connected to the Service
|
|
|
|
// Manager. Nothing left to do in that case.
|
|
|
|
if (!connection)
|
|
|
|
return;
|
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
auto registry = std::make_unique<service_manager::BinderRegistry>();
|
2017-05-31 17:33:30 +02:00
|
|
|
|
|
|
|
connection->AddConnectionFilter(
|
2018-03-20 21:15:08 +01:00
|
|
|
std::make_unique<content::SimpleConnectionFilter>(std::move(registry)));
|
2017-05-31 17:33:30 +02:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
bool CefContentUtilityClient::OnMessageReceived(const IPC::Message& message) {
|
2016-05-25 01:35:43 +02:00
|
|
|
bool handled = false;
|
2014-07-03 20:34:58 +02:00
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
if (printing_handler_->OnMessageReceived(message))
|
|
|
|
return true;
|
|
|
|
#endif
|
2014-07-03 20:34:58 +02:00
|
|
|
|
|
|
|
return handled;
|
|
|
|
}
|
|
|
|
|
2018-12-26 16:12:11 +01:00
|
|
|
std::unique_ptr<service_manager::Service>
|
2019-01-17 10:56:52 +01:00
|
|
|
CefContentUtilityClient::MaybeCreateMainThreadService(
|
2018-12-26 16:12:11 +01:00
|
|
|
const std::string& service_name,
|
|
|
|
service_manager::mojom::ServiceRequest request) {
|
|
|
|
if (service_name == printing::mojom::kServiceName) {
|
2019-01-17 10:56:52 +01:00
|
|
|
return printing::CreatePdfCompositorService(std::move(request));
|
|
|
|
}
|
|
|
|
if (service_name == printing::mojom::kChromePrintingServiceName) {
|
|
|
|
return std::make_unique<printing::PrintingService>(std::move(request));
|
2018-12-26 16:12:11 +01:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-01-17 10:56:52 +01:00
|
|
|
|
|
|
|
bool CefContentUtilityClient::HandleServiceRequest(
|
|
|
|
const std::string& service_name,
|
|
|
|
service_manager::mojom::ServiceRequest request) {
|
|
|
|
if (service_name == proxy_resolver::mojom::kProxyResolverServiceName) {
|
|
|
|
RunServiceOnIOThread(
|
|
|
|
base::BindOnce(&CreateProxyResolverService, std::move(request)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
auto service = MaybeCreateMainThreadService(service_name, std::move(request));
|
|
|
|
if (service) {
|
|
|
|
RunServiceAsyncThenTerminateProcess(std::move(service));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|