entry.ts
1import { setupMessageHandler, setRestoreReady } from "./messages";2import { setupAutoLock } from "./alarms";3import { setupSyncAlarm } from "./sync-alarm";4import { setupPasskeyProxy } from "./passkey";5import { setupCommands } from "./commands";6import { setupClipboardClearAlarm } from "./clipboard";7import { restoreFromSession, isUnlocked, lock } from "./vault";8import { ensureSettingsLoaded } from "./vault";9import { isRecoveryConfirmPending } from "./recovery-pending";10import { denyAllPendingPasskeyRequests } from "./passkey";11import {12 hydratePersistedSyncState,13 setSyncDialTargetSelector,14 setSyncRunner,15 triggerSyncIfReachable,16} from "./sync-trigger";17import { productionSyncRunner } from "./sync-runner";18import {19 drainAndRefreshLockedSyncSpool,20 refreshLockedSyncSpool,21} from "./sync-vault-adapter";22import { setLockedSyncHooks } from "./vault";23import { repairPairedRevocationOverlaps } from "./paired-devices";24import {25 requestMeshRelayReconcile,26 selectProductionDialTargets,27 setupMeshRelayListener,28} from "./mesh-relay-listener";2930// Keep alive for MV3 service worker31chrome.runtime.onInstalled.addListener(() => {});3233// The chrome.idle.onStateChanged listener lives in ./alarms (the architectural34// home for auto-lock timing). Keeping it in only one place avoids drift.3536// Lock-on-close via port disconnect. The popup opens a nonce'd port37// ("popup-<uuid>"); when that port disconnects (popup closed), lock if38// lockOnClose is enabled. Previously also declared in the never-bundled39// index.ts (deleted in LV-5) — this is now the single registration.40//41// LV-5: the lock port name carries a per-popup-open random suffix. Another42// extension cannot open ports INTO this extension (onConnectExternal is not43// registered), so the nonce is defense-in-depth — a port named exactly44// "popup" from anything that isn't our popup no longer reliably triggers45// deny-all + lock once the legacy window closes.46const POPUP_PORT_PREFIX = "popup-";4748chrome.runtime.onConnect.addListener((port) => {49 // Accept legacy bare "popup" for one release so an in-flight popup from the50 // previous version straddling the update doesn't lose lock-on-close.51 // TODO(2026-Q3): drop the bare "popup" acceptance once v3.8.0 has rolled out.52 if (port.name !== "popup" && !port.name.startsWith(POPUP_PORT_PREFIX)) return;53 port.onDisconnect.addListener(async () => {54 await denyAllPendingPasskeyRequests();55 const s = await ensureSettingsLoaded();56 if (!isUnlocked() || s.lockOnClose !== true) return;57 if (await isRecoveryConfirmPending()) return;58 lock();59 });60});6162export function start(): void {63 setLockedSyncHooks({64 refresh: refreshLockedSyncSpool,65 afterUnlock: drainAndRefreshLockedSyncSpool,66 });67 // MV3: the chrome.runtime.onMessage listener MUST be registered during the68 // worker's initial SYNCHRONOUS evaluation. If registration is deferred behind69 // an await, a popup/content message that wakes a cold worker is dispatched70 // before the listener exists → "receiving end does not exist", and the popup71 // misreads the failed HAS_VAULT as "no vault" and shows onboarding.72 //73 // So: kick off session-restore, hand its promise to the message layer as a74 // gate, and register the handler synchronously. Unlock-dependent handlers75 // await the gate (see handleMessage), which preserves the SEV-3-B fix — no76 // spurious lock screen — without delaying listener registration.77 const restored = restoreFromSession();78 setRestoreReady(restored);79 setupMessageHandler();80 void setupAutoLock();81 setupCommands();82 // HV-2: clipboard auto-clear backstop must survive worker eviction; the83 // listener has to be registered during initial synchronous evaluation so84 // a cold worker woken BY the alarm still handles it.85 setupClipboardClearAlarm();86 void setupPasskeyProxy();87 // Wire the sync production runner into the dispatcher. Without this88 // `triggerSyncIfReachable` is a no-op (the runner slot starts null) — the89 // pairing/transport/coordinator stack ships dormant until this assignment.90 setSyncRunner(productionSyncRunner);91 setSyncDialTargetSelector(selectProductionDialTargets);92 setupMeshRelayListener();93 // 10-minute background sync. Must be registered during initial synchronous94 // evaluation for the same reason as the clipboard alarm: a cold worker95 // woken BY the sync alarm needs the listener already attached. Setup is96 // idempotent — calling it after a SW revival is safe.97 setupSyncAlarm();98 // First dial without waiting for the alarm tick. The runner is wired and99 // the vault is unlocked enough that listPairedDevices works — the in-flight100 // guard inside triggerSyncIfReachable handles the case where another path101 // (e.g. a fresh pairing completing on the same wake) is already running.102 void restored103 .then(() => requestMeshRelayReconcile())104 .then(() => repairPairedRevocationOverlaps())105 .then(() => hydratePersistedSyncState())106 .then(() => triggerSyncIfReachable({ reason: "startup" }))107 .catch(() => undefined);108}109110start();