LibWeb: Unregister ResizeObserver from Document when it has no targets

According to the spec, `ResizeObserver` needs to live for as long as
it's referenced from script or has observation targets. With this change
we make sure that `ResizeObserver` is unregistered from the `Document`
when it has no target.

Fixes GC leak that caused us to keep all resize observers alive until
document they belong to is destroyed.
This commit is contained in:
Aliaksandr Kalenik
2025-07-30 00:25:12 +02:00
committed by Andreas Kling
parent 40fd2643cc
commit 4cbf47dcd2
Notes: github-actions[bot] 2025-07-29 22:56:18 +00:00
4 changed files with 53 additions and 20 deletions

View File

@@ -1,6 +1,6 @@
/*
* Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
* Copyright (c) 2024-2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -44,6 +44,8 @@ private:
virtual void visit_edges(JS::Cell::Visitor&) override;
virtual void finalize() override;
void unregister_observer_if_needed();
GC::Ptr<WebIDL::CallbackType> m_callback;
Vector<GC::Ref<ResizeObservation>> m_observation_targets;
Vector<GC::Ref<ResizeObservation>> m_active_targets;
@@ -51,6 +53,11 @@ private:
// AD-HOC: This is the document where we've registered the observer.
WeakPtr<DOM::Document> m_document;
IntrusiveListNode<ResizeObserver> m_list_node;
public:
using ResizeObserversList = IntrusiveList<&ResizeObserver::m_list_node>;
};
}