Navigable.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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/String.h>
  9. #include <LibJS/Heap/Cell.h>
  10. #include <LibWeb/Bindings/NavigationPrototype.h>
  11. #include <LibWeb/DOM/DocumentLoadEventDelayer.h>
  12. #include <LibWeb/Forward.h>
  13. #include <LibWeb/HTML/ActivateTab.h>
  14. #include <LibWeb/HTML/HistoryHandlingBehavior.h>
  15. #include <LibWeb/HTML/POSTResource.h>
  16. #include <LibWeb/HTML/SandboxingFlagSet.h>
  17. #include <LibWeb/HTML/SourceSnapshotParams.h>
  18. #include <LibWeb/HTML/StructuredSerialize.h>
  19. #include <LibWeb/HTML/TokenizedFeatures.h>
  20. #include <LibWeb/XHR/FormDataEntry.h>
  21. namespace Web::HTML {
  22. enum class CSPNavigationType {
  23. Other,
  24. FormSubmission,
  25. };
  26. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#user-navigation-involvement
  27. enum class UserNaviagationInvolvement {
  28. BrowserUI,
  29. Activation,
  30. None,
  31. };
  32. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#target-snapshot-params
  33. struct TargetSnapshotParams {
  34. SandboxingFlagSet sandboxing_flags {};
  35. };
  36. // https://html.spec.whatwg.org/multipage/document-sequences.html#navigable
  37. class Navigable : public JS::Cell {
  38. JS_CELL(Navigable, JS::Cell);
  39. public:
  40. virtual ~Navigable() override;
  41. ErrorOr<void> initialize_navigable(JS::NonnullGCPtr<DocumentState> document_state, JS::GCPtr<Navigable> parent);
  42. Vector<JS::Handle<Navigable>> child_navigables() const;
  43. bool is_traversable() const;
  44. String const& id() const { return m_id; }
  45. JS::GCPtr<Navigable> parent() const { return m_parent; }
  46. bool is_closing() const { return m_closing; }
  47. void set_closing(bool value) { m_closing = value; }
  48. void set_delaying_load_events(bool value);
  49. JS::GCPtr<SessionHistoryEntry> active_session_history_entry() const { return m_active_session_history_entry; }
  50. void set_active_session_history_entry(JS::GCPtr<SessionHistoryEntry> entry) { m_active_session_history_entry = entry; }
  51. JS::GCPtr<SessionHistoryEntry> current_session_history_entry() const { return m_current_session_history_entry; }
  52. void set_current_session_history_entry(JS::GCPtr<SessionHistoryEntry> entry) { m_current_session_history_entry = entry; }
  53. Vector<JS::NonnullGCPtr<SessionHistoryEntry>>& get_session_history_entries() const;
  54. void activate_history_entry(JS::GCPtr<SessionHistoryEntry>);
  55. JS::GCPtr<DOM::Document> active_document();
  56. JS::GCPtr<BrowsingContext> active_browsing_context();
  57. JS::GCPtr<WindowProxy> active_window_proxy();
  58. JS::GCPtr<Window> active_window();
  59. JS::GCPtr<SessionHistoryEntry> get_the_target_history_entry(int target_step) const;
  60. String target_name() const;
  61. JS::GCPtr<NavigableContainer> container() const;
  62. JS::GCPtr<DOM::Document> container_document() const;
  63. JS::GCPtr<TraversableNavigable> traversable_navigable() const;
  64. JS::GCPtr<TraversableNavigable> top_level_traversable();
  65. virtual bool is_top_level_traversable() const { return false; }
  66. enum class WindowType {
  67. ExistingOrNone,
  68. NewAndUnrestricted,
  69. NewWithNoOpener,
  70. };
  71. struct ChosenNavigable {
  72. JS::GCPtr<Navigable> navigable;
  73. WindowType window_type;
  74. };
  75. ChosenNavigable choose_a_navigable(StringView name, TokenizedFeature::NoOpener no_opener, ActivateTab = ActivateTab::Yes);
  76. static JS::GCPtr<Navigable> navigable_with_active_document(JS::NonnullGCPtr<DOM::Document>);
  77. enum class Traversal {
  78. Tag
  79. };
  80. Variant<Empty, Traversal, String> ongoing_navigation() const { return m_ongoing_navigation; }
  81. void set_ongoing_navigation(Variant<Empty, Traversal, String> ongoing_navigation);
  82. WebIDL::ExceptionOr<void> populate_session_history_entry_document(JS::GCPtr<SessionHistoryEntry>, Optional<NavigationParams>, Optional<String> navigation_id, SourceSnapshotParams const&, bool allow_POST, Function<void()>);
  83. WebIDL::ExceptionOr<void> navigate(
  84. AK::URL const&,
  85. JS::NonnullGCPtr<DOM::Document> source_document,
  86. Variant<Empty, String, POSTResource> document_resource = Empty {},
  87. JS::GCPtr<Fetch::Infrastructure::Response> = nullptr,
  88. bool exceptions_enabled = false,
  89. Bindings::NavigationHistoryBehavior = Bindings::NavigationHistoryBehavior::Auto,
  90. Optional<SerializationRecord> navigation_api_state = {},
  91. Optional<Vector<XHR::FormDataEntry>&> form_data_entry_list = {},
  92. ReferrerPolicy::ReferrerPolicy = ReferrerPolicy::ReferrerPolicy::EmptyString,
  93. UserNaviagationInvolvement = UserNaviagationInvolvement::None);
  94. WebIDL::ExceptionOr<void> navigate_to_a_fragment(AK::URL const&, HistoryHandlingBehavior, String navigation_id);
  95. WebIDL::ExceptionOr<JS::GCPtr<DOM::Document>> evaluate_javascript_url(AK::URL const&, Origin const& new_document_origin, String navigation_id);
  96. WebIDL::ExceptionOr<void> navigate_to_a_javascript_url(AK::URL const&, HistoryHandlingBehavior, Origin const& initiator_origin, CSPNavigationType csp_navigation_type, String navigation_id);
  97. void reload();
  98. // https://github.com/whatwg/html/issues/9690
  99. [[nodiscard]] bool has_been_destroyed() const { return m_has_been_destroyed; }
  100. void set_has_been_destroyed() { m_has_been_destroyed = true; }
  101. protected:
  102. Navigable();
  103. virtual void visit_edges(Cell::Visitor&) override;
  104. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#ongoing-navigation
  105. Variant<Empty, Traversal, String> m_ongoing_navigation;
  106. private:
  107. bool allowed_by_sandboxing_to_navigate(Navigable const& target, SourceSnapshotParams const&);
  108. TargetSnapshotParams snapshot_target_snapshot_params();
  109. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-id
  110. String m_id;
  111. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-parent
  112. JS::GCPtr<Navigable> m_parent;
  113. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-current-history-entry
  114. JS::GCPtr<SessionHistoryEntry> m_current_session_history_entry;
  115. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-active-history-entry
  116. JS::GCPtr<SessionHistoryEntry> m_active_session_history_entry;
  117. // https://html.spec.whatwg.org/multipage/document-sequences.html#is-closing
  118. bool m_closing { false };
  119. // https://html.spec.whatwg.org/multipage/document-sequences.html#delaying-load-events-mode
  120. Optional<DOM::DocumentLoadEventDelayer> m_delaying_the_load_event;
  121. // Implied link between navigable and its container.
  122. JS::GCPtr<NavigableContainer> m_container;
  123. bool m_has_been_destroyed { false };
  124. };
  125. HashTable<Navigable*>& all_navigables();
  126. bool navigation_must_be_a_replace(AK::URL const& url, DOM::Document const& document);
  127. void finalize_a_cross_document_navigation(JS::NonnullGCPtr<Navigable>, HistoryHandlingBehavior, JS::NonnullGCPtr<SessionHistoryEntry>);
  128. void perform_url_and_history_update_steps(DOM::Document& document, AK::URL new_url, HistoryHandlingBehavior history_handling = HistoryHandlingBehavior::Reload);
  129. }