diff --git a/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp b/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp index c05d3b06129..168f39218bb 100644 --- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp +++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp @@ -474,4 +474,36 @@ WebIDL::ExceptionOr> IDBObjectStore::delete_(JS::Value query return result; } +// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-clear +WebIDL::ExceptionOr> IDBObjectStore::clear() +{ + auto& realm = this->realm(); + + // 1. Let transaction be this’s transaction. + auto transaction = this->transaction(); + + // 2. Let store be this’s object store. + auto store = this->store(); + + // FIXME: 3. If store has been deleted, throw an "InvalidStateError" DOMException. + + // 4. If transaction’s state is not active, then throw a "TransactionInactiveError" DOMException. + if (transaction->state() != IDBTransaction::TransactionState::Active) + return WebIDL::TransactionInactiveError::create(realm, "Transaction is not active while clearing object store"_string); + + // 5. If transaction is a read-only transaction, throw a "ReadOnlyError" DOMException. + if (transaction->is_readonly()) + return WebIDL::ReadOnlyError::create(realm, "Transaction is read-only while clearing object store"_string); + + // 6. Let operation be an algorithm to run clear an object store with store. + auto operation = GC::Function()>::create(realm.heap(), [store] -> WebIDL::ExceptionOr { + return clear_an_object_store(store); + }); + + // 7. Return the result (an IDBRequest) of running asynchronously execute a request with this and operation. + auto result = asynchronously_execute_a_request(realm, GC::Ref(*this), operation); + dbgln_if(IDB_DEBUG, "Executing request for clear with uuid {}", result->uuid()); + return result; +} + } diff --git a/Libraries/LibWeb/IndexedDB/IDBObjectStore.h b/Libraries/LibWeb/IndexedDB/IDBObjectStore.h index 53998193297..140984cc551 100644 --- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.h +++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.h @@ -53,6 +53,7 @@ public: [[nodiscard]] WebIDL::ExceptionOr> get(JS::Value); [[nodiscard]] WebIDL::ExceptionOr> open_cursor(JS::Value, Bindings::IDBCursorDirection = Bindings::IDBCursorDirection::Next); [[nodiscard]] WebIDL::ExceptionOr> delete_(JS::Value); + [[nodiscard]] WebIDL::ExceptionOr> clear(); protected: explicit IDBObjectStore(JS::Realm&, GC::Ref, GC::Ref); diff --git a/Libraries/LibWeb/IndexedDB/IDBObjectStore.idl b/Libraries/LibWeb/IndexedDB/IDBObjectStore.idl index 92c916a1664..ce85a2772bf 100644 --- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.idl +++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.idl @@ -13,7 +13,7 @@ interface IDBObjectStore { [NewObject] IDBRequest put(any value, optional any key); [NewObject] IDBRequest add(any value, optional any key); [NewObject] IDBRequest delete(any query); - [FIXME, NewObject] IDBRequest clear(); + [NewObject] IDBRequest clear(); [NewObject] IDBRequest get(any query); [FIXME, NewObject] IDBRequest getKey(any query); [FIXME, NewObject] IDBRequest getAll(optional any query, optional [EnforceRange] unsigned long count);