Commit 6c35041e authored by Jay Civelli's avatar Jay Civelli Committed by Commit Bot

Servicifying PdfToEmfConverter.

Moving the PdfToEmfConverter interface to the printing service.
As a result, simplified the client of that interface in
chrome/browser/printing/pdf_to_emf_converter.cc:
- merging PdfConverterUtilityProcessHostClient and PdfConverterImpl.
- calls on the service can now happen on the UI thread instead of the
  IO thread

Renamed the mojom::PdfToEmfConverter implementation from
PdfToEmfConverterImpl to PdfToEmfConverter as is now preferred naming
style for Mojo interface implementation.

Bug: 766451
Change-Id: Ic748547f56bb193a558210f97558640ef8d6e44b
Reviewed-on: https://chromium-review.googlesource.com/826308Reviewed-by: default avatarWill Harris <wfh@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Commit-Queue: Jay Civelli <jcivelli@chromium.org>
Cr-Commit-Position: refs/heads/master@{#524534}
parent a834c680
......@@ -3271,6 +3271,7 @@ split_static_library("browser") {
"printing/pdf_to_emf_converter.cc",
"printing/pdf_to_emf_converter.h",
]
deps += [ "//chrome/services/printing/public/interfaces" ]
}
if (enable_print_preview) {
# Full printing on top of the above.
......
......@@ -11,6 +11,7 @@
#include <utility>
#include <vector>
#include "base/callback.h"
#include "base/containers/queue.h"
#include "base/files/file.h"
#include "base/files/file_util.h"
......@@ -24,18 +25,16 @@
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/threading/thread_restrictions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chrome/common/chrome_utility_printing_messages.h"
#include "chrome/common/printing/pdf_to_emf_converter.mojom.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/services/printing/public/interfaces/constants.mojom.h"
#include "chrome/services/printing/public/interfaces/pdf_to_emf_converter.mojom.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_data.h"
#include "content/public/browser/utility_process_host.h"
#include "content/public/browser/utility_process_host_client.h"
#include "content/public/common/service_manager_connection.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/public/cpp/system/platform_handle.h"
#include "printing/emf_win.h"
#include "printing/pdf_render_settings.h"
#include "ui/base/l10n/l10n_util.h"
#include "services/service_manager/public/cpp/connector.h"
using content::BrowserThread;
......@@ -43,8 +42,6 @@ namespace printing {
namespace {
class PdfConverterImpl;
void CloseFileOnBlockingTaskRunner(base::File temp_file) {
base::AssertBlockingAllowed();
temp_file.Close();
......@@ -194,36 +191,25 @@ class PostScriptMetaFile : public LazyEmf {
};
// Class for converting PDF to another format for printing (Emf, Postscript).
// Class uses UI thread, IO thread and |blocking_task_runner_|.
// Class uses UI thread and |blocking_task_runner_|.
// Internal workflow is following:
// 1. Create instance on the UI thread. (files_, settings_,)
// 2. Create pdf file on |blocking_task_runner_|.
// 3. Start utility process and start conversion on the IO thread.
// 4. Utility process returns page count.
// 3. Bind to printing service and start conversion on the UI thread (mojo
// actually makes that happen transparently on the IO thread).
// 4. Printing service returns page count.
// 5. For each page:
// 1. Clients requests page with file handle to a temp file.
// 2. Utility converts the page, save it to the file and reply.
//
// All these steps work sequentially, so no data should be accessed
// simultaneously by several threads.
class PdfConverterUtilityProcessHostClient
: public content::UtilityProcessHostClient {
class PdfConverterImpl : public PdfConverter {
public:
PdfConverterUtilityProcessHostClient(
base::WeakPtr<PdfConverterImpl> converter,
const PdfRenderSettings& settings);
void Start(const scoped_refptr<base::RefCountedMemory>& data,
PdfConverter::StartCallback start_callback);
void GetPage(int page_number,
const PdfConverter::GetPageCallback& get_page_callback);
void Stop();
// UtilityProcessHostClient implementation.
void OnProcessCrashed(int exit_code) override;
void OnProcessLaunchFailed(int exit_code) override;
PdfConverterImpl(const scoped_refptr<base::RefCountedMemory>& data,
const PdfRenderSettings& conversion_settings,
StartCallback start_callback);
~PdfConverterImpl() override;
private:
class GetPageCallbackData {
......@@ -243,8 +229,11 @@ class PdfConverterUtilityProcessHostClient
}
int page_number() const { return page_number_; }
const PdfConverter::GetPageCallback& callback() const { return callback_; }
ScopedTempFile TakeFile() { return std::move(file_); }
void set_file(ScopedTempFile file) { file_ = std::move(file); }
private:
......@@ -256,20 +245,16 @@ class PdfConverterUtilityProcessHostClient
DISALLOW_COPY_AND_ASSIGN(GetPageCallbackData);
};
~PdfConverterUtilityProcessHostClient() override;
void GetPage(int page_number,
const PdfConverter::GetPageCallback& get_page_callback) override;
bool OnMessageReceived(const IPC::Message& message) override;
void Stop();
// Helper functions: must be overridden by subclasses
// Set the process name
base::string16 GetName() const;
// Create a metafileplayer subclass file from a temporary file.
std::unique_ptr<MetafilePlayer> GetFileFromTemp(ScopedTempFile temp_file);
// Message handlers:
void OnPageCount(mojom::PdfToEmfConverterFactoryPtr factory_keep_alive,
mojom::PdfToEmfConverterPtr converter,
uint32_t page_count);
void OnPageCount(mojom::PdfToEmfConverterPtr converter, uint32_t page_count);
void OnPageDone(bool success, float scale_factor);
void OnFailed(const std::string& error_message);
......@@ -283,16 +268,11 @@ class PdfConverterUtilityProcessHostClient
scoped_refptr<RefCountedTempDir> temp_dir_;
// Used to suppress callbacks after PdfConverter is deleted.
base::WeakPtr<PdfConverterImpl> converter_;
PdfRenderSettings settings_;
// Document loaded callback.
PdfConverter::StartCallback start_callback_;
// Process host for IPC.
base::WeakPtr<content::UtilityProcessHost> utility_process_host_;
// Queue of callbacks for GetPage() requests. Utility process should reply
// with PageDone in the same order as requests were received.
// Use containers that keeps element pointers valid after push() and pop().
......@@ -306,54 +286,24 @@ class PdfConverterUtilityProcessHostClient
mojom::PdfToEmfConverterPtr pdf_to_emf_converter_;
DISALLOW_COPY_AND_ASSIGN(PdfConverterUtilityProcessHostClient);
mojom::PdfToEmfConverterFactoryPtr pdf_to_emf_converter_factory_;
base::WeakPtrFactory<PdfConverterImpl> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(PdfConverterImpl);
};
std::unique_ptr<MetafilePlayer>
PdfConverterUtilityProcessHostClient::GetFileFromTemp(
std::unique_ptr<MetafilePlayer> PdfConverterImpl::GetFileFromTemp(
ScopedTempFile temp_file) {
if (settings_.mode == PdfRenderSettings::Mode::POSTSCRIPT_LEVEL2 ||
settings_.mode == PdfRenderSettings::Mode::POSTSCRIPT_LEVEL3 ||
settings_.mode == PdfRenderSettings::Mode::TEXTONLY) {
return base::MakeUnique<PostScriptMetaFile>(temp_dir_,
return std::make_unique<PostScriptMetaFile>(temp_dir_,
std::move(temp_file));
}
return base::MakeUnique<LazyEmf>(temp_dir_, std::move(temp_file));
return std::make_unique<LazyEmf>(temp_dir_, std::move(temp_file));
}
class PdfConverterImpl : public PdfConverter {
public:
PdfConverterImpl();
~PdfConverterImpl() override;
base::WeakPtr<PdfConverterImpl> GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
void Start(const scoped_refptr<base::RefCountedMemory>& data,
const PdfRenderSettings& conversion_settings,
StartCallback start_callback);
void GetPage(int page_number,
const GetPageCallback& get_page_callback) override;
// Helps to cancel callbacks if this object is destroyed.
void RunCallback(base::OnceClosure callback);
void Start(
const scoped_refptr<PdfConverterUtilityProcessHostClient>& utility_client,
const scoped_refptr<base::RefCountedMemory>& data,
StartCallback start_callback);
private:
scoped_refptr<PdfConverterUtilityProcessHostClient> utility_client_;
base::WeakPtrFactory<PdfConverterImpl> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(PdfConverterImpl);
};
ScopedTempFile CreateTempFile(scoped_refptr<RefCountedTempDir>* temp_dir) {
if (!temp_dir->get())
*temp_dir = base::MakeRefCounted<RefCountedTempDir>();
......@@ -366,7 +316,7 @@ ScopedTempFile CreateTempFile(scoped_refptr<RefCountedTempDir>* temp_dir) {
<< (*temp_dir)->GetPath().value();
return file;
}
file = base::MakeUnique<TempFile>(base::File(
file = std::make_unique<TempFile>(base::File(
path, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
base::File::FLAG_READ | base::File::FLAG_DELETE_ON_CLOSE |
base::File::FLAG_TEMPORARY));
......@@ -462,59 +412,39 @@ bool PostScriptMetaFile::SafePlayback(HDC hdc) const {
return true;
}
PdfConverterUtilityProcessHostClient::PdfConverterUtilityProcessHostClient(
base::WeakPtr<PdfConverterImpl> converter,
const PdfRenderSettings& settings)
: converter_(converter),
settings_(settings),
PdfConverterImpl::PdfConverterImpl(
const scoped_refptr<base::RefCountedMemory>& data,
const PdfRenderSettings& settings,
StartCallback start_callback)
: settings_(settings),
start_callback_(std::move(start_callback)),
blocking_task_runner_(base::CreateSequencedTaskRunnerWithTraits(
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::BLOCK_SHUTDOWN})) {}
PdfConverterUtilityProcessHostClient::~PdfConverterUtilityProcessHostClient() {}
void PdfConverterUtilityProcessHostClient::Start(
const scoped_refptr<base::RefCountedMemory>& data,
PdfConverter::StartCallback start_callback) {
if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::BindOnce(&PdfConverterUtilityProcessHostClient::Start, this, data,
std::move(start_callback)));
return;
}
// Store callback before any OnFailed() call to make it called on failure.
DCHECK(start_callback);
start_callback_ = std::move(start_callback);
// NOTE: This process _must_ be sandboxed, otherwise the pdf dll will load
// gdiplus.dll, change how rendering happens, and not be able to correctly
// generate when sent to a metafile DC.
utility_process_host_ = content::UtilityProcessHost::Create(
this, base::ThreadTaskRunnerHandle::Get())
->AsWeakPtr();
utility_process_host_->SetName(GetName());
utility_process_host_->Start();
base::TaskShutdownBehavior::BLOCK_SHUTDOWN})),
weak_ptr_factory_(this) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(start_callback_);
base::PostTaskAndReplyWithResult(
blocking_task_runner_.get(), FROM_HERE,
base::Bind(&CreateTempPdfFile, data, &temp_dir_),
base::Bind(&PdfConverterUtilityProcessHostClient::OnTempPdfReady, this));
base::BindOnce(&CreateTempPdfFile, data, &temp_dir_),
base::BindOnce(&PdfConverterImpl::OnTempPdfReady,
weak_ptr_factory_.GetWeakPtr()));
}
void PdfConverterUtilityProcessHostClient::OnTempPdfReady(ScopedTempFile pdf) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (!utility_process_host_ || !pdf)
return OnFailed(std::string("Failed to create temporary PDF file."));
PdfConverterImpl::~PdfConverterImpl() {}
mojom::PdfToEmfConverterFactoryPtr pdf_to_emf_converter_factory_ptr;
utility_process_host_->BindInterface(
mojom::PdfToEmfConverterFactory::Name_,
mojo::MakeRequest(&pdf_to_emf_converter_factory_ptr).PassMessagePipe());
void PdfConverterImpl::OnTempPdfReady(ScopedTempFile pdf) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!pdf)
return OnFailed(std::string("Failed to create temporary PDF file."));
pdf_to_emf_converter_factory_ptr.set_connection_error_handler(base::BindOnce(
&PdfConverterUtilityProcessHostClient::OnFailed, this,
content::ServiceManagerConnection::GetForProcess()
->GetConnector()
->BindInterface(printing::mojom::kChromePrintingServiceName,
&pdf_to_emf_converter_factory_);
pdf_to_emf_converter_factory_.set_connection_error_handler(base::BindOnce(
&PdfConverterImpl::OnFailed, weak_ptr_factory_.GetWeakPtr(),
std::string("Connection to PdfToEmfConverterFactory error.")));
mojom::PdfToEmfConverterClientPtr pdf_to_emf_converter_client_ptr;
......@@ -522,59 +452,49 @@ void PdfConverterUtilityProcessHostClient::OnTempPdfReady(ScopedTempFile pdf) {
std::make_unique<PdfToEmfConverterClientImpl>(
mojo::MakeRequest(&pdf_to_emf_converter_client_ptr));
pdf_to_emf_converter_factory_ptr->CreateConverter(
pdf_to_emf_converter_factory_->CreateConverter(
mojo::WrapPlatformFile(pdf->file().TakePlatformFile()), settings_,
std::move(pdf_to_emf_converter_client_ptr),
base::BindOnce(&PdfConverterUtilityProcessHostClient::OnPageCount, this,
std::move(pdf_to_emf_converter_factory_ptr)));
base::BindOnce(&PdfConverterImpl::OnPageCount,
weak_ptr_factory_.GetWeakPtr()));
}
void PdfConverterUtilityProcessHostClient::OnPageCount(
mojom::PdfToEmfConverterFactoryPtr factory_keep_alive,
mojom::PdfToEmfConverterPtr converter,
void PdfConverterImpl::OnPageCount(mojom::PdfToEmfConverterPtr converter,
uint32_t page_count) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(!pdf_to_emf_converter_.is_bound());
pdf_to_emf_converter_ = std::move(converter);
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::BindOnce(&PdfConverterImpl::RunCallback, converter_,
base::BindOnce(std::move(start_callback_), page_count)));
pdf_to_emf_converter_.set_connection_error_handler(base::BindOnce(
&PdfConverterImpl::OnFailed, weak_ptr_factory_.GetWeakPtr(),
std::string("Connection to PdfToEmfConverter error.")));
std::move(start_callback_).Run(page_count);
}
void PdfConverterUtilityProcessHostClient::GetPage(
void PdfConverterImpl::GetPage(
int page_number,
const PdfConverter::GetPageCallback& get_page_callback) {
if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::BindOnce(&PdfConverterUtilityProcessHostClient::GetPage, this,
page_number, get_page_callback));
return;
}
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// Store callback before any OnFailed() call to make it called on failure.
get_page_callbacks_.push(GetPageCallbackData(page_number, get_page_callback));
if (!utility_process_host_)
return OnFailed(std::string("No process utility host."));
if (!pdf_to_emf_converter_)
return OnFailed(std::string("No PdfToEmfConverter."));
base::PostTaskAndReplyWithResult(
blocking_task_runner_.get(), FROM_HERE,
base::Bind(&CreateTempFile, &temp_dir_),
base::Bind(&PdfConverterUtilityProcessHostClient::OnTempFileReady, this,
base::BindOnce(&CreateTempFile, &temp_dir_),
base::BindOnce(&PdfConverterImpl::OnTempFileReady,
weak_ptr_factory_.GetWeakPtr(),
&get_page_callbacks_.back()));
}
void PdfConverterUtilityProcessHostClient::OnTempFileReady(
GetPageCallbackData* callback_data,
void PdfConverterImpl::OnTempFileReady(GetPageCallbackData* callback_data,
ScopedTempFile temp_file) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(pdf_to_emf_converter_.is_bound());
if (!utility_process_host_ || !temp_file)
return OnFailed(std::string("Error creating utility process host"));
if (!pdf_to_emf_converter_ || !temp_file)
return OnFailed(std::string("Error connecting to printing service."));
// We need to dup the file as mojo::WrapPlatformFile takes ownership of the
// passed file.
......@@ -582,13 +502,13 @@ void PdfConverterUtilityProcessHostClient::OnTempFileReady(
pdf_to_emf_converter_->ConvertPage(
callback_data->page_number(),
mojo::WrapPlatformFile(temp_file_copy.TakePlatformFile()),
base::BindOnce(&PdfConverterUtilityProcessHostClient::OnPageDone, this));
base::BindOnce(&PdfConverterImpl::OnPageDone,
weak_ptr_factory_.GetWeakPtr()));
callback_data->set_file(std::move(temp_file));
}
void PdfConverterUtilityProcessHostClient::OnPageDone(bool success,
float scale_factor) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
void PdfConverterImpl::OnPageDone(bool success, float scale_factor) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (get_page_callbacks_.empty())
return OnFailed(std::string("No get_page callbacks."));
GetPageCallbackData& data = get_page_callbacks_.front();
......@@ -596,61 +516,34 @@ void PdfConverterUtilityProcessHostClient::OnPageDone(bool success,
if (success) {
ScopedTempFile temp_file = data.TakeFile();
if (!temp_file) // Unexpected message from utility process.
if (!temp_file) // Unexpected message from printing service.
return OnFailed("No temp file.");
file = GetFileFromTemp(std::move(temp_file));
}
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&PdfConverterImpl::RunCallback, converter_,
base::BindRepeating(data.callback(), data.page_number(),
scale_factor, base::Passed(&file))));
data.callback().Run(data.page_number(), scale_factor, std::move(file));
get_page_callbacks_.pop();
}
void PdfConverterUtilityProcessHostClient::Stop() {
if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&PdfConverterUtilityProcessHostClient::Stop, this));
return;
}
void PdfConverterImpl::Stop() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// Disconnect interface ptrs so that the printing service process stop.
pdf_to_emf_converter_factory_.reset();
pdf_to_emf_converter_.reset();
// Once this becomes a Mojo service, simply disconnecting
// pdf_to_emf_converter_ above will lead to the utility process stopping.
// In the meantime we need to explictly terminate it.
if (utility_process_host_) {
utility_process_host_->Send(
new ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop());
}
}
void PdfConverterUtilityProcessHostClient::OnProcessCrashed(int exit_code) {
OnFailed("Utility process host crashed.");
}
void PdfConverterUtilityProcessHostClient::OnProcessLaunchFailed(
int exit_code) {
OnFailed("Process launch failed");
}
void PdfConverterUtilityProcessHostClient::OnFailed(
const std::string& error_message) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
void PdfConverterImpl::OnFailed(const std::string& error_message) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
LOG(ERROR) << "Failed to convert PDF: " << error_message;
if (!start_callback_.is_null()) {
OnPageCount(mojom::PdfToEmfConverterFactoryPtr(),
mojom::PdfToEmfConverterPtr(), 0);
OnPageCount(mojom::PdfToEmfConverterPtr(), 0);
}
while (!get_page_callbacks_.empty())
OnPageDone(false, 0.0f);
utility_process_host_.reset();
Stop();
}
void PdfConverterUtilityProcessHostClient::OnPreCacheFontCharacters(
const LOGFONT& font,
void PdfConverterImpl::OnPreCacheFontCharacters(const LOGFONT& font,
const base::string16& str) {
// TODO(scottmg): pdf/ppapi still require the renderer to be able to precache
// GDI fonts (http://crbug.com/383227), even when using DirectWrite.
......@@ -681,57 +574,17 @@ void PdfConverterUtilityProcessHostClient::OnPreCacheFontCharacters(
DeleteEnhMetaFile(metafile);
}
bool PdfConverterUtilityProcessHostClient::OnMessageReceived(
const IPC::Message& message) {
return false;
}
base::string16 PdfConverterUtilityProcessHostClient::GetName() const {
return l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_PDF_CONVERTOR_NAME);
}
// Pdf Converter Impl and subclasses
PdfConverterImpl::PdfConverterImpl() : weak_ptr_factory_(this) {}
PdfConverterImpl::~PdfConverterImpl() {
if (utility_client_.get())
utility_client_->Stop();
}
void PdfConverterImpl::Start(
const scoped_refptr<PdfConverterUtilityProcessHostClient>& utility_client,
const scoped_refptr<base::RefCountedMemory>& data,
StartCallback start_callback) {
DCHECK(!utility_client_);
utility_client_ = utility_client;
utility_client_->Start(data, std::move(start_callback));
}
void PdfConverterImpl::GetPage(int page_number,
const GetPageCallback& get_page_callback) {
utility_client_->GetPage(page_number, get_page_callback);
}
void PdfConverterImpl::RunCallback(base::OnceClosure callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
std::move(callback).Run();
}
} // namespace
PdfConverter::~PdfConverter() {}
PdfConverter::~PdfConverter() = default;
// static
std::unique_ptr<PdfConverter> PdfConverter::StartPdfConverter(
const scoped_refptr<base::RefCountedMemory>& data,
const PdfRenderSettings& conversion_settings,
StartCallback start_callback) {
std::unique_ptr<PdfConverterImpl> converter =
base::MakeUnique<PdfConverterImpl>();
converter->Start(new PdfConverterUtilityProcessHostClient(
converter->GetWeakPtr(), conversion_settings),
data, std::move(start_callback));
return std::move(converter);
return std::make_unique<PdfConverterImpl>(data, conversion_settings,
std::move(start_callback));
}
} // namespace printing
......@@ -7,7 +7,7 @@
#include <memory>
#include "base/callback.h"
#include "base/callback_forward.h"
#include "base/memory/ref_counted_memory.h"
namespace printing {
......
......@@ -275,7 +275,7 @@ void PrintJob::StartPdfToEmfConversion(
print_text_with_gdi ? PdfRenderSettings::Mode::GDI_TEXT
: PdfRenderSettings::Mode::NORMAL);
pdf_conversion_state_->Start(
bytes, settings, base::Bind(&PrintJob::OnPdfConversionStarted, this));
bytes, settings, base::BindOnce(&PrintJob::OnPdfConversionStarted, this));
}
void PrintJob::OnPdfConversionStarted(int page_count) {
......@@ -321,7 +321,7 @@ void PrintJob::StartPdfToTextConversion(
/*autorotate=*/true,
PdfRenderSettings::Mode::TEXTONLY);
pdf_conversion_state_->Start(
bytes, settings, base::Bind(&PrintJob::OnPdfConversionStarted, this));
bytes, settings, base::BindOnce(&PrintJob::OnPdfConversionStarted, this));
}
void PrintJob::StartPdfToPostScriptConversion(
......@@ -338,7 +338,7 @@ void PrintJob::StartPdfToPostScriptConversion(
ps_level2 ? PdfRenderSettings::Mode::POSTSCRIPT_LEVEL2
: PdfRenderSettings::Mode::POSTSCRIPT_LEVEL3);
pdf_conversion_state_->Start(
bytes, settings, base::Bind(&PrintJob::OnPdfConversionStarted, this));
bytes, settings, base::BindOnce(&PrintJob::OnPdfConversionStarted, this));
}
#endif // defined(OS_WIN)
......
......@@ -213,7 +213,6 @@ static_library("common") {
"//chrome/app/theme:theme_resources",
"//chrome/common:constants",
"//chrome/common/net",
"//chrome/common/printing:interfaces",
"//chrome/common/profiling",
"//chrome/installer/util:with_no_strings",
"//components/cast_certificate",
......
# Copyright 2017 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.
import("//mojo/public/tools/bindings/mojom.gni")
mojom("interfaces") {
sources = [
"pdf_to_emf_converter.mojom",
]
deps = [
"//chrome/services/printing/public/interfaces",
"//mojo/common:common_custom_types",
"//ui/gfx/geometry/mojo",
]
}
per-file *.mojom=set noparent
per-file *.mojom=file://ipc/SECURITY_OWNERS
......@@ -22,6 +22,17 @@ source_set("lib") {
"//chrome/services/printing/public/interfaces",
"//services/service_manager/public/cpp",
]
if (is_win) {
sources += [
"pdf_to_emf_converter.cc",
"pdf_to_emf_converter.h",
"pdf_to_emf_converter_factory.cc",
"pdf_to_emf_converter_factory.h",
]
deps += [ "//skia" ]
}
}
service_manifest("manifest") {
......
{
"name": "chrome_printing",
"display_name": "Printing",
"sandbox_type": "utility",
"interface_provider_specs": {
"service_manager:connector": {
"provides": {
"converter": [ "printing::mojom::PdfToPwgRasterConverter" ]
"converter": [
"printing::mojom::PdfToEmfConverterFactory",
"printing::mojom::PdfToPwgRasterConverter" ]
},
"requires": {
"service_manager": [ "service_manager:all_users" ]
......
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/utility/printing/pdf_to_emf_converter_impl.h"
#include "chrome/services/printing/pdf_to_emf_converter.h"
#include <algorithm>
......@@ -61,7 +61,7 @@ void RegisterConverterClient(mojom::PdfToEmfConverterClientPtr client) {
} // namespace
PdfToEmfConverterImpl::PdfToEmfConverterImpl(
PdfToEmfConverter::PdfToEmfConverter(
mojo::ScopedHandle pdf_file_in,
const printing::PdfRenderSettings& pdf_render_settings,
mojom::PdfToEmfConverterClientPtr client)
......@@ -90,15 +90,15 @@ PdfToEmfConverterImpl::PdfToEmfConverterImpl(
base::PlatformFile pdf_file;
if (mojo::UnwrapPlatformFile(std::move(pdf_file_in), &pdf_file) !=
MOJO_RESULT_OK) {
LOG(ERROR) << "Invalid PDF file passed to PdfToEmfConverterImpl";
LOG(ERROR) << "Invalid PDF file passed to PdfToEmfConverter.";
return;
}
LoadPdf(base::File(pdf_file));
}
PdfToEmfConverterImpl::~PdfToEmfConverterImpl() = default;
PdfToEmfConverter::~PdfToEmfConverter() = default;
void PdfToEmfConverterImpl::LoadPdf(base::File pdf_file) {
void PdfToEmfConverter::LoadPdf(base::File pdf_file) {
int64_t length64 = pdf_file.GetLength();
if (length64 <= 0 || length64 > std::numeric_limits<int>::max())
return;
......@@ -114,7 +114,7 @@ void PdfToEmfConverterImpl::LoadPdf(base::File pdf_file) {
total_page_count_ = page_count;
}
bool PdfToEmfConverterImpl::RenderPdfPageToMetafile(int page_number,
bool PdfToEmfConverter::RenderPdfPageToMetafile(int page_number,
base::File output_file,
float* scale_factor,
bool postscript) {
......@@ -160,7 +160,7 @@ bool PdfToEmfConverterImpl::RenderPdfPageToMetafile(int page_number,
return metafile.SaveTo(&output_file);
}
void PdfToEmfConverterImpl::ConvertPage(uint32_t page_number,
void PdfToEmfConverter::ConvertPage(uint32_t page_number,
mojo::ScopedHandle emf_file_out,
ConvertPageCallback callback) {
if (page_number >= total_page_count_) {
......
......@@ -2,24 +2,24 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_UTILITY_PRINTING_PDF_TO_EMF_CONVERTER_IMPL_H_
#define CHROME_UTILITY_PRINTING_PDF_TO_EMF_CONVERTER_IMPL_H_
#ifndef CHROME_SERVICES_PRINTING_PDF_TO_EMF_CONVERTER_H_
#define CHROME_SERVICES_PRINTING_PDF_TO_EMF_CONVERTER_H_
#include <vector>
#include "base/files/file.h"
#include "base/macros.h"
#include "chrome/common/printing/pdf_to_emf_converter.mojom.h"
#include "chrome/services/printing/public/interfaces/pdf_to_emf_converter.mojom.h"
#include "printing/pdf_render_settings.h"
namespace printing {
class PdfToEmfConverterImpl : public mojom::PdfToEmfConverter {
class PdfToEmfConverter : public mojom::PdfToEmfConverter {
public:
PdfToEmfConverterImpl(mojo::ScopedHandle pdf_file_in,
PdfToEmfConverter(mojo::ScopedHandle pdf_file_in,
const PdfRenderSettings& render_settings,
mojom::PdfToEmfConverterClientPtr client);
~PdfToEmfConverterImpl() override;
~PdfToEmfConverter() override;
int total_page_count() const { return total_page_count_; }
......@@ -39,9 +39,9 @@ class PdfToEmfConverterImpl : public mojom::PdfToEmfConverter {
PdfRenderSettings pdf_render_settings_;
std::vector<char> pdf_data_;
DISALLOW_COPY_AND_ASSIGN(PdfToEmfConverterImpl);
DISALLOW_COPY_AND_ASSIGN(PdfToEmfConverter);
};
} // namespace printing
#endif // CHROME_UTILITY_PRINTING_PDF_TO_EMF_CONVERTER_IMPL_H_
#endif // CHROME_SERVICES_PRINTING_PDF_TO_EMF_CONVERTER_H_
......@@ -2,24 +2,26 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/utility/printing/pdf_to_emf_converter_factory_impl.h"
#include "chrome/services/printing/pdf_to_emf_converter_factory.h"
#include "chrome/utility/printing/pdf_to_emf_converter_impl.h"
#include "chrome/services/printing/pdf_to_emf_converter.h"
#include "mojo/public/cpp/bindings/strong_binding.h"
#include "mojo/public/cpp/system/platform_handle.h"
namespace printing {
PdfToEmfConverterFactoryImpl::PdfToEmfConverterFactoryImpl() = default;
PdfToEmfConverterFactory::PdfToEmfConverterFactory(
std::unique_ptr<service_manager::ServiceContextRef> service_ref)
: service_ref_(std::move(service_ref)) {}
PdfToEmfConverterFactoryImpl::~PdfToEmfConverterFactoryImpl() = default;
PdfToEmfConverterFactory::~PdfToEmfConverterFactory() = default;
void PdfToEmfConverterFactoryImpl::CreateConverter(
void PdfToEmfConverterFactory::CreateConverter(
mojo::ScopedHandle pdf_file_in,
const printing::PdfRenderSettings& render_settings,
const PdfRenderSettings& render_settings,
mojom::PdfToEmfConverterClientPtr client,
CreateConverterCallback callback) {
auto converter = std::make_unique<PdfToEmfConverterImpl>(
auto converter = std::make_unique<PdfToEmfConverter>(
std::move(pdf_file_in), render_settings, std::move(client));
uint32_t page_count = converter->total_page_count();
mojom::PdfToEmfConverterPtr converter_ptr;
......@@ -29,11 +31,4 @@ void PdfToEmfConverterFactoryImpl::CreateConverter(
std::move(callback).Run(std::move(converter_ptr), page_count);
}
// static
void PdfToEmfConverterFactoryImpl::Create(
mojom::PdfToEmfConverterFactoryRequest request) {
mojo::MakeStrongBinding(base::MakeUnique<PdfToEmfConverterFactoryImpl>(),
std::move(request));
}
} // namespace printing
......@@ -2,31 +2,33 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_UTILITY_PRINTING_PDF_TO_EMF_CONVERTER_FACTORY_IMPL_H_
#define CHROME_UTILITY_PRINTING_PDF_TO_EMF_CONVERTER_FACTORY_IMPL_H_
#ifndef CHROME_SERVICES_PRINTING_PDF_TO_EMF_CONVERTER_FACTORY_H_
#define CHROME_SERVICES_PRINTING_PDF_TO_EMF_CONVERTER_FACTORY_H_
#include "base/macros.h"
#include "chrome/common/printing/pdf_to_emf_converter.mojom.h"
#include "chrome/services/printing/public/interfaces/pdf_to_emf_converter.mojom.h"
#include "services/service_manager/public/cpp/service_context_ref.h"
namespace printing {
class PdfToEmfConverterFactoryImpl : public mojom::PdfToEmfConverterFactory {
class PdfToEmfConverterFactory : public mojom::PdfToEmfConverterFactory {
public:
PdfToEmfConverterFactoryImpl();
~PdfToEmfConverterFactoryImpl() override;
static void Create(mojom::PdfToEmfConverterFactoryRequest request);
explicit PdfToEmfConverterFactory(
std::unique_ptr<service_manager::ServiceContextRef> service_ref);
~PdfToEmfConverterFactory() override;
private:
// mojom::PdfToEmfConverterFactory implementation.
void CreateConverter(mojo::ScopedHandle pdf_file_in,
const printing::PdfRenderSettings& render_settings,
const PdfRenderSettings& render_settings,
mojom::PdfToEmfConverterClientPtr client,
CreateConverterCallback callback) override;
DISALLOW_COPY_AND_ASSIGN(PdfToEmfConverterFactoryImpl);
const std::unique_ptr<service_manager::ServiceContextRef> service_ref_;
DISALLOW_COPY_AND_ASSIGN(PdfToEmfConverterFactory);
};
} // namespace printing
#endif // CHROME_UTILITY_PRINTING_PDF_TO_EMF_CONVERTER_FACTORY_IMPL_H_
#endif // CHROME_SERVICES_PRINTING_PDF_TO_EMF_CONVERTER_FACTORY_H_
......@@ -97,13 +97,13 @@ bool RenderPdfPagesToPwgRaster(base::File pdf_file,
} // namespace
PdfToPwgRasterConverterImpl::PdfToPwgRasterConverterImpl(
PdfToPwgRasterConverter::PdfToPwgRasterConverter(
std::unique_ptr<service_manager::ServiceContextRef> service_ref)
: service_ref_(std::move(service_ref)) {}
PdfToPwgRasterConverterImpl::~PdfToPwgRasterConverterImpl() {}
PdfToPwgRasterConverter::~PdfToPwgRasterConverter() {}
void PdfToPwgRasterConverterImpl::Convert(
void PdfToPwgRasterConverter::Convert(
mojo::ScopedHandle pdf_file_in,
const PdfRenderSettings& pdf_settings,
const PwgRasterSettings& pwg_raster_settings,
......@@ -112,7 +112,7 @@ void PdfToPwgRasterConverterImpl::Convert(
base::PlatformFile pdf_file;
if (mojo::UnwrapPlatformFile(std::move(pdf_file_in), &pdf_file) !=
MOJO_RESULT_OK) {
LOG(ERROR) << "Invalid PDF file passed to PdfToPwgRasterConverterImpl";
LOG(ERROR) << "Invalid PDF file passed to PdfToPwgRasterConverter.";
std::move(callback).Run(false);
return;
}
......@@ -120,8 +120,7 @@ void PdfToPwgRasterConverterImpl::Convert(
base::PlatformFile pwg_raster_file;
if (mojo::UnwrapPlatformFile(std::move(pwg_raster_file_out),
&pwg_raster_file) != MOJO_RESULT_OK) {
LOG(ERROR)
<< "Invalid PWGRaster file passed to PdfToPwgRasterConverterImpl";
LOG(ERROR) << "Invalid PWGRaster file passed to PdfToPwgRasterConverter.";
std::move(callback).Run(false);
return;
}
......
......@@ -16,12 +16,12 @@ namespace printing {
struct PdfRenderSettings;
class PdfToPwgRasterConverterImpl
class PdfToPwgRasterConverter
: public printing::mojom::PdfToPwgRasterConverter {
public:
explicit PdfToPwgRasterConverterImpl(
explicit PdfToPwgRasterConverter(
std::unique_ptr<service_manager::ServiceContextRef> service_ref);
~PdfToPwgRasterConverterImpl() override;
~PdfToPwgRasterConverter() override;
private:
// printing::mojom::PdfToPwgRasterConverter
......@@ -33,7 +33,7 @@ class PdfToPwgRasterConverterImpl
const std::unique_ptr<service_manager::ServiceContextRef> service_ref_;
DISALLOW_COPY_AND_ASSIGN(PdfToPwgRasterConverterImpl);
DISALLOW_COPY_AND_ASSIGN(PdfToPwgRasterConverter);
};
} // namespace printing
......
......@@ -8,15 +8,29 @@
#include "chrome/services/printing/pdf_to_pwg_raster_converter.h"
#include "mojo/public/cpp/bindings/strong_binding.h"
#if defined(OS_WIN)
#include "chrome/services/printing/pdf_to_emf_converter.h"
#include "chrome/services/printing/pdf_to_emf_converter_factory.h"
#endif
namespace printing {
namespace {
#if defined(OS_WIN)
void OnPdfToEmfConverterFactoryRequest(
service_manager::ServiceContextRefFactory* ref_factory,
printing::mojom::PdfToEmfConverterFactoryRequest request) {
mojo::MakeStrongBinding(std::make_unique<printing::PdfToEmfConverterFactory>(
ref_factory->CreateRef()),
std::move(request));
}
#endif
void OnPdfToPwgRasterConverterRequest(
service_manager::ServiceContextRefFactory* ref_factory,
printing::mojom::PdfToPwgRasterConverterRequest request) {
mojo::MakeStrongBinding(
std::make_unique<printing::PdfToPwgRasterConverterImpl>(
mojo::MakeStrongBinding(std::make_unique<printing::PdfToPwgRasterConverter>(
ref_factory->CreateRef()),
std::move(request));
}
......@@ -35,6 +49,10 @@ void PrintingService::OnStart() {
ref_factory_ = std::make_unique<service_manager::ServiceContextRefFactory>(
base::Bind(&service_manager::ServiceContext::RequestQuit,
base::Unretained(context())));
#if defined(OS_WIN)
registry_.AddInterface(
base::Bind(&OnPdfToEmfConverterFactoryRequest, ref_factory_.get()));
#endif
registry_.AddInterface(
base::Bind(&OnPdfToPwgRasterConverterRequest, ref_factory_.get()));
}
......
......@@ -11,6 +11,10 @@ mojom("interfaces") {
"pdf_to_pwg_raster_converter.mojom",
]
if (is_win) {
sources += [ "pdf_to_emf_converter.mojom" ]
}
deps = [
"//mojo/common:common_custom_types",
"//ui/gfx/geometry/mojo",
......
......@@ -184,15 +184,6 @@ static_library("utility") {
"printing_handler.cc",
"printing_handler.h",
]
deps += [ "//pdf" ]
if (is_win) {
sources += [
"printing/pdf_to_emf_converter_factory_impl.cc",
"printing/pdf_to_emf_converter_factory_impl.h",
"printing/pdf_to_emf_converter_impl.cc",
"printing/pdf_to_emf_converter_impl.h",
]
}
}
if (enable_basic_printing || enable_print_preview) {
......
......@@ -46,7 +46,6 @@
#if defined(OS_WIN)
#include "chrome/services/util_win/public/interfaces/constants.mojom.h"
#include "chrome/services/util_win/util_win_service.h"
#include "chrome/utility/printing/pdf_to_emf_converter_factory_impl.h"
#endif
#if BUILDFLAG(ENABLE_EXTENSIONS)
......@@ -179,11 +178,6 @@ void ChromeContentUtilityClient::UtilityThreadStarted() {
registry->AddInterface(base::Bind(CreateResourceUsageReporter),
base::ThreadTaskRunnerHandle::Get());
#endif // !defined(OS_ANDROID)
#if defined(OS_WIN)
registry->AddInterface(
base::Bind(printing::PdfToEmfConverterFactoryImpl::Create),
base::ThreadTaskRunnerHandle::Get());
#endif
}
connection->AddConnectionFilter(
......
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