mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-20 03:25:13 +00:00
LibWeb/IDB: Implement IDBFactory::databases()
This commit is contained in:
parent
5559c3cb4f
commit
664c57af16
Notes:
github-actions[bot]
2025-03-14 07:55:38 +00:00
Author: https://github.com/stelar7 Commit: https://github.com/LadybirdBrowser/ladybird/commit/664c57af163 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3923 Reviewed-by: https://github.com/gmta ✅ Reviewed-by: https://github.com/shannonbooth
5 changed files with 75 additions and 5 deletions
|
@ -1,10 +1,13 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
|
||||
* Copyright (c) 2024, stelar7 <dudedbz@gmail.com>
|
||||
* Copyright (c) 2024-2025, stelar7 <dudedbz@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Vector.h>
|
||||
#include <LibJS/Runtime/Array.h>
|
||||
#include <LibJS/Runtime/Value.h>
|
||||
#include <LibWeb/Bindings/IDBFactoryPrototype.h>
|
||||
#include <LibWeb/Bindings/Intrinsics.h>
|
||||
#include <LibWeb/DOM/Event.h>
|
||||
|
@ -16,6 +19,7 @@
|
|||
#include <LibWeb/IndexedDB/Internal/Key.h>
|
||||
#include <LibWeb/Platform/EventLoopPlugin.h>
|
||||
#include <LibWeb/StorageAPI/StorageKey.h>
|
||||
#include <LibWeb/WebIDL/Promise.h>
|
||||
|
||||
namespace Web::IndexedDB {
|
||||
|
||||
|
@ -173,4 +177,57 @@ WebIDL::ExceptionOr<GC::Ref<IDBOpenDBRequest>> IDBFactory::delete_database(Strin
|
|||
return request;
|
||||
}
|
||||
|
||||
// https://w3c.github.io/IndexedDB/#dom-idbfactory-databases
|
||||
GC::Ref<WebIDL::Promise> IDBFactory::databases()
|
||||
{
|
||||
auto& realm = this->realm();
|
||||
|
||||
// 1. Let environment be this's relevant settings object.
|
||||
auto& environment = HTML::relevant_settings_object(*this);
|
||||
|
||||
// 2. Let storageKey be the result of running obtain a storage key given environment.
|
||||
// If failure is returned, then return a promise rejected with a "SecurityError" DOMException
|
||||
auto maybe_storage_key = StorageAPI::obtain_a_storage_key(environment);
|
||||
if (!maybe_storage_key.has_value())
|
||||
return WebIDL::create_rejected_promise_from_exception(realm, WebIDL::SecurityError::create(realm, "Failed to obtain a storage key"_string));
|
||||
|
||||
auto storage_key = maybe_storage_key.release_value();
|
||||
|
||||
// 3. Let p be a new promise.
|
||||
auto p = WebIDL::create_promise(realm);
|
||||
|
||||
// 4. Run these steps in parallel:
|
||||
Platform::EventLoopPlugin::the().deferred_invoke(GC::create_function(realm.heap(), [&realm, storage_key, p]() {
|
||||
HTML::TemporaryExecutionContext context(realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes);
|
||||
|
||||
// 1. Let databases be the set of databases in storageKey.
|
||||
// If this cannot be determined for any reason, then reject p with an appropriate error (e.g. an "UnknownError" DOMException) and terminate these steps.
|
||||
auto databases = Database::for_key(storage_key);
|
||||
|
||||
// 2. Let result be a new list.
|
||||
auto result = MUST(JS::Array::create(realm, databases.size()));
|
||||
|
||||
// 3. For each db of databases:
|
||||
for (u32 i = 0; i < databases.size(); ++i) {
|
||||
auto& db = databases[i];
|
||||
|
||||
// 1. Let info be a new IDBDatabaseInfo dictionary.
|
||||
// 2. Set info’s name dictionary member to db’s name.
|
||||
// 3. Set info’s version dictionary member to db’s version.
|
||||
auto info = JS::Object::create(realm, realm.intrinsics().object_prototype());
|
||||
MUST(info->create_data_property("name", JS::PrimitiveString::create(realm.vm(), db->name())));
|
||||
MUST(info->create_data_property("version", JS::Value(db->version())));
|
||||
|
||||
// 4. Append info to result.
|
||||
MUST(result->create_data_property_or_throw(i, info));
|
||||
}
|
||||
|
||||
// 4. Resolve p with result.
|
||||
WebIDL::resolve_promise(realm, p, result);
|
||||
}));
|
||||
|
||||
// 5. Return p.
|
||||
return p;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
|
||||
* Copyright (c) 2024, stelar7 <dudedbz@gmail.com>
|
||||
* Copyright (c) 2024-2025, stelar7 <dudedbz@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibJS/Runtime/PromiseCapability.h>
|
||||
#include <LibWeb/Bindings/PlatformObject.h>
|
||||
#include <LibWeb/IndexedDB/IDBOpenDBRequest.h>
|
||||
|
||||
|
@ -22,6 +23,7 @@ public:
|
|||
|
||||
WebIDL::ExceptionOr<GC::Ref<IDBOpenDBRequest>> open(String const& name, Optional<u64> version);
|
||||
WebIDL::ExceptionOr<GC::Ref<IDBOpenDBRequest>> delete_database(String const& name);
|
||||
GC::Ref<WebIDL::Promise> databases();
|
||||
|
||||
WebIDL::ExceptionOr<i8> cmp(JS::Value first, JS::Value second);
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ interface IDBFactory {
|
|||
[NewObject] IDBOpenDBRequest open(DOMString name, optional [EnforceRange] unsigned long long version);
|
||||
[NewObject] IDBOpenDBRequest deleteDatabase(DOMString name);
|
||||
|
||||
[FIXME] Promise<sequence<IDBDatabaseInfo>> databases();
|
||||
Promise<sequence<IDBDatabaseInfo>> databases();
|
||||
|
||||
short cmp(any first, any second);
|
||||
};
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2024, stelar7 <dudedbz@gmail.com>
|
||||
* Copyright (c) 2024-2025, stelar7 <dudedbz@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -28,6 +28,16 @@ void Database::visit_edges(Visitor& visitor)
|
|||
visitor.visit(m_upgrade_transaction);
|
||||
}
|
||||
|
||||
Vector<GC::Root<Database>> Database::for_key(StorageAPI::StorageKey const& key)
|
||||
{
|
||||
Vector<GC::Root<Database>> databases;
|
||||
for (auto const& database_mapping : m_databases.get(key).value_or({})) {
|
||||
databases.append(database_mapping.value);
|
||||
}
|
||||
|
||||
return databases;
|
||||
}
|
||||
|
||||
RequestList& ConnectionQueueHandler::for_key_and_name(StorageAPI::StorageKey& key, String& name)
|
||||
{
|
||||
return ConnectionQueueHandler::the().m_open_requests.ensure(key, [] {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2024, stelar7 <dudedbz@gmail.com>
|
||||
* Copyright (c) 2024-2025, stelar7 <dudedbz@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -39,6 +39,7 @@ public:
|
|||
return connections;
|
||||
}
|
||||
|
||||
[[nodiscard]] static Vector<GC::Root<Database>> for_key(StorageAPI::StorageKey const&);
|
||||
[[nodiscard]] static Optional<GC::Root<Database> const&> for_key_and_name(StorageAPI::StorageKey&, String&);
|
||||
[[nodiscard]] static ErrorOr<GC::Root<Database>> create_for_key_and_name(JS::Realm&, StorageAPI::StorageKey&, String&);
|
||||
[[nodiscard]] static ErrorOr<void> delete_for_key_and_name(StorageAPI::StorageKey&, String&);
|
||||
|
|
Loading…
Add table
Reference in a new issue