2021-08-27 15:21:36 -06:00
|
|
|
import { clipboard } from 'electron/common';
|
2020-06-25 11:19:08 -06:00
|
|
|
import * as fs from 'fs';
|
2020-07-13 10:58:49 -06:00
|
|
|
import { ipcMainInternal } from '@electron/internal/browser/ipc-main-internal';
|
|
|
|
import * as ipcMainUtils from '@electron/internal/browser/ipc-main-internal-utils';
|
2020-10-13 15:11:06 -06:00
|
|
|
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';
|
2019-03-15 18:32:04 -06:00
|
|
|
|
2016-12-01 18:34:14 -07:00
|
|
|
// Implements window.close()
|
2020-10-13 15:11:06 -06:00
|
|
|
ipcMainInternal.on(IPC_MESSAGES.BROWSER_WINDOW_CLOSE, function (event) {
|
2020-03-20 14:28:31 -06:00
|
|
|
const window = event.sender.getOwnerBrowserWindow();
|
2017-08-17 11:56:37 -06:00
|
|
|
if (window) {
|
2020-03-20 14:28:31 -06:00
|
|
|
window.close();
|
2017-08-15 15:59:48 -06:00
|
|
|
}
|
2020-03-20 14:28:31 -06:00
|
|
|
event.returnValue = null;
|
|
|
|
});
|
2018-05-21 06:56:05 -06:00
|
|
|
|
2020-10-13 15:11:06 -06:00
|
|
|
ipcMainInternal.handle(IPC_MESSAGES.BROWSER_GET_LAST_WEB_PREFERENCES, function (event) {
|
2020-03-20 14:28:31 -06:00
|
|
|
return event.sender.getLastWebPreferences();
|
|
|
|
});
|
2018-10-13 11:50:07 -06:00
|
|
|
|
2021-10-05 16:30:31 -06:00
|
|
|
ipcMainInternal.handle(IPC_MESSAGES.BROWSER_GET_PROCESS_MEMORY_INFO, function (event) {
|
|
|
|
return event.sender._getProcessMemoryInfo();
|
|
|
|
});
|
|
|
|
|
2019-03-15 18:32:04 -06:00
|
|
|
// Methods not listed in this set are called directly in the renderer process.
|
|
|
|
const allowedClipboardMethods = (() => {
|
|
|
|
switch (process.platform) {
|
|
|
|
case 'darwin':
|
2020-03-20 14:28:31 -06:00
|
|
|
return new Set(['readFindText', 'writeFindText']);
|
2019-03-15 18:32:04 -06:00
|
|
|
case 'linux':
|
2020-03-20 14:28:31 -06:00
|
|
|
return new Set(Object.keys(clipboard));
|
2019-03-15 18:32:04 -06:00
|
|
|
default:
|
2020-03-20 14:28:31 -06:00
|
|
|
return new Set();
|
2019-03-15 18:32:04 -06:00
|
|
|
}
|
2020-03-20 14:28:31 -06:00
|
|
|
})();
|
2019-03-15 18:32:04 -06:00
|
|
|
|
2020-10-13 15:11:06 -06:00
|
|
|
ipcMainUtils.handleSync(IPC_MESSAGES.BROWSER_CLIPBOARD_SYNC, function (event, method: string, ...args: any[]) {
|
2019-03-15 18:32:04 -06:00
|
|
|
if (!allowedClipboardMethods.has(method)) {
|
2020-03-20 14:28:31 -06:00
|
|
|
throw new Error(`Invalid method: ${method}`);
|
2019-03-15 18:32:04 -06:00
|
|
|
}
|
2018-10-13 11:50:07 -06:00
|
|
|
|
2021-09-07 11:37:45 -06:00
|
|
|
return (clipboard as any)[method](...args);
|
2020-03-20 14:28:31 -06:00
|
|
|
});
|
2018-09-25 23:43:34 -06:00
|
|
|
|
2020-06-25 11:19:08 -06:00
|
|
|
const getPreloadScript = async function (preloadPath: string) {
|
2020-03-20 14:28:31 -06:00
|
|
|
let preloadSrc = null;
|
|
|
|
let preloadError = null;
|
2019-07-03 09:05:45 -06:00
|
|
|
try {
|
2021-02-08 10:32:20 -07:00
|
|
|
preloadSrc = await fs.promises.readFile(preloadPath, 'utf8');
|
2019-10-10 07:59:08 -06:00
|
|
|
} catch (error) {
|
2020-03-20 14:28:31 -06:00
|
|
|
preloadError = error;
|
2018-05-21 06:56:05 -06:00
|
|
|
}
|
2020-03-20 14:28:31 -06:00
|
|
|
return { preloadPath, preloadSrc, preloadError };
|
|
|
|
};
|
2019-01-28 18:16:46 -07:00
|
|
|
|
2020-10-13 15:11:06 -06:00
|
|
|
ipcMainUtils.handleSync(IPC_MESSAGES.BROWSER_SANDBOX_LOAD, async function (event) {
|
2020-03-20 14:28:31 -06:00
|
|
|
const preloadPaths = event.sender._getPreloadPaths();
|
2020-01-06 14:23:03 -07:00
|
|
|
|
2019-03-08 03:21:41 -07:00
|
|
|
return {
|
|
|
|
preloadScripts: await Promise.all(preloadPaths.map(path => getPreloadScript(path))),
|
2018-08-21 12:05:45 -06:00
|
|
|
process: {
|
|
|
|
arch: process.arch,
|
|
|
|
platform: process.platform,
|
2019-10-09 11:59:08 -06:00
|
|
|
env: { ...process.env },
|
2018-08-21 12:05:45 -06:00
|
|
|
version: process.version,
|
2018-11-15 13:59:01 -07:00
|
|
|
versions: process.versions,
|
|
|
|
execPath: process.helperExecPath
|
2018-08-21 12:05:45 -06:00
|
|
|
}
|
2020-03-20 14:28:31 -06:00
|
|
|
};
|
|
|
|
});
|
2019-01-18 04:03:43 -07:00
|
|
|
|
2022-06-15 14:22:28 -06:00
|
|
|
ipcMainUtils.handleSync(IPC_MESSAGES.BROWSER_NONSANDBOX_LOAD, function (event) {
|
|
|
|
return { preloadPaths: event.sender._getPreloadPaths() };
|
|
|
|
});
|
|
|
|
|
2020-10-13 15:11:06 -06:00
|
|
|
ipcMainInternal.on(IPC_MESSAGES.BROWSER_PRELOAD_ERROR, function (event, preloadPath: string, error: Error) {
|
2020-03-20 14:28:31 -06:00
|
|
|
event.sender.emit('preload-error', event, preloadPath, error);
|
|
|
|
});
|