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