/* * 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 { JS_DEFINE_ALLOCATOR(IDBFactory); IDBFactory::IDBFactory(JS::Realm& realm) : Bindings::PlatformObject(realm) { } IDBFactory::~IDBFactory() = default; void IDBFactory::initialize(JS::Realm& realm) { Base::initialize(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; } }