Navigable.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.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/InvalidateDisplayList.h>
  23. #include <LibWeb/Page/EventHandler.h>
  24. #include <LibWeb/Painting/DisplayList.h>
  25. #include <LibWeb/PixelUnits.h>
  26. #include <LibWeb/XHR/FormDataEntry.h>
  27. namespace Web::HTML {
  28. enum class CSPNavigationType {
  29. Other,
  30. FormSubmission,
  31. };
  32. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#user-navigation-involvement
  33. enum class UserNavigationInvolvement {
  34. BrowserUI,
  35. Activation,
  36. None,
  37. };
  38. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#target-snapshot-params
  39. struct TargetSnapshotParams {
  40. SandboxingFlagSet sandboxing_flags {};
  41. };
  42. // https://html.spec.whatwg.org/multipage/document-sequences.html#navigable
  43. class Navigable : public JS::Cell {
  44. GC_CELL(Navigable, JS::Cell);
  45. GC_DECLARE_ALLOCATOR(Navigable);
  46. public:
  47. virtual ~Navigable() override;
  48. using NullOrError = Optional<StringView>;
  49. using NavigationParamsVariant = Variant<NullOrError, GC::Ref<NavigationParams>, GC::Ref<NonFetchSchemeNavigationParams>>;
  50. ErrorOr<void> initialize_navigable(GC::Ref<DocumentState> document_state, GC::Ptr<Navigable> parent);
  51. void register_navigation_observer(Badge<NavigationObserver>, NavigationObserver&);
  52. void unregister_navigation_observer(Badge<NavigationObserver>, NavigationObserver&);
  53. Vector<GC::Root<Navigable>> child_navigables() const;
  54. bool is_traversable() const;
  55. String const& id() const { return m_id; }
  56. GC::Ptr<Navigable> parent() const { return m_parent; }
  57. bool is_ancestor_of(GC::Ref<Navigable>) const;
  58. bool is_closing() const { return m_closing; }
  59. void set_closing(bool value) { m_closing = value; }
  60. bool is_script_closable();
  61. void stop_loading();
  62. void set_delaying_load_events(bool value);
  63. bool is_delaying_load_events() const { return m_delaying_the_load_event.has_value(); }
  64. GC::Ptr<SessionHistoryEntry> active_session_history_entry() const { return m_active_session_history_entry; }
  65. void set_active_session_history_entry(GC::Ptr<SessionHistoryEntry> entry) { m_active_session_history_entry = entry; }
  66. GC::Ptr<SessionHistoryEntry> current_session_history_entry() const { return m_current_session_history_entry; }
  67. void set_current_session_history_entry(GC::Ptr<SessionHistoryEntry> entry) { m_current_session_history_entry = entry; }
  68. Vector<GC::Ref<SessionHistoryEntry>>& get_session_history_entries() const;
  69. void activate_history_entry(GC::Ptr<SessionHistoryEntry>);
  70. GC::Ptr<DOM::Document> active_document();
  71. GC::Ptr<BrowsingContext> active_browsing_context();
  72. GC::Ptr<WindowProxy> active_window_proxy();
  73. GC::Ptr<Window> active_window();
  74. GC::Ptr<SessionHistoryEntry> get_the_target_history_entry(int target_step) const;
  75. String target_name() const;
  76. GC::Ptr<NavigableContainer> container() const;
  77. GC::Ptr<DOM::Document> container_document() const;
  78. GC::Ptr<TraversableNavigable> traversable_navigable() const;
  79. GC::Ptr<TraversableNavigable> top_level_traversable();
  80. virtual bool is_top_level_traversable() const { return false; }
  81. [[nodiscard]] bool is_focused() const;
  82. enum class WindowType {
  83. ExistingOrNone,
  84. NewAndUnrestricted,
  85. NewWithNoOpener,
  86. };
  87. struct ChosenNavigable {
  88. GC::Ptr<Navigable> navigable;
  89. WindowType window_type;
  90. };
  91. ChosenNavigable choose_a_navigable(StringView name, TokenizedFeature::NoOpener no_opener, ActivateTab = ActivateTab::Yes, Optional<TokenizedFeature::Map const&> window_features = {});
  92. static GC::Ptr<Navigable> navigable_with_active_document(GC::Ref<DOM::Document>);
  93. enum class Traversal {
  94. Tag
  95. };
  96. Variant<Empty, Traversal, String> ongoing_navigation() const { return m_ongoing_navigation; }
  97. void set_ongoing_navigation(Variant<Empty, Traversal, String> ongoing_navigation);
  98. WebIDL::ExceptionOr<void> populate_session_history_entry_document(
  99. GC::Ptr<SessionHistoryEntry> entry,
  100. SourceSnapshotParams const& source_snapshot_params,
  101. TargetSnapshotParams const& target_snapshot_params,
  102. Optional<String> navigation_id = {},
  103. NavigationParamsVariant navigation_params = Navigable::NullOrError {},
  104. CSPNavigationType csp_navigation_type = CSPNavigationType::Other,
  105. bool allow_POST = false,
  106. GC::Ptr<GC::Function<void()>> completion_steps = {});
  107. struct NavigateParams {
  108. URL::URL const& url;
  109. GC::Ref<DOM::Document> source_document;
  110. Variant<Empty, String, POSTResource> document_resource = Empty {};
  111. GC::Ptr<Fetch::Infrastructure::Response> response = nullptr;
  112. bool exceptions_enabled = false;
  113. Bindings::NavigationHistoryBehavior history_handling = Bindings::NavigationHistoryBehavior::Auto;
  114. Optional<SerializationRecord> navigation_api_state = {};
  115. Optional<Vector<XHR::FormDataEntry>&> form_data_entry_list = {};
  116. ReferrerPolicy::ReferrerPolicy referrer_policy = ReferrerPolicy::ReferrerPolicy::EmptyString;
  117. UserNavigationInvolvement user_involvement = UserNavigationInvolvement::None;
  118. };
  119. WebIDL::ExceptionOr<void> navigate(NavigateParams);
  120. WebIDL::ExceptionOr<void> navigate_to_a_fragment(URL::URL const&, HistoryHandlingBehavior, UserNavigationInvolvement, Optional<SerializationRecord> navigation_api_state, String navigation_id);
  121. WebIDL::ExceptionOr<GC::Ptr<DOM::Document>> evaluate_javascript_url(URL::URL const&, URL::Origin const& new_document_origin, String navigation_id);
  122. WebIDL::ExceptionOr<void> navigate_to_a_javascript_url(URL::URL const&, HistoryHandlingBehavior, URL::Origin const& initiator_origin, CSPNavigationType csp_navigation_type, String navigation_id);
  123. bool allowed_by_sandboxing_to_navigate(Navigable const& target, SourceSnapshotParams const&);
  124. void reload();
  125. // https://github.com/whatwg/html/issues/9690
  126. [[nodiscard]] bool has_been_destroyed() const { return m_has_been_destroyed; }
  127. void set_has_been_destroyed() { m_has_been_destroyed = true; }
  128. CSSPixelPoint to_top_level_position(CSSPixelPoint);
  129. CSSPixelRect to_top_level_rect(CSSPixelRect const&);
  130. CSSPixelSize size() const { return m_size; }
  131. CSSPixelPoint viewport_scroll_offset() const { return m_viewport_scroll_offset; }
  132. CSSPixelRect viewport_rect() const { return { m_viewport_scroll_offset, m_size }; }
  133. void set_viewport_size(CSSPixelSize);
  134. void perform_scroll_of_viewport(CSSPixelPoint position);
  135. void set_needs_display(InvalidateDisplayList = InvalidateDisplayList::Yes);
  136. // https://html.spec.whatwg.org/#rendering-opportunity
  137. [[nodiscard]] bool has_a_rendering_opportunity() const;
  138. [[nodiscard]] TargetSnapshotParams snapshot_target_snapshot_params();
  139. Page& page() { return m_page; }
  140. Page const& page() const { return m_page; }
  141. String selected_text() const;
  142. void select_all();
  143. void paste(String const&);
  144. Web::EventHandler& event_handler() { return m_event_handler; }
  145. Web::EventHandler const& event_handler() const { return m_event_handler; }
  146. protected:
  147. explicit Navigable(GC::Ref<Page>);
  148. virtual void visit_edges(Cell::Visitor&) override;
  149. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#ongoing-navigation
  150. Variant<Empty, Traversal, String> m_ongoing_navigation;
  151. private:
  152. void reset_cursor_blink_cycle();
  153. void scroll_offset_did_change();
  154. void inform_the_navigation_api_about_aborting_navigation();
  155. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-id
  156. String m_id;
  157. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-parent
  158. GC::Ptr<Navigable> m_parent;
  159. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-current-history-entry
  160. GC::Ptr<SessionHistoryEntry> m_current_session_history_entry;
  161. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-active-history-entry
  162. GC::Ptr<SessionHistoryEntry> m_active_session_history_entry;
  163. // https://html.spec.whatwg.org/multipage/document-sequences.html#is-closing
  164. bool m_closing { false };
  165. // https://html.spec.whatwg.org/multipage/document-sequences.html#delaying-load-events-mode
  166. Optional<DOM::DocumentLoadEventDelayer> m_delaying_the_load_event;
  167. // Implied link between navigable and its container.
  168. GC::Ptr<NavigableContainer> m_container;
  169. GC::Ref<Page> m_page;
  170. HashTable<GC::Ref<NavigationObserver>> m_navigation_observers;
  171. bool m_has_been_destroyed { false };
  172. CSSPixelSize m_size;
  173. CSSPixelPoint m_viewport_scroll_offset;
  174. Web::EventHandler m_event_handler;
  175. };
  176. HashTable<Navigable*>& all_navigables();
  177. bool navigation_must_be_a_replace(URL::URL const& url, DOM::Document const& document);
  178. void finalize_a_cross_document_navigation(GC::Ref<Navigable>, HistoryHandlingBehavior, GC::Ref<SessionHistoryEntry>);
  179. void perform_url_and_history_update_steps(DOM::Document& document, URL::URL new_url, Optional<SerializationRecord> = {}, HistoryHandlingBehavior history_handling = HistoryHandlingBehavior::Replace);
  180. UserNavigationInvolvement user_navigation_involvement(DOM::Event const&);
  181. }