crash-reporter.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import * as deprecate from '@electron/internal/common/deprecate';
  2. import { app } from 'electron/main';
  3. const binding = process._linkedBinding('electron_browser_crash_reporter');
  4. class CrashReporter implements Electron.CrashReporter {
  5. start (options: Electron.CrashReporterStartOptions) {
  6. const {
  7. productName = app.name,
  8. companyName,
  9. extra = {},
  10. globalExtra = {},
  11. ignoreSystemCrashHandler = false,
  12. submitURL = '',
  13. uploadToServer = true,
  14. rateLimit = false,
  15. compress = true
  16. } = options || {};
  17. if (uploadToServer && !submitURL) throw new Error('submitURL must be specified when uploadToServer is true');
  18. if (!compress && uploadToServer) {
  19. deprecate.log('Sending uncompressed crash reports is deprecated and will be removed in a future version of Electron. Set { compress: true } to opt-in to the new behavior. Crash reports will be uploaded gzipped, which most crash reporting servers support.');
  20. }
  21. const appVersion = app.getVersion();
  22. if (companyName && globalExtra._companyName == null) globalExtra._companyName = companyName;
  23. const globalExtraAmended = {
  24. _productName: productName,
  25. _version: appVersion,
  26. ...globalExtra
  27. };
  28. binding.start(submitURL, uploadToServer,
  29. ignoreSystemCrashHandler, rateLimit, compress, globalExtraAmended, extra, false);
  30. }
  31. getLastCrashReport () {
  32. const reports = this.getUploadedReports()
  33. .sort((a, b) => {
  34. const ats = (a && a.date) ? new Date(a.date).getTime() : 0;
  35. const bts = (b && b.date) ? new Date(b.date).getTime() : 0;
  36. return bts - ats;
  37. });
  38. return (reports.length > 0) ? reports[0] : null;
  39. }
  40. getUploadedReports (): Electron.CrashReport[] {
  41. return binding.getUploadedReports();
  42. }
  43. getUploadToServer () {
  44. if (process.type === 'browser') {
  45. return binding.getUploadToServer();
  46. } else {
  47. throw new Error('getUploadToServer can only be called from the main process');
  48. }
  49. }
  50. setUploadToServer (uploadToServer: boolean) {
  51. if (process.type === 'browser') {
  52. return binding.setUploadToServer(uploadToServer);
  53. } else {
  54. throw new Error('setUploadToServer can only be called from the main process');
  55. }
  56. }
  57. addExtraParameter (key: string, value: string) {
  58. binding.addExtraParameter(key, value);
  59. }
  60. removeExtraParameter (key: string) {
  61. binding.removeExtraParameter(key);
  62. }
  63. getParameters () {
  64. return binding.getParameters();
  65. }
  66. }
  67. export default new CrashReporter();