2020-03-20 20:28:31 +00:00
|
|
|
'use strict';
|
2016-01-13 09:07:18 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const electron = require('electron');
|
|
|
|
const fs = require('fs');
|
2019-03-16 00:32:04 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const eventBinding = process.electronBinding('event');
|
|
|
|
const clipboard = process.electronBinding('clipboard');
|
|
|
|
const features = process.electronBinding('features');
|
2017-01-24 23:05:34 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const { crashReporterInit } = require('@electron/internal/browser/crash-reporter-init');
|
|
|
|
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal');
|
|
|
|
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils');
|
|
|
|
const guestViewManager = require('@electron/internal/browser/guest-view-manager');
|
|
|
|
const typeUtils = require('@electron/internal/common/type-utils');
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2019-05-01 13:07:57 +00:00
|
|
|
const emitCustomEvent = function (contents, eventName, ...args) {
|
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
|
|
|
|
2019-10-04 17:49:09 +00:00
|
|
|
const logStack = function (contents, code, stack) {
|
|
|
|
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()
|
2019-02-04 22:49:53 +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
|
|
|
|
2019-08-23 22:45:50 +00:00
|
|
|
ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_INIT', function (event, options) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return crashReporterInit(options);
|
|
|
|
});
|
2018-10-13 17:50:07 +00:00
|
|
|
|
2019-08-23 22:45:50 +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-04-13 23:39:26 +00:00
|
|
|
ipcMainUtils.handleSync('ELECTRON_BROWSER_CLIPBOARD_SYNC', function (event, method, ...args) {
|
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-03-20 20:28:31 +00:00
|
|
|
return typeUtils.serialize(electron.clipboard[method](...typeUtils.deserialize(args)));
|
|
|
|
});
|
2018-09-26 05:43:34 +00:00
|
|
|
|
2019-05-01 13:07:57 +00:00
|
|
|
if (features.isDesktopCapturerEnabled()) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const desktopCapturer = require('@electron/internal/browser/desktop-capturer');
|
2019-05-01 13:07:57 +00:00
|
|
|
|
2019-10-04 17:49:09 +00:00
|
|
|
ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', function (event, options, stack) {
|
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-03-20 20:28:31 +00:00
|
|
|
return desktopCapturer.getSources(event, options);
|
|
|
|
});
|
2019-05-01 13:07:57 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 16:52:06 +00:00
|
|
|
const isRemoteModuleEnabled = features.isRemoteModuleEnabled()
|
|
|
|
? require('@electron/internal/browser/remote/server').isRemoteModuleEnabled
|
2020-03-20 20:28:31 +00:00
|
|
|
: () => false;
|
2019-09-18 16:52:06 +00:00
|
|
|
|
2019-03-08 10:21:41 +00:00
|
|
|
const getPreloadScript = async function (preloadPath) {
|
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
|
|
|
|
2019-09-18 16:52:06 +00:00
|
|
|
if (features.isExtensionsEnabled()) {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcMainUtils.handleSync('ELECTRON_GET_CONTENT_SCRIPTS', () => []);
|
2019-07-31 21:25:41 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { getContentScripts } = require('@electron/internal/browser/chrome-extension');
|
|
|
|
ipcMainUtils.handleSync('ELECTRON_GET_CONTENT_SCRIPTS', () => getContentScripts());
|
2019-07-31 21:25:41 +00:00
|
|
|
}
|
2019-06-19 15:23:44 +00:00
|
|
|
|
2019-08-23 22:45:50 +00:00
|
|
|
ipcMainUtils.handleSync('ELECTRON_BROWSER_SANDBOX_LOAD', async function (event) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const preloadPaths = event.sender._getPreloadPaths();
|
2019-01-29 01:16:46 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let contentScripts = [];
|
2019-09-18 16:52:06 +00:00
|
|
|
if (!features.isExtensionsEnabled()) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { getContentScripts } = require('@electron/internal/browser/chrome-extension');
|
|
|
|
contentScripts = getContentScripts();
|
2019-07-31 21:25:41 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const webPreferences = event.sender.getLastWebPreferences() || {};
|
2020-01-06 21:23:03 +00:00
|
|
|
|
2019-03-08 10:21:41 +00:00
|
|
|
return {
|
2019-07-31 21:25:41 +00:00
|
|
|
contentScripts,
|
2019-03-08 10:21:41 +00:00
|
|
|
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
|
|
|
|
2019-02-04 22:49:53 +00:00
|
|
|
ipcMainInternal.on('ELECTRON_BROWSER_PRELOAD_ERROR', function (event, preloadPath, error) {
|
2020-03-20 20:28:31 +00:00
|
|
|
event.sender.emit('preload-error', event, preloadPath, error);
|
|
|
|
});
|