crash-reporter.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. const crashDir = this.getCrashesDirectory();
  45. if (!crashDir) {
  46. throw new Error('crashReporter has not been started');
  47. }
  48. return binding.getUploadedReports(crashDir);
  49. }
  50. getCrashesDirectory () {
  51. return this.crashesDirectory;
  52. }
  53. getProductName () {
  54. return this.productName;
  55. }
  56. getUploadToServer () {
  57. if (process.type === 'browser') {
  58. return binding.getUploadToServer();
  59. } else {
  60. throw new Error('getUploadToServer can only be called from the main process');
  61. }
  62. }
  63. setUploadToServer (uploadToServer) {
  64. if (process.type === 'browser') {
  65. return binding.setUploadToServer(uploadToServer);
  66. } else {
  67. throw new Error('setUploadToServer can only be called from the main process');
  68. }
  69. }
  70. addExtraParameter (key, value) {
  71. binding.addExtraParameter(key, value);
  72. }
  73. removeExtraParameter (key) {
  74. binding.removeExtraParameter(key);
  75. }
  76. getParameters () {
  77. return binding.getParameters();
  78. }
  79. }
  80. module.exports = CrashReporter;