app.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict'
  2. const bindings = process.atomBinding('app')
  3. const commandLine = process.atomBinding('command_line')
  4. const path = require('path')
  5. const { app, App } = bindings
  6. // Only one app object permitted.
  7. module.exports = app
  8. const electron = require('electron')
  9. const { deprecate, Menu } = electron
  10. const { EventEmitter } = require('events')
  11. let dockMenu = null
  12. // App is an EventEmitter.
  13. Object.setPrototypeOf(App.prototype, EventEmitter.prototype)
  14. EventEmitter.call(app)
  15. Object.assign(app, {
  16. setApplicationMenu (menu) {
  17. return Menu.setApplicationMenu(menu)
  18. },
  19. getApplicationMenu () {
  20. return Menu.getApplicationMenu()
  21. },
  22. commandLine: {
  23. hasSwitch: (...args) => commandLine.hasSwitch(...args.map(String)),
  24. getSwitchValue: (...args) => commandLine.getSwitchValue(...args.map(String)),
  25. appendSwitch: (...args) => commandLine.appendSwitch(...args.map(String)),
  26. appendArgument: (...args) => commandLine.appendArgument(...args.map(String))
  27. },
  28. enableMixedSandbox () {
  29. deprecate.log(`'enableMixedSandbox' is deprecated. Mixed-sandbox mode is now enabled by default. You can safely remove the call to enableMixedSandbox().`)
  30. }
  31. })
  32. app.getFileIcon = deprecate.promisify(app.getFileIcon)
  33. app.isPackaged = (() => {
  34. const execFile = path.basename(process.execPath).toLowerCase()
  35. if (process.platform === 'win32') {
  36. return execFile !== 'electron.exe'
  37. }
  38. return execFile !== 'electron'
  39. })()
  40. app._setDefaultAppPaths = (packagePath) => {
  41. // Set the user path according to application's name.
  42. app.setPath('userData', path.join(app.getPath('appData'), app.getName()))
  43. app.setPath('userCache', path.join(app.getPath('cache'), app.getName()))
  44. app.setAppPath(packagePath)
  45. // Add support for --user-data-dir=
  46. const userDataDirFlag = '--user-data-dir='
  47. const userDataArg = process.argv.find(arg => arg.startsWith(userDataDirFlag))
  48. if (userDataArg) {
  49. const userDataDir = userDataArg.substr(userDataDirFlag.length)
  50. if (path.isAbsolute(userDataDir)) app.setPath('userData', userDataDir)
  51. }
  52. }
  53. if (process.platform === 'darwin') {
  54. app.dock = {
  55. bounce (type = 'informational') {
  56. return bindings.dockBounce(type)
  57. },
  58. cancelBounce: bindings.dockCancelBounce,
  59. downloadFinished: bindings.dockDownloadFinished,
  60. setBadge: bindings.dockSetBadgeText,
  61. getBadge: bindings.dockGetBadgeText,
  62. hide: bindings.dockHide,
  63. show: bindings.dockShow,
  64. isVisible: bindings.dockIsVisible,
  65. setMenu (menu) {
  66. dockMenu = menu
  67. bindings.dockSetMenu(menu)
  68. },
  69. getMenu () {
  70. return dockMenu
  71. },
  72. setIcon: bindings.dockSetIcon
  73. }
  74. }
  75. if (process.platform === 'linux') {
  76. app.launcher = {
  77. setBadgeCount: bindings.unityLauncherSetBadgeCount,
  78. getBadgeCount: bindings.unityLauncherGetBadgeCount,
  79. isCounterBadgeAvailable: bindings.unityLauncherAvailable,
  80. isUnityRunning: bindings.unityLauncherAvailable
  81. }
  82. }
  83. app.allowNTLMCredentialsForAllDomains = function (allow) {
  84. deprecate.warn('app.allowNTLMCredentialsForAllDomains', 'session.allowNTLMCredentialsForDomains')
  85. const domains = allow ? '*' : ''
  86. if (!this.isReady()) {
  87. this.commandLine.appendSwitch('auth-server-whitelist', domains)
  88. } else {
  89. electron.session.defaultSession.allowNTLMCredentialsForDomains(domains)
  90. }
  91. }
  92. // Routes the events to webContents.
  93. const events = ['login', 'certificate-error', 'select-client-certificate']
  94. for (const name of events) {
  95. app.on(name, (event, webContents, ...args) => {
  96. webContents.emit(name, event, ...args)
  97. })
  98. }
  99. // Wrappers for native classes.
  100. const { DownloadItem } = process.atomBinding('download_item')
  101. Object.setPrototypeOf(DownloadItem.prototype, EventEmitter.prototype)