summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2022-09-19 12:28:46 +0200
committerAndreas Kling <kling@serenityos.org>2022-09-20 10:32:12 +0200
commit8ead228202397e79802a35b9b1b6c7ad4567bd96 (patch)
treef804f1172d1d14d9c73d142e74684a29bdd56ed0 /Userland
parente36750d5913034b6081f9ae295d90de29f44384c (diff)
downloadserenity-8ead228202397e79802a35b9b1b6c7ad4567bd96.zip
LibWeb: Implement "browsing context group" concept from the HTML spec
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibWeb/CMakeLists.txt1
-rw-r--r--Userland/Libraries/LibWeb/DOM/Document.cpp2
-rw-r--r--Userland/Libraries/LibWeb/Forward.h1
-rw-r--r--Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp42
-rw-r--r--Userland/Libraries/LibWeb/HTML/BrowsingContext.h12
-rw-r--r--Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp16
-rw-r--r--Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.cpp59
-rw-r--r--Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.h39
-rw-r--r--Userland/Libraries/LibWeb/Page/Page.cpp2
9 files changed, 163 insertions, 11 deletions
diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt
index dcd150f97e..bf2ba79cb4 100644
--- a/Userland/Libraries/LibWeb/CMakeLists.txt
+++ b/Userland/Libraries/LibWeb/CMakeLists.txt
@@ -144,6 +144,7 @@ set(SOURCES
HTML/AttributeNames.cpp
HTML/BrowsingContext.cpp
HTML/BrowsingContextContainer.cpp
+ HTML/BrowsingContextGroup.cpp
HTML/Canvas/CanvasDrawImage.cpp
HTML/Canvas/CanvasPath.cpp
HTML/Canvas/CanvasState.cpp
diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp
index b55d3564aa..5630e0657c 100644
--- a/Userland/Libraries/LibWeb/DOM/Document.cpp
+++ b/Userland/Libraries/LibWeb/DOM/Document.cpp
@@ -98,7 +98,7 @@ static NonnullRefPtr<HTML::BrowsingContext> obtain_a_browsing_context_to_use_for
// 3. Let newBrowsingContext be the result of creating a new top-level browsing context.
VERIFY(browsing_context.page());
- auto new_browsing_context = HTML::BrowsingContext::create_a_new_browsing_context(*browsing_context.page(), nullptr, nullptr);
+ auto new_browsing_context = HTML::BrowsingContext::create_a_new_top_level_browsing_context(*browsing_context.page());
// FIXME: 4. If navigationCOOP's value is "same-origin-plurs-COEP", then set newBrowsingContext's group's
// cross-origin isolation mode to either "logical" or "concrete". The choice of which is implementation-defined.
diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h
index 5595c9cb92..ead5a73949 100644
--- a/Userland/Libraries/LibWeb/Forward.h
+++ b/Userland/Libraries/LibWeb/Forward.h
@@ -211,6 +211,7 @@ class DOMRectReadOnly;
namespace Web::HTML {
class BrowsingContext;
class BrowsingContextContainer;
+class BrowsingContextGroup;
class CanvasRenderingContext2D;
class ClassicScript;
class CloseEvent;
diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp b/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp
index e06dc64ef0..919259011e 100644
--- a/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp
+++ b/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp
@@ -9,6 +9,7 @@
#include <LibWeb/DOM/HTMLCollection.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/BrowsingContextContainer.h>
+#include <LibWeb/HTML/BrowsingContextGroup.h>
#include <LibWeb/HTML/CrossOrigin/CrossOriginOpenerPolicy.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/HTMLAnchorElement.h>
@@ -86,8 +87,18 @@ HTML::Origin determine_the_origin(BrowsingContext const& browsing_context, Optio
return url_origin(*url);
}
+// https://html.spec.whatwg.org/multipage/browsers.html#creating-a-new-top-level-browsing-context
+NonnullRefPtr<BrowsingContext> BrowsingContext::create_a_new_top_level_browsing_context(Web::Page& page)
+{
+ // 1. Let group be the result of creating a new browsing context group.
+ auto group = BrowsingContextGroup::create_a_new_browsing_context_group(page);
+
+ // 2. Return group's browsing context set[0].
+ return *group->browsing_context_set().begin();
+}
+
// https://html.spec.whatwg.org/multipage/browsers.html#creating-a-new-browsing-context
-NonnullRefPtr<BrowsingContext> BrowsingContext::create_a_new_browsing_context(Page& page, JS::GCPtr<DOM::Document> creator, JS::GCPtr<DOM::Element> embedder)
+NonnullRefPtr<BrowsingContext> BrowsingContext::create_a_new_browsing_context(Page& page, JS::GCPtr<DOM::Document> creator, JS::GCPtr<DOM::Element> embedder, BrowsingContextGroup&)
{
// 1. Let browsingContext be a new browsing context.
BrowsingContextContainer* container = (embedder && is<BrowsingContextContainer>(*embedder)) ? static_cast<BrowsingContextContainer*>(embedder.ptr()) : nullptr;
@@ -805,4 +816,33 @@ void BrowsingContext::scroll_offset_did_change()
doc->pending_scroll_event_targets().append(*doc);
}
+BrowsingContextGroup* BrowsingContext::group()
+{
+ return m_group;
+}
+
+void BrowsingContext::set_group(BrowsingContextGroup* group)
+{
+ m_group = group;
+}
+
+// https://html.spec.whatwg.org/multipage/browsers.html#bcg-remove
+void BrowsingContext::remove()
+{
+ // 1. Assert: browsingContext's group is non-null, because a browsing context only gets discarded once.
+ VERIFY(group());
+
+ // 2. Let group be browsingContext's group.
+ NonnullRefPtr<BrowsingContextGroup> group = *this->group();
+
+ // 3. Set browsingContext's group to null.
+ set_group(nullptr);
+
+ // 4. Remove browsingContext from group's browsing context set.
+ group->browsing_context_set().remove(*this);
+
+ // 5. If group's browsing context set is empty, then remove group from the user agent's browsing context group set.
+ // NOTE: This is done by ~BrowsingContextGroup() when the refcount reaches 0.
+}
+
}
diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContext.h b/Userland/Libraries/LibWeb/HTML/BrowsingContext.h
index 54f0c1d9a8..c2c6e5c88e 100644
--- a/Userland/Libraries/LibWeb/HTML/BrowsingContext.h
+++ b/Userland/Libraries/LibWeb/HTML/BrowsingContext.h
@@ -26,7 +26,8 @@ namespace Web::HTML {
class BrowsingContext : public TreeNode<BrowsingContext> {
public:
- static NonnullRefPtr<BrowsingContext> create_a_new_browsing_context(Page&, JS::GCPtr<DOM::Document> creator, JS::GCPtr<DOM::Element> embedder);
+ static NonnullRefPtr<BrowsingContext> create_a_new_browsing_context(Page&, JS::GCPtr<DOM::Document> creator, JS::GCPtr<DOM::Element> embedder, BrowsingContextGroup&);
+ static NonnullRefPtr<BrowsingContext> create_a_new_top_level_browsing_context(Page&);
~BrowsingContext();
@@ -124,6 +125,12 @@ public:
// https://html.spec.whatwg.org/multipage/dom.html#still-on-its-initial-about:blank-document
bool still_on_its_initial_about_blank_document() const;
+ BrowsingContextGroup* group();
+ void set_group(BrowsingContextGroup*);
+
+ // https://html.spec.whatwg.org/multipage/browsers.html#bcg-remove
+ void remove();
+
private:
explicit BrowsingContext(Page&, HTML::BrowsingContextContainer*);
@@ -161,6 +168,9 @@ private:
HashMap<AK::URL, size_t> m_frame_nesting_levels;
String m_name;
+
+ // https://html.spec.whatwg.org/multipage/browsers.html#tlbc-group
+ RefPtr<BrowsingContextGroup> m_group;
};
HTML::Origin determine_the_origin(BrowsingContext const& browsing_context, Optional<AK::URL> url, SandboxingFlagSet sandbox_flags, Optional<HTML::Origin> invocation_origin);
diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp b/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp
index 2ad0dc33b3..62e97996af 100644
--- a/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp
+++ b/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp
@@ -9,6 +9,7 @@
#include <LibWeb/DOM/Event.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/BrowsingContextContainer.h>
+#include <LibWeb/HTML/BrowsingContextGroup.h>
#include <LibWeb/HTML/Origin.h>
#include <LibWeb/Page/Page.h>
@@ -25,18 +26,19 @@ BrowsingContextContainer::~BrowsingContextContainer() = default;
void BrowsingContextContainer::create_new_nested_browsing_context()
{
// 1. Let group be element's node document's browsing context's top-level browsing context's group.
- // FIXME: We do not have a concept of "browsing context groups" yet.
- auto* group = document().browsing_context();
- if (!group)
- return;
+ VERIFY(document().browsing_context());
+ auto* group = document().browsing_context()->top_level_browsing_context().group();
+ // NOTE: The spec assumes that `group` is non-null here.
+ VERIFY(group);
VERIFY(group->page());
// 2. Let browsingContext be the result of creating a new browsing context with element's node document, element, and group.
// 3. Set element's nested browsing context to browsingContext.
- m_nested_browsing_context = BrowsingContext::create_a_new_browsing_context(*group->page(), document(), *this);
- group->append_child(*m_nested_browsing_context);
- m_nested_browsing_context->set_frame_nesting_levels(group->frame_nesting_levels());
+ m_nested_browsing_context = BrowsingContext::create_a_new_browsing_context(*group->page(), document(), *this, *group);
+
+ document().browsing_context()->append_child(*m_nested_browsing_context);
+ m_nested_browsing_context->set_frame_nesting_levels(document().browsing_context()->frame_nesting_levels());
m_nested_browsing_context->register_frame_nesting(document().url());
// 4. If element has a name attribute, then set browsingContext's name to the value of this attribute.
diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.cpp b/Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.cpp
new file mode 100644
index 0000000000..bb2161b8dc
--- /dev/null
+++ b/Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.cpp
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWeb/HTML/BrowsingContext.h>
+#include <LibWeb/HTML/BrowsingContextGroup.h>
+
+namespace Web::HTML {
+
+// https://html.spec.whatwg.org/multipage/browsers.html#browsing-context-group-set
+static HashTable<BrowsingContextGroup*>& user_agent_browsing_context_group_set()
+{
+ static HashTable<BrowsingContextGroup*> set;
+ return set;
+}
+
+BrowsingContextGroup::BrowsingContextGroup(Web::Page& page)
+ : m_page(page)
+{
+ user_agent_browsing_context_group_set().set(this);
+}
+
+BrowsingContextGroup::~BrowsingContextGroup()
+{
+ user_agent_browsing_context_group_set().remove(this);
+}
+
+// https://html.spec.whatwg.org/multipage/browsers.html#creating-a-new-browsing-context-group
+NonnullRefPtr<BrowsingContextGroup> BrowsingContextGroup::create_a_new_browsing_context_group(Web::Page& page)
+{
+ // 1. Let group be a new browsing context group.
+ // 2. Append group to the user agent's browsing context group set.
+ auto group = adopt_ref(*new BrowsingContextGroup(page));
+
+ // 3. Let browsingContext be the result of creating a new browsing context with null, null, and group.
+ auto browsing_context = BrowsingContext::create_a_new_browsing_context(page, nullptr, nullptr, group);
+
+ // 4. Append browsingContext to group.
+ group->append(move(browsing_context));
+
+ // 5. Return group.
+ return group;
+}
+
+// https://html.spec.whatwg.org/multipage/browsers.html#bcg-append
+void BrowsingContextGroup::append(BrowsingContext& browsing_context)
+{
+ VERIFY(browsing_context.is_top_level());
+
+ // 1. Append browsingContext to group's browsing context set.
+ m_browsing_context_set.set(browsing_context);
+
+ // 2. Set browsingContext's group to group.
+ browsing_context.set_group(this);
+}
+
+}
diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.h b/Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.h
new file mode 100644
index 0000000000..2e0c08b4a4
--- /dev/null
+++ b/Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/HashMap.h>
+#include <AK/NonnullRefPtr.h>
+#include <AK/RefCounted.h>
+#include <LibWeb/Forward.h>
+
+namespace Web::HTML {
+
+class BrowsingContextGroup : public RefCounted<BrowsingContextGroup> {
+public:
+ static NonnullRefPtr<BrowsingContextGroup> create_a_new_browsing_context_group(Page&);
+ ~BrowsingContextGroup();
+
+ Page* page() { return m_page; }
+ Page const* page() const { return m_page; }
+
+ auto& browsing_context_set() { return m_browsing_context_set; }
+ auto const& browsing_context_set() const { return m_browsing_context_set; }
+
+ void append(BrowsingContext&);
+ void remove(BrowsingContext&);
+
+private:
+ explicit BrowsingContextGroup(Web::Page&);
+
+ // https://html.spec.whatwg.org/multipage/browsers.html#browsing-context-group-set
+ OrderedHashTable<NonnullRefPtr<BrowsingContext>> m_browsing_context_set;
+
+ WeakPtr<Page> m_page;
+};
+
+}
diff --git a/Userland/Libraries/LibWeb/Page/Page.cpp b/Userland/Libraries/LibWeb/Page/Page.cpp
index 3380129157..e8bd7f75dd 100644
--- a/Userland/Libraries/LibWeb/Page/Page.cpp
+++ b/Userland/Libraries/LibWeb/Page/Page.cpp
@@ -13,7 +13,7 @@ namespace Web {
Page::Page(PageClient& client)
: m_client(client)
{
- m_top_level_browsing_context = HTML::BrowsingContext::create_a_new_browsing_context(*this, nullptr, nullptr);
+ m_top_level_browsing_context = HTML::BrowsingContext::create_a_new_top_level_browsing_context(*this);
}
Page::~Page() = default;