BrowsingContextGroup.cpp 2.1 KB

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