Truly independent web browser
LibWeb: Make an IndexedDB open wait for the upgrade before it
sideshowbarker0543d88053ae
diff --git a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/request_list.rs b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/request_list.rsindex 0dd141f2413..1f5907b6f83 100644--- a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/request_list.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/request_list.rs@@ -30,6 +30,8 @@ pub(crate) struct RequestList {entries: GcRefCell<Vec<Entry>>,on_all_processed: Cell<Option<Function>>,blocked: Cell<bool>,+ // Whether this is a connection queue's list, whose requests run one at a time, each until its steps complete.+ is_connection_queue: bool,}impl RequestList {@@ -38,6 +40,14 @@ impl RequestList {entries: GcRefCell::new(Vec::new()),on_all_processed: Cell::new(None),blocked: Cell::new(false),+ is_connection_queue: false,+ }+ }++ pub(crate) fn new_for_connection_queue(defer: &DeferGc<'_>) -> Self {+ Self {+ is_connection_queue: true,+ ..Self::new(defer)}}@@ -64,7 +74,11 @@ impl RequestList {let Some(request) = entry.request.get() else {continue;};- if request.processed() {+ // AD-HOC: The spec has a connection queue's request wait only until the requests before it are processed,+ // and upgrading a database sets its request's processed flag before it fires upgradeneeded. So a+ // request would run while the upgrade transaction is live. Instead, a request waits until the one+ // before it has completed, which drops it from the queue.+ if request.processed() && !self.is_connection_queue {continue;}if request.aborted() {@@ -88,6 +102,12 @@ impl RequestList {}pub(crate) fn on_request_processed(&self, vm: &Vm) {+ if self.is_connection_queue {+ let mut entries = self.entries.borrow_mut(vm);+ if let Some(position) = entries.iter().position(|entry| entry.steps.is_none()) {+ entries.remove(position);+ }+ }self.maybe_process_next_request(vm);}diff --git a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/storage.rs b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/storage.rsindex 27d3956f82a..0a30474bcbf 100644--- a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/storage.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/storage.rs@@ -171,7 +171,7 @@ impl Storage {let new_queue = CONNECTION_QUEUE_CLASS.allocate(vm, |defer| ConnectionQueue {storage_key: key.clone(),name: name.clone(),- request_list: RequestList::new(defer),+ request_list: RequestList::new_for_connection_queue(defer),});self.connection_queues.borrow_mut(vm).push(new_queue);new_queuediff --git a/Tests/LibWeb/Text/expected/indexeddb-open-during-upgradeneeded-waits-for-upgrade.txt b/Tests/LibWeb/Text/expected/indexeddb-open-during-upgradeneeded-waits-for-upgrade.txtnew file mode 100644index 00000000000..30266084bc5--- /dev/null+++ b/Tests/LibWeb/Text/expected/indexeddb-open-during-upgradeneeded-waits-for-upgrade.txt@@ -0,0 +1,4 @@+first: upgradeneeded+first: success+second: success+second: transaction() OKdiff --git a/Tests/LibWeb/Text/input/indexeddb-open-during-upgradeneeded-waits-for-upgrade.html b/Tests/LibWeb/Text/input/indexeddb-open-during-upgradeneeded-waits-for-upgrade.htmlnew file mode 100644index 00000000000..c3da258ead6--- /dev/null+++ b/Tests/LibWeb/Text/input/indexeddb-open-during-upgradeneeded-waits-for-upgrade.html@@ -0,0 +1,43 @@+<!DOCTYPE html>+<script src="include.js"></script>+<script>+asyncTest(done => {+ setTimeout(() => {+ // We need to do this spoofing later in the event loop so that we don't end up+ // telling the test runner the wrong URL in page_did_finish_loading.+ spoofCurrentURL("https://example.com/indexeddb-open-during-upgradeneeded-waits-for-upgrade.html");++ // A second open() made from the first one's upgradeneeded handler waits until the upgrade is done: Its+ // success comes after the first request's, and it can start a transaction then. The upgrade writes records,+ // so its transaction is still live when the second request would otherwise succeed.+ const dbName = "test-open-during-upgradeneeded-" + Date.now() + Math.random();+ const deleteRequest = indexedDB.deleteDatabase(dbName);+ deleteRequest.onsuccess = deleteRequest.onerror = () => {+ const first = indexedDB.open(dbName, 1);+ first.onupgradeneeded = event => {+ println("first: upgradeneeded");+ const store = event.target.result.createObjectStore("kv");+ for (let i = 0; i < 200; ++i)+ store.put(`value ${i}`, `key ${i}`);++ const second = indexedDB.open(dbName);+ second.onsuccess = event => {+ println("second: success");+ try {+ event.target.result.transaction("kv", "readonly");+ println("second: transaction() OK");+ } catch (error) {+ println(`second: transaction() threw ${error.name}`);+ }+ done();+ };+ second.onerror = () => {+ println(`second: error ${second.error}`);+ done();+ };+ };+ first.onsuccess = () => println("first: success");+ };+ }, 0);+});+</script>