import {app, BrowserWindow, remote} from "electron"; import * as path from "path"; import * as electron from "electron"; import * as os from "os"; export function handle_crash_callback(args: string[]) { const parameter = {}; for(const argument of args) { const colon_index = argument.indexOf('='); if(colon_index == -1) { console.warn("Crash callback contains invalid argument! (%s)", argument); continue; } parameter[argument.substr(0, colon_index)] = argument.substr(colon_index + 1); } console.log("Received crash dump callback. Arguments: %o", parameter); let error = undefined; let crash_file = undefined; if(parameter["success"] == true) { /* okey we have an crash dump */ crash_file = parameter["dump_path"]; if(typeof(crash_file) === "string") { try { crash_file = Buffer.from(crash_file, 'base64').toString(); } catch(error) { console.warn("Failed to decode dump path: %o", error); crash_file = undefined; error = "failed to decode dump path!"; } } } else if(typeof(parameter["error"]) === "string") { try { error = Buffer.from(crash_file, 'base64').toString(); } catch(error) { console.warn("Failed to decode error: %o", error); error = "failed to decode error"; } } else { error = "missing parameters"; } app.on('ready', () => { const crash_window = new BrowserWindow({ show: false, width: 1000, height: 300 + (os.platform() === "win32" ? 50 : 0), webPreferences: { devTools: true, nodeIntegration: true, javascript: true } }); crash_window.setMenu(null); crash_window.loadFile(path.join(path.dirname(module.filename), "ui", "index.html")); crash_window.on('ready-to-show', () => { if(error) crash_window.webContents.send('dump-error', error); else if(!crash_file) crash_window.webContents.send('dump-error', "Missing crash file"); else crash_window.webContents.send('dump-url', crash_file); crash_window.show(); }); app.on('window-all-closed', () => { process.exit(0); }); }); } module.paths.push(...(() => { const app_path = (remote || electron).app.getAppPath(); const result = []; result.push(app_path + "/native/build/" + os.platform() + "_" + os.arch() + "/"); if(app_path.endsWith(".asar")) result.push(path.join(path.dirname(app_path), "natives")); return result; })()); export const handler = require( "teaclient_crash_handler"); export function initialize_handler(component_name: string, requires_file: boolean) { const start_path = requires_file ? (" " + path.join(__dirname, "..", "..")) : ""; const success_arguments = process.argv[0] + start_path + " crash-handler success=1 dump_path=%crash_path%"; const error_arguments = process.argv[0] + start_path + " crash-handler success=0 error=%error_message%"; console.log("Setting up crash handler. Success callback: %s; Error callback: %s", success_arguments, error_arguments); handler.setup_crash_handler( component_name, path.join((remote || electron).app.getPath('userData'), "crash_dumps"), success_arguments, error_arguments ); } export function finalize_handler() { handler.finalize(); }