2016-03-25 19:41:24 +00:00
|
|
|
'use strict'
|
2016-03-10 19:54:17 +00:00
|
|
|
|
2016-10-05 20:40:39 +00:00
|
|
|
const {spawn} = require('child_process')
|
2016-10-06 17:02:46 +00:00
|
|
|
const os = require('os')
|
2016-10-06 17:39:57 +00:00
|
|
|
const path = require('path')
|
2016-03-25 19:41:24 +00:00
|
|
|
const electron = require('electron')
|
2016-10-05 22:39:47 +00:00
|
|
|
const {app} = process.type === 'browser' ? electron : electron.remote
|
2016-03-25 19:41:24 +00:00
|
|
|
const binding = process.atomBinding('crash_reporter')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-10-05 20:40:39 +00:00
|
|
|
class CrashReporter {
|
|
|
|
start (options) {
|
2016-01-12 02:40:23 +00:00
|
|
|
if (options == null) {
|
2016-03-25 19:41:24 +00:00
|
|
|
options = {}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-10-06 17:39:57 +00:00
|
|
|
this.productName = options.productName != null ? options.productName : app.getName()
|
2016-10-05 20:40:39 +00:00
|
|
|
let {autoSubmit, companyName, extra, ignoreSystemCrashHandler, submitURL} = options
|
2016-01-12 02:40:23 +00:00
|
|
|
|
|
|
|
if (autoSubmit == null) {
|
2016-03-25 19:41:24 +00:00
|
|
|
autoSubmit = true
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (ignoreSystemCrashHandler == null) {
|
2016-03-25 19:41:24 +00:00
|
|
|
ignoreSystemCrashHandler = false
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (extra == null) {
|
2016-03-25 19:41:24 +00:00
|
|
|
extra = {}
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (extra._productName == null) {
|
2016-10-06 17:39:57 +00:00
|
|
|
extra._productName = this.getProductName()
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (extra._companyName == null) {
|
2016-03-25 19:41:24 +00:00
|
|
|
extra._companyName = companyName
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (extra._version == null) {
|
2016-03-25 19:41:24 +00:00
|
|
|
extra._version = app.getVersion()
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (companyName == null) {
|
2016-04-28 16:48:13 +00:00
|
|
|
throw new Error('companyName is a required option to crashReporter.start')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
if (submitURL == null) {
|
2016-04-28 16:48:13 +00:00
|
|
|
throw new Error('submitURL is a required option to crashReporter.start')
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-10-05 20:40:39 +00:00
|
|
|
|
2016-01-12 02:40:23 +00:00
|
|
|
if (process.platform === 'win32') {
|
2016-10-05 23:15:49 +00:00
|
|
|
const args = [
|
|
|
|
'--reporter-url=' + submitURL,
|
2016-10-06 17:39:57 +00:00
|
|
|
'--application-name=' + this.getProductName(),
|
|
|
|
'--crashes-directory=' + this.getCrashesDirectory(),
|
2016-10-05 23:15:49 +00:00
|
|
|
'--v=1'
|
|
|
|
]
|
2016-10-05 20:40:39 +00:00
|
|
|
const env = {
|
2016-05-24 17:22:13 +00:00
|
|
|
ELECTRON_INTERNAL_CRASH_SERVICE: 1
|
2016-03-25 19:41:24 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
spawn(process.execPath, args, {
|
|
|
|
env: env,
|
|
|
|
detached: true
|
2016-03-25 19:41:24 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-10-05 20:40:39 +00:00
|
|
|
|
2016-10-06 17:39:57 +00:00
|
|
|
binding.start(this.getProductName(), companyName, submitURL, this.getCrashesDirectory(), autoSubmit, ignoreSystemCrashHandler, extra)
|
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()
|
2016-01-12 02:40:23 +00:00
|
|
|
if (reports.length > 0) {
|
2016-03-25 19:41:24 +00:00
|
|
|
return reports[0]
|
2016-01-12 02:40:23 +00:00
|
|
|
} else {
|
2016-03-25 19:41:24 +00:00
|
|
|
return null
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-25 19:41:24 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-10-05 20:40:39 +00:00
|
|
|
getUploadedReports () {
|
2016-10-06 17:39:57 +00:00
|
|
|
return binding._getUploadedReports(this.getCrashesDirectory())
|
2016-03-25 19:41:24 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-10-06 17:39:57 +00: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-10-06 17:02:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 19:45:13 +00:00
|
|
|
module.exports = new CrashReporter()
|