refactor: printToPDF should be headless (#33654)
This commit is contained in:
parent
0d69067dee
commit
93b39b92b5
17 changed files with 648 additions and 414 deletions
|
@ -25,6 +25,7 @@
|
|||
#include "base/threading/thread_task_runner_handle.h"
|
||||
#include "base/values.h"
|
||||
#include "chrome/browser/browser_process.h"
|
||||
#include "chrome/browser/printing/print_view_manager_base.h"
|
||||
#include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h"
|
||||
#include "chrome/browser/ui/views/eye_dropper/eye_dropper.h"
|
||||
#include "chrome/common/pref_names.h"
|
||||
|
@ -165,9 +166,10 @@
|
|||
|
||||
#if BUILDFLAG(ENABLE_PRINTING)
|
||||
#include "components/printing/browser/print_manager_utils.h"
|
||||
#include "components/printing/browser/print_to_pdf/pdf_print_utils.h"
|
||||
#include "printing/backend/print_backend.h" // nogncheck
|
||||
#include "printing/mojom/print.mojom.h" // nogncheck
|
||||
#include "shell/browser/printing/print_preview_message_handler.h"
|
||||
#include "printing/page_range.h"
|
||||
#include "shell/browser/printing/print_view_manager_electron.h"
|
||||
|
||||
#if BUILDFLAG(IS_WIN)
|
||||
|
@ -919,10 +921,7 @@ void WebContents::InitWithWebContents(
|
|||
web_contents->SetDelegate(this);
|
||||
|
||||
#if BUILDFLAG(ENABLE_PRINTING)
|
||||
PrintPreviewMessageHandler::CreateForWebContents(web_contents.get());
|
||||
PrintViewManagerElectron::CreateForWebContents(web_contents.get());
|
||||
printing::CreateCompositeClientIfNeeded(web_contents.get(),
|
||||
browser_context->GetUserAgent());
|
||||
#endif
|
||||
|
||||
#if BUILDFLAG(ENABLE_PDF_VIEWER)
|
||||
|
@ -2807,14 +2806,87 @@ void WebContents::Print(gin::Arguments* args) {
|
|||
std::move(callback), device_name, silent));
|
||||
}
|
||||
|
||||
v8::Local<v8::Promise> WebContents::PrintToPDF(base::DictionaryValue settings) {
|
||||
// Partially duplicated and modified from
|
||||
// headless/lib/browser/protocol/page_handler.cc;l=41
|
||||
v8::Local<v8::Promise> WebContents::PrintToPDF(const base::Value& settings) {
|
||||
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
||||
gin_helper::Promise<v8::Local<v8::Value>> promise(isolate);
|
||||
v8::Local<v8::Promise> handle = promise.GetHandle();
|
||||
PrintPreviewMessageHandler::FromWebContents(web_contents())
|
||||
->PrintToPDF(std::move(settings), std::move(promise));
|
||||
|
||||
// This allows us to track headless printing calls.
|
||||
auto unique_id = settings.GetDict().FindInt(printing::kPreviewRequestID);
|
||||
auto landscape = settings.GetDict().FindBool("landscape");
|
||||
auto display_header_footer =
|
||||
settings.GetDict().FindBool("displayHeaderFooter");
|
||||
auto print_background = settings.GetDict().FindBool("shouldPrintBackgrounds");
|
||||
auto scale = settings.GetDict().FindDouble("scale");
|
||||
auto paper_width = settings.GetDict().FindInt("paperWidth");
|
||||
auto paper_height = settings.GetDict().FindInt("paperHeight");
|
||||
auto margin_top = settings.GetDict().FindIntByDottedPath("margins.top");
|
||||
auto margin_bottom = settings.GetDict().FindIntByDottedPath("margins.bottom");
|
||||
auto margin_left = settings.GetDict().FindIntByDottedPath("margins.left");
|
||||
auto margin_right = settings.GetDict().FindIntByDottedPath("margins.right");
|
||||
auto page_ranges = *settings.GetDict().FindString("pageRanges");
|
||||
auto header_template = *settings.GetDict().FindString("headerTemplate");
|
||||
auto footer_template = *settings.GetDict().FindString("footerTemplate");
|
||||
auto prefer_css_page_size = settings.GetDict().FindBool("preferCSSPageSize");
|
||||
|
||||
absl::variant<printing::mojom::PrintPagesParamsPtr, std::string>
|
||||
print_pages_params = print_to_pdf::GetPrintPagesParams(
|
||||
web_contents()->GetMainFrame()->GetLastCommittedURL(), landscape,
|
||||
display_header_footer, print_background, scale, paper_width,
|
||||
paper_height, margin_top, margin_bottom, margin_left, margin_right,
|
||||
absl::make_optional(header_template),
|
||||
absl::make_optional(footer_template), prefer_css_page_size);
|
||||
|
||||
if (absl::holds_alternative<std::string>(print_pages_params)) {
|
||||
auto error = absl::get<std::string>(print_pages_params);
|
||||
promise.RejectWithErrorMessage("Invalid print parameters: " + error);
|
||||
return handle;
|
||||
}
|
||||
|
||||
auto* manager = PrintViewManagerElectron::FromWebContents(web_contents());
|
||||
if (!manager) {
|
||||
promise.RejectWithErrorMessage("Failed to find print manager");
|
||||
return handle;
|
||||
}
|
||||
|
||||
auto params = std::move(
|
||||
absl::get<printing::mojom::PrintPagesParamsPtr>(print_pages_params));
|
||||
params->params->document_cookie = unique_id.value_or(0);
|
||||
|
||||
manager->PrintToPdf(web_contents()->GetMainFrame(), page_ranges,
|
||||
std::move(params),
|
||||
base::BindOnce(&WebContents::OnPDFCreated, GetWeakPtr(),
|
||||
std::move(promise)));
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
void WebContents::OnPDFCreated(
|
||||
gin_helper::Promise<v8::Local<v8::Value>> promise,
|
||||
PrintViewManagerElectron::PrintResult print_result,
|
||||
scoped_refptr<base::RefCountedMemory> data) {
|
||||
if (print_result != PrintViewManagerElectron::PrintResult::PRINT_SUCCESS) {
|
||||
promise.RejectWithErrorMessage(
|
||||
"Failed to generate PDF: " +
|
||||
PrintViewManagerElectron::PrintResultToString(print_result));
|
||||
return;
|
||||
}
|
||||
|
||||
v8::Isolate* isolate = promise.isolate();
|
||||
gin_helper::Locker locker(isolate);
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
v8::Context::Scope context_scope(
|
||||
v8::Local<v8::Context>::New(isolate, promise.GetContext()));
|
||||
|
||||
v8::Local<v8::Value> buffer =
|
||||
node::Buffer::Copy(isolate, reinterpret_cast<const char*>(data->front()),
|
||||
data->size())
|
||||
.ToLocalChecked();
|
||||
|
||||
promise.Resolve(buffer);
|
||||
}
|
||||
#endif
|
||||
|
||||
void WebContents::AddWorkSpace(gin::Arguments* args,
|
||||
|
|
|
@ -47,7 +47,6 @@
|
|||
#include "ui/gfx/image/image.h"
|
||||
|
||||
#if BUILDFLAG(ENABLE_PRINTING)
|
||||
#include "shell/browser/printing/print_preview_message_handler.h"
|
||||
#include "shell/browser/printing/print_view_manager_electron.h"
|
||||
#endif
|
||||
|
||||
|
@ -231,7 +230,10 @@ class WebContents : public ExclusiveAccessContext,
|
|||
std::pair<std::string, std::u16string> info);
|
||||
void Print(gin::Arguments* args);
|
||||
// Print current page as PDF.
|
||||
v8::Local<v8::Promise> PrintToPDF(base::DictionaryValue settings);
|
||||
v8::Local<v8::Promise> PrintToPDF(const base::Value& settings);
|
||||
void OnPDFCreated(gin_helper::Promise<v8::Local<v8::Value>> promise,
|
||||
PrintViewManagerElectron::PrintResult print_result,
|
||||
scoped_refptr<base::RefCountedMemory> data);
|
||||
#endif
|
||||
|
||||
void SetNextChildWebPreferences(const gin_helper::Dictionary);
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
|
||||
#if BUILDFLAG(ENABLE_PRINTING)
|
||||
#include "components/printing/browser/print_manager_utils.h"
|
||||
#include "shell/browser/printing/print_preview_message_handler.h"
|
||||
#include "shell/browser/printing/print_view_manager_electron.h"
|
||||
#endif
|
||||
|
||||
|
@ -86,7 +85,6 @@ MessagingDelegate* ElectronExtensionsAPIClient::GetMessagingDelegate() {
|
|||
void ElectronExtensionsAPIClient::AttachWebContentsHelpers(
|
||||
content::WebContents* web_contents) const {
|
||||
#if BUILDFLAG(ENABLE_PRINTING)
|
||||
electron::PrintPreviewMessageHandler::CreateForWebContents(web_contents);
|
||||
electron::PrintViewManagerElectron::CreateForWebContents(web_contents);
|
||||
#endif
|
||||
|
||||
|
|
|
@ -1,108 +0,0 @@
|
|||
// Copyright (c) 2018 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ELECTRON_SHELL_BROWSER_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_
|
||||
#define ELECTRON_SHELL_BROWSER_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "base/memory/ref_counted_memory.h"
|
||||
#include "base/memory/weak_ptr.h"
|
||||
#include "components/printing/common/print.mojom.h"
|
||||
#include "components/services/print_compositor/public/mojom/print_compositor.mojom.h"
|
||||
#include "content/public/browser/web_contents_user_data.h"
|
||||
#include "mojo/public/cpp/bindings/associated_receiver.h"
|
||||
#include "mojo/public/cpp/bindings/associated_remote.h"
|
||||
#include "printing/mojom/print.mojom.h"
|
||||
#include "shell/common/gin_helper/promise.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
namespace content {
|
||||
class RenderFrameHost;
|
||||
}
|
||||
|
||||
namespace electron {
|
||||
|
||||
// Manages the print preview handling for a WebContents.
|
||||
class PrintPreviewMessageHandler
|
||||
: public printing::mojom::PrintPreviewUI,
|
||||
public content::WebContentsUserData<PrintPreviewMessageHandler> {
|
||||
public:
|
||||
~PrintPreviewMessageHandler() override;
|
||||
|
||||
// disable copy
|
||||
PrintPreviewMessageHandler(const PrintPreviewMessageHandler&) = delete;
|
||||
PrintPreviewMessageHandler& operator=(const PrintPreviewMessageHandler&) =
|
||||
delete;
|
||||
|
||||
void PrintToPDF(base::DictionaryValue options,
|
||||
gin_helper::Promise<v8::Local<v8::Value>> promise);
|
||||
|
||||
private:
|
||||
friend class content::WebContentsUserData<PrintPreviewMessageHandler>;
|
||||
|
||||
explicit PrintPreviewMessageHandler(content::WebContents* web_contents);
|
||||
|
||||
void OnCompositeDocumentToPdfDone(
|
||||
int32_t request_id,
|
||||
printing::mojom::PrintCompositor::Status status,
|
||||
base::ReadOnlySharedMemoryRegion region);
|
||||
void OnPrepareForDocumentToPdfDone(
|
||||
int32_t request_id,
|
||||
printing::mojom::PrintCompositor::Status status);
|
||||
void OnCompositePdfPageDone(int page_number,
|
||||
int document_cookie,
|
||||
int32_t request_id,
|
||||
printing::mojom::PrintCompositor::Status status,
|
||||
base::ReadOnlySharedMemoryRegion region);
|
||||
|
||||
// printing::mojo::PrintPreviewUI:
|
||||
void SetOptionsFromDocument(
|
||||
const printing::mojom::OptionsFromDocumentParamsPtr params,
|
||||
int32_t request_id) override {}
|
||||
void PrintPreviewFailed(int32_t document_cookie, int32_t request_id) override;
|
||||
void PrintPreviewCancelled(int32_t document_cookie,
|
||||
int32_t request_id) override;
|
||||
void PrinterSettingsInvalid(int32_t document_cookie,
|
||||
int32_t request_id) override {}
|
||||
void DidPrepareDocumentForPreview(int32_t document_cookie,
|
||||
int32_t request_id) override;
|
||||
void DidPreviewPage(printing::mojom::DidPreviewPageParamsPtr params,
|
||||
int32_t request_id) override;
|
||||
void MetafileReadyForPrinting(
|
||||
printing::mojom::DidPreviewDocumentParamsPtr params,
|
||||
int32_t request_id) override;
|
||||
void DidGetDefaultPageLayout(
|
||||
printing::mojom::PageSizeMarginsPtr page_layout_in_points,
|
||||
const gfx::Rect& printable_area_in_points,
|
||||
bool has_custom_page_size_style,
|
||||
int32_t request_id) override {}
|
||||
void DidStartPreview(printing::mojom::DidStartPreviewParamsPtr params,
|
||||
int32_t request_id) override {}
|
||||
|
||||
gin_helper::Promise<v8::Local<v8::Value>> GetPromise(int request_id);
|
||||
|
||||
void ResolvePromise(int request_id,
|
||||
scoped_refptr<base::RefCountedMemory> data_bytes);
|
||||
void RejectPromise(int request_id);
|
||||
|
||||
using PromiseMap = std::map<int, gin_helper::Promise<v8::Local<v8::Value>>>;
|
||||
PromiseMap promise_map_;
|
||||
|
||||
// TODO(clavin): refactor to use the WebContents provided by the
|
||||
// WebContentsUserData base class instead of storing a duplicate ref
|
||||
content::WebContents* web_contents_ = nullptr;
|
||||
|
||||
mojo::AssociatedRemote<printing::mojom::PrintRenderFrame> print_render_frame_;
|
||||
|
||||
mojo::AssociatedReceiver<printing::mojom::PrintPreviewUI> receiver_{this};
|
||||
|
||||
base::WeakPtrFactory<PrintPreviewMessageHandler> weak_ptr_factory_{this};
|
||||
|
||||
WEB_CONTENTS_USER_DATA_KEY_DECL();
|
||||
};
|
||||
|
||||
} // namespace electron
|
||||
|
||||
#endif // ELECTRON_SHELL_BROWSER_PRINTING_PRINT_PREVIEW_MESSAGE_HANDLER_H_
|
|
@ -7,13 +7,39 @@
|
|||
#include <utility>
|
||||
|
||||
#include "build/build_config.h"
|
||||
#include "content/public/browser/web_contents_user_data.h"
|
||||
#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
|
||||
|
||||
namespace electron {
|
||||
|
||||
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.
|
||||
|
||||
PrintViewManagerElectron::PrintViewManagerElectron(
|
||||
content::WebContents* web_contents)
|
||||
: PrintViewManagerBase(web_contents),
|
||||
: printing::PrintViewManagerBase(web_contents),
|
||||
content::WebContentsUserData<PrintViewManagerElectron>(*web_contents) {}
|
||||
|
||||
PrintViewManagerElectron::~PrintViewManagerElectron() = default;
|
||||
|
@ -25,26 +51,237 @@ void PrintViewManagerElectron::BindPrintManagerHost(
|
|||
auto* web_contents = content::WebContents::FromRenderFrameHost(rfh);
|
||||
if (!web_contents)
|
||||
return;
|
||||
|
||||
auto* print_manager = PrintViewManagerElectron::FromWebContents(web_contents);
|
||||
if (!print_manager)
|
||||
return;
|
||||
|
||||
print_manager->BindReceiver(std::move(receiver), rfh);
|
||||
}
|
||||
|
||||
// static
|
||||
std::string PrintViewManagerElectron::PrintResultToString(PrintResult result) {
|
||||
switch (result) {
|
||||
case PRINT_SUCCESS:
|
||||
return std::string(); // no error message
|
||||
case PRINTING_FAILED:
|
||||
return "Printing failed";
|
||||
case INVALID_PRINTER_SETTINGS:
|
||||
return "Show invalid printer settings error";
|
||||
case INVALID_MEMORY_HANDLE:
|
||||
return "Invalid memory handle";
|
||||
case METAFILE_MAP_ERROR:
|
||||
return "Map to shared memory error";
|
||||
case METAFILE_INVALID_HEADER:
|
||||
return "Invalid metafile header";
|
||||
case METAFILE_GET_DATA_ERROR:
|
||||
return "Get data from metafile error";
|
||||
case SIMULTANEOUS_PRINT_ACTIVE:
|
||||
return "The previous printing job hasn't finished";
|
||||
case PAGE_RANGE_SYNTAX_ERROR:
|
||||
return "Page range syntax error";
|
||||
case PAGE_RANGE_INVALID_RANGE:
|
||||
return "Page range is invalid (start > end)";
|
||||
case PAGE_COUNT_EXCEEDED:
|
||||
return "Page range exceeds page count";
|
||||
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_) {
|
||||
std::move(callback).Run(SIMULTANEOUS_PRINT_ACTIVE,
|
||||
base::MakeRefCounted<base::RefCountedString>());
|
||||
return;
|
||||
}
|
||||
|
||||
if (!rfh->IsRenderFrameLive()) {
|
||||
std::move(callback).Run(PRINTING_FAILED,
|
||||
base::MakeRefCounted<base::RefCountedString>());
|
||||
return;
|
||||
}
|
||||
|
||||
absl::variant<printing::PageRanges, print_to_pdf::PageRangeError>
|
||||
parsed_ranges = print_to_pdf::TextPageRangesToPageRanges(page_ranges);
|
||||
if (absl::holds_alternative<print_to_pdf::PageRangeError>(parsed_ranges)) {
|
||||
PrintResult print_result;
|
||||
switch (absl::get<print_to_pdf::PageRangeError>(parsed_ranges)) {
|
||||
case print_to_pdf::PageRangeError::kSyntaxError:
|
||||
print_result = PAGE_RANGE_SYNTAX_ERROR;
|
||||
break;
|
||||
case print_to_pdf::PageRangeError::kInvalidRange:
|
||||
print_result = PAGE_RANGE_INVALID_RANGE;
|
||||
break;
|
||||
}
|
||||
std::move(callback).Run(print_result,
|
||||
base::MakeRefCounted<base::RefCountedString>());
|
||||
return;
|
||||
}
|
||||
|
||||
printing_rfh_ = rfh;
|
||||
print_pages_params->pages = absl::get<printing::PageRanges>(parsed_ranges);
|
||||
auto cookie = print_pages_params->params->document_cookie;
|
||||
set_cookie(cookie);
|
||||
headless_jobs_.emplace_back(cookie);
|
||||
callback_ = std::move(callback);
|
||||
|
||||
GetPrintRenderFrame(rfh)->PrintWithParams(std::move(print_pages_params));
|
||||
}
|
||||
|
||||
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() {
|
||||
ReleaseJob(INVALID_PRINTER_SETTINGS);
|
||||
}
|
||||
|
||||
void PrintViewManagerElectron::PrintingFailed(
|
||||
int32_t cookie,
|
||||
printing::mojom::PrintFailureReason reason) {
|
||||
ReleaseJob(reason == printing::mojom::PrintFailureReason::kInvalidPageRange
|
||||
? PAGE_COUNT_EXCEEDED
|
||||
: PRINTING_FAILED);
|
||||
}
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
void PrintViewManagerElectron::SetupScriptedPrintPreview(
|
||||
SetupScriptedPrintPreviewCallback callback) {
|
||||
std::move(callback).Run();
|
||||
mojo::ReportBadMessage(kInvalidSetupScriptedPrintPreviewCall);
|
||||
}
|
||||
|
||||
void PrintViewManagerElectron::ShowScriptedPrintPreview(
|
||||
bool source_is_modifiable) {}
|
||||
bool source_is_modifiable) {
|
||||
mojo::ReportBadMessage(kInvalidShowScriptedPrintPreviewCall);
|
||||
}
|
||||
|
||||
void PrintViewManagerElectron::RequestPrintPreview(
|
||||
printing::mojom::RequestPrintPreviewParamsPtr params) {}
|
||||
printing::mojom::RequestPrintPreviewParamsPtr params) {
|
||||
mojo::ReportBadMessage(kInvalidRequestPrintPreviewCall);
|
||||
}
|
||||
|
||||
void PrintViewManagerElectron::CheckForCancel(int32_t preview_ui_id,
|
||||
int32_t request_id,
|
||||
CheckForCancelCallback callback) {
|
||||
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;
|
||||
|
||||
if (callback_) {
|
||||
std::move(callback_).Run(PRINTING_FAILED,
|
||||
base::MakeRefCounted<base::RefCountedString>());
|
||||
}
|
||||
|
||||
Reset();
|
||||
}
|
||||
|
||||
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::DidPrintDocument(
|
||||
printing::mojom::DidPrintDocumentParamsPtr params,
|
||||
DidPrintDocumentCallback callback) {
|
||||
auto entry = std::find(headless_jobs_.begin(), headless_jobs_.end(),
|
||||
params->document_cookie);
|
||||
if (entry == headless_jobs_.end()) {
|
||||
PrintViewManagerBase::DidPrintDocument(std::move(params),
|
||||
std::move(callback));
|
||||
return;
|
||||
}
|
||||
|
||||
auto& content = *params->content;
|
||||
if (!content.metafile_data_region.IsValid()) {
|
||||
ReleaseJob(INVALID_MEMORY_HANDLE);
|
||||
std::move(callback).Run(false);
|
||||
return;
|
||||
}
|
||||
|
||||
base::ReadOnlySharedMemoryMapping map = content.metafile_data_region.Map();
|
||||
if (!map.IsValid()) {
|
||||
ReleaseJob(METAFILE_MAP_ERROR);
|
||||
std::move(callback).Run(false);
|
||||
return;
|
||||
}
|
||||
|
||||
data_ = std::string(static_cast<const char*>(map.memory()), map.size());
|
||||
headless_jobs_.erase(entry);
|
||||
std::move(callback).Run(true);
|
||||
ReleaseJob(PRINT_SUCCESS);
|
||||
}
|
||||
|
||||
void PrintViewManagerElectron::Reset() {
|
||||
printing_rfh_ = nullptr;
|
||||
callback_.Reset();
|
||||
data_.clear();
|
||||
}
|
||||
|
||||
void PrintViewManagerElectron::ReleaseJob(PrintResult result) {
|
||||
if (callback_) {
|
||||
DCHECK(result == PRINT_SUCCESS || data_.empty());
|
||||
std::move(callback_).Run(result,
|
||||
base::RefCountedString::TakeString(&data_));
|
||||
if (printing_rfh_ && printing_rfh_->IsRenderFrameLive()) {
|
||||
GetPrintRenderFrame(printing_rfh_)->PrintingDone(result == PRINT_SUCCESS);
|
||||
}
|
||||
|
||||
Reset();
|
||||
}
|
||||
}
|
||||
|
||||
WEB_CONTENTS_USER_DATA_KEY_IMPL(PrintViewManagerElectron);
|
||||
|
|
|
@ -5,9 +5,19 @@
|
|||
#ifndef ELECTRON_SHELL_BROWSER_PRINTING_PRINT_VIEW_MANAGER_ELECTRON_H_
|
||||
#define ELECTRON_SHELL_BROWSER_PRINTING_PRINT_VIEW_MANAGER_ELECTRON_H_
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "base/memory/raw_ptr.h"
|
||||
#include "base/memory/ref_counted_memory.h"
|
||||
#include "build/build_config.h"
|
||||
#include "chrome/browser/printing/print_view_manager_base.h"
|
||||
#include "components/printing/common/print.mojom.h"
|
||||
#include "content/public/browser/render_frame_host.h"
|
||||
#include "content/public/browser/web_contents_observer.h"
|
||||
#include "content/public/browser/web_contents_user_data.h"
|
||||
#include "printing/print_settings.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
|
@ -15,9 +25,26 @@ class PrintViewManagerElectron
|
|||
: public printing::PrintViewManagerBase,
|
||||
public content::WebContentsUserData<PrintViewManagerElectron> {
|
||||
public:
|
||||
enum PrintResult {
|
||||
PRINT_SUCCESS,
|
||||
PRINTING_FAILED,
|
||||
INVALID_PRINTER_SETTINGS,
|
||||
INVALID_MEMORY_HANDLE,
|
||||
METAFILE_MAP_ERROR,
|
||||
METAFILE_INVALID_HEADER,
|
||||
METAFILE_GET_DATA_ERROR,
|
||||
SIMULTANEOUS_PRINT_ACTIVE,
|
||||
PAGE_RANGE_SYNTAX_ERROR,
|
||||
PAGE_RANGE_INVALID_RANGE,
|
||||
PAGE_COUNT_EXCEEDED,
|
||||
};
|
||||
|
||||
using PrintToPDFCallback =
|
||||
base::OnceCallback<void(PrintResult,
|
||||
scoped_refptr<base::RefCountedMemory>)>;
|
||||
|
||||
~PrintViewManagerElectron() override;
|
||||
|
||||
// disable copy
|
||||
PrintViewManagerElectron(const PrintViewManagerElectron&) = delete;
|
||||
PrintViewManagerElectron& operator=(const PrintViewManagerElectron&) = delete;
|
||||
|
||||
|
@ -26,6 +53,35 @@ class PrintViewManagerElectron
|
|||
receiver,
|
||||
content::RenderFrameHost* rfh);
|
||||
|
||||
static std::string PrintResultToString(PrintResult result);
|
||||
|
||||
void PrintToPdf(content::RenderFrameHost* rfh,
|
||||
const std::string& page_ranges,
|
||||
printing::mojom::PrintPagesParamsPtr print_page_params,
|
||||
PrintToPDFCallback callback);
|
||||
|
||||
private:
|
||||
explicit PrintViewManagerElectron(content::WebContents* web_contents);
|
||||
friend class content::WebContentsUserData<PrintViewManagerElectron>;
|
||||
|
||||
// WebContentsObserver overrides (via PrintManager):
|
||||
void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;
|
||||
|
||||
// printing::mojom::PrintManagerHost:
|
||||
void DidPrintDocument(printing::mojom::DidPrintDocumentParamsPtr params,
|
||||
DidPrintDocumentCallback callback) override;
|
||||
void DidGetPrintedPagesCount(int32_t cookie, uint32_t number_pages) override;
|
||||
void GetDefaultPrintSettings(
|
||||
GetDefaultPrintSettingsCallback callback) override;
|
||||
void ScriptedPrint(printing::mojom::ScriptedPrintParamsPtr params,
|
||||
ScriptedPrintCallback callback) override;
|
||||
void ShowInvalidPrinterSettingsError() override;
|
||||
void PrintingFailed(int32_t cookie,
|
||||
printing::mojom::PrintFailureReason reason) override;
|
||||
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
|
||||
void UpdatePrintSettings(int32_t cookie,
|
||||
base::Value::Dict job_settings,
|
||||
UpdatePrintSettingsCallback callback) override;
|
||||
void SetupScriptedPrintPreview(
|
||||
SetupScriptedPrintPreviewCallback callback) override;
|
||||
void ShowScriptedPrintPreview(bool source_is_modifiable) override;
|
||||
|
@ -34,10 +90,15 @@ class PrintViewManagerElectron
|
|||
void CheckForCancel(int32_t preview_ui_id,
|
||||
int32_t request_id,
|
||||
CheckForCancelCallback callback) override;
|
||||
#endif
|
||||
|
||||
private:
|
||||
friend class content::WebContentsUserData<PrintViewManagerElectron>;
|
||||
explicit PrintViewManagerElectron(content::WebContents* web_contents);
|
||||
void Reset();
|
||||
void ReleaseJob(PrintResult result);
|
||||
|
||||
raw_ptr<content::RenderFrameHost> printing_rfh_ = nullptr;
|
||||
PrintToPDFCallback callback_;
|
||||
std::string data_;
|
||||
std::vector<int32_t> headless_jobs_;
|
||||
|
||||
WEB_CONTENTS_USER_DATA_KEY_DECL();
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue