Navigable.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/HashTable.h>
  9. #include <AK/String.h>
  10. #include <LibJS/Heap/Cell.h>
  11. #include <LibWeb/Bindings/NavigationPrototype.h>
  12. #include <LibWeb/DOM/DocumentLoadEventDelayer.h>
  13. #include <LibWeb/Forward.h>
  14. #include <LibWeb/HTML/ActivateTab.h>
  15. #include <LibWeb/HTML/HistoryHandlingBehavior.h>
  16. #include <LibWeb/HTML/NavigationParams.h>
  17. #include <LibWeb/HTML/POSTResource.h>
  18. #include <LibWeb/HTML/SandboxingFlagSet.h>
  19. #include <LibWeb/HTML/SourceSnapshotParams.h>
  20. #include <LibWeb/HTML/StructuredSerialize.h>
  21. #include <LibWeb/HTML/TokenizedFeatures.h>
  22. #include <LibWeb/PixelUnits.h>
  23. #include <LibWeb/XHR/FormDataEntry.h>
  24. namespace Web::HTML {
  25. enum class CSPNavigationType {
  26. Other,
  27. FormSubmission,
  28. };
  29. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#user-navigation-involvement
  30. enum class UserNavigationInvolvement {
  31. BrowserUI,
  32. Activation,
  33. None,
  34. };
  35. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#target-snapshot-params
  36. struct TargetSnapshotParams {
  37. SandboxingFlagSet sandboxing_flags {};
  38. };
  39. // https://html.spec.whatwg.org/multipage/document-sequences.html#navigable
  40. class Navigable : public JS::Cell {
  41. JS_CELL(Navigable, JS::Cell);
  42. public:
  43. virtual ~Navigable() override;
  44. ErrorOr<void> initialize_navigable(JS::NonnullGCPtr<DocumentState> document_state, JS::GCPtr<Navigable> parent);
  45. Vector<JS::Handle<Navigable>> child_navigables() const;
  46. bool is_traversable() const;
  47. String const& id() const { return m_id; }
  48. JS::GCPtr<Navigable> parent() const { return m_parent; }
  49. bool is_closing() const { return m_closing; }
  50. void set_closing(bool value) { m_closing = value; }
  51. void set_delaying_load_events(bool value);
  52. JS::GCPtr<SessionHistoryEntry> active_session_history_entry() const { return m_active_session_history_entry; }
  53. void set_active_session_history_entry(JS::GCPtr<SessionHistoryEntry> entry) { m_active_session_history_entry = entry; }
  54. JS::GCPtr<SessionHistoryEntry> current_session_history_entry() const { return m_current_session_history_entry; }
  55. void set_current_session_history_entry(JS::GCPtr<SessionHistoryEntry> entry) { m_current_session_history_entry = entry; }
  56. Vector<JS::NonnullGCPtr<SessionHistoryEntry>>& get_session_history_entries() const;
  57. void activate_history_entry(JS::GCPtr<SessionHistoryEntry>);
  58. JS::GCPtr<DOM::Document> active_document();
  59. JS::GCPtr<BrowsingContext> active_browsing_context();
  60. JS::GCPtr<WindowProxy> active_window_proxy();
  61. JS::GCPtr<Window> active_window();
  62. JS::GCPtr<SessionHistoryEntry> get_the_target_history_entry(int target_step) const;
  63. String target_name() const;
  64. JS::GCPtr<NavigableContainer> container() const;
  65. JS::GCPtr<DOM::Document> container_document() const;
  66. JS::GCPtr<TraversableNavigable> traversable_navigable() const;
  67. JS::GCPtr<TraversableNavigable> top_level_traversable();
  68. virtual bool is_top_level_traversable() const { return false; }
  69. enum class WindowType {
  70. ExistingOrNone,
  71. NewAndUnrestricted,
  72. NewWithNoOpener,
  73. };
  74. struct ChosenNavigable {
  75. JS::GCPtr<Navigable> navigable;
  76. WindowType window_type;
  77. };
  78. ChosenNavigable choose_a_navigable(StringView name, TokenizedFeature::NoOpener no_opener, ActivateTab = ActivateTab::Yes);
  79. static JS::GCPtr<Navigable> navigable_with_active_document(JS::NonnullGCPtr<DOM::Document>);
  80. enum class Traversal {
  81. Tag
  82. };
  83. Variant<Empty, Traversal, String> ongoing_navigation() const { return m_ongoing_navigation; }
  84. void set_ongoing_navigation(Variant<Empty, Traversal, String> ongoing_navigation);
  85. WebIDL::ExceptionOr<void> populate_session_history_entry_document(
  86. JS::GCPtr<SessionHistoryEntry> entry,
  87. SourceSnapshotParams const& source_snapshot_params,
  88. TargetSnapshotParams const& target_snapshot_params,
  89. Optional<String> navigation_id = {},
  90. Variant<Empty, NavigationParams, NonFetchSchemeNavigationParams> navigation_params = Empty {},
  91. CSPNavigationType csp_navigation_type = CSPNavigationType::Other,
  92. bool allow_POST = false,
  93. Function<void()> completion_steps = [] {});
  94. struct NavigateParams {
  95. AK::URL const& url;
  96. JS::NonnullGCPtr<DOM::Document> source_document;
  97. Variant<Empty, String, POSTResource> document_resource = Empty {};
  98. JS::GCPtr<Fetch::Infrastructure::Response> response = nullptr;
  99. bool exceptions_enabled = false;
  100. Bindings::NavigationHistoryBehavior history_handling = Bindings::NavigationHistoryBehavior::Auto;
  101. Optional<SerializationRecord> navigation_api_state = {};
  102. Optional<Vector<XHR::FormDataEntry>&> form_data_entry_list = {};
  103. ReferrerPolicy::ReferrerPolicy referrer_policy = ReferrerPolicy::ReferrerPolicy::EmptyString;
  104. UserNavigationInvolvement user_involvement = UserNavigationInvolvement::None;
  105. };
  106. WebIDL::ExceptionOr<void> navigate(NavigateParams);
  107. WebIDL::ExceptionOr<void> navigate_to_a_fragment(AK::URL const&, HistoryHandlingBehavior, String navigation_id);
  108. WebIDL::ExceptionOr<JS::GCPtr<DOM::Document>> evaluate_javascript_url(AK::URL const&, Origin const& new_document_origin, String navigation_id);
  109. WebIDL::ExceptionOr<void> navigate_to_a_javascript_url(AK::URL const&, HistoryHandlingBehavior, Origin const& initiator_origin, CSPNavigationType csp_navigation_type, String navigation_id);
  110. void reload();
  111. // https://github.com/whatwg/html/issues/9690
  112. [[nodiscard]] bool has_been_destroyed() const { return m_has_been_destroyed; }
  113. void set_has_been_destroyed() { m_has_been_destroyed = true; }
  114. CSSPixelPoint to_top_level_position(CSSPixelPoint);
  115. CSSPixelRect to_top_level_rect(CSSPixelRect const&);
  116. CSSPixelSize size() const { return m_size; }
  117. void set_size(CSSPixelSize);
  118. CSSPixelPoint viewport_scroll_offset() const { return m_viewport_scroll_offset; }
  119. CSSPixelRect viewport_rect() const { return { m_viewport_scroll_offset, m_size }; }
  120. void set_viewport_rect(CSSPixelRect const&);
  121. void set_needs_display();
  122. void set_needs_display(CSSPixelRect const&);
  123. void set_is_popup(TokenizedFeature::Popup is_popup) { m_is_popup = is_popup; }
  124. // https://html.spec.whatwg.org/#rendering-opportunity
  125. [[nodiscard]] bool has_a_rendering_opportunity() const;
  126. [[nodiscard]] TargetSnapshotParams snapshot_target_snapshot_params();
  127. protected:
  128. Navigable();
  129. virtual void visit_edges(Cell::Visitor&) override;
  130. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#ongoing-navigation
  131. Variant<Empty, Traversal, String> m_ongoing_navigation;
  132. // https://html.spec.whatwg.org/multipage/browsers.html#is-popup
  133. TokenizedFeature::Popup m_is_popup { TokenizedFeature::Popup::No };
  134. private:
  135. bool allowed_by_sandboxing_to_navigate(Navigable const& target, SourceSnapshotParams const&);
  136. void scroll_offset_did_change();
  137. void inform_the_navigation_api_about_aborting_navigation();
  138. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-id
  139. String m_id;
  140. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-parent
  141. JS::GCPtr<Navigable> m_parent;
  142. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-current-history-entry
  143. JS::GCPtr<SessionHistoryEntry> m_current_session_history_entry;
  144. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-active-history-entry
  145. JS::GCPtr<SessionHistoryEntry> m_active_session_history_entry;
  146. // https://html.spec.whatwg.org/multipage/document-sequences.html#is-closing
  147. bool m_closing { false };
  148. // https://html.spec.whatwg.org/multipage/document-sequences.html#delaying-load-events-mode
  149. Optional<DOM::DocumentLoadEventDelayer> m_delaying_the_load_event;
  150. // Implied link between navigable and its container.
  151. JS::GCPtr<NavigableContainer> m_container;
  152. bool m_has_been_destroyed { false };
  153. CSSPixelSize m_size;
  154. CSSPixelPoint m_viewport_scroll_offset;
  155. };
  156. HashTable<Navigable*>& all_navigables();
  157. bool navigation_must_be_a_replace(AK::URL const& url, DOM::Document const& document);
  158. void finalize_a_cross_document_navigation(JS::NonnullGCPtr<Navigable>, HistoryHandlingBehavior, JS::NonnullGCPtr<SessionHistoryEntry>);
  159. void perform_url_and_history_update_steps(DOM::Document& document, AK::URL new_url, HistoryHandlingBehavior history_handling = HistoryHandlingBehavior::Reload);
  160. }