2023-08-23 10:57:12 -06:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <LibJS/Runtime/Promise.h>
|
|
|
|
|
#include <LibWeb/Bindings/NavigationPrototype.h>
|
|
|
|
|
#include <LibWeb/DOM/EventTarget.h>
|
2023-08-28 18:03:45 +02:00
|
|
|
|
#include <LibWeb/HTML/HistoryHandlingBehavior.h>
|
2023-09-22 18:35:11 -06:00
|
|
|
|
#include <LibWeb/HTML/Navigable.h>
|
|
|
|
|
#include <LibWeb/HTML/NavigationType.h>
|
2023-08-24 16:20:38 -06:00
|
|
|
|
#include <LibWeb/HTML/StructuredSerialize.h>
|
2023-08-23 10:57:12 -06:00
|
|
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationupdatecurrententryoptions
|
|
|
|
|
struct NavigationUpdateCurrentEntryOptions {
|
|
|
|
|
JS::Value state;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationoptions
|
|
|
|
|
struct NavigationOptions {
|
2023-08-24 16:20:38 -06:00
|
|
|
|
Optional<JS::Value> info;
|
2023-08-23 10:57:12 -06:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationnavigateoptions
|
|
|
|
|
struct NavigationNavigateOptions : public NavigationOptions {
|
2023-08-24 16:20:38 -06:00
|
|
|
|
Optional<JS::Value> state;
|
2023-08-23 10:57:12 -06:00
|
|
|
|
Bindings::NavigationHistoryBehavior history = Bindings::NavigationHistoryBehavior::Auto;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationreloadoptions
|
|
|
|
|
struct NavigationReloadOptions : public NavigationOptions {
|
2023-08-24 16:20:38 -06:00
|
|
|
|
Optional<JS::Value> state;
|
2023-08-23 10:57:12 -06:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationresult
|
|
|
|
|
struct NavigationResult {
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ref<WebIDL::Promise> committed;
|
|
|
|
|
GC::Ref<WebIDL::Promise> finished;
|
2023-08-24 16:20:38 -06:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-api-method-tracker
|
|
|
|
|
struct NavigationAPIMethodTracker final : public JS::Cell {
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_CELL(NavigationAPIMethodTracker, JS::Cell);
|
|
|
|
|
GC_DECLARE_ALLOCATOR(NavigationAPIMethodTracker);
|
2023-08-24 16:20:38 -06:00
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
NavigationAPIMethodTracker(GC::Ref<Navigation> navigation,
|
2023-08-24 16:20:38 -06:00
|
|
|
|
Optional<String> key,
|
|
|
|
|
JS::Value info,
|
|
|
|
|
Optional<SerializationRecord> serialized_state,
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ptr<NavigationHistoryEntry> commited_to_entry,
|
|
|
|
|
GC::Ref<WebIDL::Promise> committed_promise,
|
|
|
|
|
GC::Ref<WebIDL::Promise> finished_promise);
|
2023-08-24 16:20:38 -06:00
|
|
|
|
|
|
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ref<Navigation> navigation;
|
2023-08-24 16:20:38 -06:00
|
|
|
|
Optional<String> key;
|
|
|
|
|
JS::Value info;
|
|
|
|
|
Optional<SerializationRecord> serialized_state;
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ptr<NavigationHistoryEntry> commited_to_entry;
|
|
|
|
|
GC::Ref<WebIDL::Promise> committed_promise;
|
|
|
|
|
GC::Ref<WebIDL::Promise> finished_promise;
|
2023-08-23 10:57:12 -06:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-interface
|
|
|
|
|
class Navigation : public DOM::EventTarget {
|
|
|
|
|
WEB_PLATFORM_OBJECT(Navigation, DOM::EventTarget);
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_DECLARE_ALLOCATOR(Navigation);
|
2023-08-23 10:57:12 -06:00
|
|
|
|
|
|
|
|
|
public:
|
2024-11-15 04:01:23 +13:00
|
|
|
|
[[nodiscard]] static GC::Ref<Navigation> create(JS::Realm&);
|
2023-08-23 10:57:12 -06:00
|
|
|
|
|
|
|
|
|
// IDL properties and methods
|
2024-11-15 04:01:23 +13:00
|
|
|
|
Vector<GC::Ref<NavigationHistoryEntry>> entries() const;
|
|
|
|
|
GC::Ptr<NavigationHistoryEntry> current_entry() const;
|
2023-08-23 10:57:12 -06:00
|
|
|
|
WebIDL::ExceptionOr<void> update_current_entry(NavigationUpdateCurrentEntryOptions);
|
2023-08-25 14:09:14 -06:00
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-transition
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ptr<NavigationTransition> transition() const { return m_transition; }
|
2023-08-25 14:09:14 -06:00
|
|
|
|
|
2023-08-23 10:57:12 -06:00
|
|
|
|
bool can_go_back() const;
|
|
|
|
|
bool can_go_forward() const;
|
|
|
|
|
|
2023-08-24 16:20:38 -06:00
|
|
|
|
WebIDL::ExceptionOr<NavigationResult> navigate(String url, NavigationNavigateOptions const&);
|
2023-08-25 14:09:14 -06:00
|
|
|
|
WebIDL::ExceptionOr<NavigationResult> reload(NavigationReloadOptions const&);
|
2023-08-23 15:34:02 -06:00
|
|
|
|
|
2023-08-25 20:35:22 -06:00
|
|
|
|
WebIDL::ExceptionOr<NavigationResult> traverse_to(String key, NavigationOptions const&);
|
|
|
|
|
WebIDL::ExceptionOr<NavigationResult> back(NavigationOptions const&);
|
|
|
|
|
WebIDL::ExceptionOr<NavigationResult> forward(NavigationOptions const&);
|
|
|
|
|
|
2023-08-23 10:57:12 -06:00
|
|
|
|
// 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;
|
2024-11-15 04:01:23 +13:00
|
|
|
|
void abort_the_ongoing_navigation(GC::Ptr<WebIDL::DOMException> error = {});
|
|
|
|
|
bool fire_a_traverse_navigate_event(GC::Ref<SessionHistoryEntry> destination_she, UserNavigationInvolvement = UserNavigationInvolvement::None);
|
2023-09-22 19:18:50 -06:00
|
|
|
|
bool fire_a_push_replace_reload_navigate_event(
|
|
|
|
|
Bindings::NavigationType,
|
2024-03-18 16:22:27 +13:00
|
|
|
|
URL::URL destination_url,
|
2023-09-22 19:18:50 -06:00
|
|
|
|
bool is_same_document,
|
|
|
|
|
UserNavigationInvolvement = UserNavigationInvolvement::None,
|
|
|
|
|
Optional<Vector<XHR::FormDataEntry>&> form_data_entry_list = {},
|
|
|
|
|
Optional<SerializationRecord> navigation_api_state = {},
|
|
|
|
|
Optional<SerializationRecord> classic_history_api_state = {});
|
2024-03-18 16:22:27 +13:00
|
|
|
|
bool fire_a_download_request_navigate_event(URL::URL destination_url, UserNavigationInvolvement user_involvement, String filename);
|
2023-08-23 10:57:12 -06:00
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
void initialize_the_navigation_api_entries_for_a_new_document(Vector<GC::Ref<SessionHistoryEntry>> const& new_shes, GC::Ref<SessionHistoryEntry> initial_she);
|
|
|
|
|
void update_the_navigation_api_entries_for_a_same_document_navigation(GC::Ref<SessionHistoryEntry> destination_she, Bindings::NavigationType);
|
2023-09-27 22:56:11 -06:00
|
|
|
|
|
2023-08-23 10:57:12 -06:00
|
|
|
|
virtual ~Navigation() override;
|
|
|
|
|
|
2023-09-22 18:24:28 -06:00
|
|
|
|
// Internal Getters/Setters
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ptr<NavigateEvent> ongoing_navigate_event() const { return m_ongoing_navigate_event; }
|
2023-09-20 23:21:16 -06:00
|
|
|
|
|
2023-09-22 18:24:28 -06:00
|
|
|
|
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; }
|
|
|
|
|
|
2023-08-23 10:57:12 -06:00
|
|
|
|
private:
|
|
|
|
|
explicit Navigation(JS::Realm&);
|
|
|
|
|
|
|
|
|
|
virtual void initialize(JS::Realm&) override;
|
|
|
|
|
virtual void visit_edges(Visitor&) override;
|
|
|
|
|
|
2023-08-24 16:20:38 -06:00
|
|
|
|
using AnyException = decltype(declval<WebIDL::ExceptionOr<void>>().exception());
|
|
|
|
|
NavigationResult early_error_result(AnyException);
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ref<NavigationAPIMethodTracker> maybe_set_the_upcoming_non_traverse_api_method_tracker(JS::Value info, Optional<SerializationRecord>);
|
|
|
|
|
GC::Ref<NavigationAPIMethodTracker> add_an_upcoming_traverse_api_method_tracker(String destination_key, JS::Value info);
|
2023-08-25 20:35:22 -06:00
|
|
|
|
WebIDL::ExceptionOr<NavigationResult> perform_a_navigation_api_traversal(String key, NavigationOptions const&);
|
2023-09-22 18:30:40 -06:00
|
|
|
|
void promote_an_upcoming_api_method_tracker_to_ongoing(Optional<String> destination_key);
|
2024-11-15 04:01:23 +13:00
|
|
|
|
void resolve_the_finished_promise(GC::Ref<NavigationAPIMethodTracker>);
|
|
|
|
|
void reject_the_finished_promise(GC::Ref<NavigationAPIMethodTracker>, JS::Value exception);
|
|
|
|
|
void clean_up(GC::Ref<NavigationAPIMethodTracker>);
|
|
|
|
|
void notify_about_the_committed_to_entry(GC::Ref<NavigationAPIMethodTracker>, GC::Ref<NavigationHistoryEntry>);
|
2023-08-24 16:20:38 -06:00
|
|
|
|
|
2023-09-22 18:35:11 -06:00
|
|
|
|
bool inner_navigate_event_firing_algorithm(
|
|
|
|
|
Bindings::NavigationType,
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ref<NavigationDestination>,
|
2023-09-22 18:35:11 -06:00
|
|
|
|
UserNavigationInvolvement,
|
|
|
|
|
Optional<Vector<XHR::FormDataEntry>&> form_data_entry_list,
|
|
|
|
|
Optional<String> download_request_filename,
|
|
|
|
|
Optional<SerializationRecord> classic_history_api_state);
|
|
|
|
|
|
2023-08-23 10:57:12 -06:00
|
|
|
|
// 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.
|
2024-11-15 04:01:23 +13:00
|
|
|
|
Vector<GC::Ref<NavigationHistoryEntry>> m_entry_list;
|
2023-08-23 10:57:12 -06:00
|
|
|
|
|
|
|
|
|
// 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 };
|
2023-08-23 15:34:02 -06:00
|
|
|
|
|
|
|
|
|
// 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.
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ptr<NavigationTransition> m_transition { nullptr };
|
2023-08-24 16:20:38 -06:00
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#ongoing-navigate-event
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ptr<NavigateEvent> m_ongoing_navigate_event { nullptr };
|
2023-08-24 16:20:38 -06:00
|
|
|
|
|
|
|
|
|
// 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_scroll_restoration_during_ongoing_navigation { false };
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#ongoing-api-method-tracker
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ptr<NavigationAPIMethodTracker> m_ongoing_api_method_tracker = nullptr;
|
2023-08-24 16:20:38 -06:00
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#upcoming-non-traverse-api-method-tracker
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ptr<NavigationAPIMethodTracker> m_upcoming_non_traverse_api_method_tracker = nullptr;
|
2023-08-24 16:20:38 -06:00
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#upcoming-non-traverse-api-method-tracker
|
2024-11-15 04:01:23 +13:00
|
|
|
|
HashMap<String, GC::Ref<NavigationAPIMethodTracker>> m_upcoming_traverse_api_method_trackers;
|
2023-08-23 10:57:12 -06:00
|
|
|
|
};
|
|
|
|
|
|
2023-08-28 18:03:45 +02:00
|
|
|
|
HistoryHandlingBehavior to_history_handling_behavior(Bindings::NavigationHistoryBehavior);
|
2024-03-28 12:58:43 +01:00
|
|
|
|
Bindings::NavigationHistoryBehavior to_navigation_history_behavior(HistoryHandlingBehavior);
|
2023-08-28 18:03:45 +02:00
|
|
|
|
|
2023-08-23 10:57:12 -06:00
|
|
|
|
}
|