2016-08-21 10:30:14 +00:00
|
|
|
/* eslint no-eval: "off" */
|
2017-02-20 13:59:39 +00:00
|
|
|
/* global binding, preloadPath, Buffer */
|
2016-08-21 10:30:14 +00:00
|
|
|
const events = require('events')
|
|
|
|
|
2017-02-27 17:14:36 +00:00
|
|
|
process.atomBinding = require('../common/atom-binding-setup')(binding.get, 'renderer')
|
2017-02-20 13:59:39 +00:00
|
|
|
|
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
|
|
|
|
2017-02-27 17:14:36 +00:00
|
|
|
const electron = require('electron')
|
2017-03-21 13:41:23 +00:00
|
|
|
const fs = require('fs')
|
2016-08-21 10:30:14 +00:00
|
|
|
const preloadModules = new Map([
|
2017-03-21 13:41:23 +00:00
|
|
|
['child_process', require('child_process')],
|
|
|
|
['electron', electron],
|
|
|
|
['fs', fs],
|
|
|
|
['os', require('os')],
|
2017-04-05 20:42:24 +00:00
|
|
|
['path', require('path')],
|
2017-03-21 13:41:23 +00:00
|
|
|
['url', require('url')],
|
|
|
|
['timers', require('timers')]
|
2016-08-21 10:30:14 +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()
|
|
|
|
preloadProcess.getProcessMemoryInfo = () => binding.getProcessMemoryInfo()
|
|
|
|
preloadProcess.getSystemMemoryInfo = () => binding.getSystemMemoryInfo()
|
2017-06-26 20:52:19 +00:00
|
|
|
preloadProcess.argv = binding.getArgv()
|
2017-04-05 20:42:24 +00:00
|
|
|
process.platform = preloadProcess.platform = electron.remote.process.platform
|
|
|
|
process.execPath = preloadProcess.execPath = electron.remote.process.execPath
|
2018-03-14 03:01:40 +00:00
|
|
|
process.env = preloadProcess.env = electron.remote.process.env
|
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) {
|
|
|
|
if (preloadModules.has(module)) {
|
|
|
|
return preloadModules.get(module)
|
|
|
|
}
|
|
|
|
throw new Error('module not found')
|
|
|
|
}
|
|
|
|
|
2018-03-22 07:15:57 +00:00
|
|
|
if (window.location.protocol === 'chrome-devtools:') {
|
|
|
|
// Override some inspector APIs.
|
|
|
|
require('../renderer/inspector')
|
|
|
|
}
|
|
|
|
|
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-03-22 07:15:57 +00:00
|
|
|
if (preloadPath) {
|
|
|
|
const preloadSrc = fs.readFileSync(preloadPath).toString()
|
|
|
|
const preloadWrapperSrc = `(function(require, process, Buffer, global, setImmediate) {
|
|
|
|
${preloadSrc}
|
|
|
|
})`
|
2016-08-21 10:30:14 +00:00
|
|
|
|
2018-03-22 07:15:57 +00:00
|
|
|
// eval in window scope:
|
|
|
|
// http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.2
|
|
|
|
const geval = eval
|
|
|
|
const preloadFn = geval(preloadWrapperSrc)
|
|
|
|
const {setImmediate} = require('timers')
|
|
|
|
preloadFn(preloadRequire, preloadProcess, Buffer, global, setImmediate)
|
|
|
|
}
|