Keep the screen on and ask before leaving while a session is busy
While a share waits or a transfer runs, the page holds a Screen Wake Lock, requested again once the page is visible, since the browser releases it whenever it's hidden. A beforeunload handler makes the browser ask before a reload or a closed tab ends the session, only while busy so the page stays eligible for the back/forward cache otherwise. Covered by awake: with a fake wake lock, a headless browser may not grant a real one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
870d55b44f
commit
44a8a72261
3 files changed
+128
-6
No files matched your search
+73
@@ -257,6 +257,50 @@ function holdReads() {
|
||||
const received = (page: Page) => page.evaluate(() => (window as unknown as { received: number }).received);
|
||||
const release = (page: Page) => page.evaluate(() => (window as unknown as { release: () => void }).release());
|
||||
|
||||
type FakeWakeLock = EventTarget & { released: boolean; release: () => Promise<void> };
|
||||
|
||||
// Init script for a page whose wake lock the test follows, a headless browser may not grant a real
|
||||
// one. Like a browser, it refuses one to a hidden page and releases each once the page is hidden,
|
||||
// which setHidden makes it. It runs in the hidden frames too, only the app's own window counts.
|
||||
function fakeWakeLock() {
|
||||
if (window !== window.top) return;
|
||||
const page = window as unknown as { wakeLocks: FakeWakeLock[]; setHidden: (hidden: boolean) => void };
|
||||
page.wakeLocks = [];
|
||||
let hidden = false;
|
||||
Object.defineProperty(document, 'hidden', { get: () => hidden });
|
||||
const request = async () => {
|
||||
if (hidden) throw new DOMException('page is hidden', 'NotAllowedError');
|
||||
const sentinel: FakeWakeLock = Object.assign(new EventTarget(), {
|
||||
released: false,
|
||||
release: async () => {
|
||||
if (sentinel.released) return;
|
||||
sentinel.released = true;
|
||||
sentinel.dispatchEvent(new Event('release'));
|
||||
},
|
||||
});
|
||||
page.wakeLocks.push(sentinel);
|
||||
return sentinel;
|
||||
};
|
||||
Object.defineProperty(navigator, 'wakeLock', { value: { request } });
|
||||
page.setHidden = (value) => {
|
||||
hidden = value;
|
||||
if (hidden) for (const sentinel of page.wakeLocks) void sentinel.release();
|
||||
document.dispatchEvent(new Event('visibilitychange'));
|
||||
};
|
||||
}
|
||||
|
||||
const setHidden = (page: Page, hidden: boolean) =>
|
||||
page.evaluate((hidden) => (window as unknown as { setHidden: (hidden: boolean) => void }).setHidden(hidden), hidden);
|
||||
|
||||
// The wake locks the page holds, and whether it would ask before a reload or a closed tab.
|
||||
const awake = (page: Page) =>
|
||||
page.evaluate(() => {
|
||||
const leave = new Event('beforeunload', { cancelable: true });
|
||||
dispatchEvent(leave);
|
||||
const { wakeLocks } = window as unknown as { wakeLocks: FakeWakeLock[] };
|
||||
return { locks: wakeLocks.filter((sentinel) => !sentinel.released).length, asks: leave.defaultPrevented };
|
||||
});
|
||||
|
||||
// Changes sw.js and has the page's browser install it, as after a deploy while the tab is open.
|
||||
async function updateWorker(page: Page) {
|
||||
await expect
|
||||
@@ -764,6 +808,35 @@ for (const name of Object.keys(BROWSERS)) {
|
||||
});
|
||||
});
|
||||
|
||||
// While a session runs, the screen stays on and the browser asks before a reload or a closed tab
|
||||
// ends it. Hidden, the page loses the wake lock and gets a new one once visible again.
|
||||
test(`awake: ${name} keeps the screen on and asks before leaving while busy`, async () => {
|
||||
const [sender, receiver] = await Promise.all([
|
||||
open(name, { init: fakeWakeLock }),
|
||||
open(name, { init: fakeWakeLock }),
|
||||
]);
|
||||
const source = await payload(randomBytes(100_000));
|
||||
|
||||
await withLogs(sender, receiver, async () => {
|
||||
for (const page of [sender, receiver]) expect(await awake(page)).toEqual({ locks: 0, asks: false });
|
||||
await connect('offline', sender, receiver, source);
|
||||
for (const page of [sender, receiver]) await expect.poll(() => awake(page)).toEqual({ locks: 1, asks: true });
|
||||
|
||||
await setHidden(sender, true);
|
||||
await expect.poll(() => awake(sender)).toEqual({ locks: 0, asks: true });
|
||||
await setHidden(sender, false);
|
||||
await expect.poll(() => awake(sender)).toEqual({ locks: 1, asks: true });
|
||||
await expect(sender.locator('#log')).toContainText(/keeping the screen on[\s\S]*keeping the screen on/);
|
||||
|
||||
await sender.click('#confirm');
|
||||
await receiver.click('#confirm');
|
||||
await receiver.click('#download');
|
||||
await expect(sender.locator('[data-phase=done] #send-status')).toHaveText('Delivered');
|
||||
await expect(receiver.locator('[data-phase=done] #receive-status')).toHaveText('Download complete');
|
||||
for (const page of [sender, receiver]) await expect.poll(() => awake(page)).toEqual({ locks: 0, asks: false });
|
||||
});
|
||||
});
|
||||
|
||||
// The proofs hold even against a man in the middle that opens and seals the signaling. They are
|
||||
// genuine, made with the right key, only the real certificates' fingerprints they cover give the man
|
||||
// in the middle away. Whoever verifies first ends its connection, the other side may never get the
|
||||
|
||||
Reference in new issue
Block a user