crash-reporter.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. getProductName () {
  50. return this.productName
  51. }
  52. getUploadToServer () {
  53. if (process.type === 'browser') {
  54. return binding.getUploadToServer()
  55. } else {
  56. throw new Error('getUploadToServer can only be called from the main process')
  57. }
  58. }
  59. setUploadToServer (uploadToServer) {
  60. if (process.type === 'browser') {
  61. return binding.setUploadToServer(uploadToServer)
  62. } else {
  63. throw new Error('setUploadToServer can only be called from the main process')
  64. }
  65. }
  66. addExtraParameter (key, value) {
  67. binding.addExtraParameter(key, value)
  68. }
  69. removeExtraParameter (key) {
  70. binding.removeExtraParameter(key)
  71. }
  72. getParameters () {
  73. return binding.getParameters()
  74. }
  75. }
  76. module.exports = CrashReporter