Navigation.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Runtime/Promise.h>
  8. #include <LibWeb/Bindings/NavigationPrototype.h>
  9. #include <LibWeb/DOM/EventTarget.h>
  10. #include <LibWeb/HTML/HistoryHandlingBehavior.h>
  11. #include <LibWeb/HTML/Navigable.h>
  12. #include <LibWeb/HTML/NavigationType.h>
  13. #include <LibWeb/HTML/StructuredSerialize.h>
  14. namespace Web::HTML {
  15. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationupdatecurrententryoptions
  16. struct NavigationUpdateCurrentEntryOptions {
  17. JS::Value state;
  18. };
  19. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationoptions
  20. struct NavigationOptions {
  21. Optional<JS::Value> info;
  22. };
  23. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationnavigateoptions
  24. struct NavigationNavigateOptions : public NavigationOptions {
  25. Optional<JS::Value> state;
  26. Bindings::NavigationHistoryBehavior history = Bindings::NavigationHistoryBehavior::Auto;
  27. };
  28. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationreloadoptions
  29. struct NavigationReloadOptions : public NavigationOptions {
  30. Optional<JS::Value> state;
  31. };
  32. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationresult
  33. struct NavigationResult {
  34. // FIXME: Are we supposed to return a PromiseCapability (WebIDL::Promise) here?
  35. JS::NonnullGCPtr<JS::Object> committed;
  36. JS::NonnullGCPtr<JS::Object> finished;
  37. };
  38. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-api-method-tracker
  39. struct NavigationAPIMethodTracker final : public JS::Cell {
  40. JS_CELL(NavigationAPIMethodTracker, JS::Cell);
  41. NavigationAPIMethodTracker(JS::NonnullGCPtr<Navigation> navigation,
  42. Optional<String> key,
  43. JS::Value info,
  44. Optional<SerializationRecord> serialized_state,
  45. JS::GCPtr<NavigationHistoryEntry> commited_to_entry,
  46. JS::NonnullGCPtr<WebIDL::Promise> committed_promise,
  47. JS::NonnullGCPtr<WebIDL::Promise> finished_promise);
  48. virtual void visit_edges(Cell::Visitor&) override;
  49. JS::NonnullGCPtr<Navigation> navigation;
  50. Optional<String> key;
  51. JS::Value info;
  52. Optional<SerializationRecord> serialized_state;
  53. JS::GCPtr<NavigationHistoryEntry> commited_to_entry;
  54. JS::NonnullGCPtr<WebIDL::Promise> committed_promise;
  55. JS::NonnullGCPtr<WebIDL::Promise> finished_promise;
  56. };
  57. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-interface
  58. class Navigation : public DOM::EventTarget {
  59. WEB_PLATFORM_OBJECT(Navigation, DOM::EventTarget);
  60. JS_DECLARE_ALLOCATOR(Navigation);
  61. public:
  62. [[nodiscard]] static JS::NonnullGCPtr<Navigation> create(JS::Realm&);
  63. // IDL properties and methods
  64. Vector<JS::NonnullGCPtr<NavigationHistoryEntry>> entries() const;
  65. JS::GCPtr<NavigationHistoryEntry> current_entry() const;
  66. WebIDL::ExceptionOr<void> update_current_entry(NavigationUpdateCurrentEntryOptions);
  67. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-transition
  68. JS::GCPtr<NavigationTransition> transition() const { return m_transition; }
  69. bool can_go_back() const;
  70. bool can_go_forward() const;
  71. WebIDL::ExceptionOr<NavigationResult> navigate(String url, NavigationNavigateOptions const&);
  72. WebIDL::ExceptionOr<NavigationResult> reload(NavigationReloadOptions const&);
  73. WebIDL::ExceptionOr<NavigationResult> traverse_to(String key, NavigationOptions const&);
  74. WebIDL::ExceptionOr<NavigationResult> back(NavigationOptions const&);
  75. WebIDL::ExceptionOr<NavigationResult> forward(NavigationOptions const&);
  76. // Event Handlers
  77. void set_onnavigate(WebIDL::CallbackType*);
  78. WebIDL::CallbackType* onnavigate();
  79. void set_onnavigatesuccess(WebIDL::CallbackType*);
  80. WebIDL::CallbackType* onnavigatesuccess();
  81. void set_onnavigateerror(WebIDL::CallbackType*);
  82. WebIDL::CallbackType* onnavigateerror();
  83. void set_oncurrententrychange(WebIDL::CallbackType*);
  84. WebIDL::CallbackType* oncurrententrychange();
  85. // Abstract Operations
  86. bool has_entries_and_events_disabled() const;
  87. i64 get_the_navigation_api_entry_index(SessionHistoryEntry const&) const;
  88. void abort_the_ongoing_navigation(Optional<JS::NonnullGCPtr<WebIDL::DOMException>> error = {});
  89. bool fire_a_traverse_navigate_event(JS::NonnullGCPtr<SessionHistoryEntry> destination_she, UserNavigationInvolvement = UserNavigationInvolvement::None);
  90. bool fire_a_push_replace_reload_navigate_event(
  91. Bindings::NavigationType,
  92. AK::URL destination_url,
  93. bool is_same_document,
  94. UserNavigationInvolvement = UserNavigationInvolvement::None,
  95. Optional<Vector<XHR::FormDataEntry>&> form_data_entry_list = {},
  96. Optional<SerializationRecord> navigation_api_state = {},
  97. Optional<SerializationRecord> classic_history_api_state = {});
  98. bool fire_a_download_request_navigate_event(AK::URL destination_url, UserNavigationInvolvement user_involvement, String filename);
  99. void initialize_the_navigation_api_entries_for_a_new_document(Vector<JS::NonnullGCPtr<SessionHistoryEntry>> const& new_shes, JS::NonnullGCPtr<SessionHistoryEntry> initial_she);
  100. void update_the_navigation_api_entries_for_a_same_document_navigation(JS::NonnullGCPtr<SessionHistoryEntry> destination_she, Bindings::NavigationType);
  101. virtual ~Navigation() override;
  102. // Internal Getters/Setters
  103. JS::GCPtr<NavigateEvent> ongoing_navigate_event() const { return m_ongoing_navigate_event; }
  104. bool focus_changed_during_ongoing_navigation() const { return m_focus_changed_during_ongoing_navigation; }
  105. void set_focus_changed_during_ongoing_navigation(bool b) { m_focus_changed_during_ongoing_navigation = b; }
  106. private:
  107. explicit Navigation(JS::Realm&);
  108. virtual void initialize(JS::Realm&) override;
  109. virtual void visit_edges(Visitor&) override;
  110. using AnyException = decltype(declval<WebIDL::ExceptionOr<void>>().exception());
  111. NavigationResult early_error_result(AnyException);
  112. JS::NonnullGCPtr<NavigationAPIMethodTracker> maybe_set_the_upcoming_non_traverse_api_method_tracker(JS::Value info, Optional<SerializationRecord>);
  113. JS::NonnullGCPtr<NavigationAPIMethodTracker> add_an_upcoming_traverse_api_method_tracker(String destination_key, JS::Value info);
  114. WebIDL::ExceptionOr<NavigationResult> perform_a_navigation_api_traversal(String key, NavigationOptions const&);
  115. void promote_an_upcoming_api_method_tracker_to_ongoing(Optional<String> destination_key);
  116. void resolve_the_finished_promise(JS::NonnullGCPtr<NavigationAPIMethodTracker>);
  117. void reject_the_finished_promise(JS::NonnullGCPtr<NavigationAPIMethodTracker>, JS::Value exception);
  118. void clean_up(JS::NonnullGCPtr<NavigationAPIMethodTracker>);
  119. void notify_about_the_committed_to_entry(JS::NonnullGCPtr<NavigationAPIMethodTracker>, JS::NonnullGCPtr<NavigationHistoryEntry>);
  120. bool inner_navigate_event_firing_algorithm(
  121. Bindings::NavigationType,
  122. JS::NonnullGCPtr<NavigationDestination>,
  123. UserNavigationInvolvement,
  124. Optional<Vector<XHR::FormDataEntry>&> form_data_entry_list,
  125. Optional<String> download_request_filename,
  126. Optional<SerializationRecord> classic_history_api_state);
  127. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-entry-list
  128. // Each Navigation has an associated entry list, a list of NavigationHistoryEntry objects, initially empty.
  129. Vector<JS::NonnullGCPtr<NavigationHistoryEntry>> m_entry_list;
  130. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-current-entry-index
  131. // Each Navigation has an associated current entry index, an integer, initially −1.
  132. i64 m_current_entry_index { -1 };
  133. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigation-transition
  134. // Each Navigation has a transition, which is a NavigationTransition or null, initially null.
  135. JS::GCPtr<NavigationTransition> m_transition { nullptr };
  136. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#ongoing-navigate-event
  137. JS::GCPtr<NavigateEvent> m_ongoing_navigate_event { nullptr };
  138. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#focus-changed-during-ongoing-navigation
  139. bool m_focus_changed_during_ongoing_navigation { false };
  140. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#suppress-normal-scroll-restoration-during-ongoing-navigation
  141. bool m_suppress_scroll_restoration_during_ongoing_navigation { false };
  142. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#ongoing-api-method-tracker
  143. JS::GCPtr<NavigationAPIMethodTracker> m_ongoing_api_method_tracker = nullptr;
  144. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#upcoming-non-traverse-api-method-tracker
  145. JS::GCPtr<NavigationAPIMethodTracker> m_upcoming_non_traverse_api_method_tracker = nullptr;
  146. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#upcoming-non-traverse-api-method-tracker
  147. HashMap<String, JS::NonnullGCPtr<NavigationAPIMethodTracker>> m_upcoming_traverse_api_method_trackers;
  148. };
  149. HistoryHandlingBehavior to_history_handling_behavior(Bindings::NavigationHistoryBehavior);
  150. }