From eb630628893748bc304340e452e8c3c8df335b1b Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Wed, 4 Mar 2020 16:22:27 +0900 Subject: [PATCH] fix: properly forward properties to webview (#22510) Co-authored-by: Shelley Vohr --- lib/browser/guest-view-manager.js | 20 +++++++++++++++++++- lib/common/web-view-methods.ts | 8 ++++++++ lib/renderer/web-view/web-view-impl.ts | 21 ++++++++++++++++++++- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/lib/browser/guest-view-manager.js b/lib/browser/guest-view-manager.js index 768c23363ea18..da1f2b09e3147 100644 --- a/lib/browser/guest-view-manager.js +++ b/lib/browser/guest-view-manager.js @@ -4,7 +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, asyncMethods } = require('@electron/internal/common/web-view-methods') +const { syncMethods, asyncMethods, properties } = require('@electron/internal/common/web-view-methods') const { serialize } = require('@electron/internal/common/type-utils') // Doesn't exist in early initialization. @@ -388,6 +388,24 @@ handleMessageSync('ELECTRON_GUEST_VIEW_MANAGER_CALL', function (event, guestInst return guest[method](...args) }) +handleMessageSync('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_GET', function (event, guestInstanceId, property) { + const guest = getGuestForWebContents(guestInstanceId, event.sender) + if (!properties.has(property)) { + throw new Error(`Invalid property: ${property}`) + } + + return guest[property] +}) + +handleMessageSync('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_SET', function (event, guestInstanceId, property, val) { + const guest = getGuestForWebContents(guestInstanceId, event.sender) + if (!properties.has(property)) { + throw new Error(`Invalid property: ${property}`) + } + + guest[property] = val +}) + handleMessage('ELECTRON_GUEST_VIEW_MANAGER_CAPTURE_PAGE', async function (event, guestInstanceId, args) { const guest = getGuestForWebContents(guestInstanceId, event.sender) diff --git a/lib/common/web-view-methods.ts b/lib/common/web-view-methods.ts index 3ddd6e2fa08ab..8018c7a87bbbe 100644 --- a/lib/common/web-view-methods.ts +++ b/lib/common/web-view-methods.ts @@ -50,6 +50,14 @@ export const syncMethods = new Set([ 'setZoomLevel' ]) +export const properties = new Set([ + 'audioMuted', + 'userAgent', + 'zoomLevel', + 'zoomFactor', + 'frameRate' +]) + export const asyncMethods = new Set([ 'loadURL', 'executeJavaScript', diff --git a/lib/renderer/web-view/web-view-impl.ts b/lib/renderer/web-view/web-view-impl.ts index 12229a407ac87..2195ee30f82db 100644 --- a/lib/renderer/web-view/web-view-impl.ts +++ b/lib/renderer/web-view/web-view-impl.ts @@ -4,7 +4,7 @@ import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-in 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, asyncMethods } from '@electron/internal/common/web-view-methods' +import { syncMethods, asyncMethods, properties } from '@electron/internal/common/web-view-methods' import { deserialize } from '@electron/internal/common/type-utils' const { webFrame } = electron @@ -274,6 +274,25 @@ export const setupMethods = (WebViewElement: typeof ElectronInternal.WebViewElem WebViewElement.prototype.capturePage = async function (...args) { return deserialize(await ipcRendererInternal.invoke('ELECTRON_GUEST_VIEW_MANAGER_CAPTURE_PAGE', this.getWebContentsId(), args)) } + + const createPropertyGetter = function (property: string) { + return function (this: ElectronInternal.WebViewElement) { + return ipcRendererUtils.invokeSync('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_GET', this.getWebContentsId(), property) + } + } + + const createPropertySetter = function (property: string) { + return function (this: ElectronInternal.WebViewElement, arg: any) { + return ipcRendererUtils.invokeSync('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_SET', this.getWebContentsId(), property, arg) + } + } + + for (const property of properties) { + Object.defineProperty(WebViewElement.prototype, property, { + get: createPropertyGetter(property) as any, + set: createPropertySetter(property) + }) + } } export const webViewImplModule = {