forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1859334 [wpt PR 42561] - Clean up partitioned service worker/cook…
…ies interaction WPT, a=testonly Automatic update from web-platform-tests Clean up partitioned service worker/cookies interaction WPT Bug: None Change-Id: I6ce33cd6685f00c150158e92a1fb2a231680adb5 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4935871 Reviewed-by: Shuran Huang <[email protected]> Commit-Queue: Dylan Cutler <[email protected]> Cr-Commit-Position: refs/heads/main@{#1210431} -- wpt-commits: c7224d2db54f0ab56b6da2e9a2f5f6fced39547f wpt-pr: 42561
- Loading branch information
1 parent
b58fc37
commit 23027a9
Showing
4 changed files
with
94 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 3 additions & 53 deletions
56
.../web-platform/tests/service-workers/service-worker/resources/partitioned-cookies-3p-sw.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,3 @@ | ||
self.addEventListener('message', ev => ev.waitUntil(onMessage(ev))); | ||
|
||
async function onMessage(event) { | ||
if (!event.data) | ||
return; | ||
switch (event.data.type) { | ||
case 'test_message': | ||
return onTestMessage(event); | ||
case 'echo_cookies_http': | ||
return onEchoCookiesHttp(event); | ||
case 'echo_cookies_js': | ||
return onEchoCookiesJs(event); | ||
case 'echo_cookies_import': | ||
return onEchoCookiesImport(event); | ||
default: | ||
return; | ||
} | ||
} | ||
|
||
// test_message just verifies that the message passing is working. | ||
async function onTestMessage(event) { | ||
event.source.postMessage({ok: true}); | ||
} | ||
|
||
async function onEchoCookiesHttp(event) { | ||
try { | ||
const resp = await fetch( | ||
`${self.origin}/cookies/resources/list.py`, {credentials: 'include'}); | ||
const cookies = await resp.json(); | ||
event.source.postMessage({ok: true, cookies: Object.keys(cookies)}); | ||
} catch (err) { | ||
event.source.postMessage({ok: false}); | ||
} | ||
} | ||
|
||
// echo_cookies returns the names of all of the cookies available to the worker. | ||
async function onEchoCookiesJs(event) { | ||
try { | ||
const cookie_objects = await self.cookieStore.getAll(); | ||
const cookies = cookie_objects.map(c => c.name); | ||
event.source.postMessage({ok: true, cookies}); | ||
} catch (err) { | ||
event.source.postMessage({ok: false}); | ||
} | ||
} | ||
|
||
// Sets `self._cookies` variable, array of the names of cookies available to | ||
// the request. | ||
importScripts(`${self.origin}/cookies/resources/list-cookies-for-script.py`); | ||
|
||
function onEchoCookiesImport(event) { | ||
event.source.postMessage({ok: true, cookies: self._cookies}); | ||
} | ||
// Import the code from the 1P partitioned worker. | ||
// We use separate URLs for each worker script to keep their scopes unique. | ||
importScripts('./partitioned-cookies-sw.js'); |
31 changes: 31 additions & 0 deletions
31
...atform/tests/service-workers/service-worker/resources/partitioned-cookies-test-helpers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Return a generator containing the worker's message. | ||
// | ||
// Usage: | ||
// await navigator.serviceWorker.register(...) | ||
// ... | ||
// const nextMessage = worker_message_generator(); | ||
// const msg_1 = await nextMessage(); | ||
// const msg_2 = await nextMessage(); | ||
// const msg_3 = await nextMessage(); | ||
// | ||
// Worker should have its own onmessage event listener that | ||
// postMessages replies to the DOM. | ||
function worker_message_generator() { | ||
const buffer = []; | ||
let resolve = null; | ||
|
||
navigator.serviceWorker.addEventListener('message', message => { | ||
buffer.push(message.data); | ||
if (resolve) { | ||
resolve(); | ||
} | ||
}); | ||
|
||
return async function () { | ||
if (buffer.length != 0) { | ||
return buffer.shift(); | ||
} | ||
await new Promise(r => resolve = r); | ||
return buffer.shift(); | ||
} | ||
} |