Skip to content

Commit 5736df4

Browse files
zcbenzcodebytere
andauthored
fix: properly forward properties to webview (electron#22512)
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
1 parent fbbe8af commit 5736df4

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

lib/browser/guest-view-manager.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { webContents } = require('electron')
44
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal')
55
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils')
66
const parseFeaturesString = require('@electron/internal/common/parse-features-string')
7-
const { syncMethods, asyncMethods } = require('@electron/internal/common/web-view-methods')
7+
const { syncMethods, asyncMethods, properties } = require('@electron/internal/common/web-view-methods')
88
const { serialize } = require('@electron/internal/common/type-utils')
99

1010
// Doesn't exist in early initialization.
@@ -357,6 +357,24 @@ handleMessage('ELECTRON_GUEST_VIEW_MANAGER_CALL', function (event, guestInstance
357357
return guest[method](...args)
358358
})
359359

360+
handleMessage('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_GET', function (event, guestInstanceId, property) {
361+
const guest = getGuestForWebContents(guestInstanceId, event.sender)
362+
if (!properties.has(property)) {
363+
throw new Error(`Invalid property: ${property}`)
364+
}
365+
366+
return guest[property]
367+
})
368+
369+
handleMessage('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_SET', function (event, guestInstanceId, property, val) {
370+
const guest = getGuestForWebContents(guestInstanceId, event.sender)
371+
if (!properties.has(property)) {
372+
throw new Error(`Invalid property: ${property}`)
373+
}
374+
375+
guest[property] = val
376+
})
377+
360378
handleMessage('ELECTRON_GUEST_VIEW_MANAGER_CAPTURE_PAGE', async function (event, guestInstanceId, args) {
361379
const guest = getGuestForWebContents(guestInstanceId, event.sender)
362380

lib/common/web-view-methods.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ export const syncMethods = new Set([
5050
'setZoomLevel'
5151
])
5252

53+
export const properties = new Set([
54+
'audioMuted',
55+
'userAgent',
56+
'zoomLevel',
57+
'zoomFactor',
58+
'frameRate'
59+
])
60+
5361
export const asyncMethods = new Set([
5462
'loadURL',
5563
'executeJavaScript',

lib/renderer/web-view/web-view-impl.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { remote, webFrame } from 'electron'
33
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils'
44
import * as guestViewInternal from '@electron/internal/renderer/web-view/guest-view-internal'
55
import { WEB_VIEW_CONSTANTS } from '@electron/internal/renderer/web-view/web-view-constants'
6-
import { syncMethods, asyncMethods } from '@electron/internal/common/web-view-methods'
6+
import { syncMethods, asyncMethods, properties } from '@electron/internal/common/web-view-methods'
77
import { deserialize } from '@electron/internal/common/type-utils'
88

99
const v8Util = process.electronBinding('v8_util')
@@ -265,6 +265,25 @@ export const setupMethods = (WebViewElement: typeof ElectronInternal.WebViewElem
265265
WebViewElement.prototype.capturePage = async function (...args) {
266266
return deserialize(await ipcRendererUtils.invoke('ELECTRON_GUEST_VIEW_MANAGER_CAPTURE_PAGE', this.getWebContentsId(), args))
267267
}
268+
269+
const createPropertyGetter = function (property: string) {
270+
return function (this: ElectronInternal.WebViewElement) {
271+
return ipcRendererUtils.invokeSync('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_GET', this.getWebContentsId(), property)
272+
}
273+
}
274+
275+
const createPropertySetter = function (property: string) {
276+
return function (this: ElectronInternal.WebViewElement, arg: any) {
277+
return ipcRendererUtils.invokeSync('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_SET', this.getWebContentsId(), property, arg)
278+
}
279+
}
280+
281+
for (const property of properties) {
282+
Object.defineProperty(WebViewElement.prototype, property, {
283+
get: createPropertyGetter(property) as any,
284+
set: createPropertySetter(property)
285+
})
286+
}
268287
}
269288

270289
export const webViewImplModule = {

0 commit comments

Comments
 (0)