BrowsingContextContainer.cpp 10 KB

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