2020-07-13 16:58:49 +00:00
|
|
|
import { app } from 'electron/main';
|
2020-10-08 01:01:23 +00:00
|
|
|
import type { WebContents } from 'electron/main';
|
2020-08-24 16:36:13 +00:00
|
|
|
import { clipboard, crashReporter, nativeImage } from 'electron/common';
|
2020-06-25 17:19:08 +00:00
|
|
|
import * as fs from 'fs';
|
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';
|
|
|
|
import * as guestViewManager from '@electron/internal/browser/guest-view-manager';
|
|
|
|
import * as typeUtils from '@electron/internal/common/type-utils';
|
2019-03-16 00:32:04 +00:00
|
|
|
|
2020-06-23 03:32:45 +00:00
|
|
|
const eventBinding = process._linkedBinding('electron_browser_event');
|
2017-01-24 23:05:34 +00:00
|
|
|
|
2020-07-13 16:58:49 +00:00
|
|
|
const emitCustomEvent = function (contents: WebContents, eventName: string, ...args: any[]) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const event = eventBinding.createWithSender(contents);
|
2019-05-01 13:07:57 +00:00
|
|
|
|
2020-07-13 16:58:49 +00:00
|
|
|
app.emit(eventName, event, contents, ...args);
|
2020-03-20 20:28:31 +00:00
|
|
|
contents.emit(eventName, event, ...args);
|
2019-05-01 13:07:57 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
return event;
|
|
|
|
};
|
2019-05-01 13:07:57 +00:00
|
|
|
|
2020-07-13 16:58:49 +00:00
|
|
|
const logStack = function (contents: WebContents, code: string, stack: string) {
|
2019-10-04 17:49:09 +00:00
|
|
|
if (stack) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.warn(`WebContents (${contents.id}): ${code}`, stack);
|
2019-10-04 17:49:09 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2019-10-04 17:49:09 +00:00
|
|
|
|
2016-12-02 01:34:14 +00:00
|
|
|
// Implements window.close()
|
2020-10-08 01:01:23 +00:00
|
|
|
ipcMainInternal.on('ELECTRON_BROWSER_WINDOW_CLOSE', function (event) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const window = event.sender.getOwnerBrowserWindow();
|
2017-08-17 17:56:37 +00:00
|
|
|
if (window) {
|
2020-03-20 20:28:31 +00:00
|
|
|
window.close();
|
2017-08-15 21:59:48 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
event.returnValue = null;
|
|
|
|
});
|
2018-05-21 12:56:05 +00:00
|
|
|
|
2020-10-08 01:01:23 +00:00
|
|
|
ipcMainInternal.handle('ELECTRON_BROWSER_GET_LAST_WEB_PREFERENCES', function (event) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return event.sender.getLastWebPreferences();
|
|
|
|
});
|
2018-10-13 17:50:07 +00:00
|
|
|
|
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':
|
2020-03-20 20:28:31 +00:00
|
|
|
return new Set(['readFindText', 'writeFindText']);
|
2019-03-16 00:32:04 +00:00
|
|
|
case 'linux':
|
2020-03-20 20:28:31 +00:00
|
|
|
return new Set(Object.keys(clipboard));
|
2019-03-16 00:32:04 +00:00
|
|
|
default:
|
2020-03-20 20:28:31 +00:00
|
|
|
return new Set();
|
2019-03-16 00:32:04 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
})();
|
2019-03-16 00:32:04 +00:00
|
|
|
|
2020-10-08 01:01:23 +00:00
|
|
|
ipcMainUtils.handleSync('ELECTRON_BROWSER_CLIPBOARD_SYNC', function (event, method: string, ...args: any[]) {
|
2019-03-16 00:32:04 +00:00
|
|
|
if (!allowedClipboardMethods.has(method)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`Invalid method: ${method}`);
|
2019-03-16 00:32:04 +00:00
|
|
|
}
|
2018-10-13 17:50:07 +00:00
|
|
|
|
2020-07-13 16:58:49 +00:00
|
|
|
return typeUtils.serialize((clipboard as any)[method](...typeUtils.deserialize(args)));
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2018-09-26 05:43:34 +00:00
|
|
|
|
2020-05-10 23:06:07 +00:00
|
|
|
if (BUILDFLAG(ENABLE_DESKTOP_CAPTURER)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const desktopCapturer = require('@electron/internal/browser/desktop-capturer');
|
2019-05-01 13:07:57 +00:00
|
|
|
|
2020-10-08 01:01:23 +00:00
|
|
|
ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', async function (event, options: Electron.SourcesOptions, stack: string) {
|
2020-03-20 20:28:31 +00:00
|
|
|
logStack(event.sender, 'desktopCapturer.getSources()', stack);
|
|
|
|
const customEvent = emitCustomEvent(event.sender, 'desktop-capturer-get-sources');
|
2019-05-01 13:07:57 +00:00
|
|
|
|
|
|
|
if (customEvent.defaultPrevented) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.error('Blocked desktopCapturer.getSources()');
|
|
|
|
return [];
|
2019-05-01 13:07:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 00:25:49 +00:00
|
|
|
return typeUtils.serialize(await desktopCapturer.getSourcesImpl(event, options));
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-05-01 13:07:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 23:06:07 +00:00
|
|
|
const isRemoteModuleEnabled = BUILDFLAG(ENABLE_REMOTE_MODULE)
|
2019-09-18 16:52:06 +00:00
|
|
|
? require('@electron/internal/browser/remote/server').isRemoteModuleEnabled
|
2020-03-20 20:28:31 +00:00
|
|
|
: () => false;
|
2019-09-18 16:52:06 +00:00
|
|
|
|
2020-06-25 17:19:08 +00:00
|
|
|
const getPreloadScript = async function (preloadPath: string) {
|
2020-03-20 20:28:31 +00:00
|
|
|
let preloadSrc = null;
|
|
|
|
let preloadError = null;
|
2019-07-03 15:05:45 +00:00
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
preloadSrc = (await fs.promises.readFile(preloadPath)).toString();
|
2019-10-10 13:59:08 +00:00
|
|
|
} catch (error) {
|
2020-03-20 20:28:31 +00:00
|
|
|
preloadError = error;
|
2018-05-21 12:56:05 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
return { preloadPath, preloadSrc, preloadError };
|
|
|
|
};
|
2019-01-29 01:16:46 +00:00
|
|
|
|
2020-10-08 01:01:23 +00:00
|
|
|
ipcMainUtils.handleSync('ELECTRON_BROWSER_SANDBOX_LOAD', async function (event) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const preloadPaths = event.sender._getPreloadPaths();
|
|
|
|
const webPreferences = event.sender.getLastWebPreferences() || {};
|
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))),
|
2019-02-11 20:42:37 +00:00
|
|
|
isRemoteModuleEnabled: isRemoteModuleEnabled(event.sender),
|
2019-01-08 09:12:34 +00:00
|
|
|
isWebViewTagEnabled: guestViewManager.isWebViewTagEnabled(event.sender),
|
2020-01-06 21:23:03 +00:00
|
|
|
guestInstanceId: webPreferences.guestInstanceId,
|
|
|
|
openerId: webPreferences.openerId,
|
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
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
|
|
|
});
|
2019-01-18 11:03:43 +00:00
|
|
|
|
2020-10-08 01:01:23 +00:00
|
|
|
ipcMainInternal.on('ELECTRON_BROWSER_PRELOAD_ERROR', function (event, preloadPath: string, error: Error) {
|
2020-03-20 20:28:31 +00:00
|
|
|
event.sender.emit('preload-error', event, preloadPath, error);
|
|
|
|
});
|
2020-05-07 20:31:26 +00:00
|
|
|
|
|
|
|
ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_GET_LAST_CRASH_REPORT', () => {
|
2020-07-13 16:58:49 +00:00
|
|
|
return crashReporter.getLastCrashReport();
|
2020-05-07 20:31:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_GET_UPLOADED_REPORTS', () => {
|
2020-07-13 16:58:49 +00:00
|
|
|
return crashReporter.getUploadedReports();
|
2020-05-07 20:31:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_GET_UPLOAD_TO_SERVER', () => {
|
2020-07-13 16:58:49 +00:00
|
|
|
return crashReporter.getUploadToServer();
|
2020-05-07 20:31:26 +00:00
|
|
|
});
|
|
|
|
|
2020-10-08 01:01:23 +00:00
|
|
|
ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_SET_UPLOAD_TO_SERVER', (event, uploadToServer: boolean) => {
|
2020-07-13 16:58:49 +00:00
|
|
|
return crashReporter.setUploadToServer(uploadToServer);
|
2020-05-07 20:31:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_GET_CRASHES_DIRECTORY', () => {
|
2020-07-13 16:58:49 +00:00
|
|
|
return crashReporter.getCrashesDirectory();
|
2020-05-07 20:31:26 +00:00
|
|
|
});
|
2020-08-24 16:36:13 +00:00
|
|
|
|
|
|
|
ipcMainInternal.handle('ELECTRON_NATIVE_IMAGE_CREATE_THUMBNAIL_FROM_PATH', async (_, path: string, size: Electron.Size) => {
|
|
|
|
return typeUtils.serialize(await nativeImage.createThumbnailFromPath(path, size));
|
|
|
|
});
|