diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /testing/web-platform/tests/storage-access-api/resources/embedded_responder.js | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/storage-access-api/resources/embedded_responder.js')
-rw-r--r-- | testing/web-platform/tests/storage-access-api/resources/embedded_responder.js | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/testing/web-platform/tests/storage-access-api/resources/embedded_responder.js b/testing/web-platform/tests/storage-access-api/resources/embedded_responder.js new file mode 100644 index 0000000000..bc13c7e7e8 --- /dev/null +++ b/testing/web-platform/tests/storage-access-api/resources/embedded_responder.js @@ -0,0 +1,97 @@ +"use strict"; + +test_driver.set_test_context(window.top); + +let worker; + +function waitForWorkerMessage(worker) { + return new Promise(resolve => { + const listener = (event) => { + worker.removeEventListener("message", listener); + resolve(event.data); + }; + worker.addEventListener("message", listener); + }); +} + +function connectAndGetRequestCookiesFrom(origin) { + return new Promise((resolve, reject) => { + const ws = new WebSocket(origin +'/echo-cookie'); + ws.onmessage = event => { + const cookies = event.data; + resolve(cookies); + ws.onerror = undefined; + ws.onclose = undefined; + }; + ws.onerror = () => reject(new Error('Unexpected error event')); + ws.onclose = evt => reject('Unexpected close event: ' + JSON.stringify(evt)); + }); +} + +window.addEventListener("message", async (event) => { + function reply(data) { + event.source.postMessage( + {timestamp: event.data.timestamp, data}, event.origin); + } + + switch (event.data["command"]) { + case "hasStorageAccess": + reply(await document.hasStorageAccess()); + break; + case "requestStorageAccess": { + const obtainedAccess = await document.requestStorageAccess() + .then(() => true, () => false); + reply(obtainedAccess); + } + break; + case "write document.cookie": + document.cookie = event.data.cookie; + reply(undefined); + break; + case "document.cookie": + reply(document.cookie); + break; + case "set_permission": + await test_driver.set_permission(...event.data.args); + reply(undefined); + break; + case "observe_permission_change": + const status = await navigator.permissions.query({name: "storage-access"}); + status.addEventListener("change", (event) => { + reply(event.target.state) + }, { once: true }); + break; + case "reload": + window.location.reload(); + break; + case "navigate": + window.location.href = event.data.url; + break; + case "httpCookies": + // The `httpCookies` variable is defined/set by + // script-with-cookie-header.py. + reply(httpCookies); + break; + case "cors fetch": + reply(await fetch(event.data.url, {mode: 'cors', credentials: 'include'}).then((resp) => resp.text())); + break; + case "no-cors fetch": + reply(await fetch(event.data.url, {mode: 'no-cors', credentials: 'include'}).then((resp) => resp.text())); + break; + case "start_dedicated_worker": + worker = new Worker("embedded_worker.js"); + reply(undefined); + break; + case "message_worker": { + const p = waitForWorkerMessage(worker); + worker.postMessage(event.data.message); + reply(await p.then(resp => resp.data)) + break; + } + case "get_cookie_via_websocket":{ + reply(await connectAndGetRequestCookiesFrom(event.data.origin)); + break; + } + default: + } +}); |