2020-06-25 17:19:08 +00:00
|
|
|
import * as electron from 'electron';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import { ipcMainInternal } from './ipc-main-internal';
|
|
|
|
import * as ipcMainUtils from './ipc-main-internal-utils';
|
|
|
|
import * as guestViewManager from './guest-view-manager';
|
|
|
|
import * as typeUtils from '../common/type-utils';
|
|
|
|
import { IpcMainInvokeEvent } from 'electron/main';
|
2019-03-16 00:32:04 +00:00
|
|
|
|
2020-06-23 03:32:45 +00:00
|
|
|
const eventBinding = process._linkedBinding('electron_browser_event');
|
|
|
|
const clipboard = process._linkedBinding('electron_common_clipboard');
|
2017-01-24 23:05:34 +00:00
|
|
|
|
2020-06-25 17:19:08 +00:00
|
|
|
const emitCustomEvent = function (contents: electron.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-03-20 20:28:31 +00:00
|
|
|
electron.app.emit(eventName, event, contents, ...args);
|
|
|
|
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-06-25 17:19:08 +00:00
|
|
|
const logStack = function (contents: electron.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-06-25 17:19:08 +00:00
|
|
|
ipcMainInternal.on('ELECTRON_BROWSER_WINDOW_CLOSE', function (event: ElectronInternal.IpcMainInternalEvent) {
|
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-06-25 17:19:08 +00:00
|
|
|
ipcMainInternal.handle('ELECTRON_BROWSER_GET_LAST_WEB_PREFERENCES', function (event: IpcMainInvokeEvent) {
|
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-06-25 17:19:08 +00:00
|
|
|
ipcMainUtils.handleSync('ELECTRON_BROWSER_CLIPBOARD_SYNC', function (event: IpcMainInvokeEvent, 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-06-25 17:19:08 +00:00
|
|
|
return typeUtils.serialize((electron.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-06-25 17:19:08 +00:00
|
|
|
ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', async function (event: IpcMainInvokeEvent, 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
|
|
|
});
|
2020-05-26 20:34:24 +00:00
|
|
|
|
2020-06-25 17:19:08 +00:00
|
|
|
ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_MEDIA_SOURCE_ID_FOR_WEB_CONTENTS', function (event: IpcMainInvokeEvent, webContentsId: number) {
|
2020-05-26 20:34:24 +00:00
|
|
|
return desktopCapturer.getMediaSourceIdForWebContents(event, webContentsId);
|
|
|
|
});
|
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-06-25 17:19:08 +00:00
|
|
|
ipcMainUtils.handleSync('ELECTRON_BROWSER_SANDBOX_LOAD', async function (event: IpcMainInvokeEvent) {
|
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-06-25 17:19:08 +00:00
|
|
|
ipcMainInternal.on('ELECTRON_BROWSER_PRELOAD_ERROR', function (event: ElectronInternal.IpcMainInternalEvent, 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', () => {
|
|
|
|
return electron.crashReporter.getLastCrashReport();
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_GET_UPLOADED_REPORTS', () => {
|
|
|
|
return electron.crashReporter.getUploadedReports();
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_GET_UPLOAD_TO_SERVER', () => {
|
|
|
|
return electron.crashReporter.getUploadToServer();
|
|
|
|
});
|
|
|
|
|
2020-06-25 17:19:08 +00:00
|
|
|
ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_SET_UPLOAD_TO_SERVER', (event: IpcMainInvokeEvent, uploadToServer: boolean) => {
|
2020-05-07 20:31:26 +00:00
|
|
|
return electron.crashReporter.setUploadToServer(uploadToServer);
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_GET_CRASHES_DIRECTORY', () => {
|
|
|
|
return electron.crashReporter.getCrashesDirectory();
|
|
|
|
});
|