Navigation.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. public:
  61. [[nodiscard]] static JS::NonnullGCPtr<Navigation> create(JS::Realm&);
  62. // IDL properties and methods
  63. Vector<JS::NonnullGCPtr<NavigationHistoryEntry>> entries() const;
  64. JS::GCPtr<NavigationHistoryEntry> current_entry() const;
  65. WebIDL::ExceptionOr<void> update_current_entry(NavigationUpdateCurrentEntryOptions);
  66. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-transition
  67. JS::GCPtr<NavigationTransition> transition() const { return m_transition; }
  68. bool can_go_back() const;
  69. bool can_go_forward() const;
  70. WebIDL::ExceptionOr<NavigationResult> navigate(String url, NavigationNavigateOptions const&);
  71. WebIDL::ExceptionOr<NavigationResult> reload(NavigationReloadOptions const&);
  72. WebIDL::ExceptionOr<NavigationResult> traverse_to(String key, NavigationOptions const&);
  73. WebIDL::ExceptionOr<NavigationResult> back(NavigationOptions const&);
  74. WebIDL::ExceptionOr<NavigationResult> forward(NavigationOptions const&);
  75. // Event Handlers
  76. void set_onnavigate(WebIDL::CallbackType*);
  77. WebIDL::CallbackType* onnavigate();
  78. void set_onnavigatesuccess(WebIDL::CallbackType*);
  79. WebIDL::CallbackType* onnavigatesuccess();
  80. void set_onnavigateerror(WebIDL::CallbackType*);
  81. WebIDL::CallbackType* onnavigateerror();
  82. void set_oncurrententrychange(WebIDL::CallbackType*);
  83. WebIDL::CallbackType* oncurrententrychange();
  84. // Abstract Operations
  85. bool has_entries_and_events_disabled() const;
  86. i64 get_the_navigation_api_entry_index(SessionHistoryEntry const&) const;
  87. void abort_the_ongoing_navigation(Optional<JS::NonnullGCPtr<WebIDL::DOMException>> error = {});
  88. virtual ~Navigation() override;
  89. // Internal Getters/Setters
  90. JS::GCPtr<NavigateEvent> ongoing_navigate_event() const { return m_ongoing_navigate_event; }
  91. bool focus_changed_during_ongoing_navigation() const { return m_focus_changed_during_ongoing_navigation; }
  92. void set_focus_changed_during_ongoing_navigation(bool b) { m_focus_changed_during_ongoing_navigation = b; }
  93. private:
  94. explicit Navigation(JS::Realm&);
  95. virtual void initialize(JS::Realm&) override;
  96. virtual void visit_edges(Visitor&) override;
  97. using AnyException = decltype(declval<WebIDL::ExceptionOr<void>>().exception());
  98. NavigationResult early_error_result(AnyException);
  99. JS::NonnullGCPtr<NavigationAPIMethodTracker> maybe_set_the_upcoming_non_traverse_api_method_tracker(JS::Value info, Optional<SerializationRecord>);
  100. JS::NonnullGCPtr<NavigationAPIMethodTracker> add_an_upcoming_traverse_api_method_tracker(String destination_key, JS::Value info);
  101. WebIDL::ExceptionOr<NavigationResult> perform_a_navigation_api_traversal(String key, NavigationOptions const&);
  102. void promote_an_upcoming_api_method_tracker_to_ongoing(Optional<String> destination_key);
  103. void resolve_the_finished_promise(JS::NonnullGCPtr<NavigationAPIMethodTracker>);
  104. void reject_the_finished_promise(JS::NonnullGCPtr<NavigationAPIMethodTracker>, JS::Value exception);
  105. void clean_up(JS::NonnullGCPtr<NavigationAPIMethodTracker>);
  106. bool inner_navigate_event_firing_algorithm(
  107. Bindings::NavigationType,
  108. JS::NonnullGCPtr<NavigationDestination>,
  109. UserNavigationInvolvement,
  110. Optional<Vector<XHR::FormDataEntry>&> form_data_entry_list,
  111. Optional<String> download_request_filename,
  112. Optional<SerializationRecord> classic_history_api_state);
  113. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-entry-list
  114. // Each Navigation has an associated entry list, a list of NavigationHistoryEntry objects, initially empty.
  115. Vector<JS::NonnullGCPtr<NavigationHistoryEntry>> m_entry_list;
  116. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-current-entry-index
  117. // Each Navigation has an associated current entry index, an integer, initially −1.
  118. i64 m_current_entry_index { -1 };
  119. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigation-transition
  120. // Each Navigation has a transition, which is a NavigationTransition or null, initially null.
  121. JS::GCPtr<NavigationTransition> m_transition { nullptr };
  122. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#ongoing-navigate-event
  123. JS::GCPtr<NavigateEvent> m_ongoing_navigate_event { nullptr };
  124. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#focus-changed-during-ongoing-navigation
  125. bool m_focus_changed_during_ongoing_navigation { false };
  126. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#suppress-normal-scroll-restoration-during-ongoing-navigation
  127. bool m_suppress_scroll_restoration_during_ongoing_navigation { false };
  128. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#ongoing-api-method-tracker
  129. JS::GCPtr<NavigationAPIMethodTracker> m_ongoing_api_method_tracker = nullptr;
  130. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#upcoming-non-traverse-api-method-tracker
  131. JS::GCPtr<NavigationAPIMethodTracker> m_upcoming_non_traverse_api_method_tracker = nullptr;
  132. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#upcoming-non-traverse-api-method-tracker
  133. HashMap<String, JS::NonnullGCPtr<NavigationAPIMethodTracker>> m_upcoming_traverse_api_method_trackers;
  134. };
  135. HistoryHandlingBehavior to_history_handling_behavior(Bindings::NavigationHistoryBehavior);
  136. }