Skip to content

Commit

Permalink
feat: add webContents.getPrintersAsync()
Browse files Browse the repository at this point in the history
This deprecates the synchronous and blocking webContents.getPrinters()
function and introduces webContents.getPrintersAsync(), which is
asynchronous and non-blocking.

Signed-off-by: Darshan Sen <darshan.sen@postman.com>
  • Loading branch information
RaisinTen committed Sep 20, 2021
1 parent aa9da78 commit 77dd7ce
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 4 deletions.
10 changes: 9 additions & 1 deletion docs/api/web-contents.md
Original file line number Diff line number Diff line change
Expand Up @@ -1366,12 +1366,20 @@ Decrease the capturer count by one. The page will be set to hidden or occluded s
browser window is hidden or occluded and the capturer count reaches zero. If you want to
decrease the hidden capturer count instead you should set `stayHidden` to true.

#### `contents.getPrinters()`
#### `contents.getPrinters()` _Deprecated_

Get the system printer list.

Returns [`PrinterInfo[]`](structures/printer-info.md)

**Deprecated:** Should use the new [`contents.getPrintersAsync`](web-contents.md#contentsgetprintersasync) API.

#### `contents.getPrintersAsync()`

Get the system printer list.

Returns `Promise<PrinterInfo[]>` - Resolves with a [`PrinterInfo[]`](structures/printer-info.md)

#### `contents.print([options], [callback])`

* `options` Object (optional)
Expand Down
11 changes: 11 additions & 0 deletions lib/browser/api/web-contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,17 @@ WebContents.prototype.getPrinters = function () {
}
};

WebContents.prototype.getPrintersAsync = async function () {
// TODO(nornagon): this API has nothing to do with WebContents and should be
// moved.
if (printing.getPrinterListAsync) {
return printing.getPrinterListAsync();
} else {
console.error('Error: Printing feature is disabled.');
return [];
}
};

WebContents.prototype.loadFile = function (filePath, options = {}) {
if (typeof filePath !== 'string') {
throw new Error('Must pass filePath as a string');
Expand Down
36 changes: 33 additions & 3 deletions shell/browser/api/electron_api_printing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#if BUILDFLAG(ENABLE_PRINTING)
#include "printing/backend/print_backend.h"
#include "shell/common/gin_helper/promise.h"
#include "shell/common/process_util.h"
#endif

namespace gin {
Expand Down Expand Up @@ -39,18 +41,43 @@ namespace electron {
namespace api {

#if BUILDFLAG(ENABLE_PRINTING)
printing::PrinterList GetPrinterList() {
printing::PrinterList GetPrinterList(v8::Isolate* isolate) {
EmitWarning(node::Environment::GetCurrent(isolate),
"Deprecation Warning: getPrinterList() is deprecated. "
"Use the asynchronous and non-blocking version, "
"getPrinterListAsync(), instead.",
"electron");
printing::PrinterList printers;
auto print_backend = printing::PrintBackend::CreateInstance(
g_browser_process->GetApplicationLocale());
{
// TODO(deepak1556): Deprecate this api in favor of an
// async version and post a non blocing task call.
base::ThreadRestrictions::ScopedAllowIO allow_io;
print_backend->EnumeratePrinters(&printers);
}
return printers;
}

v8::Local<v8::Promise> GetPrinterListAsync(v8::Isolate* isolate) {
gin_helper::Promise<printing::PrinterList> promise(isolate);
const v8::Local<v8::Promise> handle = promise.GetHandle();

base::PostTask(FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(
[](gin_helper::Promise<printing::PrinterList>&& promise) {
printing::PrinterList printers;
auto print_backend =
printing::PrintBackend::CreateInstance(
g_browser_process->GetApplicationLocale());
{
base::ThreadRestrictions::ScopedAllowIO allow_io;
print_backend->EnumeratePrinters(&printers);
}
promise.Resolve(printers);
},
std::move(promise)));

return handle;
}
#endif

} // namespace api
Expand All @@ -61,6 +88,7 @@ namespace {

#if BUILDFLAG(ENABLE_PRINTING)
using electron::api::GetPrinterList;
using electron::api::GetPrinterListAsync;
#endif

void Initialize(v8::Local<v8::Object> exports,
Expand All @@ -71,6 +99,8 @@ void Initialize(v8::Local<v8::Object> exports,
gin_helper::Dictionary dict(isolate, exports);
#if BUILDFLAG(ENABLE_PRINTING)
dict.SetMethod("getPrinterList", base::BindRepeating(&GetPrinterList));
dict.SetMethod("getPrinterListAsync",
base::BindRepeating(&GetPrinterListAsync));
#endif
}

Expand Down
10 changes: 10 additions & 0 deletions spec-main/api-web-contents-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,16 @@ describe('webContents module', () => {
});
});

ifdescribe(features.isPrintingEnabled())('getPrintersAsync()', async () => {
afterEach(closeAllWindows);
it('can get printer list', async () => {
const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true } });
await w.loadURL('about:blank');
const printers = await w.webContents.getPrintersAsync();
expect(printers).to.be.an('array');
});
});

ifdescribe(features.isPrintingEnabled())('printToPDF()', () => {
let w: BrowserWindow;

Expand Down
1 change: 1 addition & 0 deletions typings/internal-electron.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ declare namespace Electron {
_printToPDF(options: any): Promise<Buffer>;
_print(options: any, callback?: (success: boolean, failureReason: string) => void): void;
_getPrinters(): Electron.PrinterInfo[];
_getPrintersAsync(): Promise<Electron.PrinterInfo[]>;
_init(): void;
canGoToIndex(index: number): boolean;
getActiveIndex(): number;
Expand Down

0 comments on commit 77dd7ce

Please sign in to comment.