NavigableContainer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. auto target_step_she = *(traversable->session_history_entries().find_if([parent_doc_state](auto& entry) {
  92. return entry->document_state == parent_doc_state;
  93. }));
  94. // 3. Set historyEntry's step to targetStepSHE's step.
  95. history_entry->step = target_step_she->step;
  96. // 4. Let nestedHistory be a new nested history whose id is navigable's id and entries list is « historyEntry ».
  97. DocumentState::NestedHistory nested_history {
  98. .id = navigable->id(),
  99. .entries { *history_entry },
  100. };
  101. // 5. Append nestedHistory to parentDocState's nested histories.
  102. parent_doc_state->nested_histories().append(move(nested_history));
  103. // 6. Update for navigable creation/destruction given traversable
  104. traversable->update_for_navigable_creation_or_destruction();
  105. });
  106. return {};
  107. }
  108. // https://html.spec.whatwg.org/multipage/browsers.html#concept-bcc-content-document
  109. const DOM::Document* NavigableContainer::content_document() const
  110. {
  111. // 1. If container's content navigable is null, then return null.
  112. if (m_content_navigable == nullptr)
  113. return nullptr;
  114. // 2. Let document be container's content navigable's active document.
  115. auto document = m_content_navigable->active_document();
  116. // 4. If document's origin and container's node document's origin are not same origin-domain, then return null.
  117. if (!document->origin().is_same_origin_domain(m_document->origin()))
  118. return nullptr;
  119. // 5. Return document.
  120. return document;
  121. }
  122. DOM::Document const* NavigableContainer::content_document_without_origin_check() const
  123. {
  124. if (!m_content_navigable)
  125. return nullptr;
  126. return m_content_navigable->active_document();
  127. }
  128. // https://html.spec.whatwg.org/multipage/embedded-content-other.html#dom-media-getsvgdocument
  129. const DOM::Document* NavigableContainer::get_svg_document() const
  130. {
  131. // 1. Let document be this element's content document.
  132. auto const* document = content_document();
  133. // 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.
  134. if (document && document->content_type() == "image/svg+xml"sv)
  135. return document;
  136. // 3. Return null.
  137. return nullptr;
  138. }
  139. HTML::WindowProxy* NavigableContainer::content_window()
  140. {
  141. if (!m_content_navigable)
  142. return nullptr;
  143. return m_content_navigable->active_window_proxy();
  144. }
  145. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#shared-attribute-processing-steps-for-iframe-and-frame-elements
  146. Optional<AK::URL> NavigableContainer::shared_attribute_processing_steps_for_iframe_and_frame(bool initial_insertion)
  147. {
  148. // 1. Let url be the URL record about:blank.
  149. auto url = AK::URL("about:blank");
  150. // 2. If element has a src attribute specified, and its value is not the empty string,
  151. // then parse the value of that attribute relative to element's node document.
  152. // If this is successful, then set url to the resulting URL record.
  153. auto src_attribute_value = deprecated_attribute(HTML::AttributeNames::src);
  154. if (!src_attribute_value.is_null() && !src_attribute_value.is_empty()) {
  155. auto parsed_src = document().parse_url(src_attribute_value);
  156. if (parsed_src.is_valid())
  157. url = parsed_src;
  158. }
  159. // 3. If the inclusive ancestor navigables of element's node navigable contains a navigable
  160. // whose active document's URL equals url with exclude fragments set to true, then return null.
  161. if (m_content_navigable) {
  162. for (auto const& navigable : document().inclusive_ancestor_navigables()) {
  163. VERIFY(navigable->active_document());
  164. if (navigable->active_document()->url().equals(url, AK::URL::ExcludeFragment::Yes))
  165. return {};
  166. }
  167. }
  168. // 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.
  169. if (url_matches_about_blank(url) && initial_insertion) {
  170. perform_url_and_history_update_steps(*m_content_navigable->active_document(), url);
  171. }
  172. // 5. Return url.
  173. return url;
  174. }
  175. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#navigate-an-iframe-or-frame
  176. void NavigableContainer::navigate_an_iframe_or_frame(AK::URL url, ReferrerPolicy::ReferrerPolicy referrer_policy, Optional<String> srcdoc_string)
  177. {
  178. // 1. Let historyHandling be "auto".
  179. auto history_handling = Bindings::NavigationHistoryBehavior::Auto;
  180. // 2. If element's content navigable's active document is not completely loaded, then set historyHandling to "replace".
  181. if (m_content_navigable->active_document() && !m_content_navigable->active_document()->is_completely_loaded()) {
  182. history_handling = Bindings::NavigationHistoryBehavior::Replace;
  183. }
  184. // FIXME: 3. If element is an iframe, then set element's pending resource-timing start time to the current high resolution
  185. // time given element's node document's relevant global object.
  186. // 4. Navigate element's content navigable to url using element's node document, with historyHandling set to historyHandling,
  187. // referrerPolicy set to referrerPolicy, and documentResource set to scrdocString.
  188. Variant<Empty, String, POSTResource> document_resource = Empty {};
  189. if (srcdoc_string.has_value())
  190. document_resource = srcdoc_string.value();
  191. MUST(m_content_navigable->navigate(url, document(), document_resource, nullptr, false, history_handling, {}, {}, referrer_policy));
  192. }
  193. // https://html.spec.whatwg.org/multipage/document-sequences.html#destroy-a-child-navigable
  194. void NavigableContainer::destroy_the_child_navigable()
  195. {
  196. // 1. Let navigable be container's content navigable.
  197. auto navigable = content_navigable();
  198. // 2. If navigable is null, then return.
  199. if (!navigable)
  200. return;
  201. // 3. Set container's content navigable to null.
  202. m_content_navigable = nullptr;
  203. // 4. Destroy navigable's active document.
  204. navigable->active_document()->destroy();
  205. // 5. Let parentDocState be container's node navigable's active session history entry's document state.
  206. auto parent_doc_state = this->navigable()->active_session_history_entry()->document_state;
  207. // 6. Remove the nested history from parentDocState's nested histories whose id equals navigable's id.
  208. parent_doc_state->nested_histories().remove_all_matching([&](auto& nested_history) {
  209. return navigable->id() == nested_history.id;
  210. });
  211. // 7. Let traversable be container's node navigable's traversable navigable.
  212. auto traversable = this->navigable()->traversable_navigable();
  213. // Not in the spec
  214. navigable->set_has_been_destroyed();
  215. HTML::all_navigables().remove(navigable);
  216. // 8. Append the following session history traversal steps to traversable:
  217. traversable->append_session_history_traversal_steps([traversable] {
  218. // 1. Apply pending history changes to traversable.
  219. traversable->update_for_navigable_creation_or_destruction();
  220. });
  221. }
  222. }