2014-07-10 17:41:30 +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/browser/printing/print_dialog_linux.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-10-12 22:45:14 +02:00
|
|
|
#include "libcef/browser/extensions/browser_extensions_util.h"
|
2014-07-10 17:41:30 +02:00
|
|
|
#include "libcef/browser/print_settings_impl.h"
|
|
|
|
#include "libcef/browser/thread_util.h"
|
2020-06-28 23:05:36 +02:00
|
|
|
#include "libcef/common/app_manager.h"
|
2021-08-19 23:07:44 +02:00
|
|
|
#include "libcef/common/frame_util.h"
|
2014-07-10 17:41:30 +02:00
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
#include "base/files/file_util.h"
|
2023-01-30 18:43:54 +01:00
|
|
|
#include "base/functional/bind.h"
|
2014-07-10 17:41:30 +02:00
|
|
|
#include "base/lazy_instance.h"
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
|
|
|
#include "base/values.h"
|
2021-08-19 23:07:44 +02:00
|
|
|
#include "content/public/browser/global_routing_id.h"
|
2014-07-10 17:41:30 +02:00
|
|
|
#include "printing/metafile.h"
|
2021-10-19 00:17:16 +02:00
|
|
|
#include "printing/mojom/print.mojom-shared.h"
|
2014-07-10 17:41:30 +02:00
|
|
|
#include "printing/print_job_constants.h"
|
|
|
|
#include "printing/print_settings.h"
|
|
|
|
|
|
|
|
using content::BrowserThread;
|
|
|
|
using printing::PageRanges;
|
|
|
|
using printing::PrintSettings;
|
|
|
|
|
2022-04-15 21:55:23 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
CefRefPtr<CefBrowserHostBase> GetBrowserForContext(
|
|
|
|
printing::PrintingContextLinux* context) {
|
2022-10-12 23:53:06 +02:00
|
|
|
// The print preview dialog won't have a valid child ID.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!frame_util::IsValidChildId(context->render_process_id())) {
|
2022-10-12 23:53:06 +02:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2022-10-12 23:53:06 +02:00
|
|
|
|
2022-04-15 21:55:23 +02:00
|
|
|
return extensions::GetOwnerBrowserForGlobalId(
|
|
|
|
frame_util::MakeGlobalId(context->render_process_id(),
|
|
|
|
context->render_frame_id()),
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefPrintHandler> GetPrintHandlerForBrowser(
|
|
|
|
CefRefPtr<CefBrowserHostBase> browser) {
|
|
|
|
if (browser) {
|
|
|
|
if (auto client = browser->GetClient()) {
|
|
|
|
return client->GetPrintHandler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-07-10 17:41:30 +02:00
|
|
|
class CefPrintDialogCallbackImpl : public CefPrintDialogCallback {
|
|
|
|
public:
|
|
|
|
explicit CefPrintDialogCallbackImpl(CefRefPtr<CefPrintDialogLinux> dialog)
|
2017-05-17 11:29:28 +02:00
|
|
|
: dialog_(dialog) {}
|
2014-07-10 17:41:30 +02:00
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
CefPrintDialogCallbackImpl(const CefPrintDialogCallbackImpl&) = delete;
|
|
|
|
CefPrintDialogCallbackImpl& operator=(const CefPrintDialogCallbackImpl&) =
|
|
|
|
delete;
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void Continue(CefRefPtr<CefPrintSettings> settings) override {
|
2014-07-10 17:41:30 +02:00
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
2014-09-27 01:48:19 +02:00
|
|
|
if (dialog_.get()) {
|
2014-07-10 17:41:30 +02:00
|
|
|
dialog_->OnPrintContinue(settings);
|
2020-01-15 14:36:24 +01:00
|
|
|
dialog_ = nullptr;
|
2014-07-10 17:41:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-06-04 03:34:56 +02:00
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::BindOnce(&CefPrintDialogCallbackImpl::Continue, this,
|
|
|
|
settings));
|
2014-07-10 17:41:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void Cancel() override {
|
2014-07-10 17:41:30 +02:00
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
2014-09-27 01:48:19 +02:00
|
|
|
if (dialog_.get()) {
|
2014-07-10 17:41:30 +02:00
|
|
|
dialog_->OnPrintCancel();
|
2020-01-15 14:36:24 +01:00
|
|
|
dialog_ = nullptr;
|
2014-07-10 17:41:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
2021-06-04 03:34:56 +02:00
|
|
|
base::BindOnce(&CefPrintDialogCallbackImpl::Cancel, this));
|
2014-07-10 17:41:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 14:36:24 +01:00
|
|
|
void Disconnect() { dialog_ = nullptr; }
|
2014-07-10 17:41:30 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
CefRefPtr<CefPrintDialogLinux> dialog_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefPrintDialogCallbackImpl);
|
|
|
|
};
|
|
|
|
|
|
|
|
class CefPrintJobCallbackImpl : public CefPrintJobCallback {
|
|
|
|
public:
|
|
|
|
explicit CefPrintJobCallbackImpl(CefRefPtr<CefPrintDialogLinux> dialog)
|
2017-05-17 11:29:28 +02:00
|
|
|
: dialog_(dialog) {}
|
2014-07-10 17:41:30 +02:00
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
CefPrintJobCallbackImpl(const CefPrintJobCallbackImpl&) = delete;
|
|
|
|
CefPrintJobCallbackImpl& operator=(const CefPrintJobCallbackImpl&) = delete;
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void Continue() override {
|
2014-07-10 17:41:30 +02:00
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
2014-09-27 01:48:19 +02:00
|
|
|
if (dialog_.get()) {
|
2014-07-10 17:41:30 +02:00
|
|
|
dialog_->OnJobCompleted();
|
2020-01-15 14:36:24 +01:00
|
|
|
dialog_ = nullptr;
|
2014-07-10 17:41:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
2021-06-04 03:34:56 +02:00
|
|
|
base::BindOnce(&CefPrintJobCallbackImpl::Continue, this));
|
2014-07-10 17:41:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 14:36:24 +01:00
|
|
|
void Disconnect() { dialog_ = nullptr; }
|
2014-07-10 17:41:30 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
CefRefPtr<CefPrintDialogLinux> dialog_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefPrintJobCallbackImpl);
|
|
|
|
};
|
|
|
|
|
2022-07-21 19:26:10 +02:00
|
|
|
CefPrintingContextLinuxDelegate::CefPrintingContextLinuxDelegate() = default;
|
|
|
|
|
|
|
|
printing::PrintDialogLinuxInterface*
|
|
|
|
CefPrintingContextLinuxDelegate::CreatePrintDialog(
|
|
|
|
printing::PrintingContextLinux* context) {
|
2014-07-10 17:41:30 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
2022-04-15 21:55:23 +02:00
|
|
|
|
2022-07-21 19:26:10 +02:00
|
|
|
printing::PrintDialogLinuxInterface* interface = nullptr;
|
2022-04-15 21:55:23 +02:00
|
|
|
|
|
|
|
auto browser = GetBrowserForContext(context);
|
|
|
|
if (!browser) {
|
|
|
|
LOG(ERROR) << "No associated browser in CreatePrintDialog; using default "
|
|
|
|
"printing implementation.";
|
|
|
|
}
|
|
|
|
|
|
|
|
auto handler = GetPrintHandlerForBrowser(browser);
|
|
|
|
if (!handler) {
|
2022-07-21 19:26:10 +02:00
|
|
|
if (default_delegate_) {
|
|
|
|
interface = default_delegate_->CreatePrintDialog(context);
|
2022-04-15 21:55:23 +02:00
|
|
|
DCHECK(interface);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
interface = new CefPrintDialogLinux(context, browser, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!interface) {
|
|
|
|
LOG(ERROR) << "Null interface in CreatePrintDialog; printing will fail.";
|
|
|
|
}
|
|
|
|
|
|
|
|
return interface;
|
2014-07-10 17:41:30 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 19:26:10 +02:00
|
|
|
gfx::Size CefPrintingContextLinuxDelegate::GetPdfPaperSize(
|
2015-03-24 16:40:08 +01:00
|
|
|
printing::PrintingContextLinux* context) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
gfx::Size size;
|
|
|
|
|
2022-04-15 21:55:23 +02:00
|
|
|
auto browser = GetBrowserForContext(context);
|
|
|
|
if (!browser) {
|
|
|
|
LOG(ERROR) << "No associated browser in GetPdfPaperSize; using default "
|
|
|
|
"printing implementation.";
|
|
|
|
}
|
|
|
|
|
|
|
|
auto handler = GetPrintHandlerForBrowser(browser);
|
|
|
|
if (!handler) {
|
2022-07-21 19:26:10 +02:00
|
|
|
if (default_delegate_) {
|
|
|
|
size = default_delegate_->GetPdfPaperSize(context);
|
2022-04-15 21:55:23 +02:00
|
|
|
DCHECK(!size.IsEmpty());
|
2015-03-24 16:40:08 +01:00
|
|
|
}
|
2022-04-15 21:55:23 +02:00
|
|
|
} else {
|
|
|
|
const printing::PrintSettings& settings = context->settings();
|
|
|
|
CefSize cef_size = handler->GetPdfPaperSize(
|
|
|
|
browser.get(), settings.device_units_per_inch());
|
|
|
|
size.SetSize(cef_size.width, cef_size.height);
|
2015-03-24 16:40:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (size.IsEmpty()) {
|
2022-04-15 21:55:23 +02:00
|
|
|
LOG(ERROR) << "Empty size value returned in GetPdfPaperSize; PDF printing "
|
|
|
|
"will fail.";
|
2015-03-24 16:40:08 +01:00
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2022-07-21 19:26:10 +02:00
|
|
|
void CefPrintingContextLinuxDelegate::SetDefaultDelegate(
|
2023-01-12 17:49:21 +01:00
|
|
|
ui::PrintingContextLinuxDelegate* delegate) {
|
2022-07-21 19:26:10 +02:00
|
|
|
DCHECK(!default_delegate_);
|
|
|
|
default_delegate_ = delegate;
|
2022-04-15 21:55:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefPrintDialogLinux::CefPrintDialogLinux(PrintingContextLinux* context,
|
|
|
|
CefRefPtr<CefBrowserHostBase> browser,
|
|
|
|
CefRefPtr<CefPrintHandler> handler)
|
|
|
|
: context_(context), browser_(browser), handler_(handler) {
|
2022-10-12 23:53:06 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
2018-01-02 22:51:02 +01:00
|
|
|
DCHECK(context_);
|
|
|
|
DCHECK(browser_);
|
2022-04-15 21:55:23 +02:00
|
|
|
DCHECK(handler_);
|
2022-05-19 12:28:44 +02:00
|
|
|
|
|
|
|
// Paired with the ReleaseDialog() call.
|
|
|
|
AddRef();
|
2022-10-12 23:53:06 +02:00
|
|
|
|
|
|
|
handler->OnPrintStart(browser_.get());
|
2018-01-02 22:51:02 +01:00
|
|
|
}
|
2014-07-10 17:41:30 +02:00
|
|
|
|
|
|
|
CefPrintDialogLinux::~CefPrintDialogLinux() {
|
2018-01-02 22:51:02 +01:00
|
|
|
// It's not safe to dereference |context_| during the destruction of this
|
|
|
|
// object because the PrintJobWorker which owns |context_| may already have
|
|
|
|
// been deleted.
|
2014-07-10 17:41:30 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
2022-04-15 21:55:23 +02:00
|
|
|
handler_->OnPrintReset(browser_.get());
|
2014-07-10 17:41:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrintDialogLinux::UseDefaultSettings() {
|
2019-10-01 15:55:16 +02:00
|
|
|
UpdateSettings(std::make_unique<PrintSettings>(), true);
|
2014-07-10 17:41:30 +02:00
|
|
|
}
|
|
|
|
|
2019-10-01 15:55:16 +02:00
|
|
|
void CefPrintDialogLinux::UpdateSettings(
|
|
|
|
std::unique_ptr<PrintSettings> settings) {
|
|
|
|
UpdateSettings(std::move(settings), false);
|
2014-07-10 17:41:30 +02:00
|
|
|
}
|
|
|
|
|
2023-09-15 21:51:43 +02:00
|
|
|
#if BUILDFLAG(ENABLE_OOP_PRINTING_NO_OOP_BASIC_PRINT_DIALOG)
|
|
|
|
void CefPrintDialogLinux::LoadPrintSettings(
|
|
|
|
const printing::PrintSettings& settings) {
|
|
|
|
// TODO(linux): Need to read data from |settings.system_print_dialog_data()|?
|
|
|
|
UseDefaultSettings();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-07-10 17:41:30 +02:00
|
|
|
void CefPrintDialogLinux::ShowDialog(
|
|
|
|
gfx::NativeView parent_view,
|
|
|
|
bool has_selection,
|
2018-03-20 21:15:08 +01:00
|
|
|
PrintingContextLinux::PrintSettingsCallback callback) {
|
2014-07-10 17:41:30 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
callback_ = std::move(callback);
|
2014-07-10 17:41:30 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefPrintDialogCallbackImpl> callback_impl(
|
|
|
|
new CefPrintDialogCallbackImpl(this));
|
|
|
|
|
2018-01-02 22:51:02 +01:00
|
|
|
if (!handler_->OnPrintDialog(browser_.get(), has_selection,
|
2017-06-15 17:27:56 +02:00
|
|
|
callback_impl.get())) {
|
2014-07-10 17:41:30 +02:00
|
|
|
callback_impl->Disconnect();
|
|
|
|
OnPrintCancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
void CefPrintDialogLinux::PrintDocument(
|
|
|
|
const printing::MetafilePlayer& metafile,
|
2021-04-21 00:52:34 +02:00
|
|
|
const std::u16string& document_name) {
|
2014-07-10 17:41:30 +02:00
|
|
|
// This runs on the print worker thread, does not block the UI thread.
|
|
|
|
DCHECK(!CEF_CURRENTLY_ON_UIT());
|
|
|
|
|
|
|
|
// The document printing tasks can outlive the PrintingContext that created
|
|
|
|
// this dialog.
|
|
|
|
AddRef();
|
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
bool success = base::CreateTemporaryFile(&path_to_pdf_);
|
2014-07-10 17:41:30 +02:00
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
if (success) {
|
|
|
|
base::File file;
|
|
|
|
file.Initialize(path_to_pdf_,
|
|
|
|
base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
|
|
|
|
success = metafile.SaveTo(&file);
|
|
|
|
file.Close();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!success) {
|
2020-08-29 00:39:23 +02:00
|
|
|
base::DeleteFile(path_to_pdf_);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2014-07-10 17:41:30 +02:00
|
|
|
}
|
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
if (!success) {
|
|
|
|
LOG(ERROR) << "Saving metafile failed";
|
2014-07-10 17:41:30 +02:00
|
|
|
// Matches AddRef() above.
|
|
|
|
Release();
|
2014-09-27 01:48:19 +02:00
|
|
|
return;
|
2014-07-10 17:41:30 +02:00
|
|
|
}
|
2014-09-27 01:48:19 +02:00
|
|
|
|
|
|
|
// No errors, continue printing.
|
2021-06-04 03:34:56 +02:00
|
|
|
CEF_POST_TASK(
|
|
|
|
CEF_UIT, base::BindOnce(&CefPrintDialogLinux::SendDocumentToPrinter, this,
|
|
|
|
document_name));
|
2014-07-10 17:41:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrintDialogLinux::ReleaseDialog() {
|
2022-05-19 12:28:44 +02:00
|
|
|
context_ = nullptr;
|
2014-07-10 17:41:30 +02:00
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
2019-10-01 15:55:16 +02:00
|
|
|
bool CefPrintDialogLinux::UpdateSettings(
|
|
|
|
std::unique_ptr<PrintSettings> settings,
|
|
|
|
bool get_defaults) {
|
2014-07-10 17:41:30 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
CefRefPtr<CefPrintSettingsImpl> settings_impl(
|
2019-10-01 15:55:16 +02:00
|
|
|
new CefPrintSettingsImpl(std::move(settings), false));
|
2018-01-02 22:51:02 +01:00
|
|
|
handler_->OnPrintSettings(browser_.get(), settings_impl.get(), get_defaults);
|
2014-07-10 17:41:30 +02:00
|
|
|
|
2019-10-01 15:55:16 +02:00
|
|
|
context_->InitWithSettings(settings_impl->TakeOwnership());
|
2014-07-10 17:41:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrintDialogLinux::SendDocumentToPrinter(
|
2021-04-21 00:52:34 +02:00
|
|
|
const std::u16string& document_name) {
|
2014-07-10 17:41:30 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
if (!handler_.get()) {
|
2014-07-10 17:41:30 +02:00
|
|
|
OnJobCompleted();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefPrintJobCallbackImpl> callback_impl(
|
|
|
|
new CefPrintJobCallbackImpl(this));
|
|
|
|
|
2018-01-02 22:51:02 +01:00
|
|
|
if (!handler_->OnPrintJob(browser_.get(), document_name, path_to_pdf_.value(),
|
2014-07-10 17:41:30 +02:00
|
|
|
callback_impl.get())) {
|
|
|
|
callback_impl->Disconnect();
|
|
|
|
OnJobCompleted();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrintDialogLinux::OnPrintContinue(
|
|
|
|
CefRefPtr<CefPrintSettings> settings) {
|
2019-10-01 15:55:16 +02:00
|
|
|
CefPrintSettingsImpl* impl =
|
|
|
|
static_cast<CefPrintSettingsImpl*>(settings.get());
|
|
|
|
context_->InitWithSettings(impl->TakeOwnership());
|
2021-10-19 00:17:16 +02:00
|
|
|
std::move(callback_).Run(printing::mojom::ResultCode::kSuccess);
|
2014-07-10 17:41:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrintDialogLinux::OnPrintCancel() {
|
2021-10-19 00:17:16 +02:00
|
|
|
std::move(callback_).Run(printing::mojom::ResultCode::kCanceled);
|
2014-07-10 17:41:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrintDialogLinux::OnJobCompleted() {
|
2022-05-19 12:28:44 +02:00
|
|
|
CEF_POST_BACKGROUND_TASK(base::GetDeleteFileCallback(path_to_pdf_));
|
2014-07-10 17:41:30 +02:00
|
|
|
|
|
|
|
// Printing finished. Matches AddRef() in PrintDocument();
|
|
|
|
Release();
|
|
|
|
}
|