refactor: implement crashReporter.start() without the remote module (#14434)
This commit is contained in:
parent
560b1c17af
commit
3df739fa89
3 changed files with 71 additions and 50 deletions
|
@ -1,8 +1,11 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
const { spawn } = require('child_process')
|
||||||
const electron = require('electron')
|
const electron = require('electron')
|
||||||
const { EventEmitter } = require('events')
|
const { EventEmitter } = require('events')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
|
const os = require('os')
|
||||||
|
const path = require('path')
|
||||||
const v8Util = process.atomBinding('v8_util')
|
const v8Util = process.atomBinding('v8_util')
|
||||||
|
|
||||||
const { ipcMain, isPromise } = electron
|
const { ipcMain, isPromise } = electron
|
||||||
|
@ -396,6 +399,42 @@ ipcMain.on('ELECTRON_BROWSER_WINDOW_CLOSE', function (event) {
|
||||||
event.returnValue = null
|
event.returnValue = null
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const getTempDirectory = function () {
|
||||||
|
try {
|
||||||
|
return electron.app.getPath('temp')
|
||||||
|
} catch (error) {
|
||||||
|
return os.tmpdir()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ipcMain.on('ELECTRON_CRASH_REPORTER_INIT', function (event, options) {
|
||||||
|
const productName = options.productName || electron.app.getName()
|
||||||
|
const crashesDirectory = path.join(getTempDirectory(), `${productName} Crashes`)
|
||||||
|
|
||||||
|
if (process.platform === 'win32') {
|
||||||
|
const env = {
|
||||||
|
ELECTRON_INTERNAL_CRASH_SERVICE: 1
|
||||||
|
}
|
||||||
|
const args = [
|
||||||
|
'--reporter-url=' + options.submitURL,
|
||||||
|
'--application-name=' + productName,
|
||||||
|
'--crashes-directory=' + crashesDirectory,
|
||||||
|
'--v=1'
|
||||||
|
]
|
||||||
|
|
||||||
|
spawn(process.helperExecPath, args, {
|
||||||
|
env,
|
||||||
|
detached: true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
event.returnValue = {
|
||||||
|
productName,
|
||||||
|
crashesDirectory,
|
||||||
|
appVersion: electron.app.getVersion()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
ipcMain.on('ELECTRON_BROWSER_SANDBOX_LOAD', function (event) {
|
ipcMain.on('ELECTRON_BROWSER_SANDBOX_LOAD', function (event) {
|
||||||
const preloadPath = event.sender._getPreloadPath()
|
const preloadPath = event.sender._getPreloadPath()
|
||||||
let preloadSrc = null
|
let preloadSrc = null
|
||||||
|
|
|
@ -1,18 +1,29 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const { spawn } = require('child_process')
|
|
||||||
const os = require('os')
|
|
||||||
const path = require('path')
|
|
||||||
const electron = require('electron')
|
const electron = require('electron')
|
||||||
const { app } = process.type === 'browser' ? electron : electron.remote
|
|
||||||
const binding = process.atomBinding('crash_reporter')
|
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 {
|
class CrashReporter {
|
||||||
|
contructor () {
|
||||||
|
this.productName = null
|
||||||
|
this.crashesDirectory = null
|
||||||
|
}
|
||||||
|
|
||||||
start (options) {
|
start (options) {
|
||||||
if (options == null) options = {}
|
if (options == null) options = {}
|
||||||
this.productName = options.productName != null ? options.productName : app.getName()
|
|
||||||
|
|
||||||
let {
|
let {
|
||||||
|
productName,
|
||||||
companyName,
|
companyName,
|
||||||
extra,
|
extra,
|
||||||
ignoreSystemCrashHandler,
|
ignoreSystemCrashHandler,
|
||||||
|
@ -24,12 +35,9 @@ class CrashReporter {
|
||||||
uploadToServer = true
|
uploadToServer = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ignoreSystemCrashHandler == null) ignoreSystemCrashHandler = false
|
if (ignoreSystemCrashHandler == null) {
|
||||||
if (extra == null) extra = {}
|
ignoreSystemCrashHandler = false
|
||||||
|
}
|
||||||
if (extra._productName == null) extra._productName = this.getProductName()
|
|
||||||
if (extra._companyName == null) extra._companyName = companyName
|
|
||||||
if (extra._version == null) extra._version = app.getVersion()
|
|
||||||
|
|
||||||
if (companyName == null) {
|
if (companyName == null) {
|
||||||
throw new Error('companyName is a required option to crashReporter.start')
|
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')
|
throw new Error('submitURL is a required option to crashReporter.start')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (process.platform === 'win32') {
|
const ret = sendSync('ELECTRON_CRASH_REPORTER_INIT', {
|
||||||
const env = {
|
submitURL,
|
||||||
ELECTRON_INTERNAL_CRASH_SERVICE: 1
|
productName
|
||||||
}
|
|
||||||
const args = [
|
|
||||||
'--reporter-url=' + submitURL,
|
|
||||||
'--application-name=' + this.getProductName(),
|
|
||||||
'--crashes-directory=' + this.getCrashesDirectory(),
|
|
||||||
'--v=1'
|
|
||||||
]
|
|
||||||
|
|
||||||
this._crashServiceProcess = spawn(process.execPath, args, {
|
|
||||||
env: env,
|
|
||||||
detached: true
|
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
binding.start(this.getProductName(), companyName, submitURL, this.getCrashesDirectory(), uploadToServer, ignoreSystemCrashHandler, extra)
|
this.productName = ret.productName
|
||||||
|
this.crashesDirectory = ret.crashesDirectory
|
||||||
|
|
||||||
|
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 () {
|
getLastCrashReport () {
|
||||||
|
@ -74,28 +78,13 @@ class CrashReporter {
|
||||||
}
|
}
|
||||||
|
|
||||||
getCrashesDirectory () {
|
getCrashesDirectory () {
|
||||||
const crashesDir = `${this.getProductName()} Crashes`
|
return this.crashesDirectory
|
||||||
return path.join(this.getTempDirectory(), crashesDir)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getProductName () {
|
getProductName () {
|
||||||
if (this.productName == null) {
|
|
||||||
this.productName = app.getName()
|
|
||||||
}
|
|
||||||
return this.productName
|
return this.productName
|
||||||
}
|
}
|
||||||
|
|
||||||
getTempDirectory () {
|
|
||||||
if (this.tempDirectory == null) {
|
|
||||||
try {
|
|
||||||
this.tempDirectory = app.getPath('temp')
|
|
||||||
} catch (error) {
|
|
||||||
this.tempDirectory = os.tmpdir()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return this.tempDirectory
|
|
||||||
}
|
|
||||||
|
|
||||||
getUploadToServer () {
|
getUploadToServer () {
|
||||||
if (process.type === 'browser') {
|
if (process.type === 'browser') {
|
||||||
return binding.getUploadToServer()
|
return binding.getUploadToServer()
|
||||||
|
|
|
@ -204,13 +204,6 @@ describe('crashReporter module', () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('getTempDirectory', () => {
|
|
||||||
it('returns temp directory for app if one is specified', () => {
|
|
||||||
const tempDir = crashReporter.getTempDirectory()
|
|
||||||
assert.strictEqual(tempDir, app.getPath('temp'))
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('start(options)', () => {
|
describe('start(options)', () => {
|
||||||
it('requires that the companyName and submitURL options be specified', () => {
|
it('requires that the companyName and submitURL options be specified', () => {
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue