Navigable.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. WebIDL::ExceptionOr<void> populate_session_history_entry_document(JS::GCPtr<SessionHistoryEntry>, Optional<NavigationParams>, Optional<String> navigation_id, SourceSnapshotParams const&, bool allow_POST, Function<void()>);
  81. WebIDL::ExceptionOr<void> navigate(
  82. AK::URL const&,
  83. JS::NonnullGCPtr<DOM::Document> source_document,
  84. Variant<Empty, String, POSTResource> document_resource = Empty {},
  85. JS::GCPtr<Fetch::Infrastructure::Response> = nullptr,
  86. bool exceptions_enabled = false,
  87. Bindings::NavigationHistoryBehavior = Bindings::NavigationHistoryBehavior::Auto,
  88. Optional<SerializationRecord> navigation_api_state = {},
  89. Optional<Vector<XHR::FormDataEntry>&> form_data_entry_list = {},
  90. ReferrerPolicy::ReferrerPolicy = ReferrerPolicy::ReferrerPolicy::EmptyString,
  91. UserNaviagationInvolvement = UserNaviagationInvolvement::None);
  92. WebIDL::ExceptionOr<void> navigate_to_a_fragment(AK::URL const&, HistoryHandlingBehavior, String navigation_id);
  93. WebIDL::ExceptionOr<void> navigate_to_a_javascript_url(AK::URL const&, HistoryHandlingBehavior, Origin const& initiator_origin, CSPNavigationType csp_navigation_type);
  94. void reload();
  95. protected:
  96. Navigable();
  97. virtual void visit_edges(Cell::Visitor&) override;
  98. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#ongoing-navigation
  99. Variant<Empty, Traversal, String> m_ongoing_navigation;
  100. private:
  101. bool allowed_by_sandboxing_to_navigate(Navigable const& target, SourceSnapshotParams const&);
  102. TargetSnapshotParams snapshot_target_snapshot_params();
  103. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-id
  104. String m_id;
  105. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-parent
  106. JS::GCPtr<Navigable> m_parent;
  107. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-current-history-entry
  108. JS::GCPtr<SessionHistoryEntry> m_current_session_history_entry;
  109. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-active-history-entry
  110. JS::GCPtr<SessionHistoryEntry> m_active_session_history_entry;
  111. // https://html.spec.whatwg.org/multipage/document-sequences.html#is-closing
  112. bool m_closing { false };
  113. // https://html.spec.whatwg.org/multipage/document-sequences.html#delaying-load-events-mode
  114. bool m_delaying_load_events { false };
  115. // Implied link between navigable and its container.
  116. JS::GCPtr<NavigableContainer> m_container;
  117. };
  118. bool navigation_must_be_a_replace(AK::URL const& url, DOM::Document const& document);
  119. void finalize_a_cross_document_navigation(JS::NonnullGCPtr<Navigable>, HistoryHandlingBehavior, JS::NonnullGCPtr<SessionHistoryEntry>);
  120. void perform_url_and_history_update_steps(DOM::Document& document, AK::URL new_url, HistoryHandlingBehavior history_handling = HistoryHandlingBehavior::Reload);
  121. }