init.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import { EventEmitter } from 'events'
  2. import * as path from 'path'
  3. const Module = require('module')
  4. // Make sure globals like "process" and "global" are always available in preload
  5. // scripts even after they are deleted in "loaded" script.
  6. //
  7. // Note 1: We rely on a Node patch to actually pass "process" and "global" and
  8. // other arguments to the wrapper.
  9. //
  10. // Note 2: Node introduced a new code path to use native code to wrap module
  11. // code, which does not work with this hack. However by modifying the
  12. // "Module.wrapper" we can force Node to use the old code path to wrap module
  13. // code with JavaScript.
  14. Module.wrapper = [
  15. '(function (exports, require, module, __filename, __dirname, process, global, Buffer) { ' +
  16. // By running the code in a new closure, it would be possible for the module
  17. // code to override "process" and "Buffer" with local variables.
  18. 'return function (exports, require, module, __filename, __dirname) { ',
  19. '\n}.call(this, exports, require, module, __filename, __dirname); });'
  20. ]
  21. // We modified the original process.argv to let node.js load the
  22. // init.js, we need to restore it here.
  23. process.argv.splice(1, 1)
  24. // Clear search paths.
  25. require('../common/reset-search-paths')
  26. // Import common settings.
  27. require('@electron/internal/common/init')
  28. const globalPaths = Module.globalPaths
  29. // Expose public APIs.
  30. globalPaths.push(path.join(__dirname, 'api', 'exports'))
  31. // The global variable will be used by ipc for event dispatching
  32. const v8Util = process.electronBinding('v8_util')
  33. const ipcEmitter = new EventEmitter()
  34. const ipcInternalEmitter = new EventEmitter()
  35. v8Util.setHiddenValue(global, 'ipc', ipcEmitter)
  36. v8Util.setHiddenValue(global, 'ipc-internal', ipcInternalEmitter)
  37. v8Util.setHiddenValue(global, 'ipcNative', {
  38. onMessage (internal: boolean, channel: string, args: any[], senderId: number) {
  39. const sender = internal ? ipcInternalEmitter : ipcEmitter
  40. sender.emit(channel, { sender, senderId }, ...args)
  41. }
  42. })
  43. // Use electron module after everything is ready.
  44. const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal')
  45. const { webFrameInit } = require('@electron/internal/renderer/web-frame-init')
  46. webFrameInit()
  47. // Process command line arguments.
  48. const { hasSwitch, getSwitchValue } = process.electronBinding('command_line')
  49. const parseOption = function<T> (
  50. name: string, defaultValue: T, converter?: (value: string) => T
  51. ) {
  52. return hasSwitch(name)
  53. ? (
  54. converter
  55. ? converter(getSwitchValue(name))
  56. : getSwitchValue(name)
  57. )
  58. : defaultValue
  59. }
  60. const contextIsolation = hasSwitch('context-isolation')
  61. const nodeIntegration = hasSwitch('node-integration')
  62. const webviewTag = hasSwitch('webview-tag')
  63. const isHiddenPage = hasSwitch('hidden-page')
  64. const usesNativeWindowOpen = hasSwitch('native-window-open')
  65. const preloadScript = parseOption('preload', null)
  66. const preloadScripts = parseOption('preload-scripts', [], value => value.split(path.delimiter)) as string[]
  67. const appPath = parseOption('app-path', null)
  68. const guestInstanceId = parseOption('guest-instance-id', null, value => parseInt(value))
  69. const openerId = parseOption('opener-id', null, value => parseInt(value))
  70. // The arguments to be passed to isolated world.
  71. const isolatedWorldArgs = { ipcRendererInternal, guestInstanceId, isHiddenPage, openerId, usesNativeWindowOpen }
  72. // The webContents preload script is loaded after the session preload scripts.
  73. if (preloadScript) {
  74. preloadScripts.push(preloadScript)
  75. }
  76. switch (window.location.protocol) {
  77. case 'devtools:': {
  78. // Override some inspector APIs.
  79. require('@electron/internal/renderer/inspector')
  80. break
  81. }
  82. case 'chrome-extension:': {
  83. // Inject the chrome.* APIs that chrome extensions require
  84. require('@electron/internal/renderer/chrome-api').injectTo(window.location.hostname, window)
  85. break
  86. }
  87. case 'chrome:':
  88. break
  89. default: {
  90. // Override default web functions.
  91. const { windowSetup } = require('@electron/internal/renderer/window-setup')
  92. windowSetup(guestInstanceId, openerId, isHiddenPage, usesNativeWindowOpen)
  93. // Inject content scripts.
  94. require('@electron/internal/renderer/content-scripts-injector')(process.getRenderProcessPreferences)
  95. }
  96. }
  97. // Load webview tag implementation.
  98. if (process.isMainFrame) {
  99. const { webViewInit } = require('@electron/internal/renderer/web-view/web-view-init')
  100. webViewInit(contextIsolation, webviewTag, guestInstanceId)
  101. }
  102. // Pass the arguments to isolatedWorld.
  103. if (contextIsolation) {
  104. v8Util.setHiddenValue(global, 'isolated-world-args', isolatedWorldArgs)
  105. }
  106. if (nodeIntegration) {
  107. // Export node bindings to global.
  108. global.require = require
  109. global.module = module
  110. // Set the __filename to the path of html file if it is file: protocol.
  111. if (window.location.protocol === 'file:') {
  112. const location = window.location
  113. let pathname = location.pathname
  114. if (process.platform === 'win32') {
  115. if (pathname[0] === '/') pathname = pathname.substr(1)
  116. const isWindowsNetworkSharePath = location.hostname.length > 0 && globalPaths[0].startsWith('\\')
  117. if (isWindowsNetworkSharePath) {
  118. pathname = `//${location.host}/${pathname}`
  119. }
  120. }
  121. global.__filename = path.normalize(decodeURIComponent(pathname))
  122. global.__dirname = path.dirname(global.__filename)
  123. // Set module's filename so relative require can work as expected.
  124. module.filename = global.__filename
  125. // Also search for module under the html file.
  126. module.paths = module.paths.concat(Module._nodeModulePaths(global.__dirname))
  127. } else {
  128. global.__filename = __filename
  129. global.__dirname = __dirname
  130. if (appPath) {
  131. // Search for module under the app directory
  132. module.paths = module.paths.concat(Module._nodeModulePaths(appPath))
  133. }
  134. }
  135. // Redirect window.onerror to uncaughtException.
  136. window.onerror = function (_message, _filename, _lineno, _colno, error) {
  137. if (global.process.listeners('uncaughtException').length > 0) {
  138. // We do not want to add `uncaughtException` to our definitions
  139. // because we don't want anyone else (anywhere) to throw that kind
  140. // of error.
  141. global.process.emit('uncaughtException' as any, error as any)
  142. return true
  143. } else {
  144. return false
  145. }
  146. }
  147. } else {
  148. // Delete Node's symbols after the Environment has been loaded.
  149. process.once('loaded', function () {
  150. delete global.process
  151. delete global.Buffer
  152. delete global.setImmediate
  153. delete global.clearImmediate
  154. delete global.global
  155. })
  156. }
  157. const errorUtils = require('@electron/internal/common/error-utils')
  158. // Load the preload scripts.
  159. for (const preloadScript of preloadScripts) {
  160. try {
  161. require(preloadScript)
  162. } catch (error) {
  163. console.error(`Unable to load preload script: ${preloadScript}`)
  164. console.error(`${error}`)
  165. ipcRendererInternal.send('ELECTRON_BROWSER_PRELOAD_ERROR', preloadScript, errorUtils.serialize(error))
  166. }
  167. }
  168. // Warn about security issues
  169. if (process.isMainFrame) {
  170. const { securityWarnings } = require('@electron/internal/renderer/security-warnings')
  171. securityWarnings(nodeIntegration)
  172. }