Navigable.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. JS::GCPtr<SessionHistoryEntry> current_session_history_entry() const { return m_current_session_history_entry; }
  50. void set_current_session_history_entry(JS::GCPtr<SessionHistoryEntry> entry) { m_current_session_history_entry = entry; }
  51. Vector<JS::NonnullGCPtr<SessionHistoryEntry>>& get_session_history_entries() const;
  52. void activate_history_entry(JS::GCPtr<SessionHistoryEntry>);
  53. JS::GCPtr<DOM::Document> active_document();
  54. JS::GCPtr<BrowsingContext> active_browsing_context();
  55. JS::GCPtr<WindowProxy> active_window_proxy();
  56. JS::GCPtr<Window> active_window();
  57. JS::GCPtr<SessionHistoryEntry> get_the_target_history_entry(int target_step) const;
  58. String target_name() const;
  59. JS::GCPtr<NavigableContainer> container() const;
  60. JS::GCPtr<DOM::Document> container_document() const;
  61. JS::GCPtr<TraversableNavigable> traversable_navigable() const;
  62. JS::GCPtr<TraversableNavigable> top_level_traversable();
  63. virtual bool is_top_level_traversable() const { return false; }
  64. enum class WindowType {
  65. ExistingOrNone,
  66. NewAndUnrestricted,
  67. NewWithNoOpener,
  68. };
  69. struct ChosenNavigable {
  70. JS::GCPtr<Navigable> navigable;
  71. WindowType window_type;
  72. };
  73. ChosenNavigable choose_a_navigable(StringView name, TokenizedFeature::NoOpener no_opener, ActivateTab = ActivateTab::Yes);
  74. static JS::GCPtr<Navigable> navigable_with_active_document(JS::NonnullGCPtr<DOM::Document>);
  75. enum class Traversal {
  76. Tag
  77. };
  78. Variant<Empty, Traversal, String> ongoing_navigation() const { return m_ongoing_navigation; }
  79. WebIDL::ExceptionOr<void> populate_session_history_entry_document(JS::GCPtr<SessionHistoryEntry>, Optional<NavigationParams>, Optional<String> navigation_id, SourceSnapshotParams const&, bool allow_POST, Function<void()>);
  80. WebIDL::ExceptionOr<void> navigate(
  81. AK::URL const&,
  82. JS::NonnullGCPtr<DOM::Document> source_document,
  83. Variant<Empty, String, POSTResource> document_resource = Empty {},
  84. JS::GCPtr<Fetch::Infrastructure::Response> = nullptr,
  85. bool exceptions_enabled = false,
  86. Bindings::NavigationHistoryBehavior = Bindings::NavigationHistoryBehavior::Auto,
  87. Optional<SerializationRecord> navigation_api_state = {},
  88. Optional<Vector<XHR::FormDataEntry>&> form_data_entry_list = {},
  89. ReferrerPolicy::ReferrerPolicy = ReferrerPolicy::ReferrerPolicy::EmptyString,
  90. UserNaviagationInvolvement = UserNaviagationInvolvement::None);
  91. WebIDL::ExceptionOr<void> navigate_to_a_fragment(AK::URL const&, HistoryHandlingBehavior, String navigation_id);
  92. WebIDL::ExceptionOr<void> navigate_to_a_javascript_url(AK::URL const&, HistoryHandlingBehavior, Origin const& initiator_origin, CSPNavigationType csp_navigation_type);
  93. void reload();
  94. protected:
  95. Navigable();
  96. virtual void visit_edges(Cell::Visitor&) override;
  97. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#ongoing-navigation
  98. Variant<Empty, Traversal, String> m_ongoing_navigation;
  99. private:
  100. bool allowed_by_sandboxing_to_navigate(Navigable const& target, SourceSnapshotParams const&);
  101. TargetSnapshotParams snapshot_target_snapshot_params();
  102. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-id
  103. String m_id;
  104. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-parent
  105. JS::GCPtr<Navigable> m_parent;
  106. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-current-history-entry
  107. JS::GCPtr<SessionHistoryEntry> m_current_session_history_entry;
  108. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-active-history-entry
  109. JS::GCPtr<SessionHistoryEntry> m_active_session_history_entry;
  110. // https://html.spec.whatwg.org/multipage/document-sequences.html#is-closing
  111. bool m_closing { false };
  112. // https://html.spec.whatwg.org/multipage/document-sequences.html#delaying-load-events-mode
  113. bool m_delaying_load_events { false };
  114. // Implied link between navigable and its container.
  115. JS::GCPtr<NavigableContainer> m_container;
  116. };
  117. bool navigation_must_be_a_replace(AK::URL const& url, DOM::Document const& document);
  118. void finalize_a_cross_document_navigation(JS::NonnullGCPtr<Navigable>, HistoryHandlingBehavior, JS::NonnullGCPtr<SessionHistoryEntry>);
  119. }