refactor: implement crashReporter.start() without the remote module (#14434)

This commit is contained in:
Milan Burda 2018-09-26 07:43:34 +02:00 committed by Samuel Attard
parent 560b1c17af
commit 3df739fa89
3 changed files with 71 additions and 50 deletions

View file

@ -1,18 +1,29 @@
'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')
const sendSync = function (channel, ...args) {
if (process.type === 'browser') {
let event = {}
electron.ipcMain.emit(channel, event, ...args)
return event.returnValue
} else {
return electron.ipcRenderer.sendSync(channel, ...args)
}
}
class CrashReporter {
contructor () {
this.productName = null
this.crashesDirectory = null
}
start (options) {
if (options == null) options = {}
this.productName = options.productName != null ? options.productName : app.getName()
let {
productName,
companyName,
extra,
ignoreSystemCrashHandler,
@ -24,12 +35,9 @@ class CrashReporter {
uploadToServer = true
}
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()
if (ignoreSystemCrashHandler == null) {
ignoreSystemCrashHandler = false
}
if (companyName == null) {
throw new Error('companyName is a required option to crashReporter.start')
@ -38,24 +46,20 @@ class CrashReporter {
throw new Error('submitURL is a required option to crashReporter.start')
}
if (process.platform === 'win32') {
const env = {
ELECTRON_INTERNAL_CRASH_SERVICE: 1
}
const args = [
'--reporter-url=' + submitURL,
'--application-name=' + this.getProductName(),
'--crashes-directory=' + this.getCrashesDirectory(),
'--v=1'
]
const ret = sendSync('ELECTRON_CRASH_REPORTER_INIT', {
submitURL,
productName
})
this._crashServiceProcess = spawn(process.execPath, args, {
env: env,
detached: true
})
}
this.productName = ret.productName
this.crashesDirectory = ret.crashesDirectory
binding.start(this.getProductName(), companyName, submitURL, this.getCrashesDirectory(), uploadToServer, ignoreSystemCrashHandler, extra)
if (extra == null) extra = {}
if (extra._productName == null) extra._productName = ret.productName
if (extra._companyName == null) extra._companyName = companyName
if (extra._version == null) extra._version = ret.appVersion
binding.start(ret.productName, companyName, submitURL, ret.crashesDirectory, uploadToServer, ignoreSystemCrashHandler, extra)
}
getLastCrashReport () {
@ -74,28 +78,13 @@ class CrashReporter {
}
getCrashesDirectory () {
const crashesDir = `${this.getProductName()} Crashes`
return path.join(this.getTempDirectory(), crashesDir)
return this.crashesDirectory
}
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
}
getUploadToServer () {
if (process.type === 'browser') {
return binding.getUploadToServer()