Navigable.h 7.0 KB

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