LibWeb/DOM: Support changing document to observe in DocumentObserver

This commit is contained in:
Shannon Booth 2025-06-23 15:14:56 +12:00 committed by Jelle Raaijmakers
commit 3383a781f6
Notes: github-actions[bot] 2025-06-24 07:57:45 +00:00
2 changed files with 14 additions and 3 deletions

View file

@ -12,7 +12,7 @@ namespace Web::DOM {
GC_DEFINE_ALLOCATOR(DocumentObserver);
DocumentObserver::DocumentObserver(JS::Realm& realm, DOM::Document& document)
DocumentObserver::DocumentObserver(JS::Realm& realm, Document& document)
: Bindings::PlatformObject(realm)
, m_document(document)
{
@ -37,6 +37,14 @@ void DocumentObserver::finalize()
m_document->unregister_document_observer({}, *this);
}
void DocumentObserver::set_document(GC::Ref<Document> document)
{
VERIFY(document != m_document);
m_document->unregister_document_observer({}, *this);
m_document = document;
document->register_document_observer({}, *this);
}
void DocumentObserver::set_document_became_active(Function<void()> callback)
{
if (callback)

View file

@ -38,13 +38,16 @@ public:
[[nodiscard]] GC::Ptr<GC::Function<void(bool)>> document_page_showing_observer() const { return m_document_page_showing_observer; }
void set_document_page_showing_observer(Function<void(bool)>);
GC::Ref<Document> document() { return m_document; }
void set_document(GC::Ref<Document>);
private:
explicit DocumentObserver(JS::Realm&, DOM::Document&);
explicit DocumentObserver(JS::Realm&, Document&);
virtual void visit_edges(Cell::Visitor&) override;
virtual void finalize() override;
GC::Ref<DOM::Document> m_document;
GC::Ref<Document> m_document;
GC::Ptr<GC::Function<void()>> m_document_became_active;
GC::Ptr<GC::Function<void()>> m_document_became_inactive;
GC::Ptr<GC::Function<void()>> m_document_completely_loaded;