LibWeb: Add a DocumentObserver hook to be notified of readyState changes

This commit is contained in:
Timothy Flynn 2024-10-25 10:20:55 -04:00 committed by Andreas Kling
parent 44b1c4f2b5
commit 0bbe836f8c
Notes: github-actions[bot] 2024-10-26 09:27:09 +00:00
3 changed files with 18 additions and 2 deletions

View file

@ -2392,6 +2392,11 @@ void Document::update_readiness(HTML::DocumentReadyState readiness_value)
m_needs_to_call_page_did_load = true;
}
}
for (auto document_observer : m_document_observers) {
if (document_observer->document_readiness_observer())
document_observer->document_readiness_observer()->function()(m_readiness);
}
}
// https://html.spec.whatwg.org/multipage/dom.html#dom-document-lastmodified

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2023-2024, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -25,6 +25,7 @@ void DocumentObserver::visit_edges(Cell::Visitor& visitor)
visitor.visit(m_document);
visitor.visit(m_document_became_inactive);
visitor.visit(m_document_completely_loaded);
visitor.visit(m_document_readiness_observer);
}
void DocumentObserver::finalize()
@ -43,4 +44,9 @@ void DocumentObserver::set_document_completely_loaded(Function<void()> callback)
m_document_completely_loaded = JS::create_heap_function(vm().heap(), move(callback));
}
void DocumentObserver::set_document_readiness_observer(Function<void(HTML::DocumentReadyState)> callback)
{
m_document_readiness_observer = JS::create_heap_function(vm().heap(), move(callback));
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2023-2024, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -11,6 +11,7 @@
#include <LibJS/SafeFunction.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/DocumentReadyState.h>
namespace Web::DOM {
@ -25,6 +26,9 @@ public:
[[nodiscard]] JS::GCPtr<JS::HeapFunction<void()>> document_completely_loaded() const { return m_document_completely_loaded; }
void set_document_completely_loaded(Function<void()>);
[[nodiscard]] JS::GCPtr<JS::HeapFunction<void(HTML::DocumentReadyState)>> document_readiness_observer() const { return m_document_readiness_observer; }
void set_document_readiness_observer(Function<void(HTML::DocumentReadyState)>);
private:
explicit DocumentObserver(JS::Realm&, DOM::Document&);
@ -34,6 +38,7 @@ private:
JS::NonnullGCPtr<DOM::Document> m_document;
JS::GCPtr<JS::HeapFunction<void()>> m_document_became_inactive;
JS::GCPtr<JS::HeapFunction<void()>> m_document_completely_loaded;
JS::GCPtr<JS::HeapFunction<void(HTML::DocumentReadyState)>> m_document_readiness_observer;
};
}