fix: crash on print cancellation and silent print settings (#19598)

* fix: crash on print cancellation

* fix: update printing patch for new options

* refactor: use DictionaryValue for printBackground
This commit is contained in:
Shelley Vohr 2019-08-07 07:47:24 -07:00 committed by John Kleinschmidt
parent a8861e6a66
commit 9c7a216814
2 changed files with 131 additions and 72 deletions

View file

@ -1591,13 +1591,16 @@ bool WebContents::IsCurrentlyAudible() {
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");
args->ThrowError("webContents.print(): Invalid print settings specified.");
return;
}
printing::CompletionCallback callback;
if (args->Length() == 2 && !args->GetNext(&callback)) {
args->ThrowError("Invalid optional callback provided");
args->ThrowError(
"webContents.print(): Invalid optional callback provided.");
return;
}
@ -1605,8 +1608,13 @@ void WebContents::Print(mate::Arguments* args) {
bool silent = false;
options.Get("silent", &silent);
bool print_background = false;
options.Get("printBackground", &print_background);
settings.SetBoolean(printing::kSettingShouldPrintBackgrounds,
print_background);
// Set custom margin settings
mate::Dictionary margins;
mate::Dictionary margins = mate::Dictionary::CreateEmpty(args->isolate());
if (options.Get("margins", &margins)) {
printing::MarginType margin_type = printing::DEFAULT_MARGINS;
margins.Get("marginType", &margin_type);
@ -1643,6 +1651,8 @@ void WebContents::Print(mate::Arguments* args) {
options.Get("landscape", &landscape);
settings.SetBoolean(printing::kSettingLandscape, landscape);
// We set the default to empty string here and only update
// if at the Chromium level if it's non-empty
base::string16 device_name;
options.Get("deviceName", &device_name);
settings.SetString(printing::kSettingDeviceName, device_name);
@ -1663,11 +1673,6 @@ void WebContents::Print(mate::Arguments* args) {
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);
@ -1723,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() {