LibWeb: Change Storage{Bottle,Bucket,Shelf} to be GC-allocated

In upcoming changes StorageBottle will own pointers to GC-allocated
objects, so it needs to be a GC-allocated object itself to avoid
introducing more GC roots.
This commit is contained in:
Aliaksandr Kalenik 2025-06-11 18:51:22 +02:00 committed by Alexander Kalenik
commit f53559cb55
Notes: github-actions[bot] 2025-06-12 15:06:02 +00:00
12 changed files with 117 additions and 44 deletions

View file

@ -917,13 +917,13 @@ struct UnderlyingSource;
namespace Web::StorageAPI { namespace Web::StorageAPI {
class NavigatorStorage; class NavigatorStorage;
class StorageBottle;
class StorageBucket;
class StorageManager; class StorageManager;
class StorageShed; class StorageShed;
class StorageShelf;
struct StorageBottle;
struct StorageBucket;
struct StorageEndpoint; struct StorageEndpoint;
struct StorageShelf;
} }

View file

@ -25,12 +25,12 @@ static HashTable<GC::RawRef<Storage>>& all_storages()
return storages; return storages;
} }
GC::Ref<Storage> Storage::create(JS::Realm& realm, Type type, NonnullRefPtr<StorageAPI::StorageBottle> storage_bottle) GC::Ref<Storage> Storage::create(JS::Realm& realm, Type type, GC::Ref<StorageAPI::StorageBottle> storage_bottle)
{ {
return realm.create<Storage>(realm, type, move(storage_bottle)); return realm.create<Storage>(realm, type, move(storage_bottle));
} }
Storage::Storage(JS::Realm& realm, Type type, NonnullRefPtr<StorageAPI::StorageBottle> storage_bottle) Storage::Storage(JS::Realm& realm, Type type, GC::Ref<StorageAPI::StorageBottle> storage_bottle)
: Bindings::PlatformObject(realm) : Bindings::PlatformObject(realm)
, m_type(type) , m_type(type)
, m_storage_bottle(move(storage_bottle)) , m_storage_bottle(move(storage_bottle))
@ -65,6 +65,12 @@ void Storage::finalize()
all_storages().remove(*this); all_storages().remove(*this);
} }
void Storage::visit_edges(GC::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_storage_bottle);
}
// https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-length // https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-length
size_t Storage::length() const size_t Storage::length() const
{ {

View file

@ -27,7 +27,7 @@ public:
Session, Session,
}; };
[[nodiscard]] static GC::Ref<Storage> create(JS::Realm&, Type, NonnullRefPtr<StorageAPI::StorageBottle>); [[nodiscard]] static GC::Ref<Storage> create(JS::Realm&, Type, GC::Ref<StorageAPI::StorageBottle>);
~Storage(); ~Storage();
@ -44,10 +44,11 @@ public:
void dump() const; void dump() const;
private: private:
Storage(JS::Realm&, Type, NonnullRefPtr<StorageAPI::StorageBottle>); Storage(JS::Realm&, Type, GC::Ref<StorageAPI::StorageBottle>);
virtual void initialize(JS::Realm&) override; virtual void initialize(JS::Realm&) override;
virtual void finalize() override; virtual void finalize() override;
virtual void visit_edges(GC::Cell::Visitor&) override;
// ^PlatformObject // ^PlatformObject
virtual Optional<JS::Value> item_value(size_t index) const override; virtual Optional<JS::Value> item_value(size_t index) const override;
@ -61,7 +62,7 @@ private:
void broadcast(Optional<String> const& key, Optional<String> const& old_value, Optional<String> const& new_value); void broadcast(Optional<String> const& key, Optional<String> const& old_value, Optional<String> const& new_value);
Type m_type {}; Type m_type {};
NonnullRefPtr<StorageAPI::StorageBottle> m_storage_bottle; GC::Ref<StorageAPI::StorageBottle> m_storage_bottle;
u64 m_stored_bytes { 0 }; u64 m_stored_bytes { 0 };
}; };

