Navigation.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. JS_DECLARE_ALLOCATOR(NavigationAPIMethodTracker);
  42. NavigationAPIMethodTracker(JS::NonnullGCPtr<Navigation> navigation,
  43. Optional<String> key,
  44. JS::Value info,
  45. Optional<SerializationRecord> serialized_state,
  46. JS::GCPtr<NavigationHistoryEntry> commited_to_entry,
  47. JS::NonnullGCPtr<WebIDL::Promise> committed_promise,
  48. JS::NonnullGCPtr<WebIDL::Promise> finished_promise);
  49. virtual void visit_edges(Cell::Visitor&) override;
  50. JS::NonnullGCPtr<Navigation> navigation;
  51. Optional<String> key;
  52. JS::Value info;
  53. Optional<SerializationRecord> serialized_state;
  54. JS::GCPtr<NavigationHistoryEntry> commited_to_entry;
  55. JS::NonnullGCPtr<WebIDL::Promise> committed_promise;
  56. JS::NonnullGCPtr<WebIDL::Promise> finished_promise;
  57. };
  58. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-interface
  59. class Navigation : public DOM::EventTarget {
  60. WEB_PLATFORM_OBJECT(Navigation, DOM::EventTarget);
  61. JS_DECLARE_ALLOCATOR(Navigation);
  62. public:
  63. [[nodiscard]] static JS::NonnullGCPtr<Navigation> create(JS::Realm&);
  64. // IDL properties and methods
  65. Vector<JS::NonnullGCPtr<NavigationHistoryEntry>> entries() const;
  66. JS::GCPtr<NavigationHistoryEntry> current_entry() const;
  67. WebIDL::ExceptionOr<void> update_current_entry(NavigationUpdateCurrentEntryOptions);
  68. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-transition
  69. JS::GCPtr<NavigationTransition> transition() const { return m_transition; }
  70. bool can_go_back() const;
  71. bool can_go_forward() const;
  72. WebIDL::ExceptionOr<NavigationResult> navigate(String url, NavigationNavigateOptions const&);
  73. WebIDL::ExceptionOr<NavigationResult> reload(NavigationReloadOptions const&);
  74. WebIDL::ExceptionOr<NavigationResult> traverse_to(String key, NavigationOptions const&);
  75. WebIDL::ExceptionOr<NavigationResult> back(NavigationOptions const&);
  76. WebIDL::ExceptionOr<NavigationResult> forward(NavigationOptions const&);
  77. // Event Handlers
  78. void set_onnavigate(WebIDL::CallbackType*);
  79. WebIDL::CallbackType* onnavigate();
  80. void set_onnavigatesuccess(WebIDL::CallbackType*);
  81. WebIDL::CallbackType* onnavigatesuccess();
  82. void set_onnavigateerror(WebIDL::CallbackType*);
  83. WebIDL::CallbackType* onnavigateerror();
  84. void set_oncurrententrychange(WebIDL::CallbackType*);
  85. WebIDL::CallbackType* oncurrententrychange();
  86. // Abstract Operations
  87. bool has_entries_and_events_disabled() const;
  88. i64 get_the_navigation_api_entry_index(SessionHistoryEntry const&) const;
  89. void abort_the_ongoing_navigation(JS::GCPtr<WebIDL::DOMException> error = {});
  90. bool fire_a_traverse_navigate_event(JS::NonnullGCPtr<SessionHistoryEntry> destination_she, UserNavigationInvolvement = UserNavigationInvolvement::None);
  91. bool fire_a_push_replace_reload_navigate_event(
  92. Bindings::NavigationType,
  93. URL::URL destination_url,
  94. bool is_same_document,
  95. UserNavigationInvolvement = UserNavigationInvolvement::None,
  96. Optional<Vector<XHR::FormDataEntry>&> form_data_entry_list = {},
  97. Optional<SerializationRecord> navigation_api_state = {},
  98. Optional<SerializationRecord> classic_history_api_state = {});
  99. bool fire_a_download_request_navigate_event(URL::URL destination_url, UserNavigationInvolvement user_involvement, String filename);
  100. void initialize_the_navigation_api_entries_for_a_new_document(Vector<JS::NonnullGCPtr<SessionHistoryEntry>> const& new_shes, JS::NonnullGCPtr<SessionHistoryEntry> initial_she);
  101. void update_the_navigation_api_entries_for_a_same_document_navigation(JS::NonnullGCPtr<SessionHistoryEntry> destination_she, Bindings::NavigationType);
  102. virtual ~Navigation() override;
  103. // Internal Getters/Setters
  104. JS::GCPtr<NavigateEvent> ongoing_navigate_event() const { return m_ongoing_navigate_event; }
  105. bool focus_changed_during_ongoing_navigation() const { return m_focus_changed_during_ongoing_navigation; }
  106. void set_focus_changed_during_ongoing_navigation(bool b) { m_focus_changed_during_ongoing_navigation = b; }
  107. private:
  108. explicit Navigation(JS::Realm&);
  109. virtual void initialize(JS::Realm&) override;
  110. virtual void visit_edges(Visitor&) override;
  111. using AnyException = decltype(declval<WebIDL::ExceptionOr<void>>().exception());
  112. NavigationResult early_error_result(AnyException);
  113. JS::NonnullGCPtr<NavigationAPIMethodTracker> maybe_set_the_upcoming_non_traverse_api_method_tracker(JS::Value info, Optional<SerializationRecord>);
  114. JS::NonnullGCPtr<NavigationAPIMethodTracker> add_an_upcoming_traverse_api_method_tracker(String destination_key, JS::Value info);
  115. WebIDL::ExceptionOr<NavigationResult> perform_a_navigation_api_traversal(String key, NavigationOptions const&);
  116. void promote_an_upcoming_api_method_tracker_to_ongoing(Optional<String> destination_key);
  117. void resolve_the_finished_promise(JS::NonnullGCPtr<NavigationAPIMethodTracker>);
  118. void reject_the_finished_promise(JS::NonnullGCPtr<NavigationAPIMethodTracker>, JS::Value exception);
  119. void clean_up(JS::NonnullGCPtr<NavigationAPIMethodTracker>);
  120. void notify_about_the_committed_to_entry(JS::NonnullGCPtr<NavigationAPIMethodTracker>, JS::NonnullGCPtr<NavigationHistoryEntry>);
  121. void consume_history_action_user_activation(Window& window);
  122. bool inner_navigate_event_firing_algorithm(
  123. Bindings::NavigationType,
  124. JS::NonnullGCPtr<NavigationDestination>,
  125. UserNavigationInvolvement,
  126. Optional<Vector<XHR::FormDataEntry>&> form_data_entry_list,
  127. Optional<String> download_request_filename,
  128. Optional<SerializationRecord> classic_history_api_state);
  129. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-entry-list
  130. // Each Navigation has an associated entry list, a list of NavigationHistoryEntry objects, initially empty.
  131. Vector<JS::NonnullGCPtr<NavigationHistoryEntry>> m_entry_list;
  132. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-current-entry-index
  133. // Each Navigation has an associated current entry index, an integer, initially −1.
  134. i64 m_current_entry_index { -1 };
  135. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigation-transition
  136. // Each Navigation has a transition, which is a NavigationTransition or null, initially null.
  137. JS::GCPtr<NavigationTransition> m_transition { nullptr };
  138. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#ongoing-navigate-event
  139. JS::GCPtr<NavigateEvent> m_ongoing_navigate_event { nullptr };
  140. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#focus-changed-during-ongoing-navigation
  141. bool m_focus_changed_during_ongoing_navigation { false };
  142. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#suppress-normal-scroll-restoration-during-ongoing-navigation
  143. bool m_suppress_scroll_restoration_during_ongoing_navigation { false };
  144. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#ongoing-api-method-tracker
  145. JS::GCPtr<NavigationAPIMethodTracker> m_ongoing_api_method_tracker = nullptr;
  146. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#upcoming-non-traverse-api-method-tracker
  147. JS::GCPtr<NavigationAPIMethodTracker> m_upcoming_non_traverse_api_method_tracker = nullptr;
  148. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#upcoming-non-traverse-api-method-tracker
  149. HashMap<String, JS::NonnullGCPtr<NavigationAPIMethodTracker>> m_upcoming_traverse_api_method_trackers;
  150. };
  151. HistoryHandlingBehavior to_history_handling_behavior(Bindings::NavigationHistoryBehavior);
  152. Bindings::NavigationHistoryBehavior to_navigation_history_behavior(HistoryHandlingBehavior);
  153. }