init.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* eslint no-eval: "off" */
  2. /* global binding, preloadPath, Buffer */
  3. const events = require('events')
  4. process.atomBinding = require('../common/atom-binding-setup')(binding.get, 'renderer')
  5. const v8Util = process.atomBinding('v8_util')
  6. // Expose browserify Buffer as a hidden value. This is used by C++ code to
  7. // deserialize Buffer instances sent from browser process.
  8. v8Util.setHiddenValue(global, 'Buffer', Buffer)
  9. // The `lib/renderer/api/ipc-renderer.js` module looks for the ipc object in the
  10. // "ipc" hidden value
  11. v8Util.setHiddenValue(global, 'ipc', new events.EventEmitter())
  12. // The process object created by browserify is not an event emitter, fix it so
  13. // the API is more compatible with non-sandboxed renderers.
  14. for (let prop of Object.keys(events.EventEmitter.prototype)) {
  15. if (process.hasOwnProperty(prop)) {
  16. delete process[prop]
  17. }
  18. }
  19. Object.setPrototypeOf(process, events.EventEmitter.prototype)
  20. const electron = require('electron')
  21. const fs = require('fs')
  22. const preloadModules = new Map([
  23. ['child_process', require('child_process')],
  24. ['electron', electron],
  25. ['fs', fs],
  26. ['os', require('os')],
  27. ['path', require('path')],
  28. ['url', require('url')],
  29. ['timers', require('timers')]
  30. ])
  31. const preloadSrc = fs.readFileSync(preloadPath).toString()
  32. // Pass different process object to the preload script(which should not have
  33. // access to things like `process.atomBinding`).
  34. const preloadProcess = new events.EventEmitter()
  35. preloadProcess.crash = () => binding.crash()
  36. preloadProcess.hang = () => binding.hang()
  37. preloadProcess.getProcessMemoryInfo = () => binding.getProcessMemoryInfo()
  38. preloadProcess.getSystemMemoryInfo = () => binding.getSystemMemoryInfo()
  39. preloadProcess.argv = binding.getArgv()
  40. process.platform = preloadProcess.platform = electron.remote.process.platform
  41. process.execPath = preloadProcess.execPath = electron.remote.process.execPath
  42. process.on('exit', () => preloadProcess.emit('exit'))
  43. // This is the `require` function that will be visible to the preload script
  44. function preloadRequire (module) {
  45. if (preloadModules.has(module)) {
  46. return preloadModules.get(module)
  47. }
  48. throw new Error('module not found')
  49. }
  50. // Wrap the script into a function executed in global scope. It won't have
  51. // access to the current scope, so we'll expose a few objects as arguments:
  52. //
  53. // - `require`: The `preloadRequire` function
  54. // - `process`: The `preloadProcess` object
  55. // - `Buffer`: Browserify `Buffer` implementation
  56. // - `global`: The window object, which is aliased to `global` by browserify.
  57. //
  58. // Browserify bundles can make use of an external require function as explained
  59. // in https://github.com/substack/node-browserify#multiple-bundles, so electron
  60. // apps can use multi-module preload scripts in sandboxed renderers.
  61. //
  62. // For example, the user can create a bundle with:
  63. //
  64. // $ browserify -x electron preload.js > renderer.js
  65. //
  66. // and any `require('electron')` calls in `preload.js` will work as expected
  67. // since browserify won't try to include `electron` in the bundle, falling back
  68. // to the `preloadRequire` function above.
  69. const preloadWrapperSrc = `(function(require, process, Buffer, global, setImmediate) {
  70. ${preloadSrc}
  71. })`
  72. // eval in window scope:
  73. // http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.2
  74. const geval = eval
  75. const preloadFn = geval(preloadWrapperSrc)
  76. const {setImmediate} = require('timers')
  77. preloadFn(preloadRequire, preloadProcess, Buffer, global, setImmediate)