Skip to content

Truly independent web browser

LibWeb: Return the same IDBIndex from IDBObjectStore.index()

Andreas Kling9665d89262ce
diff --git a/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp b/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp
index 6b4e56d8ab0..517e93912d7 100644
--- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp
+++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp
@@ -41,6 +41,7 @@ void IDBObjectStore::visit_edges(GC::Cell::Visitor& visitor)
visitor.visit(m_transaction);
visitor.visit(m_indexes);
visitor.visit(m_key_path_arrays);
+ visitor.visit(m_index_handles);
}
JS::Object& IDBObjectStore::relevant_global_object() const
@@ -205,7 +206,7 @@ WebIDL::ExceptionOr<GC::Ref<IDBIndex>> IDBObjectStore::create_index(Utf16String
queue_the_creation_of_an_index(*this, index);
// 13. Return a new index handle associated with index and this.
- return IDBIndex::create(index, *this);
+ return m_index_handles.ensure(index, [&] { return IDBIndex::create(index, *this); });
}
// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-index
@@ -231,7 +232,9 @@ WebIDL::ExceptionOr<GC::Ref<IDBIndex>> IDBObjectStore::index(Utf16String const&
return WebIDL::NotFoundError::create("Index not found in object store"_utf16);
// 6. Return an index handle associated with index and this.
- return IDBIndex::create(*index, *this);
+ // NOTE: Each call to this method on the same IDBObjectStore instance with the same name returns the same IDBIndex
+ // instance.
+ return m_index_handles.ensure(*index, [&] { return IDBIndex::create(*index, *this); });
}
// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-deleteindex
diff --git a/Libraries/LibWeb/IndexedDB/IDBObjectStore.h b/Libraries/LibWeb/IndexedDB/IDBObjectStore.h
index b0415838b81..b33ea13fce9 100644
--- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.h
+++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.h
@@ -81,6 +81,10 @@ private:
// NB: The keyPath getter returns the same Array every time it is inspected in the same realm.
HashMap<GC::Ref<JS::Object>, GC::Ref<JS::Object>> m_key_path_arrays;
+
+ // NB: Each call to index() on the same IDBObjectStore instance with the same name returns the same IDBIndex
+ // instance. These are keyed by index so that they survive renames.
+ HashMap<GC::Ref<Index>, GC::Ref<IDBIndex>> m_index_handles;
};
}
diff --git a/Tests/LibWeb/Text/expected/indexeddb-index-handle-identity.txt b/Tests/LibWeb/Text/expected/indexeddb-index-handle-identity.txt
new file mode 100644
index 00000000000..a73d4f27bd7
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/indexeddb-index-handle-identity.txt
@@ -0,0 +1,5 @@
+createIndex returns the cached handle: true
+index returns the same handle: true
+the handle survives renaming: true
+recreating the index makes a new handle: true
+the new handle is cached: true
diff --git a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbindex-rename.any.sharedworker.txt b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbindex-rename.any.sharedworker.txt
index 275fc77a9f4..402a5055b24 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbindex-rename.any.sharedworker.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbindex-rename.any.sharedworker.txt
@@ -2,10 +2,9 @@ Harness status: OK
Found 9 tests
-7 Pass
-2 Fail
-Fail IndexedDB index rename in new transaction
-Fail IndexedDB index rename in the transaction where it is created
+9 Pass
+Pass IndexedDB index rename in new transaction
+Pass IndexedDB index rename in the transaction where it is created
Pass IndexedDB index rename to the same name succeeds
Pass IndexedDB index rename to the name of a deleted index succeeds
Pass IndexedDB index swapping via renames succeeds
diff --git a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbindex-rename.any.txt b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbindex-rename.any.txt
index 275fc77a9f4..402a5055b24 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbindex-rename.any.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbindex-rename.any.txt
@@ -2,10 +2,9 @@ Harness status: OK
Found 9 tests
-7 Pass
-2 Fail
-Fail IndexedDB index rename in new transaction
-Fail IndexedDB index rename in the transaction where it is created
+9 Pass
+Pass IndexedDB index rename in new transaction
+Pass IndexedDB index rename in the transaction where it is created
Pass IndexedDB index rename to the same name succeeds
Pass IndexedDB index rename to the name of a deleted index succeeds
Pass IndexedDB index swapping via renames succeeds
diff --git a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbindex-rename.any.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbindex-rename.any.worker.txt
index 275fc77a9f4..402a5055b24 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbindex-rename.any.worker.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbindex-rename.any.worker.txt
@@ -2,10 +2,9 @@ Harness status: OK
Found 9 tests
-7 Pass
-2 Fail
-Fail IndexedDB index rename in new transaction
-Fail IndexedDB index rename in the transaction where it is created
+9 Pass
+Pass IndexedDB index rename in new transaction
+Pass IndexedDB index rename in the transaction where it is created
Pass IndexedDB index rename to the same name succeeds
Pass IndexedDB index rename to the name of a deleted index succeeds
Pass IndexedDB index swapping via renames succeeds
diff --git a/Tests/LibWeb/Text/input/indexeddb-index-handle-identity.html b/Tests/LibWeb/Text/input/indexeddb-index-handle-identity.html
new file mode 100644
index 00000000000..7fd240dec05
--- /dev/null
+++ b/Tests/LibWeb/Text/input/indexeddb-index-handle-identity.html
@@ -0,0 +1,37 @@
+<!doctype html>
+<script src="include.js"></script>
+<script>
+ asyncTest(async done => {
+ if (document.readyState !== "complete") {
+ await new Promise(resolve => window.addEventListener("load", resolve, { once: true }));
+ }
+ spoofCurrentURL("https://example.com/indexeddb-index-handle-identity.html");
+ const name = `index-handle-identity-${Date.now()}-${Math.random()}`;
+ const request = indexedDB.open(name, 1);
+ request.onupgradeneeded = () => {
+ const store = request.result.createObjectStore("store");
+ const created = store.createIndex("index", "key");
+ println(`createIndex returns the cached handle: ${created === store.index("index")}`);
+ println(`index returns the same handle: ${store.index("index") === store.index("index")}`);
+ created.name = "renamed";
+ println(`the handle survives renaming: ${created === store.index("renamed")}`);
+ store.deleteIndex("renamed");
+ const recreated = store.createIndex("renamed", "otherKey");
+ println(`recreating the index makes a new handle: ${recreated !== created}`);
+ println(`the new handle is cached: ${recreated === store.index("renamed")}`);
+ };
+ request.onsuccess = () => {
+ request.result.close();
+ const deletion = indexedDB.deleteDatabase(name);
+ deletion.onsuccess = done;
+ deletion.onerror = () => {
+ println(`delete error: ${deletion.error.name}`);
+ done();
+ };
+ };
+ request.onerror = () => {
+ println(`open error: ${request.error.name}`);
+ done();
+ };
+ });
+</script>