Truly independent web browser
LibWeb: Stop lazy loading an iframe when its entry is restored
sideshowbarker5b854abe8160
diff --git a/Libraries/LibWeb/HTML/HistoryExecutor.cpp b/Libraries/LibWeb/HTML/HistoryExecutor.cppindex 23e70dd9e56..aa609ed7ac6 100644--- a/Libraries/LibWeb/HTML/HistoryExecutor.cpp+++ b/Libraries/LibWeb/HTML/HistoryExecutor.cpp@@ -9,6 +9,7 @@#include <LibWeb/HTML/BrowsingContext.h>#include <LibWeb/HTML/DocumentState.h>#include <LibWeb/HTML/EventLoop/EventLoop.h>+#include <LibWeb/HTML/HTMLIFrameElement.h>#include <LibWeb/HTML/HistoryExecutor.h>#include <LibWeb/HTML/LocalNavigable.h>#include <LibWeb/HTML/LocalTraversableNavigable.h>@@ -317,10 +318,21 @@ void HistoryExecutor::run_ui_changing_navigable_history_job(CrossProcessId opera// Mirror its result here.// AD-HOC: A push or replace leaves a newer navigation's ID in place, as that navigation goes on and commits after// this one, and nulls the ID of the navigation it finalizes. See https://github.com/whatwg/html/issues/12581.- if (ongoing_navigation_is_traversal)+ if (ongoing_navigation_is_traversal) {navigable->set_ongoing_navigation_without_informing_navigation_api(HTML::LocalNavigable::Traversal::Tag);- else if (finalized_navigation_id.has_value() && navigable->ongoing_navigation() == *finalized_navigation_id)++ // AD-HOC: Only step 11 of navigate stops an iframe's lazy loading, but the update for a navigable's creation+ // also traverses a child that a repopulated document created, to the entry its restored nested history+ // holds. That entry's document takes the place of the one the iframe's src would load, so the iframe+ // stops lazy loading here too — or its lazy load resumption steps would navigate the restored+ // document away once the iframe is scrolled into view.+ if (!navigation_type.has_value()) {+ if (auto container = navigable->container(); container && container->is_html_iframe_element())+ static_cast<HTMLIFrameElement&>(*container).stop_lazy_loading();+ }+ } else if (finalized_navigation_id.has_value() && navigable->ongoing_navigation() == *finalized_navigation_id) {navigable->set_ongoing_navigation_without_informing_navigation_api(Empty {});+ }operation.changing_navigable_jobs.ensure(navigable_id);diff --git a/Tests/LibWeb/Text/data/lazy-iframe-below-the-fold.html b/Tests/LibWeb/Text/data/lazy-iframe-below-the-fold.htmlnew file mode 100644index 00000000000..f952d2ceb19--- /dev/null+++ b/Tests/LibWeb/Text/data/lazy-iframe-below-the-fold.html@@ -0,0 +1,17 @@+<!DOCTYPE html>+<body style="margin: 0">+<div style="height: 3000px"></div>+<script>+ document.write(`<iframe loading="lazy" src="${new URLSearchParams(location.search).get("src")}"></iframe>`);+ const iframe = document.querySelector("iframe");+ iframe.addEventListener("load", () => parent.postMessage("lazy iframe loaded", "*"));+ addEventListener("load", () => parent.postMessage("loaded", "*"));+ addEventListener("message", async event => {+ if (event.data !== "scroll to the lazy iframe")+ return;+ scrollTo(0, iframe.getBoundingClientRect().top + scrollY);+ for (let i = 0; i < 5; ++i)+ await new Promise(resolve => requestAnimationFrame(resolve));+ parent.postMessage("scrolled", "*");+ });+</script>diff --git a/Tests/LibWeb/Text/expected/SiteIsolation/iframe/lazy-iframe-restored-from-history-stops-lazy-loading.txt b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/lazy-iframe-restored-from-history-stops-lazy-loading.txtnew file mode 100644index 00000000000..731c83786fb--- /dev/null+++ b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/lazy-iframe-restored-from-history-stops-lazy-loading.txt@@ -0,0 +1,10 @@+WebContent#0+ iframe#0: local+ iframe#0: remote WebContent#1+WebContent#0+ iframe#0: local+ iframe#0: remote WebContent#1+loads after scrolling into view: 0+WebContent#0+ iframe#0: local+ iframe#0: remote WebContent#1diff --git a/Tests/LibWeb/Text/input/SiteIsolation/iframe/lazy-iframe-restored-from-history-stops-lazy-loading.html b/Tests/LibWeb/Text/input/SiteIsolation/iframe/lazy-iframe-restored-from-history-stops-lazy-loading.htmlnew file mode 100644index 00000000000..7189c2cf46d--- /dev/null+++ b/Tests/LibWeb/Text/input/SiteIsolation/iframe/lazy-iframe-restored-from-history-stops-lazy-loading.html@@ -0,0 +1,58 @@+<!DOCTYPE html>+<script src="../../include.js"></script>+<script>+ // Going back to a document repopulates it, and the session history restores the entry its lazy iframe had. That+ // entry's document loads at once, in another process, so scrolling the iframe into view later has nothing to load.+ promiseTest(async () => {+ const lazyUrl = new URL(+ await httpTestServer().createEcho("GET", "/lazy-iframe-restored-from-history", {+ status: 200,+ headers: { "Content-Type": "text/html" },+ body: "<!DOCTYPE html>lazy",+ })+ );+ lazyUrl.hostname = uniqueLocalhostHostname("lazy-iframe-restored-from-history");++ const nextMessage = expected =>+ new Promise(resolve => {+ addEventListener("message", function listener(event) {+ if (event.data !== expected)+ return;+ removeEventListener("message", listener);+ resolve();+ });+ });++ const iframe = document.createElement("iframe");+ let message = nextMessage("loaded");+ iframe.src = `../../../data/lazy-iframe-below-the-fold.html?src=${encodeURIComponent(lazyUrl)}`;+ document.body.appendChild(iframe);+ await message;++ message = nextMessage("lazy iframe loaded");+ iframe.contentWindow.postMessage("scroll to the lazy iframe", "*");+ await message;+ println(internals.dumpSiteIsolationProcessTree().trimEnd());++ message = nextMessage("message from test iframe (1)");+ iframe.src = "../../../data/iframe-test-content-1.html";+ await message;++ message = nextMessage("lazy iframe loaded");+ history.back();+ await message;+ println(internals.dumpSiteIsolationProcessTree().trimEnd());++ let loadsAfterScrolling = 0;+ addEventListener("message", event => {+ if (event.data === "lazy iframe loaded")+ ++loadsAfterScrolling;+ });+ message = nextMessage("scrolled");+ iframe.contentWindow.postMessage("scroll to the lazy iframe", "*");+ await message;+ await timeout(500);+ println(`loads after scrolling into view: ${loadsAfterScrolling}`);+ println(internals.dumpSiteIsolationProcessTree().trimEnd());+ });+</script>