NavigableContainer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/DOM/Document.h>
  8. #include <LibWeb/DOM/Event.h>
  9. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  10. #include <LibWeb/HTML/BrowsingContext.h>
  11. #include <LibWeb/HTML/BrowsingContextGroup.h>
  12. #include <LibWeb/HTML/HTMLIFrameElement.h>
  13. #include <LibWeb/HTML/NavigableContainer.h>
  14. #include <LibWeb/HTML/NavigationParams.h>
  15. #include <LibWeb/HTML/Origin.h>
  16. #include <LibWeb/Page/Page.h>
  17. namespace Web::HTML {
  18. HashTable<NavigableContainer*>& NavigableContainer::all_instances()
  19. {
  20. static HashTable<NavigableContainer*> set;
  21. return set;
  22. }
  23. NavigableContainer::NavigableContainer(DOM::Document& document, DOM::QualifiedName qualified_name)
  24. : HTMLElement(document, move(qualified_name))
  25. {
  26. all_instances().set(this);
  27. }
  28. NavigableContainer::~NavigableContainer()
  29. {
  30. all_instances().remove(this);
  31. }
  32. void NavigableContainer::visit_edges(Cell::Visitor& visitor)
  33. {
  34. Base::visit_edges(visitor);
  35. visitor.visit(m_nested_browsing_context);
  36. }
  37. JS::GCPtr<NavigableContainer> NavigableContainer::navigable_container_with_content_navigable(JS::NonnullGCPtr<Navigable> navigable)
  38. {
  39. for (auto* navigable_container : all_instances()) {
  40. if (navigable_container->content_navigable() == navigable)
  41. return navigable_container;
  42. }
  43. return nullptr;
  44. }
  45. // https://html.spec.whatwg.org/multipage/browsers.html#creating-a-new-nested-browsing-context
  46. void NavigableContainer::create_new_nested_browsing_context()
  47. {
  48. // 1. Let group be element's node document's browsing context's top-level browsing context's group.
  49. VERIFY(document().browsing_context());
  50. auto* group = document().browsing_context()->top_level_browsing_context().group();
  51. // NOTE: The spec assumes that `group` is non-null here.
  52. VERIFY(group);
  53. VERIFY(group->page());
  54. // 2. Let browsingContext be the result of creating a new browsing context with element's node document, element, and group.
  55. // 3. Set element's nested browsing context to browsingContext.
  56. m_nested_browsing_context = BrowsingContext::create_a_new_browsing_context(*group->page(), document(), *this, *group);
  57. document().browsing_context()->append_child(*m_nested_browsing_context);
  58. m_nested_browsing_context->set_frame_nesting_levels(document().browsing_context()->frame_nesting_levels());
  59. m_nested_browsing_context->register_frame_nesting(document().url());
  60. // 4. If element has a name attribute, then set browsingContext's name to the value of this attribute.
  61. if (auto name = attribute(HTML::AttributeNames::name); !name.is_empty())
  62. m_nested_browsing_context->set_name(String::from_deprecated_string(name).release_value_but_fixme_should_propagate_errors());
  63. }
  64. // https://html.spec.whatwg.org/multipage/browsers.html#concept-bcc-content-document
  65. const DOM::Document* NavigableContainer::content_document() const
  66. {
  67. // 1. If container's nested browsing context is null, then return null.
  68. if (m_nested_browsing_context == nullptr)
  69. return nullptr;
  70. // 2. Let context be container's nested browsing context.
  71. auto const& context = *m_nested_browsing_context;
  72. // 3. Let document be context's active document.
  73. auto const* document = context.active_document();
  74. // FIXME: This should not be here, as we're expected to have a document at this point.
  75. if (!document)
  76. return nullptr;
  77. VERIFY(document);
  78. VERIFY(m_document);
  79. // 4. If document's origin and container's node document's origin are not same origin-domain, then return null.
  80. if (!document->origin().is_same_origin_domain(m_document->origin()))
  81. return nullptr;
  82. // 5. Return document.
  83. return document;
  84. }
  85. DOM::Document const* NavigableContainer::content_document_without_origin_check() const
  86. {
  87. if (!m_nested_browsing_context)
  88. return nullptr;
  89. return m_nested_browsing_context->active_document();
  90. }
  91. // https://html.spec.whatwg.org/multipage/embedded-content-other.html#dom-media-getsvgdocument
  92. const DOM::Document* NavigableContainer::get_svg_document() const
  93. {
  94. // 1. Let document be this element's content document.
  95. auto const* document = content_document();
  96. // 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.
  97. if (document && document->content_type() == "image/svg+xml"sv)
  98. return document;
  99. // 3. Return null.
  100. return nullptr;
  101. }
  102. HTML::WindowProxy* NavigableContainer::content_window()
  103. {
  104. if (!m_nested_browsing_context)
  105. return nullptr;
  106. return m_nested_browsing_context->window_proxy();
  107. }
  108. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#matches-about:blank
  109. static bool url_matches_about_blank(AK::URL const& url)
  110. {
  111. // 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.
  112. return url.scheme() == "about"sv
  113. && url.serialize_path() == "blank"sv
  114. && url.username().is_empty()
  115. && url.password().is_empty()
  116. && url.host().is_null();
  117. }
  118. // https://html.spec.whatwg.org/multipage/iframe-embed-object.html#shared-attribute-processing-steps-for-iframe-and-frame-elements
  119. void NavigableContainer::shared_attribute_processing_steps_for_iframe_and_frame(bool initial_insertion)
  120. {
  121. // 1. Let url be the URL record about:blank.
  122. auto url = AK::URL("about:blank");
  123. // 2. If element has a src attribute specified, and its value is not the empty string,
  124. // then parse the value of that attribute relative to element's node document.
  125. // If this is successful, then set url to the resulting URL record.
  126. auto src_attribute_value = attribute(HTML::AttributeNames::src);
  127. if (!src_attribute_value.is_null() && !src_attribute_value.is_empty()) {
  128. auto parsed_src = document().parse_url(src_attribute_value);
  129. if (parsed_src.is_valid())
  130. url = parsed_src;
  131. }
  132. // 3. If there exists an ancestor browsing context of element's nested browsing context
  133. // whose active document's URL, ignoring fragments, is equal to url, then return.
  134. if (m_nested_browsing_context) {
  135. for (auto ancestor = m_nested_browsing_context->parent(); ancestor; ancestor = ancestor->parent()) {
  136. VERIFY(ancestor->active_document());
  137. if (ancestor->active_document()->url().equals(url, AK::URL::ExcludeFragment::Yes))
  138. return;
  139. }
  140. }
  141. // 4. If url matches about:blank and initialInsertion is true, then:
  142. if (url_matches_about_blank(url) && initial_insertion) {
  143. // FIXME: 1. Perform the URL and history update steps given element's nested browsing context's active document and url.
  144. // 2. Run the iframe load event steps given element.
  145. // FIXME: The spec doesn't check frame vs iframe here. Bug: https://github.com/whatwg/html/issues/8295
  146. if (is<HTMLIFrameElement>(*this)) {
  147. run_iframe_load_event_steps(static_cast<HTMLIFrameElement&>(*this));
  148. }
  149. // 3. Return.
  150. return;
  151. }
  152. // 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.
  153. auto resource = Fetch::Infrastructure::Request::create(vm());
  154. resource->set_url(url);
  155. // FIXME: Set the referrer policy.
  156. // AD-HOC:
  157. if (url.scheme() == "file" && document().origin().scheme() != "file") {
  158. dbgln("iframe failed to load URL: Security violation: {} may not load {}", document().url(), url);
  159. return;
  160. }
  161. // 6. If element is an iframe element, then set element's current navigation was lazy loaded boolean to false.
  162. if (is<HTMLIFrameElement>(*this)) {
  163. static_cast<HTMLIFrameElement&>(*this).set_current_navigation_was_lazy_loaded(false);
  164. }
  165. // 7. If element is an iframe element, and the will lazy load element steps given element return true, then:
  166. if (is<HTMLIFrameElement>(*this) && static_cast<HTMLIFrameElement&>(*this).will_lazy_load_element()) {
  167. // FIXME: 1. Set element's lazy load resumption steps to the rest of this algorithm starting with the step labeled navigate to the resource.
  168. // FIXME: 2. Set element's current navigation was lazy loaded boolean to true.
  169. // FIXME: 3. Start intersection-observing a lazy loading element for element.
  170. // FIXME: 4. Return.
  171. }
  172. // 8. Navigate to the resource: navigate an iframe or frame given element and resource.
  173. navigate_an_iframe_or_frame(resource);
  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(JS::NonnullGCPtr<Fetch::Infrastructure::Request> resource)
  177. {
  178. // 1. Let historyHandling be "default".
  179. auto history_handling = HistoryHandlingBehavior::Default;
  180. // 2. If element's nested browsing context's active document is not completely loaded, then set historyHandling to "replace".
  181. VERIFY(m_nested_browsing_context);
  182. VERIFY(m_nested_browsing_context->active_document());
  183. if (!m_nested_browsing_context->active_document()->is_completely_loaded()) {
  184. history_handling = HistoryHandlingBehavior::Replace;
  185. }
  186. // FIXME: 3. Let reportFrameTiming be the following step given response response:
  187. // queue an element task on the networking task source
  188. // given element's node document's relevant global object
  189. // to finalize and report timing given response, element's node document's relevant global object, and element's local name.
  190. // 4. Navigate element's nested browsing context to resource,
  191. // with historyHandling set to historyHandling,
  192. // the source browsing context set to element's node document's browsing context,
  193. // FIXME: and processResponseEndOfBody set to reportFrameTiming.
  194. auto* source_browsing_context = document().browsing_context();
  195. VERIFY(source_browsing_context);
  196. MUST(m_nested_browsing_context->navigate(resource, *source_browsing_context, false, history_handling));
  197. }
  198. }