From deebde66f92d92aa502897a87911e24cf0d311d0 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Mon, 17 Jun 2019 11:10:02 +0200 Subject: [PATCH] feat: make async webContents / methods return a Promise (#18792) --- docs/api/web-contents.md | 8 ++++++++ docs/api/webview-tag.md | 14 ++++++++++++++ lib/browser/api/web-contents.js | 2 +- lib/browser/guest-view-manager.js | 12 ++---------- lib/common/web-view-methods.js | 11 ++++------- lib/renderer/web-view/web-view-impl.ts | 20 +++----------------- 6 files changed, 32 insertions(+), 35 deletions(-) diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index bf78df103b..f104a7d9c2 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -987,6 +987,8 @@ Returns `String` - The user agent for this web page. * `css` String +Returns `Promise` + Injects CSS into the current web page. ```js @@ -1065,6 +1067,8 @@ Returns `Number` - the current zoom level. * `minimumLevel` Number * `maximumLevel` Number +Returns `Promise` + Sets the maximum and minimum pinch-to-zoom level. > **NOTE**: Visual zoom is disabled by default in Electron. To re-enable it, call: @@ -1078,6 +1082,8 @@ Sets the maximum and minimum pinch-to-zoom level. * `minimumLevel` Number * `maximumLevel` Number +Returns `Promise` + Sets the maximum and minimum layout-based (i.e. non-visual) zoom level. #### `contents.undo()` @@ -1139,6 +1145,8 @@ Executes the editing command `replaceMisspelling` in web page. * `text` String +Returns `Promise` + Inserts `text` to the focused element. #### `contents.findInPage(text[, options])` diff --git a/docs/api/webview-tag.md b/docs/api/webview-tag.md index 7ee8c846f2..04de299ec6 100644 --- a/docs/api/webview-tag.md +++ b/docs/api/webview-tag.md @@ -379,6 +379,8 @@ Returns `String` - The user agent for guest page. * `css` String +Returns `Promise` + Injects CSS into the guest page. ### `.executeJavaScript(code[, userGesture])` @@ -490,6 +492,8 @@ Executes editing command `replaceMisspelling` in page. * `text` String +Returns `Promise` + Inserts `text` to the focused element. ### `.findInPage(text[, options])` @@ -531,6 +535,8 @@ Stops any `findInPage` request for the `webview` with the provided `action`. the web page. Default is `false`. * `deviceName` String (optional) - Set the printer device name to use. Default is `''`. +Returns `Promise` + Prints `webview`'s web page. Same as `webContents.print([options])`. ### `.printToPDF(options)` @@ -562,6 +568,8 @@ Captures a snapshot of the page within `rect`. Omitting `rect` will capture the * `channel` String * `...args` any[] +Returns `Promise` + Send an asynchronous message to renderer process via `channel`, you can also send arbitrary arguments. The renderer process can handle the message by listening to the `channel` event with the [`ipcRenderer`](ipc-renderer.md) module. @@ -573,6 +581,8 @@ examples. * `event` Object +Returns `Promise` + Sends an input `event` to the page. See [webContents.sendInputEvent](web-contents.md#contentssendinputeventevent) @@ -607,6 +617,8 @@ Returns `Number` - the current zoom level. * `minimumLevel` Number * `maximumLevel` Number +Returns `Promise` + Sets the maximum and minimum pinch-to-zoom level. ### `.setLayoutZoomLevelLimits(minimumLevel, maximumLevel)` @@ -614,6 +626,8 @@ Sets the maximum and minimum pinch-to-zoom level. * `minimumLevel` Number * `maximumLevel` Number +Returns `Promise` + Sets the maximum and minimum layout-based (i.e. non-visual) zoom level. ### `.showDefinitionForSelection()` _macOS_ diff --git a/lib/browser/api/web-contents.js b/lib/browser/api/web-contents.js index 7cd3e6a5f2..e2abc59696 100644 --- a/lib/browser/api/web-contents.js +++ b/lib/browser/api/web-contents.js @@ -180,7 +180,7 @@ const webFrameMethods = [ for (const method of webFrameMethods) { WebContents.prototype[method] = function (...args) { - ipcMainUtils.invokeInWebContents(this, false, 'ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, ...args) + return ipcMainUtils.invokeInWebContents(this, false, 'ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, ...args) } } diff --git a/lib/browser/guest-view-manager.js b/lib/browser/guest-view-manager.js index af6a4e1ae0..26d8eda2bd 100644 --- a/lib/browser/guest-view-manager.js +++ b/lib/browser/guest-view-manager.js @@ -4,11 +4,7 @@ const { webContents } = require('electron') const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal') const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils') const parseFeaturesString = require('@electron/internal/common/parse-features-string') -const { - syncMethods, - asyncCallbackMethods, - asyncPromiseMethods -} = require('@electron/internal/common/web-view-methods') +const { syncMethods, asyncMethods } = require('@electron/internal/common/web-view-methods') // Doesn't exist in early initialization. let webViewManager = null @@ -371,11 +367,7 @@ ipcMainInternal.on('ELECTRON_GUEST_VIEW_MANAGER_FOCUS_CHANGE', function (event, } }) -const allMethods = new Set([ - ...syncMethods, - ...asyncCallbackMethods, - ...asyncPromiseMethods -]) +const allMethods = new Set([ ...syncMethods, ...asyncMethods ]) handleMessage('ELECTRON_GUEST_VIEW_MANAGER_CALL', function (event, guestInstanceId, method, args) { const guest = getGuestForWebContents(guestInstanceId, event.sender) diff --git a/lib/common/web-view-methods.js b/lib/common/web-view-methods.js index 3ee1d3211c..50e68f4e8a 100644 --- a/lib/common/web-view-methods.js +++ b/lib/common/web-view-methods.js @@ -53,18 +53,15 @@ exports.syncMethods = new Set([ 'setZoomLevel' ]) -exports.asyncCallbackMethods = new Set([ +exports.asyncMethods = new Set([ + 'capturePage', + 'executeJavaScript', 'insertCSS', 'insertText', 'send', 'sendInputEvent', 'setLayoutZoomLevelLimits', 'setVisualZoomLevelLimits', - 'print' -]) - -exports.asyncPromiseMethods = new Set([ - 'capturePage', - 'executeJavaScript', + 'print', 'printToPDF' ]) diff --git a/lib/renderer/web-view/web-view-impl.ts b/lib/renderer/web-view/web-view-impl.ts index 94e8f32b42..ab59234e0e 100644 --- a/lib/renderer/web-view/web-view-impl.ts +++ b/lib/renderer/web-view/web-view-impl.ts @@ -3,11 +3,7 @@ import { remote, webFrame } from 'electron' import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils' import * as guestViewInternal from '@electron/internal/renderer/web-view/guest-view-internal' import { WEB_VIEW_CONSTANTS } from '@electron/internal/renderer/web-view/web-view-constants' -import { - syncMethods, - asyncCallbackMethods, - asyncPromiseMethods -} from '@electron/internal/common/web-view-methods' +import { syncMethods, asyncMethods } from '@electron/internal/common/web-view-methods' const v8Util = process.electronBinding('v8_util') @@ -257,23 +253,13 @@ export const setupMethods = (WebViewElement: typeof ElectronInternal.WebViewElem } const createNonBlockHandler = function (method: string) { - return function (this: ElectronInternal.WebViewElement, ...args: Array) { - ipcRendererUtils.invoke('ELECTRON_GUEST_VIEW_MANAGER_CALL', this.getWebContentsId(), method, args) - } - } - - for (const method of asyncCallbackMethods) { - (WebViewElement.prototype as Record)[method] = createNonBlockHandler(method) - } - - const createPromiseHandler = function (method: string) { return function (this: ElectronInternal.WebViewElement, ...args: Array) { return ipcRendererUtils.invoke('ELECTRON_GUEST_VIEW_MANAGER_CALL', this.getWebContentsId(), method, args) } } - for (const method of asyncPromiseMethods) { - (WebViewElement.prototype as Record)[method] = createPromiseHandler(method) + for (const method of asyncMethods) { + (WebViewElement.prototype as Record)[method] = createNonBlockHandler(method) } }