crash-reporter.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict'
  2. const binding = process.electronBinding('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. const {
  14. productName,
  15. companyName,
  16. extra = {},
  17. ignoreSystemCrashHandler = false,
  18. submitURL,
  19. uploadToServer = true
  20. } = options
  21. if (companyName == null) throw new Error('companyName is a required option to crashReporter.start')
  22. if (submitURL == null) throw new Error('submitURL is a required option to crashReporter.start')
  23. const ret = this.init({
  24. submitURL,
  25. productName
  26. })
  27. this.productName = ret.productName
  28. this.crashesDirectory = ret.crashesDirectory
  29. if (extra._productName == null) extra._productName = ret.productName
  30. if (extra._companyName == null) extra._companyName = companyName
  31. if (extra._version == null) extra._version = ret.appVersion
  32. binding.start(ret.productName, companyName, submitURL, ret.crashesDirectory, uploadToServer, ignoreSystemCrashHandler, extra)
  33. }
  34. getLastCrashReport () {
  35. const reports = this.getUploadedReports()
  36. .sort((a, b) => {
  37. const ats = (a && a.date) ? new Date(a.date).getTime() : 0
  38. const bts = (b && b.date) ? new Date(b.date).getTime() : 0
  39. return bts - ats
  40. })
  41. return (reports.length > 0) ? reports[0] : null
  42. }
  43. getUploadedReports () {
  44. return binding.getUploadedReports(this.getCrashesDirectory())
  45. }
  46. getCrashesDirectory () {
  47. return this.crashesDirectory
  48. }
  49. getUploadToServer () {
  50. if (process.type === 'browser') {
  51. return binding.getUploadToServer()
  52. } else {
  53. throw new Error('getUploadToServer can only be called from the main process')
  54. }
  55. }
  56. setUploadToServer (uploadToServer) {
  57. if (process.type === 'browser') {
  58. return binding.setUploadToServer(uploadToServer)
  59. } else {
  60. throw new Error('setUploadToServer can only be called from the main process')
  61. }
  62. }
  63. addExtraParameter (key, value) {
  64. binding.addExtraParameter(key, value)
  65. }
  66. removeExtraParameter (key) {
  67. binding.removeExtraParameter(key)
  68. }
  69. getParameters () {
  70. return binding.getParameters()
  71. }
  72. }
  73. module.exports = CrashReporter