Skip to content

Commit

Permalink
fix: crash on print cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Aug 2, 2019
1 parent 2e78589 commit 4bea46c
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 129 deletions.
37 changes: 19 additions & 18 deletions docs/api/web-contents.md
Expand Up @@ -1246,24 +1246,25 @@ 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`.
* `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.
* `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.
* `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
5 changes: 2 additions & 3 deletions patches/chromium/printing.patch
Expand Up @@ -28,7 +28,7 @@ index 63f432b58371cfa0f8079fa78a51c8865a00c183..7b39523e0b8b840191ea517d5f5e8eda
#include "printing/print_job_constants.h"
#include "printing/printed_document.h"
#include "printing/printing_utils.h"
@@ -224,7 +224,15 @@ void PrintJobWorker::UpdatePrintSettingsFromPOD(
@@ -224,7 +224,14 @@ void PrintJobWorker::UpdatePrintSettingsFromPOD(

void PrintJobWorker::GetSettingsDone(SettingsCallback callback,
PrintingContext::Result result) {
Expand All @@ -39,9 +39,8 @@ index 63f432b58371cfa0f8079fa78a51c8865a00c183..7b39523e0b8b840191ea517d5f5e8eda
+ base::BindOnce(&NotificationCallback, base::RetainedRef(print_job_),
+ JobEventDetails::USER_INIT_CANCELED, 0,
+ base::RetainedRef(document_)));
+ } else {
+ std::move(callback).Run(printing_context_->settings(), result);
+ }
+ std::move(callback).Run(printing_context_->settings(), result);
}

