Skip to content

Truly independent web browser

LibWeb: Defer CSS image prefetches for inactive documents

Andreas Kling08ecc39f77e7
diff --git a/Libraries/LibWeb/CSS/StyleValues/ImageStyleValue.cpp b/Libraries/LibWeb/CSS/StyleValues/ImageStyleValue.cpp
index 900bd1b1953..a746d37ad5d 100644
--- a/Libraries/LibWeb/CSS/StyleValues/ImageStyleValue.cpp
+++ b/Libraries/LibWeb/CSS/StyleValues/ImageStyleValue.cpp
@@ -176,6 +176,11 @@ GC::Ptr<HTML::SharedResourceRequest> ImageStyleValue::fetch_image(DOM::Document&
void ImageStyleValue::load_any_resources(DOM::Document& document)
{
+ // NB: Prefetching for inactive documents would load images merely from parsing or cloning declarations. If these
+ // declarations are later rendered in an active document, register_client() will fetch their images then.
+ if (!document.is_fully_active())
+ return;
+
if (resource_registered_by_clients(document))
return;
if (auto resolved_url = this->resolved_url(document); resolved_url.has_value() && document.css_image_resource(*resolved_url))
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index e8aa9ca75ef..9f20b32ad45 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -2664,6 +2664,11 @@ Utf16String Internals::canvas_color_scheme()
return CSS::preferred_color_scheme_to_utf16_fly_string(document.canvas_color_scheme(read)).to_utf16_string();
}
+u64 Internals::image_request_count(DOM::Document const& document) const
+{
+ return document.shared_resource_requests().size();
+}
+
WebIDL::ExceptionOr<GC::Ref<JS::Object>> Internals::image_animation_state_for_url(Utf16String const& url)
{
auto& document = window().associated_document();
diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.h
index 9d1a0871e16..7e4714dd747 100644
--- a/Libraries/LibWeb/Internals/Internals.h
+++ b/Libraries/LibWeb/Internals/Internals.h
@@ -300,6 +300,7 @@ public:
WebIDL::UnsignedLongLong active_timer_count(JS::Object& window);
WebIDL::UnsignedLongLong task_queue_length();
Utf16String canvas_color_scheme();
+ u64 image_request_count(DOM::Document const&) const;
WebIDL::ExceptionOr<GC::Ref<JS::Object>> image_animation_state_for_url(Utf16String const& url);
bool media_element_is_fetching(HTML::HTMLMediaElement&);
bool media_element_is_playing_audio(HTML::HTMLMediaElement&);
diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idl
index aaecdc3ab66..eba949b6a7a 100644
--- a/Libraries/LibWeb/Internals/Internals.idl
+++ b/Libraries/LibWeb/Internals/Internals.idl
@@ -365,6 +365,8 @@ interface Internals {
// Returns how many tasks sit in the main thread event loop's task queue, runnable or not.
unsigned long long taskQueueLength();
DOMString canvasColorScheme();
+ // Returns the number of shared image requests owned by the given document.
+ unsigned long long imageRequestCount(Document document);
// Returns the selector-insight cache state for stylesheet invalidation tests.
object imageAnimationStateForURL(USVString url);
boolean mediaElementIsFetching(HTMLMediaElement element);
diff --git a/Tests/LibWeb/Text/expected/css/inactive-document-css-images.txt b/Tests/LibWeb/Text/expected/css/inactive-document-css-images.txt
new file mode 100644
index 00000000000..d69f66010d6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/inactive-document-css-images.txt
@@ -0,0 +1,3 @@
+imported inline image requests: 0
+inactive stylesheet image requests: 0
+images requested after adoption: 2
diff --git a/Tests/LibWeb/Text/input/css/inactive-document-css-images.html b/Tests/LibWeb/Text/input/css/inactive-document-css-images.html
new file mode 100644
index 00000000000..ff8b309406c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/inactive-document-css-images.html
@@ -0,0 +1,37 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const server = httpTestServer();
+ const urls = await Promise.all(["inline", "stylesheet"].map(name => server.createEcho("GET", `/${name}.png`, {
+ status: 200,
+ headers: { "Content-Type": "image/png", "Cache-Control": "no-store" },
+ body_encoding: "base64",
+ body: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=",
+ })));
+
+ const source = document.createElement("select");
+ source.style.backgroundImage = `url("${urls[0]}")`;
+ const option = document.createElement("option");
+ option.style.backgroundImage = `url("${urls[0]}")`;
+ source.append(option);
+
+ const inactiveDocument = document.implementation.createHTMLDocument("");
+ const copy = inactiveDocument.importNode(source, true);
+ println(`imported inline image requests: ${internals.imageRequestCount(inactiveDocument)}`);
+
+ const style = inactiveDocument.createElement("style");
+ style.textContent = `#target { background-image: url("${urls[1]}"); width: 10px; height: 10px; }`;
+ inactiveDocument.head.append(style);
+ const target = inactiveDocument.createElement("div");
+ target.id = "target";
+ inactiveDocument.body.append(copy, target);
+ println(`inactive stylesheet image requests: ${internals.imageRequestCount(inactiveDocument)}`);
+
+ const before = internals.imageRequestCount(document);
+ document.head.append(style);
+ document.body.append(copy, target);
+ internals.dumpLayoutTree(document);
+ println(`images requested after adoption: ${internals.imageRequestCount(document) - before}`);
+ });
+</script>