import { setupMessageHandler, setRestoreReady } from "./messages"; import { setupAutoLock } from "./alarms"; import { setupPasskeyProxy } from "./passkey"; import { setupCommands } from "./commands"; import { setupClipboardClearAlarm } from "./clipboard"; 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 function start(): void { // MV3: the chrome.runtime.onMessage listener MUST be registered during the // worker's initial SYNCHRONOUS evaluation. If registration is deferred behind // an await, a popup/content message that wakes a cold worker is dispatched // before the listener exists → "receiving end does not exist", and the popup // misreads the failed HAS_VAULT as "no vault" and shows onboarding. // // So: kick off session-restore, hand its promise to the message layer as a // gate, and register the handler synchronously. Unlock-dependent handlers // await the gate (see handleMessage), which preserves the SEV-3-B fix — no // spurious lock screen — without delaying listener registration. const restored = restoreFromSession(); setRestoreReady(restored); setupMessageHandler(); void setupAutoLock(); setupCommands(); // HV-2: clipboard auto-clear backstop must survive worker eviction; the // listener has to be registered during initial synchronous evaluation so // a cold worker woken BY the alarm still handles it. setupClipboardClearAlarm(); 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 restored.then(() => hydratePersistedSyncState()).catch(() => undefined); } start();