16db45928fa3

register __peachRegisterMessageHandler in setupMessageHandler so chrome shim can route sendMessage on Android; implement publishInMemoryVault and clearInMemoryVault via Capacitor.Plugins.InMemoryVaultPlugin; call from UNLOCK and LOCK

time
2026-05-07 02:18 UTC · 5 months ago
machine
koh2
parent
ce27a78ad3c7
1 file src/background/messages.ts+43 −1
src/background/messages.ts+43 −1
@@ -165,6 +165,24 @@165165       return true;166166     }167167   );168+169+  // Android: the chrome shim queues sendMessage calls until this handler is170+  // registered. Without this hookup, every message hangs on Capacitor builds.171+  // eslint-disable-next-line @typescript-eslint/no-explicit-any172+  const reg = (globalThis as any).__peachRegisterMessageHandler;173+  if (typeof reg === "function") {174+    reg(async (m: unknown) => {175+      if (!isMessage(m)) return { ok: false, error: "invalid message" };176+      try {177+        return await handleMessage(m);178+      } catch (err) {179+        return {180+          ok: false,181+          error: err instanceof Error ? err.message : String(err),182+        };183+      }184+    });185+  }168186 }169187 170188 function isMessage(m: unknown): m is Message {@@ -205,6 +223,7 @@205223         if (ok) {206224           await setupAutoLock();207225           broadcastToContentScripts({ type: "VAULT_UNLOCKED" });226+          if (isNativePlatform()) await publishInMemoryVault(vault.getEntries());208227         }209228         return { ok };210229       }@@ -262,6 +281,7 @@262281       }263282       case "LOCK":264283         vault.lock();284+        if (isNativePlatform()) await clearInMemoryVault();265285         return { ok: true };266286       case "IS_UNLOCKED":267287         return { ok: true, data: vault.isUnlocked() };@@ -1063,7 +1083,29 @@10631083 10641084 // Android: populates the in-memory vault used by PeachAutofillService.10651085 async function publishInMemoryVault(entries: VaultEntry[]): Promise<void> {1066-  // TODO: wire up to autofill in-memory store (exists in Android, not here)1086+  // eslint-disable-next-line @typescript-eslint/no-explicit-any1087+  const plugin = (globalThis as any).Capacitor?.Plugins?.InMemoryVaultPlugin;1088+  if (!plugin?.setEntries) return;1089+  await plugin.setEntries({1090+    entries: entries.map((e) => ({1091+      id: e.id,1092+      title: e.title ?? "",1093+      username: e.username ?? "",1094+      email: (e as VaultEntry & { email?: string }).email ?? "",1095+      password: e.password ?? "",1096+      url: e.url ?? "",1097+      totp: (e as VaultEntry & { totp?: string }).totp ?? "",1098+      additionalUrls: (e as VaultEntry & { additionalUrls?: string[] }).additionalUrls ?? [],1099+    })),1100+  });1101+}1102+1103+// Android: clear the autofill in-memory store on lock.1104+async function clearInMemoryVault(): Promise<void> {1105+  // eslint-disable-next-line @typescript-eslint/no-explicit-any1106+  const plugin = (globalThis as any).Capacitor?.Plugins?.InMemoryVaultPlugin;1107+  if (!plugin?.clearEntries) return;1108+  await plugin.clearEntries();10671109 }10681110 10691111 // Returns all emails and usernames from the vault, deduped and sorted by frequency.

view file