void PrintJobWorker::GetSettingsWithUI(int document_page_count,
Expand Down
221 changes: 114 additions & 107 deletions shell/browser/api/atom_api_web_contents.cc
Expand Up @@ -1590,7 +1590,6 @@ bool WebContents::IsCurrentlyAudible() {
#if BUILDFLAG(ENABLE_PRINTING)
void WebContents::Print(mate::Arguments* args) {
mate::Dictionary options = mate::Dictionary::CreateEmpty(args->isolate());
base::DictionaryValue settings;
if (args->Length() >= 1 && !args->GetNext(&options)) {
args->ThrowError("Invalid print settings specified");
return;
Expand All @@ -1605,116 +1604,124 @@ void WebContents::Print(mate::Arguments* args) {
bool silent = false;
options.Get("silent", &silent);

// Set custom margin settings
mate::Dictionary margins;
if (options.Get("margins", &margins)) {
printing::MarginType margin_type = printing::DEFAULT_MARGINS;
margins.Get("marginType", &margin_type);
settings.SetInteger(printing::kSettingMarginsType, margin_type);

if (margin_type == printing::CUSTOM_MARGINS) {
int top = 0;
margins.Get("top", &top);
settings.SetInteger(printing::kSettingMarginTop, top);
int bottom = 0;
margins.Get("bottom", &bottom);
settings.SetInteger(printing::kSettingMarginBottom, bottom);
int left = 0;
margins.Get("left", &left);
settings.SetInteger(printing::kSettingMarginLeft, left);
int right = 0;
margins.Get("right", &right);
settings.SetInteger(printing::kSettingMarginRight, right);
}
} else {
settings.SetInteger(printing::kSettingMarginsType,
printing::DEFAULT_MARGINS);
}

settings.SetBoolean(printing::kSettingHeaderFooterEnabled, false);

// Set whether to print color or greyscale
bool print_color = true;
options.Get("color", &print_color);
int color_setting = print_color ? printing::COLOR : printing::GRAY;
settings.SetInteger(printing::kSettingColor, color_setting);

bool landscape = false;
options.Get("landscape", &landscape);
settings.SetBoolean(printing::kSettingLandscape, landscape);

base::string16 device_name;
options.Get("deviceName", &device_name);
settings.SetString(printing::kSettingDeviceName, device_name);

int scale_factor = 100;
options.Get("scaleFactor", &scale_factor);
settings.SetInteger(printing::kSettingScaleFactor, scale_factor);

int pages_per_sheet = 1;
options.Get("pagesPerSheet", &pages_per_sheet);
settings.SetInteger(printing::kSettingPagesPerSheet, pages_per_sheet);

bool collate = true;
options.Get("collate", &collate);
settings.SetBoolean(printing::kSettingCollate, collate);

int copies = 1;
options.Get("copies", &copies);
settings.SetInteger(printing::kSettingCopies, copies);

bool print_background = false;
options.Get("printBackground", &print_background);
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);
settings.SetBoolean(printing::kSettingCloudPrintDialog, false);
settings.SetBoolean(printing::kSettingPrintWithPrivet, false);
settings.SetBoolean(printing::kSettingShouldPrintSelectionOnly, false);
settings.SetBoolean(printing::kSettingPrintWithExtension, false);
settings.SetBoolean(printing::kSettingRasterizePdf, false);

// Set custom page ranges to print
std::vector<mate::Dictionary> page_ranges;
if (options.Get("pageRanges", &page_ranges)) {
std::unique_ptr<base::ListValue> page_range_list(new base::ListValue());
for (size_t i = 0; i < page_ranges.size(); ++i) {
int from, to;
if (page_ranges[i].Get("from", &from) && page_ranges[i].Get("to", &to)) {
std::unique_ptr<base::DictionaryValue> range(
new base::DictionaryValue());
range->SetInteger(printing::kSettingPageRangeFrom, from);
range->SetInteger(printing::kSettingPageRangeTo, to);
page_range_list->Append(std::move(range));
} else {
continue;

mate::Dictionary print_settings =
mate::Dictionary::CreateEmpty(args->isolate());
base::DictionaryValue settings;
if (silent && options.Get("printSettings", &print_settings)) {
// Set custom margin settings
mate::Dictionary margins;
if (print_settings.Get("margins", &margins)) {
printing::MarginType margin_type = printing::DEFAULT_MARGINS;
margins.Get("marginType", &margin_type);
settings.SetInteger(printing::kSettingMarginsType, margin_type);

if (margin_type == printing::CUSTOM_MARGINS) {
int top = 0;
margins.Get("top", &top);
settings.SetInteger(printing::kSettingMarginTop, top);
int bottom = 0;
margins.Get("bottom", &bottom);
settings.SetInteger(printing::kSettingMarginBottom, bottom);
int left = 0;
margins.Get("left", &left);
settings.SetInteger(printing::kSettingMarginLeft, left);
int right = 0;
margins.Get("right", &right);
settings.SetInteger(printing::kSettingMarginRight, right);
}
} else {
settings.SetInteger(printing::kSettingMarginsType,
printing::DEFAULT_MARGINS);
}

settings.SetBoolean(printing::kSettingHeaderFooterEnabled, false);

// Set whether to print color or greyscale
bool print_color = true;
print_settings.Get("color", &print_color);
int color_setting = print_color ? printing::COLOR : printing::GRAY;
settings.SetInteger(printing::kSettingColor, color_setting);

bool landscape = false;
print_settings.Get("landscape", &landscape);
settings.SetBoolean(printing::kSettingLandscape, landscape);

base::string16 device_name;
print_settings.Get("deviceName", &device_name);
settings.SetString(printing::kSettingDeviceName, device_name);

int scale_factor = 100;
print_settings.Get("scaleFactor", &scale_factor);
settings.SetInteger(printing::kSettingScaleFactor, scale_factor);

int pages_per_sheet = 1;
print_settings.Get("pagesPerSheet", &pages_per_sheet);
settings.SetInteger(printing::kSettingPagesPerSheet, pages_per_sheet);

bool collate = true;
print_settings.Get("collate", &collate);
settings.SetBoolean(printing::kSettingCollate, collate);

int copies = 1;
print_settings.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);
settings.SetBoolean(printing::kSettingCloudPrintDialog, false);
settings.SetBoolean(printing::kSettingPrintWithPrivet, false);
settings.SetBoolean(printing::kSettingShouldPrintSelectionOnly, false);
settings.SetBoolean(printing::kSettingPrintWithExtension, false);
settings.SetBoolean(printing::kSettingRasterizePdf, false);

// Set custom page ranges to print
std::vector<mate::Dictionary> page_ranges;
if (print_settings.Get("pageRanges", &page_ranges)) {
std::unique_ptr<base::ListValue> page_range_list(new base::ListValue());
for (size_t i = 0; i < page_ranges.size(); ++i) {
int from, to;
if (page_ranges[i].Get("from", &from) &&
page_ranges[i].Get("to", &to)) {
std::unique_ptr<base::DictionaryValue> range(
new base::DictionaryValue());
range->SetInteger(printing::kSettingPageRangeFrom, from);
range->SetInteger(printing::kSettingPageRangeTo, to);
page_range_list->Append(std::move(range));
} else {
continue;
}
}
if (page_range_list->GetSize() > 0)
settings.SetList(printing::kSettingPageRange,
std::move(page_range_list));
}

// Set custom duplex mode
printing::DuplexMode duplex_mode;
print_settings.Get("duplexMode", &duplex_mode);
settings.SetInteger(printing::kSettingDuplexMode, duplex_mode);

// Set custom dots per inch (dpi)
mate::Dictionary dpi_settings;
int dpi = 72;
if (print_settings.Get("dpi", &dpi_settings)) {
int horizontal = 72;
dpi_settings.Get("horizontal", &horizontal);
settings.SetInteger(printing::kSettingDpiHorizontal, horizontal);
int vertical = 72;
dpi_settings.Get("vertical", &vertical);
settings.SetInteger(printing::kSettingDpiVertical, vertical);
} else {
settings.SetInteger(printing::kSettingDpiHorizontal, dpi);
settings.SetInteger(printing::kSettingDpiVertical, dpi);
}
if (page_range_list->GetSize() > 0)
settings.SetList(printing::kSettingPageRange, std::move(page_range_list));
}

// Set custom duplex mode
printing::DuplexMode duplex_mode;
options.Get("duplexMode", &duplex_mode);
settings.SetInteger(printing::kSettingDuplexMode, duplex_mode);

// Set custom dots per inch (dpi)
mate::Dictionary dpi_settings;
int dpi = 72;
if (options.Get("dpi", &dpi_settings)) {
int horizontal = 72;
dpi_settings.Get("horizontal", &horizontal);
settings.SetInteger(printing::kSettingDpiHorizontal, horizontal);
int vertical = 72;
dpi_settings.Get("vertical", &vertical);
settings.SetInteger(printing::kSettingDpiVertical, vertical);
} else {
settings.SetInteger(printing::kSettingDpiHorizontal, dpi);
settings.SetInteger(printing::kSettingDpiVertical, dpi);
}

auto* print_view_manager =
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, printBackground: false })
mainWindow.webContents.print({ silent: true })
mainWindow.webContents.print()

mainWindow.webContents.printToPDF({
Expand Down

0 comments on commit 4bea46c

Please sign in to comment.