Navigation.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/StructuredSerialize.h>
  12. namespace Web::HTML {
  13. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationupdatecurrententryoptions
  14. struct NavigationUpdateCurrentEntryOptions {
  15. JS::Value state;
  16. };
  17. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationoptions
  18. struct NavigationOptions {
  19. Optional<JS::Value> info;
  20. };
  21. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationnavigateoptions
  22. struct NavigationNavigateOptions : public NavigationOptions {
  23. Optional<JS::Value> state;
  24. Bindings::NavigationHistoryBehavior history = Bindings::NavigationHistoryBehavior::Auto;
  25. };
  26. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationreloadoptions
  27. struct NavigationReloadOptions : public NavigationOptions {
  28. Optional<JS::Value> state;
  29. };
  30. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationresult
  31. struct NavigationResult {
  32. // FIXME: Are we supposed to return a PromiseCapability (WebIDL::Promise) here?
  33. JS::NonnullGCPtr<JS::Object> committed;
  34. JS::NonnullGCPtr<JS::Object> finished;
  35. };
  36. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-api-method-tracker
  37. struct NavigationAPIMethodTracker final : public JS::Cell {
  38. JS_CELL(NavigationAPIMethodTracker, JS::Cell);
  39. NavigationAPIMethodTracker(JS::NonnullGCPtr<Navigation> navigation,
  40. Optional<String> key,
  41. JS::Value info,
  42. Optional<SerializationRecord> serialized_state,
  43. JS::GCPtr<NavigationHistoryEntry> commited_to_entry,
  44. JS::NonnullGCPtr<WebIDL::Promise> committed_promise,
  45. JS::NonnullGCPtr<WebIDL::Promise> finished_promise);
  46. virtual void visit_edges(Cell::Visitor&) override;
  47. JS::NonnullGCPtr<Navigation> navigation;
  48. Optional<String> key;
  49. JS::Value info;
  50. Optional<SerializationRecord> serialized_state;
  51. JS::GCPtr<NavigationHistoryEntry> commited_to_entry;
  52. JS::NonnullGCPtr<WebIDL::Promise> committed_promise;
  53. JS::NonnullGCPtr<WebIDL::Promise> finished_promise;
  54. };
  55. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-interface
  56. class Navigation : public DOM::EventTarget {
  57. WEB_PLATFORM_OBJECT(Navigation, DOM::EventTarget);
  58. public:
  59. [[nodiscard]] static JS::NonnullGCPtr<Navigation> create(JS::Realm&);
  60. // IDL properties and methods
  61. Vector<JS::NonnullGCPtr<NavigationHistoryEntry>> entries() const;
  62. JS::GCPtr<NavigationHistoryEntry> current_entry() const;
  63. WebIDL::ExceptionOr<void> update_current_entry(NavigationUpdateCurrentEntryOptions);
  64. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-transition
  65. JS::GCPtr<NavigationTransition> transition() const { return m_transition; }
  66. bool can_go_back() const;
  67. bool can_go_forward() const;
  68. WebIDL::ExceptionOr<NavigationResult> navigate(String url, NavigationNavigateOptions const&);
  69. WebIDL::ExceptionOr<NavigationResult> reload(NavigationReloadOptions const&);
  70. WebIDL::ExceptionOr<NavigationResult> traverse_to(String key, NavigationOptions const&);
  71. WebIDL::ExceptionOr<NavigationResult> back(NavigationOptions const&);
  72. WebIDL::ExceptionOr<NavigationResult> forward(NavigationOptions const&);
  73. // Event Handlers
  74. void set_onnavigate(WebIDL::CallbackType*);
  75. WebIDL::CallbackType* onnavigate();
  76. void set_onnavigatesuccess(WebIDL::CallbackType*);
  77. WebIDL::CallbackType* onnavigatesuccess();
  78. void set_onnavigateerror(WebIDL::CallbackType*);
  79. WebIDL::CallbackType* onnavigateerror();
  80. void set_oncurrententrychange(WebIDL::CallbackType*);
  81. WebIDL::CallbackType* oncurrententrychange();
  82. // Abstract Operations
  83. bool has_entries_and_events_disabled() const;
  84. i64 get_the_navigation_api_entry_index(SessionHistoryEntry const&) const;
  85. virtual ~Navigation() override;
  86. private:
  87. explicit Navigation(JS::Realm&);
  88. virtual void initialize(JS::Realm&) override;
  89. virtual void visit_edges(Visitor&) override;
  90. using AnyException = decltype(declval<WebIDL::ExceptionOr<void>>().exception());
  91. NavigationResult early_error_result(AnyException);
  92. JS::NonnullGCPtr<NavigationAPIMethodTracker> maybe_set_the_upcoming_non_traverse_api_method_tracker(JS::Value info, Optional<SerializationRecord>);
  93. JS::NonnullGCPtr<NavigationAPIMethodTracker> add_an_upcoming_traverse_api_method_tracker(String destination_key, JS::Value info);
  94. WebIDL::ExceptionOr<NavigationResult> perform_a_navigation_api_traversal(String key, NavigationOptions const&);
  95. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-entry-list
  96. // Each Navigation has an associated entry list, a list of NavigationHistoryEntry objects, initially empty.
  97. Vector<JS::NonnullGCPtr<NavigationHistoryEntry>> m_entry_list;
  98. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-current-entry-index
  99. // Each Navigation has an associated current entry index, an integer, initially −1.
  100. i64 m_current_entry_index { -1 };
  101. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigation-transition
  102. // Each Navigation has a transition, which is a NavigationTransition or null, initially null.
  103. JS::GCPtr<NavigationTransition> m_transition { nullptr };
  104. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#ongoing-navigate-event
  105. JS::GCPtr<NavigateEvent> m_ongoing_navigate_event { nullptr };
  106. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#focus-changed-during-ongoing-navigation
  107. bool m_focus_changed_during_ongoing_navigation { false };
  108. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#suppress-normal-scroll-restoration-during-ongoing-navigation
  109. bool m_suppress_scroll_restoration_during_ongoing_navigation { false };
  110. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#ongoing-api-method-tracker
  111. JS::GCPtr<NavigationAPIMethodTracker> m_ongoing_api_method_tracker = nullptr;
  112. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#upcoming-non-traverse-api-method-tracker
  113. JS::GCPtr<NavigationAPIMethodTracker> m_upcoming_non_traverse_api_method_tracker = nullptr;
  114. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#upcoming-non-traverse-api-method-tracker
  115. HashMap<String, JS::NonnullGCPtr<NavigationAPIMethodTracker>> m_upcoming_traverse_api_method_trackers;
  116. };
  117. HistoryHandlingBehavior to_history_handling_behavior(Bindings::NavigationHistoryBehavior);
  118. }