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
*/
@ -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);
}