Truly independent web browser
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/HashMap.h>
#include <AK/NonnullRefPtr.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/Utf16String.h>
#include <AK/Vector.h>
#include <LibURL/Origin.h>
#include <LibURL/URL.h>
#include <LibWebCommon/HTML/CrossProcessId.h>
#include <LibWebCommon/HTML/SessionHistoryEntryDescriptor.h>
#include <LibWebCommon/HTML/UserAgentInitiated.h>
#include <LibWebView/CanonicalDocument.h>
#include <LibWebView/Export.h>
#include <LibWebView/Forward.h>
namespace WebView {
class CanonicalSessionHistoryEntry;
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#nested-history
struct CanonicalNestedHistory {
Web::HTML::CrossProcessId id;
Vector<NonnullRefPtr<CanonicalSessionHistoryEntry>> entries;
};
struct PopulatedDocument {
NonnullRefPtr<CanonicalDocument> document;
// The origin of a document the hosting process creates for inline content in place of the response.
Optional<URL::Origin> inline_content_origin {};
};
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#document-state-2
class WEBVIEW_API CanonicalDocumentState final : public RefCounted<CanonicalDocumentState> {
public:
static NonnullRefPtr<CanonicalDocumentState> create(Web::HTML::CrossProcessId);
static NonnullRefPtr<CanonicalDocumentState> create(Web::HTML::CrossProcessId, NonnullRefPtr<CanonicalDocument>);
static NonnullRefPtr<CanonicalDocumentState> create_from_descriptor(Web::HTML::SessionHistoryDocumentStateDescriptor const&);
~CanonicalDocumentState();
Web::HTML::SessionHistoryDocumentStateDescriptor descriptor() const;
void save_the_state_of_the_populated_document();
// NB: The identity the processes hosting the document state's documents give it.
Web::HTML::CrossProcessId id;
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#document-state-document
RefPtr<CanonicalDocument> document;
// AD-HOC: The document populated for the document state becomes its document when its entry is activated. A reload
// populates the document state of the active session history entry, whose document stays the navigable's
// active document until then.
Optional<PopulatedDocument> populated_document;
Variant<Web::HTML::SerializedPolicyContainer, Web::HTML::DocumentStateClient> history_policy_container { Web::HTML::DocumentStateClient::Tag };
Web::Fetch::Infrastructure::RequestReferrerType request_referrer { Web::Fetch::Infrastructure::RequestReferrer::Client };
Web::ReferrerPolicy::ReferrerPolicy request_referrer_policy { Web::ReferrerPolicy::DEFAULT_REFERRER_POLICY };
Optional<URL::Origin> initiator_origin;
Optional<URL::Origin> origin;
Optional<URL::URL> about_base_url;
Web::HTML::DocumentResource resource;
HTTP::Cookie::SameSiteStatus navigation_same_site_status { HTTP::Cookie::SameSiteStatus::CrossSite };
bool reload_pending { false };
bool reload_bypasses_cache { false };
bool ever_populated { false };
Web::HTML::UserAgentInitiated user_agent_initiated { Web::HTML::UserAgentInitiated::No };
Utf16String navigable_target_name;
Vector<CanonicalNestedHistory> nested_histories;
// Saved root used when repopulating this document state.
// The renderer can report entry URLs, but cannot supply this root.
Optional<LocalFileRoot> local_file_root;
private:
explicit CanonicalDocumentState(Web::HTML::CrossProcessId);
};
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#session-history-entry
class WEBVIEW_API CanonicalSessionHistoryEntry final : public RefCounted<CanonicalSessionHistoryEntry> {
public:
// Entries that name one document state share it, as they share the specification's document state object.
using DocumentStates = HashMap<Web::HTML::CrossProcessId, NonnullRefPtr<CanonicalDocumentState>>;
// Whether a descriptor naming a document state that exists updates it, as one from the process that holds the
// document state does, or refers to it as it is.
enum class UpdateDocumentState : bool {
No,
Yes,
};
static NonnullRefPtr<CanonicalSessionHistoryEntry> create(NonnullRefPtr<CanonicalDocumentState>);
static ErrorOr<NonnullRefPtr<CanonicalSessionHistoryEntry>> create_from_descriptor(Web::HTML::SessionHistoryEntryDescriptor const&);
static ErrorOr<NonnullRefPtr<CanonicalSessionHistoryEntry>> create_from_descriptor(Web::HTML::SessionHistoryEntryDescriptor const&, DocumentStates&, UpdateDocumentState = UpdateDocumentState::No);
~CanonicalSessionHistoryEntry();
ErrorOr<void> update_from_descriptor(Web::HTML::SessionHistoryEntryDescriptor const&);
Web::HTML::SessionHistoryEntryDescriptor descriptor() const;
Web::HTML::SessionHistoryEntryIdentity identity() const { return { document_state->id, navigation_api_id }; }
i32 step { 0 };
URL::URL url;
NonnullRefPtr<CanonicalDocumentState> document_state;
Web::HTML::StorageSerializationRecord classic_history_api_state;
Web::HTML::StorageSerializationRecord navigation_api_state;
Utf16String navigation_api_key;
Utf16String navigation_api_id;
Web::HTML::ScrollRestorationMode scroll_restoration_mode { Web::HTML::ScrollRestorationMode::Auto };
Web::HTML::SessionHistoryEntryScrollPositionData scroll_position_data;
Web::HTML::CrossProcessId directive_state_id;
Optional<String> directive_state_value;
private:
explicit CanonicalSessionHistoryEntry(NonnullRefPtr<CanonicalDocumentState>);
};
}