2016-03-24 20:34:36 +00:00
|
|
|
'use strict'
|
2016-01-13 09:07:18 +00:00
|
|
|
|
2016-03-24 20:34:36 +00:00
|
|
|
const electron = require('electron')
|
2018-05-21 12:56:05 +00:00
|
|
|
const fs = require('fs')
|
2019-03-16 00:32:04 +00:00
|
|
|
|
2019-03-18 19:37:06 +00:00
|
|
|
const eventBinding = process.electronBinding('event')
|
|
|
|
const clipboard = process.electronBinding('clipboard')
|
2019-05-01 13:07:57 +00:00
|
|
|
const features = process.electronBinding('features')
|
2017-01-24 23:05:34 +00:00
|
|
|
|
2019-02-05 20:56:44 +00:00
|
|
|
const { crashReporterInit } = require('@electron/internal/browser/crash-reporter-init')
|
2019-02-04 22:49:53 +00:00
|
|
|
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal')
|
2019-02-05 20:56:44 +00:00
|
|
|
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils')
|
2019-01-03 18:31:10 +00:00
|
|
|
const guestViewManager = require('@electron/internal/browser/guest-view-manager')
|
2018-09-26 05:44:55 +00:00
|
|
|
const errorUtils = require('@electron/internal/common/error-utils')
|
2019-03-16 00:32:04 +00:00
|
|
|
const clipboardUtils = require('@electron/internal/common/clipboard-utils')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2019-05-01 13:07:57 +00:00
|
|
|
const emitCustomEvent = function (contents, eventName, ...args) {
|
|
|
|
const event = eventBinding.createWithSender(contents)
|
|
|
|
|
|
|
|
electron.app.emit(eventName, event, contents, ...args)
|
|
|
|
contents.emit(eventName, event, ...args)
|
|
|
|
|
|
|
|
return event
|
|
|
|
}
|
|
|
|
|
2019-10-04 17:49:09 +00:00
|
|
|
const logStack = function (contents, code, stack) {
|
|
|
|
if (stack) {
|
|
|
|
console.warn(`WebContents (${contents.id}): ${code}`, stack)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
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
|
|
|
|
2019-08-23 22:45:50 +00:00
|
|
|
ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_INIT', function (event, options) {
|
2019-02-05 20:56:44 +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) {
|
2019-02-06 17:53:29 +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':
|
|
|
|
return new Set(['readFindText', 'writeFindText'])
|
|
|
|
case 'linux':
|
|
|
|
return new Set(Object.keys(clipboard))
|
|
|
|
default:
|
|
|
|
return new Set()
|
|
|
|
}
|
|
|
|
})()
|
|
|
|
|
2019-08-23 22:45:50 +00:00
|
|
|
ipcMainUtils.handleSync('ELECTRON_BROWSER_CLIPBOARD', function (event, method, ...args) {
|
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
|
|
|
|
2019-03-16 00:32:04 +00:00
|
|
|
return clipboardUtils.serialize(electron.clipboard[method](...clipboardUtils.deserialize(args)))
|
2018-09-26 05:43:34 +00:00
|
|
|
})
|
|
|
|
|
2019-05-01 13:07:57 +00:00
|
|
|
if (features.isDesktopCapturerEnabled()) {
|
|
|
|
const desktopCapturer = require('@electron/internal/browser/desktop-capturer')
|
|
|
|
|
2019-10-04 17:49:09 +00:00
|
|
|
ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', function (event, options, stack) {
|
|
|
|
logStack(event.sender, 'desktopCapturer.getSources()', stack)
|
2019-05-01 13:07:57 +00:00
|
|
|
const customEvent = emitCustomEvent(event.sender, 'desktop-capturer-get-sources')
|
|
|
|
|
|
|
|
if (customEvent.defaultPrevented) {
|
|
|
|
console.error('Blocked desktopCapturer.getSources()')
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2019-10-04 17:49:09 +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
|
|
|
|
: () => false
|
|
|
|
|
2019-03-08 10:21:41 +00:00
|
|
|
const getPreloadScript = async function (preloadPath) {
|
2018-05-21 12:56:05 +00:00
|
|
|
let preloadSrc = null
|
|
|
|
let preloadError = null
|
2019-07-03 15:05:45 +00:00
|
|
|
try {
|
|
|
|
preloadSrc = (await fs.promises.readFile(preloadPath)).toString()
|
|
|
|
} catch (err) {
|
|
|
|
preloadError = errorUtils.serialize(err)
|
2018-05-21 12:56:05 +00:00
|
|
|
}
|
2019-01-29 01:16:46 +00:00
|
|
|
return { preloadPath, preloadSrc, preloadError }
|
|
|
|
}
|
|
|
|
|
2019-09-18 16:52:06 +00:00
|
|
|
if (features.isExtensionsEnabled()) {
|
2019-08-23 22:45:50 +00:00
|
|
|
ipcMainUtils.handleSync('ELECTRON_GET_CONTENT_SCRIPTS', () => [])
|
2019-07-31 21:25:41 +00:00
|
|
|
} else {
|
|
|
|
const { getContentScripts } = require('@electron/internal/browser/chrome-extension')
|
2019-08-23 22:45:50 +00:00
|
|
|
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) {
|
2019-07-03 15:05:45 +00:00
|
|
|
const preloadPaths = event.sender._getPreloadPaths()
|
2019-01-29 01:16:46 +00:00
|
|
|
|
2019-07-31 21:25:41 +00:00
|
|
|
let contentScripts = []
|
2019-09-18 16:52:06 +00:00
|
|
|
if (!features.isExtensionsEnabled()) {
|
2019-07-31 21:25:41 +00:00
|
|
|
const { getContentScripts } = require('@electron/internal/browser/chrome-extension')
|
|
|
|
contentScripts = getContentScripts()
|
|
|
|
}
|
|
|
|
|
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),
|
2018-08-21 18:05:45 +00:00
|
|
|
process: {
|
|
|
|
arch: process.arch,
|
|
|
|
platform: process.platform,
|
|
|
|
env: process.env,
|
|
|
|
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
|
|
|
|
2019-02-04 22:49:53 +00:00
|
|
|
ipcMainInternal.on('ELECTRON_BROWSER_PRELOAD_ERROR', function (event, preloadPath, error) {
|
2019-01-18 11:03:43 +00:00
|
|
|
event.sender.emit('preload-error', event, preloadPath, errorUtils.deserialize(error))
|
|
|
|
})
|