TraversableNavigable.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. }