B2G/gecko/browser/components/sessionstore/src/SessionStore.jsm
/**
   * Restores the session state stored in _lastSessionState. This will attempt
   * to merge data into the current session. If a window was opened at startup
   * with pinned tab(s), then the remaining data from the previous session for
   * that window will be opened into that winddow. Otherwise new windows will
   * be opened.
   */
Restores the session state stored in _lastSessionState. This will attempt
to merge data into the current session. If a window was opened at startup
with pinned tab(s), then the remaining data from the previous session for
that window will be opened into that winddow. Otherwise new windows will
be opened.
Restores the session state stored in _lastSessionState . This will attempt to merge data into the current session . If a window was opened at startup with pinned tab ( s ) then the remaining data from the previous session for that window will be opened into that winddow . Otherwise new windows will be opened .
function ssi_restoreLastSession() {
    if (!this.canRestoreLastSession)
      throw (Components.returnCode = Cr.NS_ERROR_FAILURE);
    let windows = {};
    this._forEachBrowserWindow(function(aWindow) {
      if (aWindow.__SS_lastSessionWindowID)
        windows[aWindow.__SS_lastSessionWindowID] = aWindow;
    });
    let lastSessionState = this._lastSessionState;
    if (!lastSessionState.windows.length)
      throw (Components.returnCode = Cr.NS_ERROR_UNEXPECTED);
    this._restoreCount = lastSessionState.windows.length;
    this._browserSetState = true;
    let lastWindow = this._getMostRecentBrowserWindow();
    let canUseLastWindow = lastWindow &&
                           !lastWindow.__SS_lastSessionWindowID;
    let lastSessionFocusedWindow = null;
    this.windowToFocus = lastWindow;
    lastSessionState.windows.unshift(
      lastSessionState.windows.splice(lastSessionState.selectedWindow - 1, 1)[0]);
    for (let i = 0; i < lastSessionState.windows.length; i++) {
      let winState = lastSessionState.windows[i];
      let lastSessionWindowID = winState.__lastSessionWindowID;
      delete winState.__lastSessionWindowID;
      let windowToUse = windows[lastSessionWindowID];
      if (!windowToUse && canUseLastWindow) {
        windowToUse = lastWindow;
        canUseLastWindow = false;
      }
      let [canUseWindow, canOverwriteTabs] = this._prepWindowToRestoreInto(windowToUse);
      if (canUseWindow) {
        if (winState._closedTabs && winState._closedTabs.length) {
          let curWinState = this._windows[windowToUse.__SSi];
          curWinState._closedTabs = curWinState._closedTabs.concat(winState._closedTabs);
          curWinState._closedTabs.splice(this._prefBranch.getIntPref("sessionstore.max_tabs_undo"), curWinState._closedTabs.length);
        }
        this.restoreWindow(windowToUse, { windows: [winState] }, canOverwriteTabs, true);
        if (i == 0)
          lastSessionFocusedWindow = windowToUse;
        if (canOverwriteTabs && windowToUse == lastWindow)
          this.windowToFocus = lastSessionFocusedWindow;
      }
      else {
        let win = this._openWindowWithState({ windows: [winState] });
        if (i == 0)
          lastSessionFocusedWindow = win;
      }
    }
    if (lastSessionState._closedWindows) {
      this._closedWindows = this._closedWindows.concat(lastSessionState._closedWindows);
      this._capClosedWindows();
    }
    if (lastSessionState.scratchpads) {
      ScratchpadManager.restoreSession(lastSessionState.scratchpads);
    }
    this._recentCrashes = lastSessionState.session &&
                          lastSessionState.session.recentCrashes || 0;
    this._sessionStartTime = lastSessionState.session &&
                             lastSessionState.session.startTime ||
                             this._sessionStartTime;
    this._lastSessionState = null;
  }
