2020-03-20 20:28:31 +00:00
|
|
|
const { hasSwitch } = process.electronBinding('command_line');
|
|
|
|
const binding = process.electronBinding('context_bridge');
|
2019-10-18 19:57:09 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const contextIsolationEnabled = hasSwitch('context-isolation');
|
2019-10-18 19:57:09 +00:00
|
|
|
|
|
|
|
const checkContextIsolationEnabled = () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (!contextIsolationEnabled) throw new Error('contextBridge API can only be used when contextIsolation is enabled');
|
|
|
|
};
|
2019-10-18 19:57:09 +00:00
|
|
|
|
|
|
|
const contextBridge = {
|
|
|
|
exposeInMainWorld: (key: string, api: Record<string, any>) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
checkContextIsolationEnabled();
|
|
|
|
return binding.exposeAPIInMainWorld(key, api);
|
2019-10-18 19:57:09 +00:00
|
|
|
},
|
|
|
|
debugGC: () => binding._debugGCMaps({})
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2019-10-18 19:57:09 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
if (!binding._debugGCMaps) delete contextBridge.debugGC;
|
2019-10-18 19:57:09 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
export default contextBridge;
|
2020-04-22 19:42:51 +00:00
|
|
|
|
|
|
|
export const internalContextBridge = {
|
|
|
|
contextIsolationEnabled,
|
2020-04-27 19:46:04 +00:00
|
|
|
overrideGlobalValueFromIsolatedWorld: (keys: string[], value: any) => {
|
|
|
|
return binding._overrideGlobalValueFromIsolatedWorld(keys, value, false);
|
|
|
|
},
|
|
|
|
overrideGlobalValueWithDynamicPropsFromIsolatedWorld: (keys: string[], value: any) => {
|
|
|
|
return binding._overrideGlobalValueFromIsolatedWorld(keys, value, true);
|
2020-04-22 19:42:51 +00:00
|
|
|
},
|
|
|
|
overrideGlobalPropertyFromIsolatedWorld: (keys: string[], getter: Function, setter?: Function) => {
|
|
|
|
return binding._overrideGlobalPropertyFromIsolatedWorld(keys, getter, setter || null);
|
|
|
|
},
|
|
|
|
isInMainWorld: () => binding._isCalledFromMainWorld() as boolean
|
|
|
|
};
|