BrowsingContextGroup.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/MainThreadVM.h>
  7. #include <LibWeb/HTML/BrowsingContext.h>
  8. #include <LibWeb/HTML/BrowsingContextGroup.h>
  9. #include <LibWeb/Page/Page.h>
  10. namespace Web::HTML {
  11. JS_DEFINE_ALLOCATOR(BrowsingContextGroup);
  12. // https://html.spec.whatwg.org/multipage/browsers.html#browsing-context-group-set
  13. static HashTable<JS::NonnullGCPtr<BrowsingContextGroup>>& user_agent_browsing_context_group_set()
  14. {
  15. static HashTable<JS::NonnullGCPtr<BrowsingContextGroup>> set;
  16. return set;
  17. }
  18. BrowsingContextGroup::BrowsingContextGroup(JS::NonnullGCPtr<Web::Page> page)
  19. : m_page(page)
  20. {
  21. user_agent_browsing_context_group_set().set(*this);
  22. }
  23. BrowsingContextGroup::~BrowsingContextGroup()
  24. {
  25. user_agent_browsing_context_group_set().remove(*this);
  26. }
  27. void BrowsingContextGroup::visit_edges(Cell::Visitor& visitor)
  28. {
  29. Base::visit_edges(visitor);
  30. visitor.visit(m_page);
  31. visitor.visit(m_browsing_context_set);
  32. }
  33. // https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-browsing-context-group-and-document
  34. auto BrowsingContextGroup::create_a_new_browsing_context_group_and_document(JS::NonnullGCPtr<Page> page) -> WebIDL::ExceptionOr<BrowsingContextGroupAndDocument>
  35. {
  36. // 1. Let group be a new browsing context group.
  37. // 2. Append group to the user agent's browsing context group set.
  38. auto group = Bindings::main_thread_vm().heap().allocate_without_realm<BrowsingContextGroup>(page);
  39. // 3. Let browsingContext and document be the result of creating a new browsing context and document with null, null, and group.
  40. auto [browsing_context, document] = TRY(BrowsingContext::create_a_new_browsing_context_and_document(page, nullptr, nullptr, group));
  41. // 4. Append browsingContext to group.
  42. group->append(browsing_context);
  43. // 5. Return group and document.
  44. return BrowsingContextGroupAndDocument { group, document };
  45. }
  46. // https://html.spec.whatwg.org/multipage/browsers.html#bcg-append
  47. void BrowsingContextGroup::append(BrowsingContext& browsing_context)
  48. {
  49. VERIFY(browsing_context.is_top_level());
  50. // 1. Append browsingContext to group's browsing context set.
  51. m_browsing_context_set.set(browsing_context);
  52. // 2. Set browsingContext's group to group.
  53. browsing_context.set_group(this);
  54. }
  55. }