View file

@ -50,6 +50,7 @@ static RefPtr<Gfx::SkiaBackendContext> get_skia_backend_context()
TraversableNavigable::TraversableNavigable(GC::Ref<Page> page) TraversableNavigable::TraversableNavigable(GC::Ref<Page> page)
: Navigable(page) : Navigable(page)
, m_storage_shed(StorageAPI::StorageShed::create(page->heap()))
, m_session_history_traversal_queue(vm().heap().allocate<SessionHistoryTraversalQueue>()) , m_session_history_traversal_queue(vm().heap().allocate<SessionHistoryTraversalQueue>())
{ {
if (!page->client().is_svg_page_client()) { if (!page->client().is_svg_page_client()) {
@ -75,6 +76,7 @@ void TraversableNavigable::visit_edges(Cell::Visitor& visitor)
Base::visit_edges(visitor); Base::visit_edges(visitor);
visitor.visit(m_session_history_entries); visitor.visit(m_session_history_entries);
visitor.visit(m_session_history_traversal_queue); visitor.visit(m_session_history_traversal_queue);
visitor.visit(m_storage_shed);
} }
static OrderedHashTable<TraversableNavigable*>& user_agent_top_level_traversable_set() static OrderedHashTable<TraversableNavigable*>& user_agent_top_level_traversable_set()

View file

@ -164,7 +164,7 @@ private:
// https://storage.spec.whatwg.org/#traversable-navigable-storage-shed // https://storage.spec.whatwg.org/#traversable-navigable-storage-shed
// A traversable navigable holds a storage shed, which is a storage shed. A traversable navigables storage shed holds all session storage data. // A traversable navigable holds a storage shed, which is a storage shed. A traversable navigables storage shed holds all session storage data.
StorageAPI::StorageShed m_storage_shed; GC::Ref<StorageAPI::StorageShed> m_storage_shed;
GC::Ref<SessionHistoryTraversalQueue> m_session_history_traversal_queue; GC::Ref<SessionHistoryTraversalQueue> m_session_history_traversal_queue;

View file

@ -471,7 +471,7 @@ WebIDL::ExceptionOr<GC::Ref<Storage>> Window::local_storage()
return WebIDL::SecurityError::create(realm, "localStorage is not available"_string); return WebIDL::SecurityError::create(realm, "localStorage is not available"_string);
// 4. Let storage be a new Storage object whose map is map. // 4. Let storage be a new Storage object whose map is map.
auto storage = Storage::create(realm, Storage::Type::Session, map.release_nonnull()); auto storage = Storage::create(realm, Storage::Type::Session, *map);
// 5. Set this's associated Document's local storage holder to storage. // 5. Set this's associated Document's local storage holder to storage.
associated_document.set_local_storage_holder(storage); associated_document.set_local_storage_holder(storage);
@ -498,7 +498,7 @@ WebIDL::ExceptionOr<GC::Ref<Storage>> Window::session_storage()
return WebIDL::SecurityError::create(realm, "sessionStorage is not available"_string); return WebIDL::SecurityError::create(realm, "sessionStorage is not available"_string);
// 4. Let storage be a new Storage object whose map is map. // 4. Let storage be a new Storage object whose map is map.
auto storage = Storage::create(realm, Storage::Type::Session, map.release_nonnull()); auto storage = Storage::create(realm, Storage::Type::Session, *map);
// 5. Set this's associated Document's session storage holder to storage. // 5. Set this's associated Document's session storage holder to storage.
associated_document.set_session_storage_holder(storage); associated_document.set_session_storage_holder(storage);

View file

@ -13,6 +13,15 @@
namespace Web::StorageAPI { namespace Web::StorageAPI {
GC_DEFINE_ALLOCATOR(StorageBottle);
GC_DEFINE_ALLOCATOR(StorageBucket);
void StorageBucket::visit_edges(GC::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_bottle_map);
}
StorageBucket::StorageBucket(StorageType type) StorageBucket::StorageBucket(StorageType type)
{ {
// 1. Let bucket be null. // 1. Let bucket be null.
@ -24,21 +33,21 @@ StorageBucket::StorageBucket(StorageType type)
// 4. For each endpoint of registered storage endpoints whose types contain type, set buckets bottle map[endpoints identifier] to a new storage bottle whose quota is endpoints quota. // 4. For each endpoint of registered storage endpoints whose types contain type, set buckets bottle map[endpoints identifier] to a new storage bottle whose quota is endpoints quota.
for (auto const& endpoint : StorageEndpoint::registered_endpoints()) { for (auto const& endpoint : StorageEndpoint::registered_endpoints()) {
if (endpoint.type == type) if (endpoint.type == type)
bottle_map.set(endpoint.identifier, StorageBottle::create(endpoint.quota)); m_bottle_map.set(endpoint.identifier, StorageBottle::create(heap(), endpoint.quota));
} }
// 5. Return bucket. // 5. Return bucket.
} }
// https://storage.spec.whatwg.org/#obtain-a-storage-bottle-map // https://storage.spec.whatwg.org/#obtain-a-storage-bottle-map
RefPtr<StorageBottle> obtain_a_storage_bottle_map(StorageType type, HTML::EnvironmentSettingsObject& environment, StringView identifier) GC::Ptr<StorageBottle> obtain_a_storage_bottle_map(StorageType type, HTML::EnvironmentSettingsObject& environment, StringView identifier)
{ {
// 1. Let shed be null. // 1. Let shed be null.
StorageShed* shed = nullptr; GC::Ptr<StorageShed> shed = nullptr;
// 2. If type is "local", then set shed to the user agents storage shed. // 2. If type is "local", then set shed to the user agents storage shed.
if (type == StorageType::Local) { if (type == StorageType::Local) {
shed = &user_agent_storage_shed(); shed = user_agent_storage_shed(environment.heap());
} }
// 3. Otherwise: // 3. Otherwise:
else { else {
@ -54,14 +63,14 @@ RefPtr<StorageBottle> obtain_a_storage_bottle_map(StorageType type, HTML::Enviro
auto shelf = shed->obtain_a_storage_shelf(environment, type); auto shelf = shed->obtain_a_storage_shelf(environment, type);
// 5. If shelf is failure, then return failure. // 5. If shelf is failure, then return failure.
if (!shelf.has_value()) if (!shelf)
return {}; return {};
// 6. Let bucket be shelfs bucket map["default"]. // 6. Let bucket be shelfs bucket map["default"].
auto& bucket = shelf->bucket_map.get("default"sv).value(); auto& bucket = shelf->bucket_map().get("default"sv).value();
// 7. Let bottle be buckets bottle map[identifier]. // 7. Let bottle be buckets bottle map[identifier].
auto bottle = bucket.bottle_map.get(identifier).value(); auto bottle = bucket->bottle_map().get(identifier).value();
// 8. Let proxyMap be a new storage proxy map whose backing map is bottles map. // 8. Let proxyMap be a new storage proxy map whose backing map is bottles map.
// 9. Append proxyMap to bottles proxy map reference set. // 9. Append proxyMap to bottles proxy map reference set.
@ -70,7 +79,7 @@ RefPtr<StorageBottle> obtain_a_storage_bottle_map(StorageType type, HTML::Enviro
} }
// https://storage.spec.whatwg.org/#obtain-a-session-storage-bottle-map // https://storage.spec.whatwg.org/#obtain-a-session-storage-bottle-map
RefPtr<StorageBottle> obtain_a_session_storage_bottle_map(HTML::EnvironmentSettingsObject& environment, StringView identifier) GC::Ptr<StorageBottle> obtain_a_session_storage_bottle_map(HTML::EnvironmentSettingsObject& environment, StringView identifier)
{ {
// To obtain a session storage bottle map, given an environment settings object environment and storage identifier identifier, // To obtain a session storage bottle map, given an environment settings object environment and storage identifier identifier,
// return the result of running obtain a storage bottle map with "session", environment, and identifier. // return the result of running obtain a storage bottle map with "session", environment, and identifier.
@ -78,7 +87,7 @@ RefPtr<StorageBottle> obtain_a_session_storage_bottle_map(HTML::EnvironmentSetti
} }
// https://storage.spec.whatwg.org/#obtain-a-local-storage-bottle-map // https://storage.spec.whatwg.org/#obtain-a-local-storage-bottle-map
RefPtr<StorageBottle> obtain_a_local_storage_bottle_map(HTML::EnvironmentSettingsObject& environment, StringView identifier) GC::Ptr<StorageBottle> obtain_a_local_storage_bottle_map(HTML::EnvironmentSettingsObject& environment, StringView identifier)
{ {
// To obtain a local storage bottle map, given an environment settings object environment and storage identifier identifier, // To obtain a local storage bottle map, given an environment settings object environment and storage identifier identifier,
// return the result of running obtain a storage bottle map with "local", environment, and identifier. // return the result of running obtain a storage bottle map with "local", environment, and identifier.

View file

@ -14,14 +14,17 @@
namespace Web::StorageAPI { namespace Web::StorageAPI {
// https://storage.spec.whatwg.org/#storage-bottle // https://storage.spec.whatwg.org/#storage-bottle
struct StorageBottle : public RefCounted<StorageBottle> { class StorageBottle : public GC::Cell {
static NonnullRefPtr<StorageBottle> create(Optional<u64> quota) { return adopt_ref(*new StorageBottle(quota)); } GC_CELL(StorageBottle, GC::Cell);
GC_DECLARE_ALLOCATOR(StorageBottle);
static GC::Ref<StorageBottle> create(GC::Heap& heap, Optional<u64> quota) { return heap.allocate<StorageBottle>(quota); }
// A storage bottle has a map, which is initially an empty map // A storage bottle has a map, which is initially an empty map
OrderedHashMap<String, String> map; OrderedHashMap<String, String> map;
// A storage bottle also has a proxy map reference set, which is initially an empty set // A storage bottle also has a proxy map reference set, which is initially an empty set
NonnullRefPtr<StorageBottle> proxy() { return *this; } GC::Ref<StorageBottle> proxy() { return *this; }
// A storage bottle also has a quota, which is null or a number representing a conservative estimate of // A storage bottle also has a quota, which is null or a number representing a conservative estimate of
// the total amount of bytes it can hold. Null indicates the lack of a limit. // the total amount of bytes it can hold. Null indicates the lack of a limit.
@ -34,19 +37,31 @@ private:
} }
}; };
using BottleMap = OrderedHashMap<String, NonnullRefPtr<StorageBottle>>; using BottleMap = OrderedHashMap<String, GC::Ref<StorageBottle>>;
// https://storage.spec.whatwg.org/#storage-bucket // https://storage.spec.whatwg.org/#storage-bucket
// A storage bucket is a place for storage endpoints to store data. // A storage bucket is a place for storage endpoints to store data.
struct StorageBucket { class StorageBucket : public GC::Cell {
GC_CELL(StorageBucket, GC::Cell);
GC_DECLARE_ALLOCATOR(StorageBucket);
public:
static GC::Ref<StorageBucket> create(GC::Heap& heap, StorageType type) { return heap.allocate<StorageBucket>(type); }
BottleMap& bottle_map() { return m_bottle_map; }
BottleMap const& bottle_map() const { return m_bottle_map; }
virtual void visit_edges(GC::Cell::Visitor& visitor) override;
private:
explicit StorageBucket(StorageType); explicit StorageBucket(StorageType);
// A storage bucket has a bottle map of storage identifiers to storage bottles. // A storage bucket has a bottle map of storage identifiers to storage bottles.
BottleMap bottle_map; BottleMap m_bottle_map;
}; };
RefPtr<StorageBottle> obtain_a_session_storage_bottle_map(HTML::EnvironmentSettingsObject&, StringView storage_identifier); GC::Ptr<StorageBottle> obtain_a_session_storage_bottle_map(HTML::EnvironmentSettingsObject&, StringView storage_identifier);
RefPtr<StorageBottle> obtain_a_local_storage_bottle_map(HTML::EnvironmentSettingsObject&, StringView storage_identifier); GC::Ptr<StorageBottle> obtain_a_local_storage_bottle_map(HTML::EnvironmentSettingsObject&, StringView storage_identifier);
RefPtr<StorageBottle> obtain_a_storage_bottle_map(StorageType, HTML::EnvironmentSettingsObject&, StringView storage_identifier); GC::Ptr<StorageBottle> obtain_a_storage_bottle_map(StorageType, HTML::EnvironmentSettingsObject&, StringView storage_identifier);
} }

View file

@ -4,13 +4,22 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <LibGC/Heap.h>
#include <LibWeb/HTML/Scripting/Environments.h> #include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/StorageAPI/StorageShed.h> #include <LibWeb/StorageAPI/StorageShed.h>
namespace Web::StorageAPI { namespace Web::StorageAPI {
GC_DEFINE_ALLOCATOR(StorageShed);
void StorageShed::visit_edges(GC::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_data);
}
// https://storage.spec.whatwg.org/#obtain-a-storage-shelf // https://storage.spec.whatwg.org/#obtain-a-storage-shelf
Optional<StorageShelf&> StorageShed::obtain_a_storage_shelf(HTML::EnvironmentSettingsObject const& environment, StorageType type) GC::Ptr<StorageShelf> StorageShed::obtain_a_storage_shelf(HTML::EnvironmentSettingsObject const& environment, StorageType type)
{ {
// 1. Let key be the result of running obtain a storage key with environment. // 1. Let key be the result of running obtain a storage key with environment.
auto key = obtain_a_storage_key(environment); auto key = obtain_a_storage_key(environment);
@ -21,18 +30,18 @@ Optional<StorageShelf&> StorageShed::obtain_a_storage_shelf(HTML::EnvironmentSet
// 3. If shed[key] does not exist, then set shed[key] to the result of running create a storage shelf with type. // 3. If shed[key] does not exist, then set shed[key] to the result of running create a storage shelf with type.
// 4. Return shed[key]. // 4. Return shed[key].
return m_data.ensure(key.value(), [type] { return m_data.ensure(key.value(), [type, &heap = this->heap()] {
return StorageShelf { type }; return StorageShelf::create(heap, type);
}); });
} }
// https://storage.spec.whatwg.org/#user-agent-storage-shed // https://storage.spec.whatwg.org/#user-agent-storage-shed
StorageShed& user_agent_storage_shed() GC::Ref<StorageShed> user_agent_storage_shed(GC::Heap& heap)
{ {
// A user agent holds a storage shed, which is a storage shed. A user agents storage shed holds all local storage data. // A user agent holds a storage shed, which is a storage shed. A user agents storage shed holds all local storage data.
// FIXME: Storing this statically in memory is not the correct place or way of doing this! // FIXME: Storing this statically in memory is not the correct place or way of doing this!
static StorageShed storage_shed; static GC::Root<StorageShed> storage_shed = GC::make_root(StorageShed::create(heap));
return storage_shed; return *storage_shed;
} }
} }

View file

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <AK/HashMap.h> #include <AK/HashMap.h>
#include <LibGC/Ptr.h>
#include <LibWeb/Forward.h> #include <LibWeb/Forward.h>
#include <LibWeb/StorageAPI/StorageKey.h> #include <LibWeb/StorageAPI/StorageKey.h>
#include <LibWeb/StorageAPI/StorageShelf.h> #include <LibWeb/StorageAPI/StorageShelf.h>
@ -16,14 +17,23 @@ namespace Web::StorageAPI {
// https://storage.spec.whatwg.org/#storage-shed // https://storage.spec.whatwg.org/#storage-shed
// A storage shed is a map of storage keys to storage shelves. It is initially empty. // A storage shed is a map of storage keys to storage shelves. It is initially empty.
class StorageShed { class StorageShed : public GC::Cell {
GC_CELL(StorageShed, GC::Cell);
GC_DECLARE_ALLOCATOR(StorageShed);
public: public:
Optional<StorageShelf&> obtain_a_storage_shelf(HTML::EnvironmentSettingsObject const&, StorageType); static GC::Ref<StorageShed> create(GC::Heap& heap) { return heap.allocate<StorageShed>(); }
GC::Ptr<StorageShelf> obtain_a_storage_shelf(HTML::EnvironmentSettingsObject const&, StorageType);
virtual void visit_edges(GC::Cell::Visitor& visitor) override;
private: private:
OrderedHashMap<StorageKey, StorageShelf> m_data; StorageShed() = default;
OrderedHashMap<StorageKey, GC::Ref<StorageShelf>> m_data;
}; };
StorageShed& user_agent_storage_shed(); GC::Ref<StorageShed> user_agent_storage_shed(GC::Heap&);
} }

View file

@ -4,16 +4,25 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <LibGC/Heap.h>
#include <LibWeb/StorageAPI/StorageShelf.h> #include <LibWeb/StorageAPI/StorageShelf.h>
namespace Web::StorageAPI { namespace Web::StorageAPI {
GC_DEFINE_ALLOCATOR(StorageShelf);
void StorageShelf::visit_edges(GC::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_bucket_map);
}
// https://storage.spec.whatwg.org/#create-a-storage-shelf // https://storage.spec.whatwg.org/#create-a-storage-shelf
StorageShelf::StorageShelf(StorageType type) StorageShelf::StorageShelf(StorageType type)
{ {
// 1. Let shelf be a new storage shelf. // 1. Let shelf be a new storage shelf.
// 2. Set shelfs bucket map["default"] to the result of running create a storage bucket with type. // 2. Set shelfs bucket map["default"] to the result of running create a storage bucket with type.
bucket_map.set("default"_string, StorageBucket { type }); m_bucket_map.set("default"_string, StorageBucket::create(heap(), type));
// 3. Return shelf. // 3. Return shelf.
} }

View file

@ -8,7 +8,7 @@
#include <AK/HashMap.h> #include <AK/HashMap.h>
#include <AK/String.h> #include <AK/String.h>
#include <LibWeb/Forward.h> #include <LibGC/Ptr.h>
#include <LibWeb/StorageAPI/StorageBottle.h> #include <LibWeb/StorageAPI/StorageBottle.h>
#include <LibWeb/StorageAPI/StorageType.h> #include <LibWeb/StorageAPI/StorageType.h>
@ -16,12 +16,24 @@ namespace Web::StorageAPI {
// https://storage.spec.whatwg.org/#storage-shelf // https://storage.spec.whatwg.org/#storage-shelf
// A storage shelf exists for each storage key within a storage shed. It holds a bucket map, which is a map of strings to storage buckets. // A storage shelf exists for each storage key within a storage shed. It holds a bucket map, which is a map of strings to storage buckets.
using BucketMap = OrderedHashMap<String, StorageBucket>; using BucketMap = OrderedHashMap<String, GC::Ref<StorageBucket>>;
struct StorageShelf { class StorageShelf : public GC::Cell {
GC_CELL(StorageShelf, GC::Cell);
GC_DECLARE_ALLOCATOR(StorageShelf);
public:
static GC::Ref<StorageShelf> create(GC::Heap& heap, StorageType type) { return heap.allocate<StorageShelf>(type); }
BucketMap& bucket_map() { return m_bucket_map; }
BucketMap const& bucket_map() const { return m_bucket_map; }
virtual void visit_edges(GC::Cell::Visitor& visitor) override;
private:
explicit StorageShelf(StorageType); explicit StorageShelf(StorageType);
BucketMap bucket_map; BucketMap m_bucket_map;
}; };
} }