Skip to content

Truly independent web browser

LibWeb: Don’t read the layout of a document a rendering update skipped

sideshowbarker043edc87a8a6
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
index bebad027c6a..e75f0abad2c 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
@@ -1430,7 +1430,7 @@ void EventLoop::update_the_rendering_after_style_and_layout(Vector<GC::Root<DOM:
if (synced_pages.contains_slow(GC::Ref { document->page() }))
continue;
synced_pages.append(document->page());
- document->page().sync_media_element_video_sink_ticking();
+ document->page().sync_media_element_video_sink_ticking(docs);
}
// FIXME: 20. For each doc of docs, record rendering time for doc given unsafeStyleAndLayoutStartTime.
diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
index b4822837732..0b30e963fcc 100644
--- a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp
@@ -3017,7 +3017,7 @@ void HTMLMediaElement::update_ready_state()
}
}
-bool HTMLMediaElement::video_sink_should_tick() const
+bool HTMLMediaElement::video_sink_should_tick(DocumentWasLaidOut document_was_laid_out) const
{
Layout::ForcedReadScope read { document() };
if (m_video_frame_was_recently_captured)
@@ -3029,17 +3029,22 @@ bool HTMLMediaElement::video_sink_should_tick() const
return true;
if (document().visibility_state() != VisibilityState::Visible)
return false;
+ // A document that the rendering update didn't lay out isn't rendered (one in a display: none iframe, e.g.), so
+ // nothing of it is painted. Its layout isn't read either: A style update may be flying for it, which can make that
+ // layout stale at any moment.
+ if (document_was_laid_out == DocumentWasLaidOut::No)
+ return false;
auto const* layout_node = this->layout_node(read);
return layout_node && Painting::has_committed_box(*layout_node) && Painting::is_visible(*layout_node);
}
-void HTMLMediaElement::sync_video_sink_ticking() const
+void HTMLMediaElement::sync_video_sink_ticking(DocumentWasLaidOut document_was_laid_out) const
{
auto handle = video_sink_handle();
if (!handle.has_value())
return;
- auto should_tick = video_sink_should_tick();
+ auto should_tick = video_sink_should_tick(document_was_laid_out);
if (m_video_sink_is_ticking == should_tick)
return;
m_video_sink_is_ticking = should_tick;
diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.h b/Libraries/LibWeb/HTML/HTMLMediaElement.h
index 784609c7991..02bbca0e8b0 100644
--- a/Libraries/LibWeb/HTML/HTMLMediaElement.h
+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.h
@@ -175,7 +175,11 @@ public:
void set_selected_video_track(Badge<VideoTrack>, GC::Ptr<HTML::VideoTrack> video_track);
void add_current_video_sink();
- void sync_video_sink_ticking() const;
+ enum class DocumentWasLaidOut {
+ No,
+ Yes,
+ };
+ void sync_video_sink_ticking(DocumentWasLaidOut = DocumentWasLaidOut::Yes) const;
void video_sink_ticking_inputs_changed() const;
void detach_video_sink_edge();
@@ -328,7 +332,7 @@ private:
void update_volume();
void attach_selected_video_track_sink(Media::Track const&);
- bool video_sink_should_tick() const;
+ bool video_sink_should_tick(DocumentWasLaidOut) const;
virtual bool is_shown_elsewhere() const { return false; }
virtual HTMLMediaElement& media_element_for_controls() { return *this; }
diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cpp
index 1593b879bce..88dc5d6e11d 100644
--- a/Libraries/LibWeb/Page/Page.cpp
+++ b/Libraries/LibWeb/Page/Page.cpp
@@ -6,6 +6,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/AnyOf.h>
#include <AK/SourceLocation.h>
#include <AK/Utf16StringBuilder.h>
#include <LibAudio/RecordStream.h>
@@ -1902,10 +1903,11 @@ bool Page::has_media_element_playing_audio(DOM::Document const& document) const
return false;
}
-void Page::sync_media_element_video_sink_ticking()
+void Page::sync_media_element_video_sink_ticking(ReadonlySpan<GC::Root<DOM::Document>> laid_out_documents)
{
for_each_media_element([&](auto& media_element) {
- media_element.sync_video_sink_ticking();
+ auto document_was_laid_out = any_of(laid_out_documents, [&](auto const& document) { return document.ptr() == &media_element.document(); });
+ media_element.sync_video_sink_ticking(document_was_laid_out ? HTML::HTMLMediaElement::DocumentWasLaidOut::Yes : HTML::HTMLMediaElement::DocumentWasLaidOut::No);
});
}
diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.h
index a9faee1500a..81fa8c13c3b 100644
--- a/Libraries/LibWeb/Page/Page.h
+++ b/Libraries/LibWeb/Page/Page.h
@@ -420,7 +420,7 @@ public:
bool has_media_element_playing_audio(DOM::Document const&) const;
Vector<GC::Root<HTML::HTMLVideoElement>> video_elements_of(DOM::Document const&);
- void sync_media_element_video_sink_ticking();
+ void sync_media_element_video_sink_ticking(ReadonlySpan<GC::Root<DOM::Document>> laid_out_documents);
void restore_all_media_element_video_sinks();
void detach_all_media_element_video_sinks_after_compositor_lost();
diff --git a/Tests/LibWeb/Crash/HTML/video-in-iframe-hidden-while-playing.html b/Tests/LibWeb/Crash/HTML/video-in-iframe-hidden-while-playing.html
new file mode 100644
index 00000000000..8bbd21e8b19
--- /dev/null
+++ b/Tests/LibWeb/Crash/HTML/video-in-iframe-hidden-while-playing.html
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<html class="test-wait">
+<iframe id="frame" srcdoc="<video muted loop src='../../Assets/solid-blue-160x120.webm'></video><div id=text>x</div>"></iframe>
+<script>
+ // A display: none iframe's document is left out of rendering updates, so its layout goes stale once its DOM changes,
+ // while the video in it keeps playing.
+ internals.setAutoplayPolicy("allow-audio-and-video");
+ const frame = document.getElementById("frame");
+ const animationFrame = () => new Promise(resolve => requestAnimationFrame(resolve));
+ frame.onload = async () => {
+ const video = frame.contentDocument.querySelector("video");
+ await video.play();
+ await new Promise(resolve => setTimeout(resolve, 100));
+ frame.style.display = "none";
+ await animationFrame();
+ for (let i = 0; i < 10; ++i) {
+ frame.contentDocument.getElementById("text").textContent += "y";
+ await new Promise(resolve => setTimeout(resolve, 16));
+ }
+ document.documentElement.classList.remove("test-wait");
+ };
+</script>