Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a getScopedToSender API on ipcMain
This solves the usecase where folks for either security or use-case reasons only want to listen for message X coming from webContents Y. Instead of checking the `sender` or `sender.id` each time and hoping you don't miss a listener or writing your own abstraction around IPC (let's be real we've all done that before) this provides a low-overhead first-party API for solving this problem. This use-case has actually come up in internal core code as well so we can benefit from this API internally.
- Loading branch information
1 parent
1c7b302
commit 47e7126
Showing
6 changed files
with
66 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,3 @@ | ||
import { EventEmitter } from 'events' | ||
import { createIndependentIpcMain } from '@electron/internal/browser/ipc-main-creator' | ||
|
||
const emitter = new EventEmitter() | ||
|
||
// Do not throw exception when channel name is "error". | ||
emitter.on('error', () => {}) | ||
|
||
export default emitter | ||
export default createIndependentIpcMain() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { | ||
EventEmitter | ||
} from 'events' | ||
|
||
enum IpcMainHeirachy { | ||
ROOT = 'ROOT', | ||
SCOPED = 'SCOPED', | ||
} | ||
|
||
export const createIndependentIpcMain = () => { | ||
const senderScoped = new Map < number, | ||
IpcMain >() | ||
|
||
class IpcMain extends EventEmitter implements Electron.IpcMain { | ||
constructor (private heirachy: IpcMainHeirachy) { | ||
super() | ||
// Do not throw exception when channel name is "error". | ||
this.on('error', () => {}) | ||
} | ||
|
||
getScopedToSender (webContents: Electron.WebContents) { | ||
const senderId = webContents.id | ||
let scoped = senderScoped.get(senderId) | ||
if (!scoped) { | ||
// Store based on senderId here and don't persist the webContents to avoid it leaking | ||
scoped = new IpcMain(IpcMainHeirachy.SCOPED) | ||
senderScoped.set(senderId, scoped) | ||
} | ||
return scoped | ||
} | ||
|
||
emit (channel: string, event: Electron.IpcMainEvent, ...args: any[]): boolean { | ||
let handled = false | ||
// If this is the root instance of IpcMain and we have an event with a valid sender | ||
// we should try emit the event on the scoped IpcMain instance | ||
if (this.heirachy === IpcMainHeirachy.ROOT && event && event.sender && event.sender.id) { | ||
const scoped = senderScoped.get(event.sender.id) | ||
if (scoped) { | ||
handled = scoped.emit(channel, event, ...args) || handled | ||
} | ||
} | ||
handled = super.emit(channel, event, ...args) || handled | ||
return handled | ||
} | ||
} | ||
|
||
const ipcMain = new IpcMain(IpcMainHeirachy.ROOT) | ||
return ipcMain | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,3 @@ | ||
import { EventEmitter } from 'events' | ||
import { createIndependentIpcMain } from '@electron/internal/browser/ipc-main-creator' | ||
|
||
const emitter = new EventEmitter() | ||
|
||
// Do not throw exception when channel name is "error". | ||
emitter.on('error', () => {}) | ||
|
||
export const ipcMainInternal = emitter as ElectronInternal.IpcMainInternal | ||
export const ipcMainInternal = createIndependentIpcMain() as ElectronInternal.IpcMainInternal |