Skip to content

Commit ec6fd5b

Browse files
domeniccodex
andcommitted
Select storage event recipients at mutation time
Snapshot recipient windows when storage changes and queue an event for each one, instead of selecting recipients only when the delayed callback runs. This prevents documents created later from receiving events for changes made before they existed. Drop queued events if the recipient window closes, while allowing events to reach surviving recipients even if the source document is destroyed. Follow https://html.spec.whatwg.org/multipage/webstorage.html#concept-storage-broadcast. Co-authored-by: codex <[email protected]>
1 parent bcc037a commit ec6fd5b

2 files changed

Lines changed: 110 additions & 14 deletions

File tree

‎lib/jsdom/living/webstorage/Storage-impl.js‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,25 @@ class StorageImpl {
1919
this._globalObject = globalObject;
2020
}
2121

22-
_dispatchStorageEvent(key, oldValue, newValue) {
22+
// Select recipients before queuing their events.
23+
// https://html.spec.whatwg.org/multipage/webstorage.html#concept-storage-broadcast
24+
_broadcast(key, oldValue, newValue) {
2325
return this._associatedWindow._currentOriginData.windowsInSameOrigin
2426
.filter(target => target !== this._associatedWindow)
25-
.forEach(target => fireAnEvent("storage", target, StorageEvent, {
26-
key,
27-
oldValue,
28-
newValue,
29-
url: this._url,
30-
storageArea: target["_" + this._type]
31-
}));
27+
.forEach(target => {
28+
setTimeout(() => {
29+
if (!target._document) {
30+
return;
31+
}
32+
fireAnEvent("storage", target, StorageEvent, {
33+
key,
34+
oldValue,
35+
newValue,
36+
url: this._url,
37+
storageArea: target["_" + this._type]
38+
});
39+
}, 0);
40+
});
3241
}
3342

3443
get length() {
@@ -71,24 +80,22 @@ class StorageImpl {
7180
]);
7281
}
7382

74-
setTimeout(this._dispatchStorageEvent.bind(this), 0, key, oldValue, value);
75-
7683
this._items.set(key, value);
84+
this._broadcast(key, oldValue, value);
7785
}
7886

7987
removeItem(key) {
8088
if (this._items.has(key)) {
81-
setTimeout(this._dispatchStorageEvent.bind(this), 0, key, this._items.get(key), null);
82-
89+
const oldValue = this._items.get(key);
8390
this._items.delete(key);
91+
this._broadcast(key, oldValue, null);
8492
}
8593
}
8694

8795
clear() {
8896
if (this._items.size > 0) {
89-
setTimeout(this._dispatchStorageEvent.bind(this), 0, null, null, null);
90-
9197
this._items.clear();
98+
this._broadcast(null, null, null);
9299
}
93100
}
94101

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<!doctype html>
2+
<meta charset="utf-8">
3+
<title>Storage event recipients are selected when storage changes</title>
4+
<link rel="help" href="https://html.spec.whatwg.org/multipage/webstorage.html#concept-storage-broadcast">
5+
<script src="/resources/testharness.js"></script>
6+
<script src="/resources/testharnessreport.js"></script>
7+
<script src="/common/utils.js"></script>
8+
<script src="/html/browsers/browsing-the-web/resources/helpers.js"></script>
9+
<body>
10+
<script>
11+
"use strict";
12+
/* global addIframe, token */
13+
14+
for (const storageType of ["localStorage", "sessionStorage"]) {
15+
for (const operation of ["setItem", "removeItem", "clear"]) {
16+
promise_test(async t => {
17+
const existing = await addIframe();
18+
t.add_cleanup(() => existing.remove());
19+
const storage = window[storageType];
20+
const key = token();
21+
t.add_cleanup(() => storage.removeItem(key));
22+
const watcher = new EventWatcher(t, existing.contentWindow, "storage");
23+
if (operation !== "setItem") {
24+
const initialized = watcher.wait_for("storage");
25+
storage.setItem(key, "initial");
26+
await initialized;
27+
}
28+
const delivered = watcher.wait_for("storage");
29+
if (operation === "setItem") {
30+
storage.setItem(key, "value");
31+
} else if (operation === "removeItem") {
32+
storage.removeItem(key);
33+
} else {
34+
storage.clear();
35+
}
36+
const late = document.createElement("iframe");
37+
t.add_cleanup(() => late.remove());
38+
document.body.append(late);
39+
const events = [];
40+
const checkpointKey = token();
41+
t.add_cleanup(() => storage.removeItem(checkpointKey));
42+
const checkpoint = new Promise(resolve => {
43+
late.contentWindow.onstorage = event => {
44+
events.push(event.key);
45+
if (event.key === checkpointKey) {
46+
resolve();
47+
}
48+
};
49+
});
50+
const event = await delivered;
51+
watcher.stop_watching();
52+
assert_equals(event.key, operation === "clear" ? null : key);
53+
// Deliver a subsequent storage event to the late document before checking that it missed the earlier one.
54+
storage.setItem(checkpointKey, "checkpoint");
55+
await checkpoint;
56+
assert_array_equals(events, [checkpointKey], "A document created after the mutation is not a recipient");
57+
}, `${storageType}.${operation} does not send events to documents created after the mutation`);
58+
}
59+
60+
promise_test(async t => {
61+
const source = await addIframe();
62+
t.add_cleanup(() => source.remove());
63+
const key = token();
64+
t.add_cleanup(() => window[storageType].removeItem(key));
65+
const delivered = new EventWatcher(t, window, "storage").wait_for("storage");
66+
source.contentWindow[storageType].setItem(key, "value");
67+
source.remove();
68+
assert_equals((await delivered).key, key);
69+
}, `${storageType} events survive destruction of the source document`);
70+
71+
promise_test(async t => {
72+
const recipient = await addIframe();
73+
t.add_cleanup(() => recipient.remove());
74+
const surviving = await addIframe();
75+
t.add_cleanup(() => surviving.remove());
76+
const child = recipient.contentWindow;
77+
const key = token();
78+
t.add_cleanup(() => window[storageType].removeItem(key));
79+
const delivered = new EventWatcher(t, surviving.contentWindow, "storage").wait_for("storage");
80+
window[storageType].setItem(key, "value");
81+
recipient.remove();
82+
// Install the listener after removal so clearing listeners during destruction cannot make the test pass.
83+
const events = [];
84+
child.onstorage = event => events.push(event.key);
85+
assert_equals((await delivered).key, key);
86+
assert_array_equals(events, []);
87+
}, `${storageType} events are discarded when the recipient document is destroyed`);
88+
}
89+
</script>

0 commit comments

Comments
 (0)