123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- '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')
- class CrashReporter {
- start (options) {
- if (options == null) options = {}
- this.productName = options.productName != null ? options.productName : app.getName()
- let {
- companyName,
- extra,
- ignoreSystemCrashHandler,
- submitURL,
- uploadToServer
- } = options
- if (uploadToServer == null) {
- 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 (companyName == null) {
- throw new Error('companyName is a required option to crashReporter.start')
- }
- if (submitURL == null) {
- 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'
- ]
- this._crashServiceProcess = spawn(process.execPath, args, {
- env: env,
- detached: true
- })
- }
- binding.start(this.getProductName(), companyName, submitURL, this.getCrashesDirectory(), uploadToServer, ignoreSystemCrashHandler, extra)
- }
- getLastCrashReport () {
- const reports = this.getUploadedReports()
- .sort((a, b) => {
- const ats = (a && a.date) ? new Date(a.date).getTime() : 0
- const bts = (b && b.date) ? new Date(b.date).getTime() : 0
- return bts - ats
- })
- return (reports.length > 0) ? reports[0] : null
- }
- getUploadedReports () {
- return binding.getUploadedReports(this.getCrashesDirectory())
- }
- 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) {
- this.tempDirectory = os.tmpdir()
- }
- }
- return this.tempDirectory
- }
- getUploadToServer () {
- if (process.type === 'browser') {
- return binding.getUploadToServer()
- } else {
- throw new Error('getUploadToServer can only be called from the main process')
- }
- }
- setUploadToServer (uploadToServer) {
- if (process.type === 'browser') {
- return binding.setUploadToServer(uploadToServer)
- } else {
- throw new Error('setUploadToServer can only be called from the main process')
- }
- }
- addExtraParameter (key, value) {
- binding.addExtraParameter(key, value)
- }
- removeExtraParameter (key) {
- binding.removeExtraParameter(key)
- }
- getParameters (key, value) {
- return binding.getParameters()
- }
- }
- module.exports = new CrashReporter()
|