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
parent 70a29f36c6
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

@ -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 agents 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;
}
}