refactor: crashReporterInit (#16729)

This commit is contained in:
Milan Burda 2019-02-05 21:56:44 +01:00 committed by Shelley Vohr
parent c486ab207a
commit 6d674eebb1
6 changed files with 59 additions and 67 deletions

View file

@ -36,6 +36,7 @@ filenames = {
"lib/browser/api/web-contents-view.js", "lib/browser/api/web-contents-view.js",
"lib/browser/chrome-devtools.js", "lib/browser/chrome-devtools.js",
"lib/browser/chrome-extension.js", "lib/browser/chrome-extension.js",
"lib/browser/crash-reporter-init.js",
"lib/browser/default-menu.js", "lib/browser/default-menu.js",
"lib/browser/guest-view-manager.js", "lib/browser/guest-view-manager.js",
"lib/browser/guest-window-manager.js", "lib/browser/guest-window-manager.js",

View file

@ -1,13 +1,11 @@
'use strict' 'use strict'
const CrashReporter = require('@electron/internal/common/crash-reporter') const CrashReporter = require('@electron/internal/common/crash-reporter')
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal') const { crashReporterInit } = require('@electron/internal/browser/crash-reporter-init')
class CrashReporterMain extends CrashReporter { class CrashReporterMain extends CrashReporter {
sendSync (channel, ...args) { init (options) {
const event = {} return crashReporterInit(options)
ipcMainInternal.emit(channel, event, ...args)
return event.returnValue
} }
} }

View file

@ -0,0 +1,46 @@
'use strict'
const { app } = require('electron')
const cp = require('child_process')
const os = require('os')
const path = require('path')
const getTempDirectory = function () {
try {
return app.getPath('temp')
} catch (error) {
return os.tmpdir()
}
}
exports.crashReporterInit = function (options) {
const productName = options.productName || app.getName()
const crashesDirectory = path.join(getTempDirectory(), `${productName} Crashes`)
let crashServicePid
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'
]
const crashServiceProcess = cp.spawn(process.helperExecPath, args, {
env,
detached: true
})
crashServicePid = crashServiceProcess.pid
}
return {
productName,
crashesDirectory,
crashServicePid,
appVersion: app.getVersion()
}
}

View file

@ -1,17 +1,16 @@
'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 eventBinding = process.atomBinding('event') const eventBinding = process.atomBinding('event')
const { isPromise } = electron const { isPromise } = electron
const { crashReporterInit } = require('@electron/internal/browser/crash-reporter-init')
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal') const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal')
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils')
const objectsRegistry = require('@electron/internal/browser/objects-registry') const objectsRegistry = require('@electron/internal/browser/objects-registry')
const guestViewManager = require('@electron/internal/browser/guest-view-manager') const guestViewManager = require('@electron/internal/browser/guest-view-manager')
const bufferUtils = require('@electron/internal/common/buffer-utils') const bufferUtils = require('@electron/internal/common/buffer-utils')
@ -470,46 +469,6 @@ ipcMainInternal.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()
}
}
const crashReporterInit = function (options) {
const productName = options.productName || electron.app.getName()
const crashesDirectory = path.join(getTempDirectory(), `${productName} Crashes`)
let crashServicePid
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'
]
const crashServiceProcess = spawn(process.helperExecPath, args, {
env,
detached: true
})
crashServicePid = crashServiceProcess.pid
}
return {
productName,
crashesDirectory,
crashServicePid,
appVersion: electron.app.getVersion()
}
}
const setReturnValue = function (event, getValue) { const setReturnValue = function (event, getValue) {
try { try {
event.returnValue = [null, getValue()] event.returnValue = [null, getValue()]
@ -518,8 +477,8 @@ const setReturnValue = function (event, getValue) {
} }
} }
ipcMainInternal.on('ELECTRON_CRASH_REPORTER_INIT', function (event, options) { ipcMainUtils.handleSync('ELECTRON_CRASH_REPORTER_INIT', function (event, options) {
setReturnValue(event, () => crashReporterInit(options)) return crashReporterInit(options)
}) })
ipcMainInternal.on('ELECTRON_BROWSER_GET_LAST_WEB_PREFERENCES', function (event) { ipcMainInternal.on('ELECTRON_BROWSER_GET_LAST_WEB_PREFERENCES', function (event) {

View file

@ -2,28 +2,16 @@
const binding = process.atomBinding('crash_reporter') const binding = process.atomBinding('crash_reporter')
const errorUtils = require('@electron/internal/common/error-utils')
class CrashReporter { class CrashReporter {
contructor () { contructor () {
this.productName = null this.productName = null
this.crashesDirectory = null this.crashesDirectory = null
} }
sendSync (channel, ...args) { init (options) {
throw new Error('Not implemented') throw new Error('Not implemented')
} }
invoke (command, ...args) {
const [ error, result ] = this.sendSync(command, ...args)
if (error) {
throw errorUtils.deserialize(error)
}
return result
}
start (options) { start (options) {
if (options == null) options = {} if (options == null) options = {}
@ -51,7 +39,7 @@ class CrashReporter {
throw new Error('submitURL is a required option to crashReporter.start') throw new Error('submitURL is a required option to crashReporter.start')
} }
const ret = this.invoke('ELECTRON_CRASH_REPORTER_INIT', { const ret = this.init({
submitURL, submitURL,
productName productName
}) })

View file

@ -1,11 +1,11 @@
'use strict' 'use strict'
const CrashReporter = require('@electron/internal/common/crash-reporter') const CrashReporter = require('@electron/internal/common/crash-reporter')
const ipcRenderer = require('@electron/internal/renderer/ipc-renderer-internal') const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils')
class CrashReporterRenderer extends CrashReporter { class CrashReporterRenderer extends CrashReporter {
sendSync (channel, ...args) { init (options) {
return ipcRenderer.sendSync(channel, ...args) return ipcRendererUtils.invokeSync('ELECTRON_CRASH_REPORTER_INIT', options)
} }
} }