electron/lib/common/api/crash-reporter.js

129 lines
3.4 KiB
JavaScript
Raw Normal View History

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