Navigation.h 6.6 KB

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