-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
37 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,53 @@ | ||
import { contextBridge, ipcRenderer } from 'electron'; | ||
import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron'; | ||
|
||
// Whitelist of valid channels used for IPC communication (Send message from Renderer to Main) | ||
const mainAvailChannels: string[] = ['msgRequestGetVersion', 'msgOpenExternalLink']; | ||
const rendererAvailChannels: string[] = ['msgReceivedVersion']; | ||
const rendererAvailChannels: string[] = []; | ||
|
||
contextBridge.exposeInMainWorld('mainApi', { | ||
send: (channel: string, ...data: any[]): void => { | ||
if (mainAvailChannels.includes(channel)) { | ||
ipcRenderer.send.apply(null, [channel, ...data]); | ||
} else { | ||
throw new Error(`Send failed: Unknown ipc channel name: ${channel}`); | ||
throw new Error(`Unknown ipc channel name: ${channel}`); | ||
} | ||
}, | ||
receive: (channel: string, cbFunc: Function): void => { | ||
sendSync: (channel: string, ...data: any[]): void => { | ||
if (mainAvailChannels.includes(channel)) { | ||
return ipcRenderer.sendSync.apply(null, [channel, ...data]); | ||
} | ||
|
||
throw new Error(`Unknown ipc channel name: ${channel}`); | ||
}, | ||
// eslint-disable-next-line no-unused-vars | ||
on: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void): void => { | ||
if (rendererAvailChannels.includes(channel)) { | ||
ipcRenderer.on(channel, listener); | ||
} else { | ||
throw new Error(`Unknown ipc channel name: ${channel}`); | ||
} | ||
}, | ||
// eslint-disable-next-line no-unused-vars | ||
once: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void): void => { | ||
if (rendererAvailChannels.includes(channel)) { | ||
ipcRenderer.once(channel, listener); | ||
} else { | ||
throw new Error(`Unknown ipc channel name: ${channel}`); | ||
} | ||
}, | ||
// eslint-disable-next-line no-unused-vars | ||
off: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void): void => { | ||
if (rendererAvailChannels.includes(channel)) { | ||
ipcRenderer.on(channel, (event, ...args) => cbFunc(event, ...args)); | ||
ipcRenderer.off(channel, listener); | ||
} else { | ||
throw new Error(`Receive failed: Unknown ipc channel name: ${channel}`); | ||
throw new Error(`Unknown ipc channel name: ${channel}`); | ||
} | ||
}, | ||
invoke: async (channel: string, ...data: any[]): Promise<any> => { | ||
if (mainAvailChannels.includes(channel)) { | ||
const result = await ipcRenderer.invoke.apply(null, [channel, ...data]); | ||
return result; | ||
} | ||
throw new Error(`Invoke failed: Unknown ipc channel name: ${channel}`); | ||
throw new Error(`Unknown ipc channel name: ${channel}`); | ||
}, | ||
}); |
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