Skip to content

Truly independent web browser

LibWeb: Stop delaying the load event on an object’s stopped fetch

sideshowbarker6c4fd5ac0ecf
diff --git a/Libraries/LibWeb/HTML/HTMLObjectElement.cpp b/Libraries/LibWeb/HTML/HTMLObjectElement.cpp
index 2feadde8096..31446ca0b6d 100644
--- a/Libraries/LibWeb/HTML/HTMLObjectElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLObjectElement.cpp
@@ -295,12 +295,17 @@ void HTMLObjectElement::queue_element_task_to_run_object_representation_steps()
request->set_use_url_credentials(true);
Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input { Fetch::Engine::BodyIntent::DeliverStreamToConsumerAgent };
- fetch_algorithms_input.process_response = GC::create_function(GC::Heap::the(), GC::captures(self), [](auto& self, GC::Ref<Fetch::Infrastructure::ResponseView> response) {
+ // NB: Each fetch releases its load event delay once, when its response is handled or when it's stopped,
+ // whichever comes first. Anything that arrives for it after that is ignored.
+ auto fetch_id = ++self.m_last_resource_fetch_id;
+ self.m_pending_resource_fetches.set(fetch_id);
+
+ fetch_algorithms_input.process_response = GC::create_function(GC::Heap::the(), GC::captures(self, fetch_id), [](auto& self, auto fetch_id, GC::Ref<Fetch::Infrastructure::ResponseView> response) {
auto& realm = HTML::relevant_realm(self);
auto& global = self.document().relevant_settings_object().realm().global_object();
if (response->is_network_error()) {
- self.resource_did_fail();
+ self.resource_did_fail(fetch_id);
return;
}
@@ -313,35 +318,42 @@ void HTMLObjectElement::queue_element_task_to_run_object_representation_steps()
auto ignore_body_error = GC::create_function(GC::Heap::the(), [](JS::Value) { });
(void)body->incrementally_read(realm, ignore_chunk, ignore_end_of_body, ignore_body_error, GC::Ref { global });
}
- self.resource_did_fail();
+ self.resource_did_fail(fetch_id);
return;
}
if (response->type() == Fetch::Infrastructure::Response::Type::Opaque || response->type() == Fetch::Infrastructure::Response::Type::OpaqueRedirect)
response = response->unsafe_response();
- auto on_data_read = GC::create_function(GC::Heap::the(), GC::captures(self, response), [](auto& self, auto& response, ByteBuffer data) {
- self.resource_did_load(response, data);
+ auto on_data_read = GC::create_function(GC::Heap::the(), GC::captures(self, response, fetch_id), [](auto& self, auto& response, auto fetch_id, ByteBuffer data) {
+ self.resource_did_load(fetch_id, response, data);
});
- auto on_error = GC::create_function(GC::Heap::the(), GC::captures(self), [](auto& self, JS::Value) {
- self.resource_did_fail();
+ auto on_error = GC::create_function(GC::Heap::the(), GC::captures(self, fetch_id), [](auto& self, auto fetch_id, JS::Value) {
+ self.resource_did_fail(fetch_id);
});
// NB: A response without a body, such as a 204, has nothing to read: Its data is empty.
if (!response->body()) {
- self.resource_did_load(response, {});
+ self.resource_did_load(fetch_id, response, {});
return;
}
response->body()->fully_read(realm, on_data_read, on_error, GC::Ref { global });
});
// 5. Fetch request.
- (void)Fetch::Fetching::fetch(realm, request, Fetch::Infrastructure::FetchAlgorithms::create(move(fetch_algorithms_input)));
+ auto fetch_controller = Fetch::Fetching::fetch(realm, request, Fetch::Infrastructure::FetchAlgorithms::create(move(fetch_algorithms_input)));
// Fetching the resource must delay the load event of the element's node document until the task that is
// queued by the networking task source once the resource has been fetched (defined next) has been run.
self.m_document_load_event_delayer_for_resource_load.empend(self.document());
+ // AD-HOC: A stopped fetch, such as one of a document that was stopped, never processes its response. Stop
+ // delaying the load event on it then, as if the user agent had given up on loading it.
+ fetch_controller->set_stop_steps(GC::create_function(GC::Heap::the(), GC::captures(GC::Weak { self }, fetch_id), [](auto& weak_self, auto fetch_id) {
+ if (auto object = weak_self.ptr(); object && object->m_pending_resource_fetches.remove(fetch_id))
+ object->m_document_load_event_delayer_for_resource_load.take_last();
+ }));
+
// 6. If the resource is not yet available (e.g. because the resource was not available in the cache, so that
// loading the resource required making a request over the network), then jump to the step below labeled
// fallback. The task that is queued by the networking task source once the resource is available must
@@ -366,8 +378,10 @@ void HTMLObjectElement::queue_element_task_to_run_object_representation_steps()
}
// https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-object-element:concept-event-fire-2
-void HTMLObjectElement::resource_did_fail()
+void HTMLObjectElement::resource_did_fail(u64 fetch_id)
{
+ if (!m_pending_resource_fetches.remove(fetch_id))
+ return;
ScopeGuard guard { [&]() { m_document_load_event_delayer_for_resource_load.take_last(); } };
// 3.7. If the load failed (e.g. there was an HTTP 404 error, there was a DNS error), fire an event named error at
@@ -377,8 +391,10 @@ void HTMLObjectElement::resource_did_fail()
}
// https://html.spec.whatwg.org/multipage/iframe-embed-object.html#object-type-detection
-void HTMLObjectElement::resource_did_load(Fetch::Infrastructure::ResponseView const& response, ReadonlyBytes data)
+void HTMLObjectElement::resource_did_load(u64 fetch_id, Fetch::Infrastructure::ResponseView const& response, ReadonlyBytes data)
{
+ if (!m_pending_resource_fetches.remove(fetch_id))
+ return;
ScopeGuard guard { [&]() { m_document_load_event_delayer_for_resource_load.take_last(); } };
// 3.8. Determine the resource type, as follows:
diff --git a/Libraries/LibWeb/HTML/HTMLObjectElement.h b/Libraries/LibWeb/HTML/HTMLObjectElement.h
index b4a91e66235..3fbb03484bb 100644
--- a/Libraries/LibWeb/HTML/HTMLObjectElement.h
+++ b/Libraries/LibWeb/HTML/HTMLObjectElement.h
@@ -6,6 +6,7 @@
#pragma once
+#include <AK/HashTable.h>
#include <AK/Utf16View.h>
#include <LibCore/Forward.h>
#include <LibGfx/Forward.h>
@@ -83,8 +84,8 @@ private:
void load_image();
void update_layout_and_child_objects(Representation);
- void resource_did_load(Fetch::Infrastructure::ResponseView const&, ReadonlyBytes);
- void resource_did_fail();
+ void resource_did_load(u64 fetch_id, Fetch::Infrastructure::ResponseView const&, ReadonlyBytes);
+ void resource_did_fail(u64 fetch_id);
// ^DOM::Element
virtual i32 default_tab_index_value() const override;
@@ -107,6 +108,10 @@ private:
Vector<DOM::DocumentLoadEventDelayer> m_document_load_event_delayer_for_object_representation_task;
Vector<DOM::DocumentLoadEventDelayer> m_document_load_event_delayer_for_resource_load;
+
+ // The resource fetches that still delay the load event, until their response is handled or they're stopped.
+ HashTable<u64> m_pending_resource_fetches;
+ u64 m_last_resource_fetch_id { 0 };
};
}
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index cbb7c590d10..0f9b2c6df5d 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -11,6 +11,8 @@ Text/input/replaced/embed-window-stop-during-fetch.html
Text/input/replaced/embed-window-stop-during-body.html
Text/input/replaced/embed-removed-during-body.html
Text/input/replaced/embed-in-shadow-tree.html
+Text/input/HTML/HTMLObjectElement-window-stop-during-fetch.html
+Text/input/HTML/HTMLObjectElement-window-stop-during-body.html
Text/input/wpt-import/html/semantics/embedded-content/the-embed-element/embed-ignored-in-media-element.html
Text/input/wpt-import/html/semantics/embedded-content/the-embed-element/embed-document.html
Text/input/wpt-import/html/semantics/embedded-content/the-embed-element/embed-in-object-fallback.html
diff --git a/Tests/LibWeb/Text/expected/HTML/HTMLObjectElement-window-stop-during-body.txt b/Tests/LibWeb/Text/expected/HTML/HTMLObjectElement-window-stop-during-body.txt
new file mode 100644
index 00000000000..c51ad8dd679
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/HTMLObjectElement-window-stop-during-body.txt
@@ -0,0 +1 @@
+readyState: complete
diff --git a/Tests/LibWeb/Text/expected/HTML/HTMLObjectElement-window-stop-during-fetch.txt b/Tests/LibWeb/Text/expected/HTML/HTMLObjectElement-window-stop-during-fetch.txt
new file mode 100644
index 00000000000..c51ad8dd679
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/HTMLObjectElement-window-stop-during-fetch.txt
@@ -0,0 +1 @@
+readyState: complete
diff --git a/Tests/LibWeb/Text/input/HTML/HTMLObjectElement-window-stop-during-body.html b/Tests/LibWeb/Text/input/HTML/HTMLObjectElement-window-stop-during-body.html
new file mode 100644
index 00000000000..9272f000deb
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/HTMLObjectElement-window-stop-during-body.html
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<object id="object" data="../wpt-import/images/green-256x256.png?pipe=trickle(100:d3)"></object>
+<script>
+ asyncTest(done => {
+ // The object's fetch starts in a queued task, and its body is held back for 3 seconds after the first 100 bytes.
+ setTimeout(() => {
+ // Stopping the document stops the object's fetch while its body is still arriving.
+ window.stop();
+ let frames = 0;
+ const check = () => {
+ if (document.readyState === "complete" || ++frames === 60) {
+ println(`readyState: ${document.readyState}`);
+ done();
+ return;
+ }
+ requestAnimationFrame(check);
+ };
+ check();
+ }, 100);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/HTMLObjectElement-window-stop-during-fetch.html b/Tests/LibWeb/Text/input/HTML/HTMLObjectElement-window-stop-during-fetch.html
new file mode 100644
index 00000000000..6aaa64f1b6e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/HTMLObjectElement-window-stop-during-fetch.html
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<object id="object" data="../wpt-import/xhr/resources/delay.py?ms=3000"></object>
+<script>
+ asyncTest(done => {
+ // The object's fetch starts in a queued task, and the server holds back its response headers for 3 seconds.
+ setTimeout(() => {
+ // Stopping the document stops the object's fetch before it has a response.
+ window.stop();
+ let frames = 0;
+ const check = () => {
+ if (document.readyState === "complete" || ++frames === 60) {
+ println(`readyState: ${document.readyState}`);
+ done();
+ return;
+ }
+ requestAnimationFrame(check);
+ };
+ check();
+ }, 100);
+ });
+</script>