BrowsingContextContainer.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/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()
  19. {
  20. }
  21. void BrowsingContextContainer::inserted()
  22. {
  23. HTMLElement::inserted();
  24. if (!is_connected())
  25. return;
  26. if (auto* browsing_context = document().browsing_context()) {
  27. VERIFY(browsing_context->page());
  28. m_nested_browsing_context = BrowsingContext::create_nested(*browsing_context->page(), *this);
  29. browsing_context->append_child(*m_nested_browsing_context);
  30. m_nested_browsing_context->set_frame_nesting_levels(browsing_context->frame_nesting_levels());
  31. m_nested_browsing_context->register_frame_nesting(document().url());
  32. }
  33. }
  34. void BrowsingContextContainer::removed_from(Node* old_parent)
  35. {
  36. HTMLElement::removed_from(old_parent);
  37. if (m_nested_browsing_context && m_nested_browsing_context->parent())
  38. m_nested_browsing_context->parent()->remove_child(*m_nested_browsing_context);
  39. }
  40. // https://html.spec.whatwg.org/multipage/browsers.html#concept-bcc-content-document
  41. const DOM::Document* BrowsingContextContainer::content_document() const
  42. {
  43. // 1. If container's nested browsing context is null, then return null.
  44. if (m_nested_browsing_context == nullptr)
  45. return nullptr;
  46. // 2. Let context be container's nested browsing context.
  47. auto const& context = *m_nested_browsing_context;
  48. // 3. Let document be context's active document.
  49. auto const* document = context.active_document();
  50. VERIFY(document);
  51. VERIFY(m_document);
  52. // 4. If document's origin and container's node document's origin are not same origin-domain, then return null.
  53. if (!document->origin().is_same_origin_domain(m_document->origin()))
  54. return nullptr;
  55. // 5. Return document.
  56. return document;
  57. }
  58. DOM::Document const* BrowsingContextContainer::content_document_without_origin_check() const
  59. {
  60. if (!m_nested_browsing_context)
  61. return nullptr;
  62. return m_nested_browsing_context->active_document();
  63. }
  64. }