browser/components/sessionstore/src/SessionCookies.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 this.EXPORTED_SYMBOLS = ["SessionCookies"];
michael@0 8
michael@0 9 const Cu = Components.utils;
michael@0 10 const Ci = Components.interfaces;
michael@0 11
michael@0 12 Cu.import("resource://gre/modules/Services.jsm", this);
michael@0 13 Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
michael@0 14
michael@0 15 XPCOMUtils.defineLazyModuleGetter(this, "Utils",
michael@0 16 "resource:///modules/sessionstore/Utils.jsm");
michael@0 17 XPCOMUtils.defineLazyModuleGetter(this, "PrivacyLevel",
michael@0 18 "resource:///modules/sessionstore/PrivacyLevel.jsm");
michael@0 19
michael@0 20 // MAX_EXPIRY should be 2^63-1, but JavaScript can't handle that precision.
michael@0 21 const MAX_EXPIRY = Math.pow(2, 62);
michael@0 22
michael@0 23 /**
michael@0 24 * The external API implemented by the SessionCookies module.
michael@0 25 */
michael@0 26 this.SessionCookies = Object.freeze({
michael@0 27 update: function (windows) {
michael@0 28 SessionCookiesInternal.update(windows);
michael@0 29 },
michael@0 30
michael@0 31 getHostsForWindow: function (window, checkPrivacy = false) {
michael@0 32 return SessionCookiesInternal.getHostsForWindow(window, checkPrivacy);
michael@0 33 }
michael@0 34 });
michael@0 35
michael@0 36 /**
michael@0 37 * The internal API.
michael@0 38 */
michael@0 39 let SessionCookiesInternal = {
michael@0 40 /**
michael@0 41 * Stores whether we're initialized, yet.
michael@0 42 */
michael@0 43 _initialized: false,
michael@0 44
michael@0 45 /**
michael@0 46 * Retrieve the list of all hosts contained in the given windows' session
michael@0 47 * history entries (per window) and collect the associated cookies for those
michael@0 48 * hosts, if any. The given state object is being modified.
michael@0 49 *
michael@0 50 * @param windows
michael@0 51 * Array of window state objects.
michael@0 52 * [{ tabs: [...], cookies: [...] }, ...]
michael@0 53 */
michael@0 54 update: function (windows) {
michael@0 55 this._ensureInitialized();
michael@0 56
michael@0 57 for (let window of windows) {
michael@0 58 let cookies = [];
michael@0 59
michael@0 60 // Collect all hosts for the current window.
michael@0 61 let hosts = this.getHostsForWindow(window, true);
michael@0 62
michael@0 63 for (let host of Object.keys(hosts)) {
michael@0 64 let isPinned = hosts[host];
michael@0 65
michael@0 66 for (let cookie of CookieStore.getCookiesForHost(host)) {
michael@0 67 // _getCookiesForHost() will only return hosts with the right privacy
michael@0 68 // rules, so there is no need to do anything special with this call
michael@0 69 // to PrivacyLevel.canSave().
michael@0 70 if (PrivacyLevel.canSave({isHttps: cookie.secure, isPinned: isPinned})) {
michael@0 71 cookies.push(cookie);
michael@0 72 }
michael@0 73 }
michael@0 74 }
michael@0 75
michael@0 76 // Don't include/keep empty cookie sections.
michael@0 77 if (cookies.length) {
michael@0 78 window.cookies = cookies;
michael@0 79 } else if ("cookies" in window) {
michael@0 80 delete window.cookies;
michael@0 81 }
michael@0 82 }
michael@0 83 },
michael@0 84
michael@0 85 /**
michael@0 86 * Returns a map of all hosts for a given window that we might want to
michael@0 87 * collect cookies for.
michael@0 88 *
michael@0 89 * @param window
michael@0 90 * A window state object containing tabs with history entries.
michael@0 91 * @param checkPrivacy (bool)
michael@0 92 * Whether to check the privacy level for each host.
michael@0 93 * @return {object} A map of hosts for a given window state object. The keys
michael@0 94 * will be hosts, the values are boolean and determine
michael@0 95 * whether we will use the deferred privacy level when
michael@0 96 * checking how much data to save on quitting.
michael@0 97 */
michael@0 98 getHostsForWindow: function (window, checkPrivacy = false) {
michael@0 99 let hosts = {};
michael@0 100
michael@0 101 for (let tab of window.tabs) {
michael@0 102 for (let entry of tab.entries) {
michael@0 103 this._extractHostsFromEntry(entry, hosts, checkPrivacy, tab.pinned);
michael@0 104 }
michael@0 105 }
michael@0 106
michael@0 107 return hosts;
michael@0 108 },
michael@0 109
michael@0 110 /**
michael@0 111 * Handles observers notifications that are sent whenever cookies are added,
michael@0 112 * changed, or removed. Ensures that the storage is updated accordingly.
michael@0 113 */
michael@0 114 observe: function (subject, topic, data) {
michael@0 115 switch (data) {
michael@0 116 case "added":
michael@0 117 case "changed":
michael@0 118 this._updateCookie(subject);
michael@0 119 break;
michael@0 120 case "deleted":
michael@0 121 this._removeCookie(subject);
michael@0 122 break;
michael@0 123 case "cleared":
michael@0 124 CookieStore.clear();
michael@0 125 break;
michael@0 126 case "batch-deleted":
michael@0 127 this._removeCookies(subject);
michael@0 128 break;
michael@0 129 case "reload":
michael@0 130 CookieStore.clear();
michael@0 131 this._reloadCookies();
michael@0 132 break;
michael@0 133 default:
michael@0 134 throw new Error("Unhandled cookie-changed notification.");
michael@0 135 }
michael@0 136 },
michael@0 137
michael@0 138 /**
michael@0 139 * If called for the first time in a session, iterates all cookies in the
michael@0 140 * cookies service and puts them into the store if they're session cookies.
michael@0 141 */
michael@0 142 _ensureInitialized: function () {
michael@0 143 if (!this._initialized) {
michael@0 144 this._reloadCookies();
michael@0 145 this._initialized = true;
michael@0 146 Services.obs.addObserver(this, "cookie-changed", false);
michael@0 147 }
michael@0 148 },
michael@0 149
michael@0 150 /**
michael@0 151 * Fill a given map with hosts found in the given entry's session history and
michael@0 152 * any child entries.
michael@0 153 *
michael@0 154 * @param entry
michael@0 155 * the history entry, serialized
michael@0 156 * @param hosts
michael@0 157 * the hash that will be used to store hosts eg, { hostname: true }
michael@0 158 * @param checkPrivacy
michael@0 159 * should we check the privacy level for https
michael@0 160 * @param isPinned
michael@0 161 * is the entry we're evaluating for a pinned tab; used only if
michael@0 162 * checkPrivacy
michael@0 163 */
michael@0 164 _extractHostsFromEntry: function (entry, hosts, checkPrivacy, isPinned) {
michael@0 165 let host = entry._host;
michael@0 166 let scheme = entry._scheme;
michael@0 167
michael@0 168 // If host & scheme aren't defined, then we are likely here in the startup
michael@0 169 // process via _splitCookiesFromWindow. In that case, we'll turn entry.url
michael@0 170 // into an nsIURI and get host/scheme from that. This will throw for about:
michael@0 171 // urls in which case we don't need to do anything.
michael@0 172 if (!host && !scheme) {
michael@0 173 try {
michael@0 174 let uri = Utils.makeURI(entry.url);
michael@0 175 host = uri.host;
michael@0 176 scheme = uri.scheme;
michael@0 177 this._extractHostsFromHostScheme(host, scheme, hosts, checkPrivacy, isPinned);
michael@0 178 }
michael@0 179 catch (ex) { }
michael@0 180 }
michael@0 181
michael@0 182 if (entry.children) {
michael@0 183 for (let child of entry.children) {
michael@0 184 this._extractHostsFromEntry(child, hosts, checkPrivacy, isPinned);
michael@0 185 }
michael@0 186 }
michael@0 187 },
michael@0 188
michael@0 189 /**
michael@0 190 * Add a given host to a given map of hosts if the privacy level allows
michael@0 191 * saving cookie data for it.
michael@0 192 *
michael@0 193 * @param host
michael@0 194 * the host of a uri (usually via nsIURI.host)
michael@0 195 * @param scheme
michael@0 196 * the scheme of a uri (usually via nsIURI.scheme)
michael@0 197 * @param hosts
michael@0 198 * the hash that will be used to store hosts eg, { hostname: true }
michael@0 199 * @param checkPrivacy
michael@0 200 * should we check the privacy level for https
michael@0 201 * @param isPinned
michael@0 202 * is the entry we're evaluating for a pinned tab; used only if
michael@0 203 * checkPrivacy
michael@0 204 */
michael@0 205 _extractHostsFromHostScheme:
michael@0 206 function (host, scheme, hosts, checkPrivacy, isPinned) {
michael@0 207 // host and scheme may not be set (for about: urls for example), in which
michael@0 208 // case testing scheme will be sufficient.
michael@0 209 if (/https?/.test(scheme) && !hosts[host] &&
michael@0 210 (!checkPrivacy ||
michael@0 211 PrivacyLevel.canSave({isHttps: scheme == "https", isPinned: isPinned}))) {
michael@0 212 // By setting this to true or false, we can determine when looking at
michael@0 213 // the host in update() if we should check for privacy.
michael@0 214 hosts[host] = isPinned;
michael@0 215 } else if (scheme == "file") {
michael@0 216 hosts[host] = true;
michael@0 217 }
michael@0 218 },
michael@0 219
michael@0 220 /**
michael@0 221 * Updates or adds a given cookie to the store.
michael@0 222 */
michael@0 223 _updateCookie: function (cookie) {
michael@0 224 cookie.QueryInterface(Ci.nsICookie2);
michael@0 225
michael@0 226 if (cookie.isSession) {
michael@0 227 CookieStore.set(cookie);
michael@0 228 }
michael@0 229 },
michael@0 230
michael@0 231 /**
michael@0 232 * Removes a given cookie from the store.
michael@0 233 */
michael@0 234 _removeCookie: function (cookie) {
michael@0 235 cookie.QueryInterface(Ci.nsICookie2);
michael@0 236
michael@0 237 if (cookie.isSession) {
michael@0 238 CookieStore.delete(cookie);
michael@0 239 }
michael@0 240 },
michael@0 241
michael@0 242 /**
michael@0 243 * Removes a given list of cookies from the store.
michael@0 244 */
michael@0 245 _removeCookies: function (cookies) {
michael@0 246 for (let i = 0; i < cookies.length; i++) {
michael@0 247 this._removeCookie(cookies.queryElementAt(i, Ci.nsICookie2));
michael@0 248 }
michael@0 249 },
michael@0 250
michael@0 251 /**
michael@0 252 * Iterates all cookies in the cookies service and puts them into the store
michael@0 253 * if they're session cookies.
michael@0 254 */
michael@0 255 _reloadCookies: function () {
michael@0 256 let iter = Services.cookies.enumerator;
michael@0 257 while (iter.hasMoreElements()) {
michael@0 258 this._updateCookie(iter.getNext());
michael@0 259 }
michael@0 260 }
michael@0 261 };
michael@0 262
michael@0 263 /**
michael@0 264 * The internal cookie storage that keeps track of every active session cookie.
michael@0 265 * These are stored using maps per host, path, and cookie name.
michael@0 266 */
michael@0 267 let CookieStore = {
michael@0 268 /**
michael@0 269 * The internal structure holding all known cookies.
michael@0 270 *
michael@0 271 * Host =>
michael@0 272 * Path =>
michael@0 273 * Name => {path: "/", name: "sessionid", secure: true}
michael@0 274 *
michael@0 275 * Maps are used for storage but the data structure is equivalent to this:
michael@0 276 *
michael@0 277 * this._hosts = {
michael@0 278 * "www.mozilla.org": {
michael@0 279 * "/": {
michael@0 280 * "username": {name: "username", value: "my_name_is", etc...},
michael@0 281 * "sessionid": {name: "sessionid", value: "1fdb3a", etc...}
michael@0 282 * }
michael@0 283 * },
michael@0 284 * "tbpl.mozilla.org": {
michael@0 285 * "/path": {
michael@0 286 * "cookiename": {name: "cookiename", value: "value", etc...}
michael@0 287 * }
michael@0 288 * }
michael@0 289 * };
michael@0 290 */
michael@0 291 _hosts: new Map(),
michael@0 292
michael@0 293 /**
michael@0 294 * Returns the list of stored session cookies for a given host.
michael@0 295 *
michael@0 296 * @param host
michael@0 297 * A string containing the host name we want to get cookies for.
michael@0 298 */
michael@0 299 getCookiesForHost: function (host) {
michael@0 300 if (!this._hosts.has(host)) {
michael@0 301 return [];
michael@0 302 }
michael@0 303
michael@0 304 let cookies = [];
michael@0 305
michael@0 306 for (let pathToNamesMap of this._hosts.get(host).values()) {
michael@0 307 cookies.push(...pathToNamesMap.values());
michael@0 308 }
michael@0 309
michael@0 310 return cookies;
michael@0 311 },
michael@0 312
michael@0 313 /**
michael@0 314 * Stores a given cookie.
michael@0 315 *
michael@0 316 * @param cookie
michael@0 317 * The nsICookie2 object to add to the storage.
michael@0 318 */
michael@0 319 set: function (cookie) {
michael@0 320 let jscookie = {host: cookie.host, value: cookie.value};
michael@0 321
michael@0 322 // Only add properties with non-default values to save a few bytes.
michael@0 323 if (cookie.path) {
michael@0 324 jscookie.path = cookie.path;
michael@0 325 }
michael@0 326
michael@0 327 if (cookie.name) {
michael@0 328 jscookie.name = cookie.name;
michael@0 329 }
michael@0 330
michael@0 331 if (cookie.isSecure) {
michael@0 332 jscookie.secure = true;
michael@0 333 }
michael@0 334
michael@0 335 if (cookie.isHttpOnly) {
michael@0 336 jscookie.httponly = true;
michael@0 337 }
michael@0 338
michael@0 339 if (cookie.expiry < MAX_EXPIRY) {
michael@0 340 jscookie.expiry = cookie.expiry;
michael@0 341 }
michael@0 342
michael@0 343 this._ensureMap(cookie).set(cookie.name, jscookie);
michael@0 344 },
michael@0 345
michael@0 346 /**
michael@0 347 * Removes a given cookie.
michael@0 348 *
michael@0 349 * @param cookie
michael@0 350 * The nsICookie2 object to be removed from storage.
michael@0 351 */
michael@0 352 delete: function (cookie) {
michael@0 353 this._ensureMap(cookie).delete(cookie.name);
michael@0 354 },
michael@0 355
michael@0 356 /**
michael@0 357 * Removes all cookies.
michael@0 358 */
michael@0 359 clear: function () {
michael@0 360 this._hosts.clear();
michael@0 361 },
michael@0 362
michael@0 363 /**
michael@0 364 * Creates all maps necessary to store a given cookie.
michael@0 365 *
michael@0 366 * @param cookie
michael@0 367 * The nsICookie2 object to create maps for.
michael@0 368 *
michael@0 369 * @return The newly created Map instance mapping cookie names to
michael@0 370 * internal jscookies, in the given path of the given host.
michael@0 371 */
michael@0 372 _ensureMap: function (cookie) {
michael@0 373 if (!this._hosts.has(cookie.host)) {
michael@0 374 this._hosts.set(cookie.host, new Map());
michael@0 375 }
michael@0 376
michael@0 377 let pathToNamesMap = this._hosts.get(cookie.host);
michael@0 378
michael@0 379 if (!pathToNamesMap.has(cookie.path)) {
michael@0 380 pathToNamesMap.set(cookie.path, new Map());
michael@0 381 }
michael@0 382
michael@0 383 return pathToNamesMap.get(cookie.path);
michael@0 384 }
michael@0 385 };

mercurial