crash-reporter.js 2.6 KB

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