Truly independent web browser
LibWeb: Hold the largest contentful paint element weakly
Shannon Boothf2d15ff68b19
diff --git a/Libraries/LibWeb/LargestContentfulPaint/LargestContentfulPaint.cpp b/Libraries/LibWeb/LargestContentfulPaint/LargestContentfulPaint.cppindex 2da631b7164..cce0b790914 100644--- a/Libraries/LibWeb/LargestContentfulPaint/LargestContentfulPaint.cpp+++ b/Libraries/LibWeb/LargestContentfulPaint/LargestContentfulPaint.cpp@@ -61,17 +61,12 @@ HighResolutionTime::DOMHighResTimeStamp LargestContentfulPaint::render_time() coGC::Ptr<DOM::Element> LargestContentfulPaint::element() const{// 1. If this's element is not exposed for paint timing given null, return null.- if (!m_element || !PaintTiming::is_exposed_for_paint_timing(*m_element, nullptr))+ auto element = m_element.ptr();+ if (!element || !PaintTiming::is_exposed_for_paint_timing(*element, nullptr))return nullptr;// 2. Return this's element.- return m_element;-}--void LargestContentfulPaint::visit_edges(GC::Cell::Visitor& visitor)-{- Base::visit_edges(visitor);- visitor.visit(m_element);+ return element;}// https://w3c.github.io/largest-contentful-paint/#effective-visual-size-result@@ -343,7 +338,7 @@ static GC::Ref<LargestContentfulPaint> create_a_largest_contentful_paint_entry(Pauto size = static_cast<WebIDL::UnsignedLong>(clamp(candidate.size, 0.0, static_cast<double>(NumericLimits<WebIDL::UnsignedLong>::max())));// 4. Return entry.- return LargestContentfulPaint::create(paint_timing_info, size, move(url), move(id), candidate.load_time, candidate.element);+ return LargestContentfulPaint::create(paint_timing_info, size, move(url), move(id), candidate.load_time, *candidate.element.ptr());}// https://w3c.github.io/largest-contentful-paint/#report-largest-contentful-paintdiff --git a/Libraries/LibWeb/LargestContentfulPaint/LargestContentfulPaint.h b/Libraries/LibWeb/LargestContentfulPaint/LargestContentfulPaint.hindex 395d4b79487..e6de59c6185 100644--- a/Libraries/LibWeb/LargestContentfulPaint/LargestContentfulPaint.h+++ b/Libraries/LibWeb/LargestContentfulPaint/LargestContentfulPaint.h@@ -50,8 +50,6 @@ public:private:LargestContentfulPaint(PaintTiming::PaintTimingInfo, WebIDL::UnsignedLong size, Utf16String url, Utf16String id, HighResolutionTime::DOMHighResTimeStamp load_time, GC::Ref<DOM::Element>);- virtual void visit_edges(GC::Cell::Visitor&) override;-// https://w3c.github.io/largest-contentful-paint/#largestcontentfulpaint-sizeWebIDL::UnsignedLong m_size { 0 };@@ -65,7 +63,8 @@ private:Utf16String m_url;// https://w3c.github.io/largest-contentful-paint/#largestcontentfulpaint-element- GC::Ptr<DOM::Element> m_element;+ // NB: Held weakly, as the element getter returns null for an element that is not connected.+ GC::Weak<DOM::Element> m_element;};// https://w3c.github.io/largest-contentful-paint/#report-largest-contentful-paintdiff --git a/Libraries/LibWeb/PaintTiming/DocumentPaintTimingState.h b/Libraries/LibWeb/PaintTiming/DocumentPaintTimingState.hindex 97a25a85546..20cfc2e051c 100644--- a/Libraries/LibWeb/PaintTiming/DocumentPaintTimingState.h+++ b/Libraries/LibWeb/PaintTiming/DocumentPaintTimingState.h@@ -13,6 +13,7 @@#include <LibGC/ConservativeHashMap.h>#include <LibGC/Ptr.h>#include <LibGC/RootVector.h>+#include <LibGC/Weak.h>#include <LibWeb/Forward.h>#include <LibWebCommon/HighResolutionTime/DOMHighResTimeStamp.h>#include <LibWebCommon/PixelUnits.h>@@ -44,7 +45,8 @@ struct PendingImageRecord {// https://w3c.github.io/largest-contentful-paint/#largest-contentful-paint-candidatestruct LargestContentfulPaintCandidate {// element, an element- GC::Ref<DOM::Element> element;+ // NB: Held weakly, as only the size of the current candidate is read once its element is removed.+ GC::Weak<DOM::Element> element;// size, a numberdouble size { 0 };diff --git a/Libraries/LibWeb/PaintTiming/PaintTiming.cpp b/Libraries/LibWeb/PaintTiming/PaintTiming.cppindex daec4e4ac57..500a51aaa84 100644--- a/Libraries/LibWeb/PaintTiming/PaintTiming.cpp+++ b/Libraries/LibWeb/PaintTiming/PaintTiming.cpp@@ -658,8 +658,6 @@ void DocumentPaintTimingState::visit_edges(GC::Cell::Visitor& visitor)for (auto& record : images_pending_rendering)visitor.visit(record.element);visitor.visit(text_nodes_pending_first_paint);- if (current_largest_contentful_paint_candidate.has_value())- visitor.visit(current_largest_contentful_paint_candidate->element);}}diff --git a/Tests/LibWeb/Text/expected/LargestContentfulPaint/removed-candidate-is-collectable.txt b/Tests/LibWeb/Text/expected/LargestContentfulPaint/removed-candidate-is-collectable.txtnew file mode 100644index 00000000000..dba2a525581--- /dev/null+++ b/Tests/LibWeb/Text/expected/LargestContentfulPaint/removed-candidate-is-collectable.txt@@ -0,0 +1,3 @@+reported: candidate+removed candidate collected: true+entry element: nulldiff --git a/Tests/LibWeb/Text/input/LargestContentfulPaint/removed-candidate-is-collectable.html b/Tests/LibWeb/Text/input/LargestContentfulPaint/removed-candidate-is-collectable.htmlnew file mode 100644index 00000000000..089ebf4a513--- /dev/null+++ b/Tests/LibWeb/Text/input/LargestContentfulPaint/removed-candidate-is-collectable.html@@ -0,0 +1,33 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+promiseTest(async () => {+ async function paintAndRemoveCandidate() {+ const candidate = document.createElement("div");+ candidate.id = "candidate";+ candidate.style.fontSize = "48px";+ candidate.textContent = "Largest contentful paint";+ const reported = new Promise(resolve => {+ new PerformanceObserver((list, observer) => {+ const entry = list.getEntries().find(entry => entry.element === candidate);+ if (!entry)+ return;+ observer.disconnect();+ resolve(entry);+ }).observe({ type: "largest-contentful-paint", buffered: true });+ });+ document.body.append(candidate);+ const entry = await reported;+ println(`reported: ${entry.id}`);+ candidate.remove();+ // Preserve the wrapper so collection checks the element, not just a disposable JS wrapper.+ candidate.gcSentinel = true;+ return { entry, weakCandidate: new WeakRef(candidate) };+ }+ const { entry, weakCandidate } = await paintAndRemoveCandidate();++ await collectGarbageUntilCleared([weakCandidate]);+ println(`removed candidate collected: ${weakCandidate.deref() === undefined}`);+ println(`entry element: ${entry.element}`);+});+</script>