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-20 03:43:26 +00:00
|
|
|
process.atomBinding = require('@electron/internal/common/atom-binding-setup')(binding.get, 'renderer')
|
2017-02-20 13:59:39 +00:00
|
|
|
|
2018-08-27 18:16:52 +00:00
|
|
|
// The electron module depends on process.atomBinding
|
|
|
|
const electron = require('electron')
|
|
|
|
|
2017-02-27 17:14:36 +00:00
|
|
|
const v8Util = process.atomBinding('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
|
|
|
|
v8Util.setHiddenValue(global, 'ipc', new events.EventEmitter())
|
|
|
|
// The process object created by browserify is not an event emitter, fix it so
|
|
|
|
// the API is more compatible with non-sandboxed renderers.
|
|
|
|
for (let prop of Object.keys(events.EventEmitter.prototype)) {
|
|
|
|
if (process.hasOwnProperty(prop)) {
|
|
|
|
delete process[prop]
|
|
|
|
}
|
2016-08-21 10:30:14 +00:00
|
|
|
}
|
2017-02-27 17:14:36 +00:00
|
|
|
Object.setPrototypeOf(process, events.EventEmitter.prototype)
|
2016-08-21 10:30:14 +00:00
|
|
|
|
2018-05-21 12:56:05 +00:00
|
|
|
const remoteModules = new Set([
|
|
|
|
'child_process',
|
|
|
|
'fs',
|
|
|
|
'os',
|
|
|
|
'path'
|
|
|
|
])
|
|
|
|
|
|
|
|
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
|
|
|
])
|
|
|
|
|
2018-05-21 12:56:05 +00:00
|
|
|
const {
|
2018-08-21 18:05:45 +00:00
|
|
|
preloadSrc, preloadError, process: processProps
|
2018-08-10 22:19:49 +00:00
|
|
|
} = electron.ipcRenderer.sendSync('ELECTRON_BROWSER_SANDBOX_LOAD')
|
2018-05-21 12:56:05 +00:00
|
|
|
|
2018-09-20 03:43:26 +00:00
|
|
|
require('@electron/internal/renderer/web-frame-init')()
|
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
|
|
|
|
// access to things like `process.atomBinding`).
|
|
|
|
const preloadProcess = new events.EventEmitter()
|
2017-03-21 13:41:23 +00:00
|
|
|
preloadProcess.crash = () => binding.crash()
|
2017-05-01 12:08:41 +00:00
|
|
|
preloadProcess.hang = () => binding.hang()
|
2018-06-10 12:00:36 +00:00
|
|
|
preloadProcess.getHeapStatistics = () => binding.getHeapStatistics()
|
2017-05-01 12:08:41 +00:00
|
|
|
preloadProcess.getSystemMemoryInfo = () => binding.getSystemMemoryInfo()
|
2018-08-21 18:05:45 +00:00
|
|
|
preloadProcess.getCPUUsage = () => binding.getCPUUsage()
|
|
|
|
preloadProcess.getIOCounters = () => binding.getIOCounters()
|
2018-09-26 05:44:55 +00:00
|
|
|
|
|
|
|
Object.assign(processProps, {
|
|
|
|
argv: binding.getArgv(),
|
|
|
|
execPath: binding.getExecPath(),
|
|
|
|
pid: binding.getPid(),
|
|
|
|
resourcesPath: binding.getResourcesPath(),
|
|
|
|
sandboxed: true,
|
|
|
|
type: 'renderer'
|
|
|
|
})
|
|
|
|
|
2018-08-21 18:05:45 +00:00
|
|
|
Object.assign(preloadProcess, processProps)
|
|
|
|
Object.assign(process, processProps)
|
2018-05-21 12:56:05 +00:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if (remoteModules.has(module)) {
|
|
|
|
return require(module)
|
2016-08-21 10:30:14 +00:00
|
|
|
}
|
|
|
|
throw new Error('module not found')
|
|
|
|
}
|
|
|
|
|
2018-03-22 07:15:57 +00:00
|
|
|
if (window.location.protocol === 'chrome-devtools:') {
|
|
|
|
// Override some inspector APIs.
|
2018-09-20 03:43:26 +00:00
|
|
|
require('@electron/internal/renderer/inspector')
|
2018-03-22 07:15:57 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 16:06:26 +00:00
|
|
|
if (binding.guestInstanceId) {
|
|
|
|
process.guestInstanceId = parseInt(binding.guestInstanceId)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!process.guestInstanceId && preloadProcess.argv.indexOf('--webview-tag=true') !== -1) {
|
|
|
|
// don't allow recursive `<webview>`
|
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')
|
2018-07-02 16:06:26 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2018-05-21 12:56:05 +00:00
|
|
|
if (preloadSrc) {
|
|
|
|
const preloadWrapperSrc = `(function(require, process, Buffer, global, setImmediate, clearImmediate) {
|
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')
|
2018-05-21 12:56:05 +00:00
|
|
|
preloadFn(preloadRequire, preloadProcess, Buffer, global, setImmediate, clearImmediate)
|
|
|
|
} else if (preloadError) {
|
|
|
|
console.error(preloadError.stack)
|
2018-03-22 07:15:57 +00:00
|
|
|
}
|