NavigableContainer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibURL/Origin.h>
  9. #include <LibWeb/Bindings/MainThreadVM.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/Event.h>
  12. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  13. #include <LibWeb/HTML/BrowsingContext.h>
  14. #include <LibWeb/HTML/BrowsingContextGroup.h>
  15. #include <LibWeb/HTML/DocumentState.h>
  16. #include <LibWeb/HTML/HTMLIFrameElement.h>
  17. #include <LibWeb/HTML/Navigable.h>
  18. #include <LibWeb/HTML/NavigableContainer.h>
  19. #include <LibWeb/HTML/NavigationParams.h>
  20. #include <LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.h>
  21. #include <LibWeb/HTML/TraversableNavigable.h>
  22. #include <LibWeb/HTML/Window.h>
  23. #include <LibWeb/HighResolutionTime/TimeOrigin.h>
  24. #include <LibWeb/Page/Page.h>
  25. namespace Web::HTML {
  26. HashTable<NavigableContainer*>& NavigableContainer::all_instances()
  27. {
  28. static HashTable<NavigableContainer*> set;
  29. return set;
  30. }
  31. NavigableContainer::NavigableContainer(DOM::Document& document, DOM::QualifiedName qualified_name)
  32. : HTMLElement(document, move(qualified_name))
  33. {
  34. all_instances().set(this);
  35. }
  36. NavigableContainer::~NavigableContainer()
  37. {
  38. all_instances().remove(this);
  39. }
  40. void NavigableContainer::visit_edges(Cell::Visitor& visitor)
  41. {
  42. Base::visit_edges(visitor);
  43. visitor.visit(m_content_navigable);
  44. }
  45. JS::GCPtr<NavigableContainer> NavigableContainer::navigable_container_with_content_navigable(JS::NonnullGCPtr<Navigable> navigable)
  46. {
  47. for (auto* navigable_container : all_instances()) {
  48. if (navigable_container->content_navigable() == navigable)
  49. return navigable_container;
  50. }
  51. return nullptr;
  52. }
  53. // https://html.spec.whatwg.org/multipage/document-sequences.html#create-a-new-child-navigable
  54. WebIDL::ExceptionOr<void> NavigableContainer::create_new_child_navigable(JS::GCPtr<JS::HeapFunction<void()>> after_session_history_update)
  55. {
  56. // 1. Let parentNavigable be element's node navigable.
  57. auto parent_navigable = navigable();
  58. // 2. Let group be element's node document's browsing context's top-level browsing context's group.
  59. VERIFY(document().browsing_context());
  60. auto group = document().browsing_context()->top_level_browsing_context()->group();
  61. VERIFY(group);
  62. // 3. Let browsingContext and document be the result of creating a new browsing context and document given element's node document, element, and group.
  63. auto& page = document().page();
  64. auto [browsing_context, document] = TRY(BrowsingContext::create_a_new_browsing_context_and_document(page, this->document(), *this, *group));
  65. // 4. Let targetName be null.
  66. Optional<String> target_name;
  67. // 5. If element has a name content attribute, then set targetName to the value of that attribute.
  68. if (name().has_value())
  69. target_name = name().value().to_string();
  70. // 6. Let documentState be a new document state, with
  71. // - document: document
  72. // - initiator origin: document's origin
  73. // - origin: document's origin
  74. // - navigable target name: targetName
  75. // - about base URL: document's about base URL
  76. JS::NonnullGCPtr<DocumentState> document_state = *heap().allocate<HTML::DocumentState>();
  77. document_state->set_document(document);
  78. document_state->set_initiator_origin(document->origin());
  79. document_state->set_origin(document->origin());
  80. if (target_name.has_value())
  81. document_state->set_navigable_target_name(*target_name);
  82. document_state->set_about_base_url(document->about_base_url());
  83. // 7. Let navigable be a new navigable.
  84. JS::NonnullGCPtr<Navigable> navigable = *heap().allocate<Navigable>(page);
  85. // 8. Initialize the navigable navigable given documentState and parentNavigable.
  86. TRY_OR_THROW_OOM(vm(), navigable->initialize_navigable(document_state, parent_navigable));
  87. // 9. Set element's content navigable to navigable.
  88. m_content_navigable = navigable;
  89. // 10. Let historyEntry be navigable's active session history entry.
  90. auto history_entry = navigable->active_session_history_entry();
  91. // 11. Let traversable be parentNavigable's traversable navigable.
  92. auto traversable = parent_navigable->traversable_navigable();
  93. // 12. Append the following session history traversal steps to traversable:
  94. traversable->append_session_history_traversal_steps(JS::create_heap_function(heap(), [traversable, navigable, parent_navigable, history_entry, after_session_history_update] {
  95. // 1. Let parentDocState be parentNavigable's active session history entry's document state.
  96. auto parent_doc_state = parent_navigable->active_session_history_entry()->document_state();
  97. // 2. Let parentNavigableEntries be the result of getting session history entries for parentNavigable.
  98. auto parent_navigable_entries = parent_navigable->get_session_history_entries();
  99. // 3. Let targetStepSHE be the first session history entry in parentNavigableEntries whose document state equals parentDocState.
  100. auto target_step_she = *parent_navigable_entries.find_if([parent_doc_state](auto& entry) {
  101. return entry->document_state() == parent_doc_state;
  102. });
  103. // 4. Set historyEntry's step to targetStepSHE's step.
  104. history_entry->set_step(target_step_she->step());
  105. // 5. Let nestedHistory be a new nested history whose id is navigable's id and entries list is « historyEntry ».
  106. DocumentState::NestedHistory nested_history {
  107. .id = navigable->id(),
  108. .entries { *history_entry },
  109. };
  110. // 6. Append nestedHistory to parentDocState's nested histories.
  111. parent_doc_state->nested_histories().append(move(nested_history));
  112. // 7. Update for navigable creation/destruction given traversable
  113. traversable->update_for_navigable_creation_or_destruction();
  114. if (after_session_history_update) {
  115. after_session_history_update->function()();
  116. }
  117. }));
  118. return {};
  119. }
  120. // https://html.spec.whatwg.org/multipage/browsers.html#concept-bcc-content-document
  121. const DOM::Document* NavigableContainer::content_document() const
  122. {
  123. // 1. If container's content navigable is null, then return null.
  124. if (m_content_navigable == nullptr)
  125. return nullptr;
  126. // 2. Let document be container's content navigable's active document.
  127. auto document = m_content_navigable->active_document();
  128. // 4. If document's origin and container's node document's origin are not same origin-domain, then return null.
  129. if (!document->origin().is_same_origin_domain(m_document->origin()))
  130. return nullptr;
  131. // 5. Return document.
  132. return document;
  133. }
  134. DOM::Document const* NavigableContainer::content_document_without_origin_check() const
  135. {
  136. if (!m_content_navigable)
  137. return nullptr;
  138. return m_content_navigable->active_document();
  139. }
  140. // https://html.spec.whatwg.org/multipage/embedded-content-other.html#dom-media-getsvgdocument
  141. const DOM::Document* NavigableContainer::get_svg_document() const
  142. {
  143. // 1. Let document be this element's content document.
  144. auto const* document = content_document();
  145. // 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.
  146. if (document && document->content_type() == "image/svg+xml"sv)
  147. return document;
  148. // 3. Return null.
  149. return nullptr;
  150. }
  151. HTML::WindowProxy* NavigableContainer::content_window()
  152. {
  153. if (!m_content_navigable)
  154. return nullptr;
  155. return m_content_navigable->active_window_proxy();
  156. }
  157. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#shared-attribute-processing-steps-for-iframe-and-frame-elements
  158. Optional<URL::URL> NavigableContainer::shared_attribute_processing_steps_for_iframe_and_frame(bool initial_insertion)
  159. {
  160. // AD-HOC: If the element was added and immediately removed, the content navigable will be null. Don't process the
  161. // src attribute any further.
  162. if (!m_content_navigable)
  163. return {};
  164. // 1. Let url be the URL record about:blank.
  165. auto url = URL::URL("about:blank");
  166. // 2. If element has a src attribute specified, and its value is not the empty string,
  167. // then parse the value of that attribute relative to element's node document.
  168. // If this is successful, then set url to the resulting URL record.
  169. auto src_attribute_value = get_attribute_value(HTML::AttributeNames::src);
  170. if (!src_attribute_value.is_empty()) {
  171. auto parsed_src = document().parse_url(src_attribute_value);
  172. if (parsed_src.is_valid())
  173. url = parsed_src;
  174. }
  175. // 3. If the inclusive ancestor navigables of element's node navigable contains a navigable
  176. // whose active document's URL equals url with exclude fragments set to true, then return null.
  177. for (auto const& navigable : document().inclusive_ancestor_navigables()) {
  178. VERIFY(navigable->active_document());
  179. if (navigable->active_document()->url().equals(url, URL::ExcludeFragment::Yes))
  180. return {};
  181. }
  182. // 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.
  183. if (url_matches_about_blank(url) && initial_insertion) {
  184. auto& document = *m_content_navigable->active_document();
  185. perform_url_and_history_update_steps(document, url);
  186. }
  187. // 5. Return url.
  188. return url;
  189. }
  190. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#navigate-an-iframe-or-frame
  191. void NavigableContainer::navigate_an_iframe_or_frame(URL::URL url, ReferrerPolicy::ReferrerPolicy referrer_policy, Optional<String> srcdoc_string)
  192. {
  193. // 1. Let historyHandling be "auto".
  194. auto history_handling = Bindings::NavigationHistoryBehavior::Auto;
  195. // 2. If element's content navigable's active document is not completely loaded, then set historyHandling to "replace".
  196. if (m_content_navigable->active_document() && !m_content_navigable->active_document()->is_completely_loaded()) {
  197. history_handling = Bindings::NavigationHistoryBehavior::Replace;
  198. }
  199. // FIXME: 3. If element is an iframe, then set element's pending resource-timing start time to the current high resolution
  200. // time given element's node document's relevant global object.
  201. // 4. Navigate element's content navigable to url using element's node document, with historyHandling set to historyHandling,
  202. // referrerPolicy set to referrerPolicy, and documentResource set to scrdocString.
  203. Variant<Empty, String, POSTResource> document_resource = Empty {};
  204. if (srcdoc_string.has_value())
  205. document_resource = srcdoc_string.value();
  206. MUST(m_content_navigable->navigate({ .url = url,
  207. .source_document = document(),
  208. .document_resource = document_resource,
  209. .history_handling = history_handling,
  210. .referrer_policy = referrer_policy }));
  211. }
  212. // https://html.spec.whatwg.org/multipage/document-sequences.html#destroy-a-child-navigable
  213. void NavigableContainer::destroy_the_child_navigable()
  214. {
  215. // 1. Let navigable be container's content navigable.
  216. auto navigable = content_navigable();
  217. // 2. If navigable is null, then return.
  218. if (!navigable)
  219. return;
  220. // Not in the spec:
  221. // Setting container's content navigable makes document *not* be "fully active".
  222. // Therefore, it is moved to run in afterAllDestruction callback of "destroy a document and its descendants"
  223. // when all queued tasks are done.
  224. // "Has been destroyed" flag is used instead to check whether navigable is already destroyed.
  225. if (navigable->has_been_destroyed())
  226. return;
  227. navigable->set_has_been_destroyed();
  228. // FIXME: 4. Inform the navigation API about child navigable destruction given navigable.
  229. // 5. Destroy a document and its descendants given navigable's active document.
  230. navigable->active_document()->destroy_a_document_and_its_descendants(JS::create_heap_function(heap(), [this, navigable] {
  231. // 3. Set container's content navigable to null.
  232. m_content_navigable = nullptr;
  233. // Not in the spec:
  234. HTML::all_navigables().remove(navigable);
  235. // 6. Let parentDocState be container's node navigable's active session history entry's document state.
  236. auto parent_doc_state = this->navigable()->active_session_history_entry()->document_state();
  237. // 7. Remove the nested history from parentDocState's nested histories whose id equals navigable's id.
  238. parent_doc_state->nested_histories().remove_all_matching([&](auto& nested_history) {
  239. return navigable->id() == nested_history.id;
  240. });
  241. // 8. Let traversable be container's node navigable's traversable navigable.
  242. auto traversable = this->navigable()->traversable_navigable();
  243. // 9. Append the following session history traversal steps to traversable:
  244. traversable->append_session_history_traversal_steps(JS::create_heap_function(heap(), [traversable] {
  245. // 1. Update for navigable creation/destruction given traversable.
  246. traversable->update_for_navigable_creation_or_destruction();
  247. }));
  248. }));
  249. }
  250. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#potentially-delays-the-load-event
  251. bool NavigableContainer::currently_delays_the_load_event() const
  252. {
  253. if (!m_content_navigable_initialized)
  254. return true;
  255. if (!m_potentially_delays_the_load_event)
  256. return false;
  257. // If an element type potentially delays the load event, then for each element element of that type,
  258. // the user agent must delay the load event of element's node document if element's content navigable is non-null
  259. // and any of the following are true:
  260. if (!m_content_navigable)
  261. return false;
  262. // - element's content navigable's active document is not ready for post-load tasks;
  263. if (!m_content_navigable->active_document()->ready_for_post_load_tasks())
  264. return true;
  265. // - element's content navigable's is delaying load events is true; or
  266. if (m_content_navigable->is_delaying_load_events())
  267. return true;
  268. // - anything is delaying the load event of element's content navigable's active document.
  269. if (m_content_navigable->active_document()->anything_is_delaying_the_load_event())
  270. return true;
  271. return false;
  272. }
  273. }