Skip to content

Truly independent web browser

LibWeb: Stop delaying the load event for aborted image fetches

Andreas Klingf73c7c867797
diff --git a/Libraries/LibWeb/HTML/HTMLImageElement.cpp b/Libraries/LibWeb/HTML/HTMLImageElement.cpp
index 4769406e38d..0480757cf68 100644
--- a/Libraries/LibWeb/HTML/HTMLImageElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLImageElement.cpp
@@ -741,7 +741,7 @@ void HTMLImageElement::decode(GC::Ref<WebIDL::Promise> promise) const
return;
}
- current_request.add_callbacks(
+ current_request.add_callbacks_without_delaying_the_load_event(
// AD-HOC: Enqueue on the batching dispatcher to preserve ordering relative to update_the_image_data's step
// 16, which also goes through the batching dispatcher. Otherwise decode() can resolve before the
// current request transitions to CompletelyAvailable, leaving the image dimensions at zero when
@@ -1074,6 +1074,7 @@ after_step_7:
unregister_with_decoded_image_data_if_needed();
abort_the_image_request(m_current_request);
abort_the_image_request(m_pending_request);
+ stop_delaying_the_load_event_for_aborted_requests();
m_pending_request = nullptr;
// AD-HOC: The element may have been rendering as blank space while a load was pending;
@@ -1115,6 +1116,7 @@ after_step_7:
unregister_with_decoded_image_data_if_needed();
abort_the_image_request(m_current_request);
abort_the_image_request(m_pending_request);
+ stop_delaying_the_load_event_for_aborted_requests();
// 2. Set the current request's state to broken.
m_current_request->set_state(ImageRequest::State::Broken);
@@ -1423,6 +1425,16 @@ void HTMLImageElement::add_callbacks_to_image_request(GC::Ref<ImageRequest> imag
});
}
+// NB: Aborting the image requests aborts their fetches, so nothing that the img element is fetching delays the load
+// event of its node document anymore. Our fetches are shared and keep going, so stop waiting for them here.
+void HTMLImageElement::stop_delaying_the_load_event_for_aborted_requests()
+{
+ m_current_request->stop_waiting_for_fetch();
+ if (m_pending_request)
+ m_pending_request->stop_waiting_for_fetch();
+ m_load_event_delayer.clear();
+}
+
void HTMLImageElement::did_set_viewport_rect(CSSPixelRect const& viewport_rect)
{
if (viewport_rect.size() == m_last_seen_viewport_size)
diff --git a/Libraries/LibWeb/HTML/HTMLImageElement.h b/Libraries/LibWeb/HTML/HTMLImageElement.h
index 09d5f37c277..15fd717b6ef 100644
--- a/Libraries/LibWeb/HTML/HTMLImageElement.h
+++ b/Libraries/LibWeb/HTML/HTMLImageElement.h
@@ -162,6 +162,7 @@ private:
void handle_failed_fetch();
void add_callbacks_to_image_request(GC::Ref<ImageRequest>, bool maybe_omit_events, Utf16View url_string, Utf16View previous_url);
+ void stop_delaying_the_load_event_for_aborted_requests();
void create_alt_text_shadow_tree();
void remove_alt_text_shadow_tree();
diff --git a/Libraries/LibWeb/HTML/ImageRequest.cpp b/Libraries/LibWeb/HTML/ImageRequest.cpp
index a397d3b63a6..ed4c4c6eaaf 100644
--- a/Libraries/LibWeb/HTML/ImageRequest.cpp
+++ b/Libraries/LibWeb/HTML/ImageRequest.cpp
@@ -69,8 +69,12 @@ void ImageRequest::set_current_url(DOM::Document& document, String url, CORSSett
{
m_current_url = Utf16String::from_utf8(url);
- if (auto parsed_url = DOMURL::parse(m_current_url); parsed_url.has_value())
- m_shared_resource_request = SharedResourceRequest::get_or_create(document, parsed_url.release_value(), cors_setting);
+ if (auto parsed_url = DOMURL::parse(m_current_url); parsed_url.has_value()) {
+ auto shared_resource_request = SharedResourceRequest::get_or_create(document, parsed_url.release_value(), cors_setting);
+ if (shared_resource_request != m_shared_resource_request)
+ stop_waiting_for_fetch();
+ m_shared_resource_request = shared_resource_request;
+ }
}
void ImageRequest::set_current_url(DOM::Document& document, Utf16String url, CORSSettingAttribute cors_setting)
@@ -130,7 +134,23 @@ void ImageRequest::fetch_image(NonnullRefPtr<Fetch::Infrastructure::Request> req
void ImageRequest::add_callbacks(Function<void()> on_finish, Function<void()> on_fail, Function<void()> on_stop)
{
VERIFY(m_shared_resource_request);
- m_shared_resource_request->add_callbacks(move(on_finish), move(on_fail), move(on_stop));
+ if (auto id = m_shared_resource_request->add_removable_callbacks(move(on_finish), move(on_fail), move(on_stop)); id.has_value())
+ m_shared_resource_request_callbacks_ids.append(*id);
+}
+
+void ImageRequest::add_callbacks_without_delaying_the_load_event(Function<void()> on_finish, Function<void()> on_fail)
+{
+ VERIFY(m_shared_resource_request);
+ (void)m_shared_resource_request->add_removable_callbacks(move(on_finish), move(on_fail), {}, SharedResourceRequest::DelaysLoadEvent::No);
+}
+
+void ImageRequest::stop_waiting_for_fetch()
+{
+ if (m_shared_resource_request) {
+ for (auto id : m_shared_resource_request_callbacks_ids)
+ m_shared_resource_request->remove_callbacks(id);
+ }
+ m_shared_resource_request_callbacks_ids.clear();
}
}
diff --git a/Libraries/LibWeb/HTML/ImageRequest.h b/Libraries/LibWeb/HTML/ImageRequest.h
index e90a606b925..f70020443c3 100644
--- a/Libraries/LibWeb/HTML/ImageRequest.h
+++ b/Libraries/LibWeb/HTML/ImageRequest.h
@@ -60,6 +60,13 @@ public:
void fetch_image(NonnullRefPtr<Fetch::Infrastructure::Request>);
void add_callbacks(Function<void()> on_finish, Function<void()> on_fail, Function<void()> on_stop = {});
+ // Adds callbacks that do not make the fetch delay the load event, and that stop_waiting_for_fetch() keeps.
+ void add_callbacks_without_delaying_the_load_event(Function<void()> on_finish, Function<void()> on_fail);
+
+ // Stops waiting for the fetch of the shared resource request, which then no longer delays the load event for
+ // this image request.
+ void stop_waiting_for_fetch();
+
GC::Ptr<SharedResourceRequest const> shared_resource_request() const { return m_shared_resource_request; }
[[nodiscard]] bool was_aborted() const { return m_was_aborted; }
@@ -96,6 +103,8 @@ private:
// a SharedResourceRequest that may be used by others, we don't abort the fetch itself; instead, fetch
// callbacks use this flag to discard their work for aborted requests.
bool m_was_aborted { false };
+
+ Vector<u64> m_shared_resource_request_callbacks_ids;
};
// https://html.spec.whatwg.org/multipage/images.html#abort-the-image-request
diff --git a/Libraries/LibWeb/HTML/SharedResourceRequest.cpp b/Libraries/LibWeb/HTML/SharedResourceRequest.cpp
index 6d445ae98fb..c9d713f6258 100644
--- a/Libraries/LibWeb/HTML/SharedResourceRequest.cpp
+++ b/Libraries/LibWeb/HTML/SharedResourceRequest.cpp
@@ -197,6 +197,7 @@ void SharedResourceRequest::fetch_resource(NonnullRefPtr<Fetch::Infrastructure::
m_state = State::Fetching;
m_load_event_delayer.emplace(*m_document);
+ m_fetch_has_user_without_callbacks = m_callbacks.is_empty();
auto fetch_controller = Fetch::Fetching::fetch(
realm,
@@ -211,26 +212,33 @@ void SharedResourceRequest::fetch_resource(NonnullRefPtr<Fetch::Infrastructure::
}
void SharedResourceRequest::add_callbacks(Function<void()> on_finish, Function<void()> on_fail, Function<void()> on_stop)
+{
+ (void)add_removable_callbacks(move(on_finish), move(on_fail), move(on_stop));
+}
+
+Optional<u64> SharedResourceRequest::add_removable_callbacks(Function<void()> on_finish, Function<void()> on_fail, Function<void()> on_stop, DelaysLoadEvent delays_load_event)
{
if (m_state == State::Finished) {
if (on_finish)
on_finish();
- return;
+ return {};
}
if (m_state == State::Failed) {
if (on_fail)
on_fail();
- return;
+ return {};
}
if (m_state == State::Stopped) {
if (on_stop)
on_stop();
- return;
+ return {};
}
Callbacks callbacks;
+ callbacks.id = m_next_callbacks_id++;
+ callbacks.delays_load_event = delays_load_event;
if (on_finish)
callbacks.on_finish = GC::create_function(GC::Heap::the(), move(on_finish));
if (on_fail)
@@ -238,7 +246,31 @@ void SharedResourceRequest::add_callbacks(Function<void()> on_finish, Function<v
if (on_stop)
callbacks.on_stop = GC::create_function(GC::Heap::the(), move(on_stop));
+ auto id = callbacks.id;
m_callbacks.append(move(callbacks));
+
+ // NB: The fetch may have stopped delaying the load event because nothing waited for it, but now something does.
+ if (m_state == State::Fetching && delays_load_event == DelaysLoadEvent::Yes && !m_load_event_delayer.has_value())
+ m_load_event_delayer.emplace(*m_document);
+ return id;
+}
+
+void SharedResourceRequest::remove_callbacks(u64 id)
+{
+ // NB: Only mark the entry as no longer waiting, and skip it when invoking the callbacks. A callback that is being
+ // invoked can get here, and the invocation goes on over m_callbacks.
+ for (auto& callbacks : m_callbacks) {
+ if (callbacks.id == id)
+ callbacks.id = 0;
+ }
+
+ // NB: The fetch keeps going so that its result can still be cached, but once nothing waits for it anymore, it
+ // must not keep delaying the load event of the document.
+ auto nothing_waits = all_of(m_callbacks, [](auto const& callbacks) {
+ return callbacks.id == 0 || callbacks.delays_load_event == DelaysLoadEvent::No;
+ });
+ if (m_state == State::Fetching && nothing_waits && !m_fetch_has_user_without_callbacks)
+ m_load_event_delayer.clear();
}
void SharedResourceRequest::handle_successful_fetch(URL::URL const& url_string, IsSVGImage is_svg_image, ByteBuffer data, bool image_data_is_cors_cross_origin)
@@ -314,7 +346,7 @@ void SharedResourceRequest::handle_failed_fetch()
m_load_event_delayer.clear();
m_fetch_controller = nullptr;
for (auto& callback : m_callbacks) {
- if (callback.on_fail)
+ if (callback.id != 0 && callback.on_fail)
callback.on_fail->function()();
}
m_callbacks.clear();
@@ -333,7 +365,7 @@ void SharedResourceRequest::handle_stopped_fetch()
m_load_event_delayer.clear();
m_fetch_controller = nullptr;
for (auto& callback : m_callbacks) {
- if (callback.on_stop)
+ if (callback.id != 0 && callback.on_stop)
callback.on_stop->function()();
}
m_callbacks.clear();
@@ -346,7 +378,7 @@ void SharedResourceRequest::handle_successful_resource_load()
m_load_event_delayer.clear();
m_fetch_controller = nullptr;
for (auto& callback : m_callbacks) {
- if (callback.on_finish)
+ if (callback.id != 0 && callback.on_finish)
callback.on_finish->function()();
}
m_callbacks.clear();
diff --git a/Libraries/LibWeb/HTML/SharedResourceRequest.h b/Libraries/LibWeb/HTML/SharedResourceRequest.h
index 5d977babc24..614e4203e3d 100644
--- a/Libraries/LibWeb/HTML/SharedResourceRequest.h
+++ b/Libraries/LibWeb/HTML/SharedResourceRequest.h
@@ -53,6 +53,15 @@ public:
void add_callbacks(Function<void()> on_finish, Function<void()> on_fail, Function<void()> on_stop = {});
+ enum class DelaysLoadEvent : u8 {
+ No,
+ Yes,
+ };
+
+ // Returns an ID for remove_callbacks(), or nothing if the callbacks already ran.
+ Optional<u64> add_removable_callbacks(Function<void()> on_finish, Function<void()> on_fail, Function<void()> on_stop = {}, DelaysLoadEvent = DelaysLoadEvent::Yes);
+ void remove_callbacks(u64 id);
+
bool is_fetching() const;
bool needs_fetching() const;
@@ -88,11 +97,18 @@ private:
GC::Ref<Page> m_page;
struct Callbacks {
+ // NB: Zero when nothing waits for these callbacks anymore.
+ u64 id { 0 };
+ DelaysLoadEvent delays_load_event { DelaysLoadEvent::Yes };
GC::Ptr<GC::Function<void()>> on_finish;
GC::Ptr<GC::Function<void()>> on_fail;
GC::Ptr<GC::Function<void()>> on_stop;
};
Vector<Callbacks> m_callbacks;
+ u64 m_next_callbacks_id { 1 };
+
+ // NB: Set when the fetch was started for a user that waits for it without callbacks.
+ bool m_fetch_has_user_without_callbacks { false };
Key m_key;
GC::Ptr<DecodedImageData> m_image_data;
diff --git a/Tests/LibWeb/Text/expected/HTML/image-decode-then-empty-src-does-not-delay-load-event.txt b/Tests/LibWeb/Text/expected/HTML/image-decode-then-empty-src-does-not-delay-load-event.txt
new file mode 100644
index 00000000000..15aff0322d7
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/image-decode-then-empty-src-does-not-delay-load-event.txt
@@ -0,0 +1 @@
+PASS: The load event of the iframe fired before the image arrived
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/delay-load-event-until-move-to-empty-source.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/delay-load-event-until-move-to-empty-source.txt
new file mode 100644
index 00000000000..072c186bf5e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/delay-load-event-until-move-to-empty-source.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Image element delays window's load event until the image changes to empty source
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/HTML/image-decode-then-empty-src-does-not-delay-load-event.html b/Tests/LibWeb/Text/input/HTML/image-decode-then-empty-src-does-not-delay-load-event.html
new file mode 100644
index 00000000000..82632a3593d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/image-decode-then-empty-src-does-not-delay-load-event.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+// Test: Setting the src of an img to the empty string while it loads stops delaying the load event of its document,
+// even when decode() was called on the image while it loaded.
+asyncTest(async done => {
+ const server = httpTestServer();
+ const slowImageUrl = await server.createEcho("GET", "/decode-then-empty-src-slow.svg", {
+ status: 200,
+ headers: {
+ "Content-Type": "image/svg+xml",
+ "Access-Control-Allow-Origin": "*",
+ },
+ body: '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><rect width="16" height="16" fill="green"/></svg>',
+ delay_ms: 10000,
+ });
+
+ const iframe = document.createElement("iframe");
+ const start = performance.now();
+ iframe.onload = () => {
+ if (performance.now() - start < 5000)
+ println("PASS: The load event of the iframe fired before the image arrived");
+ else
+ println("FAIL: The load event of the iframe waited for the image");
+ done();
+ };
+ iframe.srcdoc = `<img src="${slowImageUrl}"><script>
+ const image = document.querySelector("img");
+ image.decode().catch(() => {});
+ setTimeout(() => { image.src = ""; }, 0);
+ <\/script>`;
+ document.body.appendChild(iframe);
+});
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-img-element/delay-load-event-until-move-to-empty-source.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-img-element/delay-load-event-until-move-to-empty-source.html
new file mode 100644
index 00000000000..55c544006a4
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/embedded-content/the-img-element/delay-load-event-until-move-to-empty-source.html
@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>Inline image element blocks load until source is changed to empty source</title>
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+<img src="../../../../images/blue.png?pipe=trickle(d100)">
+<script>
+
+async_test(t => {
+ const image = document.querySelector("img");
+
+ assert_false(image.complete, "The image is loading initially");
+
+ // Complete the test as soon as we obtained the window "load" event,
+ // which should happen as soon as the image stops loading by moving
+ // to an empty source.
+ window.addEventListener("load", t.step_func_done(() => {
+ assert_true(image.complete, "The image is no longer loading once the window 'load' event is dispatched");
+ }));
+
+ // Stop loading the image.
+ image.src = "";
+}, "Image element delays window's load event until the image changes to empty source");
+
+</script>