electron/lib/renderer/init.js

162 lines
5.3 KiB
JavaScript
Raw Normal View History

2016-03-25 19:57:17 +00:00
'use strict'
2016-01-13 03:55:49 +00:00
const { EventEmitter } = require('events')
2016-03-25 19:57:17 +00:00
const path = require('path')
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
// 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.
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.
require('@electron/internal/common/init')
2016-01-12 02:40:23 +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
const v8Util = process.atomBinding('v8_util')
2016-01-12 02:40:23 +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.
const ipcRenderer = require('@electron/internal/renderer/ipc-renderer-internal')
2016-01-13 03:55:49 +00:00
require('@electron/internal/renderer/web-frame-init')()
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Process command line arguments.
const { hasSwitch, getSwitchValue } = process.atomBinding('command_line')
const parseOption = function (name, defaultValue, converter = value => value) {
return hasSwitch(name) ? converter(getSwitchValue(name)) : defaultValue
2016-01-12 02:40:23 +00:00
}
const contextIsolation = hasSwitch('context-isolation')
const nodeIntegration = hasSwitch('node-integration')
const webviewTag = hasSwitch('webview-tag')
const isHiddenPage = hasSwitch('hidden-page')
const isBackgroundPage = hasSwitch('background-page')
const usesNativeWindowOpen = hasSwitch('native-window-open')
const preloadScript = parseOption('preload', null)
const preloadScripts = parseOption('preload-scripts', [], value => value.split(path.delimiter))
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
// The arguments to be passed to isolated world.
const isolatedWorldArgs = { ipcRenderer, guestInstanceId, isHiddenPage, openerId, usesNativeWindowOpen }
// The webContents preload script is loaded after the session preload scripts.
if (preloadScript) {
preloadScripts.push(preloadScript)
}
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
require('@electron/internal/renderer/chrome-api').injectTo(window.location.hostname, isBackgroundPage, window)
break
}
default: {
// Override default web functions.
require('@electron/internal/renderer/window-setup')(ipcRenderer, guestInstanceId, openerId, isHiddenPage, usesNativeWindowOpen)
// Inject content scripts.
require('@electron/internal/renderer/content-scripts-injector')
2016-01-12 02:40:23 +00:00
}
}
// Load webview tag implementation.
require('@electron/internal/renderer/web-view/web-view-init')(contextIsolation, webviewTag, guestInstanceId)
// Pass the arguments to isolatedWorld.
if (contextIsolation) {
v8Util.setHiddenValue(global, 'isolated-world-args', isolatedWorldArgs)
}
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:') {
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-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.
2016-03-25 19:57:17 +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) {
2016-03-25 19:57:17 +00:00
global.process.emit('uncaughtException', error)
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
}
const errorUtils = require('@electron/internal/common/error-utils')
// Load the preload scripts.
for (const preloadScript of preloadScripts) {
2016-01-12 02:40:23 +00:00
try {
2016-03-25 19:57:17 +00:00
require(preloadScript)
2016-01-14 22:20:06 +00:00
} catch (error) {
console.error(`Unable to load preload script: ${preloadScript}`)
console.error(`${error}`)
ipcRenderer.send('ELECTRON_BROWSER_PRELOAD_ERROR', preloadScript, errorUtils.serialize(error))
2016-01-12 02:40:23 +00:00
}
}
// Warn about security issues
require('@electron/internal/renderer/security-warnings')(nodeIntegration)