Skip to content

Commit efc1156

Browse files
authored
fix: properly forward properties to webview (electron#22485)
1 parent 8352c39 commit efc1156

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

lib/browser/guest-view-manager.js

+19-1
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.
@@ -384,6 +384,24 @@ handleMessageSync('ELECTRON_GUEST_VIEW_MANAGER_CALL', function (event, guestInst
384384
return guest[method](...args)
385385
})
386386

387+
handleMessageSync('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_GET', function (event, guestInstanceId, property) {
388+
const guest = getGuestForWebContents(guestInstanceId, event.sender)
389+
if (!properties.has(property)) {
390+
throw new Error(`Invalid property: ${property}`)
391+
}
392+
393+
return guest[property]
394+
})
395+
396+
handleMessageSync('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_SET', function (event, guestInstanceId, property, val) {
397+
const guest = getGuestForWebContents(guestInstanceId, event.sender)
398+
if (!properties.has(property)) {
399+
throw new Error(`Invalid property: ${property}`)
400+
}
401+
402+
guest[property] = val
403+
})
404+
387405
handleMessage('ELECTRON_GUEST_VIEW_MANAGER_CAPTURE_PAGE', async function (event, guestInstanceId, args) {
388406
const guest = getGuestForWebContents(guestInstanceId, event.sender)
389407

lib/common/web-view-methods.ts

+8
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

+20-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-in
44
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils'
55
import * as guestViewInternal from '@electron/internal/renderer/web-view/guest-view-internal'
66
import { WEB_VIEW_CONSTANTS } from '@electron/internal/renderer/web-view/web-view-constants'
7-
import { syncMethods, asyncMethods } from '@electron/internal/common/web-view-methods'
7+
import { syncMethods, asyncMethods, properties } from '@electron/internal/common/web-view-methods'
88
import { deserialize } from '@electron/internal/common/type-utils'
99
const { webFrame } = electron
1010

@@ -249,6 +249,25 @@ export const setupMethods = (WebViewElement: typeof ElectronInternal.WebViewElem
249249
WebViewElement.prototype.capturePage = async function (...args) {
250250
return deserialize(await ipcRendererInternal.invoke('ELECTRON_GUEST_VIEW_MANAGER_CAPTURE_PAGE', this.getWebContentsId(), args))
251251
}
252+
253+
const createPropertyGetter = function (property: string) {
254+
return function (this: ElectronInternal.WebViewElement) {
255+
return ipcRendererUtils.invokeSync('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_GET', this.getWebContentsId(), property)
256+
}
257+
}
258+
259+
const createPropertySetter = function (property: string) {
260+
return function (this: ElectronInternal.WebViewElement, arg: any) {
261+
return ipcRendererUtils.invokeSync('ELECTRON_GUEST_VIEW_MANAGER_PROPERTY_SET', this.getWebContentsId(), property, arg)
262+
}
263+
}
264+
265+
for (const property of properties) {
266+
Object.defineProperty(WebViewElement.prototype, property, {
267+
get: createPropertyGetter(property) as any,
268+
set: createPropertySetter(property)
269+
})
270+
}
252271
}
253272

254273
export const webViewImplModule = {

0 commit comments

Comments
 (0)