LibWeb: Stub out a Database connection store

This commit is contained in:
stelar7 2024-11-04 19:01:35 +01:00 committed by Andrew Kaster
parent 60ae5e75f8
commit 914ad7bbd6
Notes: github-actions[bot] 2024-11-08 18:11:51 +00:00
3 changed files with 89 additions and 0 deletions

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2024, stelar7 <dudedbz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/HashMap.h>
#include <LibJS/Heap/Handle.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/IndexedDB/IDBRequest.h>
#include <LibWeb/StorageAPI/StorageKey.h>
namespace Web::IndexedDB {
using ConnectionQueue = AK::Vector<JS::Handle<IDBRequest>>;
using ConnectionMap = HashMap<StorageAPI::StorageKey, HashMap<String, ConnectionQueue>>;
// https://w3c.github.io/IndexedDB/#connection-queues
class ConnectionQueueHandler {
public:
static ConnectionQueue& for_key_and_name(StorageAPI::StorageKey& key, String& name);
static ConnectionQueueHandler& the()
{
static ConnectionQueueHandler s_instance;
return s_instance;
}
private:
ConnectionMap m_open_requests;
};
}

View file

@ -4,10 +4,14 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/IndexedDB/Internal/ConnectionQueueHandler.h>
#include <LibWeb/IndexedDB/Internal/Database.h>
namespace Web::IndexedDB {
using IDBDatabaseMapping = HashMap<StorageAPI::StorageKey, HashMap<String, JS::Handle<Database>>>;
static IDBDatabaseMapping m_databases;
JS_DEFINE_ALLOCATOR(Database);
Database::~Database() = default;
@ -23,5 +27,52 @@ void Database::visit_edges(Visitor& visitor)
visitor.visit(m_associated_connections);
}
ConnectionQueue& ConnectionQueueHandler::for_key_and_name(StorageAPI::StorageKey& key, String& name)
{
return ConnectionQueueHandler::the().m_open_requests.ensure(key, [] {
return HashMap<String, ConnectionQueue>();
})
.ensure(name, [] {
return ConnectionQueue();
});
}
Optional<JS::Handle<Database>> Database::for_key_and_name(StorageAPI::StorageKey& key, String& name)
{
return m_databases.ensure(key, [] {
return HashMap<String, JS::Handle<Database>>();
})
.get(name);
}
ErrorOr<JS::Handle<Database>> Database::create_for_key_and_name(JS::Realm& realm, StorageAPI::StorageKey& key, String& name)
{
auto database_mapping = TRY(m_databases.try_ensure(key, [] {
return HashMap<String, JS::Handle<Database>>();
}));
return database_mapping.try_ensure(name, [&] {
return Database::create(realm, name);
});
}
ErrorOr<void> Database::delete_for_key_and_name(StorageAPI::StorageKey& key, String& name)
{
// FIXME: Is a missing entry a failure?
auto maybe_database_mapping = m_databases.get(key);
if (!maybe_database_mapping.has_value())
return {};
auto& database_mapping = maybe_database_mapping.value();
auto maybe_database = database_mapping.get(name);
if (!maybe_database.has_value())
return {};
auto did_remove = database_mapping.remove(name);
if (!did_remove)
return {};
return {};
}
}

View file

@ -37,6 +37,10 @@ public:
return connections;
}
[[nodiscard]] static Optional<JS::Handle<Database>> for_key_and_name(StorageAPI::StorageKey&, String&);
[[nodiscard]] static ErrorOr<JS::Handle<Database>> create_for_key_and_name(JS::Realm&, StorageAPI::StorageKey&, String&);
[[nodiscard]] static ErrorOr<void> delete_for_key_and_name(StorageAPI::StorageKey&, String&);
[[nodiscard]] static JS::NonnullGCPtr<Database> create(JS::Realm&, String const&);
virtual ~Database();