TraversableNavigable.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/MainThreadVM.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/HTML/BrowsingContextGroup.h>
  9. #include <LibWeb/HTML/DocumentState.h>
  10. #include <LibWeb/HTML/SessionHistoryEntry.h>
  11. #include <LibWeb/HTML/TraversableNavigable.h>
  12. namespace Web::HTML {
  13. TraversableNavigable::TraversableNavigable() = default;
  14. TraversableNavigable::~TraversableNavigable() = default;
  15. void TraversableNavigable::visit_edges(Cell::Visitor& visitor)
  16. {
  17. Base::visit_edges(visitor);
  18. for (auto& entry : m_session_history_entries)
  19. visitor.visit(entry);
  20. }
  21. static OrderedHashTable<TraversableNavigable*>& user_agent_top_level_traversable_set()
  22. {
  23. static OrderedHashTable<TraversableNavigable*> set;
  24. return set;
  25. }
  26. struct BrowsingContextAndDocument {
  27. JS::NonnullGCPtr<HTML::BrowsingContext> browsing_context;
  28. JS::NonnullGCPtr<DOM::Document> document;
  29. };
  30. // https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-top-level-browsing-context
  31. static WebIDL::ExceptionOr<BrowsingContextAndDocument> create_a_new_top_level_browsing_context_and_document(Page& page)
  32. {
  33. // 1. Let group and document be the result of creating a new browsing context group and document.
  34. auto [group, document] = TRY(BrowsingContextGroup::create_a_new_browsing_context_group_and_document(page));
  35. // 2. Return group's browsing context set[0] and document.
  36. return BrowsingContextAndDocument { **group->browsing_context_set().begin(), document };
  37. }
  38. // https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-top-level-traversable
  39. WebIDL::ExceptionOr<JS::NonnullGCPtr<TraversableNavigable>> TraversableNavigable::create_a_new_top_level_traversable(Page& page, JS::GCPtr<HTML::BrowsingContext> opener, String target_name)
  40. {
  41. auto& vm = Bindings::main_thread_vm();
  42. // 1. Let document be null.
  43. JS::GCPtr<DOM::Document> document = nullptr;
  44. // 2. If opener is null, then set document to the second return value of creating a new top-level browsing context and document.
  45. if (!opener) {
  46. document = TRY(create_a_new_top_level_browsing_context_and_document(page)).document;
  47. }
  48. // 3. Otherwise, set document to the second return value of creating a new auxiliary browsing context and document given opener.
  49. else {
  50. document = TRY(BrowsingContext::create_a_new_auxiliary_browsing_context_and_document(page, *opener)).document;
  51. }
  52. // 4. Let documentState be a new document state, with
  53. auto document_state = vm.heap().allocate_without_realm<DocumentState>();
  54. // document: document
  55. document_state->set_document(document);
  56. // navigable target name: targetName
  57. document_state->set_navigable_target_name(target_name);
  58. // 5. Let traversable be a new traversable navigable.
  59. auto traversable = vm.heap().allocate_without_realm<TraversableNavigable>();
  60. // 6. Initialize the navigable traversable given documentState.
  61. TRY_OR_THROW_OOM(vm, traversable->initialize_navigable(document_state, nullptr));
  62. // 7. Let initialHistoryEntry be traversable's active session history entry.
  63. auto initial_history_entry = traversable->active_session_history_entry();
  64. VERIFY(initial_history_entry);
  65. // 8. Set initialHistoryEntry's step to 0.
  66. initial_history_entry->step = 0;
  67. // 9. Append initialHistoryEntry to traversable's session history entries.
  68. traversable->m_session_history_entries.append(*initial_history_entry);
  69. // FIXME: 10. If opener is non-null, then legacy-clone a traversable storage shed given opener's top-level traversable and traversable. [STORAGE]
  70. // 11. Append traversable to the user agent's top-level traversable set.
  71. user_agent_top_level_traversable_set().set(traversable);
  72. // 12. Return traversable.
  73. return traversable;
  74. }
  75. // https://html.spec.whatwg.org/multipage/document-sequences.html#create-a-fresh-top-level-traversable
  76. WebIDL::ExceptionOr<JS::NonnullGCPtr<TraversableNavigable>> TraversableNavigable::create_a_fresh_top_level_traversable(Page& page, AK::URL const& initial_navigation_url, Variant<Empty, String, POSTResource> initial_navigation_post_resource)
  77. {
  78. // 1. Let traversable be the result of creating a new top-level traversable given null and the empty string.
  79. auto traversable = TRY(create_a_new_top_level_traversable(page, nullptr, {}));
  80. // 2. Navigate traversable to initialNavigationURL using traversable's active document, with documentResource set to initialNavigationPostResource.
  81. TRY(traversable->navigate(initial_navigation_url, *traversable->active_document(), initial_navigation_post_resource));
  82. // 3. Return traversable.
  83. return traversable;
  84. }
  85. // https://html.spec.whatwg.org/multipage/document-sequences.html#top-level-traversable
  86. bool TraversableNavigable::is_top_level_traversable() const
  87. {
  88. // A top-level traversable is a traversable navigable with a null parent.
  89. return parent() == nullptr;
  90. }
  91. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-all-used-history-steps
  92. Vector<int> TraversableNavigable::get_all_used_history_steps() const
  93. {
  94. // FIXME: 1. Assert: this is running within traversable's session history traversal queue.
  95. // 2. Let steps be an empty ordered set of non-negative integers.
  96. OrderedHashTable<int> steps;
  97. // 3. Let entryLists be the ordered set « traversable's session history entries ».
  98. Vector<Vector<JS::NonnullGCPtr<SessionHistoryEntry>>> entry_lists { session_history_entries() };
  99. // 4. For each entryList of entryLists:
  100. while (!entry_lists.is_empty()) {
  101. auto entry_list = entry_lists.take_first();
  102. // 1. For each entry of entryList:
  103. for (auto& entry : entry_list) {
  104. // 1. Append entry's step to steps.
  105. steps.set(entry->step.get<int>());
  106. // 2. For each nestedHistory of entry's document state's nested histories, append nestedHistory's entries list to entryLists.
  107. for (auto& nested_history : entry->document_state->nested_histories())
  108. entry_lists.append(nested_history.entries);
  109. }
  110. }
  111. // 5. Return steps, sorted.
  112. auto sorted_steps = steps.values();
  113. quick_sort(sorted_steps);
  114. return sorted_steps;
  115. }
  116. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#clear-the-forward-session-history
  117. void TraversableNavigable::clear_the_forward_session_history()
  118. {
  119. // FIXME: 1. Assert: this is running within navigable's session history traversal queue.
  120. // 2. Let step be the navigable's current session history step.
  121. auto step = current_session_history_step();
  122. // 3. Let entryLists be the ordered set « navigable's session history entries ».
  123. Vector<Vector<JS::NonnullGCPtr<SessionHistoryEntry>>&> entry_lists;
  124. entry_lists.append(session_history_entries());
  125. // 4. For each entryList of entryLists:
  126. while (!entry_lists.is_empty()) {
  127. auto& entry_list = entry_lists.take_first();
  128. // 1. Remove every session history entry from entryList that has a step greater than step.
  129. entry_list.remove_all_matching([step](auto& entry) {
  130. return entry->step.template get<int>() > step;
  131. });
  132. // 2. For each entry of entryList:
  133. for (auto& entry : entry_list) {
  134. // 1. For each nestedHistory of entry's document state's nested histories, append nestedHistory's entries list to entryLists.
  135. for (auto& nested_history : entry->document_state->nested_histories()) {
  136. entry_lists.append(nested_history.entries);
  137. }
  138. }
  139. }
  140. }
  141. }