2024-11-07 18:52:59 +01:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2024, stelar7 <dudedbz@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2025-04-01 18:36:39 +02:00
|
|
|
|
#include <LibJS/Runtime/Array.h>
|
2024-11-07 18:52:59 +01:00
|
|
|
|
#include <LibWeb/Bindings/IDBIndexPrototype.h>
|
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
|
|
|
|
#include <LibWeb/HTML/EventNames.h>
|
|
|
|
|
#include <LibWeb/IndexedDB/IDBIndex.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::IndexedDB {
|
|
|
|
|
|
|
|
|
|
GC_DEFINE_ALLOCATOR(IDBIndex);
|
|
|
|
|
|
|
|
|
|
IDBIndex::~IDBIndex() = default;
|
|
|
|
|
|
2025-04-01 18:36:39 +02:00
|
|
|
|
IDBIndex::IDBIndex(JS::Realm& realm, GC::Ref<Index> index, GC::Ref<IDBObjectStore> object_store)
|
2024-11-07 18:52:59 +01:00
|
|
|
|
: PlatformObject(realm)
|
2025-04-01 18:36:39 +02:00
|
|
|
|
, m_index(index)
|
|
|
|
|
, m_object_store_handle(object_store)
|
|
|
|
|
, m_name(index->name())
|
2024-11-07 18:52:59 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-01 18:36:39 +02:00
|
|
|
|
GC::Ref<IDBIndex> IDBIndex::create(JS::Realm& realm, GC::Ref<Index> index, GC::Ref<IDBObjectStore> object_store)
|
2024-11-07 18:52:59 +01:00
|
|
|
|
{
|
2025-04-01 18:36:39 +02:00
|
|
|
|
return realm.create<IDBIndex>(realm, index, object_store);
|
2024-11-07 18:52:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IDBIndex::initialize(JS::Realm& realm)
|
|
|
|
|
{
|
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(IDBIndex);
|
2025-04-20 16:22:57 +02:00
|
|
|
|
Base::initialize(realm);
|
2024-11-07 18:52:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
2025-04-01 18:36:39 +02:00
|
|
|
|
void IDBIndex::visit_edges(Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
visitor.visit(m_index);
|
|
|
|
|
visitor.visit(m_object_store_handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://w3c.github.io/IndexedDB/#dom-idbindex-name
|
|
|
|
|
WebIDL::ExceptionOr<void> IDBIndex::set_name(String const& value)
|
|
|
|
|
{
|
|
|
|
|
auto& realm = this->realm();
|
|
|
|
|
|
|
|
|
|
// 1. Let name be the given value.
|
|
|
|
|
auto const& name = value;
|
|
|
|
|
|
|
|
|
|
// 2. Let transaction be this’s transaction.
|
|
|
|
|
auto transaction = this->transaction();
|
|
|
|
|
|
|
|
|
|
// 3. Let index be this’s index.
|
|
|
|
|
auto index = this->index();
|
|
|
|
|
|
|
|
|
|
// 4. If transaction is not an upgrade transaction, throw an "InvalidStateError" DOMException.
|
|
|
|
|
if (!transaction->is_upgrade_transaction())
|
|
|
|
|
return WebIDL::InvalidStateError::create(realm, "Transaction is not an upgrade transaction"_string);
|
|
|
|
|
|
|
|
|
|
// 5. If transaction’s state is not active, then throw a "TransactionInactiveError" DOMException.
|
|
|
|
|
if (transaction->state() != IDBTransaction::TransactionState::Active)
|
2025-04-29 17:24:30 +02:00
|
|
|
|
return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while updating index name"_string);
|
2025-04-01 18:36:39 +02:00
|
|
|
|
|
|
|
|
|
// FIXME: 6. If index or index’s object store has been deleted, throw an "InvalidStateError" DOMException.
|
|
|
|
|
|
|
|
|
|
// 7. If index’s name is equal to name, terminate these steps.
|
|
|
|
|
if (index->name() == name)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
// 8. If an index named name already exists in index’s object store, throw a "ConstraintError" DOMException.
|
2025-04-01 18:37:23 +02:00
|
|
|
|
if (index->object_store()->index_set().contains(name))
|
|
|
|
|
return WebIDL::ConstraintError::create(realm, "An index with the given name already exists"_string);
|
2025-04-01 18:36:39 +02:00
|
|
|
|
|
|
|
|
|
// 9. Set index’s name to name.
|
|
|
|
|
index->set_name(name);
|
|
|
|
|
|
2025-04-01 18:37:23 +02:00
|
|
|
|
// NOTE: Update the key in the map so it still matches the name
|
|
|
|
|
auto old_value = m_object_store_handle->index_set().take(m_name).release_value();
|
|
|
|
|
m_object_store_handle->index_set().set(name, old_value);
|
|
|
|
|
|
2025-04-01 18:36:39 +02:00
|
|
|
|
// 10. Set this’s name to name.
|
|
|
|
|
m_name = name;
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://w3c.github.io/IndexedDB/#dom-idbindex-keypath
|
|
|
|
|
JS::Value IDBIndex::key_path() const
|
|
|
|
|
{
|
|
|
|
|
return m_index->key_path().visit(
|
|
|
|
|
[&](String const& value) -> JS::Value {
|
|
|
|
|
return JS::PrimitiveString::create(realm().vm(), value);
|
|
|
|
|
},
|
|
|
|
|
[&](Vector<String> const& value) -> JS::Value {
|
|
|
|
|
return JS::Array::create_from<String>(realm(), value.span(), [&](auto const& entry) -> JS::Value {
|
|
|
|
|
return JS::PrimitiveString::create(realm().vm(), entry);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 18:52:59 +01:00
|
|
|
|
}
|