NavigableContainer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/MainThreadVM.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/Event.h>
  10. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  11. #include <LibWeb/HTML/BrowsingContext.h>
  12. #include <LibWeb/HTML/BrowsingContextGroup.h>
  13. #include <LibWeb/HTML/DocumentState.h>
  14. #include <LibWeb/HTML/HTMLIFrameElement.h>
  15. #include <LibWeb/HTML/Navigable.h>
  16. #include <LibWeb/HTML/NavigableContainer.h>
  17. #include <LibWeb/HTML/NavigationParams.h>
  18. #include <LibWeb/HTML/Origin.h>
  19. #include <LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.h>
  20. #include <LibWeb/HTML/TraversableNavigable.h>
  21. #include <LibWeb/HighResolutionTime/TimeOrigin.h>
  22. #include <LibWeb/Page/Page.h>
  23. namespace Web::HTML {
  24. HashTable<NavigableContainer*>& NavigableContainer::all_instances()
  25. {
  26. static HashTable<NavigableContainer*> set;
  27. return set;
  28. }
  29. NavigableContainer::NavigableContainer(DOM::Document& document, DOM::QualifiedName qualified_name)
  30. : HTMLElement(document, move(qualified_name))
  31. {
  32. all_instances().set(this);
  33. }
  34. NavigableContainer::~NavigableContainer()
  35. {
  36. all_instances().remove(this);
  37. }
  38. void NavigableContainer::visit_edges(Cell::Visitor& visitor)
  39. {
  40. Base::visit_edges(visitor);
  41. visitor.visit(m_content_navigable);
  42. }
  43. JS::GCPtr<NavigableContainer> NavigableContainer::navigable_container_with_content_navigable(JS::NonnullGCPtr<Navigable> navigable)
  44. {
  45. for (auto* navigable_container : all_instances()) {
  46. if (navigable_container->content_navigable() == navigable)
  47. return navigable_container;
  48. }
  49. return nullptr;
  50. }
  51. // https://html.spec.whatwg.org/multipage/document-sequences.html#create-a-new-child-navigable
  52. WebIDL::ExceptionOr<void> NavigableContainer::create_new_child_navigable()
  53. {
  54. // 1. Let parentNavigable be element's node navigable.
  55. auto parent_navigable = navigable();
  56. // 2. Let group be element's node document's browsing context's top-level browsing context's group.
  57. VERIFY(document().browsing_context());
  58. auto group = document().browsing_context()->top_level_browsing_context()->group();
  59. VERIFY(group);
  60. // 3. Let browsingContext and document be the result of creating a new browsing context and document given element's node document, element, and group.
  61. auto* page = document().page();
  62. VERIFY(page);
  63. auto [browsing_context, document] = TRY(BrowsingContext::create_a_new_browsing_context_and_document(*page, this->document(), *this, *group));
  64. // 4. Let targetName be null.
  65. Optional<String> target_name;
  66. // 5. If element has a name content attribute, then set targetName to the value of that attribute.
  67. if (auto value = deprecated_attribute(HTML::AttributeNames::name); !value.is_null())
  68. target_name = String::from_deprecated_string(value).release_value_but_fixme_should_propagate_errors();
  69. // 6. Let documentState be a new document state, with
  70. // document: document
  71. // navigable target name: targetName
  72. JS::NonnullGCPtr<DocumentState> document_state = *heap().allocate_without_realm<HTML::DocumentState>();
  73. document_state->set_document(document);
  74. if (target_name.has_value())
  75. document_state->set_navigable_target_name(*target_name);
  76. // 7. Let navigable be a new navigable.
  77. JS::NonnullGCPtr<Navigable> navigable = *heap().allocate_without_realm<Navigable>();
  78. // 8. Initialize the navigable navigable given documentState and parentNavigable.
  79. TRY_OR_THROW_OOM(vm(), navigable->initialize_navigable(document_state, parent_navigable));
  80. // 9. Set element's content navigable to navigable.
  81. m_content_navigable = navigable;
  82. // 10. Let historyEntry be navigable's active session history entry.
  83. auto history_entry = navigable->active_session_history_entry();
  84. // 11. Let traversable be parentNavigable's traversable navigable.
  85. auto traversable = parent_navigable->traversable_navigable();
  86. // 12. Append the following session history traversal steps to traversable:
  87. traversable->append_session_history_traversal_steps([traversable, navigable, parent_navigable, history_entry] {
  88. // 1. Let parentDocState be parentNavigable's active session history entry's document state.
  89. auto parent_doc_state = parent_navigable->active_session_history_entry()->document_state;
  90. // 2. Let targetStepSHE be the first session history entry in traversable's session history entries whose document state equals parentDocState.
  91. // NOTE: We need to look for parent document state in parent navigable instead of traversable as specification says. https://github.com/whatwg/html/issues/9686
  92. auto target_step_she = *(parent_navigable->get_session_history_entries().find_if([parent_doc_state](auto& entry) {
  93. return entry->document_state == parent_doc_state;
  94. }));
  95. // 3. Set historyEntry's step to targetStepSHE's step.
  96. history_entry->step = target_step_she->step;
  97. // 4. Let nestedHistory be a new nested history whose id is navigable's id and entries list is « historyEntry ».
  98. DocumentState::NestedHistory nested_history {
  99. .id = navigable->id(),
  100. .entries { *history_entry },
  101. };
  102. // 5. Append nestedHistory to parentDocState's nested histories.
  103. parent_doc_state->nested_histories().append(move(nested_history));
  104. // 6. Update for navigable creation/destruction given traversable
  105. traversable->update_for_navigable_creation_or_destruction();
  106. });
  107. return {};
  108. }
  109. // https://html.spec.whatwg.org/multipage/browsers.html#concept-bcc-content-document
  110. const DOM::Document* NavigableContainer::content_document() const
  111. {
  112. // 1. If container's content navigable is null, then return null.
  113. if (m_content_navigable == nullptr)
  114. return nullptr;
  115. // 2. Let document be container's content navigable's active document.
  116. auto document = m_content_navigable->active_document();
  117. // 4. If document's origin and container's node document's origin are not same origin-domain, then return null.
  118. if (!document->origin().is_same_origin_domain(m_document->origin()))
  119. return nullptr;
  120. // 5. Return document.
  121. return document;
  122. }
  123. DOM::Document const* NavigableContainer::content_document_without_origin_check() const
  124. {
  125. if (!m_content_navigable)
  126. return nullptr;
  127. return m_content_navigable->active_document();
  128. }
  129. // https://html.spec.whatwg.org/multipage/embedded-content-other.html#dom-media-getsvgdocument
  130. const DOM::Document* NavigableContainer::get_svg_document() const
  131. {
  132. // 1. Let document be this element's content document.
  133. auto const* document = content_document();
  134. // 2. If document is non-null and was created by the page load processing model for XML files section because the computed type of the resource in the navigate algorithm was image/svg+xml, then return document.
  135. if (document && document->content_type() == "image/svg+xml"sv)
  136. return document;
  137. // 3. Return null.
  138. return nullptr;
  139. }
  140. HTML::WindowProxy* NavigableContainer::content_window()
  141. {
  142. if (!m_content_navigable)
  143. return nullptr;
  144. return m_content_navigable->active_window_proxy();
  145. }
  146. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#shared-attribute-processing-steps-for-iframe-and-frame-elements
  147. Optional<AK::URL> NavigableContainer::shared_attribute_processing_steps_for_iframe_and_frame(bool initial_insertion)
  148. {
  149. // 1. Let url be the URL record about:blank.
  150. auto url = AK::URL("about:blank");
  151. // 2. If element has a src attribute specified, and its value is not the empty string,
  152. // then parse the value of that attribute relative to element's node document.
  153. // If this is successful, then set url to the resulting URL record.
  154. auto src_attribute_value = deprecated_attribute(HTML::AttributeNames::src);
  155. if (!src_attribute_value.is_null() && !src_attribute_value.is_empty()) {
  156. auto parsed_src = document().parse_url(src_attribute_value);
  157. if (parsed_src.is_valid())
  158. url = parsed_src;
  159. }
  160. // 3. If the inclusive ancestor navigables of element's node navigable contains a navigable
  161. // whose active document's URL equals url with exclude fragments set to true, then return null.
  162. if (m_content_navigable) {
  163. for (auto const& navigable : document().inclusive_ancestor_navigables()) {
  164. VERIFY(navigable->active_document());
  165. if (navigable->active_document()->url().equals(url, AK::URL::ExcludeFragment::Yes))
  166. return {};
  167. }
  168. }
  169. // 4. If url matches about:blank and initialInsertion is true, then perform the URL and history update steps given element's content navigable's active document and url.
  170. if (url_matches_about_blank(url) && initial_insertion) {
  171. perform_url_and_history_update_steps(*m_content_navigable->active_document(), url);
  172. // NOTE: Not in the spec but we need to make sure that "apply the history step" for initial navigation to about:blank
  173. // is applied before subsequent navigation.
  174. navigable()->traversable_navigable()->process_session_history_traversal_queue();
  175. }
  176. // 5. Return url.
  177. return url;
  178. }
  179. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#navigate-an-iframe-or-frame
  180. void NavigableContainer::navigate_an_iframe_or_frame(AK::URL url, ReferrerPolicy::ReferrerPolicy referrer_policy, Optional<String> srcdoc_string)
  181. {
  182. // 1. Let historyHandling be "auto".
  183. auto history_handling = Bindings::NavigationHistoryBehavior::Auto;
  184. // 2. If element's content navigable's active document is not completely loaded, then set historyHandling to "replace".
  185. if (m_content_navigable->active_document() && !m_content_navigable->active_document()->is_completely_loaded()) {
  186. history_handling = Bindings::NavigationHistoryBehavior::Replace;
  187. }
  188. // FIXME: 3. If element is an iframe, then set element's pending resource-timing start time to the current high resolution
  189. // time given element's node document's relevant global object.
  190. // 4. Navigate element's content navigable to url using element's node document, with historyHandling set to historyHandling,
  191. // referrerPolicy set to referrerPolicy, and documentResource set to scrdocString.
  192. Variant<Empty, String, POSTResource> document_resource = Empty {};
  193. if (srcdoc_string.has_value())
  194. document_resource = srcdoc_string.value();
  195. MUST(m_content_navigable->navigate(url, document(), document_resource, nullptr, false, history_handling, {}, {}, referrer_policy));
  196. }
  197. // https://html.spec.whatwg.org/multipage/document-sequences.html#destroy-a-child-navigable
  198. void NavigableContainer::destroy_the_child_navigable()
  199. {
  200. // 1. Let navigable be container's content navigable.
  201. auto navigable = content_navigable();
  202. // 2. If navigable is null, then return.
  203. if (!navigable)
  204. return;
  205. // 3. Set container's content navigable to null.
  206. m_content_navigable = nullptr;
  207. // 4. Destroy navigable's active document.
  208. navigable->active_document()->destroy();
  209. // 5. Let parentDocState be container's node navigable's active session history entry's document state.
  210. auto parent_doc_state = navigable->active_session_history_entry()->document_state;
  211. // 6. Remove the nested history from parentDocState's nested histories whose id equals navigable's id.
  212. parent_doc_state->nested_histories().remove_all_matching([&](auto& nested_history) {
  213. return navigable->id() == nested_history.id;
  214. });
  215. // 7. Let traversable be container's node navigable's traversable navigable.
  216. auto traversable = navigable->traversable_navigable();
  217. // Not in the spec
  218. navigable->set_has_been_destroyed();
  219. HTML::all_navigables().remove(navigable);
  220. // 8. Append the following session history traversal steps to traversable:
  221. traversable->append_session_history_traversal_steps([traversable] {
  222. // 1. Apply pending history changes to traversable.
  223. traversable->update_for_navigable_creation_or_destruction();
  224. });
  225. }
  226. }