From 8ead228202397e79802a35b9b1b6c7ad4567bd96 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 19 Sep 2022 12:28:46 +0200 Subject: [PATCH] LibWeb: Implement "browsing context group" concept from the HTML spec --- Userland/Libraries/LibWeb/CMakeLists.txt | 1 + Userland/Libraries/LibWeb/DOM/Document.cpp | 2 +- Userland/Libraries/LibWeb/Forward.h | 1 + .../Libraries/LibWeb/HTML/BrowsingContext.cpp | 42 ++++++++++++- .../Libraries/LibWeb/HTML/BrowsingContext.h | 12 +++- .../LibWeb/HTML/BrowsingContextContainer.cpp | 16 ++--- .../LibWeb/HTML/BrowsingContextGroup.cpp | 59 +++++++++++++++++++ .../LibWeb/HTML/BrowsingContextGroup.h | 39 ++++++++++++ Userland/Libraries/LibWeb/Page/Page.cpp | 2 +- 9 files changed, 163 insertions(+), 11 deletions(-) create mode 100644 Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.cpp create mode 100644 Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.h diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index dcd150f97ed..bf2ba79cb48 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 b55d3564aac..5630e0657ce 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -98,7 +98,7 @@ static NonnullRefPtr 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 5595c9cb922..ead5a739491 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 e06dc64ef05..919259011e5 100644 --- a/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp +++ b/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -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::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::create_a_new_browsing_context(Page& page, JS::GCPtr creator, JS::GCPtr embedder) +NonnullRefPtr BrowsingContext::create_a_new_browsing_context(Page& page, JS::GCPtr creator, JS::GCPtr embedder, BrowsingContextGroup&) { // 1. Let browsingContext be a new browsing context. BrowsingContextContainer* container = (embedder && is(*embedder)) ? static_cast(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 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 54f0c1d9a82..c2c6e5c88e2 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 { public: - static NonnullRefPtr create_a_new_browsing_context(Page&, JS::GCPtr creator, JS::GCPtr embedder); + static NonnullRefPtr create_a_new_browsing_context(Page&, JS::GCPtr creator, JS::GCPtr embedder, BrowsingContextGroup&); + static NonnullRefPtr 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 m_frame_nesting_levels; String m_name; + + // https://html.spec.whatwg.org/multipage/browsers.html#tlbc-group + RefPtr m_group; }; HTML::Origin determine_the_origin(BrowsingContext const& browsing_context, Optional url, SandboxingFlagSet sandbox_flags, Optional invocation_origin); diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp b/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp index 2ad0dc33b3f..62e97996afa 100644 --- a/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp +++ b/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -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 00000000000..bb2161b8dc6 --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace Web::HTML { + +// https://html.spec.whatwg.org/multipage/browsers.html#browsing-context-group-set +static HashTable& user_agent_browsing_context_group_set() +{ + static HashTable 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::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 00000000000..2e0c08b4a4e --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include + +namespace Web::HTML { + +class BrowsingContextGroup : public RefCounted { +public: + static NonnullRefPtr 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> m_browsing_context_set; + + WeakPtr m_page; +}; + +} diff --git a/Userland/Libraries/LibWeb/Page/Page.cpp b/Userland/Libraries/LibWeb/Page/Page.cpp index 3380129157c..e8bd7f75dd0 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;