B2G/gecko/browser/components/sessionstore/src/SessionStore.jsm
/**
   * This is going to take a state as provided at startup (via
   * nsISessionStartup.state) and split it into 2 parts. The first part
   * (defaultState) will be a state that should still be restored at startup,
   * while the second part (state) is a state that should be saved for later.
   * defaultState will be comprised of windows with only pinned tabs, extracted
   * from state. It will contain the cookies that go along with the history
   * entries in those tabs. It will also contain window position information.
   *
   * defaultState will be restored at startup. state will be placed into
   * this._lastSessionState and will be kept in case the user explicitly wants
   * to restore the previous session (publicly exposed as restoreLastSession).
   *
   * @param state
   *        The state, presumably from nsISessionStartup.state
   * @returns [defaultState, state]
   */
This is going to take a state as provided at startup (via
nsISessionStartup.state) and split it into 2 parts. The first part
(defaultState) will be a state that should still be restored at startup,
while the second part (state) is a state that should be saved for later.
defaultState will be comprised of windows with only pinned tabs, extracted
from state. It will contain the cookies that go along with the history
entries in those tabs. It will also contain window position information.

defaultState will be restored at startup. state will be placed into
this._lastSessionState and will be kept in case the user explicitly wants
to restore the previous session (publicly exposed as restoreLastSession).


This is going to take a state as provided at startup ( via nsISessionStartup . state ) and split it into 2 parts . The first part ( defaultState ) will be a state that should still be restored at startup while the second part ( state ) is a state that should be saved for later . defaultState will be comprised of windows with only pinned tabs extracted from state . It will contain the cookies that go along with the history entries in those tabs . It will also contain window position information . defaultState will be restored at startup . state will be placed into this . _lastSessionState and will be kept in case the user explicitly wants to restore the previous session ( publicly exposed as restoreLastSession ) .
function ssi_prepDataForDeferredRestore(state) {
    let defaultState = { windows: [], selectedWindow: 1 };
    state.selectedWindow = state.selectedWindow || 1;
    for (let wIndex = 0; wIndex < state.windows.length;) {
      let window = state.windows[wIndex];
      window.selected = window.selected || 1;
      let pinnedWindowState = { tabs: [], cookies: []};
      for (let tIndex = 0; tIndex < window.tabs.length;) {
        if (window.tabs[tIndex].pinned) {
          if (tIndex + 1 < window.selected)
            window.selected -= 1;
          else if (tIndex + 1 == window.selected)
            pinnedWindowState.selected = pinnedWindowState.tabs.length + 2;
          pinnedWindowState.tabs =
            pinnedWindowState.tabs.concat(window.tabs.splice(tIndex, 1));
          continue;
        }
        tIndex++;
      }
      if (pinnedWindowState.tabs.length) {
        WINDOW_ATTRIBUTES.forEach(function(attr) {
          if (attr in window) {
            pinnedWindowState[attr] = window[attr];
            delete window[attr];
          }
        });
        window.__lastSessionWindowID = pinnedWindowState.__lastSessionWindowID
                                     = "" + Date.now() + Math.random();
        this._splitCookiesFromWindow(window, pinnedWindowState);
        defaultState.windows.push(pinnedWindowState);
        if (!window.tabs.length) {
          if (wIndex + 1 <= state.selectedWindow)
            state.selectedWindow -= 1;
          else if (wIndex + 1 == state.selectedWindow)
            defaultState.selectedIndex = defaultState.windows.length + 1;
          state.windows.splice(wIndex, 1);
          continue;
        }
      }
      wIndex++;
    }
    return [defaultState, state];
  }
function ssi_prepDataForDeferredRestore ( state ) { let defaultState = { windows : [ ] , selectedWindow : 1 } ; state . selectedWindow = state . selectedWindow || 1 ; for ( let wIndex = 0 ; wIndex < state . windows . length ; ) { let window = state . windows [ wIndex ] ; window . selected = window . selected || 1 ; let pinnedWindowState = { tabs : [ ] , cookies : [ ] } ; for ( let tIndex = 0 ; tIndex < window . tabs . length ; ) { if ( window . tabs [ tIndex ] . pinned ) { if ( tIndex + 1 < window . selected ) window . selected -= 1 ; else if ( tIndex + 1 == window . selected ) pinnedWindowState . selected = pinnedWindowState . tabs . length + 2 ; pinnedWindowState . tabs = pinnedWindowState . tabs . concat ( window . tabs . splice ( tIndex , 1 ) ) ; continue ; } tIndex ++ ; } if ( pinnedWindowState . tabs . length ) { WINDOW_ATTRIBUTES . forEach ( function ( attr ) { if ( attr in window ) { pinnedWindowState [ attr ] = window [ attr ] ; delete window [ attr ] ; } } ) ; window . __lastSessionWindowID = pinnedWindowState . __lastSessionWindowID = "" + Date . now ( ) + Math . random ( ) ; this . _splitCookiesFromWindow ( window , pinnedWindowState ) ; defaultState . windows . push ( pinnedWindowState ) ; if ( ! window . tabs . length ) { if ( wIndex + 1 <= state . selectedWindow ) state . selectedWindow -= 1 ; else if ( wIndex + 1 == state . selectedWindow ) defaultState . selectedIndex = defaultState . windows . length + 1 ; state . windows . splice ( wIndex , 1 ) ; continue ; } } wIndex ++ ; } return [ defaultState , state ] ; }
This is going to take a state as provided at startup (via
 nsISessionStartup.state) and split it into 2 parts.
This is going to take a state as provided at startup ( via nsISessionStartup . state ) and split it into 2 parts .
// Look at each window, remove pinned tabs, adjust selectedindex, // remove window if necessary. // We're going to put the state of the window into this object // Adjust window.selected // + 2 because the tab isn't actually in the array yet // Now add the pinned tab to our window // We don't want to increment tIndex here. // At this point the window in the state object has been modified (or not) // We want to build the rest of this new window object if we have pinnedTabs. // First get the other attributes off the window // We're just copying position data into the pinned window. // Not copying over: // - _closedTabs // - extData // - isPopup // - hidden // Assign a unique ID to correlate the window to be opened with the // remaining data // Extract the cookies that belong with each pinned tab // Actually add this window to our defaultState // Remove the window from the state if it doesn't have any tabs // We don't want to increment wIndex here.