Truly independent web browser
LibWeb: Let a media element of an unloaded document be collected
Jelle Raaijmakersa7028c2d3abf
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.hindex 9b26d0d581f..71289bb4065 100644--- a/Libraries/LibWeb/DOM/Document.h+++ b/Libraries/LibWeb/DOM/Document.h@@ -926,6 +926,7 @@ public:return update_is_fully_active();}bool is_active() const;+ bool observers_consider_fully_active() const { return m_observers_consider_document_fully_active; }// To call whenever anything that decides if a document is fully active changes: a navigable's parent, its being// destroyed, or its active document, or a document's navigable or unload counter.diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Libraries/LibWeb/HTML/HTMLMediaElement.cppindex 2cd8e26cc56..2e4a67bf10c 100644--- a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp@@ -3515,7 +3515,10 @@ bool HTMLMediaElement::should_be_kept_alive() const// element is in a state where no further audio could ever be played by that element may the element be garbage// collected.// NB: Playback stops while the node document is not fully active, and resumes only if something reaches the element.- if (!document().is_fully_active())+ // Document observers learn that the document stopped being fully active while it unloads, when it is still its+ // navigable's active document, and pause the element then. Nothing reconsiders the element after the unload, so+ // it must stop keeping itself alive as soon as the observers are told.+ if (!document().is_fully_active() || !document().observers_consider_fully_active())return false;// NB: The load may still fire events, and reach the readyState at which the element starts playing.diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.iniindex 19079a68fd9..68ce0d997b7 100644--- a/Tests/LibWeb/TestConfig.ini+++ b/Tests/LibWeb/TestConfig.ini@@ -140,6 +140,7 @@ Crash/HTML/media-element-load-with-pending-source-buffer-append.htmlText/input/HTML/HTMLMediaElement-bodyless-response.htmlText/input/HTML/HTMLMediaElement-failed-range-response-after-metadata.htmlText/input/HTML/HTMLMediaElement-gc-during-download.html+Text/input/HTML/HTMLMediaElement-gc-during-download-in-replaced-document.htmlText/input/HTML/HTMLMediaElement-gc-during-load.htmlText/input/HTML/HTMLMediaElement-preload-metadata-suspends-download.htmlText/input/HTML/HTMLMediaElement-seek-past-fetched-data-after-network-error.htmldiff --git a/Tests/LibWeb/Text/expected/HTML/HTMLMediaElement-gc-during-download-in-replaced-document.txt b/Tests/LibWeb/Text/expected/HTML/HTMLMediaElement-gc-during-download-in-replaced-document.txtnew file mode 100644index 00000000000..24590b8b796--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/HTMLMediaElement-gc-during-download-in-replaced-document.txt@@ -0,0 +1,2 @@+networkState before navigating away: 2+replaced document collected: truediff --git a/Tests/LibWeb/Text/input/HTML/HTMLMediaElement-gc-during-download-in-replaced-document.html b/Tests/LibWeb/Text/input/HTML/HTMLMediaElement-gc-during-download-in-replaced-document.htmlnew file mode 100644index 00000000000..9ece137f6f6--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/HTMLMediaElement-gc-during-download-in-replaced-document.html@@ -0,0 +1,52 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ promiseTest(async () => {+ const server = httpTestServer();+ const tone = new Uint8Array(await (await fetch(server.getStaticURL("Assets/tone.mp3"))).arrayBuffer());+ const audioFrames = tone.subarray(226);+ const repetitions = Math.ceil((64 * 1024) / audioFrames.length);+ const media = new Uint8Array(audioFrames.length * repetitions);+ for (let i = 0; i < repetitions; i++)+ media.set(audioFrames, i * audioFrames.length);+ let binary = "";+ for (const byte of media)+ binary += String.fromCharCode(byte);+ const token = `gc-during-download-in-replaced-document-${crypto.randomUUID()}`;+ const mediaURL = new URL(await server.createEcho("GET", `/${token}.mp3`, {+ status: 200,+ headers: { "Content-Type": "audio/mpeg", "Content-Length": String(media.length), "Cache-Control": "no-store" },+ body: btoa(binary),+ body_encoding: "base64",+ }));+ mediaURL.search = `?chunks=16384&chunk_unblock_tokens=${token}`;+ const unblockURL = new URL(`/unblock/${token}`, mediaURL);+ unblockURL.hostname = uniqueLocalhostHostname("gc-during-download-in-replaced-document-unblock");++ const iframe = document.createElement("iframe");+ document.body.append(iframe);+ const navigate = html =>+ new Promise(resolve => {+ iframe.addEventListener("load", resolve, { once: true });+ iframe.srcdoc = html;+ });++ await navigate("<!DOCTYPE html>");+ const weakDocument = await (async () => {+ await navigate(`<!DOCTYPE html><audio preload="auto" src="${mediaURL.href}"></audio>`);+ const replacedDocument = iframe.contentDocument;+ const audio = replacedDocument.querySelector("audio");+ println(`networkState before navigating away: ${audio.networkState}`);+ replacedDocument.preserveWrapper = true;+ const weakDocument = new WeakRef(replacedDocument);+ await navigate("<!DOCTYPE html>");+ return weakDocument;+ })();++ await collectGarbageUntilCleared([weakDocument]);+ println(`replaced document collected: ${weakDocument.deref() === undefined}`);++ await fetch(unblockURL);+ iframe.remove();+ });+</script>