Skip to content

Truly independent web browser

11.9 KB
Raw Blame
/*
 * Copyright (c) 2023, Andrew Kaster <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Utf16String.h>
#include <LibJS/Runtime/Promise.h>
#include <LibWeb/Bindings/NavigationType.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/HTML/StructuredSerializeTypes.h>
#include <LibWeb/HTML/UserNavigationInvolvement.h>
#include <LibWebCommon/HTML/HistoryHandlingBehavior.h>

namespace Web::HTML {

class NavigationHistoryEntry;

using NavigationUpdateCurrentEntryOptions = Bindings::NavigationUpdateCurrentEntryOptions;
using NavigationOptions = Bindings::NavigationOptions;
using NavigationNavigateOptions = Bindings::NavigationNavigateOptions;
using NavigationReloadOptions = Bindings::NavigationReloadOptions;

struct NavigationResult {
    static NavigationResult from_promises(GC::Ref<WebIDL::Promise> committed, GC::Ref<WebIDL::Promise> finished)
    {
        return NavigationResult {
            .committed = committed,
            .finished = finished,
            .entry = nullptr,
        };
    }

    static NavigationResult resolved_with_entry(GC::Ref<NavigationHistoryEntry> entry)
    {
        return NavigationResult {
            .committed = nullptr,
            .finished = nullptr,
            .entry = entry,
        };
    }

    GC::Ptr<WebIDL::Promise> committed;
    GC::Ptr<WebIDL::Promise> finished;
    GC::Ptr<NavigationHistoryEntry> entry;
};

// https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-api-method-tracker
struct NavigationAPIMethodTracker final : public JS::Cell {
    GC_CELL(NavigationAPIMethodTracker, JS::Cell);
    GC_DECLARE_ALLOCATOR(NavigationAPIMethodTracker);

    enum class Pending : bool {
        No,
        Yes,
    };

    NavigationAPIMethodTracker(GC::Ref<Navigation> navigation,
        Optional<Utf16String> key,
        JS::Value info,
        Optional<StorageSerializationRecord> serialized_state,
        GC::Ptr<NavigationHistoryEntry> committed_to_entry,
        GC::Ref<WebIDL::Promise> committed_promise,
        GC::Ref<WebIDL::Promise> finished_promise,
        Pending pending);

    virtual void visit_edges(Cell::Visitor&) override;

    GC::Ref<Navigation> navigation;
    Optional<Utf16String> key;
    JS::Value info;
    Optional<StorageSerializationRecord> serialized_state;
    GC::Ptr<NavigationHistoryEntry> committed_to_entry;
    GC::Ref<WebIDL::Promise> committed_promise;
    GC::Ref<WebIDL::Promise> finished_promise;
    Pending pending { Pending::No };
};

// https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-interface
class Navigation : public DOM::EventTarget {
    WEB_WRAPPABLE(Navigation, DOM::EventTarget);
    GC_DECLARE_ALLOCATOR(Navigation);

public:
    [[nodiscard]] static GC::Ref<Navigation> create(Window&);

    // IDL properties and methods
    ReadonlySpan<GC::Ref<NavigationHistoryEntry>> entries() const;
    GC::Ptr<NavigationHistoryEntry> current_entry() const;
    WebIDL::ExceptionOr<void> update_current_entry(NavigationUpdateCurrentEntryOptions);

    // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-transition
    GC::Ptr<NavigationTransition> transition() const { return m_transition; }
    static constexpr size_t transition_offset() { return offsetof(Navigation, m_transition); }

    // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-activation
    GC::Ptr<NavigationActivation> activation() const { return m_activation; }
    static constexpr size_t activation_offset() { return offsetof(Navigation, m_activation); }
    void set_activation(GC::Ref<NavigationActivation> activation) { m_activation = activation; }

    Vector<GC::Ref<NavigationHistoryEntry>> const& entry_list() const { return m_entry_list; }

    bool can_go_back() const;
    bool can_go_forward() const;

    WebIDL::ExceptionOr<NavigationResult> navigate(Utf16String url, Bindings::NavigationNavigateOptions const&);
    WebIDL::ExceptionOr<NavigationResult> reload(Bindings::NavigationReloadOptions const&);

    WebIDL::ExceptionOr<NavigationResult> traverse_to(Utf16String key, Bindings::NavigationOptions const&);
    WebIDL::ExceptionOr<NavigationResult> back(Bindings::NavigationOptions const&);
    WebIDL::ExceptionOr<NavigationResult> forward(Bindings::NavigationOptions const&);

    // Event Handlers
    void set_onnavigate(WebIDL::CallbackType*);
    WebIDL::CallbackType* onnavigate();

    void set_onnavigatesuccess(WebIDL::CallbackType*);
    WebIDL::CallbackType* onnavigatesuccess();

    void set_onnavigateerror(WebIDL::CallbackType*);
    WebIDL::CallbackType* onnavigateerror();

    void set_oncurrententrychange(WebIDL::CallbackType*);
    WebIDL::CallbackType* oncurrententrychange();

    // Abstract Operations
    bool has_entries_and_events_disabled() const;
    i64 get_the_navigation_api_entry_index(SessionHistoryEntry const&) const;
    void abort_the_ongoing_navigation(GC::Ptr<WebIDL::DOMException> error = {});
    void abort_a_navigate_event(GC::Ref<NavigateEvent>, GC::Ref<WebIDL::DOMException> reason);
    void abort_a_navigate_event(GC::Ref<NavigateEvent>, JS::Value reason);
    bool fire_a_traverse_navigate_event(NonnullRefPtr<SessionHistoryEntry> destination_she, UserNavigationInvolvement = UserNavigationInvolvement::None);
    bool fire_a_push_replace_reload_navigate_event(
        NavigationType,
        URL::URL destination_url,
        bool is_same_document,
        UserNavigationInvolvement = UserNavigationInvolvement::None,
        GC::Ptr<DOM::Element> source_element = {},
        Optional<GC::RootVector<XHR::FormDataEntry>&> form_data_entry_list = {},
        Optional<StorageSerializationRecord> navigation_api_state = {},
        Optional<StorageSerializationRecord> classic_history_api_state = {},
        GC::Ptr<NavigationAPIMethodTracker> api_method_tracker = {});
    bool fire_a_download_request_navigate_event(URL::URL destination_url, UserNavigationInvolvement user_involvement, GC::Ptr<DOM::Element> source_element, Utf16String filename);

    void initialize_the_navigation_api_entries_for_a_new_document(Vector<NonnullRefPtr<SessionHistoryEntry>> const& new_shes, NonnullRefPtr<SessionHistoryEntry> initial_she);
    void initialize_the_navigation_api_entries_for_reconstructed_session_history(Vector<NonnullRefPtr<SessionHistoryEntry>> const& new_shes, NonnullRefPtr<SessionHistoryEntry> initial_she);
    void update_the_navigation_api_entries_for_a_same_document_navigation(NonnullRefPtr<SessionHistoryEntry> destination_she, Bindings::NavigationType);

    virtual ~Navigation() override;

    // Internal Getters/Setters
    GC::Ptr<NavigateEvent> ongoing_navigate_event() const { return m_ongoing_navigate_event; }
    void set_ongoing_navigate_event(GC::Ptr<NavigateEvent> event) { m_ongoing_navigate_event = event; }
    GC::Ptr<NavigationAPIMethodTracker> ongoing_api_method_tracker() const { return m_ongoing_api_method_tracker; }
    void set_ongoing_api_method_tracker(GC::Ptr<NavigationAPIMethodTracker> tracker) { m_ongoing_api_method_tracker = tracker; }

    bool focus_changed_during_ongoing_navigation() const { return m_focus_changed_during_ongoing_navigation; }
    void set_focus_changed_during_ongoing_navigation(bool b) { m_focus_changed_during_ongoing_navigation = b; }

    bool suppress_normal_scroll_restoration_during_ongoing_navigation() const { return m_suppress_normal_scroll_restoration_during_ongoing_navigation; }

    void set_was_initial_about_blank_opened(bool b) { m_was_initial_about_blank_opened = b; }

private:
    explicit Navigation(Window&);

    Window& window() const;
    virtual GC::Ptr<Bindings::Wrappable> relevant_global_impl() const override;

    virtual void visit_edges(Visitor&) override;

    using AnyException = decltype(declval<WebIDL::ExceptionOr<void>>().exception());
    NavigationResult early_error_result(AnyException);
    NavigationResult early_error_result(GC::Ref<WebIDL::DOMException>);

    NavigationResult navigation_api_method_tracker_derived_result(GC::Ref<NavigationAPIMethodTracker>);
    GC::Ref<NavigationAPIMethodTracker> set_up_a_navigate_reload_api_method_tracker(JS::Value info, Optional<StorageSerializationRecord>);
    GC::Ref<NavigationAPIMethodTracker> add_an_upcoming_traverse_api_method_tracker(Utf16String destination_key, JS::Value info);
    WebIDL::ExceptionOr<NavigationResult> perform_a_navigation_api_traversal(Utf16String key, Bindings::NavigationOptions const&);
    WebIDL::ExceptionOr<NavigationResult> reload_internal(Bindings::NavigationReloadOptions const&);
    WebIDL::ExceptionOr<NavigationResult> back_internal(Bindings::NavigationOptions const&);
    WebIDL::ExceptionOr<NavigationResult> forward_internal(Bindings::NavigationOptions const&);
    void resolve_the_finished_promise(GC::Ref<NavigationAPIMethodTracker>);
    void reject_the_finished_promise(GC::Ref<NavigationAPIMethodTracker>, JS::Value exception);
    void reject_the_finished_promise(GC::Ref<NavigationAPIMethodTracker>, GC::Ref<WebIDL::DOMException> exception);
    void clean_up(GC::Ref<NavigationAPIMethodTracker>);
    void notify_about_the_committed_to_entry(GC::Ref<NavigationAPIMethodTracker>, GC::Ref<NavigationHistoryEntry>);
    void run_the_navigate_event_intercept_commit_handler_steps(GC::Ref<NavigateEvent>, GC::Ptr<NavigationAPIMethodTracker>);
    void commit_a_navigate_event(GC::Ref<NavigateEvent>, GC::Ptr<NavigationAPIMethodTracker>);
    void process_navigate_event_handler_failure(GC::Ref<NavigateEvent>, JS::Value reason);

    bool inner_navigate_event_firing_algorithm(
        NavigationType,
        GC::Ref<NavigationDestination>,
        UserNavigationInvolvement,
        GC::Ptr<DOM::Element> source_element,
        Optional<GC::RootVector<XHR::FormDataEntry>&> form_data_entry_list,
        Optional<Utf16String> download_request_filename,
        Optional<StorageSerializationRecord> classic_history_api_state,
        GC::Ptr<NavigationAPIMethodTracker> api_method_tracker = {});

    // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-entry-list
    // Each Navigation has an associated entry list, a list of NavigationHistoryEntry objects, initially empty.
    Vector<GC::Ref<NavigationHistoryEntry>> m_entry_list;

    // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-current-entry-index
    // Each Navigation has an associated current entry index, an integer, initially −1.
    i64 m_current_entry_index { -1 };

    // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigation-transition
    // Each Navigation has a transition, which is a NavigationTransition or null, initially null.
    GC::Ptr<NavigationTransition> m_transition { nullptr };

    // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-activation
    // Each Navigation has an associated activation, which is null or a NavigationActivation object, initially null.
    GC::Ptr<NavigationActivation> m_activation;

    // https://html.spec.whatwg.org/multipage/nav-history-apis.html#ongoing-navigate-event
    GC::Ptr<NavigateEvent> m_ongoing_navigate_event { nullptr };

    GC::Ref<Window> m_window;

    // https://html.spec.whatwg.org/multipage/nav-history-apis.html#focus-changed-during-ongoing-navigation
    bool m_focus_changed_during_ongoing_navigation { false };

    // https://html.spec.whatwg.org/multipage/nav-history-apis.html#suppress-normal-scroll-restoration-during-ongoing-navigation
    bool m_suppress_normal_scroll_restoration_during_ongoing_navigation { false };

    // https://html.spec.whatwg.org/multipage/nav-history-apis.html#ongoing-api-method-tracker
    GC::Ptr<NavigationAPIMethodTracker> m_ongoing_api_method_tracker = nullptr;

    // https://html.spec.whatwg.org/multipage/nav-history-apis.html#upcoming-traverse-api-method-trackers
    HashMap<Utf16String, GC::Ref<NavigationAPIMethodTracker>> m_upcoming_traverse_api_method_trackers;

    // AD-HOC: Set when document.open() is called on an initial about:blank document.
    bool m_was_initial_about_blank_opened { false };
};

HistoryHandlingBehavior to_history_handling_behavior(NavigationHistoryBehavior);

}