refactor: replace webFrame.routingId with sync IPC (#47717)

* refactor: replace webFrame.routingId with sync IPC

* fix: GetConstructor missing isolate

* fix: missing isolate
This commit is contained in:
Sam Maddock
2025-08-02 11:00:42 -04:00
committed by GitHub
parent 5c98e3609f
commit bdaf3b9a2b
6 changed files with 88 additions and 42 deletions

View File

@@ -3,6 +3,7 @@ import * as ipcMainUtils from '@electron/internal/browser/ipc-main-internal-util
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
import { clipboard } from 'electron/common';
import { webFrameMain } from 'electron/main';
import * as fs from 'fs';
import * as path from 'path';
@@ -109,3 +110,17 @@ ipcMainInternal.on(IPC_MESSAGES.BROWSER_PRELOAD_ERROR, function (event, preloadP
if (event.type !== 'frame') return;
event.sender?.emit('preload-error', event, preloadPath, error);
});
ipcMainUtils.handleSync(IPC_MESSAGES.BROWSER_GET_FRAME_ROUTING_ID_SYNC, function (event, frameToken: string) {
if (event.type !== 'frame') return;
const senderFrame = event.senderFrame;
if (!senderFrame || senderFrame.isDestroyed()) return;
return webFrameMain.fromFrameToken(senderFrame.processId, frameToken)?.routingId;
});
ipcMainUtils.handleSync(IPC_MESSAGES.BROWSER_GET_FRAME_TOKEN_SYNC, function (event, routingId: number) {
if (event.type !== 'frame') return;
const senderFrame = event.senderFrame;
if (!senderFrame || senderFrame.isDestroyed()) return;
return webFrameMain.fromId(senderFrame.processId, routingId)?.frameToken;
});

View File

@@ -6,6 +6,8 @@ export const enum IPC_MESSAGES {
BROWSER_NONSANDBOX_LOAD = 'BROWSER_NONSANDBOX_LOAD',
BROWSER_WINDOW_CLOSE = 'BROWSER_WINDOW_CLOSE',
BROWSER_GET_PROCESS_MEMORY_INFO = 'BROWSER_GET_PROCESS_MEMORY_INFO',
BROWSER_GET_FRAME_ROUTING_ID_SYNC = 'BROWSER_GET_FRAME_ROUTING_ID_SYNC',
BROWSER_GET_FRAME_TOKEN_SYNC = 'BROWSER_GET_FRAME_TOKEN_SYNC',
GUEST_INSTANCE_VISIBILITY_CHANGE = 'GUEST_INSTANCE_VISIBILITY_CHANGE',

View File

@@ -1,3 +1,36 @@
const { mainFrame } = process._linkedBinding('electron_renderer_web_frame');
import * as deprecate from '@electron/internal/common/deprecate';
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils';
const { mainFrame, WebFrame } = process._linkedBinding('electron_renderer_web_frame');
// @ts-expect-error - WebFrame types are cursed. It's an instanced class, but
// the docs define it as a static module.
// TODO(smaddock): Fix web-frame.md to define it as an instance class.
const WebFramePrototype: Electron.WebFrame = WebFrame.prototype;
const routingIdDeprecated = deprecate.warnOnce('webFrame.routingId', 'webFrame.frameToken');
Object.defineProperty(WebFramePrototype, 'routingId', {
configurable: true,
get: function (this: Electron.WebFrame) {
routingIdDeprecated();
return ipcRendererUtils.invokeSync<number>(
IPC_MESSAGES.BROWSER_GET_FRAME_ROUTING_ID_SYNC,
this.frameToken
);
}
});
const findFrameByRoutingIdDeprecated = deprecate.warnOnce('webFrame.findFrameByRoutingId', 'webFrame.findFrameByToken');
WebFramePrototype.findFrameByRoutingId = function (
routingId: number
): Electron.WebFrame | null {
findFrameByRoutingIdDeprecated();
const frameToken = ipcRendererUtils.invokeSync<string | undefined>(
IPC_MESSAGES.BROWSER_GET_FRAME_TOKEN_SYNC,
routingId
);
return frameToken ? this.findFrameByToken(frameToken) : null;
};
export default mainFrame;