Everywhere: Implement persistence of localStorage using sqlite

This change follows the pattern of our cookies persistence
implementation: the "browser" process is responsible for interacting
with the sqlite database, and WebContent communicates all storage
operations via IPC.

The new database table uses (storage_endpoint, storage_key, bottle_key)
as the primary key. This design follows concepts from the
https://storage.spec.whatwg.org/ and is intended to support reuse of the
persistence layer for other APIs (e.g., CacheStorage, IndexedDB). For
now, `storage_endpoint` is always "localStorage", `storage_key` is the
website's origin, and `bottle_key` is the name of the localStorage key.
This commit is contained in:
Aliaksandr Kalenik 2025-06-08 23:35:46 +02:00 committed by Alexander Kalenik
commit 84b9224121
Notes: github-actions[bot] 2025-06-12 15:05:54 +00:00
24 changed files with 694 additions and 118 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2024-2025, Shannon Booth <shannon@serenityos.org>
* Copyright (c) 2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -13,16 +14,18 @@
namespace Web::StorageAPI {
GC_DEFINE_ALLOCATOR(StorageBottle);
GC_DEFINE_ALLOCATOR(LocalStorageBottle);
GC_DEFINE_ALLOCATOR(SessionStorageBottle);
GC_DEFINE_ALLOCATOR(StorageBucket);
void StorageBucket::visit_edges(GC::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_bottle_map);
for (auto& entry : m_bottle_map)
visitor.visit(entry);
}
StorageBucket::StorageBucket(StorageType type)
StorageBucket::StorageBucket(GC::Ref<Page> page, StorageKey key, StorageType type)
{
// 1. Let bucket be null.
// 2. If type is "local", then set bucket to a new local storage bucket.
@ -33,21 +36,23 @@ 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.
for (auto const& endpoint : StorageEndpoint::registered_endpoints()) {
if (endpoint.type == type)
m_bottle_map.set(endpoint.identifier, StorageBottle::create(heap(), endpoint.quota));
m_bottle_map[to_underlying(endpoint.identifier)] = StorageBottle::create(heap(), page, type, key, endpoint.quota);
}
// 5. Return bucket.
}
// https://storage.spec.whatwg.org/#obtain-a-storage-bottle-map
GC::Ptr<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, StorageEndpointType endpoint_type)
{
// 1. Let shed be null.
GC::Ptr<StorageShed> shed = nullptr;
GC::Ptr<StorageShed> shed;
// 2. If type is "local", then set shed to the user agents storage shed.
if (type == StorageType::Local) {
shed = user_agent_storage_shed(environment.heap());
// NOTE: Bottle for local storage is constructed directly, bypassing this function, because
// in that case StorageJar located on browser process side is used as a shed.
VERIFY_NOT_REACHED();
}
// 3. Otherwise:
else {
@ -67,10 +72,10 @@ GC::Ptr<StorageBottle> obtain_a_storage_bottle_map(StorageType type, HTML::Envir
return {};
// 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].
auto bottle = bucket->bottle_map().get(identifier).value();
auto bottle = bucket->bottle_map()[to_underlying(endpoint_type)];
// 8. Let proxyMap be a new storage proxy map whose backing map is bottles map.
// 9. Append proxyMap to bottles proxy map reference set.
@ -79,19 +84,100 @@ GC::Ptr<StorageBottle> obtain_a_storage_bottle_map(StorageType type, HTML::Envir
}
// https://storage.spec.whatwg.org/#obtain-a-session-storage-bottle-map
GC::Ptr<StorageBottle> obtain_a_session_storage_bottle_map(HTML::EnvironmentSettingsObject& environment, StringView identifier)
GC::Ptr<StorageBottle> obtain_a_session_storage_bottle_map(HTML::EnvironmentSettingsObject& environment, StorageEndpointType 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 obtain_a_storage_bottle_map(StorageType::Session, environment, identifier);
}
// https://storage.spec.whatwg.org/#obtain-a-local-storage-bottle-map
GC::Ptr<StorageBottle> obtain_a_local_storage_bottle_map(HTML::EnvironmentSettingsObject& environment, StringView identifier)
GC::Ref<StorageBottle> StorageBottle::create(GC::Heap& heap, GC::Ref<Page> page, StorageType type, StorageKey key, Optional<u64> quota)
{
// 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 obtain_a_storage_bottle_map(StorageType::Local, environment, identifier);
if (type == StorageType::Local)
return LocalStorageBottle::create(heap, page, key, quota);
return SessionStorageBottle::create(heap, quota);
}
void LocalStorageBottle::visit_edges(GC::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_page);
}
size_t LocalStorageBottle::size() const
{
return m_page->client().page_did_request_storage_keys(Web::StorageAPI::StorageEndpointType::LocalStorage, m_storage_key.to_string()).size();
}
Vector<String> LocalStorageBottle::keys() const
{
return m_page->client().page_did_request_storage_keys(Web::StorageAPI::StorageEndpointType::LocalStorage, m_storage_key.to_string());
}
Optional<String> LocalStorageBottle::get(String const& key) const
{
return m_page->client().page_did_request_storage_item(Web::StorageAPI::StorageEndpointType::LocalStorage, m_storage_key.to_string(), key);
}
WebView::StorageOperationError LocalStorageBottle::set(String const& key, String const& value)
{
return m_page->client().page_did_set_storage_item(Web::StorageAPI::StorageEndpointType::LocalStorage, m_storage_key.to_string(), key, value);
}
void LocalStorageBottle::clear()
{
m_page->client().page_did_clear_storage(Web::StorageAPI::StorageEndpointType::LocalStorage, m_storage_key.to_string());
}
void LocalStorageBottle::remove(String const& key)
{
m_page->client().page_did_remove_storage_item(Web::StorageAPI::StorageEndpointType::LocalStorage, m_storage_key.to_string(), key);
}
size_t SessionStorageBottle::size() const
{
return m_map.size();
}
Vector<String> SessionStorageBottle::keys() const
{
return m_map.keys();
}
Optional<String> SessionStorageBottle::get(String const& key) const
{
if (auto value = m_map.get(key); value.has_value())
return value.value();
return OptionalNone {};
}
WebView::StorageOperationError SessionStorageBottle::set(String const& key, String const& value)
{
if (m_quota.has_value()) {
size_t current_size = 0;
for (auto const& [existing_key, existing_value] : m_map) {
if (existing_key != key) {
current_size += existing_key.bytes().size();
current_size += existing_value.bytes().size();
}
}
size_t new_size = key.bytes().size() + value.bytes().size();
if (current_size + new_size > m_quota.value())
return WebView::StorageOperationError::QuotaExceededError;
}
m_map.set(key, value);
return WebView::StorageOperationError::None;
}
void SessionStorageBottle::clear()
{
m_map.clear();
}
void SessionStorageBottle::remove(String const& key)
{
m_map.remove(key);
}
}

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2024-2025, Shannon Booth <shannon@serenityos.org>
* Copyright (c) 2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -8,36 +9,105 @@
#include <AK/HashMap.h>
#include <AK/String.h>
#include <LibGC/Ptr.h>
#include <LibWeb/Forward.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/StorageAPI/StorageEndpoint.h>
#include <LibWeb/StorageAPI/StorageKey.h>
#include <LibWeb/StorageAPI/StorageType.h>
#include <LibWebView/StorageOperationError.h>
namespace Web::StorageAPI {
// https://storage.spec.whatwg.org/#storage-bottle
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); }
public:
static GC::Ref<StorageBottle> create(GC::Heap& heap, GC::Ref<Page> page, StorageType type, StorageKey key, Optional<u64> quota);
// A storage bottle has a map, which is initially an empty map
OrderedHashMap<String, String> map;
virtual ~StorageBottle() = default;
// A storage bottle also has a proxy map reference set, which is initially an empty set
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.
Optional<u64> quota;
virtual size_t size() const = 0;
virtual Vector<String> keys() const = 0;
virtual Optional<String> get(String const&) const = 0;
virtual WebView::StorageOperationError set(String const& key, String const& value) = 0;
virtual void clear() = 0;
virtual void remove(String const&) = 0;
private:
explicit StorageBottle(Optional<u64> quota_)
: quota(quota_)
Optional<u64> quota() const { return m_quota; }
protected:
explicit StorageBottle(Optional<u64> quota)
: m_quota(quota)
{
}
Optional<u64> m_quota;
};
using BottleMap = OrderedHashMap<String, GC::Ref<StorageBottle>>;
class LocalStorageBottle final : public StorageBottle {
GC_CELL(LocalStorageBottle, StorageBottle);
GC_DECLARE_ALLOCATOR(LocalStorageBottle);
public:
static GC::Ref<LocalStorageBottle> create(GC::Heap& heap, GC::Ref<Page> page, StorageKey key, Optional<u64> quota)
{
return heap.allocate<LocalStorageBottle>(page, key, quota);
}
virtual size_t size() const override;
virtual Vector<String> keys() const override;
virtual Optional<String> get(String const&) const override;
virtual WebView::StorageOperationError set(String const& key, String const& value) override;
virtual void clear() override;
virtual void remove(String const&) override;
virtual void visit_edges(GC::Cell::Visitor& visitor) override;
private:
explicit LocalStorageBottle(GC::Ref<Page> page, StorageKey key, Optional<u64> quota)
: StorageBottle(quota)
, m_page(move(page))
, m_storage_key(move(key))
{
}
GC::Ref<Page> m_page;
StorageKey m_storage_key;
};
class SessionStorageBottle final : public StorageBottle {
GC_CELL(SessionStorageBottle, StorageBottle);
GC_DECLARE_ALLOCATOR(SessionStorageBottle);
public:
static GC::Ref<SessionStorageBottle> create(GC::Heap& heap, Optional<u64> quota)
{
return heap.allocate<SessionStorageBottle>(quota);
}
virtual size_t size() const override;
virtual Vector<String> keys() const override;
virtual Optional<String> get(String const&) const override;
virtual WebView::StorageOperationError set(String const& key, String const& value) override;
virtual void clear() override;
virtual void remove(String const&) override;
private:
explicit SessionStorageBottle(Optional<u64> quota)
: StorageBottle(quota)
{
}
// A storage bottle has a map, which is initially an empty map
OrderedHashMap<String, String> m_map;
};
using BottleMap = Array<GC::Ptr<StorageBottle>, to_underlying(StorageEndpointType::Count)>;
// https://storage.spec.whatwg.org/#storage-bucket
// A storage bucket is a place for storage endpoints to store data.
@ -46,7 +116,7 @@ class StorageBucket : public GC::Cell {
GC_DECLARE_ALLOCATOR(StorageBucket);
public:
static GC::Ref<StorageBucket> create(GC::Heap& heap, StorageType type) { return heap.allocate<StorageBucket>(type); }
static GC::Ref<StorageBucket> create(GC::Heap& heap, GC::Ref<Page> page, StorageKey key, StorageType type) { return heap.allocate<StorageBucket>(page, key, type); }
BottleMap& bottle_map() { return m_bottle_map; }
BottleMap const& bottle_map() const { return m_bottle_map; }
@ -54,14 +124,13 @@ public:
virtual void visit_edges(GC::Cell::Visitor& visitor) override;
private:
explicit StorageBucket(StorageType);
explicit StorageBucket(GC::Ref<Page> page, StorageKey key, StorageType type);
// A storage bucket has a bottle map of storage identifiers to storage bottles.
BottleMap m_bottle_map;
};
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);
GC::Ptr<StorageBottle> obtain_a_session_storage_bottle_map(HTML::EnvironmentSettingsObject&, StorageEndpointType endpoint_type);
GC::Ptr<StorageBottle> obtain_a_storage_bottle_map(StorageType, HTML::EnvironmentSettingsObject&, StorageEndpointType endpoint_type);
}

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2024-2025, Shannon Booth <shannon@serenityos.org>
* Copyright (c) 2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -12,11 +13,11 @@ ReadonlySpan<StorageEndpoint> StorageEndpoint::registered_endpoints()
{
// https://storage.spec.whatwg.org/#registered-storage-endpoints
static auto const endpoints = to_array<StorageEndpoint>({
{ "caches"_string, StorageType::Local, {} },
{ "indexedDB"_string, StorageType::Local, {} },
{ "localStorage"_string, StorageType::Local, 5 * MiB },
{ "serviceWorkerRegistrations"_string, StorageType::Local, {} },
{ "sessionStorage"_string, StorageType::Session, 5 * MiB },
{ StorageEndpointType::Caches, StorageType::Local, {} },
{ StorageEndpointType::IndexedDB, StorageType::Local, {} },
{ StorageEndpointType::LocalStorage, StorageType::Local, LOCAL_STORAGE_QUOTA },
{ StorageEndpointType::ServiceWorkerRegistrations, StorageType::Local, {} },
{ StorageEndpointType::SessionStorage, StorageType::Session, SESSION_STORAGE_QUOTA },
});
return endpoints;
}

