2021-05-19 23:15:47 +00:00
|
|
|
// Copyright 2020 Microsoft, Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "shell/browser/printing/print_view_manager_electron.h"
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
2022-07-20 11:03:34 +00:00
|
|
|
#include "base/bind.h"
|
2021-05-19 23:15:47 +00:00
|
|
|
#include "build/build_config.h"
|
2022-05-31 06:21:25 +00:00
|
|
|
#include "components/printing/browser/print_to_pdf/pdf_print_utils.h"
|
|
|
|
#include "printing/mojom/print.mojom.h"
|
|
|
|
#include "printing/page_range.h"
|
|
|
|
#include "third_party/abseil-cpp/absl/types/variant.h"
|
|
|
|
|
|
|
|
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
|
|
|
|
#include "mojo/public/cpp/bindings/message.h"
|
|
|
|
#endif
|
2021-05-19 23:15:47 +00:00
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
2022-05-31 06:21:25 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
|
|
|
|
constexpr char kInvalidUpdatePrintSettingsCall[] =
|
|
|
|
"Invalid UpdatePrintSettings Call";
|
|
|
|
constexpr char kInvalidSetupScriptedPrintPreviewCall[] =
|
|
|
|
"Invalid SetupScriptedPrintPreview Call";
|
|
|
|
constexpr char kInvalidShowScriptedPrintPreviewCall[] =
|
|
|
|
"Invalid ShowScriptedPrintPreview Call";
|
|
|
|
constexpr char kInvalidRequestPrintPreviewCall[] =
|
|
|
|
"Invalid RequestPrintPreview Call";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// This file subclasses printing::PrintViewManagerBase
|
|
|
|
// but the implementations are duplicated from
|
|
|
|
// components/printing/browser/print_to_pdf/pdf_print_manager.cc.
|
|
|
|
|
2021-05-19 23:15:47 +00:00
|
|
|
PrintViewManagerElectron::PrintViewManagerElectron(
|
|
|
|
content::WebContents* web_contents)
|
2022-05-31 06:21:25 +00:00
|
|
|
: printing::PrintViewManagerBase(web_contents),
|
2022-01-10 22:31:39 +00:00
|
|
|
content::WebContentsUserData<PrintViewManagerElectron>(*web_contents) {}
|
2021-05-19 23:15:47 +00:00
|
|
|
|
2021-06-04 04:16:13 +00:00
|
|
|
PrintViewManagerElectron::~PrintViewManagerElectron() = default;
|
2021-05-19 23:15:47 +00:00
|
|
|
|
2021-08-24 00:52:17 +00:00
|
|
|
// static
|
|
|
|
void PrintViewManagerElectron::BindPrintManagerHost(
|
|
|
|
mojo::PendingAssociatedReceiver<printing::mojom::PrintManagerHost> receiver,
|
|
|
|
content::RenderFrameHost* rfh) {
|
|
|
|
auto* web_contents = content::WebContents::FromRenderFrameHost(rfh);
|
|
|
|
if (!web_contents)
|
|
|
|
return;
|
2022-05-31 06:21:25 +00:00
|
|
|
|
2021-08-24 00:52:17 +00:00
|
|
|
auto* print_manager = PrintViewManagerElectron::FromWebContents(web_contents);
|
|
|
|
if (!print_manager)
|
|
|
|
return;
|
2022-05-31 06:21:25 +00:00
|
|
|
|
2021-08-24 00:52:17 +00:00
|
|
|
print_manager->BindReceiver(std::move(receiver), rfh);
|
|
|
|
}
|
|
|
|
|
2022-05-31 06:21:25 +00:00
|
|
|
// static
|
|
|
|
std::string PrintViewManagerElectron::PrintResultToString(PrintResult result) {
|
|
|
|
switch (result) {
|
2022-08-17 18:35:53 +00:00
|
|
|
case kPrintSuccess:
|
2022-05-31 06:21:25 +00:00
|
|
|
return std::string(); // no error message
|
2022-08-17 18:35:53 +00:00
|
|
|
case kPrintFailure:
|
2022-05-31 06:21:25 +00:00
|
|
|
return "Printing failed";
|
2022-08-17 18:35:53 +00:00
|
|
|
case kInvalidPrinterSettings:
|
2022-05-31 06:21:25 +00:00
|
|
|
return "Show invalid printer settings error";
|
2022-08-17 18:35:53 +00:00
|
|
|
case kInvalidMemoryHandle:
|
2022-05-31 06:21:25 +00:00
|
|
|
return "Invalid memory handle";
|
2022-08-17 18:35:53 +00:00
|
|
|
case kMetafileMapError:
|
2022-05-31 06:21:25 +00:00
|
|
|
return "Map to shared memory error";
|
2022-08-17 18:35:53 +00:00
|
|
|
case kMetafileInvalidHeader:
|
2022-05-31 06:21:25 +00:00
|
|
|
return "Invalid metafile header";
|
2022-08-17 18:35:53 +00:00
|
|
|
case kMetafileGetDataError:
|
2022-05-31 06:21:25 +00:00
|
|
|
return "Get data from metafile error";
|
2022-08-17 18:35:53 +00:00
|
|
|
case kSimultaneousPrintActive:
|
2022-05-31 06:21:25 +00:00
|
|
|
return "The previous printing job hasn't finished";
|
2022-08-17 18:35:53 +00:00
|
|
|
case kPageRangeSyntaxError:
|
2022-05-31 06:21:25 +00:00
|
|
|
return "Page range syntax error";
|
2022-08-17 18:35:53 +00:00
|
|
|
case kPageRangeInvalidRange:
|
2022-05-31 06:21:25 +00:00
|
|
|
return "Page range is invalid (start > end)";
|
2022-08-17 18:35:53 +00:00
|
|
|
case kPageCountExceeded:
|
2022-05-31 06:21:25 +00:00
|
|
|
return "Page range exceeds page count";
|
2022-09-07 07:46:37 +00:00
|
|
|
case kPrintingInProgress:
|
|
|
|
return "Page is already being printed";
|
2022-05-31 06:21:25 +00:00
|
|
|
default:
|
|
|
|
NOTREACHED();
|
|
|
|
return "Unknown PrintResult";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintViewManagerElectron::PrintToPdf(
|
|
|
|
content::RenderFrameHost* rfh,
|
|
|
|
const std::string& page_ranges,
|
|
|
|
printing::mojom::PrintPagesParamsPtr print_pages_params,
|
|
|
|
PrintToPDFCallback callback) {
|
|
|
|
DCHECK(callback);
|
|
|
|
|
|
|
|
if (callback_) {
|
2022-08-17 18:35:53 +00:00
|
|
|
std::move(callback).Run(kSimultaneousPrintActive,
|
2022-05-31 06:21:25 +00:00
|
|
|
base::MakeRefCounted<base::RefCountedString>());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rfh->IsRenderFrameLive()) {
|
2022-08-17 18:35:53 +00:00
|
|
|
std::move(callback).Run(kPrintFailure,
|
2022-05-31 06:21:25 +00:00
|
|
|
base::MakeRefCounted<base::RefCountedString>());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-17 18:35:53 +00:00
|
|
|
absl::variant<printing::PageRanges, print_to_pdf::PdfPrintResult>
|
2022-05-31 06:21:25 +00:00
|
|
|
parsed_ranges = print_to_pdf::TextPageRangesToPageRanges(page_ranges);
|
2022-08-17 18:35:53 +00:00
|
|
|
if (absl::holds_alternative<print_to_pdf::PdfPrintResult>(parsed_ranges)) {
|
|
|
|
DCHECK_NE(absl::get<print_to_pdf::PdfPrintResult>(parsed_ranges),
|
|
|
|
print_to_pdf::PdfPrintResult::kPrintSuccess);
|
|
|
|
std::move(callback).Run(
|
|
|
|
static_cast<PrintResult>(
|
|
|
|
absl::get<print_to_pdf::PdfPrintResult>(parsed_ranges)),
|
|
|
|
base::MakeRefCounted<base::RefCountedString>());
|
2022-05-31 06:21:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printing_rfh_ = rfh;
|
|
|
|
print_pages_params->pages = absl::get<printing::PageRanges>(parsed_ranges);
|
2022-10-18 13:46:19 +00:00
|
|
|
headless_jobs_.emplace_back(print_pages_params->params->document_cookie);
|
2022-05-31 06:21:25 +00:00
|
|
|
callback_ = std::move(callback);
|
|
|
|
|
2022-07-20 11:03:34 +00:00
|
|
|
// There is no need for a weak pointer here since the mojo proxy is held
|
|
|
|
// in the base class. If we're gone, mojo will discard the callback.
|
|
|
|
GetPrintRenderFrame(rfh)->PrintWithParams(
|
|
|
|
std::move(print_pages_params),
|
|
|
|
base::BindOnce(&PrintViewManagerElectron::OnDidPrintWithParams,
|
|
|
|
base::Unretained(this)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintViewManagerElectron::OnDidPrintWithParams(
|
|
|
|
printing::mojom::PrintWithParamsResultPtr result) {
|
|
|
|
if (result->is_failure_reason()) {
|
|
|
|
switch (result->get_failure_reason()) {
|
|
|
|
case printing::mojom::PrintFailureReason::kGeneralFailure:
|
2022-08-17 18:35:53 +00:00
|
|
|
FailJob(kPrintFailure);
|
2022-07-20 11:03:34 +00:00
|
|
|
return;
|
|
|
|
case printing::mojom::PrintFailureReason::kInvalidPageRange:
|
2022-08-17 18:35:53 +00:00
|
|
|
FailJob(kPageCountExceeded);
|
2022-07-20 11:03:34 +00:00
|
|
|
return;
|
2022-09-07 07:46:37 +00:00
|
|
|
case printing::mojom::PrintFailureReason::kPrintingInProgress:
|
|
|
|
FailJob(kPrintingInProgress);
|
|
|
|
return;
|
2022-07-20 11:03:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 13:46:19 +00:00
|
|
|
printing::mojom::DidPrintDocumentParamsPtr& params = result->get_params();
|
|
|
|
|
|
|
|
auto& content = *params->content;
|
2022-07-20 11:03:34 +00:00
|
|
|
if (!content.metafile_data_region.IsValid()) {
|
2022-08-17 18:35:53 +00:00
|
|
|
FailJob(kInvalidMemoryHandle);
|
2022-07-20 11:03:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-08-17 18:35:53 +00:00
|
|
|
|
2022-07-20 11:03:34 +00:00
|
|
|
base::ReadOnlySharedMemoryMapping map = content.metafile_data_region.Map();
|
|
|
|
if (!map.IsValid()) {
|
2022-08-17 18:35:53 +00:00
|
|
|
FailJob(kMetafileMapError);
|
2022-07-20 11:03:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-08-17 18:35:53 +00:00
|
|
|
|
|
|
|
std::string data =
|
|
|
|
std::string(static_cast<const char*>(map.memory()), map.size());
|
|
|
|
std::move(callback_).Run(kPrintSuccess,
|
|
|
|
base::RefCountedString::TakeString(&data));
|
2022-10-18 13:46:19 +00:00
|
|
|
base::Erase(headless_jobs_, params->document_cookie);
|
2022-08-17 18:35:53 +00:00
|
|
|
Reset();
|
2022-05-31 06:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintViewManagerElectron::GetDefaultPrintSettings(
|
|
|
|
GetDefaultPrintSettingsCallback callback) {
|
|
|
|
if (printing_rfh_) {
|
|
|
|
LOG(ERROR) << "Scripted print is not supported";
|
|
|
|
std::move(callback).Run(printing::mojom::PrintParams::New());
|
|
|
|
} else {
|
|
|
|
PrintViewManagerBase::GetDefaultPrintSettings(std::move(callback));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintViewManagerElectron::ScriptedPrint(
|
|
|
|
printing::mojom::ScriptedPrintParamsPtr params,
|
|
|
|
ScriptedPrintCallback callback) {
|
|
|
|
auto entry =
|
|
|
|
std::find(headless_jobs_.begin(), headless_jobs_.end(), params->cookie);
|
|
|
|
if (entry == headless_jobs_.end()) {
|
|
|
|
PrintViewManagerBase::ScriptedPrint(std::move(params), std::move(callback));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto default_param = printing::mojom::PrintPagesParams::New();
|
|
|
|
default_param->params = printing::mojom::PrintParams::New();
|
|
|
|
LOG(ERROR) << "Scripted print is not supported";
|
|
|
|
std::move(callback).Run(std::move(default_param), /*cancelled*/ false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintViewManagerElectron::ShowInvalidPrinterSettingsError() {
|
2022-07-19 12:46:08 +00:00
|
|
|
if (headless_jobs_.size() == 0) {
|
|
|
|
PrintViewManagerBase::ShowInvalidPrinterSettingsError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-17 18:35:53 +00:00
|
|
|
FailJob(kInvalidPrinterSettings);
|
2022-05-31 06:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
|
|
|
|
void PrintViewManagerElectron::UpdatePrintSettings(
|
|
|
|
int32_t cookie,
|
|
|
|
base::Value::Dict job_settings,
|
|
|
|
UpdatePrintSettingsCallback callback) {
|
|
|
|
auto entry = std::find(headless_jobs_.begin(), headless_jobs_.end(), cookie);
|
|
|
|
if (entry == headless_jobs_.end()) {
|
|
|
|
PrintViewManagerBase::UpdatePrintSettings(cookie, std::move(job_settings),
|
|
|
|
std::move(callback));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mojo::ReportBadMessage(kInvalidUpdatePrintSettingsCall);
|
|
|
|
}
|
|
|
|
|
2021-05-19 23:15:47 +00:00
|
|
|
void PrintViewManagerElectron::SetupScriptedPrintPreview(
|
|
|
|
SetupScriptedPrintPreviewCallback callback) {
|
2022-05-31 06:21:25 +00:00
|
|
|
mojo::ReportBadMessage(kInvalidSetupScriptedPrintPreviewCall);
|
2021-05-19 23:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintViewManagerElectron::ShowScriptedPrintPreview(
|
2022-05-31 06:21:25 +00:00
|
|
|
bool source_is_modifiable) {
|
|
|
|
mojo::ReportBadMessage(kInvalidShowScriptedPrintPreviewCall);
|
|
|
|
}
|
2021-05-19 23:15:47 +00:00
|
|
|
|
|
|
|
void PrintViewManagerElectron::RequestPrintPreview(
|
2022-05-31 06:21:25 +00:00
|
|
|
printing::mojom::RequestPrintPreviewParamsPtr params) {
|
|
|
|
mojo::ReportBadMessage(kInvalidRequestPrintPreviewCall);
|
|
|
|
}
|
2021-05-19 23:15:47 +00:00
|
|
|
|
|
|
|
void PrintViewManagerElectron::CheckForCancel(int32_t preview_ui_id,
|
|
|
|
int32_t request_id,
|
|
|
|
CheckForCancelCallback callback) {
|
2022-05-31 06:21:25 +00:00
|
|
|
std::move(callback).Run(false);
|
|
|
|
}
|
|
|
|
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
|
|
|
|
|
|
|
|
void PrintViewManagerElectron::RenderFrameDeleted(
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
|
|
|
PrintViewManagerBase::RenderFrameDeleted(render_frame_host);
|
|
|
|
|
|
|
|
if (printing_rfh_ != render_frame_host)
|
|
|
|
return;
|
|
|
|
|
2022-08-17 18:35:53 +00:00
|
|
|
FailJob(kPrintFailure);
|
2022-05-31 06:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintViewManagerElectron::DidGetPrintedPagesCount(int32_t cookie,
|
|
|
|
uint32_t number_pages) {
|
|
|
|
auto entry = std::find(headless_jobs_.begin(), headless_jobs_.end(), cookie);
|
|
|
|
if (entry == headless_jobs_.end()) {
|
|
|
|
PrintViewManagerBase::DidGetPrintedPagesCount(cookie, number_pages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintViewManagerElectron::Reset() {
|
|
|
|
printing_rfh_ = nullptr;
|
|
|
|
callback_.Reset();
|
|
|
|
data_.clear();
|
|
|
|
}
|
|
|
|
|
2022-08-17 18:35:53 +00:00
|
|
|
void PrintViewManagerElectron::FailJob(PrintResult result) {
|
|
|
|
DCHECK_NE(result, kPrintSuccess);
|
2022-05-31 06:21:25 +00:00
|
|
|
if (callback_) {
|
|
|
|
std::move(callback_).Run(result,
|
2022-08-17 18:35:53 +00:00
|
|
|
base::MakeRefCounted<base::RefCountedString>());
|
2022-05-31 06:21:25 +00:00
|
|
|
}
|
2022-08-17 18:35:53 +00:00
|
|
|
|
|
|
|
Reset();
|
2021-05-19 23:15:47 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 18:51:36 +00:00
|
|
|
WEB_CONTENTS_USER_DATA_KEY_IMPL(PrintViewManagerElectron);
|
2021-05-19 23:15:47 +00:00
|
|
|
|
|
|
|
} // namespace electron
|