mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 23:50:19 +00:00
LibWeb: Add an interface to be notified of Document state changes
Some HTML elements, e.g. HTMLMediaElement, need to take action when the document becomes inactive.
This commit is contained in:
parent
88b8969443
commit
f78eadf00f
Notes:
sideshowbarker
2024-07-16 20:42:33 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/f78eadf00f Pull-request: https://github.com/SerenityOS/serenity/pull/18638 Reviewed-by: https://github.com/awesomekling
6 changed files with 92 additions and 1 deletions
|
@ -126,9 +126,10 @@ set(SOURCES
|
|||
DOM/DOMTokenList.cpp
|
||||
DOM/DOMTokenList.idl
|
||||
DOM/Document.cpp
|
||||
DOM/DocumentLoading.cpp
|
||||
DOM/DocumentFragment.cpp
|
||||
DOM/DocumentLoadEventDelayer.cpp
|
||||
DOM/DocumentLoading.cpp
|
||||
DOM/DocumentObserver.cpp
|
||||
DOM/DocumentType.cpp
|
||||
DOM/Element.cpp
|
||||
DOM/ElementFactory.cpp
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include <LibWeb/DOM/DOMImplementation.h>
|
||||
#include <LibWeb/DOM/Document.h>
|
||||
#include <LibWeb/DOM/DocumentFragment.h>
|
||||
#include <LibWeb/DOM/DocumentObserver.h>
|
||||
#include <LibWeb/DOM/DocumentType.h>
|
||||
#include <LibWeb/DOM/Element.h>
|
||||
#include <LibWeb/DOM/ElementFactory.h>
|
||||
|
@ -371,6 +372,9 @@ void Document::visit_edges(Cell::Visitor& visitor)
|
|||
for (auto& node_iterator : m_node_iterators)
|
||||
visitor.visit(node_iterator);
|
||||
|
||||
for (auto& document_observer : m_document_observers)
|
||||
visitor.visit(document_observer);
|
||||
|
||||
for (auto& target : m_pending_scroll_event_targets)
|
||||
visitor.visit(target);
|
||||
for (auto& target : m_pending_scrollend_event_targets)
|
||||
|
@ -2032,6 +2036,18 @@ void Document::unregister_node_iterator(Badge<NodeIterator>, NodeIterator& node_
|
|||
VERIFY(was_removed);
|
||||
}
|
||||
|
||||
void Document::register_document_observer(Badge<DocumentObserver>, DocumentObserver& document_observer)
|
||||
{
|
||||
auto result = m_document_observers.set(document_observer);
|
||||
VERIFY(result == AK::HashSetResult::InsertedNewEntry);
|
||||
}
|
||||
|
||||
void Document::unregister_document_observer(Badge<DocumentObserver>, DocumentObserver& document_observer)
|
||||
{
|
||||
bool was_removed = m_document_observers.remove(document_observer);
|
||||
VERIFY(was_removed);
|
||||
}
|
||||
|
||||
void Document::increment_number_of_things_delaying_the_load_event(Badge<DocumentLoadEventDelayer>)
|
||||
{
|
||||
++m_number_of_things_delaying_the_load_event;
|
||||
|
@ -2437,6 +2453,11 @@ bool Document::is_allowed_to_use_feature(PolicyControlledFeature feature) const
|
|||
void Document::did_stop_being_active_document_in_browsing_context(Badge<HTML::BrowsingContext>)
|
||||
{
|
||||
tear_down_layout_tree();
|
||||
|
||||
for (auto& document_observer : m_document_observers) {
|
||||
if (document_observer->document_became_inactive)
|
||||
document_observer->document_became_inactive();
|
||||
}
|
||||
}
|
||||
|
||||
// https://w3c.github.io/editing/docs/execCommand/#querycommandsupported()
|
||||
|
|
|
@ -394,6 +394,9 @@ public:
|
|||
void register_node_iterator(Badge<NodeIterator>, NodeIterator&);
|
||||
void unregister_node_iterator(Badge<NodeIterator>, NodeIterator&);
|
||||
|
||||
void register_document_observer(Badge<DocumentObserver>, DocumentObserver&);
|
||||
void unregister_document_observer(Badge<DocumentObserver>, DocumentObserver&);
|
||||
|
||||
template<typename Callback>
|
||||
void for_each_node_iterator(Callback callback)
|
||||
{
|
||||
|
@ -588,6 +591,8 @@ private:
|
|||
|
||||
HashTable<JS::GCPtr<NodeIterator>> m_node_iterators;
|
||||
|
||||
HashTable<JS::NonnullGCPtr<DocumentObserver>> m_document_observers;
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/dom.html#is-initial-about:blank
|
||||
bool m_is_initial_about_blank { false };
|
||||
|
||||
|
|
32
Userland/Libraries/LibWeb/DOM/DocumentObserver.cpp
Normal file
32
Userland/Libraries/LibWeb/DOM/DocumentObserver.cpp
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibJS/Runtime/Realm.h>
|
||||
#include <LibWeb/DOM/Document.h>
|
||||
#include <LibWeb/DOM/DocumentObserver.h>
|
||||
|
||||
namespace Web::DOM {
|
||||
|
||||
DocumentObserver::DocumentObserver(JS::Realm& realm, DOM::Document& document)
|
||||
: Bindings::PlatformObject(realm)
|
||||
, m_document(document)
|
||||
{
|
||||
m_document->register_document_observer({}, *this);
|
||||
}
|
||||
|
||||
void DocumentObserver::visit_edges(Cell::Visitor& visitor)
|
||||
{
|
||||
Base::visit_edges(visitor);
|
||||
visitor.visit(m_document);
|
||||
}
|
||||
|
||||
void DocumentObserver::finalize()
|
||||
{
|
||||
Base::finalize();
|
||||
m_document->unregister_document_observer({}, *this);
|
||||
}
|
||||
|
||||
}
|
31
Userland/Libraries/LibWeb/DOM/DocumentObserver.h
Normal file
31
Userland/Libraries/LibWeb/DOM/DocumentObserver.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibJS/Forward.h>
|
||||
#include <LibJS/SafeFunction.h>
|
||||
#include <LibWeb/Bindings/PlatformObject.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
|
||||
namespace Web::DOM {
|
||||
|
||||
class DocumentObserver final : public Bindings::PlatformObject {
|
||||
WEB_PLATFORM_OBJECT(DocumentObserver, Bindings::PlatformObject);
|
||||
|
||||
public:
|
||||
JS::SafeFunction<void()> document_became_inactive;
|
||||
|
||||
private:
|
||||
explicit DocumentObserver(JS::Realm&, DOM::Document&);
|
||||
|
||||
virtual void visit_edges(Cell::Visitor&) override;
|
||||
virtual void finalize() override;
|
||||
|
||||
JS::NonnullGCPtr<DOM::Document> m_document;
|
||||
};
|
||||
|
||||
}
|
|
@ -189,6 +189,7 @@ class CustomEvent;
|
|||
class Document;
|
||||
class DocumentFragment;
|
||||
class DocumentLoadEventDelayer;
|
||||
class DocumentObserver;
|
||||
class DocumentType;
|
||||
class DOMEventListener;
|
||||
class DOMImplementation;
|
||||
|
|
Loading…
Reference in a new issue