gecko/browser/components/sessionstore/SessionStorage.jsm
Birunthan Mohanathas 3571e0740d Bug 1038458 - Part 6: Flatten browser/components/sessionstore/src/ directory. r=mak
--HG--
rename : browser/components/sessionstore/src/ContentRestore.jsm => browser/components/sessionstore/ContentRestore.jsm
rename : browser/components/sessionstore/src/DocShellCapabilities.jsm => browser/components/sessionstore/DocShellCapabilities.jsm
rename : browser/components/sessionstore/src/FrameTree.jsm => browser/components/sessionstore/FrameTree.jsm
rename : browser/components/sessionstore/src/GlobalState.jsm => browser/components/sessionstore/GlobalState.jsm
rename : browser/components/sessionstore/src/PageStyle.jsm => browser/components/sessionstore/PageStyle.jsm
rename : browser/components/sessionstore/src/PrivacyFilter.jsm => browser/components/sessionstore/PrivacyFilter.jsm
rename : browser/components/sessionstore/src/PrivacyLevel.jsm => browser/components/sessionstore/PrivacyLevel.jsm
rename : browser/components/sessionstore/src/RecentlyClosedTabsAndWindowsMenuUtils.jsm => browser/components/sessionstore/RecentlyClosedTabsAndWindowsMenuUtils.jsm
rename : browser/components/sessionstore/src/SessionCookies.jsm => browser/components/sessionstore/SessionCookies.jsm
rename : browser/components/sessionstore/src/SessionFile.jsm => browser/components/sessionstore/SessionFile.jsm
rename : browser/components/sessionstore/src/SessionHistory.jsm => browser/components/sessionstore/SessionHistory.jsm
rename : browser/components/sessionstore/src/SessionMigration.jsm => browser/components/sessionstore/SessionMigration.jsm
rename : browser/components/sessionstore/src/SessionSaver.jsm => browser/components/sessionstore/SessionSaver.jsm
rename : browser/components/sessionstore/src/SessionStorage.jsm => browser/components/sessionstore/SessionStorage.jsm
rename : browser/components/sessionstore/src/SessionStore.jsm => browser/components/sessionstore/SessionStore.jsm
rename : browser/components/sessionstore/src/SessionWorker.js => browser/components/sessionstore/SessionWorker.js
rename : browser/components/sessionstore/src/SessionWorker.jsm => browser/components/sessionstore/SessionWorker.jsm
rename : browser/components/sessionstore/src/TabAttributes.jsm => browser/components/sessionstore/TabAttributes.jsm
rename : browser/components/sessionstore/src/TabState.jsm => browser/components/sessionstore/TabState.jsm
rename : browser/components/sessionstore/src/TabStateCache.jsm => browser/components/sessionstore/TabStateCache.jsm
rename : browser/components/sessionstore/src/Utils.jsm => browser/components/sessionstore/Utils.jsm
rename : browser/components/sessionstore/src/nsSessionStartup.js => browser/components/sessionstore/nsSessionStartup.js
rename : browser/components/sessionstore/src/nsSessionStore.js => browser/components/sessionstore/nsSessionStore.js
rename : browser/components/sessionstore/src/nsSessionStore.manifest => browser/components/sessionstore/nsSessionStore.manifest
2014-07-26 09:32:08 -07:00

162 lines
5.5 KiB
JavaScript

/* 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/. */
"use strict";
this.EXPORTED_SYMBOLS = ["SessionStorage"];
const Cu = Components.utils;
const Ci = Components.interfaces;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "console",
"resource://gre/modules/devtools/Console.jsm");
// Returns the principal for a given |frame| contained in a given |docShell|.
function getPrincipalForFrame(docShell, frame) {
let ssm = Services.scriptSecurityManager;
let uri = frame.document.documentURIObject;
return ssm.getDocShellCodebasePrincipal(uri, docShell);
}
this.SessionStorage = Object.freeze({
/**
* Updates all sessionStorage "super cookies"
* @param docShell
* That tab's docshell (containing the sessionStorage)
* @param frameTree
* The docShell's FrameTree instance.
* @return Returns a nested object that will have hosts as keys and per-host
* session storage data as values. For example:
* {"example.com": {"key": "value", "my_number": 123}}
*/
collect: function (docShell, frameTree) {
return SessionStorageInternal.collect(docShell, frameTree);
},
/**
* Restores all sessionStorage "super cookies".
* @param aDocShell
* A tab's docshell (containing the sessionStorage)
* @param aStorageData
* A nested object with storage data to be restored that has hosts as
* keys and per-host session storage data as values. For example:
* {"example.com": {"key": "value", "my_number": 123}}
*/
restore: function (aDocShell, aStorageData) {
SessionStorageInternal.restore(aDocShell, aStorageData);
}
});
let SessionStorageInternal = {
/**
* Reads all session storage data from the given docShell.
* @param docShell
* A tab's docshell (containing the sessionStorage)
* @param frameTree
* The docShell's FrameTree instance.
* @return Returns a nested object that will have hosts as keys and per-host
* session storage data as values. For example:
* {"example.com": {"key": "value", "my_number": 123}}
*/
collect: function (docShell, frameTree) {
let data = {};
let visitedOrigins = new Set();
frameTree.forEach(frame => {
let principal = getPrincipalForFrame(docShell, frame);
if (!principal) {
return;
}
// Get the root domain of the current history entry
// and use that as a key for the per-host storage data.
let origin = principal.jarPrefix + principal.origin;
if (visitedOrigins.has(origin)) {
// Don't read a host twice.
return;
}
// Mark the current origin as visited.
visitedOrigins.add(origin);
let originData = this._readEntry(principal, docShell);
if (Object.keys(originData).length) {
data[origin] = originData;
}
});
return Object.keys(data).length ? data : null;
},
/**
* Writes session storage data to the given tab.
* @param aDocShell
* A tab's docshell (containing the sessionStorage)
* @param aStorageData
* A nested object with storage data to be restored that has hosts as
* keys and per-host session storage data as values. For example:
* {"example.com": {"key": "value", "my_number": 123}}
*/
restore: function (aDocShell, aStorageData) {
for (let host of Object.keys(aStorageData)) {
let data = aStorageData[host];
let uri = Services.io.newURI(host, null, null);
let principal = Services.scriptSecurityManager.getDocShellCodebasePrincipal(uri, aDocShell);
let storageManager = aDocShell.QueryInterface(Ci.nsIDOMStorageManager);
let window = aDocShell.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
// There is no need to pass documentURI, it's only used to fill documentURI property of
// 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.
let storage = storageManager.createStorage(window, principal, "", aDocShell.usePrivateBrowsing);
for (let key of Object.keys(data)) {
try {
storage.setItem(key, data[key]);
} catch (e) {
// throws e.g. for URIs that can't have sessionStorage
console.error(e);
}
}
}
},
/**
* Reads an entry in the session storage data contained in a tab's history.
* @param aURI
* That history entry uri
* @param aDocShell
* A tab's docshell (containing the sessionStorage)
*/
_readEntry: function (aPrincipal, aDocShell) {
let hostData = {};
let storage;
let window = aDocShell.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
try {
let storageManager = aDocShell.QueryInterface(Ci.nsIDOMStorageManager);
storage = storageManager.getStorage(window, aPrincipal);
} 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++) {
try {
let key = storage.key(i);
hostData[key] = storage.getItem(key);
} catch (e) {
// This currently throws for secured items (cf. bug 442048).
}
}
}
return hostData;
}
};