mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-24 18:02:20 +00:00
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:
parent
70a29f36c6
commit
f53559cb55
Notes:
github-actions[bot]
2025-06-12 15:06:02 +00:00
Author: https://github.com/kalenikaliaksandr
Commit: f53559cb55
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5052
Reviewed-by: https://github.com/shannonbooth ✅
Reviewed-by: https://github.com/trflynn89
12 changed files with 117 additions and 44 deletions
|
@ -13,6 +13,15 @@
|
|||
|
||||
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)
|
||||
{
|
||||
// 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 bucket’s bottle map[endpoint’s identifier] to a new storage bottle whose quota is endpoint’s quota.
|
||||
for (auto const& endpoint : StorageEndpoint::registered_endpoints()) {
|
||||
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.
|
||||
}
|
||||
|
||||
// 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.
|
||||
StorageShed* shed = nullptr;
|
||||
GC::Ptr<StorageShed> shed = nullptr;
|
||||
|
||||
// 2. If type is "local", then set shed to the user agent’s storage shed.
|
||||
if (type == StorageType::Local) {
|
||||
shed = &user_agent_storage_shed();
|
||||
shed = user_agent_storage_shed(environment.heap());
|
||||
}
|
||||
// 3. Otherwise:
|
||||
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);
|
||||
|
||||
// 5. If shelf is failure, then return failure.
|
||||
if (!shelf.has_value())
|
||||
if (!shelf)
|
||||
return {};
|
||||
|
||||
// 6. Let bucket be shelf’s 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 bucket’s 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 bottle’s map.
|
||||
// 9. Append proxyMap to bottle’s 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
|
||||
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,
|
||||
// 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
|
||||
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,
|
||||
// return the result of running obtain a storage bottle map with "local", environment, and identifier.
|
||||
|
|
|
@ -14,14 +14,17 @@
|
|||
namespace Web::StorageAPI {
|
||||
|
||||
// https://storage.spec.whatwg.org/#storage-bottle
|
||||
struct StorageBottle : public RefCounted<StorageBottle> {
|
||||
static NonnullRefPtr<StorageBottle> create(Optional<u64> quota) { return adopt_ref(*new StorageBottle(quota)); }
|
||||
class StorageBottle : public GC::Cell {
|
||||
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
|
||||
OrderedHashMap<String, String> map;
|
||||
|
||||
// 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
|
||||
// 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
|
||||
// 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);
|
||||
|
||||
// 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);
|
||||
RefPtr<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_session_storage_bottle_map(HTML::EnvironmentSettingsObject&, StringView storage_identifier);
|
||||
GC::Ptr<StorageBottle> obtain_a_local_storage_bottle_map(HTML::EnvironmentSettingsObject&, StringView storage_identifier);
|
||||
GC::Ptr<StorageBottle> obtain_a_storage_bottle_map(StorageType, HTML::EnvironmentSettingsObject&, StringView storage_identifier);
|
||||
|
||||
}
|
||||
|
|
|
@ -4,13 +4,22 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibGC/Heap.h>
|
||||
#include <LibWeb/HTML/Scripting/Environments.h>
|
||||
#include <LibWeb/StorageAPI/StorageShed.h>
|
||||
|
||||
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
|
||||
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.
|
||||
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.
|
||||
// 4. Return shed[key].
|
||||
return m_data.ensure(key.value(), [type] {
|
||||
return StorageShelf { type };
|
||||
return m_data.ensure(key.value(), [type, &heap = this->heap()] {
|
||||
return StorageShelf::create(heap, type);
|
||||
});
|
||||
}
|
||||
|
||||
// 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 agent’s storage shed holds all local storage data.
|
||||
// FIXME: Storing this statically in memory is not the correct place or way of doing this!
|
||||
static StorageShed storage_shed;
|
||||
return storage_shed;
|
||||
static GC::Root<StorageShed> storage_shed = GC::make_root(StorageShed::create(heap));
|
||||
return *storage_shed;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/HashMap.h>
|
||||
#include <LibGC/Ptr.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
#include <LibWeb/StorageAPI/StorageKey.h>
|
||||
#include <LibWeb/StorageAPI/StorageShelf.h>
|
||||
|
@ -16,14 +17,23 @@ namespace Web::StorageAPI {
|
|||
|
||||
// https://storage.spec.whatwg.org/#storage-shed
|
||||
// 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:
|
||||
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:
|
||||
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&);
|
||||
|
||||
}
|
||||
|
|
|
@ -4,16 +4,25 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibGC/Heap.h>
|
||||
#include <LibWeb/StorageAPI/StorageShelf.h>
|
||||
|
||||
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
|
||||
StorageShelf::StorageShelf(StorageType type)
|
||||
{
|
||||
// 1. Let shelf be a new storage shelf.
|
||||
// 2. Set shelf’s 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.
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
#include <LibGC/Ptr.h>
|
||||
#include <LibWeb/StorageAPI/StorageBottle.h>
|
||||
#include <LibWeb/StorageAPI/StorageType.h>
|
||||
|
||||
|
@ -16,12 +16,24 @@ namespace Web::StorageAPI {
|
|||
|
||||
// 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.
|
||||
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);
|
||||
|
||||
BucketMap bucket_map;
|
||||
BucketMap m_bucket_map;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue