Skip to content

Commit

Permalink
fix: potential exception when calling webFrameMainBinding.fromIdOrNull()
Browse files Browse the repository at this point in the history
  • Loading branch information
Milan Burda committed Sep 23, 2022
1 parent d357218 commit b9bd74d
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/browser/api/web-contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,14 @@ const addReturnValueToEvent = (event: Electron.IpcMainEvent) => {
});
};

const getWebFrameForEvent = (event: Electron.IpcMainEvent) => {
try {
return webFrameMainBinding.fromIdOrNull(event.processId, event.frameId);
} catch {
return null;
}
};

const commandLine = process._linkedBinding('electron_common_command_line');
const environment = process._linkedBinding('electron_common_environment');

Expand Down Expand Up @@ -574,7 +582,7 @@ WebContents.prototype._init = function () {
} else {
addReplyToEvent(event);
this.emit('ipc-message', event, channel, ...args);
const maybeWebFrame = webFrameMainBinding.fromIdOrNull(event.processId, event.frameId);
const maybeWebFrame = getWebFrameForEvent(event);
maybeWebFrame && maybeWebFrame.ipc.emit(channel, event, ...args);
ipc.emit(channel, event, ...args);
ipcMain.emit(channel, event, ...args);
Expand All @@ -588,7 +596,7 @@ WebContents.prototype._init = function () {
console.error(`Error occurred in handler for '${channel}':`, error);
event.sendReply({ error: error.toString() });
};
const maybeWebFrame = webFrameMainBinding.fromIdOrNull(event.processId, event.frameId);
const maybeWebFrame = getWebFrameForEvent(event);
const targets: (ElectronInternal.IpcMainInternal| undefined)[] = internal ? [ipcMainInternal] : [maybeWebFrame && maybeWebFrame.ipc, ipc, ipcMain];
const target = targets.find(target => target && (target as any)._invokeHandlers.has(channel));
if (target) {
Expand All @@ -605,7 +613,7 @@ WebContents.prototype._init = function () {
ipcMainInternal.emit(channel, event, ...args);
} else {
addReplyToEvent(event);
const maybeWebFrame = webFrameMainBinding.fromIdOrNull(event.processId, event.frameId);
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)) {
console.warn(`WebContents #${this.id} called ipcRenderer.sendSync() with '${channel}' channel without listeners.`);
}
Expand All @@ -619,7 +627,7 @@ WebContents.prototype._init = function () {
this.on('-ipc-ports' as any, function (event: Electron.IpcMainEvent, internal: boolean, channel: string, message: any, ports: any[]) {
addSenderFrameToEvent(event);
event.ports = ports.map(p => new MessagePortMain(p));
const maybeWebFrame = webFrameMainBinding.fromIdOrNull(event.processId, event.frameId);
const maybeWebFrame = getWebFrameForEvent(event);
maybeWebFrame && maybeWebFrame.ipc.emit(channel, event, message);
ipc.emit(channel, event, message);
ipcMain.emit(channel, event, message);
Expand Down

0 comments on commit b9bd74d

Please sign in to comment.