2016-11-03 20:54:08 +00:00
|
|
|
// Any requires added here need to be added to the browserify_entries array
|
|
|
|
// in filenames.gypi so they get built into the preload_bundle.js bundle
|
|
|
|
|
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-20 13:59:39 +00:00
|
|
|
const atomBinding = require('../common/atom-binding-setup')(binding.get, 'renderer')
|
|
|
|
|
|
|
|
const v8Util = atomBinding('v8_util')
|
|
|
|
const ipc = atomBinding('ipc')
|
|
|
|
|
2016-08-21 10:30:14 +00:00
|
|
|
const ipcRenderer = new events.EventEmitter()
|
|
|
|
const proc = new events.EventEmitter()
|
|
|
|
// eval in window scope:
|
|
|
|
// http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.2
|
|
|
|
const geval = eval
|
|
|
|
|
2017-02-20 13:59:39 +00:00
|
|
|
require('../renderer/api/ipc-renderer-setup')(ipcRenderer, ipc)
|
2016-08-21 10:30:14 +00:00
|
|
|
|
2017-02-20 13:59:39 +00:00
|
|
|
ipc.onMessage = function (channel, args) {
|
2017-02-27 13:23:32 +00:00
|
|
|
ipcRenderer.emit(channel, {sender: ipcRenderer}, ...args)
|
2016-08-21 10:30:14 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 13:59:39 +00:00
|
|
|
ipc.onExit = function () {
|
2016-08-21 10:30:14 +00:00
|
|
|
proc.emit('exit')
|
|
|
|
}
|
|
|
|
|
2017-02-20 13:59:39 +00:00
|
|
|
v8Util.setHiddenValue(global, 'ipc', ipc)
|
|
|
|
|
2016-08-21 10:30:14 +00:00
|
|
|
const preloadModules = new Map([
|
|
|
|
['electron', {
|
|
|
|
ipcRenderer: ipcRenderer
|
|
|
|
}]
|
|
|
|
])
|
|
|
|
|
|
|
|
function preloadRequire (module) {
|
|
|
|
if (preloadModules.has(module)) {
|
|
|
|
return preloadModules.get(module)
|
|
|
|
}
|
|
|
|
throw new Error('module not found')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the source for the preload
|
|
|
|
let preloadSrc = ipcRenderer.sendSync('ELECTRON_BROWSER_READ_FILE', preloadPath)
|
|
|
|
if (preloadSrc.err) {
|
|
|
|
throw new Error(preloadSrc.err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap the source into a function receives a `require` function as argument.
|
|
|
|
// Browserify bundles can make use of this, as explained in:
|
|
|
|
// https://github.com/substack/node-browserify#multiple-bundles
|
|
|
|
//
|
|
|
|
// For example, the user can create a browserify bundle with:
|
|
|
|
//
|
|
|
|
// $ browserify -x electron preload.js > renderer.js
|
|
|
|
//
|
|
|
|
// and any `require('electron')` calls in `preload.js` will work as expected
|
|
|
|
// since browserify won't try to include `electron` in the bundle and will fall
|
|
|
|
// back to the `preloadRequire` function above.
|
|
|
|
let preloadWrapperSrc = `(function(require, process, Buffer, global) {
|
|
|
|
${preloadSrc.data}
|
|
|
|
})`
|
|
|
|
|
|
|
|
let preloadFn = geval(preloadWrapperSrc)
|
|
|
|
preloadFn(preloadRequire, proc, Buffer, global)
|