TeaSpeak-Client/modules/crash_handler/index.ts

108 lines
3.9 KiB
TypeScript
Raw Normal View History

2019-10-27 14:33:00 -04:00
require("../shared/require").setup_require(module);
2020-12-02 12:08:49 -05:00
import {app, BrowserWindow, dialog, remote} from "electron";
2019-10-25 19:51:40 -04:00
import * as path from "path";
import * as electron from "electron";
import * as os from "os";
2020-07-28 14:01:25 -04:00
import * as url from "url";
2019-10-25 19:51:40 -04:00
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);
2020-12-02 12:08:49 -05:00
let error;
let crashFile;
2019-10-25 19:51:40 -04:00
if(parameter["success"] == true) {
/* okey we have an crash dump */
2020-12-02 12:08:49 -05:00
crashFile = parameter["dump_path"];
if(typeof(crashFile) === "string") {
2019-10-25 19:51:40 -04:00
try {
2020-12-02 12:08:49 -05:00
crashFile = Buffer.from(crashFile, 'base64').toString();
2019-10-25 19:51:40 -04:00
} catch(error) {
console.warn("Failed to decode dump path: %o", error);
2020-12-02 12:08:49 -05:00
crashFile = undefined;
2019-10-25 19:51:40 -04:00
error = "failed to decode dump path!";
}
}
} else if(typeof(parameter["error"]) === "string") {
try {
2020-12-02 12:08:49 -05:00
error = Buffer.from(parameter["error"], 'base64').toString();
2019-10-25 19:51:40 -04:00
} catch(error) {
console.warn("Failed to decode error: %o", error);
error = "failed to decode error";
}
} else {
error = "missing parameters";
}
app.on('ready', () => {
2020-12-02 12:08:49 -05:00
const crashWindow = new BrowserWindow({
2019-10-25 19:51:40 -04:00
show: false,
width: 1000,
height: 300 + (os.platform() === "win32" ? 50 : 0),
webPreferences: {
devTools: true,
nodeIntegration: true,
javascript: true
}
});
2020-12-02 12:08:49 -05:00
crashWindow.on('focus', event => crashWindow.flashFrame(false));
2019-10-27 18:21:08 -04:00
2020-12-02 12:08:49 -05:00
crashWindow.setMenu(null);
crashWindow.loadURL(url.pathToFileURL(path.join(path.dirname(module.filename), "ui", "index.html")).toString()).catch(error => {
dialog.showErrorBox("Crash window failed to load", "Failed to load the crash window.\nThis indicates that something went incredible wrong.\n\nError:\n" + error);
2019-10-25 19:51:40 -04:00
});
2020-12-02 12:08:49 -05:00
crashWindow.on('ready-to-show', () => {
if(error) {
crashWindow.webContents.send('dump-error', error);
} else if(!crashFile) {
crashWindow.webContents.send('dump-error', "Missing crash file");
} else {
crashWindow.webContents.send('dump-url', crashFile);
}
crashWindow.show();
crashWindow.setProgressBar(1, { mode: "error" });
crashWindow.flashFrame(true);
});
2019-10-25 19:51:40 -04:00
app.on('window-all-closed', () => {
process.exit(0);
});
});
2019-10-27 18:21:08 -04:00
app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required');
2019-10-25 19:51:40 -04:00
}
2020-12-02 12:08:49 -05:00
export const handler = require("teaclient_crash_handler");
if(typeof window === "object") {
2020-08-22 15:33:30 -04:00
(window as any).crash = handler;
2020-12-02 12:08:49 -05:00
}
2019-10-25 19:51:40 -04:00
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();
}