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