Skip to content

Commit

Permalink
refactor: use DictionaryValue for printBackground
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Aug 6, 2019
1 parent 8bbf5f6 commit 16773bb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 69 deletions.
37 changes: 18 additions & 19 deletions docs/api/web-contents.md
Expand Up @@ -1246,25 +1246,24 @@ Returns [`PrinterInfo[]`](structures/printer-info.md).
* `silent` Boolean (optional) - Don't ask user for print settings. Default is `false`.
* `printBackground` Boolean (optional) - Prints the background color and image of
the web page. Default is `false`.
* `printSettings` Object (optional) - Only applicable when printing with `silent: true`.
* `deviceName` String (optional) - Set the printer device name to use. Default is `''`.
* `color` Boolean (optional) - Set whether the printed web page will be in color or grayscale. Default is `true`.
* `margins` Object (optional)
* `marginType` String (optional) - Can be `default`, `none`, `printableArea`, or `custom`. If `custom` is chosen, you will also need to specify `top`, `bottom`, `left`, and `right`.
* `top` Number (optional) - The top margin of the printed web page, in pixels.
* `bottom` Number (optional) - The bottom margin of the printed web page, in pixels.
* `left` Number (optional) - The left margin of the printed web page, in pixels.
* `right` Number (optional) - The right margin of the printed web page, in pixels.
* `landscape` Boolean (optional) - Whether the web page should be printed in landscape mode. Default is `false`.
* `scaleFactor` Number (optional) - The scale factor of the web page.
* `pagesPerSheet` Number (optional) - The number of pages to print per page sheet.
* `collate` Boolean (optional) - Whether the web page should be collated.
* `copies` Number (optional) - The number of copies of the web page to print.
* `pageRanges` Record<string, number> (optional) - The page range to print. Should have two keys: `from` and `to`.
* `duplexMode` String (optional) - Set the duplex mode of the printed web page. Can be `simplex`, `shortEdge`, or `longEdge`.
* `dpi` Object (optional)
* `horizontal` Number (optional) - The horizontal dpi.
* `vertical` Number (optional) - The vertical dpi.
* `deviceName` String (optional) - Set the printer device name to use. Default is `''`.
* `color` Boolean (optional) - Set whether the printed web page will be in color or grayscale. Default is `true`.
* `margins` Object (optional)
* `marginType` String (optional) - Can be `default`, `none`, `printableArea`, or `custom`. If `custom` is chosen, you will also need to specify `top`, `bottom`, `left`, and `right`.
* `top` Number (optional) - The top margin of the printed web page, in pixels.
* `bottom` Number (optional) - The bottom margin of the printed web page, in pixels.
* `left` Number (optional) - The left margin of the printed web page, in pixels.
* `right` Number (optional) - The right margin of the printed web page, in pixels.
* `landscape` Boolean (optional) - Whether the web page should be printed in landscape mode. Default is `false`.
* `scaleFactor` Number (optional) - The scale factor of the web page.
* `pagesPerSheet` Number (optional) - The number of pages to print per page sheet.
* `collate` Boolean (optional) - Whether the web page should be collated.
* `copies` Number (optional) - The number of copies of the web page to print.
* `pageRanges` Record<string, number> (optional) - The page range to print. Should have two keys: `from` and `to`.
* `duplexMode` String (optional) - Set the duplex mode of the printed web page. Can be `simplex`, `shortEdge`, or `longEdge`.
* `dpi` Object (optional)
* `horizontal` Number (optional) - The horizontal dpi.
* `vertical` Number (optional) - The vertical dpi.
* `callback` Function (optional)
* `success` Boolean - Indicates success of the print call.
* `failureReason` String - Called back if the print fails; can be `cancelled` or `failed`.
Expand Down
66 changes: 25 additions & 41 deletions patches/chromium/printing.patch
Expand Up @@ -378,23 +378,22 @@ index 9fbea6d0a2dbe55b1d600fbc217dee5aa8ae8cd5..de9bd267e408c02fd4da7d903523c0e6
// content::BrowserMessageFilter:
bool OnMessageReceived(const IPC::Message& message) override;
diff --git a/components/printing/common/print_messages.h b/components/printing/common/print_messages.h
index 1802034a6e15a6ad8b0d9591cfb79ba5873dc982..a827091facdb4f6b1d74ce826c3492ced27c008e 100644
index 1802034a6e15a6ad8b0d9591cfb79ba5873dc982..331ac71d925c056d3b7577123251514c35f30fde 100644
--- a/components/printing/common/print_messages.h
+++ b/components/printing/common/print_messages.h
@@ -368,7 +368,10 @@ IPC_MESSAGE_ROUTED0(PrintMsg_PrintNodeUnderContextMenu)
@@ -368,7 +368,9 @@ IPC_MESSAGE_ROUTED0(PrintMsg_PrintNodeUnderContextMenu)
#if BUILDFLAG(ENABLE_PRINTING)
// Tells the RenderFrame to switch the CSS to print media type, renders every
// requested pages and switch back the CSS to display media type.
-IPC_MESSAGE_ROUTED0(PrintMsg_PrintPages)
+IPC_MESSAGE_ROUTED3(PrintMsg_PrintPages,
+IPC_MESSAGE_ROUTED2(PrintMsg_PrintPages,
+ bool /* silent print */,
+ bool /* print page's background */,
+ base::DictionaryValue /* settings */)

// Like PrintMsg_PrintPages, but using the print preview document's frame/node.
IPC_MESSAGE_ROUTED0(PrintMsg_PrintForSystemDialog)
diff --git a/components/printing/renderer/print_render_frame_helper.cc b/components/printing/renderer/print_render_frame_helper.cc
index ef580254bd8feba84ac02924b77b9b4feaf14d96..572c196732c25d14b8f181f424ec4032c8be77c2 100644
index ef580254bd8feba84ac02924b77b9b4feaf14d96..8b10469dd2e91edec2ddf9411b5281b76a8398a1 100644
--- a/components/printing/renderer/print_render_frame_helper.cc
+++ b/components/printing/renderer/print_render_frame_helper.cc
@@ -37,6 +37,7 @@
Expand All @@ -405,49 +404,47 @@ index ef580254bd8feba84ac02924b77b9b4feaf14d96..572c196732c25d14b8f181f424ec4032
#include "printing/units.h"
#include "third_party/blink/public/common/frame/frame_owner_element_type.h"
#include "third_party/blink/public/common/frame/sandbox_flags.h"
@@ -1116,7 +1117,9 @@ void PrintRenderFrameHelper::ScriptedPrint(bool user_initiated) {
@@ -1116,7 +1117,8 @@ void PrintRenderFrameHelper::ScriptedPrint(bool user_initiated) {
web_frame->DispatchBeforePrintEvent();
if (!weak_this)
return;
- Print(web_frame, blink::WebNode(), PrintRequestType::kScripted);
+ Print(web_frame, blink::WebNode(), PrintRequestType::kScripted,
+ false /* silent */, false /* print_background */,
+ base::DictionaryValue() /* new_settings */);
+ false /* silent */, base::DictionaryValue() /* new_settings */);
if (weak_this)
web_frame->DispatchAfterPrintEvent();
}
@@ -1164,7 +1167,10 @@ void PrintRenderFrameHelper::OnDestruct() {
@@ -1164,7 +1166,9 @@ void PrintRenderFrameHelper::OnDestruct() {
delete this;
}

-void PrintRenderFrameHelper::OnPrintPages() {
+void PrintRenderFrameHelper::OnPrintPages(
+ bool silent,
+ bool print_background,
+ const base::DictionaryValue& settings) {
if (ipc_nesting_level_ > 1)
return;

@@ -1177,7 +1183,8 @@ void PrintRenderFrameHelper::OnPrintPages() {
@@ -1177,7 +1181,8 @@ void PrintRenderFrameHelper::OnPrintPages() {
// If we are printing a PDF extension frame, find the plugin node and print
// that instead.
auto plugin = delegate_->GetPdfElement(frame);
- Print(frame, plugin, PrintRequestType::kRegular);
+ Print(frame, plugin, PrintRequestType::kRegular,
+ silent, print_background, settings);
+ silent, settings);
if (weak_this)
frame->DispatchAfterPrintEvent();
// WARNING: |this| may be gone at this point. Do not do any more work here and
@@ -1194,7 +1201,7 @@ void PrintRenderFrameHelper::OnPrintForSystemDialog() {
@@ -1194,7 +1199,7 @@ void PrintRenderFrameHelper::OnPrintForSystemDialog() {
}
auto weak_this = weak_ptr_factory_.GetWeakPtr();
Print(frame, print_preview_context_.source_node(),
- PrintRequestType::kRegular);
+ PrintRequestType::kRegular, false, false, base::DictionaryValue());
+ PrintRequestType::kRegular, false, base::DictionaryValue());
if (weak_this)
frame->DispatchAfterPrintEvent();
// WARNING: |this| may be gone at this point. Do not do any more work here and
@@ -1230,6 +1237,8 @@ void PrintRenderFrameHelper::OnPrintPreview(
@@ -1230,6 +1235,8 @@ void PrintRenderFrameHelper::OnPrintPreview(
if (ipc_nesting_level_ > 1)
return;

Expand All @@ -456,31 +453,29 @@ index ef580254bd8feba84ac02924b77b9b4feaf14d96..572c196732c25d14b8f181f424ec4032
print_preview_context_.OnPrintPreview();

UMA_HISTOGRAM_ENUMERATION("PrintPreview.PreviewEvent",
@@ -1622,7 +1631,10 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {
@@ -1622,7 +1629,9 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {

auto self = weak_ptr_factory_.GetWeakPtr();
Print(duplicate_node.GetDocument().GetFrame(), duplicate_node,
- PrintRequestType::kRegular);
+ PrintRequestType::kRegular,
+ false /* silent */,
+ false /* print_background */,
+ base::DictionaryValue() /* new_settings */);
// Check if |this| is still valid.
if (!self)
return;
@@ -1633,7 +1645,10 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {
@@ -1633,7 +1642,9 @@ void PrintRenderFrameHelper::PrintNode(const blink::WebNode& node) {

void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
const blink::WebNode& node,
- PrintRequestType print_request_type) {
+ PrintRequestType print_request_type,
+ bool silent,
+ bool print_background,
+ const base::DictionaryValue& settings) {
// If still not finished with earlier print request simply ignore.
if (prep_frame_view_)
return;
@@ -1641,7 +1656,7 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
@@ -1641,7 +1652,7 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
FrameReference frame_ref(frame);

int expected_page_count = 0;
Expand All @@ -489,7 +484,7 @@ index ef580254bd8feba84ac02924b77b9b4feaf14d96..572c196732c25d14b8f181f424ec4032
DidFinishPrinting(FAIL_PRINT_INIT);
return; // Failed to init print page settings.
}
@@ -1661,8 +1676,11 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
@@ -1661,8 +1672,11 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,

PrintMsg_PrintPages_Params print_settings;
auto self = weak_ptr_factory_.GetWeakPtr();
Expand All @@ -503,15 +498,7 @@ index ef580254bd8feba84ac02924b77b9b4feaf14d96..572c196732c25d14b8f181f424ec4032
// Check if |this| is still valid.
if (!self)
return;
@@ -1672,6 +1690,7 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame,
? blink::kWebPrintScalingOptionSourceSize
: scaling_option;
SetPrintPagesParams(print_settings);
+ print_settings.params.should_print_backgrounds = print_background;
if (print_settings.params.dpi.IsEmpty() ||
!print_settings.params.document_cookie) {
DidFinishPrinting(OK); // Release resources and fail silently on failure.
@@ -1867,10 +1886,17 @@ std::vector<int> PrintRenderFrameHelper::GetPrintedPages(
@@ -1867,10 +1881,17 @@ std::vector<int> PrintRenderFrameHelper::GetPrintedPages(
return printed_pages;
}

Expand All @@ -532,7 +519,7 @@ index ef580254bd8feba84ac02924b77b9b4feaf14d96..572c196732c25d14b8f181f424ec4032
// Check if the printer returned any settings, if the settings is empty, we
// can safely assume there are no printer drivers configured. So we safely
// terminate.
@@ -1890,12 +1916,14 @@ bool PrintRenderFrameHelper::InitPrintSettings(bool fit_to_paper_size) {
@@ -1890,12 +1911,14 @@ bool PrintRenderFrameHelper::InitPrintSettings(bool fit_to_paper_size) {
return result;
}

Expand All @@ -552,33 +539,31 @@ index ef580254bd8feba84ac02924b77b9b4feaf14d96..572c196732c25d14b8f181f424ec4032
Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id()));
return false;
diff --git a/components/printing/renderer/print_render_frame_helper.h b/components/printing/renderer/print_render_frame_helper.h
index 71c0c15217b62cd7a6087c6d9ae50481f9041d5f..e2250a66517dbd909cd3b5407173ad91c11ec32f 100644
index 71c0c15217b62cd7a6087c6d9ae50481f9041d5f..18d853d7f808aaf816de86e8c5b8231738d16f55 100644
--- a/components/printing/renderer/print_render_frame_helper.h
+++ b/components/printing/renderer/print_render_frame_helper.h
@@ -193,7 +193,9 @@ class PrintRenderFrameHelper
@@ -193,7 +193,8 @@ class PrintRenderFrameHelper
bool OnMessageReceived(const IPC::Message& message) override;

// Message handlers ---------------------------------------------------------
- void OnPrintPages();
+ void OnPrintPages(bool silent,
+ bool print_background,
+ const base::DictionaryValue& settings);
void OnPrintForSystemDialog();
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
void OnInitiatePrintPreview(bool has_selection);
@@ -243,7 +245,10 @@ class PrintRenderFrameHelper
@@ -243,7 +244,9 @@ class PrintRenderFrameHelper
// WARNING: |this| may be gone after this method returns.
void Print(blink::WebLocalFrame* frame,
const blink::WebNode& node,
- PrintRequestType print_request_type);
+ PrintRequestType print_request_type,
+ bool silent,
+ bool print_background,
+ const base::DictionaryValue& settings);

// Notification when printing is done - signal tear-down/free resources.
void DidFinishPrinting(PrintingResult result);
@@ -252,12 +257,14 @@ class PrintRenderFrameHelper
@@ -252,12 +255,14 @@ class PrintRenderFrameHelper

// Initialize print page settings with default settings.
// Used only for native printing workflow.
Expand All @@ -599,13 +584,12 @@ diff --git a/printing/print_settings_conversion.cc b/printing/print_settings_con
index 2563ae6a87b2354ff2f2b45c17f61d2f44910efa..1f34f905791f38a44ae8c892cc9cfb38d15b659d 100644
--- a/printing/print_settings_conversion.cc
+++ b/printing/print_settings_conversion.cc
@@ -190,11 +190,13 @@ bool PrintSettingsFromJobSettings(const base::Value& job_settings,
@@ -190,11 +190,12 @@ bool PrintSettingsFromJobSettings(const base::Value& job_settings,

settings->set_dpi_xy(dpi_horizontal.value(), dpi_vertical.value());
#endif
+ std::string print_device_name = *device_name;
+ if (print_device_name != "")
+ settings->set_device_name(base::UTF8ToUTF16(print_device_name));
+ if (!device_name->empty())
+ settings->set_device_name(base::UTF8ToUTF16(*device_name));

settings->set_collate(collate.value());
settings->set_copies(copies.value());
Expand Down
14 changes: 6 additions & 8 deletions shell/browser/api/atom_api_web_contents.cc
Expand Up @@ -1610,6 +1610,8 @@ void WebContents::Print(mate::Arguments* args) {

bool print_background = false;
options.Get("printBackground", &print_background);
settings.SetBoolean(printing::kSettingShouldPrintBackgrounds,
print_background);

// Set custom margin settings
mate::Dictionary margins = mate::Dictionary::CreateEmpty(args->isolate());
Expand Down Expand Up @@ -1671,9 +1673,6 @@ void WebContents::Print(mate::Arguments* args) {
options.Get("copies", &copies);
settings.SetInteger(printing::kSettingCopies, copies);

settings.SetBoolean(printing::kSettingShouldPrintBackgrounds,
print_background);

// For now we don't want to allow the user to enable these settings
// but we need to set them or a CHECK is hit.
settings.SetBoolean(printing::kSettingPrintToPDF, false);
Expand Down Expand Up @@ -1729,11 +1728,10 @@ void WebContents::Print(mate::Arguments* args) {
auto* rfh = focused_frame && focused_frame->HasSelection()
? focused_frame
: web_contents()->GetMainFrame();
print_view_manager->PrintNow(
rfh,
std::make_unique<PrintMsg_PrintPages>(rfh->GetRoutingID(), silent,
print_background, settings),
std::move(callback));
print_view_manager->PrintNow(rfh,
std::make_unique<PrintMsg_PrintPages>(
rfh->GetRoutingID(), silent, settings),
std::move(callback));
}

std::vector<printing::PrinterBasicInfo> WebContents::GetPrinterList() {
Expand Down
2 changes: 1 addition & 1 deletion spec/ts-smoke/electron/main.ts
Expand Up @@ -83,7 +83,7 @@ app.on('ready', () => {
mainWindow.webContents.setVisualZoomLevelLimits(50, 200)
mainWindow.webContents.setLayoutZoomLevelLimits(50, 200)

mainWindow.webContents.print({ silent: true })
mainWindow.webContents.print({ silent: true, printBackground: false })
mainWindow.webContents.print()

mainWindow.webContents.printToPDF({
Expand Down

0 comments on commit 16773bb

Please sign in to comment.