2016-03-25 19:57:17 +00:00
|
|
|
'use strict'
|
2016-01-13 03:55:49 +00:00
|
|
|
|
2018-10-06 11:48:00 +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
|
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.
|
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
|
2018-09-28 23:17:00 +00:00
|
|
|
const v8Util = process.atomBinding('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.
|
2018-10-06 11:48:00 +00:00
|
|
|
const ipcRenderer = require('@electron/internal/renderer/ipc-renderer-internal')
|
2016-01-13 03:55:49 +00:00
|
|
|
|
2018-09-20 03:43:26 +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.
|
2018-10-08 13:51:31 +00:00
|
|
|
let nodeIntegration = false
|
|
|
|
let webviewTag = false
|
2016-05-29 01:46:48 +00:00
|
|
|
let preloadScript = null
|
2017-12-05 06:59:15 +00:00
|
|
|
let preloadScripts = []
|
2016-05-29 01:46:48 +00:00
|
|
|
let isBackgroundPage = false
|
2017-04-04 00:36:01 +00:00
|
|
|
let appPath = null
|
2018-10-02 01:56:31 +00:00
|
|
|
for (const arg of process.argv) {
|
2016-01-12 02:40:23 +00:00
|
|
|
if (arg.indexOf('--guest-instance-id=') === 0) {
|
2016-01-14 18:35:29 +00:00
|
|
|
// This is a guest web view.
|
2016-03-25 19:57:17 +00:00
|
|
|
process.guestInstanceId = parseInt(arg.substr(arg.indexOf('=') + 1))
|
2016-01-12 02:40:23 +00:00
|
|
|
} else if (arg.indexOf('--opener-id=') === 0) {
|
2016-01-14 18:35:29 +00:00
|
|
|
// This is a guest BrowserWindow.
|
2016-03-25 19:57:17 +00:00
|
|
|
process.openerId = parseInt(arg.substr(arg.indexOf('=') + 1))
|
2016-01-12 02:40:23 +00:00
|
|
|
} else if (arg.indexOf('--node-integration=') === 0) {
|
2018-10-08 13:51:31 +00:00
|
|
|
nodeIntegration = arg.substr(arg.indexOf('=') + 1) === 'true'
|
2016-01-12 02:40:23 +00:00
|
|
|
} else if (arg.indexOf('--preload=') === 0) {
|
2016-03-25 19:57:17 +00:00
|
|
|
preloadScript = arg.substr(arg.indexOf('=') + 1)
|
2016-05-29 01:46:48 +00:00
|
|
|
} else if (arg === '--background-page') {
|
|
|
|
isBackgroundPage = true
|
2017-04-04 00:36:01 +00:00
|
|
|
} else if (arg.indexOf('--app-path=') === 0) {
|
|
|
|
appPath = arg.substr(arg.indexOf('=') + 1)
|
2017-05-17 20:09:24 +00:00
|
|
|
} else if (arg.indexOf('--webview-tag=') === 0) {
|
2018-10-08 13:51:31 +00:00
|
|
|
webviewTag = arg.substr(arg.indexOf('=') + 1) === 'true'
|
2017-12-05 06:59:15 +00:00
|
|
|
} else if (arg.indexOf('--preload-scripts') === 0) {
|
|
|
|
preloadScripts = arg.substr(arg.indexOf('=') + 1).split(path.delimiter)
|
2016-01-12 02:40:23 +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)
|
|
|
|
}
|
|
|
|
|
2016-03-30 21:07:27 +00:00
|
|
|
if (window.location.protocol === 'chrome-devtools:') {
|
2016-01-14 18:35:29 +00:00
|
|
|
// Override some inspector APIs.
|
2018-09-20 03:43:26 +00:00
|
|
|
require('@electron/internal/renderer/inspector')
|
2018-10-08 13:51:31 +00:00
|
|
|
nodeIntegration = false
|
2017-01-18 13:58:20 +00:00
|
|
|
} else if (window.location.protocol === 'chrome-extension:') {
|
2016-01-14 18:35:29 +00:00
|
|
|
// Add implementations of chrome API.
|
2018-09-20 03:43:26 +00:00
|
|
|
require('@electron/internal/renderer/chrome-api').injectTo(window.location.hostname, isBackgroundPage, window)
|
2018-10-08 13:51:31 +00:00
|
|
|
nodeIntegration = false
|
2017-02-04 14:49:01 +00:00
|
|
|
} else if (window.location.protocol === 'chrome:') {
|
|
|
|
// Disable node integration for chrome UI scheme.
|
2018-10-08 13:51:31 +00:00
|
|
|
nodeIntegration = false
|
2016-01-12 02:40:23 +00:00
|
|
|
} else {
|
2016-01-14 18:35:29 +00:00
|
|
|
// Override default web functions.
|
2018-09-20 03:43:26 +00:00
|
|
|
require('@electron/internal/renderer/override')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-05-27 01:29:57 +00:00
|
|
|
// Inject content scripts.
|
2018-09-20 03:43:26 +00:00
|
|
|
require('@electron/internal/renderer/content-scripts-injector')
|
2016-05-27 01:29:57 +00:00
|
|
|
|
2016-01-14 18:35:29 +00:00
|
|
|
// Load webview tag implementation.
|
2018-10-08 13:51:31 +00:00
|
|
|
if (webviewTag && process.guestInstanceId == null) {
|
2018-09-20 03:43:26 +00:00
|
|
|
require('@electron/internal/renderer/web-view/web-view')
|
|
|
|
require('@electron/internal/renderer/web-view/web-view-attributes')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
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
|
|
|
}
|
|
|
|
|
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 {
|
2016-03-25 19:57:17 +00:00
|
|
|
require(preloadScript)
|
2016-01-14 22:20:06 +00:00
|
|
|
} catch (error) {
|
2016-05-18 22:02:42 +00:00
|
|
|
console.error('Unable to load preload script: ' + preloadScript)
|
|
|
|
console.error(error.stack || error.message)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-03 14:50:12 +00:00
|
|
|
|
|
|
|
// Warn about security issues
|
2018-10-08 13:51:31 +00:00
|
|
|
require('@electron/internal/renderer/security-warnings')(nodeIntegration)
|
2018-08-28 18:35:44 +00:00
|
|
|
|
|
|
|
// Report focus/blur events of webview to browser.
|
|
|
|
// Note that while Chromium content APIs have observer for focus/blur, they
|
|
|
|
// unfortunately do not work for webview.
|
|
|
|
if (process.guestInstanceId) {
|
|
|
|
window.addEventListener('focus', () => {
|
|
|
|
ipcRenderer.send('ELECTRON_GUEST_VIEW_MANAGER_FOCUS_CHANGE', true, process.guestInstanceId)
|
|
|
|
})
|
|
|
|
window.addEventListener('blur', () => {
|
|
|
|
ipcRenderer.send('ELECTRON_GUEST_VIEW_MANAGER_FOCUS_CHANGE', false, process.guestInstanceId)
|
|
|
|
})
|
|
|
|
}
|