mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-03 14:50:18 +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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue