The simple-storage
/localStorage
APIs suck because of synchronous file I/O.
There are alternatives such as IndexedDB
which can be used from chrome
/add-on code quite easily.
You can also use localStorage
in your add-on (no need to use the SDK simple-storage
API), but should not use window.localStorage
in overlays because that would be shared between add-ons, and cannot use window.localStorage
in bootstrap.js
and/or js code modules because there simply is no window
. But you can construct a storage object yourself.
function getStorage(uri) {
if (!(uri instanceof Ci.nsIURI)) {
uri = Services.io.newURI(uri, null, null);
}
let principal = Cc["@mozilla.org/scriptsecuritymanager;1"].
getService(Ci.nsIScriptSecurityManager).
getNoAppCodebasePrincipal(uri);
let dsm = Cc["@mozilla.org/dom/localStorage-manager;1"].
getService(Ci.nsIDOMStorageManager);
return dsm.createStorage(principal, "");
}
var s1 = getStorage("chrome://my-addon/content/whatever.xul"); // does not actually have to point to a resource.
The usual limitations of localStorage
apply (quotas and such).
BTW: The code also lets you access the localStorage
of websites, e.g. getStorage("http://stackoverflow.com/");
.