2018-09-22 12:28:50 +00:00
|
|
|
'use strict'
|
|
|
|
|
2016-08-21 10:30:14 +00:00
|
|
|
/* eslint no-eval: "off" */
|
2018-08-10 22:19:49 +00:00
|
|
|
/* global binding, Buffer */
|
2016-08-21 10:30:14 +00:00
|
|
|
const events = require('events')
|
2018-09-30 21:24:00 +00:00
|
|
|
const { EventEmitter } = events
|
2016-08-21 10:30:14 +00:00
|
|
|
|
2019-03-18 19:37:06 +00:00
|
|
|
process.electronBinding = require('@electron/internal/common/atom-binding-setup').electronBindingSetup(binding.get, 'renderer')
|
2017-02-20 13:59:39 +00:00
|
|
|
|
2019-03-18 19:37:06 +00:00
|
|
|
const v8Util = process.electronBinding('v8_util')
|
2017-03-16 16:20:09 +00:00
|
|
|
// Expose browserify Buffer as a hidden value. This is used by C++ code to
|
|
|
|
// deserialize Buffer instances sent from browser process.
|
|
|
|
v8Util.setHiddenValue(global, 'Buffer', Buffer)
|
2017-02-27 17:14:36 +00:00
|
|
|
// The `lib/renderer/api/ipc-renderer.js` module looks for the ipc object in the
|
|
|
|
// "ipc" hidden value
|
2018-09-30 21:24:00 +00:00
|
|
|
v8Util.setHiddenValue(global, 'ipc', new EventEmitter())
|
2018-10-06 11:48:00 +00:00
|
|
|
// The `lib/renderer/ipc-renderer-internal.js` module looks for the ipc object in the
|
|
|
|
// "ipc-internal" hidden value
|
|
|
|
v8Util.setHiddenValue(global, 'ipc-internal', new EventEmitter())
|
2017-02-27 17:14:36 +00:00
|
|
|
// The process object created by browserify is not an event emitter, fix it so
|
|
|
|
// the API is more compatible with non-sandboxed renderers.
|
2018-10-02 01:56:31 +00:00
|
|
|
for (const prop of Object.keys(EventEmitter.prototype)) {
|
2017-02-27 17:14:36 +00:00
|
|
|
if (process.hasOwnProperty(prop)) {
|
|
|
|
delete process[prop]
|
|
|
|
}
|
2016-08-21 10:30:14 +00:00
|
|
|
}
|
2018-09-30 21:24:00 +00:00
|
|
|
Object.setPrototypeOf(process, EventEmitter.prototype)
|
2016-08-21 10:30:14 +00:00
|
|
|
|
2019-02-15 01:24:25 +00:00
|
|
|
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal')
|
2019-03-08 10:21:41 +00:00
|
|
|
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils')
|
2018-12-05 18:07:56 +00:00
|
|
|
|
|
|
|
const {
|
2019-01-29 01:16:46 +00:00
|
|
|
preloadScripts, isRemoteModuleEnabled, isWebViewTagEnabled, process: processProps
|
2019-03-08 10:21:41 +00:00
|
|
|
} = ipcRendererUtils.invokeSync('ELECTRON_BROWSER_SANDBOX_LOAD')
|
2018-12-05 18:07:56 +00:00
|
|
|
|
|
|
|
process.isRemoteModuleEnabled = isRemoteModuleEnabled
|
|
|
|
|
2019-03-18 19:37:06 +00:00
|
|
|
// The electron module depends on process.electronBinding
|
2018-12-05 18:07:56 +00:00
|
|
|
const electron = require('electron')
|
|
|
|
|
2018-05-21 12:56:05 +00:00
|
|
|
const loadedModules = new Map([
|
2017-03-21 13:41:23 +00:00
|
|
|
['electron', electron],
|
2018-05-04 16:51:32 +00:00
|
|
|
['events', events],
|
2018-05-21 12:56:05 +00:00
|
|
|
['timers', require('timers')],
|
|
|
|
['url', require('url')]
|
2016-08-21 10:30:14 +00:00
|
|
|
])
|
|
|
|
|
2019-04-02 22:38:16 +00:00
|
|
|
// ElectronApiServiceImpl will look for the "ipcNative" hidden object when
|
|
|
|
// invoking the 'onMessage' callback.
|
|
|
|
v8Util.setHiddenValue(global, 'ipcNative', {
|
|
|
|
onMessage (internal, channel, args, senderId) {
|
|
|
|
const sender = internal ? ipcRendererInternal : electron.ipcRenderer
|
|
|
|
sender.emit(channel, { sender, senderId }, ...args)
|
2019-01-23 16:24:57 +00:00
|
|
|
}
|
2019-04-02 22:38:16 +00:00
|
|
|
})
|
2019-03-08 00:00:28 +00:00
|
|
|
|
2019-04-02 22:38:16 +00:00
|
|
|
// AtomSandboxedRendererClient will look for the "lifecycle" hidden object when
|
|
|
|
v8Util.setHiddenValue(global, 'lifecycle', {
|
2019-04-04 18:35:06 +00:00
|
|
|
onLoaded () {
|
|
|
|
process.emit('loaded')
|
|
|
|
},
|
2019-04-02 22:38:16 +00:00
|
|
|
onExit () {
|
|
|
|
process.emit('exit')
|
|
|
|
},
|
|
|
|
onDocumentStart () {
|
|
|
|
process.emit('document-start')
|
|
|
|
},
|
|
|
|
onDocumentEnd () {
|
|
|
|
process.emit('document-end')
|
|
|
|
}
|
|
|
|
})
|
2019-03-08 00:00:28 +00:00
|
|
|
|
2019-02-18 00:24:18 +00:00
|
|
|
const { webFrameInit } = require('@electron/internal/renderer/web-frame-init')
|
|
|
|
webFrameInit()
|
2018-04-12 01:57:40 +00:00
|
|
|
|
2017-02-27 17:14:36 +00:00
|
|
|
// Pass different process object to the preload script(which should not have
|
2019-03-18 19:37:06 +00:00
|
|
|
// access to things like `process.electronBinding`).
|
2018-09-30 21:24:00 +00:00
|
|
|
const preloadProcess = new EventEmitter()
|
2018-09-26 05:44:55 +00:00
|
|
|
|
2018-09-30 21:24:00 +00:00
|
|
|
Object.assign(preloadProcess, binding.process)
|
2018-08-21 18:05:45 +00:00
|
|
|
Object.assign(preloadProcess, processProps)
|
2018-09-30 21:24:00 +00:00
|
|
|
|
|
|
|
Object.assign(process, binding.process)
|
2018-08-21 18:05:45 +00:00
|
|
|
Object.assign(process, processProps)
|
2018-05-21 12:56:05 +00:00
|
|
|
|
2018-12-05 09:34:09 +00:00
|
|
|
Object.defineProperty(preloadProcess, 'noDeprecation', {
|
|
|
|
get () {
|
|
|
|
return process.noDeprecation
|
|
|
|
},
|
|
|
|
set (value) {
|
|
|
|
process.noDeprecation = value
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-04-04 18:35:06 +00:00
|
|
|
process.on('loaded', () => preloadProcess.emit('loaded'))
|
2017-02-27 17:14:36 +00:00
|
|
|
process.on('exit', () => preloadProcess.emit('exit'))
|
|
|
|
|
|
|
|
// This is the `require` function that will be visible to the preload script
|
2016-08-21 10:30:14 +00:00
|
|
|
function preloadRequire (module) {
|
2018-05-21 12:56:05 +00:00
|
|
|
if (loadedModules.has(module)) {
|
|
|
|
return loadedModules.get(module)
|
|
|
|
}
|
2019-03-19 19:00:41 +00:00
|
|
|
throw new Error(`module not found: ${module}`)
|
2016-08-21 10:30:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
// Process command line arguments.
|
2019-03-18 19:37:06 +00:00
|
|
|
const { hasSwitch } = process.electronBinding('command_line')
|
2019-01-03 16:22:34 +00:00
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
const contextIsolation = hasSwitch('context-isolation')
|
|
|
|
|
2018-11-21 17:56:58 +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)
|
2018-11-21 17:56:58 +00:00
|
|
|
break
|
|
|
|
}
|
2019-03-08 00:00:28 +00:00
|
|
|
case 'chrome': {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
// Inject content scripts.
|
|
|
|
require('@electron/internal/renderer/content-scripts-injector')(binding.getRenderProcessPreferences)
|
|
|
|
}
|
2018-03-22 07:15:57 +00:00
|
|
|
}
|
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
const guestInstanceId = binding.guestInstanceId && parseInt(binding.guestInstanceId)
|
2018-07-02 16:06:26 +00:00
|
|
|
|
2019-01-22 01:08:16 +00:00
|
|
|
// Load webview tag implementation.
|
2019-01-24 18:53:16 +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, isWebViewTagEnabled, guestInstanceId)
|
2019-01-24 18:53:16 +00:00
|
|
|
}
|
2018-07-02 16:06:26 +00:00
|
|
|
|
2019-01-18 11:03:43 +00:00
|
|
|
const errorUtils = require('@electron/internal/common/error-utils')
|
|
|
|
|
2017-02-27 17:14:36 +00:00
|
|
|
// Wrap the script into a function executed in global scope. It won't have
|
|
|
|
// access to the current scope, so we'll expose a few objects as arguments:
|
|
|
|
//
|
|
|
|
// - `require`: The `preloadRequire` function
|
|
|
|
// - `process`: The `preloadProcess` object
|
|
|
|
// - `Buffer`: Browserify `Buffer` implementation
|
|
|
|
// - `global`: The window object, which is aliased to `global` by browserify.
|
|
|
|
//
|
|
|
|
// Browserify bundles can make use of an external require function as explained
|
|
|
|
// in https://github.com/substack/node-browserify#multiple-bundles, so electron
|
|
|
|
// apps can use multi-module preload scripts in sandboxed renderers.
|
2016-08-21 10:30:14 +00:00
|
|
|
//
|
2017-02-27 17:14:36 +00:00
|
|
|
// For example, the user can create a bundle with:
|
2016-08-21 10:30:14 +00:00
|
|
|
//
|
|
|
|
// $ browserify -x electron preload.js > renderer.js
|
|
|
|
//
|
|
|
|
// and any `require('electron')` calls in `preload.js` will work as expected
|
2017-02-27 17:14:36 +00:00
|
|
|
// since browserify won't try to include `electron` in the bundle, falling back
|
|
|
|
// to the `preloadRequire` function above.
|
2019-01-18 11:03:43 +00:00
|
|
|
function runPreloadScript (preloadSrc) {
|
2019-03-11 23:13:46 +00:00
|
|
|
const preloadWrapperSrc = `(function(require, process, Buffer, global, setImmediate, clearImmediate, exports) {
|
2018-03-22 07:15:57 +00:00
|
|
|
${preloadSrc}
|
|
|
|
})`
|
2016-08-21 10:30:14 +00:00
|
|
|
|
2018-07-13 02:17:11 +00:00
|
|
|
// eval in window scope
|
|
|
|
const preloadFn = binding.createPreloadScript(preloadWrapperSrc)
|
2018-09-13 16:10:51 +00:00
|
|
|
const { setImmediate, clearImmediate } = require('timers')
|
2019-01-18 11:03:43 +00:00
|
|
|
|
2019-03-11 23:13:46 +00:00
|
|
|
preloadFn(preloadRequire, preloadProcess, Buffer, global, setImmediate, clearImmediate, {})
|
2019-01-18 11:03:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 01:16:46 +00:00
|
|
|
for (const { preloadPath, preloadSrc, preloadError } of preloadScripts) {
|
|
|
|
try {
|
|
|
|
if (preloadSrc) {
|
|
|
|
runPreloadScript(preloadSrc)
|
|
|
|
} else if (preloadError) {
|
|
|
|
throw errorUtils.deserialize(preloadError)
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`Unable to load preload script: ${preloadPath}`)
|
|
|
|
console.error(`${error}`)
|
|
|
|
|
2019-02-15 01:24:25 +00:00
|
|
|
ipcRendererInternal.send('ELECTRON_BROWSER_PRELOAD_ERROR', preloadPath, errorUtils.serialize(error))
|
2019-01-18 11:03:43 +00:00
|
|
|
}
|
2018-03-22 07:15:57 +00:00
|
|
|
}
|
2018-10-03 19:36:12 +00:00
|
|
|
|
|
|
|
// Warn about security issues
|
2019-01-24 18:53:16 +00:00
|
|
|
if (process.isMainFrame) {
|
2019-02-17 01:06:30 +00:00
|
|
|
const { securityWarnings } = require('@electron/internal/renderer/security-warnings')
|
|
|
|
securityWarnings()
|
2019-01-24 18:53:16 +00:00
|
|
|
}
|