TraversableNavigable.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Vector.h>
  8. #include <LibWeb/HTML/Navigable.h>
  9. #include <LibWeb/HTML/NavigationType.h>
  10. #include <LibWeb/HTML/SessionHistoryTraversalQueue.h>
  11. #include <LibWeb/HTML/VisibilityState.h>
  12. namespace Web::HTML {
  13. // https://html.spec.whatwg.org/multipage/document-sequences.html#traversable-navigable
  14. class TraversableNavigable final : public Navigable {
  15. JS_CELL(TraversableNavigable, Navigable);
  16. JS_DECLARE_ALLOCATOR(TraversableNavigable);
  17. public:
  18. static WebIDL::ExceptionOr<JS::NonnullGCPtr<TraversableNavigable>> create_a_new_top_level_traversable(JS::NonnullGCPtr<Page>, JS::GCPtr<BrowsingContext> opener, String target_name);
  19. static WebIDL::ExceptionOr<JS::NonnullGCPtr<TraversableNavigable>> create_a_fresh_top_level_traversable(JS::NonnullGCPtr<Page>, URL::URL const& initial_navigation_url, Variant<Empty, String, POSTResource> = Empty {});
  20. virtual ~TraversableNavigable() override;
  21. virtual bool is_top_level_traversable() const override;
  22. int current_session_history_step() const { return m_current_session_history_step; }
  23. Vector<JS::NonnullGCPtr<SessionHistoryEntry>>& session_history_entries() { return m_session_history_entries; }
  24. Vector<JS::NonnullGCPtr<SessionHistoryEntry>> const& session_history_entries() const { return m_session_history_entries; }
  25. bool running_nested_apply_history_step() const { return m_running_nested_apply_history_step; }
  26. VisibilityState system_visibility_state() const { return m_system_visibility_state; }
  27. void set_system_visibility_state(VisibilityState);
  28. struct HistoryObjectLengthAndIndex {
  29. u64 script_history_length;
  30. u64 script_history_index;
  31. };
  32. HistoryObjectLengthAndIndex get_the_history_object_length_and_index(int) const;
  33. enum class HistoryStepResult {
  34. InitiatorDisallowed,
  35. CanceledByBeforeUnload,
  36. CanceledByNavigate,
  37. Applied,
  38. };
  39. HistoryStepResult apply_the_traverse_history_step(int, Optional<SourceSnapshotParams>, JS::GCPtr<Navigable>, UserNavigationInvolvement);
  40. HistoryStepResult apply_the_reload_history_step();
  41. HistoryStepResult apply_the_push_or_replace_history_step(int step, HistoryHandlingBehavior history_handling);
  42. HistoryStepResult update_for_navigable_creation_or_destruction();
  43. int get_the_used_step(int step) const;
  44. Vector<JS::Handle<Navigable>> get_all_navigables_whose_current_session_history_entry_will_change_or_reload(int) const;
  45. Vector<JS::Handle<Navigable>> get_all_navigables_that_only_need_history_object_length_index_update(int) const;
  46. Vector<JS::Handle<Navigable>> get_all_navigables_that_might_experience_a_cross_document_traversal(int) const;
  47. Vector<int> get_all_used_history_steps() const;
  48. void clear_the_forward_session_history();
  49. void traverse_the_history_by_delta(int delta, Optional<DOM::Document&> source_document = {});
  50. void close_top_level_traversable();
  51. void destroy_top_level_traversable();
  52. void append_session_history_traversal_steps(JS::SafeFunction<void()> steps)
  53. {
  54. m_session_history_traversal_queue.append(move(steps));
  55. }
  56. void append_session_history_synchronous_navigation_steps(JS::NonnullGCPtr<Navigable> target_navigable, JS::SafeFunction<void()> steps)
  57. {
  58. m_session_history_traversal_queue.append_sync(move(steps), target_navigable);
  59. }
  60. void process_session_history_traversal_queue()
  61. {
  62. m_session_history_traversal_queue.process();
  63. }
  64. Page& page() { return m_page; }
  65. Page const& page() const { return m_page; }
  66. String window_handle() const { return m_window_handle; }
  67. void set_window_handle(String window_handle) { m_window_handle = move(window_handle); }
  68. private:
  69. TraversableNavigable(JS::NonnullGCPtr<Page>);
  70. virtual void visit_edges(Cell::Visitor&) override;
  71. // FIXME: Fix spec typo cancelation --> cancellation
  72. HistoryStepResult apply_the_history_step(
  73. int step,
  74. bool check_for_cancelation,
  75. Optional<SourceSnapshotParams>,
  76. JS::GCPtr<Navigable> initiator_to_check,
  77. Optional<UserNavigationInvolvement> user_involvement_for_navigate_events,
  78. Optional<Bindings::NavigationType> navigation_type);
  79. Vector<JS::NonnullGCPtr<SessionHistoryEntry>> get_session_history_entries_for_the_navigation_api(JS::NonnullGCPtr<Navigable>, int);
  80. // https://html.spec.whatwg.org/multipage/document-sequences.html#tn-current-session-history-step
  81. int m_current_session_history_step { 0 };
  82. // https://html.spec.whatwg.org/multipage/document-sequences.html#tn-session-history-entries
  83. Vector<JS::NonnullGCPtr<SessionHistoryEntry>> m_session_history_entries;
  84. // FIXME: https://html.spec.whatwg.org/multipage/document-sequences.html#tn-session-history-traversal-queue
  85. // https://html.spec.whatwg.org/multipage/document-sequences.html#tn-running-nested-apply-history-step
  86. bool m_running_nested_apply_history_step { false };
  87. // https://html.spec.whatwg.org/multipage/document-sequences.html#system-visibility-state
  88. VisibilityState m_system_visibility_state { VisibilityState::Visible };
  89. SessionHistoryTraversalQueue m_session_history_traversal_queue;
  90. JS::NonnullGCPtr<Page> m_page;
  91. String m_window_handle;
  92. };
  93. struct BrowsingContextAndDocument {
  94. JS::NonnullGCPtr<HTML::BrowsingContext> browsing_context;
  95. JS::NonnullGCPtr<DOM::Document> document;
  96. };
  97. WebIDL::ExceptionOr<BrowsingContextAndDocument> create_a_new_top_level_browsing_context_and_document(JS::NonnullGCPtr<Page> page);
  98. void finalize_a_same_document_navigation(JS::NonnullGCPtr<TraversableNavigable> traversable, JS::NonnullGCPtr<Navigable> target_navigable, JS::NonnullGCPtr<SessionHistoryEntry> target_entry, JS::GCPtr<SessionHistoryEntry> entry_to_replace, HistoryHandlingBehavior);
  99. }