View file

@ -1,12 +1,12 @@
/*
* Copyright (c) 2024-2025, Shannon Booth <shannon@serenityos.org>
* Copyright (c) 2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Array.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/Types.h>
@ -14,14 +14,27 @@
namespace Web::StorageAPI {
enum class StorageEndpointType : u8 {
Caches = 0,
IndexedDB = 1,
LocalStorage = 2,
ServiceWorkerRegistrations = 3,
SessionStorage = 4,
Count = 5 // This should always be the last value in the enum.
};
// https://storage.spec.whatwg.org/#storage-endpoint
//
// A storage endpoint is a local or session storage API that uses the infrastructure defined by this
// standard, most notably storage bottles, to keep track of its storage needs.
struct StorageEndpoint {
static constexpr u64 LOCAL_STORAGE_QUOTA = 5 * MiB;
static constexpr u64 SESSION_STORAGE_QUOTA = 5 * MiB;
// https://storage.spec.whatwg.org/#storage-endpoint-identifier
// A storage endpoint has an identifier, which is a storage identifier.
String identifier;
StorageEndpointType identifier;
// https://storage.spec.whatwg.org/#storage-endpoint-types
// A storage endpoint also has types, which is a set of storage types.

View file

@ -20,6 +20,11 @@ struct StorageKey {
// NOTE: This is expected to change; see Client-Side Storage Partitioning https://privacycg.github.io/storage-partitioning/.
URL::Origin origin;
String to_string() const
{
return origin.serialize();
}
friend bool operator==(StorageKey const& a, StorageKey const& b)
{
// To determine whether a storage key A equals storage key B, run these steps:

View file

@ -6,6 +6,7 @@
#include <LibGC/Heap.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/StorageAPI/StorageShed.h>
namespace Web::StorageAPI {
@ -19,29 +20,22 @@ void StorageShed::visit_edges(GC::Cell::Visitor& visitor)
}
// https://storage.spec.whatwg.org/#obtain-a-storage-shelf
GC::Ptr<StorageShelf> StorageShed::obtain_a_storage_shelf(HTML::EnvironmentSettingsObject const& environment, StorageType type)
GC::Ptr<StorageShelf> StorageShed::obtain_a_storage_shelf(HTML::EnvironmentSettingsObject& environment, StorageType type)
{
// 1. Let key be the result of running obtain a storage key with environment.
auto key = obtain_a_storage_key(environment);
auto& page = as<HTML::Window>(environment.global_object()).page();
// 2. If key is failure, then return failure.
if (!key.has_value())
return {};
// 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, &heap = this->heap()] {
return StorageShelf::create(heap, type);
return m_data.ensure(key.value(), [&page, key, type, &heap = this->heap()] {
return StorageShelf::create(heap, page, *key, type);
});
}
// https://storage.spec.whatwg.org/#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 GC::Root<StorageShed> storage_shed = GC::make_root(StorageShed::create(heap));
return *storage_shed;
}
}

View file

@ -24,7 +24,7 @@ class StorageShed : public GC::Cell {
public:
static GC::Ref<StorageShed> create(GC::Heap& heap) { return heap.allocate<StorageShed>(); }
GC::Ptr<StorageShelf> obtain_a_storage_shelf(HTML::EnvironmentSettingsObject const&, StorageType);
GC::Ptr<StorageShelf> obtain_a_storage_shelf(HTML::EnvironmentSettingsObject&, StorageType);
virtual void visit_edges(GC::Cell::Visitor& visitor) override;
@ -34,6 +34,4 @@ private:
OrderedHashMap<StorageKey, GC::Ref<StorageShelf>> m_data;
};
GC::Ref<StorageShed> user_agent_storage_shed(GC::Heap&);
}

View file

@ -18,11 +18,11 @@ void StorageShelf::visit_edges(GC::Cell::Visitor& visitor)
}
// https://storage.spec.whatwg.org/#create-a-storage-shelf
StorageShelf::StorageShelf(StorageType type)
StorageShelf::StorageShelf(GC::Ref<Page> page, StorageKey key, StorageType type)
{
// 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.
m_bucket_map.set("default"_string, StorageBucket::create(heap(), type));
m_bucket_map.set("default"_string, StorageBucket::create(heap(), page, key, type));
// 3. Return shelf.
}

View file

@ -23,7 +23,7 @@ class StorageShelf : public GC::Cell {
GC_DECLARE_ALLOCATOR(StorageShelf);
public:
static GC::Ref<StorageShelf> create(GC::Heap& heap, StorageType type) { return heap.allocate<StorageShelf>(type); }
static GC::Ref<StorageShelf> create(GC::Heap& heap, GC::Ref<Page> page, StorageKey key, StorageType type) { return heap.allocate<StorageShelf>(page, key, type); }
BucketMap& bucket_map() { return m_bucket_map; }
BucketMap const& bucket_map() const { return m_bucket_map; }
@ -31,7 +31,7 @@ public:
virtual void visit_edges(GC::Cell::Visitor& visitor) override;
private:
explicit StorageShelf(StorageType);
explicit StorageShelf(GC::Ref<Page>, StorageKey, StorageType);
BucketMap m_bucket_map;
};