Skip to content

Truly independent web browser

LibWeb: Hold worker rendering updates while the event loop is paused

Tim Ledbetter12b52bbf0235
diff --git a/Libraries/LibWeb/HTML/DedicatedWorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/DedicatedWorkerGlobalScope.cpp
index ebbc4c311d8..cb36f69f6e9 100644
--- a/Libraries/LibWeb/HTML/DedicatedWorkerGlobalScope.cpp
+++ b/Libraries/LibWeb/HTML/DedicatedWorkerGlobalScope.cpp
@@ -12,9 +12,11 @@
#include <LibWeb/HTML/AnimationFrameCallbackDriver.h>
#include <LibWeb/HTML/DedicatedWorkerGlobalScope.h>
#include <LibWeb/HTML/EventHandler.h>
+#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/MessageEvent.h>
#include <LibWeb/HTML/MessagePort.h>
+#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
#include <LibWeb/HTML/StructuredSerialize.h>
#include <LibWeb/Page/Page.h>
@@ -126,6 +128,10 @@ void DedicatedWorkerGlobalScope::schedule_rendering_update()
{
if (!m_rendering_update_timer) {
m_rendering_update_timer = Platform::Timer::create_single_shot(heap(), AK::Duration::from_milliseconds(0), GC::create_function(heap(), [this] {
+ if (relevant_settings_object(*this).responsible_event_loop().execution_paused()) {
+ schedule_rendering_update();
+ return;
+ }
run_rendering_update();
}));
}
diff --git a/Tests/LibWeb/Text/expected/Worker/Worker-synchronous-XHR-send.txt b/Tests/LibWeb/Text/expected/Worker/Worker-synchronous-XHR-send.txt
index a6d146e74c8..1967fc73ce6 100644
--- a/Tests/LibWeb/Text/expected/Worker/Worker-synchronous-XHR-send.txt
+++ b/Tests/LibWeb/Text/expected/Worker/Worker-synchronous-XHR-send.txt
@@ -2,3 +2,4 @@ before send
after send: status=200 responseText=hello over http
microtask ran
timer fired
+animation frame callback ran after send
diff --git a/Tests/LibWeb/Text/input/Worker/Worker-synchronous-XHR-send.html b/Tests/LibWeb/Text/input/Worker/Worker-synchronous-XHR-send.html
index 93b401a48f3..894fe2071bd 100644
--- a/Tests/LibWeb/Text/input/Worker/Worker-synchronous-XHR-send.html
+++ b/Tests/LibWeb/Text/input/Worker/Worker-synchronous-XHR-send.html
@@ -1,23 +1,33 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
- asyncTest(done => {
- httpTestServer()
- .createEcho("GET", "/worker-synchronous-xhr-send", {
- status: 200,
- headers: {
- "Access-Control-Allow-Origin": "*",
- "Content-Type": "text/plain",
- },
- body: "hello over http",
- })
- .then(url => {
- const worker = new Worker("worker-synchronous-XHR-send.js");
- worker.onmessage = event => {
- println(event.data);
- done();
- };
- worker.postMessage(url);
- });
+ promiseTest(async () => {
+ const server = httpTestServer();
+ const token = crypto.randomUUID();
+ const url = await server.createEcho("GET", "/worker-synchronous-xhr-send", {
+ status: 200,
+ headers: {
+ "Access-Control-Allow-Origin": "*",
+ "Content-Type": "text/plain",
+ },
+ body: "hello over http",
+ wait_for_unblock: token,
+ });
+
+ const frameWorkerSource = "onmessage = () => requestAnimationFrame(() => postMessage(null));";
+ const frameWorker = new Worker(URL.createObjectURL(new Blob([frameWorkerSource], { type: "text/javascript" })));
+ const worker = new Worker("worker-synchronous-XHR-send.js");
+ const log = new Promise(resolve => (worker.onmessage = event => resolve(event.data)));
+ worker.postMessage(url);
+
+ // A frame of another worker shows that the animation frame the blocked worker requested is due.
+ await server.waitForRequest(url);
+ await new Promise(resolve => {
+ frameWorker.onmessage = resolve;
+ frameWorker.postMessage(null);
+ });
+ await fetch(`${server.baseURL}/unblock/${token}`);
+
+ println(await log);
});
</script>
diff --git a/Tests/LibWeb/Text/input/Worker/worker-synchronous-XHR-send.js b/Tests/LibWeb/Text/input/Worker/worker-synchronous-XHR-send.js
index c474c814d92..1a0efbd2ff0 100644
--- a/Tests/LibWeb/Text/input/Worker/worker-synchronous-XHR-send.js
+++ b/Tests/LibWeb/Text/input/Worker/worker-synchronous-XHR-send.js
@@ -1,18 +1,25 @@
-// A sync XHR send() in a worker: the worker's event loop is paused while send() blocks, so the timer and the
-// microtask armed here must not run until send() has returned.
+// A sync XHR send() in a worker: the worker's event loop is paused while send() blocks, so the timer, the microtask
+// and the animation frame callback armed here must not run until send() has returned.
self.onmessage = event => {
const log = [];
+ let sendReturned = false;
+ let animationFrameCallback = "did not run";
setTimeout(() => log.push("timer fired"), 0);
Promise.resolve().then(() => log.push("microtask ran"));
+ requestAnimationFrame(() => (animationFrameCallback = sendReturned ? "ran after send" : "ran during send"));
const xhr = new XMLHttpRequest();
xhr.open("GET", event.data, false);
log.push("before send");
xhr.send();
+ sendReturned = true;
log.push(`after send: status=${xhr.status} responseText=${xhr.responseText}`);
- setTimeout(() => {
- postMessage(log.join("\n"));
- self.close();
- }, 0);
+ requestAnimationFrame(() => {
+ setTimeout(() => {
+ log.push(`animation frame callback ${animationFrameCallback}`);
+ postMessage(log.join("\n"));
+ self.close();
+ }, 0);
+ });
};