// 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

/* eslint no-eval: "off" */
/* global binding, preloadPath, Buffer */
const events = require('events')

const atomBinding = require('../common/atom-binding-setup')(binding.get, 'renderer')

const v8Util = atomBinding('v8_util')
const ipc = atomBinding('ipc')

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

require('../renderer/api/ipc-renderer-setup')(ipcRenderer, ipc)

ipc.onMessage = function (channel, args) {
  ipcRenderer.emit(channel, ...args)
}

ipc.onExit = function () {
  proc.emit('exit')
}

v8Util.setHiddenValue(global, 'ipc', ipc)

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)

// 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

/* eslint no-eval: "off" */
/* global binding, preloadPath, Buffer */
const events = require('events')

const atomBinding = require('../common/atom-binding-setup')(binding.get, 'renderer')

const v8Util = atomBinding('v8_util')
const ipc = atomBinding('ipc')

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

require('../renderer/api/ipc-renderer-setup')(ipcRenderer, ipc)

ipc.onMessage = function (channel, args) {
  ipcRenderer.emit(channel, {sender: ipcRenderer}, ...args)
}

ipc.onExit = function () {
  proc.emit('exit')
}

v8Util.setHiddenValue(global, 'ipc', ipc)

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)

Make sandboxed ipcRenderer API compatible with non-sandboxed version