electron/lib/renderer/api/context-bridge.ts
Samuel Attard 96bf9ce77f
refactor: port parts of window-setup to use ctx bridge instead of being run in the main world (#23194)
* refactor: port parts of window-setup to use ctx bridge instead of being run in the main world

* chore: update ctx bridge specs for new base numbers
2020-04-22 12:42:51 -07:00

31 lines
1.1 KiB
TypeScript

const { hasSwitch } = process.electronBinding('command_line');
const binding = process.electronBinding('context_bridge');
const contextIsolationEnabled = hasSwitch('context-isolation');
const checkContextIsolationEnabled = () => {
if (!contextIsolationEnabled) throw new Error('contextBridge API can only be used when contextIsolation is enabled');
};
const contextBridge = {
exposeInMainWorld: (key: string, api: Record<string, any>) => {
checkContextIsolationEnabled();
return binding.exposeAPIInMainWorld(key, api);
},
debugGC: () => binding._debugGCMaps({})
};
if (!binding._debugGCMaps) delete contextBridge.debugGC;
export default contextBridge;
export const internalContextBridge = {
contextIsolationEnabled,
overrideGlobalMethodFromIsolatedWorld: (keys: string[], method: Function) => {
return binding._overrideGlobalMethodFromIsolatedWorld(keys, method);
},
overrideGlobalPropertyFromIsolatedWorld: (keys: string[], getter: Function, setter?: Function) => {
return binding._overrideGlobalPropertyFromIsolatedWorld(keys, getter, setter || null);
},
isInMainWorld: () => binding._isCalledFromMainWorld() as boolean
};