LibWeb: Implement most of IDBFactory::open

This commit is contained in:
stelar7 2024-11-04 18:19:19 +01:00 committed by Andrew Kaster
parent 47d6499f6d
commit 64eea90f29
Notes: github-actions[bot] 2024-11-08 18:12:20 +00:00
3 changed files with 46 additions and 2 deletions

View file

@ -1,12 +1,18 @@
/*
* Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
* Copyright (c) 2024, stelar7 <dudedbz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/IDBFactoryPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
#include <LibWeb/IndexedDB/IDBFactory.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
#include <LibWeb/StorageAPI/StorageKey.h>
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<JS::NonnullGCPtr<IDBOpenDBRequest>> IDBFactory::open(String const& name, Optional<u64> 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;
}
}

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
* Copyright (c) 2024, stelar7 <dudedbz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -7,6 +8,7 @@
#pragma once
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/IndexedDB/IDBOpenDBRequest.h>
namespace Web::IndexedDB {
@ -18,6 +20,8 @@ class IDBFactory : public Bindings::PlatformObject {
public:
virtual ~IDBFactory() override;
WebIDL::ExceptionOr<JS::NonnullGCPtr<IDBOpenDBRequest>> open(String const& name, Optional<u64> version);
protected:
explicit IDBFactory(JS::Realm&);

View file

@ -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<sequence<IDBDatabaseInfo>> databases();