crash-reporter.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use strict'
  2. const binding = process.atomBinding('crash_reporter')
  3. const errorUtils = require('@electron/internal/common/error-utils')
  4. class CrashReporter {
  5. contructor () {
  6. this.productName = null
  7. this.crashesDirectory = null
  8. }
  9. sendSync (channel, ...args) {
  10. throw new Error('Not implemented')
  11. }
  12. invoke (command, ...args) {
  13. const [ error, result ] = this.sendSync(command, ...args)
  14. if (error) {
  15. throw errorUtils.deserialize(error)
  16. }
  17. return result
  18. }
  19. start (options) {
  20. if (options == null) options = {}
  21. let {
  22. productName,
  23. companyName,
  24. extra,
  25. ignoreSystemCrashHandler,
  26. submitURL,
  27. uploadToServer
  28. } = options
  29. if (uploadToServer == null) {
  30. uploadToServer = true
  31. }
  32. if (ignoreSystemCrashHandler == null) {
  33. ignoreSystemCrashHandler = false
  34. }
  35. if (companyName == null) {
  36. throw new Error('companyName is a required option to crashReporter.start')
  37. }
  38. if (submitURL == null) {
  39. throw new Error('submitURL is a required option to crashReporter.start')
  40. }
  41. const ret = this.invoke('ELECTRON_CRASH_REPORTER_INIT', {
  42. submitURL,
  43. productName
  44. })
  45. this.productName = ret.productName
  46. this.crashesDirectory = ret.crashesDirectory
  47. this.crashServicePid = ret.crashServicePid
  48. if (extra == null) extra = {}
  49. if (extra._productName == null) extra._productName = ret.productName
  50. if (extra._companyName == null) extra._companyName = companyName
  51. if (extra._version == null) extra._version = ret.appVersion
  52. binding.start(ret.productName, companyName, submitURL, ret.crashesDirectory, uploadToServer, ignoreSystemCrashHandler, extra)
  53. }
  54. getLastCrashReport () {
  55. const reports = this.getUploadedReports()
  56. .sort((a, b) => {
  57. const ats = (a && a.date) ? new Date(a.date).getTime() : 0
  58. const bts = (b && b.date) ? new Date(b.date).getTime() : 0
  59. return bts - ats
  60. })
  61. return (reports.length > 0) ? reports[0] : null
  62. }
  63. getUploadedReports () {
  64. return binding.getUploadedReports(this.getCrashesDirectory())
  65. }
  66. getCrashesDirectory () {
  67. return this.crashesDirectory
  68. }
  69. getProductName () {
  70. return this.productName
  71. }
  72. getUploadToServer () {
  73. if (process.type === 'browser') {
  74. return binding.getUploadToServer()
  75. } else {
  76. throw new Error('getUploadToServer can only be called from the main process')
  77. }
  78. }
  79. setUploadToServer (uploadToServer) {
  80. if (process.type === 'browser') {
  81. return binding.setUploadToServer(uploadToServer)
  82. } else {
  83. throw new Error('setUploadToServer can only be called from the main process')
  84. }
  85. }
  86. addExtraParameter (key, value) {
  87. binding.addExtraParameter(key, value)
  88. }
  89. removeExtraParameter (key) {
  90. binding.removeExtraParameter(key)
  91. }
  92. getParameters (key, value) {
  93. return binding.getParameters()
  94. }
  95. }
  96. module.exports = CrashReporter