B2G/gecko/browser/components/sessionstore/src/SessionStore.jsm
/**
   * Serialize cookie data
   * @param aWindows
   *        JS object containing window data references
   *        { id: winData, etc. }
   */
Serialize cookie data
@param aWindows
JS object containing window data references
{ id: winData, etc.
Serialize cookie data @param aWindows JS object containing window data references { id : winData etc .
function ssi_updateCookies(aWindows) {
    function addCookieToHash(aHash, aHost, aPath, aName, aCookie) {
      if (!aHash[aHost])
        aHash[aHost] = {};
      if (!aHash[aHost][aPath])
        aHash[aHost][aPath] = {};
      aHash[aHost][aPath][aName] = aCookie;
    }
    var jscookies = {};
    var _this = this;
    var MAX_EXPIRY = Math.pow(2, 62);
    for (let [id, window] in Iterator(aWindows)) {
      window.cookies = [];
      let internalWindow = this._internalWindows[id];
      if (!internalWindow.hosts)
        return;
      for (var [host, isPinned] in Iterator(internalWindow.hosts)) {
        let list;
        try {
          list = Services.cookies.getCookiesFromHost(host);
        }
        catch (ex) {
          debug("getCookiesFromHost failed. Host: " + host);
        }
        while (list && list.hasMoreElements()) {
          var cookie = list.getNext().QueryInterface(Ci.nsICookie2);
          if (cookie.isSession && _this.checkPrivacyLevel(cookie.isSecure, isPinned)) {
            if (!(cookie.host in jscookies &&
                  cookie.path in jscookies[cookie.host] &&
                  cookie.name in jscookies[cookie.host][cookie.path])) {
              var jscookie = { "host": cookie.host, "value": cookie.value };
              if (cookie.path) jscookie.path = cookie.path;
              if (cookie.name) jscookie.name = cookie.name;
              if (cookie.isSecure) jscookie.secure = true;
              if (cookie.isHttpOnly) jscookie.httponly = true;
              if (cookie.expiry < MAX_EXPIRY) jscookie.expiry = cookie.expiry;
              addCookieToHash(jscookies, cookie.host, cookie.path, cookie.name, jscookie);
            }
            window.cookies.push(jscookies[cookie.host][cookie.path][cookie.name]);
          }
        }
      }
      if (!window.cookies.length)
        delete window.cookies;
    }
  }
function ssi_updateCookies ( aWindows ) { function addCookieToHash ( aHash , aHost , aPath , aName , aCookie ) { if ( ! aHash [ aHost ] ) aHash [ aHost ] = { } ; if ( ! aHash [ aHost ] [ aPath ] ) aHash [ aHost ] [ aPath ] = { } ; aHash [ aHost ] [ aPath ] [ aName ] = aCookie ; } var jscookies = { } ; var _this = this ; var MAX_EXPIRY = Math . pow ( 2 , 62 ) ; for ( let [ id , window ] in Iterator ( aWindows ) ) { window . cookies = [ ] ; let internalWindow = this . _internalWindows [ id ] ; if ( ! internalWindow . hosts ) return ; for ( var [ host , isPinned ] in Iterator ( internalWindow . hosts ) ) { let list ; try { list = Services . cookies . getCookiesFromHost ( host ) ; } catch ( ex ) { debug ( "getCookiesFromHost failed. Host: " + host ) ; } while ( list && list . hasMoreElements ( ) ) { var cookie = list . getNext ( ) . QueryInterface ( Ci . nsICookie2 ) ; if ( cookie . isSession && _this . checkPrivacyLevel ( cookie . isSecure , isPinned ) ) { if ( ! ( cookie . host in jscookies && cookie . path in jscookies [ cookie . host ] && cookie . name in jscookies [ cookie . host ] [ cookie . path ] ) ) { var jscookie = { "host" : cookie . host , "value" : cookie . value } ; if ( cookie . path ) jscookie . path = cookie . path ; if ( cookie . name ) jscookie . name = cookie . name ; if ( cookie . isSecure ) jscookie . secure = true ; if ( cookie . isHttpOnly ) jscookie . httponly = true ; if ( cookie . expiry < MAX_EXPIRY ) jscookie . expiry = cookie . expiry ; addCookieToHash ( jscookies , cookie . host , cookie . path , cookie . name , jscookie ) ; } window . cookies . push ( jscookies [ cookie . host ] [ cookie . path ] [ cookie . name ] ) ; } } } if ( ! window . cookies . length ) delete window . cookies ; } }
Serialize cookie data
 @param aWindows
        JS object containing window data references
        { id: winData, etc. }
Serialize cookie data @param aWindows JS object containing window data references { id : winData etc . }
// lazily build up a 3-dimensional hash, with // aHost, aPath, and aName as keys // MAX_EXPIRY should be 2^63-1, but JavaScript can't handle that precision // window._hosts will only have hosts with the right privacy rules, // so there is no need to do anything special with this call to // checkPrivacyLevel. // use the cookie's host, path, and name as keys into a hash, // to make sure we serialize each cookie only once // only add attributes with non-default values (saving a few bits) // don't include empty cookie sections