2012-06-03 09:45:51 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-08-10 03:33:07 +00:00
|
|
|
"use strict";
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.EXPORTED_SYMBOLS = ["SessionStorage"];
|
2012-06-03 09:45:51 +00:00
|
|
|
|
|
|
|
const Cu = Components.utils;
|
2013-08-28 11:11:14 +00:00
|
|
|
const Ci = Components.interfaces;
|
2012-06-03 09:45:51 +00:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
|
2014-01-06 20:27:25 +00:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "console",
|
2015-10-15 10:45:22 +00:00
|
|
|
"resource://gre/modules/Console.jsm");
|
2013-12-20 13:23:32 +00:00
|
|
|
|
|
|
|
// Returns the principal for a given |frame| contained in a given |docShell|.
|
|
|
|
function getPrincipalForFrame(docShell, frame) {
|
|
|
|
let ssm = Services.scriptSecurityManager;
|
2014-01-07 10:56:28 +00:00
|
|
|
let uri = frame.document.documentURIObject;
|
2013-12-20 13:23:32 +00:00
|
|
|
return ssm.getDocShellCodebasePrincipal(uri, docShell);
|
|
|
|
}
|
2012-06-03 09:45:51 +00:00
|
|
|
|
2013-10-27 14:30:56 +00:00
|
|
|
this.SessionStorage = Object.freeze({
|
2012-06-03 09:45:51 +00:00
|
|
|
/**
|
|
|
|
* Updates all sessionStorage "super cookies"
|
2013-12-20 13:23:32 +00:00
|
|
|
* @param docShell
|
2012-06-03 09:45:51 +00:00
|
|
|
* That tab's docshell (containing the sessionStorage)
|
2013-12-20 13:23:32 +00:00
|
|
|
* @param frameTree
|
|
|
|
* The docShell's FrameTree instance.
|
2013-10-27 14:30:56 +00:00
|
|
|
* @return Returns a nested object that will have hosts as keys and per-host
|
2015-10-20 12:15:17 +00:00
|
|
|
* session storage data as strings. For example:
|
|
|
|
* {"example.com": {"key": "value", "my_number": "123"}}
|
2012-06-03 09:45:51 +00:00
|
|
|
*/
|
2013-12-20 13:23:32 +00:00
|
|
|
collect: function (docShell, frameTree) {
|
|
|
|
return SessionStorageInternal.collect(docShell, frameTree);
|
2012-06-03 09:45:51 +00:00
|
|
|
},
|
2012-06-03 09:45:51 +00:00
|
|
|
|
2012-06-03 09:45:51 +00:00
|
|
|
/**
|
|
|
|
* Restores all sessionStorage "super cookies".
|
|
|
|
* @param aDocShell
|
|
|
|
* A tab's docshell (containing the sessionStorage)
|
|
|
|
* @param aStorageData
|
2013-10-27 14:30:56 +00:00
|
|
|
* A nested object with storage data to be restored that has hosts as
|
2015-10-20 12:15:17 +00:00
|
|
|
* keys and per-host session storage data as strings. For example:
|
|
|
|
* {"example.com": {"key": "value", "my_number": "123"}}
|
2012-06-03 09:45:51 +00:00
|
|
|
*/
|
2013-10-27 14:30:56 +00:00
|
|
|
restore: function (aDocShell, aStorageData) {
|
|
|
|
SessionStorageInternal.restore(aDocShell, aStorageData);
|
2015-10-20 12:15:17 +00:00
|
|
|
},
|
2013-10-27 14:30:56 +00:00
|
|
|
});
|
2012-06-03 09:45:51 +00:00
|
|
|
|
2015-09-15 18:19:45 +00:00
|
|
|
var SessionStorageInternal = {
|
2012-06-03 09:45:51 +00:00
|
|
|
/**
|
|
|
|
* Reads all session storage data from the given docShell.
|
2013-12-20 13:23:32 +00:00
|
|
|
* @param docShell
|
2012-06-03 09:45:51 +00:00
|
|
|
* A tab's docshell (containing the sessionStorage)
|
2013-12-20 13:23:32 +00:00
|
|
|
* @param frameTree
|
|
|
|
* The docShell's FrameTree instance.
|
2013-10-27 14:30:56 +00:00
|
|
|
* @return Returns a nested object that will have hosts as keys and per-host
|
2015-10-20 12:15:17 +00:00
|
|
|
* session storage data as strings. For example:
|
|
|
|
* {"example.com": {"key": "value", "my_number": "123"}}
|
2012-06-03 09:45:51 +00:00
|
|
|
*/
|
2013-12-20 13:23:32 +00:00
|
|
|
collect: function (docShell, frameTree) {
|
2012-06-03 09:45:51 +00:00
|
|
|
let data = {};
|
2013-12-20 13:23:32 +00:00
|
|
|
let visitedOrigins = new Set();
|
2012-06-03 09:45:51 +00:00
|
|
|
|
2013-12-20 13:23:32 +00:00
|
|
|
frameTree.forEach(frame => {
|
|
|
|
let principal = getPrincipalForFrame(docShell, frame);
|
2013-10-27 14:30:56 +00:00
|
|
|
if (!principal) {
|
2013-12-20 13:23:32 +00:00
|
|
|
return;
|
2013-10-27 14:30:56 +00:00
|
|
|
}
|
2012-07-21 07:29:40 +00:00
|
|
|
|
2015-08-04 09:14:57 +00:00
|
|
|
// Get the origin of the current history entry
|
|
|
|
// and use that as a key for the per-principal storage data.
|
|
|
|
let origin = principal.origin;
|
2013-12-20 13:23:32 +00:00
|
|
|
if (visitedOrigins.has(origin)) {
|
2012-07-21 07:29:40 +00:00
|
|
|
// Don't read a host twice.
|
2013-12-20 13:23:32 +00:00
|
|
|
return;
|
2013-10-27 14:30:56 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 13:23:32 +00:00
|
|
|
// Mark the current origin as visited.
|
|
|
|
visitedOrigins.add(origin);
|
|
|
|
|
|
|
|
let originData = this._readEntry(principal, docShell);
|
2013-10-27 14:30:56 +00:00
|
|
|
if (Object.keys(originData).length) {
|
|
|
|
data[origin] = originData;
|
2012-06-03 09:45:51 +00:00
|
|
|
}
|
2013-12-20 13:23:32 +00:00
|
|
|
});
|
2012-06-03 09:45:51 +00:00
|
|
|
|
2013-10-27 14:30:56 +00:00
|
|
|
return Object.keys(data).length ? data : null;
|
2012-06-03 09:45:51 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2012-06-03 09:45:51 +00:00
|
|
|
* Writes session storage data to the given tab.
|
|
|
|
* @param aDocShell
|
|
|
|
* A tab's docshell (containing the sessionStorage)
|
2012-06-03 09:45:51 +00:00
|
|
|
* @param aStorageData
|
2013-10-27 14:30:56 +00:00
|
|
|
* A nested object with storage data to be restored that has hosts as
|
2015-10-20 12:15:17 +00:00
|
|
|
* keys and per-host session storage data as strings. For example:
|
|
|
|
* {"example.com": {"key": "value", "my_number": "123"}}
|
2012-06-03 09:45:51 +00:00
|
|
|
*/
|
2013-10-27 14:30:56 +00:00
|
|
|
restore: function (aDocShell, aStorageData) {
|
2015-08-04 09:14:57 +00:00
|
|
|
for (let origin of Object.keys(aStorageData)) {
|
|
|
|
let data = aStorageData[origin];
|
2015-08-26 11:12:13 +00:00
|
|
|
let principal = Services.scriptSecurityManager.createCodebasePrincipalFromOrigin(origin);
|
2013-12-20 13:23:32 +00:00
|
|
|
let storageManager = aDocShell.QueryInterface(Ci.nsIDOMStorageManager);
|
2014-07-23 05:07:12 +00:00
|
|
|
let window = aDocShell.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
|
2013-04-15 12:38:48 +00:00
|
|
|
|
|
|
|
// There is no need to pass documentURI, it's only used to fill documentURI property of
|
2013-08-28 11:11:14 +00:00
|
|
|
// domstorage event, which in this case has no consumer. Prevention of events in case
|
|
|
|
// of missing documentURI will be solved in a followup bug to bug 600307.
|
2014-07-23 05:07:12 +00:00
|
|
|
let storage = storageManager.createStorage(window, principal, "", aDocShell.usePrivateBrowsing);
|
2012-06-03 09:45:51 +00:00
|
|
|
|
2014-01-22 10:25:39 +00:00
|
|
|
for (let key of Object.keys(data)) {
|
2012-06-03 09:45:51 +00:00
|
|
|
try {
|
2014-01-22 10:25:39 +00:00
|
|
|
storage.setItem(key, data[key]);
|
2012-06-03 09:45:51 +00:00
|
|
|
} catch (e) {
|
|
|
|
// throws e.g. for URIs that can't have sessionStorage
|
2014-01-06 20:27:25 +00:00
|
|
|
console.error(e);
|
2012-06-03 09:45:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads an entry in the session storage data contained in a tab's history.
|
|
|
|
* @param aURI
|
|
|
|
* That history entry uri
|
2012-06-03 09:45:51 +00:00
|
|
|
* @param aDocShell
|
|
|
|
* A tab's docshell (containing the sessionStorage)
|
|
|
|
*/
|
2013-10-27 14:30:56 +00:00
|
|
|
_readEntry: function (aPrincipal, aDocShell) {
|
2012-06-03 09:45:51 +00:00
|
|
|
let hostData = {};
|
|
|
|
let storage;
|
|
|
|
|
2014-07-23 05:07:12 +00:00
|
|
|
let window = aDocShell.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
|
|
|
|
|
2012-06-03 09:45:51 +00:00
|
|
|
try {
|
2013-12-20 13:23:32 +00:00
|
|
|
let storageManager = aDocShell.QueryInterface(Ci.nsIDOMStorageManager);
|
2014-07-23 05:07:12 +00:00
|
|
|
storage = storageManager.getStorage(window, aPrincipal);
|
2012-06-03 09:45:51 +00:00
|
|
|
} catch (e) {
|
|
|
|
// sessionStorage might throw if it's turned off, see bug 458954
|
|
|
|
}
|
|
|
|
|
|
|
|
if (storage && storage.length) {
|
|
|
|
for (let i = 0; i < storage.length; i++) {
|
2012-06-03 09:45:51 +00:00
|
|
|
try {
|
2012-06-03 09:45:51 +00:00
|
|
|
let key = storage.key(i);
|
|
|
|
hostData[key] = storage.getItem(key);
|
|
|
|
} catch (e) {
|
|
|
|
// This currently throws for secured items (cf. bug 442048).
|
2012-06-03 09:45:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-03 09:45:51 +00:00
|
|
|
|
|
|
|
return hostData;
|
2012-06-03 09:45:51 +00:00
|
|
|
}
|
|
|
|
};
|