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
|
|
|
|
|
|
|
#include "base/bind.h"
|
2014-11-12 20:25:15 +01:00
|
|
|
#include "base/files/file_util.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;
|
|
|
|
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(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
|
|
|
|
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);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefPrintJobCallbackImpl);
|
|
|
|
};
|
|
|
|
|
|
|
|
// static
|
|
|
|
printing::PrintDialogGtkInterface* CefPrintDialogLinux::CreatePrintDialog(
|
|
|
|
PrintingContextLinux* context) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
return new CefPrintDialogLinux(context);
|
|
|
|
}
|
|
|
|
|
2015-03-24 16:40:08 +01:00
|
|
|
// static
|
|
|
|
gfx::Size CefPrintDialogLinux::GetPdfPaperSize(
|
|
|
|
printing::PrintingContextLinux* context) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
gfx::Size size;
|
|
|
|
|
2021-08-19 23:07:44 +02:00
|
|
|
auto browser = extensions::GetOwnerBrowserForGlobalId(
|
|
|
|
frame_util::MakeGlobalId(context->render_process_id(),
|
|
|
|
context->render_frame_id()),
|
|
|
|
nullptr);
|
2021-03-30 18:16:51 +02:00
|
|
|
DCHECK(browser);
|
|
|
|
if (browser && browser->GetClient()) {
|
|
|
|
if (auto handler = browser->GetClient()->GetPrintHandler()) {
|
|
|
|
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()) {
|
|
|
|
LOG(ERROR) << "Empty size value returned in GetPdfPaperSize; "
|
|
|
|
"PDF printing will fail.";
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2015-10-12 22:45:14 +02:00
|
|
|
// static
|
2020-10-08 21:54:42 +02:00
|
|
|
void CefPrintDialogLinux::OnPrintStart(CefRefPtr<CefBrowserHostBase> browser) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
DCHECK(browser);
|
2021-03-30 18:16:51 +02:00
|
|
|
if (browser && browser->GetClient()) {
|
|
|
|
if (auto handler = browser->GetClient()->GetPrintHandler()) {
|
|
|
|
handler->OnPrintStart(browser.get());
|
2020-10-08 21:54:42 +02:00
|
|
|
}
|
2015-10-12 22:45:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 17:41:30 +02:00
|
|
|
CefPrintDialogLinux::CefPrintDialogLinux(PrintingContextLinux* context)
|
2018-01-02 22:51:02 +01:00
|
|
|
: context_(context) {
|
|
|
|
DCHECK(context_);
|
2021-08-19 23:07:44 +02:00
|
|
|
browser_ = extensions::GetOwnerBrowserForGlobalId(
|
|
|
|
frame_util::MakeGlobalId(context_->render_process_id(),
|
|
|
|
context_->render_frame_id()),
|
|
|
|
nullptr);
|
2018-01-02 22:51:02 +01:00
|
|
|
DCHECK(browser_);
|
|
|
|
}
|
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();
|
|
|
|
ReleaseHandler();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
SetHandler();
|
2014-09-27 01:48:19 +02:00
|
|
|
if (!handler_.get()) {
|
2021-10-19 00:17:16 +02:00
|
|
|
std::move(callback).Run(printing::mojom::ResultCode::kCanceled);
|
2014-07-10 17:41:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
if (!success)
|
2020-08-29 00:39:23 +02:00
|
|
|
base::DeleteFile(path_to_pdf_);
|
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::AddRefToDialog() {
|
|
|
|
AddRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrintDialogLinux::ReleaseDialog() {
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrintDialogLinux::SetHandler() {
|
2015-03-24 16:40:08 +01:00
|
|
|
if (handler_.get())
|
|
|
|
return;
|
2014-07-10 17:41:30 +02:00
|
|
|
|
2021-03-30 18:16:51 +02:00
|
|
|
if (browser_ && browser_->GetClient()) {
|
|
|
|
handler_ = browser_->GetClient()->GetPrintHandler();
|
2014-07-10 17:41:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrintDialogLinux::ReleaseHandler() {
|
2014-09-27 01:48:19 +02:00
|
|
|
if (handler_.get()) {
|
2018-01-02 22:51:02 +01:00
|
|
|
handler_->OnPrintReset(browser_.get());
|
2020-01-15 14:36:24 +01:00
|
|
|
handler_ = nullptr;
|
2014-07-10 17:41:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
SetHandler();
|
2014-09-27 01:48:19 +02:00
|
|
|
if (!handler_.get())
|
2014-07-10 17:41:30 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
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() {
|
2018-03-20 21:15:08 +01:00
|
|
|
CEF_POST_BACKGROUND_TASK(
|
2020-07-08 19:23:29 +02:00
|
|
|
base::BindOnce(base::GetDeleteFileCallback(), path_to_pdf_));
|
2014-07-10 17:41:30 +02:00
|
|
|
|
|
|
|
// Printing finished. Matches AddRef() in PrintDocument();
|
|
|
|
Release();
|
|
|
|
}
|