BrowsingContextGroup.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. for (auto& context : m_browsing_context_set)
  32. visitor.visit(context);
  33. }
  34. // https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-browsing-context-group-and-document
  35. auto BrowsingContextGroup::create_a_new_browsing_context_group_and_document(JS::NonnullGCPtr<Page> page) -> WebIDL::ExceptionOr<BrowsingContextGroupAndDocument>
  36. {
  37. // 1. Let group be a new browsing context group.
  38. // 2. Append group to the user agent's browsing context group set.
  39. auto group = Bindings::main_thread_vm().heap().allocate_without_realm<BrowsingContextGroup>(page);
  40. // 3. Let browsingContext and document be the result of creating a new browsing context and document with null, null, and group.
  41. auto [browsing_context, document] = TRY(BrowsingContext::create_a_new_browsing_context_and_document(page, nullptr, nullptr, group));
  42. // 4. Append browsingContext to group.
  43. group->append(browsing_context);
  44. // 5. Return group and document.
  45. return BrowsingContextGroupAndDocument { group, document };
  46. }
  47. // https://html.spec.whatwg.org/multipage/browsers.html#bcg-append
  48. void BrowsingContextGroup::append(BrowsingContext& browsing_context)
  49. {
  50. VERIFY(browsing_context.is_top_level());
  51. // 1. Append browsingContext to group's browsing context set.
  52. m_browsing_context_set.set(browsing_context);
  53. // 2. Set browsingContext's group to group.
  54. browsing_context.set_group(this);
  55. }
  56. }