Files
privacy.sexy/src/presentation/electron/main/index.ts
undergroundwires c84a1bb74c Fix script deletion during execution on desktop
This commit fixes an issue seen on certain Windows environments (Windows
10 22H2 and 11 23H2 Pro Azure VMs) where scripts were being deleted
during execution due to temporary directory usage. To resolve this,
scripts are now stored in a persistent directory, enhancing reliability
for long-running scripts and improving auditability along with
troubleshooting.

Key changes:

- Move script execution logic to the `main` process from `preloader` to
  utilize Electron's `app.getPath`.
- Improve runtime environment detection for non-browser environments to
  allow its usage in Electron main process.
- Introduce a secure module to expose IPC channels from the main process
  to the renderer via the preloader process.

Supporting refactorings include:

- Simplify `CodeRunner` interface by removing the `tempScriptFolderName`
  parameter.
- Rename `NodeSystemOperations` to `NodeElectronSystemOperations` as it
  now wraps electron APIs too, and convert it to class for simplicity.
- Rename `TemporaryFileCodeRunner` to `ScriptFileCodeRunner` to reflect
  its new functinoality.
- Rename `SystemOperations` folder to `System` for simplicity.
- Rename `HostRuntimeEnvironment` to `BrowserRuntimeEnvironment` for
  clarity.
- Refactor main Electron process configuration to align with latest
  Electron documentation/recommendations.
- Refactor unit tests `BrowserRuntimeEnvironment` to simplify singleton
  workaround.
- Use alias imports like `electron/main` and `electron/common` for
  better clarity.
2024-01-06 18:47:58 +01:00

180 lines
5.4 KiB
TypeScript

// Initializes Electron's main process, always runs in the background, and manages the main window.
import {
app, protocol, BrowserWindow, screen,
} from 'electron/main';
import { shell } from 'electron/common';
import log from 'electron-log/main';
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer';
import { validateRuntimeSanity } from '@/infrastructure/RuntimeSanity/SanityChecks';
import { ElectronLogger } from '@/infrastructure/Log/ElectronLogger';
import { name } from '@/../package.json' assert { type: 'json' };
import { setupAutoUpdater } from './Update/UpdateInitializer';
import {
APP_ICON_PATH, PRELOADER_SCRIPT_PATH, RENDERER_HTML_PATH, RENDERER_URL,
} from './ElectronConfig';
import { registerAllIpcChannels } from './IpcRegistration';
const isDevelopment = !app.isPackaged;
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win: BrowserWindow | null;
// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
{ scheme: 'app', privileges: { secure: true, standard: true } },
]);
setupLogger();
validateRuntimeSanity({
// Metadata is used by manual updates.
validateEnvironmentVariables: true,
// Environment is populated by the preload script and is in the renderer's context;
// it's not directly accessible from the main process.
validateWindowVariables: false,
});
function createWindow() {
// Create the browser window.
const size = getWindowSize(1650, 955);
win = new BrowserWindow({
width: size.width,
height: size.height,
webPreferences: {
nodeIntegration: true, // disabling does not work with electron-vite, https://electron-vite.org/guide/dev.html#nodeintegration
contextIsolation: true,
preload: PRELOADER_SCRIPT_PATH,
},
icon: APP_ICON_PATH,
});
win.setMenuBarVisibility(false);
configureExternalsUrlsOpenBrowser(win);
loadApplication(win);
win.on('closed', () => {
win = null;
});
}
configureAppQuitBehavior();
registerAllIpcChannels();
setAppName();
app.whenReady().then(async () => {
if (isDevelopment) {
try {
await installExtension(VUEJS_DEVTOOLS);
} catch (e) {
ElectronLogger.error('Vue Devtools failed to install:', e.toString());
}
}
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
createWindow();
}
});
});
// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
if (process.platform === 'win32') {
process.on('message', (data) => {
if (data === 'graceful-exit') {
app.quit();
}
});
} else {
process.on('SIGTERM', () => {
app.quit();
});
}
}
function loadApplication(window: BrowserWindow) {
if (RENDERER_URL) { // Populated in a dev server during development
loadUrlWithNodeWorkaround(window, RENDERER_URL);
} else {
loadUrlWithNodeWorkaround(window, RENDERER_HTML_PATH);
}
if (isDevelopment) {
window.webContents.openDevTools();
} else {
const updater = setupAutoUpdater();
updater.checkForUpdates();
}
// Do not remove [WINDOW_INIT]; it's a marker used in tests.
ElectronLogger.info('[WINDOW_INIT] Main window initialized and content loading.');
}
function configureExternalsUrlsOpenBrowser(window: BrowserWindow) {
window.webContents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url);
return { action: 'deny' };
});
}
// Workaround for https://github.com/electron/electron/issues/19554 otherwise fs does not work
function loadUrlWithNodeWorkaround(window: BrowserWindow, url: string) {
setTimeout(() => {
window.loadURL(url);
}, 10);
}
function getWindowSize(idealWidth: number, idealHeight: number) {
let { width, height } = screen.getPrimaryDisplay().workAreaSize;
// To ensure not creating a screen bigger than current screen size
// Not using "enableLargerThanScreen" as it's macOS only (see https://www.electronjs.org/docs/api/browser-window)
width = Math.min(width, idealWidth);
height = Math.min(height, idealHeight);
return { width, height };
}
function setupLogger(): void {
// log.initialize(); ← We inject logger to renderer through preloader, this is not needed.
log.transports.file.level = 'silly';
log.eventLogger.startLogging();
}
function configureAppQuitBehavior() {
let macOsQuit = false;
// Quit when all windows are closed.
app.on('window-all-closed', () => {
if (process.platform === 'darwin'
&& !macOsQuit) {
/*
On macOS it is common for applications and their menu bar
to stay active until the user quits explicitly with Cmd + Q
*/
return;
}
app.quit();
});
if (process.platform === 'darwin') {
/*
On macOS we application quit is stopped if user does not Cmd + Q
But we still want to be able to use app.quit() and quit the application
on menu bar, after updates etc.
*/
app.on('before-quit', () => {
macOsQuit = true;
});
}
}
function setAppName() {
/*
Set the app name in development mode to ensure correct userData path.
Without this, `app.getPath('userData')` defaults to 'Electron'.
*/
if (isDevelopment) {
app.setName(name); // Works only for Windows, unsolved for macOS.
}
}