feat: add WebFrameMain detached property (#43473)
* feat: add WebFrameMain detached property fix: throw instead of returning null senderFrame test: detached frames fix: ensure IPCs of pending deletion RFHs are dispatched fix: lookup WFM by FTN ID to dispatch IPCs feat: add frame.isDestroyed() return null fix: return undefined docs: add null to all frame properties refactor: option c, return null and emit warning refactor: add routingId & processId to navigation events test: null frame property docs: clarify warning message better wording clarify null frame fix: browserwindow spec * maybe fix 🤷 * fix: use updated util #43722 * docs: add notice for frame change of behavior * docs: clarify why frame properties may be null * lint * wip * fix: content::FrameTreeNodeId lookup and converter * refactor: avoid holey array deoptimization --------- Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
This commit is contained in:
parent
527efc01a4
commit
8b3d70a2a3
20 changed files with 410 additions and 126 deletions
|
@ -485,10 +485,6 @@ const addReplyToEvent = (event: Electron.IpcMainEvent) => {
|
|||
|
||||
const addSenderToEvent = (event: Electron.IpcMainEvent | Electron.IpcMainInvokeEvent, sender: Electron.WebContents) => {
|
||||
event.sender = sender;
|
||||
const { processId, frameId } = event;
|
||||
Object.defineProperty(event, 'senderFrame', {
|
||||
get: () => webFrameMain.fromId(processId, frameId)
|
||||
});
|
||||
};
|
||||
|
||||
const addReturnValueToEvent = (event: Electron.IpcMainEvent) => {
|
||||
|
@ -498,11 +494,6 @@ const addReturnValueToEvent = (event: Electron.IpcMainEvent) => {
|
|||
});
|
||||
};
|
||||
|
||||
const getWebFrameForEvent = (event: Electron.IpcMainEvent | Electron.IpcMainInvokeEvent) => {
|
||||
if (!event.processId || !event.frameId) return null;
|
||||
return webFrameMainBinding.fromIdOrNull(event.processId, event.frameId);
|
||||
};
|
||||
|
||||
const commandLine = process._linkedBinding('electron_common_command_line');
|
||||
const environment = process._linkedBinding('electron_common_environment');
|
||||
|
||||
|
@ -580,6 +571,28 @@ WebContents.prototype._init = function () {
|
|||
enumerable: true
|
||||
});
|
||||
|
||||
/**
|
||||
* Cached IPC emitters sorted by dispatch priority.
|
||||
* Caching is used to avoid frequent array allocations.
|
||||
*
|
||||
* 0: WebFrameMain ipc
|
||||
* 1: WebContents ipc
|
||||
* 2: ipcMain
|
||||
*/
|
||||
const cachedIpcEmitters: (ElectronInternal.IpcMainInternal | undefined)[] = [undefined, ipc, ipcMain];
|
||||
|
||||
// Get list of relevant IPC emitters for dispatch.
|
||||
const getIpcEmittersForEvent = (event: Electron.IpcMainEvent | Electron.IpcMainInvokeEvent): (ElectronInternal.IpcMainInternal | undefined)[] => {
|
||||
// Lookup by FrameTreeNode ID to ensure IPCs received after a frame swap are
|
||||
// always received. This occurs when a RenderFrame sends an IPC while it's
|
||||
// unloading and its internal state is pending deletion.
|
||||
const { frameTreeNodeId } = event;
|
||||
const webFrameByFtn = frameTreeNodeId ? webFrameMainBinding._fromFtnIdIfExists(frameTreeNodeId) : undefined;
|
||||
cachedIpcEmitters[0] = webFrameByFtn?.ipc;
|
||||
|
||||
return cachedIpcEmitters;
|
||||
};
|
||||
|
||||
// Add navigationHistory property which handles session history,
|
||||
// maintaining a list of navigation entries for backward and forward navigation.
|
||||
Object.defineProperty(this, 'navigationHistory', {
|
||||
|
@ -610,10 +623,9 @@ WebContents.prototype._init = function () {
|
|||
} else {
|
||||
addReplyToEvent(event);
|
||||
this.emit('ipc-message', event, channel, ...args);
|
||||
const maybeWebFrame = getWebFrameForEvent(event);
|
||||
maybeWebFrame && maybeWebFrame.ipc.emit(channel, event, ...args);
|
||||
ipc.emit(channel, event, ...args);
|
||||
ipcMain.emit(channel, event, ...args);
|
||||
for (const ipcEmitter of getIpcEmittersForEvent(event)) {
|
||||
ipcEmitter?.emit(channel, event, ...args);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -624,9 +636,8 @@ WebContents.prototype._init = function () {
|
|||
console.error(`Error occurred in handler for '${channel}':`, error);
|
||||
event._replyChannel.sendReply({ error: error.toString() });
|
||||
};
|
||||
const maybeWebFrame = getWebFrameForEvent(event);
|
||||
const targets: (ElectronInternal.IpcMainInternal| undefined)[] = internal ? [ipcMainInternal] : [maybeWebFrame?.ipc, ipc, ipcMain];
|
||||
const target = targets.find(target => target && (target as any)._invokeHandlers.has(channel));
|
||||
const targets: (ElectronInternal.IpcMainInternal | undefined)[] = internal ? [ipcMainInternal] : getIpcEmittersForEvent(event);
|
||||
const target = targets.find(target => (target as any)?._invokeHandlers.has(channel));
|
||||
if (target) {
|
||||
const handler = (target as any)._invokeHandlers.get(channel);
|
||||
try {
|
||||
|
@ -646,24 +657,27 @@ WebContents.prototype._init = function () {
|
|||
ipcMainInternal.emit(channel, event, ...args);
|
||||
} else {
|
||||
addReplyToEvent(event);
|
||||
const maybeWebFrame = getWebFrameForEvent(event);
|
||||
if (this.listenerCount('ipc-message-sync') === 0 && ipc.listenerCount(channel) === 0 && ipcMain.listenerCount(channel) === 0 && (!maybeWebFrame || maybeWebFrame.ipc.listenerCount(channel) === 0)) {
|
||||
const ipcEmitters = getIpcEmittersForEvent(event);
|
||||
if (
|
||||
this.listenerCount('ipc-message-sync') === 0 &&
|
||||
ipcEmitters.every(emitter => !emitter || emitter.listenerCount(channel) === 0)
|
||||
) {
|
||||
console.warn(`WebContents #${this.id} called ipcRenderer.sendSync() with '${channel}' channel without listeners.`);
|
||||
}
|
||||
this.emit('ipc-message-sync', event, channel, ...args);
|
||||
maybeWebFrame && maybeWebFrame.ipc.emit(channel, event, ...args);
|
||||
ipc.emit(channel, event, ...args);
|
||||
ipcMain.emit(channel, event, ...args);
|
||||
for (const ipcEmitter of ipcEmitters) {
|
||||
ipcEmitter?.emit(channel, event, ...args);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.on('-ipc-ports', function (this: Electron.WebContents, event: Electron.IpcMainEvent, internal: boolean, channel: string, message: any, ports: any[]) {
|
||||
addSenderToEvent(event, this);
|
||||
event.ports = ports.map(p => new MessagePortMain(p));
|
||||
const maybeWebFrame = getWebFrameForEvent(event);
|
||||
maybeWebFrame && maybeWebFrame.ipc.emit(channel, event, message);
|
||||
ipc.emit(channel, event, message);
|
||||
ipcMain.emit(channel, event, message);
|
||||
const ipcEmitters = getIpcEmittersForEvent(event);
|
||||
for (const ipcEmitter of ipcEmitters) {
|
||||
ipcEmitter?.emit(channel, event, message);
|
||||
}
|
||||
});
|
||||
|
||||
this.on('render-process-gone', (event, details) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue