2023-10-10 15:50:47 +00:00
import { app } from 'electron/main' ;
import * as deprecate from '@electron/internal/common/deprecate' ;
2016-03-10 19:54:17 +00:00
2020-06-23 03:32:45 +00:00
const binding = process . _linkedBinding ( 'electron_browser_crash_reporter' ) ;
2016-01-12 02:40:23 +00:00
2016-10-05 20:40:39 +00:00
class CrashReporter {
2020-05-07 20:31:26 +00:00
start ( options : Electron.CrashReporterStartOptions ) {
2019-03-28 19:04:16 +00:00
const {
2020-05-07 20:31:26 +00:00
productName = app . name ,
2017-10-31 02:51:22 +00:00
companyName ,
2019-03-28 19:04:16 +00:00
extra = { } ,
2020-05-07 20:31:26 +00:00
globalExtra = { } ,
2019-03-28 19:04:16 +00:00
ignoreSystemCrashHandler = false ,
2021-03-18 21:15:19 +00:00
submitURL = '' ,
2020-04-14 17:36:31 +00:00
uploadToServer = true ,
rateLimit = false ,
2020-09-03 19:43:58 +00:00
compress = true
2020-05-07 20:31:26 +00:00
} = options || { } ;
2016-11-28 22:51:24 +00:00
2021-03-18 21:15:19 +00:00
if ( uploadToServer && ! submitURL ) throw new Error ( 'submitURL must be specified when uploadToServer is true' ) ;
2016-10-05 20:40:39 +00:00
2021-03-05 01:13:17 +00:00
if ( ! compress && uploadToServer ) {
2020-05-19 20:47:21 +00:00
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.' ) ;
}
2020-05-07 20:31:26 +00:00
const appVersion = app . getVersion ( ) ;
2018-09-26 05:43:34 +00:00
2020-05-07 20:31:26 +00:00
if ( companyName && globalExtra . _companyName == null ) globalExtra . _companyName = companyName ;
2018-09-26 05:43:34 +00:00
2020-05-07 20:31:26 +00:00
const globalExtraAmended = {
_productName : productName ,
_version : appVersion ,
. . . globalExtra
} ;
2016-10-05 20:40:39 +00:00
2020-05-07 20:31:26 +00:00
binding . start ( submitURL , uploadToServer ,
ignoreSystemCrashHandler , rateLimit , compress , globalExtraAmended , extra , false ) ;
2016-03-25 19:41:24 +00:00
}
2016-01-12 02:40:23 +00:00
2016-10-05 20:40:39 +00:00
getLastCrashReport ( ) {
const reports = this . getUploadedReports ( )
2018-03-13 20:57:12 +00:00
. sort ( ( a , b ) = > {
2020-03-20 20:28:31 +00:00
const ats = ( a && a . date ) ? new Date ( a . date ) . getTime ( ) : 0 ;
const bts = ( b && b . date ) ? new Date ( b . date ) . getTime ( ) : 0 ;
return bts - ats ;
} ) ;
2018-03-13 20:57:12 +00:00
2020-03-20 20:28:31 +00:00
return ( reports . length > 0 ) ? reports [ 0 ] : null ;
2016-03-25 19:41:24 +00:00
}
2016-01-12 02:40:23 +00:00
2020-05-07 20:31:26 +00:00
getUploadedReports ( ) : Electron . CrashReport [ ] {
return binding . getUploadedReports ( ) ;
2016-03-25 19:41:24 +00:00
}
2016-01-12 02:40:23 +00:00
2016-11-22 08:30:20 +00:00
getUploadToServer ( ) {
2016-11-08 00:39:11 +00:00
if ( process . type === 'browser' ) {
2020-03-20 20:28:31 +00:00
return binding . getUploadToServer ( ) ;
2016-11-08 00:39:11 +00:00
} else {
2020-03-20 20:28:31 +00:00
throw new Error ( 'getUploadToServer can only be called from the main process' ) ;
2016-11-08 00:39:11 +00:00
}
2016-11-08 00:03:57 +00:00
}
2020-05-07 20:31:26 +00:00
setUploadToServer ( uploadToServer : boolean ) {
2016-11-08 00:39:11 +00:00
if ( process . type === 'browser' ) {
2020-03-20 20:28:31 +00:00
return binding . setUploadToServer ( uploadToServer ) ;
2016-11-08 00:39:11 +00:00
} else {
2020-03-20 20:28:31 +00:00
throw new Error ( 'setUploadToServer can only be called from the main process' ) ;
2016-11-08 00:39:11 +00:00
}
2016-11-08 00:03:57 +00:00
}
2017-02-02 22:23:21 +00:00
2020-05-07 20:31:26 +00:00
addExtraParameter ( key : string , value : string ) {
2020-03-20 20:28:31 +00:00
binding . addExtraParameter ( key , value ) ;
2017-11-02 01:57:43 +00:00
}
2020-05-07 20:31:26 +00:00
removeExtraParameter ( key : string ) {
2020-03-20 20:28:31 +00:00
binding . removeExtraParameter ( key ) ;
2017-11-02 01:57:43 +00:00
}
2019-03-28 19:04:16 +00:00
getParameters ( ) {
2020-03-20 20:28:31 +00:00
return binding . getParameters ( ) ;
2017-10-31 17:06:54 +00:00
}
2016-10-06 17:02:46 +00:00
}
2020-05-07 20:31:26 +00:00
export default new CrashReporter ( ) ;