LibWeb: Move viewport subscriptions from BrowsingContext to Document

With this change, elements that want to receive viewport rect updates
will need to register on document instead of the browsing context.

This change solves the problem where a browsing context for a document
is guaranteed to exist only while the document is active so browsing
context might not exit by the time DOM node that want to register is
constructed.

This is a part of preparation work before switching to navigables where
this issue becomes more visible.
This commit is contained in:
Aliaksandr Kalenik 2023-08-23 18:58:42 +02:00 committed by Andreas Kling
parent 48e9097aa4
commit 5ff7448fee
Notes: sideshowbarker 2024-07-17 04:49:48 +09:00
10 changed files with 66 additions and 61 deletions

View file

@ -39,8 +39,7 @@ HTMLImageElement::HTMLImageElement(DOM::Document& document, DOM::QualifiedName q
m_animation_timer = Core::Timer::try_create().release_value_but_fixme_should_propagate_errors();
m_animation_timer->on_timeout = [this] { animate(); };
if (auto* browsing_context = document.browsing_context())
browsing_context->register_viewport_client(*this);
document.register_viewport_client(*this);
}
HTMLImageElement::~HTMLImageElement() = default;
@ -48,8 +47,7 @@ HTMLImageElement::~HTMLImageElement() = default;
void HTMLImageElement::finalize()
{
Base::finalize();
if (auto* browsing_context = document().browsing_context())
browsing_context->unregister_viewport_client(*this);
document().unregister_viewport_client(*this);
}
void HTMLImageElement::initialize(JS::Realm& realm)
@ -60,6 +58,12 @@ void HTMLImageElement::initialize(JS::Realm& realm)
m_current_request = ImageRequest::create(realm, *document().page());
}
void HTMLImageElement::adopted_from(DOM::Document& old_document)
{
old_document.unregister_viewport_client(*this);
document().register_viewport_client(*this);
}
void HTMLImageElement::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
@ -630,7 +634,7 @@ void HTMLImageElement::add_callbacks_to_image_request(JS::NonnullGCPtr<ImageRequ
});
}
void HTMLImageElement::browsing_context_did_set_viewport_rect(CSSPixelRect const& viewport_rect)
void HTMLImageElement::did_set_viewport_rect(CSSPixelRect const& viewport_rect)
{
if (viewport_rect.size() == m_last_seen_viewport_size)
return;