import { app , crashReporter , dialog , RenderProcessGoneDetails } from 'electron' ; import { i18n } from '../common/i18n' ; import { logger } from '../common/logger' ; import { analytics , AnalyticsElements , ICrashData , SDACrashProcess , } from './analytics-handler' ; import { ICustomBrowserWindow } from './window-handler' ; import { windowExists } from './window-utils' ; class CrashHandler { param browserWindow private private static async showMessageToUser ( browserWindow ) { if ( ! browserWindow || ! windowExists ( browserWindow ) ) { return ; } const { response } = await dialog . showMessageBox ( { type : 'error' , title : i18n . t ( 'Renderer Process Crashed' ) ( ) , message : i18n . t ( 'Oops! Looks like we have had a crash. Please reload or close this window.' ) ( ) , buttons : [ 'Reload' , 'Close' ] , } ) ; response === 0 ? browserWindow . reload ( ) : browserWindow . close ( ) ; } private private static handleGpuCrash ( ) { app . on ( 'gpu-process-crashed' , ( _event , _killed ) => { logger . info ( `crash-handler: GPU process crashed.` ) ; const eventData = { element : AnalyticsElements . SDA_CRASH , process : SDACrashProcess . GPU , windowName : 'main' , crashCause : _killed ? 'killed' : 'crashed' , } ; analytics . track ( eventData ) ; logger . info ( `crash-handler: GPU process crash event processed with data ${ JSON . stringify ( eventData ) }` ) ; } ) ; } private private static handleMainProcessCrash ( ) { const lastCrash = crashReporter . getLastCrashReport ( ) ; if ( ! lastCrash ) { logger . info ( `crash-handler: No crashes found for main process` ) ; return ; } logger . info ( 'crash-handler: Main process crashes found' ) ; } constructor ( ) { CrashHandler . handleMainProcessCrash ( ) ; CrashHandler . handleGpuCrash ( ) ; } param browserWindow public handleRendererCrash ( browserWindow ) { browserWindow . webContents . on ( 'render-process-gone' , async ( _event , details ) => { logger . info ( `crash-handler: Renderer process for ${ browserWindow . winName } crashed.
            Reason is ${ details . reason }` ) ; const eventData = { element : AnalyticsElements . SDA_CRASH , process : SDACrashProcess . RENDERER , windowName : browserWindow . winName , crashCause : details . reason , } ; switch ( details . reason ) { case 'abnormal-exit' : case 'crashed' : case 'integrity-failure' : case 'launch-failed' : case 'oom' : await CrashHandler . showMessageToUser ( browserWindow ) ; analytics . track ( eventData ) ; logger . info ( `crash-handler: Renderer process crash event processed with data ${ JSON . stringify ( eventData ) }` , details ) ; break ; default : break ; } } ) ; } } const crashHandler = new CrashHandler ( ) ; export default crashHandler ;