2016-03-25 12:41:24 -07:00
|
|
|
'use strict'
|
2016-03-10 11:54:17 -08:00
|
|
|
|
2018-09-14 02:10:51 +10:00
|
|
|
const { spawn } = require('child_process')
|
2016-10-06 10:02:46 -07:00
|
|
|
const os = require('os')
|
2016-10-06 10:39:57 -07:00
|
|
|
const path = require('path')
|
2016-03-25 12:41:24 -07:00
|
|
|
const electron = require('electron')
|
2018-09-14 02:10:51 +10:00
|
|
|
const { app } = process.type === 'browser' ? electron : electron.remote
|
2016-03-25 12:41:24 -07:00
|
|
|
const binding = process.atomBinding('crash_reporter')
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-10-05 13:40:39 -07:00
|
|
|
class CrashReporter {
|
|
|
|
start (options) {
|
2017-10-30 22:51:22 -04:00
|
|
|
if (options == null) options = {}
|
2016-10-06 10:39:57 -07:00
|
|
|
this.productName = options.productName != null ? options.productName : app.getName()
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2017-10-30 22:51:22 -04:00
|
|
|
let {
|
|
|
|
companyName,
|
|
|
|
extra,
|
|
|
|
ignoreSystemCrashHandler,
|
|
|
|
submitURL,
|
|
|
|
uploadToServer
|
|
|
|
} = options
|
2016-11-28 14:51:24 -08:00
|
|
|
|
2018-02-20 23:31:48 -05:00
|
|
|
if (uploadToServer == null) {
|
2018-05-23 09:07:14 -07:00
|
|
|
uploadToServer = true
|
2018-02-20 23:31:48 -05:00
|
|
|
}
|
|
|
|
|
2017-10-30 22:51:22 -04:00
|
|
|
if (ignoreSystemCrashHandler == null) ignoreSystemCrashHandler = false
|
|
|
|
if (extra == null) extra = {}
|
|
|
|
|
|
|
|
if (extra._productName == null) extra._productName = this.getProductName()
|
|
|
|
if (extra._companyName == null) extra._companyName = companyName
|
|
|
|
if (extra._version == null) extra._version = app.getVersion()
|
2016-11-28 14:51:24 -08:00
|
|
|
|
2016-01-11 18:40:23 -08:00
|
|
|
if (companyName == null) {
|
2016-04-28 09:48:13 -07:00
|
|
|
throw new Error('companyName is a required option to crashReporter.start')
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
|
|
|
if (submitURL == null) {
|
2016-04-28 09:48:13 -07:00
|
|
|
throw new Error('submitURL is a required option to crashReporter.start')
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
2016-10-05 13:40:39 -07:00
|
|
|
|
2016-01-11 18:40:23 -08:00
|
|
|
if (process.platform === 'win32') {
|
2017-10-31 23:42:25 -04:00
|
|
|
const env = {
|
|
|
|
ELECTRON_INTERNAL_CRASH_SERVICE: 1
|
|
|
|
}
|
2016-10-05 16:15:49 -07:00
|
|
|
const args = [
|
|
|
|
'--reporter-url=' + submitURL,
|
2016-10-06 10:39:57 -07:00
|
|
|
'--application-name=' + this.getProductName(),
|
|
|
|
'--crashes-directory=' + this.getCrashesDirectory(),
|
2016-10-05 16:15:49 -07:00
|
|
|
'--v=1'
|
|
|
|
]
|
2017-10-30 22:51:22 -04:00
|
|
|
|
2017-04-19 16:32:43 -07:00
|
|
|
this._crashServiceProcess = spawn(process.execPath, args, {
|
2016-01-11 18:40:23 -08:00
|
|
|
env: env,
|
|
|
|
detached: true
|
2016-03-25 12:41:24 -07:00
|
|
|
})
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
2016-10-05 13:40:39 -07:00
|
|
|
|
2016-11-22 19:30:20 +11:00
|
|
|
binding.start(this.getProductName(), companyName, submitURL, this.getCrashesDirectory(), uploadToServer, ignoreSystemCrashHandler, extra)
|
2016-03-25 12:41:24 -07:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-10-05 13:40:39 -07:00
|
|
|
getLastCrashReport () {
|
|
|
|
const reports = this.getUploadedReports()
|
2018-03-13 13:57:12 -07:00
|
|
|
.sort((a, b) => {
|
2018-03-13 17:35:55 -07:00
|
|
|
const ats = (a && a.date) ? new Date(a.date).getTime() : 0
|
|
|
|
const bts = (b && b.date) ? new Date(b.date).getTime() : 0
|
2018-03-13 13:57:12 -07:00
|
|
|
return bts - ats
|
|
|
|
})
|
|
|
|
|
2017-10-30 22:51:22 -04:00
|
|
|
return (reports.length > 0) ? reports[0] : null
|
2016-03-25 12:41:24 -07:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-10-05 13:40:39 -07:00
|
|
|
getUploadedReports () {
|
2017-02-13 10:46:19 -08:00
|
|
|
return binding.getUploadedReports(this.getCrashesDirectory())
|
2016-03-25 12:41:24 -07:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-10-06 10:39:57 -07:00
|
|
|
getCrashesDirectory () {
|
2017-10-30 22:51:22 -04:00
|
|
|
const crashesDir = `${this.getProductName()} Crashes`
|
2016-10-06 10:39:57 -07:00
|
|
|
return path.join(this.getTempDirectory(), crashesDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
getProductName () {
|
|
|
|
if (this.productName == null) {
|
|
|
|
this.productName = app.getName()
|
|
|
|
}
|
|
|
|
return this.productName
|
|
|
|
}
|
|
|
|
|
|
|
|
getTempDirectory () {
|
|
|
|
if (this.tempDirectory == null) {
|
|
|
|
try {
|
|
|
|
this.tempDirectory = app.getPath('temp')
|
|
|
|
} catch (error) {
|
|
|
|
this.tempDirectory = os.tmpdir()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.tempDirectory
|
2016-10-06 10:02:46 -07:00
|
|
|
}
|
2016-11-08 11:03:57 +11:00
|
|
|
|
2016-11-22 19:30:20 +11:00
|
|
|
getUploadToServer () {
|
2016-11-08 13:39:11 +13:00
|
|
|
if (process.type === 'browser') {
|
2017-02-02 13:00:00 -08:00
|
|
|
return binding.getUploadToServer()
|
2016-11-08 13:39:11 +13:00
|
|
|
} else {
|
2016-11-22 19:30:20 +11:00
|
|
|
throw new Error('getUploadToServer can only be called from the main process')
|
2016-11-08 13:39:11 +13:00
|
|
|
}
|
2016-11-08 11:03:57 +11:00
|
|
|
}
|
|
|
|
|
2016-11-22 19:30:20 +11:00
|
|
|
setUploadToServer (uploadToServer) {
|
2016-11-08 13:39:11 +13:00
|
|
|
if (process.type === 'browser') {
|
2017-02-02 13:00:00 -08:00
|
|
|
return binding.setUploadToServer(uploadToServer)
|
2016-11-08 13:39:11 +13:00
|
|
|
} else {
|
2016-11-22 19:30:20 +11:00
|
|
|
throw new Error('setUploadToServer can only be called from the main process')
|
2016-11-08 13:39:11 +13:00
|
|
|
}
|
2016-11-08 11:03:57 +11:00
|
|
|
}
|
2017-02-02 14:23:21 -08:00
|
|
|
|
2017-11-01 21:57:43 -04:00
|
|
|
addExtraParameter (key, value) {
|
|
|
|
binding.addExtraParameter(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
removeExtraParameter (key) {
|
|
|
|
binding.removeExtraParameter(key)
|
|
|
|
}
|
|
|
|
|
2017-10-31 13:06:54 -04:00
|
|
|
getParameters (key, value) {
|
2017-10-31 13:51:44 -04:00
|
|
|
return binding.getParameters()
|
2017-10-31 13:06:54 -04:00
|
|
|
}
|
2016-10-06 10:02:46 -07:00
|
|
|
}
|
|
|
|
|
2016-03-25 12:45:13 -07:00
|
|
|
module.exports = new CrashReporter()
|