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-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-03-20 20:28:31 +00:00
|
|
|
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
|
|
|
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
|
|
|
|
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-05-13 17:05:53 +00:00
|
|
|
ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', async 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-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
|
|
|
|
|
|
|
ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_MEDIA_SOURCE_ID_FOR_WEB_CONTENTS', function (event, webContentsId, stack) {
|
|
|
|
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
|
|
|
|
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-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();
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
});
|
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();
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_SET_UPLOAD_TO_SERVER', (event, uploadToServer) => {
|
|
|
|
return electron.crashReporter.setUploadToServer(uploadToServer);
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_GET_CRASHES_DIRECTORY', () => {
|
|
|
|
return electron.crashReporter.getCrashesDirectory();
|
|
|
|
});
|