mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-24 18:28:57 +00:00
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:
parent
f53559cb55
commit
84b9224121
Notes:
github-actions[bot]
2025-06-12 15:05:54 +00:00
Author: https://github.com/kalenikaliaksandr
Commit: 84b9224121
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5052
Reviewed-by: https://github.com/shannonbooth ✅
Reviewed-by: https://github.com/trflynn89
24 changed files with 694 additions and 118 deletions
96
Libraries/LibWebView/StorageJar.h
Normal file
96
Libraries/LibWebView/StorageJar.h
Normal file
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
* Copyright (c) 2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/Traits.h>
|
||||
#include <LibWeb/StorageAPI/StorageEndpoint.h>
|
||||
#include <LibWebView/Database.h>
|
||||
#include <LibWebView/Forward.h>
|
||||
#include <LibWebView/StorageOperationError.h>
|
||||
|
||||
namespace WebView {
|
||||
|
||||
using StorageEndpointType = Web::StorageAPI::StorageEndpointType;
|
||||
|
||||
struct StorageLocation {
|
||||
bool operator==(StorageLocation const&) const = default;
|
||||
|
||||
StorageEndpointType storage_endpoint;
|
||||
String storage_key;
|
||||
String bottle_key;
|
||||
};
|
||||
|
||||
class StorageJar {
|
||||
AK_MAKE_NONCOPYABLE(StorageJar);
|
||||
AK_MAKE_NONMOVABLE(StorageJar);
|
||||
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<StorageJar>> create(Database&);
|
||||
static NonnullOwnPtr<StorageJar> create();
|
||||
|
||||
~StorageJar();
|
||||
|
||||
Optional<String> get_item(StorageEndpointType storage_endpoint, String const& storage_key, String const& bottle_key);
|
||||
StorageOperationError set_item(StorageEndpointType storage_endpoint, String const& storage_key, String const& bottle_key, String const& bottle_value);
|
||||
void remove_item(StorageEndpointType storage_endpoint, String const& storage_key, String const& key);
|
||||
void clear_storage_key(StorageEndpointType storage_endpoint, String const& storage_key);
|
||||
Vector<String> get_all_keys(StorageEndpointType storage_endpoint, String const& storage_key);
|
||||
|
||||
private:
|
||||
struct Statements {
|
||||
Database::StatementID set_item { 0 };
|
||||
Database::StatementID delete_item { 0 };
|
||||
Database::StatementID get_item { 0 };
|
||||
Database::StatementID clear { 0 };
|
||||
Database::StatementID get_keys { 0 };
|
||||
Database::StatementID calculate_size_excluding_key { 0 };
|
||||
};
|
||||
|
||||
class TransientStorage {
|
||||
public:
|
||||
StorageOperationError set_item(StorageLocation const& key, String const& value);
|
||||
Optional<String> get_item(StorageLocation const& key);
|
||||
void delete_item(StorageLocation const& key);
|
||||
void clear(StorageEndpointType storage_endpoint, String const& storage_key);
|
||||
Vector<String> get_keys(StorageEndpointType storage_endpoint, String const& storage_key);
|
||||
|
||||
private:
|
||||
HashMap<StorageLocation, String> m_storage_items;
|
||||
};
|
||||
|
||||
struct PersistedStorage {
|
||||
StorageOperationError set_item(StorageLocation const& key, String const& value);
|
||||
Optional<String> get_item(StorageLocation const& key);
|
||||
void delete_item(StorageLocation const& key);
|
||||
void clear(StorageEndpointType storage_endpoint, String const& storage_key);
|
||||
Vector<String> get_keys(StorageEndpointType storage_endpoint, String const& storage_key);
|
||||
|
||||
Database& database;
|
||||
Statements statements;
|
||||
};
|
||||
|
||||
explicit StorageJar(Optional<PersistedStorage>);
|
||||
|
||||
Optional<PersistedStorage> m_persisted_storage;
|
||||
TransientStorage m_transient_storage;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<>
|
||||
struct AK::Traits<WebView::StorageLocation> : public AK::DefaultTraits<WebView::StorageLocation> {
|
||||
static unsigned hash(WebView::StorageLocation const& key)
|
||||
{
|
||||
unsigned hash = 0;
|
||||
hash = pair_int_hash(hash, to_underlying(key.storage_endpoint));
|
||||
hash = pair_int_hash(hash, key.storage_key.hash());
|
||||
hash = pair_int_hash(hash, key.bottle_key.hash());
|
||||
return hash;
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue