BrowsingContextGroup.cpp 2.3 KB

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