Skip to content

Truly independent web browser

7.1 KB
Raw Blame
/*
 * Copyright (c) 2022, Andreas Kling <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/HashMap.h>
#include <AK/Optional.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/Types.h>
#include <AK/Utf16String.h>
#include <AK/Vector.h>
#include <LibURL/URL.h>
#include <LibWeb/Export.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/DirectiveState.h>
#include <LibWeb/HTML/DocumentState.h>
#include <LibWeb/HTML/StructuredSerializeTypes.h>
#include <LibWebCommon/HTML/CrossProcessId.h>
#include <LibWebCommon/HTML/SessionHistoryEntryDescriptor.h>
#include <LibWebCommon/PixelUnits.h>
#include <LibWebCommon/ReferrerPolicy/ReferrerPolicy.h>

namespace Web::HTML {

class WEB_API SessionHistoryEntry final : public RefCounted<SessionHistoryEntry> {
public:
    GC_HOLDS_NO_CELL_EDGES;

    static NonnullRefPtr<SessionHistoryEntry> create();
    static NonnullRefPtr<SessionHistoryEntry> create(NonnullRefPtr<DirectiveState>);

    SessionHistoryEntry();
    explicit SessionHistoryEntry(NonnullRefPtr<DirectiveState>);
    ~SessionHistoryEntry();

    enum class Pending {
        Tag,
    };

    [[nodiscard]] Variant<int, Pending> step() const { return m_step; }
    void set_step(Variant<int, Pending> step) { m_step = step; }
    [[nodiscard]] Optional<int> step_value() const
    {
        if (auto const* step = m_step.get_pointer<int>())
            return *step;
        return {};
    }

    [[nodiscard]] URL::URL const& url() const { return m_url; }
    void set_url(URL::URL url) { m_url = move(url); }

    [[nodiscard]] RefPtr<HTML::DocumentState> document_state() const;
    void set_document_state(RefPtr<HTML::DocumentState>);

    [[nodiscard]] UniqueNodeID document_id() const;

    [[nodiscard]] StorageSerializationRecord const& classic_history_api_state() const { return m_classic_history_api_state; }
    void set_classic_history_api_state(StorageSerializationRecord classic_history_api_state) { m_classic_history_api_state = move(classic_history_api_state); }

    [[nodiscard]] StorageSerializationRecord const& navigation_api_state() const { return m_navigation_api_state; }
    void set_navigation_api_state(StorageSerializationRecord navigation_api_state) { m_navigation_api_state = move(navigation_api_state); }

    [[nodiscard]] Utf16String const& navigation_api_key() const { return m_navigation_api_key; }
    void set_navigation_api_key(Utf16String navigation_api_key) { m_navigation_api_key = move(navigation_api_key); }

    [[nodiscard]] Utf16String const& navigation_api_id() const { return m_navigation_api_id; }
    void set_navigation_api_id(Utf16String navigation_api_id) { m_navigation_api_id = move(navigation_api_id); }

    [[nodiscard]] ScrollRestorationMode scroll_restoration_mode() const { return m_scroll_restoration_mode; }
    void set_scroll_restoration_mode(ScrollRestorationMode scroll_restoration_mode) { m_scroll_restoration_mode = scroll_restoration_mode; }

    [[nodiscard]] SessionHistoryEntryScrollPositionData const& scroll_position_data() const { return m_scroll_position_data; }
    void set_scroll_position_data(SessionHistoryEntryScrollPositionData scroll_position_data) { m_scroll_position_data = move(scroll_position_data); }
    [[nodiscard]] NonnullRefPtr<DirectiveState> const& directive_state() const { return m_directive_state; }
    void set_directive_state(NonnullRefPtr<DirectiveState> state) { m_directive_state = move(state); }

private:
    // https://html.spec.whatwg.org/multipage/browsing-the-web.html#she-step
    // step, a non-negative integer or "pending", initially "pending".
    Variant<int, Pending> m_step { Pending::Tag };

    // https://html.spec.whatwg.org/multipage/browsing-the-web.html#she-url
    // URL, a URL
    URL::URL m_url;

    // https://html.spec.whatwg.org/multipage/browsing-the-web.html#she-document-state
    RefPtr<HTML::DocumentState> m_document_state;

    // https://html.spec.whatwg.org/multipage/browsing-the-web.html#she-classic-history-api-state
    // classic history API state, which is serialized state, initially StructuredSerializeForStorage(null).
    StorageSerializationRecord m_classic_history_api_state;

    // https://html.spec.whatwg.org/multipage/browsing-the-web.html#she-navigation-api-state
    // navigation API state, which is a serialized state, initially StructuredSerializeForStorage(undefined).
    StorageSerializationRecord m_navigation_api_state;

    // https://html.spec.whatwg.org/multipage/browsing-the-web.html#she-navigation-api-key
    // navigation API key, which is a string, initially set to the result of generating a random UUID.
    Utf16String m_navigation_api_key;

    // https://html.spec.whatwg.org/multipage/browsing-the-web.html#she-navigation-api-id
    // navigation API ID, which is a string, initially set to the result of generating a random UUID.
    Utf16String m_navigation_api_id;

    // https://html.spec.whatwg.org/multipage/browsing-the-web.html#she-scroll-restoration-mode
    // scroll restoration mode, a scroll restoration mode, initially "auto"
    ScrollRestorationMode m_scroll_restoration_mode { ScrollRestorationMode::Auto };

    // https://html.spec.whatwg.org/multipage/browsing-the-web.html#she-scroll-position
    // scroll position data, which is scroll position data for the document's restorable scrollable regions
    SessionHistoryEntryScrollPositionData m_scroll_position_data;
    NonnullRefPtr<DirectiveState> m_directive_state;

    // https://html.spec.whatwg.org/multipage/browsing-the-web.html#she-other
    // FIXME: persisted user state, which is implementation-defined, initially null
    // NOTE: This is where we could remember the state of form controls, for example.
};

WEB_API SessionHistoryEntryDescriptor create_session_history_entry_descriptor(SessionHistoryEntry const&);

WEB_API SessionHistoryDocumentStateDescriptor create_session_history_document_state_descriptor(DocumentState const&);
WEB_API PendingSessionHistoryEntryDescriptor create_pending_session_history_entry_descriptor(SessionHistoryEntry const&);
WEB_API Optional<SessionHistoryEntryPersistedState> create_session_history_entry_persisted_state(SessionHistoryEntry const&);
WEB_API SessionHistoryEntryIdentity session_history_entry_identity(SessionHistoryEntry const&);

// Document states already reconstructed from the UI process's descriptors, so entries that share a document state
// there share one here too.
struct SessionHistoryEntryReconstructionState {
    HashMap<CrossProcessId, RefPtr<DocumentState>> document_states;
    HashMap<CrossProcessId, NonnullRefPtr<DirectiveState>> directive_states;
};

WEB_API void apply_session_history_entry_descriptor_from_ui_process(SessionHistoryEntry&, SessionHistoryEntryDescriptor&);
WEB_API void apply_session_history_document_state_descriptor_from_ui_process(DocumentState&, SessionHistoryDocumentStateDescriptor const&);
WEB_API RefPtr<DocumentState> get_or_create_document_state_from_ui_process(SessionHistoryDocumentStateDescriptor const&, SessionHistoryEntryReconstructionState&);
WEB_API NonnullRefPtr<SessionHistoryEntry> create_session_history_entry_from_ui_process(SessionHistoryEntryDescriptor, SessionHistoryEntryReconstructionState&);

}