BrowsingContextContainer.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2020-2021, 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/HTML/BrowsingContext.h>
  10. #include <LibWeb/HTML/BrowsingContextContainer.h>
  11. #include <LibWeb/HTML/Origin.h>
  12. #include <LibWeb/Page/Page.h>
  13. namespace Web::HTML {
  14. BrowsingContextContainer::BrowsingContextContainer(DOM::Document& document, DOM::QualifiedName qualified_name)
  15. : HTMLElement(document, move(qualified_name))
  16. {
  17. }
  18. BrowsingContextContainer::~BrowsingContextContainer() = default;
  19. // https://html.spec.whatwg.org/multipage/browsers.html#creating-a-new-nested-browsing-context
  20. void BrowsingContextContainer::create_new_nested_browsing_context()
  21. {
  22. // 1. Let group be element's node document's browsing context's top-level browsing context's group.
  23. // FIXME: We do not have a concept of "browsing context groups" yet.
  24. auto* group = document().browsing_context();
  25. if (!group)
  26. return;
  27. VERIFY(group->page());
  28. // 2. Let browsingContext be the result of creating a new browsing context with element's node document, element, and group.
  29. // 3. Set element's nested browsing context to browsingContext.
  30. m_nested_browsing_context = BrowsingContext::create_a_new_browsing_context(*group->page(), document(), *this);
  31. group->append_child(*m_nested_browsing_context);
  32. m_nested_browsing_context->set_frame_nesting_levels(group->frame_nesting_levels());
  33. m_nested_browsing_context->register_frame_nesting(document().url());
  34. // 4. If element has a name attribute, then set browsingContext's name to the value of this attribute.
  35. if (auto name = attribute(HTML::AttributeNames::name); !name.is_empty())
  36. m_nested_browsing_context->set_name(name);
  37. }
  38. // https://html.spec.whatwg.org/multipage/window-object.html#a-browsing-context-is-discarded
  39. void BrowsingContextContainer::discard_nested_browsing_context()
  40. {
  41. // 1. Discard all Document objects for all the entries in browsingContext's session history.
  42. if (m_nested_browsing_context && m_nested_browsing_context->parent())
  43. m_nested_browsing_context->parent()->remove_child(*m_nested_browsing_context);
  44. // 2. If browsingContext is a top-level browsing context, then remove browsingContext.
  45. // NOTE: We skip this here because this is by definition a nested browsing context, not top-level.
  46. }
  47. // https://html.spec.whatwg.org/multipage/browsers.html#concept-bcc-content-document
  48. const DOM::Document* BrowsingContextContainer::content_document() const
  49. {
  50. // 1. If container's nested browsing context is null, then return null.
  51. if (m_nested_browsing_context == nullptr)
  52. return nullptr;
  53. // 2. Let context be container's nested browsing context.
  54. auto const& context = *m_nested_browsing_context;
  55. // 3. Let document be context's active document.
  56. auto const* document = context.active_document();
  57. // FIXME: This should not be here, as we're expected to have a document at this point.
  58. if (!document)
  59. return nullptr;
  60. VERIFY(document);
  61. VERIFY(m_document);
  62. // 4. If document's origin and container's node document's origin are not same origin-domain, then return null.
  63. if (!document->origin().is_same_origin_domain(m_document->origin()))
  64. return nullptr;
  65. // 5. Return document.
  66. return document;
  67. }
  68. DOM::Document const* BrowsingContextContainer::content_document_without_origin_check() const
  69. {
  70. if (!m_nested_browsing_context)
  71. return nullptr;
  72. return m_nested_browsing_context->active_document();
  73. }
  74. // https://html.spec.whatwg.org/multipage/embedded-content-other.html#dom-media-getsvgdocument
  75. const DOM::Document* BrowsingContextContainer::get_svg_document() const
  76. {
  77. // 1. Let document be this element's content document.
  78. auto const* document = content_document();
  79. // 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.
  80. if (document && document->content_type() == "image/svg+xml"sv)
  81. return document;
  82. // 3. Return null.
  83. return nullptr;
  84. }
  85. HTML::Window* BrowsingContextContainer::content_window() const
  86. {
  87. // FIXME: This should return the WindowProxy
  88. auto* document = content_document();
  89. if (!document)
  90. return nullptr;
  91. return const_cast<HTML::Window*>(&document->window());
  92. }
  93. }