From e8c628ecdf0b7e03a4dcdb52c4d188204f717aac Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Wed, 4 Mar 2020 02:20:31 +0000 Subject: [PATCH] fix: bail early if no printers on the network (#22418) --- docs/api/web-contents.md | 2 +- shell/browser/api/electron_api_web_contents.cc | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index e28d593ab7..9653b509a1 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -1286,7 +1286,7 @@ Returns [`PrinterInfo[]`](structures/printer-info.md) `A4`, `A5`, `Legal`, `Letter`, `Tabloid` or an Object containing `height`. * `callback` Function (optional) * `success` Boolean - Indicates success of the print call. - * `failureReason` String - Called back if the print fails; can be `cancelled` or `failed`. + * `failureReason` String - Error description called back if the print fails. Prints window's web page. When `silent` is set to `true`, Electron will pick the system's default printer if `deviceName` is empty and the default settings for printing. diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 414b5f1710..15b1b5748b 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -1766,6 +1766,14 @@ void WebContents::OnGetDefaultPrinter( base::string16 printer_name = device_name.empty() ? default_printer : device_name; + + // If there are no valid printers available on the network, we bail. + if (printer_name.empty() || !IsDeviceNameValid(printer_name)) { + if (print_callback) + std::move(print_callback).Run(false, "no valid printers available"); + return; + } + print_settings.SetStringKey(printing::kSettingDeviceName, printer_name); auto* print_view_manager =