Skip to content

Truly independent web browser

3.5 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Function.h>
#include <AK/HashTable.h>
#include <AK/Optional.h>
#include <AK/RefPtr.h>
#include <AK/Vector.h>
#include <AK/WeakPtr.h>
#include <LibCore/Promise.h>
#include <LibWebCommon/HTML/CrossProcessId.h>
#include <LibWebCommon/Page/PageId.h>
#include <LibWebView/Export.h>

namespace WebView {

class CanonicalSessionHistoryEntry;

// The steps resolve the promise they are given once they are complete.
using SessionHistoryTraversalSteps = Function<void(NonnullRefPtr<Core::Promise<Empty>>)>;

// https://html.spec.whatwg.org/multipage/document-sequences.html#tn-session-history-traversal-queue
class WEBVIEW_API SessionHistoryTraversalQueue
    : public Weakable<SessionHistoryTraversalQueue> {
    AK_MAKE_NONCOPYABLE(SessionHistoryTraversalQueue);
    AK_MAKE_NONMOVABLE(SessionHistoryTraversalQueue);

public:
    AK_ALLOC_WITH_KMALLOC;

    SessionHistoryTraversalQueue() = default;

    // The operation a page requested and can revoke while its steps have not run.
    struct RevocableOperation {
        Web::PageId page_id;
        Web::HTML::CrossProcessId operation_id;

        bool operator==(RevocableOperation const&) const = default;
    };

    struct Item {
        // Present for synchronous navigation steps, empty for plain algorithm steps.
        Optional<Web::HTML::CrossProcessId> target_navigable;
        SessionHistoryTraversalSteps steps;
        u64 sequence_number { 0 };
        // The entry synchronous navigation steps finalize.
        RefPtr<CanonicalSessionHistoryEntry> target_entry;
        Optional<RevocableOperation> revocable_operation;
    };

    // https://html.spec.whatwg.org/multipage/browsing-the-web.html#tn-append-session-history-traversal-steps
    void append_session_history_traversal_steps(SessionHistoryTraversalSteps, Optional<RevocableOperation> = {});
    bool remove_steps_of_revoked_operation(RevocableOperation const&);

    // https://html.spec.whatwg.org/multipage/browsing-the-web.html#tn-append-session-history-sync-nav-steps
    void append_session_history_synchronous_navigation_steps(Web::HTML::CrossProcessId target_navigable, RefPtr<CanonicalSessionHistoryEntry> target_entry, SessionHistoryTraversalSteps);
    void for_each_synchronous_navigation_target_entry(Web::HTML::CrossProcessId target_navigable, Function<void(CanonicalSessionHistoryEntry&)> const&) const;

    // https://html.spec.whatwg.org/multipage/browsing-the-web.html#sync-navigations-jump-queue
    Optional<Item> take_first_synchronous_navigation_steps_not_targeting(HashTable<Web::HTML::CrossProcessId> const& excluded_navigables);
    Optional<Item> take_first_synchronous_navigation_steps_not_targeting(HashTable<Web::HTML::CrossProcessId> const& excluded_navigables, u64 maximum_sequence_number);

    u64 last_enqueued_sequence_number() const { return m_last_enqueued_sequence_number; }

    bool current_item_is_synchronous_navigation_steps() const { return m_current_item_is_synchronous_navigation_steps; }

    bool is_empty() const { return m_algorithm_set.is_empty(); }

private:
    void process_queue();
    void schedule_processing();

    // https://html.spec.whatwg.org/multipage/browsing-the-web.html#session-history-traversal-parallel-queue-algorithm-set
    Vector<Item> m_algorithm_set;
    u64 m_last_enqueued_sequence_number { 0 };

    bool m_processing_scheduled { false };
    bool m_current_item_is_synchronous_navigation_steps { false };
    RefPtr<Core::Promise<Empty>> m_running_steps;
};

}