ladybird/Libraries/LibWeb/HTML/SessionHistoryTraversalQueue.h
Aliaksandr Kalenik 2a69fd4c52 LibWeb: Replace spin_until in apply_the_history_step with state machine
Replace the blocking spin_processing_tasks_with_source_until calls
in apply_the_history_step_after_unload_check() with an event-driven
ApplyHistoryStepState GC cell that tracks 5 phases, following the
same pattern used by CheckUnloadingCanceledState.

Key changes:
- Introduce ApplyHistoryStepState with phases:
  WaitingForDocumentPopulation, ProcessingContinuations,
  WaitingForChangeJobCompletion, WaitingForNonChangingJobs and Completed
- Add on_complete callbacks to apply_the_push_or_replace_history_step,
  finalize_a_same_document_navigation,
  finalize_a_cross_document_navigation, and
  update_for_navigable_creation_or_destruction
- Remove spin_until from Document::open()
- Use null-document tasks for non-changing navigable updates and
  document unload/destroy to avoid stuck tasks when documents become
  non-fully-active
- Defer completely_finish_loading when document has no navigable yet,
  and re-trigger post-load steps in activate_history_entry for documents
  that completed loading before activation

Co-Authored-By: Shannon Booth <shannon@serenityos.org>
2026-03-31 09:47:59 +02:00

72 lines
2.4 KiB
C++

/*
* Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Vector.h>
#include <LibCore/EventLoop.h>
#include <LibCore/Promise.h>
#include <LibGC/CellAllocator.h>
#include <LibGC/Function.h>
#include <LibGC/Ptr.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap/Cell.h>
#include <LibWeb/Export.h>
#include <LibWeb/Forward.h>
namespace Web::HTML {
using SessionHistoryTraversalSteps = GC::Function<void(NonnullRefPtr<Core::Promise<Empty>>)>;
struct SessionHistoryTraversalQueueEntry : public JS::Cell {
GC_CELL(SessionHistoryTraversalQueueEntry, JS::Cell);
GC_DECLARE_ALLOCATOR(SessionHistoryTraversalQueueEntry);
public:
static GC::Ref<SessionHistoryTraversalQueueEntry> create(JS::VM& vm, GC::Ref<SessionHistoryTraversalSteps> steps, GC::Ptr<HTML::Navigable> target_navigable);
GC::Ptr<HTML::Navigable> target_navigable() const { return m_target_navigable; }
void execute_steps(NonnullRefPtr<Core::Promise<Empty>> promise) const { m_steps->function()(move(promise)); }
private:
SessionHistoryTraversalQueueEntry(GC::Ref<SessionHistoryTraversalSteps> steps, GC::Ptr<HTML::Navigable> target_navigable)
: m_steps(steps)
, m_target_navigable(target_navigable)
{
}
virtual void visit_edges(Cell::Visitor&) override;
GC::Ref<SessionHistoryTraversalSteps> m_steps;
GC::Ptr<HTML::Navigable> m_target_navigable;
};
// https://html.spec.whatwg.org/multipage/document-sequences.html#tn-session-history-traversal-queue
class WEB_API SessionHistoryTraversalQueue : public JS::Cell {
GC_CELL(SessionHistoryTraversalQueue, JS::Cell);
GC_DECLARE_ALLOCATOR(SessionHistoryTraversalQueue);
public:
SessionHistoryTraversalQueue();
void append(GC::Ref<SessionHistoryTraversalSteps> steps);
void append_sync(GC::Ref<SessionHistoryTraversalSteps> steps, GC::Ptr<Navigable> target_navigable);
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#sync-navigations-jump-queue
GC::Ptr<SessionHistoryTraversalQueueEntry> first_synchronous_navigation_steps_with_target_navigable_not_contained_in(HashTable<GC::Ref<Navigable>> const&);
private:
virtual void visit_edges(Cell::Visitor&) override;
void process_queue();
void schedule_processing();
Vector<GC::Ref<SessionHistoryTraversalQueueEntry>> m_queue;
bool m_processing_scheduled { false };
RefPtr<Core::Promise<Empty>> m_current_promise;
};
}