init.js 3.9 KB

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