Navigable.h 10 KB

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