mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-29 04:09:13 +00:00
LibWeb: Correctly initialize Storage objects on the Document
Instead of storing all storage objects in static memory, we now follow the the spec by lazily creating a unique Storage object on each document object. Each Storage object now holds a 'proxy' to the underlying backing storage. For now, this proxy is simply a reference to the backing object. In the future, it will need to be some type of interface object that stores on a SQLite database or similar. Session storage is now correctly stored / tracked as part of the TraversableNavigable object. Local storage is still stored in a static map, but eventually this should be factored into something that is stored at the user agent level.
This commit is contained in:
parent
c536f65160
commit
2066ed2318
Notes:
github-actions[bot]
2025-01-02 10:39:14 +00:00
Author: https://github.com/shannonbooth
Commit: 2066ed2318
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3070
Reviewed-by: https://github.com/awesomekling
Reviewed-by: https://github.com/konradekk
20 changed files with 503 additions and 44 deletions
88
Libraries/LibWeb/StorageAPI/StorageBottle.cpp
Normal file
88
Libraries/LibWeb/StorageAPI/StorageBottle.cpp
Normal file
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
* Copyright (c) 2024-2025, Shannon Booth <shannon@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/HTML/Scripting/Environments.h>
|
||||
#include <LibWeb/HTML/TraversableNavigable.h>
|
||||
#include <LibWeb/HTML/Window.h>
|
||||
#include <LibWeb/StorageAPI/StorageBottle.h>
|
||||
#include <LibWeb/StorageAPI/StorageEndpoint.h>
|
||||
#include <LibWeb/StorageAPI/StorageShed.h>
|
||||
|
||||
namespace Web::StorageAPI {
|
||||
|
||||
StorageBucket::StorageBucket(StorageType type)
|
||||
{
|
||||
// 1. Let bucket be null.
|
||||
// 2. If type is "local", then set bucket to a new local storage bucket.
|
||||
// 3. Otherwise:
|
||||
// 1. Assert: type is "session".
|
||||
// 2. Set bucket to a new session storage bucket.
|
||||
|
||||
// 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));
|
||||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
// 1. Let shed be null.
|
||||
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();
|
||||
}
|
||||
// 3. Otherwise:
|
||||
else {
|
||||
// 1. Assert: type is "session".
|
||||
VERIFY(type == StorageType::Session);
|
||||
|
||||
// 2. Set shed to environment’s global object’s associated Document’s node navigable’s traversable navigable’s storage shed.
|
||||
shed = &verify_cast<HTML::Window>(environment.global_object()).associated_document().navigable()->traversable_navigable()->storage_shed();
|
||||
}
|
||||
|
||||
// 4. Let shelf be the result of running obtain a storage shelf, with shed, environment, and type.
|
||||
VERIFY(shed);
|
||||
auto shelf = shed->obtain_a_storage_shelf(environment, type);
|
||||
|
||||
// 5. If shelf is failure, then return failure.
|
||||
if (!shelf.has_value())
|
||||
return {};
|
||||
|
||||
// 6. Let bucket be shelf’s bucket map["default"].
|
||||
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();
|
||||
|
||||
// 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.
|
||||
// 10. Return proxyMap.
|
||||
return bottle->proxy();
|
||||
}
|
||||
|
||||
// https://storage.spec.whatwg.org/#obtain-a-session-storage-bottle-map
|
||||
RefPtr<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.
|
||||
return obtain_a_storage_bottle_map(StorageType::Session, environment, identifier);
|
||||
}
|
||||
|
||||
// https://storage.spec.whatwg.org/#obtain-a-local-storage-bottle-map
|
||||
RefPtr<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.
|
||||
return obtain_a_storage_bottle_map(StorageType::Local, environment, identifier);
|
||||
}
|
||||
|
||||
}
|
52
Libraries/LibWeb/StorageAPI/StorageBottle.h
Normal file
52
Libraries/LibWeb/StorageAPI/StorageBottle.h
Normal file
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright (c) 2024-2025, Shannon Booth <shannon@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
#include <LibWeb/StorageAPI/StorageType.h>
|
||||
|
||||
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)); }
|
||||
|
||||
// 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; }
|
||||
|
||||
// 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;
|
||||
|
||||
private:
|
||||
explicit StorageBottle(Optional<u64> quota_)
|
||||
: quota(quota_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
using BottleMap = OrderedHashMap<String, NonnullRefPtr<StorageBottle>>;
|
||||
|
||||
// https://storage.spec.whatwg.org/#storage-bucket
|
||||
// A storage bucket is a place for storage endpoints to store data.
|
||||
struct StorageBucket {
|
||||
explicit StorageBucket(StorageType);
|
||||
|
||||
// A storage bucket has a bottle map of storage identifiers to storage bottles.
|
||||
BottleMap 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);
|
||||
|
||||
}
|
24
Libraries/LibWeb/StorageAPI/StorageEndpoint.cpp
Normal file
24
Libraries/LibWeb/StorageAPI/StorageEndpoint.cpp
Normal file
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
* Copyright (c) 2024-2025, Shannon Booth <shannon@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/StorageAPI/StorageEndpoint.h>
|
||||
|
||||
namespace Web::StorageAPI {
|
||||
|
||||
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 },
|
||||
});
|
||||
return endpoints;
|
||||
}
|
||||
|
||||
}
|
38
Libraries/LibWeb/StorageAPI/StorageEndpoint.h
Normal file
38
Libraries/LibWeb/StorageAPI/StorageEndpoint.h
Normal file
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Copyright (c) 2024-2025, Shannon Booth <shannon@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Array.h>
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/Types.h>
|
||||
#include <LibWeb/StorageAPI/StorageType.h>
|
||||
|
||||
namespace Web::StorageAPI {
|
||||
|
||||
// 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 {
|
||||
// https://storage.spec.whatwg.org/#storage-endpoint-identifier
|
||||
// A storage endpoint has an identifier, which is a storage identifier.
|
||||
String identifier;
|
||||
|
||||
// https://storage.spec.whatwg.org/#storage-endpoint-types
|
||||
// A storage endpoint also has types, which is a set of storage types.
|
||||
// NOTE: We do not implement this as a set as it is not neccessary in the current implementation.
|
||||
StorageType type;
|
||||
|
||||
// https://storage.spec.whatwg.org/#storage-endpoint-quota
|
||||
// A storage endpoint also has a quota, which is null or a number representing a recommended quota (in bytes) for each storage bottle corresponding to this storage endpoint.
|
||||
Optional<u64> quota;
|
||||
|
||||
static ReadonlySpan<StorageEndpoint> registered_endpoints();
|
||||
};
|
||||
|
||||
}
|
38
Libraries/LibWeb/StorageAPI/StorageShed.cpp
Normal file
38
Libraries/LibWeb/StorageAPI/StorageShed.cpp
Normal file
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Copyright (c) 2024-2025, Shannon Booth <shannon@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/HTML/Scripting/Environments.h>
|
||||
#include <LibWeb/StorageAPI/StorageShed.h>
|
||||
|
||||
namespace Web::StorageAPI {
|
||||
|
||||
// https://storage.spec.whatwg.org/#obtain-a-storage-shelf
|
||||
Optional<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);
|
||||
|
||||
// 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] {
|
||||
return StorageShelf { type };
|
||||
});
|
||||
}
|
||||
|
||||
// https://storage.spec.whatwg.org/#user-agent-storage-shed
|
||||
StorageShed& user_agent_storage_shed()
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
|
||||
}
|
29
Libraries/LibWeb/StorageAPI/StorageShed.h
Normal file
29
Libraries/LibWeb/StorageAPI/StorageShed.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copyright (c) 2024-2025, Shannon Booth <shannon@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/HashMap.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
#include <LibWeb/StorageAPI/StorageKey.h>
|
||||
#include <LibWeb/StorageAPI/StorageShelf.h>
|
||||
#include <LibWeb/StorageAPI/StorageType.h>
|
||||
|
||||
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 {
|
||||
public:
|
||||
Optional<StorageShelf&> obtain_a_storage_shelf(HTML::EnvironmentSettingsObject const&, StorageType);
|
||||
|
||||
private:
|
||||
OrderedHashMap<StorageKey, StorageShelf> m_data;
|
||||
};
|
||||
|
||||
StorageShed& user_agent_storage_shed();
|
||||
|
||||
}
|
20
Libraries/LibWeb/StorageAPI/StorageShelf.cpp
Normal file
20
Libraries/LibWeb/StorageAPI/StorageShelf.cpp
Normal file
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* Copyright (c) 2024-2025, Shannon Booth <shannon@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/StorageAPI/StorageShelf.h>
|
||||
|
||||
namespace Web::StorageAPI {
|
||||
|
||||
// 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 });
|
||||
// 3. Return shelf.
|
||||
}
|
||||
|
||||
}
|
27
Libraries/LibWeb/StorageAPI/StorageShelf.h
Normal file
27
Libraries/LibWeb/StorageAPI/StorageShelf.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright (c) 2024-2025, Shannon Booth <shannon@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
#include <LibWeb/StorageAPI/StorageBottle.h>
|
||||
#include <LibWeb/StorageAPI/StorageType.h>
|
||||
|
||||
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>;
|
||||
|
||||
struct StorageShelf {
|
||||
explicit StorageShelf(StorageType);
|
||||
|
||||
BucketMap bucket_map;
|
||||
};
|
||||
|
||||
}
|
18
Libraries/LibWeb/StorageAPI/StorageType.h
Normal file
18
Libraries/LibWeb/StorageAPI/StorageType.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
* Copyright (c) 2024-2025, Shannon Booth <shannon@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace Web::StorageAPI {
|
||||
|
||||
// https://storage.spec.whatwg.org/#storage-type
|
||||
// A storage type is "local" or "session".
|
||||
enum class StorageType {
|
||||
Local,
|
||||
Session,
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue