NavigableContainer.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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_nested_browsing_context);
  42. visitor.visit(m_content_navigable);
  43. }
  44. JS::GCPtr<NavigableContainer> NavigableContainer::navigable_container_with_content_navigable(JS::NonnullGCPtr<Navigable> navigable)
  45. {
  46. for (auto* navigable_container : all_instances()) {
  47. if (navigable_container->content_navigable() == navigable)
  48. return navigable_container;
  49. }
  50. return nullptr;
  51. }
  52. // https://html.spec.whatwg.org/multipage/document-sequences.html#create-a-new-child-navigable
  53. WebIDL::ExceptionOr<void> NavigableContainer::create_new_child_navigable()
  54. {
  55. // 1. Let parentNavigable be element's node navigable.
  56. auto parent_navigable = navigable();
  57. // 2. Let group be element's node document's browsing context's top-level browsing context's group.
  58. VERIFY(document().browsing_context());
  59. auto group = document().browsing_context()->top_level_browsing_context().group();
  60. VERIFY(group);
  61. // 3. Let browsingContext and document be the result of creating a new browsing context and document given element's node document, element, and group.
  62. auto* page = document().page();
  63. VERIFY(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 (auto value = attribute(HTML::AttributeNames::name); !value.is_null())
  69. target_name = String::from_deprecated_string(value).release_value_but_fixme_should_propagate_errors();
  70. // 6. Let documentState be a new document state, with
  71. // document: document
  72. // navigable target name: targetName
  73. JS::NonnullGCPtr<DocumentState> document_state = *heap().allocate_without_realm<HTML::DocumentState>();
  74. document_state->set_document(document);
  75. if (target_name.has_value())
  76. document_state->set_navigable_target_name(*target_name);
  77. // 7. Let navigable be a new navigable.
  78. JS::NonnullGCPtr<Navigable> navigable = *heap().allocate_without_realm<Navigable>();
  79. // 8. Initialize the navigable navigable given documentState and parentNavigable.
  80. TRY_OR_THROW_OOM(vm(), navigable->initialize_navigable(document_state, parent_navigable));
  81. // 9. Set element's content navigable to navigable.
  82. m_content_navigable = navigable;
  83. // 10. Let historyEntry be navigable's active session history entry.
  84. auto history_entry = navigable->active_session_history_entry();
  85. // 11. Let traversable be parentNavigable's traversable navigable.
  86. auto traversable = parent_navigable->traversable_navigable();
  87. // 12. Append the following session history traversal steps to traversable:
  88. traversable->append_session_history_traversal_steps([traversable, navigable, parent_navigable, history_entry] {
  89. // 1. Let parentDocState be parentNavigable's active session history entry's document state.
  90. auto parent_doc_state = parent_navigable->active_session_history_entry()->document_state;
  91. // 2. Let targetStepSHE be the first session history entry in traversable's session history entries whose document state equals parentDocState.
  92. auto target_step_she = *(traversable->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. // FIXME: 6. Update for navigable creation/destruction given traversable
  105. });
  106. return {};
  107. }
  108. // https://html.spec.whatwg.org/multipage/browsers.html#creating-a-new-nested-browsing-context
  109. void NavigableContainer::create_new_nested_browsing_context()
  110. {
  111. // 1. Let group be element's node document's browsing context's top-level browsing context's group.
  112. VERIFY(document().browsing_context());
  113. auto* group = document().browsing_context()->top_level_browsing_context().group();
  114. // NOTE: The spec assumes that `group` is non-null here.
  115. VERIFY(group);
  116. VERIFY(group->page());
  117. // 2. Let browsingContext be the result of creating a new browsing context with element's node document, element, and group.
  118. // 3. Set element's nested browsing context to browsingContext.
  119. m_nested_browsing_context = BrowsingContext::create_a_new_browsing_context(*group->page(), document(), *this, *group);
  120. document().browsing_context()->append_child(*m_nested_browsing_context);
  121. m_nested_browsing_context->set_frame_nesting_levels(document().browsing_context()->frame_nesting_levels());
  122. m_nested_browsing_context->register_frame_nesting(document().url());
  123. // 4. If element has a name attribute, then set browsingContext's name to the value of this attribute.
  124. if (auto name = attribute(HTML::AttributeNames::name); !name.is_empty())
  125. m_nested_browsing_context->set_name(String::from_deprecated_string(name).release_value_but_fixme_should_propagate_errors());
  126. }
  127. // https://html.spec.whatwg.org/multipage/browsers.html#concept-bcc-content-document
  128. const DOM::Document* NavigableContainer::content_document() const
  129. {
  130. // 1. If container's nested browsing context is null, then return null.
  131. if (m_nested_browsing_context == nullptr)
  132. return nullptr;
  133. // 2. Let context be container's nested browsing context.
  134. auto const& context = *m_nested_browsing_context;
  135. // 3. Let document be context's active document.
  136. auto const* document = context.active_document();
  137. // FIXME: This should not be here, as we're expected to have a document at this point.
  138. if (!document)
  139. return nullptr;
  140. VERIFY(document);
  141. VERIFY(m_document);
  142. // 4. If document's origin and container's node document's origin are not same origin-domain, then return null.
  143. if (!document->origin().is_same_origin_domain(m_document->origin()))
  144. return nullptr;
  145. // 5. Return document.
  146. return document;
  147. }
  148. DOM::Document const* NavigableContainer::content_document_without_origin_check() const
  149. {
  150. if (!m_nested_browsing_context)
  151. return nullptr;
  152. return m_nested_browsing_context->active_document();
  153. }
  154. // https://html.spec.whatwg.org/multipage/embedded-content-other.html#dom-media-getsvgdocument
  155. const DOM::Document* NavigableContainer::get_svg_document() const
  156. {
  157. // 1. Let document be this element's content document.
  158. auto const* document = content_document();
  159. // 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.
  160. if (document && document->content_type() == "image/svg+xml"sv)
  161. return document;
  162. // 3. Return null.
  163. return nullptr;
  164. }
  165. HTML::WindowProxy* NavigableContainer::content_window()
  166. {
  167. if (!m_nested_browsing_context)
  168. return nullptr;
  169. return m_nested_browsing_context->window_proxy();
  170. }
  171. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#matches-about:blank
  172. static bool url_matches_about_blank(AK::URL const& url)
  173. {
  174. // A URL matches about:blank if its scheme is "about", its path contains a single string "blank", its username and password are the empty string, and its host is null.
  175. return url.scheme() == "about"sv
  176. && url.serialize_path() == "blank"sv
  177. && url.raw_username().is_empty()
  178. && url.raw_password().is_empty()
  179. && url.host().has<Empty>();
  180. }
  181. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#shared-attribute-processing-steps-for-iframe-and-frame-elements
  182. void NavigableContainer::shared_attribute_processing_steps_for_iframe_and_frame(bool initial_insertion)
  183. {
  184. // 1. Let url be the URL record about:blank.
  185. auto url = AK::URL("about:blank");
  186. // 2. If element has a src attribute specified, and its value is not the empty string,
  187. // then parse the value of that attribute relative to element's node document.
  188. // If this is successful, then set url to the resulting URL record.
  189. auto src_attribute_value = attribute(HTML::AttributeNames::src);
  190. if (!src_attribute_value.is_null() && !src_attribute_value.is_empty()) {
  191. auto parsed_src = document().parse_url(src_attribute_value);
  192. if (parsed_src.is_valid())
  193. url = parsed_src;
  194. }
  195. // 3. If there exists an ancestor browsing context of element's nested browsing context
  196. // whose active document's URL, ignoring fragments, is equal to url, then return.
  197. if (m_nested_browsing_context) {
  198. for (auto ancestor = m_nested_browsing_context->parent(); ancestor; ancestor = ancestor->parent()) {
  199. VERIFY(ancestor->active_document());
  200. if (ancestor->active_document()->url().equals(url, AK::URL::ExcludeFragment::Yes))
  201. return;
  202. }
  203. }
  204. // 4. If url matches about:blank and initialInsertion is true, then:
  205. if (url_matches_about_blank(url) && initial_insertion) {
  206. // FIXME: 1. Perform the URL and history update steps given element's nested browsing context's active document and url.
  207. // 2. Run the iframe load event steps given element.
  208. // FIXME: The spec doesn't check frame vs iframe here. Bug: https://github.com/whatwg/html/issues/8295
  209. if (is<HTMLIFrameElement>(*this)) {
  210. run_iframe_load_event_steps(static_cast<HTMLIFrameElement&>(*this));
  211. }
  212. // 3. Return.
  213. return;
  214. }
  215. // 5. Let resource be a new request whose URL is url and whose referrer policy is the current state of element's referrerpolicy content attribute.
  216. auto resource = Fetch::Infrastructure::Request::create(vm());
  217. resource->set_url(url);
  218. // FIXME: Set the referrer policy.
  219. // AD-HOC:
  220. if (url.scheme() == "file" && document().origin().scheme() != "file") {
  221. dbgln("iframe failed to load URL: Security violation: {} may not load {}", document().url(), url);
  222. return;
  223. }
  224. // 6. If element is an iframe element, then set element's current navigation was lazy loaded boolean to false.
  225. if (is<HTMLIFrameElement>(*this)) {
  226. static_cast<HTMLIFrameElement&>(*this).set_current_navigation_was_lazy_loaded(false);
  227. }
  228. // 7. If element is an iframe element, and the will lazy load element steps given element return true, then:
  229. if (is<HTMLIFrameElement>(*this) && static_cast<HTMLIFrameElement&>(*this).will_lazy_load_element()) {
  230. // FIXME: 1. Set element's lazy load resumption steps to the rest of this algorithm starting with the step labeled navigate to the resource.
  231. // FIXME: 2. Set element's current navigation was lazy loaded boolean to true.
  232. // FIXME: 3. Start intersection-observing a lazy loading element for element.
  233. // FIXME: 4. Return.
  234. }
  235. // 8. Navigate to the resource: navigate an iframe or frame given element and resource.
  236. navigate_an_iframe_or_frame(resource);
  237. }
  238. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#navigate-an-iframe-or-frame
  239. void NavigableContainer::navigate_an_iframe_or_frame(JS::NonnullGCPtr<Fetch::Infrastructure::Request> resource)
  240. {
  241. // 1. Let historyHandling be "default".
  242. auto history_handling = HistoryHandlingBehavior::Default;
  243. // 2. If element's nested browsing context's active document is not completely loaded, then set historyHandling to "replace".
  244. VERIFY(m_nested_browsing_context);
  245. VERIFY(m_nested_browsing_context->active_document());
  246. if (!m_nested_browsing_context->active_document()->is_completely_loaded()) {
  247. history_handling = HistoryHandlingBehavior::Replace;
  248. }
  249. // FIXME: 3. Let reportFrameTiming be the following step given response response:
  250. // queue an element task on the networking task source
  251. // given element's node document's relevant global object
  252. // to finalize and report timing given response, element's node document's relevant global object, and element's local name.
  253. // 4. Navigate element's nested browsing context to resource,
  254. // with historyHandling set to historyHandling,
  255. // the source browsing context set to element's node document's browsing context,
  256. // FIXME: and processResponseEndOfBody set to reportFrameTiming.
  257. auto* source_browsing_context = document().browsing_context();
  258. VERIFY(source_browsing_context);
  259. MUST(m_nested_browsing_context->navigate(resource, *source_browsing_context, false, history_handling));
  260. }
  261. // https://html.spec.whatwg.org/multipage/document-sequences.html#destroy-a-child-navigable
  262. void NavigableContainer::destroy_the_child_navigable()
  263. {
  264. // 1. Let navigable be container's content navigable.
  265. auto navigable = content_navigable();
  266. // 2. If navigable is null, then return.
  267. if (!navigable)
  268. return;
  269. // 3. Set container's content navigable to null.
  270. m_content_navigable = nullptr;
  271. // 4. Destroy navigable's active document.
  272. navigable->active_document()->destroy();
  273. // 5. Let parentDocState be container's node navigable's active session history entry's document state.
  274. auto parent_doc_state = this->navigable()->active_session_history_entry()->document_state;
  275. // 6. Remove the nested history from parentDocState's nested histories whose id equals navigable's id.
  276. parent_doc_state->nested_histories().remove_all_matching([&](auto& nested_history) {
  277. return navigable->id() == nested_history.id;
  278. });
  279. // 7. Let traversable be container's node navigable's traversable navigable.
  280. auto traversable = this->navigable()->traversable_navigable();
  281. // 8. Append the following session history traversal steps to traversable:
  282. traversable->append_session_history_traversal_steps([traversable] {
  283. // 1. Apply pending history changes to traversable.
  284. traversable->apply_pending_history_changes();
  285. });
  286. }
  287. }