From 858dbc690b159ecf767e6ae55e476f73ead931f6 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Wed, 29 Jan 2020 08:29:00 -0800 Subject: [PATCH] address review feedback --- docs/api/webview-tag.md | 23 ++-------------------- shell/browser/api/atom_api_web_contents.cc | 14 +++++++------ spec-main/api-web-contents-spec.ts | 1 - 3 files changed, 10 insertions(+), 28 deletions(-) diff --git a/docs/api/webview-tag.md b/docs/api/webview-tag.md index fa55909f09c0a..626c607ce7751 100644 --- a/docs/api/webview-tag.md +++ b/docs/api/webview-tag.md @@ -545,28 +545,9 @@ Stops any `findInPage` request for the `webview` with the provided `action`. * `options` Object (optional) * `silent` Boolean (optional) - Don't ask user for print settings. Default is `false`. - * `printBackground` Boolean (optional) - Prints the background color and image of + * `printBackground` Boolean (optional) - Also prints the background color and image of the web page. Default is `false`. - * `deviceName` String (optional) - Set the printer device name to use. Must be the system-defined name and not the 'friendly' name, e.g 'Brother_QL_820NWB' and not 'Brother QL-820NWB'. - * `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 (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. - * `header` String (optional) - String to be printed as page header. - * `footer` String (optional) - String to be printed as page footer. + * `deviceName` String (optional) - Set the printer device name to use. Default is `''`. Returns `Promise` diff --git a/shell/browser/api/atom_api_web_contents.cc b/shell/browser/api/atom_api_web_contents.cc index 58b2e2f2d9288..f079a2f964853 100644 --- a/shell/browser/api/atom_api_web_contents.cc +++ b/shell/browser/api/atom_api_web_contents.cc @@ -354,14 +354,17 @@ base::Optional GetCursorBlinkInterval() { // This will return false if no printer with the provided device_name can be // found on the network. We need to check this because Chromium does not do // sanity checking of device_name validity and so will crash on invalid names. -bool DeviceNameValid(const std::string& device_name) { +bool IsDeviceNameValid(const base::string16& device_name) { #if defined(OS_MACOSX) base::ScopedCFTypeRef new_printer_id( - base::SysUTF8ToCFStringRef(device_name)); - return PMPrinterCreateFromPrinterID(new_printer_id.get()); + base::SysUTF16ToCFStringRef(device_name)); + PMPrinter new_printer = PMPrinterCreateFromPrinterID(new_printer_id.get()); + bool printer_exists = new_printer != nullptr; + PMRelease(new_printer); + return printer_exists; #elif defined(OS_WIN) printing::ScopedPrinterHandle printer; - return printer.OpenPrinterWithName(base::UTF8ToUTF16(device_name).c_str()); + return printer.OpenPrinterWithName(device_name.c_str()); #endif return true; } @@ -1804,8 +1807,7 @@ void WebContents::Print(gin_helper::Arguments* args) { // Printer device name as opened by the OS. base::string16 device_name; options.Get("deviceName", &device_name); - if (!device_name.empty() && - !DeviceNameValid(base::UTF16ToUTF8(device_name))) { + if (!device_name.empty() && !IsDeviceNameValid(device_name)) { args->ThrowError("webContents.print(): Invalid deviceName provided."); return; } diff --git a/spec-main/api-web-contents-spec.ts b/spec-main/api-web-contents-spec.ts index e1347d99f5f48..b619653b41b03 100644 --- a/spec-main/api-web-contents-spec.ts +++ b/spec-main/api-web-contents-spec.ts @@ -128,7 +128,6 @@ describe('webContents module', () => { }) it('does not crash', () => { - const w = new BrowserWindow({ show: false }) expect(() => { w.webContents.print({ silent: true }) }).to.not.throw()