Bug 1212360. Split up the MozStorageChanged event into separate events for sessionstorage and localstorage. r=khuey

This commit is contained in:
Boris Zbarsky 2015-10-09 16:48:10 -04:00
parent 753f764a70
commit bc046e3aff
5 changed files with 20 additions and 31 deletions

View File

@ -61,20 +61,6 @@ function createLazy(fn) {
};
}
/**
* Determines whether the given storage event was triggered by changes
* to the sessionStorage object and not the local or globalStorage.
*/
function isSessionStorageEvent(event) {
try {
return event.storageArea == event.target.sessionStorage;
} catch (ex if ex instanceof Ci.nsIException && ex.result == Cr.NS_ERROR_NOT_AVAILABLE) {
// This page does not have a DOMSessionStorage
// (this is typically the case for about: pages)
return false;
}
}
/**
* Listens for and handles content events that we need for the
* session store service to be notified of state changes in content.
@ -520,7 +506,7 @@ var DocShellCapabilitiesListener = {
*/
var SessionStorageListener = {
init: function () {
addEventListener("MozStorageChanged", this, true);
addEventListener("MozSessionStorageChanged", this, true);
Services.obs.addObserver(this, "browser:purge-domain-data", false);
gFrameTree.addObserver(this);
},
@ -530,8 +516,7 @@ var SessionStorageListener = {
},
handleEvent: function (event) {
// Ignore events triggered by localStorage or globalStorage changes.
if (gFrameTree.contains(event.target) && isSessionStorageEvent(event)) {
if (gFrameTree.contains(event.target)) {
this.collect();
}
},

View File

@ -550,13 +550,13 @@ function modifySessionStorage(browser, data, options = {}) {
let storage = frame.sessionStorage;
return new Promise(resolve => {
addEventListener("MozStorageChanged", function onStorageChanged(event) {
addEventListener("MozSessionStorageChanged", function onStorageChanged(event) {
if (event.storageArea == storage) {
keys.delete(event.key);
}
if (keys.size == 0) {
removeEventListener("MozStorageChanged", onStorageChanged, true);
removeEventListener("MozSessionStorageChanged", onStorageChanged, true);
resolve();
}
}, true);

View File

@ -11837,6 +11837,8 @@ nsGlobalWindow::Observe(nsISupports* aSubject, const char* aTopic,
nsCOMPtr<nsIDOMStorage> istorage = changingStorage.get();
bool fireMozStorageChanged = false;
nsAutoString eventType;
eventType.AssignLiteral("storage");
principal = GetPrincipal();
if (!principal) {
return NS_OK;
@ -11872,6 +11874,9 @@ nsGlobalWindow::Observe(nsISupports* aSubject, const char* aTopic,
}
fireMozStorageChanged = mSessionStorage == changingStorage;
if (fireMozStorageChanged) {
eventType.AssignLiteral("MozSessionStorageChanged");
}
break;
}
@ -11889,6 +11894,9 @@ nsGlobalWindow::Observe(nsISupports* aSubject, const char* aTopic,
return NS_OK;
fireMozStorageChanged = mLocalStorage == changingStorage;
if (fireMozStorageChanged) {
eventType.AssignLiteral("MozLocalStorageChanged");
};
break;
}
default:
@ -11898,11 +11906,8 @@ nsGlobalWindow::Observe(nsISupports* aSubject, const char* aTopic,
// Clone the storage event included in the observer notification. We want
// to dispatch clones rather than the original event.
ErrorResult error;
nsRefPtr<StorageEvent> newEvent =
CloneStorageEvent(fireMozStorageChanged ?
NS_LITERAL_STRING("MozStorageChanged") :
NS_LITERAL_STRING("storage"),
event, error);
nsRefPtr<StorageEvent> newEvent = CloneStorageEvent(eventType,
event, error);
if (error.Failed()) {
return error.StealNSResult();
}

View File

@ -62,8 +62,8 @@ function startTest()
}
}
// Listen for MozStorageChanged
SpecialPowers.addChromeEventListener("MozStorageChanged", onStorageChanged, false);
// Listen for MozLocalStorageChanged
SpecialPowers.addChromeEventListener("MozLocalStorageChanged", onStorageChanged, false);
// add an empty-value key
localStorage.setItem("empty", "");
@ -227,7 +227,7 @@ function startTest()
localStorage.removeItem("key2"); // Just check there is no exception
SimpleTest.executeSoon(function () {
SpecialPowers.removeChromeEventListener("MozStorageChanged", onStorageChanged, false);
SpecialPowers.removeChromeEventListener("MozLocalStorageChanged", onStorageChanged, false);
is(expectedEvents.length, 0, "received the correct number of events");
localStorage.clear();

View File

@ -44,7 +44,6 @@ function startTest()
is(typeof sessionStorage["nonexisting2"], "undefined", "['nonexisting2'] is undefined");
is(typeof sessionStorage.nonexisting2, "undefined", "nonexisting2 is undefined");
var mozStorageChangedReceived = 0;
var sessionStorageCopy = sessionStorage;
function onStorageChanged(e) {
@ -55,8 +54,8 @@ function startTest()
}
}
// Listen for MozStorageChanged
SpecialPowers.addChromeEventListener("MozStorageChanged", onStorageChanged, false);
// Listen for MozSessionStorageChanged
SpecialPowers.addChromeEventListener("MozSessionStorageChanged", onStorageChanged, false);
// add an empty-value key
sessionStorage.setItem("empty", "");
@ -161,7 +160,7 @@ function startTest()
sessionStorage.removeItem("key2"); // Just check there is no exception
SimpleTest.executeSoon(function () {
SpecialPowers.removeChromeEventListener("MozStorageChanged", onStorageChanged, false);
SpecialPowers.removeChromeEventListener("MozSessionStorageChanged", onStorageChanged, false);
is(expectedEvents.length, 0, "received the correct number of events");
sessionStorage.clear();