BrowsingContextContainer.cpp 2.6 KB

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