diff --git a/REFACTOR_PLAN.md b/REFACTOR_PLAN.md index 4f72956..25a8ed6 100644 --- a/REFACTOR_PLAN.md +++ b/REFACTOR_PLAN.md @@ -171,7 +171,7 @@ IPC modules should validate and translate renderer requests, then call services. - Electron Builder and the desktop Actions artifact name derive from `package.json`. - Android `versionName` derives from the same package version. Its current `versionCode` mapping is `major * 10000 + minor * 100 + patch`; keep minor and patch below 100, and revise the mapping before reaching that boundary to avoid collisions. - `scripts/build-all.ps1` uses the package version for APK naming. Before a release, bump once with `npm run bump:build`, verify the lockfile and computed Android version code, and build every target without another bump. -- Latest version check: `package.json` and both lockfile version fields are `0.4.39`; Android derives `versionName 0.4.39` and `versionCode 439`. OAuth2 desktop support is in this source version; the artifact still needs to be built and verified. +- Latest version check: `package.json` and both lockfile version fields are `0.5.0`; Android derives `versionName 0.5.0` and `versionCode 500`. OAuth2 desktop support is in this source version; the artifact still needs to be built and verified. ## Progress log @@ -212,7 +212,7 @@ IPC modules should validate and translate renderer requests, then call services. - Step 6 at version `0.4.36` consolidates the HTML sanitization and remote-image policy API under `window.epMailFeatures.contentSecurity`. - Step 7 at version `0.4.37` adds focused sanitizer coverage for dangerous DOM nodes/attributes and verifies that remote images remain data-only pending explicit loading. - Step 7 at version `0.4.38` tests main-process native confirmation for OpenPGP key replacement and trust elevation; cancellation must not mutate stored key state. -- Desktop OAuth2 support for Google and Microsoft was added after the `0.4.38` artifact; `0.4.39` uses configurable OAuth client IDs, PKCE, encrypted token persistence, and provider-fixed mail endpoints. +- Desktop OAuth2 support for Google and Microsoft was added after the `0.4.38` artifact; `0.5.0` uses configurable OAuth client IDs, PKCE, encrypted token persistence, and provider-fixed mail endpoints. - Step 7 coverage status: sanitizer/remote-image, DAV reference/ETag, and OpenPGP trust/replacement paths have regression coverage. A direct plaintext-send IPC confirmation test and the documented human/security review remain open. - Renderer modularization step 6 is complete: feature modules are scoped, legacy renderer-state aliases are removed, public cross-feature interfaces are namespaced, and root/`www/` parity plus classic-script ordering are tested. Native ES module conversion remains a deferred follow-up. - The current IPC/DAV validation changes pass the full suite: 108 tests. diff --git a/desktop/main.cjs b/desktop/main.cjs index e17c37f..48498d1 100644 --- a/desktop/main.cjs +++ b/desktop/main.cjs @@ -1,3 +1,12 @@ +const startupTraceEnabled = process.env.EP_MAIL_STARTUP_TRACE === '1'; +const startupTraceStartedAt = process.hrtime.bigint(); +function traceStartup(message) { + if (!startupTraceEnabled) return; + const elapsed = Number(process.hrtime.bigint() - startupTraceStartedAt) / 1e6; + console.info(`[ep-mail startup +${elapsed.toFixed(1)}ms] ${message}`); +} +traceStartup('main process entered'); + const { app, BrowserWindow, shell, ipcMain, safeStorage, dialog, Notification, powerMonitor, Menu, Tray, nativeImage } = require('electron'); const fsSync = require('node:fs'); const path = require('node:path'); @@ -82,10 +91,18 @@ let windowsAppIcon = null; let tray = null; function registerIpcHandler(channel, handler) { - return ipcMainHandle(channel, (event, ...args) => { - assertTrustedIpcSender(event, applicationHtmlPath); - validateIpcArguments(channel, args); - return handler(event, ...args); + return ipcMainHandle(channel, async (event, ...args) => { + const startedAt = startupTraceEnabled ? process.hrtime.bigint() : null; + try { + assertTrustedIpcSender(event, applicationHtmlPath); + validateIpcArguments(channel, args); + return await handler(event, ...args); + } finally { + if (startedAt) { + const elapsed = Number(process.hrtime.bigint() - startedAt) / 1e6; + traceStartup(`${channel} IPC completed in ${elapsed.toFixed(1)}ms`); + } + } }); } @@ -502,8 +519,10 @@ registerDavIpcHandlers({ randomUUID, assertDavObjectReference }); +traceStartup('main services and IPC handlers initialized'); function createWindow() { + traceStartup('creating BrowserWindow'); const window = new BrowserWindow({ width: 1440, height: 920, @@ -522,9 +541,17 @@ function createWindow() { } }); - window.once('ready-to-show', () => window.show()); + window.webContents.on('dom-ready', () => traceStartup('renderer DOM ready')); + window.webContents.on('did-finish-load', () => traceStartup('renderer load complete')); + window.once('ready-to-show', () => { + traceStartup('BrowserWindow ready-to-show'); + window.show(); + }); - window.loadFile(path.join(__dirname, '..', 'index.html')); + window.loadFile(path.join(__dirname, '..', 'index.html')).catch(error => { + traceStartup(`loadFile failed: ${error.message}`); + console.error('Could not load the application window:', error); + }); window.webContents.setWindowOpenHandler(() => { return { action: 'deny' }; }); @@ -596,22 +623,29 @@ function repairWindowsStartMenuShortcut() { } app.whenReady().then(() => { + traceStartup('Electron app.whenReady'); app.setAppUserModelId(windowsAppUserModelId); if (process.platform === 'win32') { windowsAppIcon = nativeImage.createFromPath(path.join(__dirname, '..', 'icon.ico')); if (windowsAppIcon.isEmpty()) throw new Error('Could not load the Windows application icon.'); - repairWindowsStartMenuShortcut(); - createWindowsTray(); } - createWindow(); + const mainWindow = createWindow(); + mainWindow.once('ready-to-show', () => { + if (process.platform === 'win32') { + repairWindowsStartMenuShortcut(); + createWindowsTray(); + } + void checkSnoozeReminders(); + const startupSyncTimer = setTimeout(() => requestBackgroundSync(), 3000); + startupSyncTimer.unref(); + traceStartup('post-first-frame work started; background sync scheduled'); + }); const syncTimer = setInterval(() => { void runBackgroundSync(true).catch(error => console.error('Periodic mail sync failed:', error)); }, backgroundSyncIntervalMs); syncTimer.unref(); const snoozeTimer = setInterval(() => void checkSnoozeReminders(), snoozeCheckIntervalMs); snoozeTimer.unref(); - void checkSnoozeReminders(); - requestBackgroundSync(); powerMonitor.on('suspend', () => { idleWatcherManager.stopAll(); }); diff --git a/index.html b/index.html index fdc7c00..67c039b 100644 --- a/index.html +++ b/index.html @@ -67,7 +67,7 @@ - + diff --git a/package-lock.json b/package-lock.json index 00beb29..c7de132 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ep-mail", - "version": "0.4.39", + "version": "0.5.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "ep-mail", - "version": "0.4.39", + "version": "0.5.0", "dependencies": { "@capacitor/android": "^8.5.2", "@capacitor/core": "^8.5.2", diff --git a/package.json b/package.json index ca9e8d9..c5c9b2c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ep-mail", - "version": "0.4.39", + "version": "0.5.0", "private": true, "description": "Eternity Project mail client", "homepage": "https://git.eternityproject.fi/thuttu/ep-mail-client", @@ -40,6 +40,7 @@ "files": [ "index.html", "platform-bridge.js", + "platform-bridge-loader.js", "styles.css", "branding.css", "renderer-state.js", diff --git a/platform-bridge-loader.js b/platform-bridge-loader.js new file mode 100644 index 0000000..6868b75 --- /dev/null +++ b/platform-bridge-loader.js @@ -0,0 +1,5 @@ +(() => { + // Electron's preload already provides the desktop bridge; avoid parsing the Capacitor bundle there. + if (window.daybreakMail) return; + document.write(''); const mailApi = html.indexOf(''); const app = html.indexOf(''); - const platformBridge = html.indexOf(''); + const platformBridgeLoader = html.indexOf(''); const mailUi = html.indexOf(''); const productivity = html.indexOf(''); const davUi = html.indexOf(''); const accountUi = html.indexOf(''); - assert.ok(state >= 0 && contentSecurity > state && app > contentSecurity && mailApi > platformBridge && mailUi > mailApi); + assert.ok(state >= 0 && contentSecurity > state && app > contentSecurity && platformBridgeLoader > app && mailApi > platformBridgeLoader && mailUi > mailApi); assert.ok(productivity > mailUi && davUi > productivity && accountUi > davUi); }); +test('desktop skips the generated Capacitor bridge while web/Android bootstrap loads it synchronously', () => { + const source = read('platform-bridge-loader.js'); + let desktopWrite = false; + vm.runInNewContext(source, { window: { daybreakMail: {} }, document: { write() { desktopWrite = true; } } }); + assert.equal(desktopWrite, false); + + let written = ''; + vm.runInNewContext(source, { window: {}, document: { write(value) { written = value; } } }); + assert.equal(written, ''); +}); + test('core app shell uses the namespaced renderer state', () => { const app = read('app.js'); assert.match(app, /^\(\(\) => \{/); @@ -44,7 +55,7 @@ test('desktop, Android web generation, and offline cache include renderer suppor const buildFiles = require(path.join(root, 'package.json')).build.files; const androidAssets = read('scripts/prepare-android-web.cjs'); const worker = read('service-worker.js'); - for (const file of ['renderer-state.js', 'mail-content-security.js', 'renderer-mail-api.js']) { + for (const file of ['renderer-state.js', 'mail-content-security.js', 'renderer-mail-api.js', 'platform-bridge-loader.js']) { assert.ok(buildFiles.includes(file), `Electron package is missing ${file}`); assert.ok(androidAssets.includes(`'${file}'`), `Android web generator is missing ${file}`); assert.ok(worker.includes(`'./${file}'`), `service worker is missing ${file}`); @@ -54,6 +65,7 @@ test('desktop, Android web generation, and offline cache include renderer suppor test('classic renderer scripts have no duplicate global lexical declarations', () => { const html = read('index.html'); const scripts = [...html.matchAll(/ - + diff --git a/www/platform-bridge-loader.js b/www/platform-bridge-loader.js new file mode 100644 index 0000000..6868b75 --- /dev/null +++ b/www/platform-bridge-loader.js @@ -0,0 +1,5 @@ +(() => { + // Electron's preload already provides the desktop bridge; avoid parsing the Capacitor bundle there. + if (window.daybreakMail) return; + document.write('