Skip to content

Commit

Permalink
LibWeb: Implement "browsing context group" concept from the HTML spec
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Sep 20, 2022
1 parent e36750d commit 8ead228
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 11 deletions.
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/DOM/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/Forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class DOMRectReadOnly;
namespace Web::HTML {
class BrowsingContext;
class BrowsingContextContainer;
class BrowsingContextGroup;
class CanvasRenderingContext2D;
class ClassicScript;
class CloseEvent;
Expand Down
42 changes: 41 additions & 1 deletion Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
}

}
12 changes: 11 additions & 1 deletion Userland/Libraries/LibWeb/HTML/BrowsingContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -124,6 +125,12 @@ class BrowsingContext : public TreeNode<BrowsingContext> {
// 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*);

Expand Down Expand Up @@ -161,6 +168,9 @@ class BrowsingContext : public TreeNode<BrowsingContext> {

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);
Expand Down
16 changes: 9 additions & 7 deletions Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand All @@ -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.
Expand Down
59 changes: 59 additions & 0 deletions Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2022, Andreas Kling <[email protected]>
*
* 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);
}

}
39 changes: 39 additions & 0 deletions Userland/Libraries/LibWeb/HTML/BrowsingContextGroup.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2022, Andreas Kling <[email protected]>
*
* 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;
};

}
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/Page/Page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8ead228

Please sign in to comment.