init.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. 'use strict'
  2. const events = require('events')
  3. const path = require('path')
  4. const Module = require('module')
  5. // We modified the original process.argv to let node.js load the
  6. // init.js, we need to restore it here.
  7. process.argv.splice(1, 1)
  8. // Clear search paths.
  9. require('../common/reset-search-paths')
  10. // Import common settings.
  11. require('../common/init')
  12. var globalPaths = Module.globalPaths
  13. // Expose public APIs.
  14. globalPaths.push(path.join(__dirname, 'api', 'exports'))
  15. // The global variable will be used by ipc for event dispatching
  16. var v8Util = process.atomBinding('v8_util')
  17. v8Util.setHiddenValue(global, 'ipc', new events.EventEmitter())
  18. // Use electron module after everything is ready.
  19. const {ipcRenderer} = require('electron')
  20. const {
  21. warnAboutNodeWithRemoteContent,
  22. warnAboutDisabledWebSecurity,
  23. warnAboutInsecureContentAllowed,
  24. warnAboutExperimentalFeatures,
  25. warnAboutEnableBlinkFeatures,
  26. warnAboutInsecureResources,
  27. warnAboutInsecureCSP,
  28. warnAboutAllowedPopups,
  29. shouldLogSecurityWarnings
  30. } = require('./security-warnings')
  31. require('./web-frame-init')()
  32. // Process command line arguments.
  33. let nodeIntegration = 'false'
  34. let webviewTag = 'false'
  35. let preloadScript = null
  36. let preloadScripts = []
  37. let isBackgroundPage = false
  38. let appPath = null
  39. for (let arg of process.argv) {
  40. if (arg.indexOf('--guest-instance-id=') === 0) {
  41. // This is a guest web view.
  42. process.guestInstanceId = parseInt(arg.substr(arg.indexOf('=') + 1))
  43. } else if (arg.indexOf('--opener-id=') === 0) {
  44. // This is a guest BrowserWindow.
  45. process.openerId = parseInt(arg.substr(arg.indexOf('=') + 1))
  46. } else if (arg.indexOf('--node-integration=') === 0) {
  47. nodeIntegration = arg.substr(arg.indexOf('=') + 1)
  48. } else if (arg.indexOf('--preload=') === 0) {
  49. preloadScript = arg.substr(arg.indexOf('=') + 1)
  50. } else if (arg === '--background-page') {
  51. isBackgroundPage = true
  52. } else if (arg.indexOf('--app-path=') === 0) {
  53. appPath = arg.substr(arg.indexOf('=') + 1)
  54. } else if (arg.indexOf('--webview-tag=') === 0) {
  55. webviewTag = arg.substr(arg.indexOf('=') + 1)
  56. } else if (arg.indexOf('--preload-scripts') === 0) {
  57. preloadScripts = arg.substr(arg.indexOf('=') + 1).split(path.delimiter)
  58. }
  59. }
  60. // The webContents preload script is loaded after the session preload scripts.
  61. if (preloadScript) {
  62. preloadScripts.push(preloadScript)
  63. }
  64. if (window.location.protocol === 'chrome-devtools:') {
  65. // Override some inspector APIs.
  66. require('./inspector')
  67. nodeIntegration = 'false'
  68. } else if (window.location.protocol === 'chrome-extension:') {
  69. // Add implementations of chrome API.
  70. require('./chrome-api').injectTo(window.location.hostname, isBackgroundPage, window)
  71. nodeIntegration = 'false'
  72. } else if (window.location.protocol === 'chrome:') {
  73. // Disable node integration for chrome UI scheme.
  74. nodeIntegration = 'false'
  75. } else {
  76. // Override default web functions.
  77. require('./override')
  78. // Inject content scripts.
  79. require('./content-scripts-injector')
  80. // Load webview tag implementation.
  81. if (webviewTag === 'true' && process.guestInstanceId == null) {
  82. require('./web-view/web-view')
  83. require('./web-view/web-view-attributes')
  84. }
  85. }
  86. if (nodeIntegration === 'true') {
  87. // Export node bindings to global.
  88. global.require = require
  89. global.module = module
  90. // Set the __filename to the path of html file if it is file: protocol.
  91. if (window.location.protocol === 'file:') {
  92. const location = window.location
  93. let pathname = location.pathname
  94. if (process.platform === 'win32') {
  95. if (pathname[0] === '/') pathname = pathname.substr(1)
  96. const isWindowsNetworkSharePath = location.hostname.length > 0 && globalPaths[0].startsWith('\\')
  97. if (isWindowsNetworkSharePath) {
  98. pathname = `//${location.host}/${pathname}`
  99. }
  100. }
  101. global.__filename = path.normalize(decodeURIComponent(pathname))
  102. global.__dirname = path.dirname(global.__filename)
  103. // Set module's filename so relative require can work as expected.
  104. module.filename = global.__filename
  105. // Also search for module under the html file.
  106. module.paths = module.paths.concat(Module._nodeModulePaths(global.__dirname))
  107. } else {
  108. global.__filename = __filename
  109. global.__dirname = __dirname
  110. if (appPath) {
  111. // Search for module under the app directory
  112. module.paths = module.paths.concat(Module._nodeModulePaths(appPath))
  113. }
  114. }
  115. // Redirect window.onerror to uncaughtException.
  116. window.onerror = function (message, filename, lineno, colno, error) {
  117. if (global.process.listeners('uncaughtException').length > 0) {
  118. global.process.emit('uncaughtException', error)
  119. return true
  120. } else {
  121. return false
  122. }
  123. }
  124. } else {
  125. // Delete Node's symbols after the Environment has been loaded.
  126. process.once('loaded', function () {
  127. delete global.process
  128. delete global.Buffer
  129. delete global.setImmediate
  130. delete global.clearImmediate
  131. delete global.global
  132. })
  133. }
  134. // Load the preload scripts.
  135. for (const preloadScript of preloadScripts) {
  136. try {
  137. require(preloadScript)
  138. } catch (error) {
  139. console.error('Unable to load preload script: ' + preloadScript)
  140. console.error(error.stack || error.message)
  141. }
  142. }
  143. // Warn about security issues
  144. window.addEventListener('load', function loadHandler () {
  145. if (shouldLogSecurityWarnings()) {
  146. if (nodeIntegration === 'true') {
  147. warnAboutNodeWithRemoteContent()
  148. }
  149. warnAboutDisabledWebSecurity()
  150. warnAboutInsecureResources()
  151. warnAboutInsecureContentAllowed()
  152. warnAboutExperimentalFeatures()
  153. warnAboutEnableBlinkFeatures()
  154. warnAboutInsecureCSP()
  155. warnAboutAllowedPopups()
  156. }
  157. window.removeEventListener('load', loadHandler)
  158. })
  159. // Report focus/blur events of webview to browser.
  160. // Note that while Chromium content APIs have observer for focus/blur, they
  161. // unfortunately do not work for webview.
  162. if (process.guestInstanceId) {
  163. window.addEventListener('focus', () => {
  164. ipcRenderer.send('ELECTRON_GUEST_VIEW_MANAGER_FOCUS_CHANGE', true, process.guestInstanceId)
  165. })
  166. window.addEventListener('blur', () => {
  167. ipcRenderer.send('ELECTRON_GUEST_VIEW_MANAGER_FOCUS_CHANGE', false, process.guestInstanceId)
  168. })
  169. }