function ssi_restoreLastSession ( ) { if ( ! this . canRestoreLastSession ) throw ( Components . returnCode = Cr . NS_ERROR_FAILURE ) ; let windows = { } ; this . _forEachBrowserWindow ( function ( aWindow ) { if ( aWindow . __SS_lastSessionWindowID ) windows [ aWindow . __SS_lastSessionWindowID ] = aWindow ; } ) ; let lastSessionState = this . _lastSessionState ; if ( ! lastSessionState . windows . length ) throw ( Components . returnCode = Cr . NS_ERROR_UNEXPECTED ) ; this . _restoreCount = lastSessionState . windows . length ; this . _browserSetState = true ; let lastWindow = this . _getMostRecentBrowserWindow ( ) ; let canUseLastWindow = lastWindow && ! lastWindow . __SS_lastSessionWindowID ; let lastSessionFocusedWindow = null ; this . windowToFocus = lastWindow ; lastSessionState . windows . unshift ( lastSessionState . windows . splice ( lastSessionState . selectedWindow - 1 , 1 ) [ 0 ] ) ; for ( let i = 0 ; i < lastSessionState . windows . length ; i ++ ) { let winState = lastSessionState . windows [ i ] ; let lastSessionWindowID = winState . __lastSessionWindowID ; delete winState . __lastSessionWindowID ; let windowToUse = windows [ lastSessionWindowID ] ; if ( ! windowToUse && canUseLastWindow ) { windowToUse = lastWindow ; canUseLastWindow = false ; } let [ canUseWindow , canOverwriteTabs ] = this . _prepWindowToRestoreInto ( windowToUse ) ; if ( canUseWindow ) { if ( winState . _closedTabs && winState . _closedTabs . length ) { let curWinState = this . _windows [ windowToUse . __SSi ] ; curWinState . _closedTabs = curWinState . _closedTabs . concat ( winState . _closedTabs ) ; curWinState . _closedTabs . splice ( this . _prefBranch . getIntPref ( "sessionstore.max_tabs_undo" ) , curWinState . _closedTabs . length ) ; } this . restoreWindow ( windowToUse , { windows : [ winState ] } , canOverwriteTabs , true ) ; if ( i == 0 ) lastSessionFocusedWindow = windowToUse ; if ( canOverwriteTabs && windowToUse == lastWindow ) this . windowToFocus = lastSessionFocusedWindow ; } else { let win = this . _openWindowWithState ( { windows : [ winState ] } ) ; if ( i == 0 ) lastSessionFocusedWindow = win ; } } if ( lastSessionState . _closedWindows ) { this . _closedWindows = this . _closedWindows . concat ( lastSessionState . _closedWindows ) ; this . _capClosedWindows ( ) ; } if ( lastSessionState . scratchpads ) { ScratchpadManager . restoreSession ( lastSessionState . scratchpads ) ; } this . _recentCrashes = lastSessionState . session && lastSessionState . session . recentCrashes || 0 ; this . _sessionStartTime = lastSessionState . session && lastSessionState . session . startTime || this . _sessionStartTime ; this . _lastSessionState = null ; }
Restores the session state stored in _lastSessionState.
Restores the session state stored in _lastSessionState .
// Use the public getter since it also checks PB mode // First collect each window with its id... // This shouldn't ever be the case... // We're technically doing a restore, so set things up so we send the // notification when we're done. We want to send "sessionstore-browser-state-restored". // We want to re-use the last opened window instead of opening a new one in // the case where it's "empty" and not associated with a window in the session. // We will do more processing via _prepWindowToRestoreInto if we need to use // the lastWindow. // move the last focused window to the start of the array so that we // minimize window movement (see bug 669272) // Restore into windows or open new ones as needed. // delete lastSessionWindowID so we don't add that to the window again // See if we can use an open window. First try one that is associated with // the state we're trying to restore and then fallback to the last selected // window. // If there's a window already open that we can restore into, use that // Since we're not overwriting existing tabs, we want to merge _closedTabs, // putting existing ones first. Then make sure we're respecting the max pref. // Restore into that window - pretend it's a followup since we'll already // have a focused window. //XXXzpao This is going to merge extData together (taking what was in //        winState over what is in the window already. The hack we have //        in _preWindowToRestoreInto will prevent most (all?) Panorama //        weirdness but we will still merge other extData. //        Bug 588217 should make this go away by merging the group data. // if we overwrote the tabs for our last focused window, we should // give focus to the window that had it in the previous session // Merge closed windows from this session with ones from last session // Set data that persists between sessions