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

127 lines
3.3 KiB
JavaScript
Raw Normal View History

'use strict'
const {spawn} = require('child_process')
const os = require('os')
const path = require('path')
const electron = require('electron')
const {app} = process.type === 'browser' ? electron : electron.remote
const binding = process.atomBinding('crash_reporter')
2016-01-11 18:40:23 -08:00
class CrashReporter {
start (options) {
2016-01-11 18:40:23 -08:00
if (options == null) {
options = {}
2016-01-11 18:40:23 -08:00
}
this.productName = options.productName != null ? options.productName : app.getName()
let {companyName, extra, ignoreSystemCrashHandler, submitURL, uploadToServer} = options
2016-01-11 18:40:23 -08:00
if (uploadToServer == null) {
// TODO: Remove deprecated autoSubmit property in 2.0
uploadToServer = options.autoSubmit
}
if (uploadToServer == null) {
2016-11-22 19:30:20 +11:00
uploadToServer = true
2016-01-11 18:40:23 -08:00
}
2016-01-11 18:40:23 -08:00
if (ignoreSystemCrashHandler == null) {
ignoreSystemCrashHandler = false
2016-01-11 18:40:23 -08:00
}
if (extra == null) {
extra = {}
2016-01-11 18:40:23 -08:00
}
if (extra._productName == null) {
extra._productName = this.getProductName()
2016-01-11 18:40:23 -08:00
}
if (extra._companyName == null) {
extra._companyName = companyName
2016-01-11 18:40:23 -08:00
}
if (extra._version == null) {
extra._version = app.getVersion()
2016-01-11 18:40:23 -08:00
}
if (companyName == null) {
throw new Error('companyName is a required option to crashReporter.start')
2016-01-11 18:40:23 -08:00
}
if (submitURL == null) {
throw new Error('submitURL is a required option to crashReporter.start')
2016-01-11 18:40:23 -08:00
}
2016-01-11 18:40:23 -08:00
if (process.platform === 'win32') {
const args = [
'--reporter-url=' + submitURL,
'--application-name=' + this.getProductName(),
'--crashes-directory=' + this.getCrashesDirectory(),
'--v=1'
]
const env = {
ELECTRON_INTERNAL_CRASH_SERVICE: 1
}
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-01-11 18:40:23 -08:00
}
2016-11-22 19:30:20 +11:00
binding.start(this.getProductName(), companyName, submitURL, this.getCrashesDirectory(), uploadToServer, ignoreSystemCrashHandler, extra)
}
2016-01-11 18:40:23 -08:00
getLastCrashReport () {
const reports = this.getUploadedReports()
2016-01-11 18:40:23 -08:00
if (reports.length > 0) {
return reports[0]
2016-01-11 18:40:23 -08:00
} else {
return null
2016-01-11 18:40:23 -08:00
}
}
2016-01-11 18:40:23 -08:00
getUploadedReports () {
2017-02-13 10:46:19 -08:00
return binding.getUploadedReports(this.getCrashesDirectory())
}
2016-01-11 18:40:23 -08:00
getCrashesDirectory () {
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) {
// app.getPath may throw so fallback to OS temp directory
this.tempDirectory = os.tmpdir()
}
}
return this.tempDirectory
}
2016-11-08 11:03:57 +11:00
2016-11-22 19:30:20 +11:00
getUploadToServer () {
if (process.type === 'browser') {
2017-02-02 13:00:00 -08:00
return binding.getUploadToServer()
} else {
2016-11-22 19:30:20 +11:00
throw new Error('getUploadToServer can only be called from the main process')
}
2016-11-08 11:03:57 +11:00
}
2016-11-22 19:30:20 +11:00
setUploadToServer (uploadToServer) {
if (process.type === 'browser') {
2017-02-02 13:00:00 -08:00
return binding.setUploadToServer(uploadToServer)
} else {
2016-11-22 19:30:20 +11:00
throw new Error('setUploadToServer can only be called from the main process')
}
2016-11-08 11:03:57 +11:00
}
2017-02-02 14:23:21 -08:00
setExtraParameter (key, value) {
binding.setExtraParameter(key, value)
}
}
2016-03-25 12:45:13 -07:00
module.exports = new CrashReporter()