main.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const { app, BrowserWindow, crashReporter } = require('electron');
  2. const path = require('path');
  3. const childProcess = require('child_process');
  4. app.setVersion('0.1.0');
  5. const url = app.commandLine.getSwitchValue('crash-reporter-url');
  6. const uploadToServer = !app.commandLine.hasSwitch('no-upload');
  7. const setExtraParameters = app.commandLine.hasSwitch('set-extra-parameters-in-renderer');
  8. const addGlobalParam = app.commandLine.getSwitchValue('add-global-param')?.split(':');
  9. crashReporter.start({
  10. productName: 'Zombies',
  11. companyName: 'Umbrella Corporation',
  12. compress: false,
  13. uploadToServer,
  14. submitURL: url,
  15. ignoreSystemCrashHandler: true,
  16. extra: {
  17. mainProcessSpecific: 'mps'
  18. },
  19. globalExtra: addGlobalParam[0] ? { [addGlobalParam[0]]: addGlobalParam[1] } : {}
  20. });
  21. app.whenReady().then(() => {
  22. const crashType = app.commandLine.getSwitchValue('crash-type');
  23. if (crashType === 'main') {
  24. process.crash();
  25. } else if (crashType === 'renderer') {
  26. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  27. w.loadURL('about:blank');
  28. if (setExtraParameters) {
  29. w.webContents.executeJavaScript(`
  30. require('electron').crashReporter.addExtraParameter('rendererSpecific', 'rs');
  31. require('electron').crashReporter.addExtraParameter('addedThenRemoved', 'to-be-removed');
  32. require('electron').crashReporter.removeExtraParameter('addedThenRemoved');
  33. `);
  34. }
  35. w.webContents.executeJavaScript('process.crash()');
  36. w.webContents.on('render-process-gone', () => process.exit(0));
  37. } else if (crashType === 'sandboxed-renderer') {
  38. const w = new BrowserWindow({
  39. show: false,
  40. webPreferences: {
  41. sandbox: true,
  42. preload: path.resolve(__dirname, 'sandbox-preload.js'),
  43. contextIsolation: false
  44. }
  45. });
  46. w.loadURL(`about:blank?set_extra=${setExtraParameters ? 1 : 0}`);
  47. w.webContents.on('render-process-gone', () => process.exit(0));
  48. } else if (crashType === 'node') {
  49. const crashPath = path.join(__dirname, 'node-crash.js');
  50. const child = childProcess.fork(crashPath, { silent: true });
  51. child.on('exit', () => process.exit(0));
  52. } else if (crashType === 'node-fork') {
  53. const scriptPath = path.join(__dirname, 'fork.js');
  54. const child = childProcess.fork(scriptPath, { silent: true });
  55. child.on('exit', () => process.exit(0));
  56. } else if (crashType === 'node-extra-args') {
  57. let exitcode = -1;
  58. const crashPath = path.join(__dirname, 'node-extra-args.js');
  59. const child = childProcess.fork(crashPath, ['--enable-logging'], { silent: true });
  60. child.send('message');
  61. child.on('message', (forkedArgs) => {
  62. if (JSON.stringify(forkedArgs) !== JSON.stringify(child.spawnargs)) { exitcode = 1; } else { exitcode = 0; }
  63. process.exit(exitcode);
  64. });
  65. } else {
  66. console.error(`Unrecognized crash type: '${crashType}'`);
  67. process.exit(1);
  68. }
  69. });
  70. setTimeout(() => app.exit(), 30000);