From 64eea90f298d7de5eeedb18ae8b27606be1c5544 Mon Sep 17 00:00:00 2001 From: stelar7 Date: Mon, 4 Nov 2024 18:19:19 +0100 Subject: [PATCH] LibWeb: Implement most of IDBFactory::open --- .../Libraries/LibWeb/IndexedDB/IDBFactory.cpp | 41 +++++++++++++++++++ .../Libraries/LibWeb/IndexedDB/IDBFactory.h | 4 ++ .../Libraries/LibWeb/IndexedDB/IDBFactory.idl | 3 +- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibWeb/IndexedDB/IDBFactory.cpp b/Userland/Libraries/LibWeb/IndexedDB/IDBFactory.cpp index 72ed33ec6cc..99516c37797 100644 --- a/Userland/Libraries/LibWeb/IndexedDB/IDBFactory.cpp +++ b/Userland/Libraries/LibWeb/IndexedDB/IDBFactory.cpp @@ -1,12 +1,18 @@ /* * Copyright (c) 2024, Shannon Booth + * Copyright (c) 2024, stelar7 * * SPDX-License-Identifier: BSD-2-Clause */ #include #include +#include +#include +#include #include +#include +#include namespace Web::IndexedDB { @@ -25,4 +31,39 @@ void IDBFactory::initialize(JS::Realm& realm) WEB_SET_PROTOTYPE_FOR_INTERFACE(IDBFactory); } +// https://w3c.github.io/IndexedDB/#dom-idbfactory-open +WebIDL::ExceptionOr> IDBFactory::open(String const& name, Optional version) +{ + auto& realm = this->realm(); + + // 1. If version is 0 (zero), throw a TypeError. + if (version.has_value() && version.value() == 0) + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "The version provided must not be 0"_string }; + + // 2. Let environment be this's relevant settings object. + auto& environment = HTML::relevant_settings_object(*this); + + // 3. Let storageKey be the result of running obtain a storage key given environment. + // If failure is returned, then throw a "SecurityError" DOMException and abort these steps. + auto storage_key = StorageAPI::obtain_a_storage_key(environment); + if (!storage_key.has_value()) + return WebIDL::SecurityError::create(realm, "Failed to obtain a storage key"_string); + + // 4. Let request be a new open request. + auto request = IDBOpenDBRequest::create(realm); + + // 5. Run these steps in parallel: + Platform::EventLoopPlugin::the().deferred_invoke(JS::create_heap_function(realm.heap(), [&realm, storage_key, name, version, request] { + HTML::TemporaryExecutionContext context(realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes); + + (void)storage_key; + (void)name; + (void)version; + (void)request; + })); + + // 6. Return a new IDBOpenDBRequest object for request. + return request; +} + } diff --git a/Userland/Libraries/LibWeb/IndexedDB/IDBFactory.h b/Userland/Libraries/LibWeb/IndexedDB/IDBFactory.h index b2d87a3eb78..0e4bdc2d91a 100644 --- a/Userland/Libraries/LibWeb/IndexedDB/IDBFactory.h +++ b/Userland/Libraries/LibWeb/IndexedDB/IDBFactory.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2024, Shannon Booth + * Copyright (c) 2024, stelar7 * * SPDX-License-Identifier: BSD-2-Clause */ @@ -7,6 +8,7 @@ #pragma once #include +#include namespace Web::IndexedDB { @@ -18,6 +20,8 @@ class IDBFactory : public Bindings::PlatformObject { public: virtual ~IDBFactory() override; + WebIDL::ExceptionOr> open(String const& name, Optional version); + protected: explicit IDBFactory(JS::Realm&); diff --git a/Userland/Libraries/LibWeb/IndexedDB/IDBFactory.idl b/Userland/Libraries/LibWeb/IndexedDB/IDBFactory.idl index c64cc356a32..e6b457c1457 100644 --- a/Userland/Libraries/LibWeb/IndexedDB/IDBFactory.idl +++ b/Userland/Libraries/LibWeb/IndexedDB/IDBFactory.idl @@ -1,8 +1,7 @@ // https://w3c.github.io/IndexedDB/#idbfactory [Exposed=(Window,Worker)] interface IDBFactory { - [FIXME, NewObject] IDBOpenDBRequest open(DOMString name, - optional [EnforceRange] unsigned long long version); + [NewObject] IDBOpenDBRequest open(DOMString name, optional [EnforceRange] unsigned long long version); [FIXME, NewObject] IDBOpenDBRequest deleteDatabase(DOMString name); [FIXME] Promise> databases();