Skip to content

The agent that grows with you

fix(webapp): take the blocked-popup toast down when a slow pop-out opens after all

unsupportedpastels9bfe07398563
diff --git a/apps/desktop/src/lib/browser-window.test.ts b/apps/desktop/src/lib/browser-window.test.ts
index 51c6d637536..a42b2421a25 100644
--- a/apps/desktop/src/lib/browser-window.test.ts
+++ b/apps/desktop/src/lib/browser-window.test.ts
@@ -1,9 +1,11 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
+import { $notifications } from '@/store/notifications'
+import { openNewWindow } from '@/store/windows'
import { browserOpensWebappWindows, stubBroadcastChannels } from '@/test/webapp-windows'
import { installBrowserDesktopBridge } from './browser-desktop-bridge'
-import { createBrowserWindowOpener } from './browser-window'
+import { announceWebappWindow, createBrowserWindowOpener } from './browser-window'
const POPUP_BLOCKED = 'Webapp window did not open. Allow popups for this site and try again.'
const bootstrap = window as Window & { __HERMES_AUTH_REQUIRED__?: boolean }
@@ -16,6 +18,7 @@ afterEach(() => {
window.history.replaceState(null, '', '/')
vi.unstubAllGlobals()
vi.restoreAllMocks()
+ $notifications.set([])
})
function opener(privateSession: boolean, api = vi.fn()) {
@@ -54,8 +57,55 @@ function handoffPage(href: string): unknown[] {
return received
}
+/** The toasts showing, by message. */
+function toasts(): string[] {
+ return $notifications.get().map(({ message }) => message)
+}
+
+/** The window opens that address late and boots the Webapp, which says its hello. */
+function bootLate(href: string) {
+ const opener = window.location.href
+ window.history.replaceState(null, '', href)
+ announceWebappWindow()
+ window.history.replaceState(null, '', opener)
+}
+
+// A window that boots after the deadline (a slow machine, or a gated window landing on the login
+// page or a 503) opened after all: its late `ready` takes the "Allow popups" toast back down.
+describe('a window that says ready after the deadline', () => {
+ it.each([
+ ['gated host', false],
+ ['launch-link', true]
+ ])('takes the blocked-popup toast down on a %s pop-out', async (_name, privateSession) => {
+ vi.useFakeTimers()
+ const ticket = 'd'.repeat(43)
+ const channels = stubBroadcastChannels()
+ const open = vi.spyOn(window, 'open').mockReturnValue(null)
+ const api = vi.fn().mockResolvedValue({ ticket })
+
+ ;(window as unknown as { hermesDesktop: object }).hermesDesktop = {
+ openWindow: () => opener(privateSession, api)(target())
+ }
+
+ const opening = openNewWindow()
+ await vi.advanceTimersByTimeAsync(3_000)
+ await opening
+ expect(toasts()).toEqual([POPUP_BLOCKED])
+
+ await vi.advanceTimersByTimeAsync(1_000)
+ const href = String(open.mock.calls[0]?.[0])
+ const received = privateSession ? handoffPage(href) : (bootLate(href), [])
+ await vi.advanceTimersByTimeAsync(0)
+
+ expect(toasts()).toEqual([])
+ expect(received).toEqual(privateSession ? [{ ticket }] : [])
+ expect(api).toHaveBeenCalledTimes(privateSession ? 1 : 0)
+ expect(channels.size).toBe(0)
+ })
+})
+
describe('gated host pop-out', () => {
- it('reports a window the browser blocked within seconds', async () => {
+ it('reports a window the browser blocked within seconds and stops listening for a late one', async () => {
vi.useFakeTimers()
const { channels, open } = browserOpensWebappWindows({ blocked: true })
const result = outcome(opener(false)(target()))
@@ -65,6 +115,9 @@ describe('gated host pop-out', () => {
await vi.advanceTimersByTimeAsync(5_000)
expect(result()).toBe(POPUP_BLOCKED)
expect(open.mock.calls[0]?.[2]).toBe('noopener,noreferrer')
+ // Still listening: a late window could yet say ready.
+ expect(channels.size).toBe(1)
+ await vi.advanceTimersByTimeAsync(25_000)
expect(channels.size).toBe(0)
})
diff --git a/apps/desktop/src/lib/browser-window.ts b/apps/desktop/src/lib/browser-window.ts
index 10348463cfc..597283d192f 100644
--- a/apps/desktop/src/lib/browser-window.ts
+++ b/apps/desktop/src/lib/browser-window.ts
@@ -1,6 +1,7 @@
import { randomUuid } from '@hermes/shared'
import type { HermesApiRequest } from '@/global'
+import { dismissNotification } from '@/store/notifications'
interface BrowserWindowOptions {
api: <T>(request: HermesApiRequest) => Promise<T>
@@ -10,6 +11,16 @@ interface BrowserWindowOptions {
const POPUP_BLOCKED = 'Webapp window did not open. Allow popups for this site and try again.'
+/** The blocked-window rejection. Its toast goes up under `notificationId` (runWindowOpen), so a
+ * window that says `ready` after the deadline can take it back down. */
+class WindowBlockedError extends Error {
+ readonly notificationId = `webapp-window-blocked:${randomUuid()}`
+
+ constructor() {
+ super(POPUP_BLOCKED)
+ }
+}
+
// The new window gets its own browsing context group: it does not share this tab's event loop,
// starts with empty sessionStorage and gets no Referer. The cost: window.open returns null whether
// or not the window opened (HTML "window open steps"), so each window says `ready` on a
@@ -19,9 +30,13 @@ const WINDOW_FEATURES = 'noopener,noreferrer'
// A window that has not said `ready` by then is reported as blocked. In Chrome, Edge and WebKit a
// window that opened said it within 0.4 s (the launch-link handoff page) and about 1.3 s (the
// whole app on a gated host), even with the browser held to one CPU. A slower window still opens
-// after the toast, and on a launch-link tab it still signs in.
+// and takes the toast back down, and on a launch-link tab it still signs in.
const READY_DEADLINE_MS = 3_000
+// How long a pop-out keeps listening past the deadline: for a window that starts late, or one the
+// user opens from the browser's blocked-popup prompt.
+const LATE_WINDOW_MS = 30_000
+
// A gated host's window is the app itself, which says `ready` at boot on the channel this
// parameter names (`announceWebappWindow`).
const HELLO_PARAM = 'hello'
@@ -47,23 +62,32 @@ function openGatedWindow(target: URL): Promise<{ ok: true }> {
href.searchParams.set(HELLO_PARAM, id)
return new Promise((resolve, reject) => {
- const finish = (error?: Error) => {
+ const blocked = new WindowBlockedError()
+
+ const close = () => {
window.clearTimeout(deadline)
+ window.clearTimeout(expiry)
channel.close()
-
- if (error) {reject(error)} else {resolve({ ok: true })}
}
- const deadline = window.setTimeout(() => finish(new Error(POPUP_BLOCKED)), READY_DEADLINE_MS)
+ const deadline = window.setTimeout(() => reject(blocked), READY_DEADLINE_MS)
+ const expiry = window.setTimeout(close, LATE_WINDOW_MS)
+ // Before the deadline this resolves; past it the promise has rejected, and the window that
+ // opened after all takes its toast down.
channel.onmessage = event => {
- if (event.data?.type === 'ready') {finish()}
+ if (event.data?.type === 'ready') {
+ close()
+ resolve({ ok: true })
+ dismissNotification(blocked.notificationId)
+ }
}
try {
window.open(href.href, '_blank', WINDOW_FEATURES)
} catch (error) {
- finish(asError(error))
+ close()
+ reject(asError(error))
}
})
}
@@ -85,6 +109,7 @@ function openLaunchLinkWindow(
launch.hash = new URLSearchParams({ channel: id, query: target.search, route: target.hash.slice(1) }).toString()
return new Promise((resolve, reject) => {
+ const blocked = new WindowBlockedError()
let minting = false
let closed = false
@@ -102,8 +127,8 @@ function openLaunchLinkWindow(
settle(error)
}
- const deadline = window.setTimeout(() => settle(new Error(POPUP_BLOCKED)), READY_DEADLINE_MS)
- const expiry = window.setTimeout(() => close(new Error(POPUP_BLOCKED)), 30_000)
+ const deadline = window.setTimeout(() => settle(blocked), READY_DEADLINE_MS)
+ const expiry = window.setTimeout(() => close(blocked), LATE_WINDOW_MS)
channel.onmessage = event => {
if (closed) {return}
@@ -111,6 +136,7 @@ function openLaunchLinkWindow(
if (event.data?.type === 'ready' && !minting) {
minting = true
window.clearTimeout(deadline)
+ dismissNotification(blocked.notificationId)
void api<{ ticket: string }>({ method: 'POST', path: '/api/webapp/window-ticket' }).then(({ ticket }) => {
if (closed) {return}
channel.postMessage({ ticket })
diff --git a/apps/desktop/src/store/windows.ts b/apps/desktop/src/store/windows.ts
index c81bd6730cc..d971c9c3c69 100644
--- a/apps/desktop/src/store/windows.ts
+++ b/apps/desktop/src/store/windows.ts
@@ -225,7 +225,10 @@ async function runWindowOpen(call: () => Promise<WindowOpenResult>, failMessage:
return true
} catch (err) {
- notifyError(err, failMessage)
+ // A failure that may yet be undone names its toast so it can take it down (the Webapp's
+ // blocked pop-out, whose window can still open late).
+ const id = (err as { notificationId?: unknown } | null)?.notificationId
+ notifyError(err, failMessage, typeof id === 'string' ? { id } : {})
return false
}