LibWeb/HTML: Store the type of the storage object

This will be needed by the storage broadcast implementation to perform
different steps if the object is for session storage.
This commit is contained in:
Shannon Booth 2024-12-23 22:32:49 +13:00 committed by Andreas Kling
parent f3ecd23a21
commit bbf4739c8e
Notes: github-actions[bot] 2025-01-02 10:39:40 +00:00
3 changed files with 18 additions and 7 deletions
Libraries/LibWeb/HTML

View file

@ -15,13 +15,14 @@ namespace Web::HTML {
GC_DEFINE_ALLOCATOR(Storage);
GC::Ref<Storage> Storage::create(JS::Realm& realm, u64 quota_bytes)
GC::Ref<Storage> Storage::create(JS::Realm& realm, Type type, u64 quota_bytes)
{
return realm.create<Storage>(realm, quota_bytes);
return realm.create<Storage>(realm, type, quota_bytes);
}
Storage::Storage(JS::Realm& realm, u64 quota_bytes)
Storage::Storage(JS::Realm& realm, Type type, u64 quota_bytes)
: Bindings::PlatformObject(realm)
, m_type(type)
, m_quota_bytes(quota_bytes)
{
m_legacy_platform_object_flags = LegacyPlatformObjectFlags {

View file

@ -13,12 +13,20 @@
namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/webstorage.html#storage-2
class Storage : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(Storage, Bindings::PlatformObject);
GC_DECLARE_ALLOCATOR(Storage);
public:
[[nodiscard]] static GC::Ref<Storage> create(JS::Realm&, u64 quota_bytes);
// https://html.spec.whatwg.org/multipage/webstorage.html#concept-storage-type
enum class Type {
Local,
Session,
};
[[nodiscard]] static GC::Ref<Storage> create(JS::Realm&, Type, u64 quota_bytes);
~Storage();
size_t length() const;
@ -29,11 +37,12 @@ public:
void clear();
auto const& map() const { return m_map; }
Type type() const { return m_type; }
void dump() const;
private:
explicit Storage(JS::Realm&, u64 quota_limit);
Storage(JS::Realm&, Type, u64 quota_limit);
virtual void initialize(JS::Realm&) override;
@ -49,6 +58,7 @@ private:
void broadcast(StringView key, StringView old_value, StringView new_value);
OrderedHashMap<String, String> m_map;
Type m_type {};
u64 m_quota_bytes { 0 };
u64 m_stored_bytes { 0 };
};

View file

@ -453,7 +453,7 @@ WebIDL::ExceptionOr<GC::Ref<Storage>> Window::local_storage()
// FIXME: Implement according to spec.
static HashMap<URL::Origin, GC::Root<Storage>> local_storage_per_origin;
auto storage = local_storage_per_origin.ensure(associated_document().origin(), [this]() -> GC::Root<Storage> {
return Storage::create(realm(), quota_bytes);
return Storage::create(realm(), Storage::Type::Local, quota_bytes);
});
return GC::Ref { *storage };
}
@ -467,7 +467,7 @@ WebIDL::ExceptionOr<GC::Ref<Storage>> Window::session_storage()
// FIXME: Implement according to spec.
static HashMap<URL::Origin, GC::Root<Storage>> session_storage_per_origin;
auto storage = session_storage_per_origin.ensure(associated_document().origin(), [this]() -> GC::Root<Storage> {
return Storage::create(realm(), quota_bytes);
return Storage::create(realm(), Storage::Type::Session, quota_bytes);
});
return GC::Ref { *storage };
}