2019-02-19 17:05:14 +00:00
|
|
|
import { EventEmitter } from 'events'
|
2019-03-28 10:38:51 +00:00
|
|
|
import * as fs from 'fs'
|
2019-02-19 17:05:14 +00:00
|
|
|
import * as path from 'path'
|
2016-01-13 03:55:49 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
const Module = require('module')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 19:10:12 +00:00
|
|
|
// We modified the original process.argv to let node.js load the
|
2017-03-08 09:58:54 +00:00
|
|
|
// init.js, we need to restore it here.
|
2016-03-25 19:57:17 +00:00
|
|
|
process.argv.splice(1, 1)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Clear search paths.
|
2019-02-15 01:24:25 +00:00
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
require('../common/reset-search-paths')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Import common settings.
|
2018-09-20 03:43:26 +00:00
|
|
|
require('@electron/internal/common/init')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-09-28 23:17:00 +00:00
|
|
|
const globalPaths = Module.globalPaths
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Expose public APIs.
|
2016-03-25 19:57:17 +00:00
|
|
|
globalPaths.push(path.join(__dirname, 'api', 'exports'))
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// The global variable will be used by ipc for event dispatching
|
2019-03-18 19:37:06 +00:00
|
|
|
const v8Util = process.electronBinding('v8_util')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-10-06 11:48:00 +00:00
|
|
|
v8Util.setHiddenValue(global, 'ipc', new EventEmitter())
|
|
|
|
v8Util.setHiddenValue(global, 'ipc-internal', new EventEmitter())
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-13 03:55:49 +00:00
|
|
|
// Use electron module after everything is ready.
|
2019-02-15 01:24:25 +00:00
|
|
|
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal')
|
2019-02-18 00:24:18 +00:00
|
|
|
const { webFrameInit } = require('@electron/internal/renderer/web-frame-init')
|
|
|
|
webFrameInit()
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Process command line arguments.
|
2019-03-18 19:37:06 +00:00
|
|
|
const { hasSwitch, getSwitchValue } = process.electronBinding('command_line')
|
2019-01-03 16:22:34 +00:00
|
|
|
|
2019-02-19 17:05:14 +00:00
|
|
|
const parseOption = function<T> (
|
|
|
|
name: string, defaultValue: T, converter?: (value: string) => T
|
|
|
|
) {
|
|
|
|
return hasSwitch(name)
|
|
|
|
? (
|
|
|
|
converter
|
|
|
|
? converter(getSwitchValue(name))
|
|
|
|
: getSwitchValue(name)
|
|
|
|
)
|
|
|
|
: defaultValue
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 16:22:34 +00:00
|
|
|
const contextIsolation = hasSwitch('context-isolation')
|
2019-01-07 19:19:27 +00:00
|
|
|
const nodeIntegration = hasSwitch('node-integration')
|
2019-01-03 16:22:34 +00:00
|
|
|
const webviewTag = hasSwitch('webview-tag')
|
|
|
|
const isHiddenPage = hasSwitch('hidden-page')
|
|
|
|
const usesNativeWindowOpen = hasSwitch('native-window-open')
|
|
|
|
|
|
|
|
const preloadScript = parseOption('preload', null)
|
2019-02-19 17:05:14 +00:00
|
|
|
const preloadScripts = parseOption('preload-scripts', [], value => value.split(path.delimiter)) as string[]
|
2019-01-03 16:22:34 +00:00
|
|
|
const appPath = parseOption('app-path', null)
|
|
|
|
const guestInstanceId = parseOption('guest-instance-id', null, value => parseInt(value))
|
|
|
|
const openerId = parseOption('opener-id', null, value => parseInt(value))
|
2018-12-12 21:31:16 +00:00
|
|
|
|
2019-01-12 01:00:43 +00:00
|
|
|
// The arguments to be passed to isolated world.
|
2019-02-15 01:24:25 +00:00
|
|
|
const isolatedWorldArgs = { ipcRendererInternal, guestInstanceId, isHiddenPage, openerId, usesNativeWindowOpen }
|
2019-01-12 01:00:43 +00:00
|
|
|
|
2017-12-05 06:59:15 +00:00
|
|
|
// The webContents preload script is loaded after the session preload scripts.
|
|
|
|
if (preloadScript) {
|
|
|
|
preloadScripts.push(preloadScript)
|
|
|
|
}
|
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
switch (window.location.protocol) {
|
|
|
|
case 'chrome-devtools:': {
|
|
|
|
// Override some inspector APIs.
|
|
|
|
require('@electron/internal/renderer/inspector')
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case 'chrome-extension:': {
|
|
|
|
// Inject the chrome.* APIs that chrome extensions require
|
2019-03-26 02:38:35 +00:00
|
|
|
require('@electron/internal/renderer/chrome-api').injectTo(window.location.hostname, window)
|
2019-01-22 01:08:16 +00:00
|
|
|
break
|
|
|
|
}
|
2019-01-22 18:32:18 +00:00
|
|
|
case 'chrome:':
|
|
|
|
break
|
2019-01-22 01:08:16 +00:00
|
|
|
default: {
|
|
|
|
// Override default web functions.
|
2019-02-14 13:52:38 +00:00
|
|
|
const { windowSetup } = require('@electron/internal/renderer/window-setup')
|
2019-03-30 01:10:21 +00:00
|
|
|
windowSetup(guestInstanceId, openerId, isHiddenPage, usesNativeWindowOpen)
|
2016-05-27 01:29:57 +00:00
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
// Inject content scripts.
|
2019-03-08 23:53:25 +00:00
|
|
|
require('@electron/internal/renderer/content-scripts-injector')(process.getRenderProcessPreferences)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
// Load webview tag implementation.
|
2019-01-22 19:24:46 +00:00
|
|
|
if (process.isMainFrame) {
|
2019-03-07 23:26:23 +00:00
|
|
|
const { webViewInit } = require('@electron/internal/renderer/web-view/web-view-init')
|
|
|
|
webViewInit(contextIsolation, webviewTag, guestInstanceId)
|
2019-01-22 19:24:46 +00:00
|
|
|
}
|
2019-01-22 01:08:16 +00:00
|
|
|
|
2019-01-12 01:00:43 +00:00
|
|
|
// Pass the arguments to isolatedWorld.
|
|
|
|
if (contextIsolation) {
|
|
|
|
v8Util.setHiddenValue(global, 'isolated-world-args', isolatedWorldArgs)
|
|
|
|
}
|
|
|
|
|
2018-10-08 13:51:31 +00:00
|
|
|
if (nodeIntegration) {
|
2016-01-14 18:35:29 +00:00
|
|
|
// Export node bindings to global.
|
2016-03-25 19:57:17 +00:00
|
|
|
global.require = require
|
|
|
|
global.module = module
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Set the __filename to the path of html file if it is file: protocol.
|
2016-01-12 02:40:23 +00:00
|
|
|
if (window.location.protocol === 'file:') {
|
2018-03-15 02:45:13 +00:00
|
|
|
const location = window.location
|
|
|
|
let pathname = location.pathname
|
|
|
|
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
if (pathname[0] === '/') pathname = pathname.substr(1)
|
|
|
|
|
|
|
|
const isWindowsNetworkSharePath = location.hostname.length > 0 && globalPaths[0].startsWith('\\')
|
|
|
|
if (isWindowsNetworkSharePath) {
|
|
|
|
pathname = `//${location.host}/${pathname}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 19:57:17 +00:00
|
|
|
global.__filename = path.normalize(decodeURIComponent(pathname))
|
|
|
|
global.__dirname = path.dirname(global.__filename)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Set module's filename so relative require can work as expected.
|
2016-03-25 19:57:17 +00:00
|
|
|
module.filename = global.__filename
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Also search for module under the html file.
|
2016-03-25 19:57:17 +00:00
|
|
|
module.paths = module.paths.concat(Module._nodeModulePaths(global.__dirname))
|
2016-01-12 02:40:23 +00:00
|
|
|
} else {
|
2016-03-25 19:57:17 +00:00
|
|
|
global.__filename = __filename
|
|
|
|
global.__dirname = __dirname
|
2017-04-03 11:12:02 +00:00
|
|
|
|
2017-04-04 00:36:01 +00:00
|
|
|
if (appPath) {
|
2017-04-03 13:11:29 +00:00
|
|
|
// Search for module under the app directory
|
2017-04-04 00:36:01 +00:00
|
|
|
module.paths = module.paths.concat(Module._nodeModulePaths(appPath))
|
2017-04-03 13:11:29 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Redirect window.onerror to uncaughtException.
|
2019-02-19 17:05:14 +00:00
|
|
|
window.onerror = function (_message, _filename, _lineno, _colno, error) {
|
2016-01-12 02:40:23 +00:00
|
|
|
if (global.process.listeners('uncaughtException').length > 0) {
|
2019-02-19 17:05:14 +00:00
|
|
|
// We do not want to add `uncaughtException` to our definitions
|
|
|
|
// because we don't want anyone else (anywhere) to throw that kind
|
|
|
|
// of error.
|
|
|
|
global.process.emit('uncaughtException' as any, error as any)
|
2016-03-25 19:57:17 +00:00
|
|
|
return true
|
2016-01-12 02:40:23 +00:00
|
|
|
} else {
|
2016-03-25 19:57:17 +00:00
|
|
|
return false
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 19:57:17 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
} else {
|
2016-01-14 18:35:29 +00:00
|
|
|
// Delete Node's symbols after the Environment has been loaded.
|
2016-03-25 19:57:17 +00:00
|
|
|
process.once('loaded', function () {
|
|
|
|
delete global.process
|
2017-02-13 16:48:39 +00:00
|
|
|
delete global.Buffer
|
2016-03-25 19:57:17 +00:00
|
|
|
delete global.setImmediate
|
|
|
|
delete global.clearImmediate
|
2016-09-08 17:20:39 +00:00
|
|
|
delete global.global
|
2016-03-25 19:57:17 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 11:03:43 +00:00
|
|
|
const errorUtils = require('@electron/internal/common/error-utils')
|
2019-03-28 10:38:51 +00:00
|
|
|
const { isParentDir } = require('@electron/internal/common/path-utils')
|
|
|
|
|
|
|
|
let absoluteAppPath: string
|
|
|
|
const getAppPath = function () {
|
|
|
|
if (absoluteAppPath === undefined) {
|
|
|
|
absoluteAppPath = fs.realpathSync(appPath!)
|
|
|
|
}
|
|
|
|
return absoluteAppPath
|
|
|
|
}
|
2019-01-18 11:03:43 +00:00
|
|
|
|
2017-12-05 06:59:15 +00:00
|
|
|
// Load the preload scripts.
|
|
|
|
for (const preloadScript of preloadScripts) {
|
2016-01-12 02:40:23 +00:00
|
|
|
try {
|
2019-03-28 10:38:51 +00:00
|
|
|
if (!isParentDir(getAppPath(), fs.realpathSync(preloadScript))) {
|
|
|
|
throw new Error('Preload scripts outside of app path are not allowed')
|
|
|
|
}
|
2016-03-25 19:57:17 +00:00
|
|
|
require(preloadScript)
|
2016-01-14 22:20:06 +00:00
|
|
|
} catch (error) {
|
2019-01-18 11:03:43 +00:00
|
|
|
console.error(`Unable to load preload script: ${preloadScript}`)
|
|
|
|
console.error(`${error}`)
|
|
|
|
|
2019-02-15 01:24:25 +00:00
|
|
|
ipcRendererInternal.send('ELECTRON_BROWSER_PRELOAD_ERROR', preloadScript, errorUtils.serialize(error))
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-03 14:50:12 +00:00
|
|
|
|
|
|
|
// Warn about security issues
|
2019-01-22 19:24:46 +00:00
|
|
|
if (process.isMainFrame) {
|
2019-02-17 01:06:30 +00:00
|
|
|
const { securityWarnings } = require('@electron/internal/renderer/security-warnings')
|
|
|
|
securityWarnings(nodeIntegration)
|
2019-01-22 19:24:46 +00:00
|
|
|
}
|