Truly independent web browser
LibWeb/HTML: Track adopted media elements in their new page
Luke Wilde6d95f88fba72
diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Libraries/LibWeb/HTML/HTMLMediaElement.cppindex 7fb83633e29..4c793afd92f 100644--- a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp@@ -420,6 +420,12 @@ void HTMLMediaElement::adopted_from(DOM::Document& old_document)// second fetch on top of it.m_document_observer->retarget_for_adoption(document());+ // The element's page keeps track of it, and is asked below whether the element's document is playing audio.+ if (&old_document.page() != &document().page()) {+ old_document.page().unregister_media_element({}, unique_id());+ document().page().register_media_element({}, unique_id());+ }+// An element playing audio takes the timer exemption it earns its document along with it (see// Document::is_playing_audio()).if (m_is_playing_audio) {diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cppindex 69f825360c1..fd8e5ba1fa8 100644--- a/Libraries/LibWeb/Internals/Internals.cpp+++ b/Libraries/LibWeb/Internals/Internals.cpp@@ -2117,6 +2117,11 @@ bool Internals::media_element_is_playing_audio(HTML::HTMLMediaElement& element)return element.is_playing_audio();}+bool Internals::document_is_playing_audio(DOM::Document& document)+{+ return document.is_playing_audio();+}+bool Internals::media_element_video_sink_is_ticking(HTML::HTMLMediaElement& element){return element.m_video_sink_is_ticking;diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.hindex a451d6babce..b5dacb22763 100644--- a/Libraries/LibWeb/Internals/Internals.h+++ b/Libraries/LibWeb/Internals/Internals.h@@ -319,6 +319,7 @@ public: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&);+ bool document_is_playing_audio(DOM::Document&);bool media_element_video_sink_is_ticking(HTML::HTMLMediaElement&);void set_media_element_ready_state(HTML::HTMLMediaElement&, u16 ready_state);void set_media_element_paused(HTML::HTMLMediaElement&, bool paused);diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idlindex e36e1ecbdb2..2ecb549c285 100644--- a/Libraries/LibWeb/Internals/Internals.idl+++ b/Libraries/LibWeb/Internals/Internals.idl@@ -393,6 +393,7 @@ interface Internals {object imageAnimationStateForURL(USVString url);boolean mediaElementIsFetching(HTMLMediaElement element);boolean mediaElementIsPlayingAudio(HTMLMediaElement element);+ boolean documentIsPlayingAudio(Document document);boolean mediaElementVideoSinkIsTicking(HTMLMediaElement element);undefined setMediaElementReadyState(HTMLMediaElement element, unsigned short readyState);undefined setMediaElementPaused(HTMLMediaElement element, boolean paused);diff --git a/Tests/LibWeb/Text/expected/HTML/HTMLMediaElement-adoption-into-another-page.txt b/Tests/LibWeb/Text/expected/HTML/HTMLMediaElement-adoption-into-another-page.txtnew file mode 100644index 00000000000..64059b372fc--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/HTMLMediaElement-adoption-into-another-page.txt@@ -0,0 +1,3 @@+before adoption: opener playing audio: true, popup playing audio: false+adopted into the popup: opener playing audio: false, popup playing audio: true+adopted back: opener playing audio: true, popup playing audio: falsediff --git a/Tests/LibWeb/Text/input/HTML/HTMLMediaElement-adoption-into-another-page.html b/Tests/LibWeb/Text/input/HTML/HTMLMediaElement-adoption-into-another-page.htmlnew file mode 100644index 00000000000..5ffad5653cc--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/HTMLMediaElement-adoption-into-another-page.html@@ -0,0 +1,26 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>internals.setAutoplayPolicy("allow-audio-and-video");</script>+<video loop src="../../../Assets/test-webm.webm"></video>+<script>+ promiseTest(async () => {+ internals.setTestTimeout(15_000);+ const video = document.querySelector("video");+ const popup = window.open("about:blank", "_blank");+ const report = label => {+ println(`${label}: opener playing audio: ${internals.documentIsPlayingAudio(document)}, popup playing audio: ${internals.documentIsPlayingAudio(popup.document)}`);+ };++ await video.play();+ report("before adoption");++ popup.document.body.appendChild(popup.document.adoptNode(video));+ report("adopted into the popup");++ document.body.appendChild(document.adoptNode(video));+ report("adopted back");++ video.pause();+ popup.close();+ });+</script>