import { setupMessageHandler } from "./messages"; import { setupAutoLock } from "./alarms"; import { setupPasskeyProxy } from "./passkey"; import { setupCommands } from "./commands"; import { restoreFromSession, isUnlocked, lock } from "./vault"; import { ensureSettingsLoaded } from "./vault"; import { isRecoveryConfirmPending } from "./recovery-pending"; import { denyAllPendingPasskeyRequests } from "./passkey"; import { hydratePersistedSyncState, setSyncRunner } from "./sync-trigger"; import { productionSyncRunner } from "./chirp-sync-runner"; // Keep alive for MV3 service worker chrome.runtime.onInstalled.addListener(() => {}); // The chrome.idle.onStateChanged listener lives in ./alarms (the architectural // home for auto-lock timing). Keeping it in only one place avoids drift. // Lock-on-close via port disconnect. The popup opens a "popup" named port; // when that port disconnects (popup closed), lock if lockOnClose is enabled. // Previously declared in index.ts which was never bundled — moved here. chrome.runtime.onConnect.addListener((port) => { if (port.name !== "popup") return; port.onDisconnect.addListener(async () => { await denyAllPendingPasskeyRequests(); const s = await ensureSettingsLoaded(); if (!isUnlocked() || s.lockOnClose === false) return; if (await isRecoveryConfirmPending()) return; lock(); }); }); export async function start(): Promise { // Restore vault unlock state from chrome.storage.session if the MV3 service // worker was restarted mid-session. This MUST complete before the message // handler is registered: a popup/content message that arrives in the gap // between handler registration and restore completing would see a locked // vault and bounce the user to the lock screen even though the session is // still unlocked (SEV-3-B). The cost is one session-storage read (<1ms). await restoreFromSession(); setupMessageHandler(); void setupAutoLock(); setupCommands(); void setupPasskeyProxy(); // Wire the chirp-sync production runner into the dispatcher. Without this // `triggerSyncIfReachable` is a no-op (the runner slot starts null) — the // pairing/transport/coordinator stack ships dormant until this assignment. setSyncRunner(productionSyncRunner); void hydratePersistedSyncState(); } void start();