Truly independent web browser
LibWeb: Coalesce queued object representation tasks
Andreas Kling894f7f5173c1
diff --git a/Libraries/LibWeb/HTML/HTMLObjectElement.cpp b/Libraries/LibWeb/HTML/HTMLObjectElement.cppindex 4a942246598..a00340bc046 100644--- a/Libraries/LibWeb/HTML/HTMLObjectElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLObjectElement.cpp@@ -88,6 +88,7 @@ void HTMLObjectElement::visit_edges(Cell::Visitor& visitor)Base::visit_edges(visitor);visitor.visit(m_resource_request);visitor.visit(m_document_observer);+ visitor.visit(m_document_of_queued_object_representation_task);}void HTMLObjectElement::adopted_from(DOM::Document& old_document)@@ -225,14 +226,25 @@ void HTMLObjectElement::queue_element_task_to_run_object_representation_steps()if (!document().is_fully_active())return;+ // OPTIMIZATION: A queued task that has not started yet reads the element's state when it runs, so another task+ // would only repeat its work. It would also fetch the resource a second time.+ // NB: A task queued before the element moved to another document stays with that document, which may never run+ // it, so the element's new document queues a task of its own.+ if (m_document_of_queued_object_representation_task.ptr() == &document())+ return;+ m_document_of_queued_object_representation_task = document();+ auto task_number = ++m_object_representation_task_count;+// This task being queued or actively running must delay the load event of the element's node document.m_document_load_event_delayer_for_object_representation_task.empend(document());auto release_load_event_delay = GC::create_function(GC::Heap::the(), GC::captures(*this), [](auto& self) {self.m_document_load_event_delayer_for_object_representation_task.take_last();});- auto steps = GC::create_function(GC::Heap::the(), GC::captures(*this, release_load_event_delay), [](auto& self, auto& release_load_event_delay) {+ auto steps = GC::create_function(GC::Heap::the(), GC::captures(*this, release_load_event_delay, task_number), [](auto& self, auto& release_load_event_delay, auto task_number) {ScopeGuard guard { [&] { release_load_event_delay->function()(); } };+ if (self.m_object_representation_task_count == task_number)+ self.m_document_of_queued_object_representation_task = nullptr;auto& realm = HTML::relevant_realm(self);// FIXME: 1. If the user has indicated a preference that this object element's fallback content be shown instead of the@@ -341,7 +353,12 @@ void HTMLObjectElement::queue_element_task_to_run_object_representation_steps()}});- queue_an_element_task(HTML::Task::Source::DOMManipulation, move(steps), release_load_event_delay);+ auto discard_steps = GC::create_function(GC::Heap::the(), GC::captures(*this, release_load_event_delay, task_number), [](auto& self, auto& release_load_event_delay, auto task_number) {+ if (self.m_object_representation_task_count == task_number)+ self.m_document_of_queued_object_representation_task = nullptr;+ release_load_event_delay->function()();+ });+ queue_an_element_task(HTML::Task::Source::DOMManipulation, move(steps), discard_steps);}// https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-object-element:concept-event-fire-2diff --git a/Libraries/LibWeb/HTML/HTMLObjectElement.h b/Libraries/LibWeb/HTML/HTMLObjectElement.hindex 01e0be23d03..bcb8516b468 100644--- a/Libraries/LibWeb/HTML/HTMLObjectElement.h+++ b/Libraries/LibWeb/HTML/HTMLObjectElement.h@@ -99,6 +99,10 @@ private:GC::Ptr<DOM::DocumentObserver> m_document_observer;+ // The document of the last object representation task queued, while that task has not started yet.+ GC::Ptr<DOM::Document> m_document_of_queued_object_representation_task;+ u64 m_object_representation_task_count { 0 };+Vector<DOM::DocumentLoadEventDelayer> m_document_load_event_delayer_for_object_representation_task;Vector<DOM::DocumentLoadEventDelayer> m_document_load_event_delayer_for_resource_load;};diff --git a/Tests/LibWeb/Text/expected/HTML/object-element-coalesces-representation-tasks.txt b/Tests/LibWeb/Text/expected/HTML/object-element-coalesces-representation-tasks.txtnew file mode 100644index 00000000000..42fc78c582c--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/object-element-coalesces-representation-tasks.txt@@ -0,0 +1 @@+Requests for the object's document: 2diff --git a/Tests/LibWeb/Text/input/HTML/object-element-coalesces-representation-tasks.html b/Tests/LibWeb/Text/input/HTML/object-element-coalesces-representation-tasks.htmlnew file mode 100644index 00000000000..7b09bce5095--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/object-element-coalesces-representation-tasks.html@@ -0,0 +1,24 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ promiseTest(async () => {+ const server = httpTestServer();+ const url = await server.createEcho("GET", "/object-element-coalesces-representation-tasks", {+ status: 200,+ headers: { "Content-Type": "text/html", "Access-Control-Allow-Origin": "*" },+ body: "<!DOCTYPE html><p>Hello</p>",+ });++ // Creating the object and setting its data attribute both queue a task to determine what it represents. Only+ // one of them runs, so the document is requested twice: once by the object's fetch, and once by the+ // navigation of its content navigable.+ const object = document.createElement("object");+ const loaded = new Promise(resolve => object.onload = resolve);+ object.data = url;+ document.body.appendChild(object);+ await loaded;++ const requestCount = await (await fetch(server.recordedRequestCountURL(url))).json();+ println(`Requests for the object's document: ${requestCount}`);+ });+</script>diff --git a/Tests/LibWeb/Text/input/object-with-unsupported-type-in-data-attribute.html b/Tests/LibWeb/Text/input/object-with-unsupported-type-in-data-attribute.htmlindex c1e29db00a9..dfdaabe949c 100644--- a/Tests/LibWeb/Text/input/object-with-unsupported-type-in-data-attribute.html+++ b/Tests/LibWeb/Text/input/object-with-unsupported-type-in-data-attribute.html@@ -5,15 +5,10 @@</object><script>asyncTest(done => {- // Fetching the data: URL is performed on the following deferred tasks:- // 1. The element task to trigger the fetch.- // 2. The fetch task to parse the data URL.- // So we must churn the event loop twice to reliably let the fallback representation be chosen.- setTimeout(() => {- setTimeout(() => {- println(document.body.innerText);- done();- });+ // Fetching the object's data delays the load event, so the fallback representation is chosen by then.+ window.addEventListener("load", () => {+ println(document.body.innerText);+ done();});});</script>