diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 24ec7c34678..1dcebd6affa 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -501,6 +501,7 @@ set(SOURCES Infra/ByteSequences.cpp Infra/JSON.cpp Infra/Strings.cpp + IndexedDB/Internal/Database.cpp IndexedDB/IDBFactory.cpp IndexedDB/IDBOpenDBRequest.cpp IndexedDB/IDBRequest.cpp diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index 2060fdba116..0e828e5960a 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -566,6 +566,7 @@ class Performance; } namespace Web::IndexedDB { +class Database; class IDBFactory; class IDBOpenDBRequest; class IDBRequest; diff --git a/Userland/Libraries/LibWeb/IndexedDB/Internal/Database.cpp b/Userland/Libraries/LibWeb/IndexedDB/Internal/Database.cpp new file mode 100644 index 00000000000..1e84eab310f --- /dev/null +++ b/Userland/Libraries/LibWeb/IndexedDB/Internal/Database.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2024, stelar7 + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +namespace Web::IndexedDB { + +JS_DEFINE_ALLOCATOR(Database); + +Database::~Database() = default; + +JS::NonnullGCPtr Database::create(JS::Realm& realm, String const& name) +{ + return realm.heap().allocate(realm, realm, name); +} + +void Database::visit_edges(Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(m_associated_connections); +} + + +} diff --git a/Userland/Libraries/LibWeb/IndexedDB/Internal/Database.h b/Userland/Libraries/LibWeb/IndexedDB/Internal/Database.h new file mode 100644 index 00000000000..b06f3f98c29 --- /dev/null +++ b/Userland/Libraries/LibWeb/IndexedDB/Internal/Database.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2024, stelar7 + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace Web::IndexedDB { + +// https://www.w3.org/TR/IndexedDB/#database-construct +class Database : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(Database, Bindings::PlatformObject); + JS_DECLARE_ALLOCATOR(Database); + +public: + void set_version(u64 version) { m_version = version; } + u64 version() const { return m_version; } + String name() const { return m_name; } + + void associate(JS::NonnullGCPtr connection) { m_associated_connections.append(connection); } + ReadonlySpan> associated_connections() { return m_associated_connections; } + Vector> associated_connections_except(IDBDatabase& connection) + { + Vector> connections; + for (auto& associated_connection : m_associated_connections) { + if (associated_connection != &connection) + connections.append(associated_connection); + } + return connections; + } + + [[nodiscard]] static JS::NonnullGCPtr create(JS::Realm&, String const&); + virtual ~Database(); + +protected: + explicit Database(IDBDatabase& database); + + explicit Database(JS::Realm& realm, String name) + : PlatformObject(realm) + , m_name(move(name)) + { + } + + virtual void visit_edges(Visitor&) override; + +private: + Vector> m_associated_connections; + + // FIXME: A database has zero or more object stores which hold the data stored in the database. + + // A database has a name which identifies it within a specific storage key. + String m_name; + + // A database has a version. When a database is first created, its version is 0 (zero). + u64 m_version { 0 }; + + // FIXME: A database has at most one associated upgrade transaction, which is either null or an upgrade transaction, and is initially null. +}; + +}