Skip to content

Truly independent web browser

LibWeb: Keep whether a document is fully active until that can change

sideshowbarkerb01eaae743d9
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 9408213df04..17979baf4f2 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -4919,7 +4919,16 @@ void Document::set_referrer(Utf16String referrer)
}
// https://html.spec.whatwg.org/multipage/document-sequences.html#fully-active
-bool Document::is_fully_active() const
+u64 Document::s_full_activity_epoch = 1;
+
+bool Document::update_is_fully_active() const
+{
+ m_is_fully_active = compute_is_fully_active();
+ m_full_activity_epoch = s_full_activity_epoch;
+ return m_is_fully_active;
+}
+
+bool Document::compute_is_fully_active() const
{
// A Document d is said to be fully active when d is the active document of a navigable navigable, and either
// navigable is a top-level traversable or navigable's container document is fully active.
@@ -6223,6 +6232,7 @@ void Document::unload(GC::Ptr<Document> new_document)
// 8. Increase oldDocument's unload counter by 1.
m_unload_counter += 1;
+ invalidate_full_activity();
// 9. If intendToKeepInBfcache is false, then set oldDocument's salvageable state to false.
if (!intend_to_store_in_bfcache)
@@ -6299,6 +6309,7 @@ void Document::unload(GC::Ptr<Document> new_document)
// 21. Decrease oldDocument's unload counter by 1.
m_unload_counter -= 1;
+ invalidate_full_activity();
// 22. If newDocument is given, newDocument's was created via cross-origin redirects is false, and newDocument's
// origin is the same as oldDocument's origin, then set newDocument's previous document unload timing to
@@ -10219,6 +10230,7 @@ void Document::set_navigable(GC::Ptr<HTML::LocalNavigable> navigable)
GC::Ptr<Page> previous_page = m_navigable ? &m_navigable->page() : nullptr;
m_navigable = navigable.ptr();
+ invalidate_full_activity();
HTML::main_thread_event_loop().document_navigable_did_change({});
if (previous_page)
@@ -10671,6 +10683,7 @@ Document::StepsToFireBeforeunloadResult Document::steps_to_fire_beforeunload(boo
// 2. Increase the document's unload counter by 1.
m_unload_counter++;
+ invalidate_full_activity();
// 3. Increase document's relevant agent's event loop's termination nesting level by 1.
auto& event_loop = *HTML::relevant_agent(*this).event_loop;
@@ -10715,6 +10728,7 @@ Document::StepsToFireBeforeunloadResult Document::steps_to_fire_beforeunload(boo
// 7. Decrease document's unload counter by 1.
m_unload_counter--;
+ invalidate_full_activity();
// 8. Return (unloadPromptShown, unloadPromptCanceled).
return { unload_prompt_shown, unload_prompt_canceled };
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index b1d20680fdf..ff6f403c559 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -826,9 +826,19 @@ public:
[[nodiscard]] bool has_a_style_sheet_that_is_blocking_scripts() const;
- bool is_fully_active() const;
+ bool is_fully_active() const
+ {
+ // Whether a document is fully active rarely changes, so the answer is kept until invalidate_full_activity().
+ if (m_full_activity_epoch == s_full_activity_epoch) [[likely]]
+ return m_is_fully_active;
+ return update_is_fully_active();
+ }
bool is_active() const;
+ // To call whenever anything that decides if a document is fully active changes: a navigable's parent, its being
+ // destroyed, or its active document, or a document's navigable or unload counter.
+ static void invalidate_full_activity() { ++s_full_activity_epoch; }
+
[[nodiscard]] HTML::HTMLParser::AllowDeclarativeShadowRoots allow_declarative_shadow_roots() const;
void set_allow_declarative_shadow_roots(HTML::HTMLParser::AllowDeclarativeShadowRoots allow_declarative_shadow_roots);
@@ -1626,6 +1636,9 @@ protected:
void initialize_document();
private:
+ bool update_is_fully_active() const;
+ bool compute_is_fully_active() const;
+
// Records the document's viewport for the host to publish, or, given `committed`, commits the recording.
Optional<Painting::DisplayListRecording> start_recording(Layout::BegunRead const&, HTML::PaintConfig, Painting::PaintCommandCacheMode, Optional<Compositor::FlightPresentation>&& committed);
@@ -1848,6 +1861,11 @@ private:
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#unload-counter
u32 m_unload_counter { 0 };
+ // The answer is_fully_active() last gave, and the value of s_full_activity_epoch it's valid for.
+ mutable bool m_is_fully_active { false };
+ mutable u64 m_full_activity_epoch { 0 };
+ static u64 s_full_activity_epoch;
+
// https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#throw-on-dynamic-markup-insertion-counter
u32 m_throw_on_dynamic_markup_insertion_counter { 0 };
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index 8d223d4e825..a85ecc184dd 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -708,6 +708,7 @@ void LocalNavigable::set_has_been_destroyed()
cancel_hover_update_after_async_scroll();
destroy_compositor_context();
m_has_been_destroyed = true;
+ DOM::Document::invalidate_full_activity();
resolve_all_pending_async_scroll_operations();
cancel_user_scroll_settlement();
}
@@ -1097,6 +1098,7 @@ void LocalNavigable::initialize_navigable(NonnullRefPtr<DocumentState> document_
// 4. Set navigable's active session history entry to entry.
m_active_session_history_entry = entry;
m_active_document = document;
+ DOM::Document::invalidate_full_activity();
document->set_navigable(this);
// 5. Set navigable's parent to parent.
@@ -1159,6 +1161,7 @@ void LocalNavigable::activate_history_entry(NonnullRefPtr<SessionHistoryEntry> e
m_active_document->set_navigable(nullptr);
}
m_active_document = new_document;
+ DOM::Document::invalidate_full_activity();
new_document->set_navigable(this);
set_needs_to_record_display_list();
@@ -1568,6 +1571,7 @@ void LocalNavigable::set_active_document(GC::Ptr<DOM::Document> document)
m_active_document->set_navigable(nullptr);
}
m_active_document = document;
+ DOM::Document::invalidate_full_activity();
if (document)
document->set_navigable(this);
set_needs_to_record_display_list();
diff --git a/Libraries/LibWeb/HTML/Navigable.cpp b/Libraries/LibWeb/HTML/Navigable.cpp
index 56a904fa003..c43188a9cf2 100644
--- a/Libraries/LibWeb/HTML/Navigable.cpp
+++ b/Libraries/LibWeb/HTML/Navigable.cpp
@@ -85,6 +85,12 @@ GC::Ptr<DOM::Node> Navigable::currently_focused_area_shown_by_focused_navigable(
return nullptr;
}
+void Navigable::set_parent(GC::Ptr<Navigable> parent)
+{
+ m_parent = parent;
+ DOM::Document::invalidate_full_activity();
+}
+
// AD-HOC: Destroying a child navigable detaches its subtree from the traversable, although the documents in that
// subtree keep their navigable until the destruction's unload steps complete. The spec destroys each of those
// documents from a queued task, so the node navigable of a removed iframe's documents stays non-null — leaving
diff --git a/Libraries/LibWeb/HTML/Navigable.h b/Libraries/LibWeb/HTML/Navigable.h
index c04f96d8860..08b0899a665 100644
--- a/Libraries/LibWeb/HTML/Navigable.h
+++ b/Libraries/LibWeb/HTML/Navigable.h
@@ -106,7 +106,7 @@ public:
protected:
explicit Navigable(GC::Ref<Page>);
void set_id(CrossProcessId id) { m_id = id; }
- void set_parent(GC::Ptr<Navigable> parent) { m_parent = parent; }
+ void set_parent(GC::Ptr<Navigable>);
virtual WebIDL::ExceptionOr<void> continue_navigation_in_active_document_agent(PreparedNavigation) = 0;
diff --git a/Libraries/LibWeb/HTML/RemoteNavigable.cpp b/Libraries/LibWeb/HTML/RemoteNavigable.cpp
index cb00d9f1b72..bc1d8a870e4 100644
--- a/Libraries/LibWeb/HTML/RemoteNavigable.cpp
+++ b/Libraries/LibWeb/HTML/RemoteNavigable.cpp
@@ -153,11 +153,18 @@ Vector<GC::Root<Navigable>> RemoteNavigable::active_document_inclusive_descendan
return navigables;
}
+void RemoteNavigable::set_has_been_destroyed()
+{
+ m_has_been_destroyed = true;
+ DOM::Document::invalidate_full_activity();
+}
+
void RemoteNavigable::set_replicated_state(ReplicatedNavigableState state)
{
auto was_delaying_the_load_event_of_its_container = m_replicated_state.delays_the_load_event_of_its_container;
auto previous_compositor_context_id = m_replicated_state.compositor_context_id;
m_replicated_state = move(state);
+ DOM::Document::invalidate_full_activity();
// The documents of the navigable's children hosted here composite into the context its document is painted
// through, wherever that document is hosted.
diff --git a/Libraries/LibWeb/HTML/RemoteNavigable.h b/Libraries/LibWeb/HTML/RemoteNavigable.h
index d64f6e29ee4..e143e635409 100644
--- a/Libraries/LibWeb/HTML/RemoteNavigable.h
+++ b/Libraries/LibWeb/HTML/RemoteNavigable.h
@@ -38,7 +38,7 @@ public:
void set_provisional_navigable(GC::Ptr<LocalNavigable> navigable) { m_provisional_navigable = navigable; }
virtual bool has_been_destroyed() const override { return m_has_been_destroyed; }
- virtual void set_has_been_destroyed() override { m_has_been_destroyed = true; }
+ virtual void set_has_been_destroyed() override;
Optional<Web::CompositorContextId> compositor_context_id() const { return m_replicated_state.compositor_context_id; }