michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = ["SessionStore"]; michael@0: michael@0: const Cu = Components.utils; michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cr = Components.results; michael@0: michael@0: const STATE_STOPPED = 0; michael@0: const STATE_RUNNING = 1; michael@0: const STATE_QUITTING = -1; michael@0: michael@0: const TAB_STATE_NEEDS_RESTORE = 1; michael@0: const TAB_STATE_RESTORING = 2; michael@0: michael@0: const NOTIFY_WINDOWS_RESTORED = "sessionstore-windows-restored"; michael@0: const NOTIFY_BROWSER_STATE_RESTORED = "sessionstore-browser-state-restored"; michael@0: const NOTIFY_LAST_SESSION_CLEARED = "sessionstore-last-session-cleared"; michael@0: michael@0: const NOTIFY_TAB_RESTORED = "sessionstore-debug-tab-restored"; // WARNING: debug-only michael@0: michael@0: // Maximum number of tabs to restore simultaneously. Previously controlled by michael@0: // the browser.sessionstore.max_concurrent_tabs pref. michael@0: const MAX_CONCURRENT_TAB_RESTORES = 3; michael@0: michael@0: // global notifications observed michael@0: const OBSERVING = [ michael@0: "domwindowopened", "domwindowclosed", michael@0: "quit-application-requested", "quit-application-granted", michael@0: "browser-lastwindow-close-granted", michael@0: "quit-application", "browser:purge-session-history", michael@0: "browser:purge-domain-data", michael@0: "gather-telemetry", michael@0: ]; michael@0: michael@0: // XUL Window properties to (re)store michael@0: // Restored in restoreDimensions() michael@0: const WINDOW_ATTRIBUTES = ["width", "height", "screenX", "screenY", "sizemode"]; michael@0: michael@0: // Hideable window features to (re)store michael@0: // Restored in restoreWindowFeatures() michael@0: const WINDOW_HIDEABLE_FEATURES = [ michael@0: "menubar", "toolbar", "locationbar", "personalbar", "statusbar", "scrollbars" michael@0: ]; michael@0: michael@0: const MESSAGES = [ michael@0: // The content script gives us a reference to an object that performs michael@0: // synchronous collection of session data. michael@0: "SessionStore:setupSyncHandler", michael@0: michael@0: // The content script sends us data that has been invalidated and needs to michael@0: // be saved to disk. michael@0: "SessionStore:update", michael@0: michael@0: // The restoreHistory code has run. This is a good time to run SSTabRestoring. michael@0: "SessionStore:restoreHistoryComplete", michael@0: michael@0: // The load for the restoring tab has begun. We update the URL bar at this michael@0: // time; if we did it before, the load would overwrite it. michael@0: "SessionStore:restoreTabContentStarted", michael@0: michael@0: // All network loads for a restoring tab are done, so we should consider michael@0: // restoring another tab in the queue. michael@0: "SessionStore:restoreTabContentComplete", michael@0: michael@0: // The document has been restored, so the restore is done. We trigger michael@0: // SSTabRestored at this time. michael@0: "SessionStore:restoreDocumentComplete", michael@0: michael@0: // A tab that is being restored was reloaded. We call restoreTabContent to michael@0: // finish restoring it right away. michael@0: "SessionStore:reloadPendingTab", michael@0: ]; michael@0: michael@0: // These are tab events that we listen to. michael@0: const TAB_EVENTS = [ michael@0: "TabOpen", "TabClose", "TabSelect", "TabShow", "TabHide", "TabPinned", michael@0: "TabUnpinned" michael@0: ]; michael@0: michael@0: // The number of milliseconds in a day michael@0: const MS_PER_DAY = 1000.0 * 60.0 * 60.0 * 24.0; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm", this); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm", this); michael@0: Cu.import("resource://gre/modules/TelemetryTimestamps.jsm", this); michael@0: Cu.import("resource://gre/modules/TelemetryStopwatch.jsm", this); michael@0: Cu.import("resource://gre/modules/osfile.jsm", this); michael@0: Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm", this); michael@0: Cu.import("resource://gre/modules/Promise.jsm", this); michael@0: Cu.import("resource://gre/modules/Task.jsm", this); michael@0: michael@0: XPCOMUtils.defineLazyServiceGetter(this, "gSessionStartup", michael@0: "@mozilla.org/browser/sessionstartup;1", "nsISessionStartup"); michael@0: XPCOMUtils.defineLazyServiceGetter(this, "gScreenManager", michael@0: "@mozilla.org/gfx/screenmanager;1", "nsIScreenManager"); michael@0: XPCOMUtils.defineLazyServiceGetter(this, "Telemetry", michael@0: "@mozilla.org/base/telemetry;1", "nsITelemetry"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "console", michael@0: "resource://gre/modules/devtools/Console.jsm"); michael@0: XPCOMUtils.defineLazyModuleGetter(this, "RecentWindow", michael@0: "resource:///modules/RecentWindow.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "GlobalState", michael@0: "resource:///modules/sessionstore/GlobalState.jsm"); michael@0: XPCOMUtils.defineLazyModuleGetter(this, "PrivacyFilter", michael@0: "resource:///modules/sessionstore/PrivacyFilter.jsm"); michael@0: XPCOMUtils.defineLazyModuleGetter(this, "ScratchpadManager", michael@0: "resource:///modules/devtools/scratchpad-manager.jsm"); michael@0: XPCOMUtils.defineLazyModuleGetter(this, "SessionSaver", michael@0: "resource:///modules/sessionstore/SessionSaver.jsm"); michael@0: XPCOMUtils.defineLazyModuleGetter(this, "SessionCookies", michael@0: "resource:///modules/sessionstore/SessionCookies.jsm"); michael@0: XPCOMUtils.defineLazyModuleGetter(this, "SessionFile", michael@0: "resource:///modules/sessionstore/SessionFile.jsm"); michael@0: XPCOMUtils.defineLazyModuleGetter(this, "TabAttributes", michael@0: "resource:///modules/sessionstore/TabAttributes.jsm"); michael@0: XPCOMUtils.defineLazyModuleGetter(this, "TabState", michael@0: "resource:///modules/sessionstore/TabState.jsm"); michael@0: XPCOMUtils.defineLazyModuleGetter(this, "TabStateCache", michael@0: "resource:///modules/sessionstore/TabStateCache.jsm"); michael@0: XPCOMUtils.defineLazyModuleGetter(this, "Utils", michael@0: "resource:///modules/sessionstore/Utils.jsm"); michael@0: michael@0: /** michael@0: * |true| if we are in debug mode, |false| otherwise. michael@0: * Debug mode is controlled by preference browser.sessionstore.debug michael@0: */ michael@0: let gDebuggingEnabled = false; michael@0: function debug(aMsg) { michael@0: if (gDebuggingEnabled) { michael@0: aMsg = ("SessionStore: " + aMsg).replace(/\S{80}/g, "$&\n"); michael@0: Services.console.logStringMessage(aMsg); michael@0: } michael@0: } michael@0: michael@0: this.SessionStore = { michael@0: get promiseInitialized() { michael@0: return SessionStoreInternal.promiseInitialized; michael@0: }, michael@0: michael@0: get canRestoreLastSession() { michael@0: return SessionStoreInternal.canRestoreLastSession; michael@0: }, michael@0: michael@0: set canRestoreLastSession(val) { michael@0: SessionStoreInternal.canRestoreLastSession = val; michael@0: }, michael@0: michael@0: init: function ss_init() { michael@0: SessionStoreInternal.init(); michael@0: }, michael@0: michael@0: getBrowserState: function ss_getBrowserState() { michael@0: return SessionStoreInternal.getBrowserState(); michael@0: }, michael@0: michael@0: setBrowserState: function ss_setBrowserState(aState) { michael@0: SessionStoreInternal.setBrowserState(aState); michael@0: }, michael@0: michael@0: getWindowState: function ss_getWindowState(aWindow) { michael@0: return SessionStoreInternal.getWindowState(aWindow); michael@0: }, michael@0: michael@0: setWindowState: function ss_setWindowState(aWindow, aState, aOverwrite) { michael@0: SessionStoreInternal.setWindowState(aWindow, aState, aOverwrite); michael@0: }, michael@0: michael@0: getTabState: function ss_getTabState(aTab) { michael@0: return SessionStoreInternal.getTabState(aTab); michael@0: }, michael@0: michael@0: setTabState: function ss_setTabState(aTab, aState) { michael@0: SessionStoreInternal.setTabState(aTab, aState); michael@0: }, michael@0: michael@0: duplicateTab: function ss_duplicateTab(aWindow, aTab, aDelta = 0) { michael@0: return SessionStoreInternal.duplicateTab(aWindow, aTab, aDelta); michael@0: }, michael@0: michael@0: getClosedTabCount: function ss_getClosedTabCount(aWindow) { michael@0: return SessionStoreInternal.getClosedTabCount(aWindow); michael@0: }, michael@0: michael@0: getClosedTabData: function ss_getClosedTabDataAt(aWindow) { michael@0: return SessionStoreInternal.getClosedTabData(aWindow); michael@0: }, michael@0: michael@0: undoCloseTab: function ss_undoCloseTab(aWindow, aIndex) { michael@0: return SessionStoreInternal.undoCloseTab(aWindow, aIndex); michael@0: }, michael@0: michael@0: forgetClosedTab: function ss_forgetClosedTab(aWindow, aIndex) { michael@0: return SessionStoreInternal.forgetClosedTab(aWindow, aIndex); michael@0: }, michael@0: michael@0: getClosedWindowCount: function ss_getClosedWindowCount() { michael@0: return SessionStoreInternal.getClosedWindowCount(); michael@0: }, michael@0: michael@0: getClosedWindowData: function ss_getClosedWindowData() { michael@0: return SessionStoreInternal.getClosedWindowData(); michael@0: }, michael@0: michael@0: undoCloseWindow: function ss_undoCloseWindow(aIndex) { michael@0: return SessionStoreInternal.undoCloseWindow(aIndex); michael@0: }, michael@0: michael@0: forgetClosedWindow: function ss_forgetClosedWindow(aIndex) { michael@0: return SessionStoreInternal.forgetClosedWindow(aIndex); michael@0: }, michael@0: michael@0: getWindowValue: function ss_getWindowValue(aWindow, aKey) { michael@0: return SessionStoreInternal.getWindowValue(aWindow, aKey); michael@0: }, michael@0: michael@0: setWindowValue: function ss_setWindowValue(aWindow, aKey, aStringValue) { michael@0: SessionStoreInternal.setWindowValue(aWindow, aKey, aStringValue); michael@0: }, michael@0: michael@0: deleteWindowValue: function ss_deleteWindowValue(aWindow, aKey) { michael@0: SessionStoreInternal.deleteWindowValue(aWindow, aKey); michael@0: }, michael@0: michael@0: getTabValue: function ss_getTabValue(aTab, aKey) { michael@0: return SessionStoreInternal.getTabValue(aTab, aKey); michael@0: }, michael@0: michael@0: setTabValue: function ss_setTabValue(aTab, aKey, aStringValue) { michael@0: SessionStoreInternal.setTabValue(aTab, aKey, aStringValue); michael@0: }, michael@0: michael@0: deleteTabValue: function ss_deleteTabValue(aTab, aKey) { michael@0: SessionStoreInternal.deleteTabValue(aTab, aKey); michael@0: }, michael@0: michael@0: getGlobalValue: function ss_getGlobalValue(aKey) { michael@0: return SessionStoreInternal.getGlobalValue(aKey); michael@0: }, michael@0: michael@0: setGlobalValue: function ss_setGlobalValue(aKey, aStringValue) { michael@0: SessionStoreInternal.setGlobalValue(aKey, aStringValue); michael@0: }, michael@0: michael@0: deleteGlobalValue: function ss_deleteGlobalValue(aKey) { michael@0: SessionStoreInternal.deleteGlobalValue(aKey); michael@0: }, michael@0: michael@0: persistTabAttribute: function ss_persistTabAttribute(aName) { michael@0: SessionStoreInternal.persistTabAttribute(aName); michael@0: }, michael@0: michael@0: restoreLastSession: function ss_restoreLastSession() { michael@0: SessionStoreInternal.restoreLastSession(); michael@0: }, michael@0: michael@0: getCurrentState: function (aUpdateAll) { michael@0: return SessionStoreInternal.getCurrentState(aUpdateAll); michael@0: }, michael@0: michael@0: /** michael@0: * Backstage pass to implementation details, used for testing purpose. michael@0: * Controlled by preference "browser.sessionstore.testmode". michael@0: */ michael@0: get _internal() { michael@0: if (Services.prefs.getBoolPref("browser.sessionstore.debug")) { michael@0: return SessionStoreInternal; michael@0: } michael@0: return undefined; michael@0: }, michael@0: }; michael@0: michael@0: // Freeze the SessionStore object. We don't want anyone to modify it. michael@0: Object.freeze(SessionStore); michael@0: michael@0: let SessionStoreInternal = { michael@0: QueryInterface: XPCOMUtils.generateQI([ michael@0: Ci.nsIDOMEventListener, michael@0: Ci.nsIObserver, michael@0: Ci.nsISupportsWeakReference michael@0: ]), michael@0: michael@0: // set default load state michael@0: _loadState: STATE_STOPPED, michael@0: michael@0: _globalState: new GlobalState(), michael@0: michael@0: // During the initial restore and setBrowserState calls tracks the number of michael@0: // windows yet to be restored michael@0: _restoreCount: -1, michael@0: michael@0: // This number gets incremented each time we start to restore a tab. michael@0: _nextRestoreEpoch: 1, michael@0: michael@0: // For each element being restored, records the current epoch. michael@0: _browserEpochs: new WeakMap(), michael@0: michael@0: // whether a setBrowserState call is in progress michael@0: _browserSetState: false, michael@0: michael@0: // time in milliseconds when the session was started (saved across sessions), michael@0: // defaults to now if no session was restored or timestamp doesn't exist michael@0: _sessionStartTime: Date.now(), michael@0: michael@0: // states for all currently opened windows michael@0: _windows: {}, michael@0: michael@0: // counter for creating unique window IDs michael@0: _nextWindowID: 0, michael@0: michael@0: // states for all recently closed windows michael@0: _closedWindows: [], michael@0: michael@0: // collection of session states yet to be restored michael@0: _statesToRestore: {}, michael@0: michael@0: // counts the number of crashes since the last clean start michael@0: _recentCrashes: 0, michael@0: michael@0: // whether the last window was closed and should be restored michael@0: _restoreLastWindow: false, michael@0: michael@0: // number of tabs currently restoring michael@0: _tabsRestoringCount: 0, michael@0: michael@0: // When starting Firefox with a single private window, this is the place michael@0: // where we keep the session we actually wanted to restore in case the user michael@0: // decides to later open a non-private window as well. michael@0: _deferredInitialState: null, michael@0: michael@0: // A promise resolved once initialization is complete michael@0: _deferredInitialized: Promise.defer(), michael@0: michael@0: // Whether session has been initialized michael@0: _sessionInitialized: false, michael@0: michael@0: // Promise that is resolved when we're ready to initialize michael@0: // and restore the session. michael@0: _promiseReadyForInitialization: null, michael@0: michael@0: /** michael@0: * A promise fulfilled once initialization is complete. michael@0: */ michael@0: get promiseInitialized() { michael@0: return this._deferredInitialized.promise; michael@0: }, michael@0: michael@0: get canRestoreLastSession() { michael@0: return LastSession.canRestore; michael@0: }, michael@0: michael@0: set canRestoreLastSession(val) { michael@0: // Cheat a bit; only allow false. michael@0: if (!val) { michael@0: LastSession.clear(); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Initialize the sessionstore service. michael@0: */ michael@0: init: function () { michael@0: if (this._initialized) { michael@0: throw new Error("SessionStore.init() must only be called once!"); michael@0: } michael@0: michael@0: TelemetryTimestamps.add("sessionRestoreInitialized"); michael@0: OBSERVING.forEach(function(aTopic) { michael@0: Services.obs.addObserver(this, aTopic, true); michael@0: }, this); michael@0: michael@0: this._initPrefs(); michael@0: this._initialized = true; michael@0: }, michael@0: michael@0: /** michael@0: * Initialize the session using the state provided by SessionStartup michael@0: */ michael@0: initSession: function () { michael@0: let state; michael@0: let ss = gSessionStartup; michael@0: michael@0: try { michael@0: if (ss.doRestore() || michael@0: ss.sessionType == Ci.nsISessionStartup.DEFER_SESSION) michael@0: state = ss.state; michael@0: } michael@0: catch(ex) { dump(ex + "\n"); } // no state to restore, which is ok michael@0: michael@0: if (state) { michael@0: try { michael@0: // If we're doing a DEFERRED session, then we want to pull pinned tabs michael@0: // out so they can be restored. michael@0: if (ss.sessionType == Ci.nsISessionStartup.DEFER_SESSION) { michael@0: let [iniState, remainingState] = this._prepDataForDeferredRestore(state); michael@0: // If we have a iniState with windows, that means that we have windows michael@0: // with app tabs to restore. michael@0: if (iniState.windows.length) michael@0: state = iniState; michael@0: else michael@0: state = null; michael@0: michael@0: if (remainingState.windows.length) { michael@0: LastSession.setState(remainingState); michael@0: } michael@0: } michael@0: else { michael@0: // Get the last deferred session in case the user still wants to michael@0: // restore it michael@0: LastSession.setState(state.lastSessionState); michael@0: michael@0: if (ss.previousSessionCrashed) { michael@0: this._recentCrashes = (state.session && michael@0: state.session.recentCrashes || 0) + 1; michael@0: michael@0: if (this._needsRestorePage(state, this._recentCrashes)) { michael@0: // replace the crashed session with a restore-page-only session michael@0: let pageData = { michael@0: url: "about:sessionrestore", michael@0: formdata: { michael@0: id: { "sessionData": state }, michael@0: xpath: {} michael@0: } michael@0: }; michael@0: state = { windows: [{ tabs: [{ entries: [pageData] }] }] }; michael@0: } else if (this._hasSingleTabWithURL(state.windows, michael@0: "about:welcomeback")) { michael@0: // On a single about:welcomeback URL that crashed, replace about:welcomeback michael@0: // with about:sessionrestore, to make clear to the user that we crashed. michael@0: state.windows[0].tabs[0].entries[0].url = "about:sessionrestore"; michael@0: } michael@0: } michael@0: michael@0: // Update the session start time using the restored session state. michael@0: this._updateSessionStartTime(state); michael@0: michael@0: // make sure that at least the first window doesn't have anything hidden michael@0: delete state.windows[0].hidden; michael@0: // Since nothing is hidden in the first window, it cannot be a popup michael@0: delete state.windows[0].isPopup; michael@0: // We don't want to minimize and then open a window at startup. michael@0: if (state.windows[0].sizemode == "minimized") michael@0: state.windows[0].sizemode = "normal"; michael@0: // clear any lastSessionWindowID attributes since those don't matter michael@0: // during normal restore michael@0: state.windows.forEach(function(aWindow) { michael@0: delete aWindow.__lastSessionWindowID; michael@0: }); michael@0: } michael@0: } michael@0: catch (ex) { debug("The session file is invalid: " + ex); } michael@0: } michael@0: michael@0: // at this point, we've as good as resumed the session, so we can michael@0: // clear the resume_session_once flag, if it's set michael@0: if (this._loadState != STATE_QUITTING && michael@0: this._prefBranch.getBoolPref("sessionstore.resume_session_once")) michael@0: this._prefBranch.setBoolPref("sessionstore.resume_session_once", false); michael@0: michael@0: this._performUpgradeBackup(); michael@0: michael@0: return state; michael@0: }, michael@0: michael@0: /** michael@0: * If this is the first time we launc this build of Firefox, michael@0: * backup sessionstore.js. michael@0: */ michael@0: _performUpgradeBackup: function ssi_performUpgradeBackup() { michael@0: // Perform upgrade backup, if necessary michael@0: const PREF_UPGRADE = "sessionstore.upgradeBackup.latestBuildID"; michael@0: michael@0: let buildID = Services.appinfo.platformBuildID; michael@0: let latestBackup = this._prefBranch.getCharPref(PREF_UPGRADE); michael@0: if (latestBackup == buildID) { michael@0: return Promise.resolve(); michael@0: } michael@0: return Task.spawn(function task() { michael@0: try { michael@0: // Perform background backup michael@0: yield SessionFile.createBackupCopy("-" + buildID); michael@0: michael@0: this._prefBranch.setCharPref(PREF_UPGRADE, buildID); michael@0: michael@0: // In case of success, remove previous backup. michael@0: yield SessionFile.removeBackupCopy("-" + latestBackup); michael@0: } catch (ex) { michael@0: debug("Could not perform upgrade backup " + ex); michael@0: debug(ex.stack); michael@0: } michael@0: }.bind(this)); michael@0: }, michael@0: michael@0: _initPrefs : function() { michael@0: this._prefBranch = Services.prefs.getBranch("browser."); michael@0: michael@0: gDebuggingEnabled = this._prefBranch.getBoolPref("sessionstore.debug"); michael@0: michael@0: Services.prefs.addObserver("browser.sessionstore.debug", () => { michael@0: gDebuggingEnabled = this._prefBranch.getBoolPref("sessionstore.debug"); michael@0: }, false); michael@0: michael@0: this._max_tabs_undo = this._prefBranch.getIntPref("sessionstore.max_tabs_undo"); michael@0: this._prefBranch.addObserver("sessionstore.max_tabs_undo", this, true); michael@0: michael@0: this._max_windows_undo = this._prefBranch.getIntPref("sessionstore.max_windows_undo"); michael@0: this._prefBranch.addObserver("sessionstore.max_windows_undo", this, true); michael@0: }, michael@0: michael@0: /** michael@0: * Called on application shutdown, after notifications: michael@0: * quit-application-granted, quit-application michael@0: */ michael@0: _uninit: function ssi_uninit() { michael@0: if (!this._initialized) { michael@0: throw new Error("SessionStore is not initialized."); michael@0: } michael@0: michael@0: // save all data for session resuming michael@0: if (this._sessionInitialized) { michael@0: SessionSaver.run(); michael@0: } michael@0: michael@0: // clear out priority queue in case it's still holding refs michael@0: TabRestoreQueue.reset(); michael@0: michael@0: // Make sure to cancel pending saves. michael@0: SessionSaver.cancel(); michael@0: }, michael@0: michael@0: /** michael@0: * Handle notifications michael@0: */ michael@0: observe: function ssi_observe(aSubject, aTopic, aData) { michael@0: switch (aTopic) { michael@0: case "domwindowopened": // catch new windows michael@0: this.onOpen(aSubject); michael@0: break; michael@0: case "domwindowclosed": // catch closed windows michael@0: this.onClose(aSubject); michael@0: break; michael@0: case "quit-application-requested": michael@0: this.onQuitApplicationRequested(); michael@0: break; michael@0: case "quit-application-granted": michael@0: this.onQuitApplicationGranted(); michael@0: break; michael@0: case "browser-lastwindow-close-granted": michael@0: this.onLastWindowCloseGranted(); michael@0: break; michael@0: case "quit-application": michael@0: this.onQuitApplication(aData); michael@0: break; michael@0: case "browser:purge-session-history": // catch sanitization michael@0: this.onPurgeSessionHistory(); michael@0: break; michael@0: case "browser:purge-domain-data": michael@0: this.onPurgeDomainData(aData); michael@0: break; michael@0: case "nsPref:changed": // catch pref changes michael@0: this.onPrefChange(aData); michael@0: break; michael@0: case "gather-telemetry": michael@0: this.onGatherTelemetry(); michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * This method handles incoming messages sent by the session store content michael@0: * script and thus enables communication with OOP tabs. michael@0: */ michael@0: receiveMessage: function ssi_receiveMessage(aMessage) { michael@0: var browser = aMessage.target; michael@0: var win = browser.ownerDocument.defaultView; michael@0: let tab = this._getTabForBrowser(browser); michael@0: if (!tab) { michael@0: // Ignore messages from elements that are not tabs. michael@0: return; michael@0: } michael@0: michael@0: switch (aMessage.name) { michael@0: case "SessionStore:setupSyncHandler": michael@0: TabState.setSyncHandler(browser, aMessage.objects.handler); michael@0: break; michael@0: case "SessionStore:update": michael@0: this.recordTelemetry(aMessage.data.telemetry); michael@0: TabState.update(browser, aMessage.data); michael@0: this.saveStateDelayed(win); michael@0: break; michael@0: case "SessionStore:restoreHistoryComplete": michael@0: if (this.isCurrentEpoch(browser, aMessage.data.epoch)) { michael@0: // Notify the tabbrowser that the tab chrome has been restored. michael@0: let tabData = browser.__SS_data; michael@0: michael@0: // wall-paper fix for bug 439675: make sure that the URL to be loaded michael@0: // is always visible in the address bar michael@0: let activePageData = tabData.entries[tabData.index - 1] || null; michael@0: let uri = activePageData ? activePageData.url || null : null; michael@0: browser.userTypedValue = uri; michael@0: michael@0: // If the page has a title, set it. michael@0: if (activePageData) { michael@0: if (activePageData.title) { michael@0: tab.label = activePageData.title; michael@0: tab.crop = "end"; michael@0: } else if (activePageData.url != "about:blank") { michael@0: tab.label = activePageData.url; michael@0: tab.crop = "center"; michael@0: } michael@0: } michael@0: michael@0: // Restore the tab icon. michael@0: if ("image" in tabData) { michael@0: win.gBrowser.setIcon(tab, tabData.image); michael@0: } michael@0: michael@0: let event = win.document.createEvent("Events"); michael@0: event.initEvent("SSTabRestoring", true, false); michael@0: tab.dispatchEvent(event); michael@0: } michael@0: break; michael@0: case "SessionStore:restoreTabContentStarted": michael@0: if (this.isCurrentEpoch(browser, aMessage.data.epoch)) { michael@0: // If the user was typing into the URL bar when we crashed, but hadn't hit michael@0: // enter yet, then we just need to write that value to the URL bar without michael@0: // loading anything. This must happen after the load, since it will clear michael@0: // userTypedValue. michael@0: let tabData = browser.__SS_data; michael@0: if (tabData.userTypedValue && !tabData.userTypedClear) { michael@0: browser.userTypedValue = tabData.userTypedValue; michael@0: win.URLBarSetURI(); michael@0: } michael@0: } michael@0: break; michael@0: case "SessionStore:restoreTabContentComplete": michael@0: if (this.isCurrentEpoch(browser, aMessage.data.epoch)) { michael@0: // This callback is used exclusively by tests that want to michael@0: // monitor the progress of network loads. michael@0: if (gDebuggingEnabled) { michael@0: Services.obs.notifyObservers(browser, NOTIFY_TAB_RESTORED, null); michael@0: } michael@0: michael@0: if (tab) { michael@0: SessionStoreInternal._resetLocalTabRestoringState(tab); michael@0: SessionStoreInternal.restoreNextTab(); michael@0: } michael@0: } michael@0: break; michael@0: case "SessionStore:restoreDocumentComplete": michael@0: if (this.isCurrentEpoch(browser, aMessage.data.epoch)) { michael@0: // Document has been restored. Delete all the state associated michael@0: // with it and trigger SSTabRestored. michael@0: let tab = browser.__SS_restore_tab; michael@0: michael@0: delete browser.__SS_restore_data; michael@0: delete browser.__SS_restore_tab; michael@0: delete browser.__SS_data; michael@0: michael@0: this._sendTabRestoredNotification(tab); michael@0: } michael@0: break; michael@0: case "SessionStore:reloadPendingTab": michael@0: if (this.isCurrentEpoch(browser, aMessage.data.epoch)) { michael@0: if (tab && browser.__SS_restoreState == TAB_STATE_NEEDS_RESTORE) { michael@0: this.restoreTabContent(tab); michael@0: } michael@0: } michael@0: break; michael@0: default: michael@0: debug("received unknown message '" + aMessage.name + "'"); michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Record telemetry measurements stored in an object. michael@0: * @param telemetry michael@0: * {histogramID: value, ...} An object mapping histogramIDs to the michael@0: * value to be recorded for that ID, michael@0: */ michael@0: recordTelemetry: function (telemetry) { michael@0: for (let histogramId in telemetry){ michael@0: Telemetry.getHistogramById(histogramId).add(telemetry[histogramId]); michael@0: } michael@0: }, michael@0: michael@0: /* ........ Window Event Handlers .............. */ michael@0: michael@0: /** michael@0: * Implement nsIDOMEventListener for handling various window and tab events michael@0: */ michael@0: handleEvent: function ssi_handleEvent(aEvent) { michael@0: var win = aEvent.currentTarget.ownerDocument.defaultView; michael@0: let browser; michael@0: switch (aEvent.type) { michael@0: case "TabOpen": michael@0: this.onTabAdd(win, aEvent.originalTarget); michael@0: break; michael@0: case "TabClose": michael@0: // aEvent.detail determines if the tab was closed by moving to a different window michael@0: if (!aEvent.detail) michael@0: this.onTabClose(win, aEvent.originalTarget); michael@0: this.onTabRemove(win, aEvent.originalTarget); michael@0: break; michael@0: case "TabSelect": michael@0: this.onTabSelect(win); michael@0: break; michael@0: case "TabShow": michael@0: this.onTabShow(win, aEvent.originalTarget); michael@0: break; michael@0: case "TabHide": michael@0: this.onTabHide(win, aEvent.originalTarget); michael@0: break; michael@0: case "TabPinned": michael@0: case "TabUnpinned": michael@0: this.saveStateDelayed(win); michael@0: break; michael@0: } michael@0: this._clearRestoringWindows(); michael@0: }, michael@0: michael@0: /** michael@0: * Generate a unique window identifier michael@0: * @return string michael@0: * A unique string to identify a window michael@0: */ michael@0: _generateWindowID: function ssi_generateWindowID() { michael@0: return "window" + (this._nextWindowID++); michael@0: }, michael@0: michael@0: /** michael@0: * If it's the first window load since app start... michael@0: * - determine if we're reloading after a crash or a forced-restart michael@0: * - restore window state michael@0: * - restart downloads michael@0: * Set up event listeners for this window's tabs michael@0: * @param aWindow michael@0: * Window reference michael@0: * @param aInitialState michael@0: * The initial state to be loaded after startup (optional) michael@0: */ michael@0: onLoad: function ssi_onLoad(aWindow, aInitialState = null) { michael@0: // return if window has already been initialized michael@0: if (aWindow && aWindow.__SSi && this._windows[aWindow.__SSi]) michael@0: return; michael@0: michael@0: // ignore windows opened while shutting down michael@0: if (this._loadState == STATE_QUITTING) michael@0: return; michael@0: michael@0: // Assign the window a unique identifier we can use to reference michael@0: // internal data about the window. michael@0: aWindow.__SSi = this._generateWindowID(); michael@0: michael@0: let mm = aWindow.messageManager; michael@0: MESSAGES.forEach(msg => mm.addMessageListener(msg, this)); michael@0: michael@0: // Load the frame script after registering listeners. michael@0: mm.loadFrameScript("chrome://browser/content/content-sessionStore.js", true); michael@0: michael@0: // and create its data object michael@0: this._windows[aWindow.__SSi] = { tabs: [], selected: 0, _closedTabs: [], busy: false }; michael@0: michael@0: let isPrivateWindow = false; michael@0: if (PrivateBrowsingUtils.isWindowPrivate(aWindow)) michael@0: this._windows[aWindow.__SSi].isPrivate = isPrivateWindow = true; michael@0: if (!this._isWindowLoaded(aWindow)) michael@0: this._windows[aWindow.__SSi]._restoring = true; michael@0: if (!aWindow.toolbar.visible) michael@0: this._windows[aWindow.__SSi].isPopup = true; michael@0: michael@0: // perform additional initialization when the first window is loading michael@0: if (this._loadState == STATE_STOPPED) { michael@0: this._loadState = STATE_RUNNING; michael@0: SessionSaver.updateLastSaveTime(); michael@0: michael@0: // restore a crashed session resp. resume the last session if requested michael@0: if (aInitialState) { michael@0: if (isPrivateWindow) { michael@0: // We're starting with a single private window. Save the state we michael@0: // actually wanted to restore so that we can do it later in case michael@0: // the user opens another, non-private window. michael@0: this._deferredInitialState = gSessionStartup.state; michael@0: michael@0: // Nothing to restore now, notify observers things are complete. michael@0: Services.obs.notifyObservers(null, NOTIFY_WINDOWS_RESTORED, ""); michael@0: } else { michael@0: TelemetryTimestamps.add("sessionRestoreRestoring"); michael@0: this._restoreCount = aInitialState.windows ? aInitialState.windows.length : 0; michael@0: michael@0: // global data must be restored before restoreWindow is called so that michael@0: // it happens before observers are notified michael@0: this._globalState.setFromState(aInitialState); michael@0: michael@0: let overwrite = this._isCmdLineEmpty(aWindow, aInitialState); michael@0: let options = {firstWindow: true, overwriteTabs: overwrite}; michael@0: this.restoreWindow(aWindow, aInitialState, options); michael@0: } michael@0: } michael@0: else { michael@0: // Nothing to restore, notify observers things are complete. michael@0: Services.obs.notifyObservers(null, NOTIFY_WINDOWS_RESTORED, ""); michael@0: michael@0: // The next delayed save request should execute immediately. michael@0: SessionSaver.clearLastSaveTime(); michael@0: } michael@0: } michael@0: // this window was opened by _openWindowWithState michael@0: else if (!this._isWindowLoaded(aWindow)) { michael@0: let state = this._statesToRestore[aWindow.__SS_restoreID]; michael@0: let options = {overwriteTabs: true, isFollowUp: state.windows.length == 1}; michael@0: this.restoreWindow(aWindow, state, options); michael@0: } michael@0: // The user opened another, non-private window after starting up with michael@0: // a single private one. Let's restore the session we actually wanted to michael@0: // restore at startup. michael@0: else if (this._deferredInitialState && !isPrivateWindow && michael@0: aWindow.toolbar.visible) { michael@0: michael@0: // global data must be restored before restoreWindow is called so that michael@0: // it happens before observers are notified michael@0: this._globalState.setFromState(this._deferredInitialState); michael@0: michael@0: this._restoreCount = this._deferredInitialState.windows ? michael@0: this._deferredInitialState.windows.length : 0; michael@0: this.restoreWindow(aWindow, this._deferredInitialState, {firstWindow: true}); michael@0: this._deferredInitialState = null; michael@0: } michael@0: else if (this._restoreLastWindow && aWindow.toolbar.visible && michael@0: this._closedWindows.length && !isPrivateWindow) { michael@0: michael@0: // default to the most-recently closed window michael@0: // don't use popup windows michael@0: let closedWindowState = null; michael@0: let closedWindowIndex; michael@0: for (let i = 0; i < this._closedWindows.length; i++) { michael@0: // Take the first non-popup, point our object at it, and break out. michael@0: if (!this._closedWindows[i].isPopup) { michael@0: closedWindowState = this._closedWindows[i]; michael@0: closedWindowIndex = i; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (closedWindowState) { michael@0: let newWindowState; michael@0: #ifndef XP_MACOSX michael@0: if (!this._doResumeSession()) { michael@0: #endif michael@0: // We want to split the window up into pinned tabs and unpinned tabs. michael@0: // Pinned tabs should be restored. If there are any remaining tabs, michael@0: // they should be added back to _closedWindows. michael@0: // We'll cheat a little bit and reuse _prepDataForDeferredRestore michael@0: // even though it wasn't built exactly for this. michael@0: let [appTabsState, normalTabsState] = michael@0: this._prepDataForDeferredRestore({ windows: [closedWindowState] }); michael@0: michael@0: // These are our pinned tabs, which we should restore michael@0: if (appTabsState.windows.length) { michael@0: newWindowState = appTabsState.windows[0]; michael@0: delete newWindowState.__lastSessionWindowID; michael@0: } michael@0: michael@0: // In case there were no unpinned tabs, remove the window from _closedWindows michael@0: if (!normalTabsState.windows.length) { michael@0: this._closedWindows.splice(closedWindowIndex, 1); michael@0: } michael@0: // Or update _closedWindows with the modified state michael@0: else { michael@0: delete normalTabsState.windows[0].__lastSessionWindowID; michael@0: this._closedWindows[closedWindowIndex] = normalTabsState.windows[0]; michael@0: } michael@0: #ifndef XP_MACOSX michael@0: } michael@0: else { michael@0: // If we're just restoring the window, make sure it gets removed from michael@0: // _closedWindows. michael@0: this._closedWindows.splice(closedWindowIndex, 1); michael@0: newWindowState = closedWindowState; michael@0: delete newWindowState.hidden; michael@0: } michael@0: #endif michael@0: if (newWindowState) { michael@0: // Ensure that the window state isn't hidden michael@0: this._restoreCount = 1; michael@0: let state = { windows: [newWindowState] }; michael@0: let options = {overwriteTabs: this._isCmdLineEmpty(aWindow, state)}; michael@0: this.restoreWindow(aWindow, state, options); michael@0: } michael@0: } michael@0: // we actually restored the session just now. michael@0: this._prefBranch.setBoolPref("sessionstore.resume_session_once", false); michael@0: } michael@0: if (this._restoreLastWindow && aWindow.toolbar.visible) { michael@0: // always reset (if not a popup window) michael@0: // we don't want to restore a window directly after, for example, michael@0: // undoCloseWindow was executed. michael@0: this._restoreLastWindow = false; michael@0: } michael@0: michael@0: var tabbrowser = aWindow.gBrowser; michael@0: michael@0: // add tab change listeners to all already existing tabs michael@0: for (let i = 0; i < tabbrowser.tabs.length; i++) { michael@0: this.onTabAdd(aWindow, tabbrowser.tabs[i], true); michael@0: } michael@0: // notification of tab add/remove/selection/show/hide michael@0: TAB_EVENTS.forEach(function(aEvent) { michael@0: tabbrowser.tabContainer.addEventListener(aEvent, this, true); michael@0: }, this); michael@0: }, michael@0: michael@0: /** michael@0: * On window open michael@0: * @param aWindow michael@0: * Window reference michael@0: */ michael@0: onOpen: function ssi_onOpen(aWindow) { michael@0: let onload = () => { michael@0: aWindow.removeEventListener("load", onload); michael@0: michael@0: let windowType = aWindow.document.documentElement.getAttribute("windowtype"); michael@0: michael@0: // Ignore non-browser windows. michael@0: if (windowType != "navigator:browser") { michael@0: return; michael@0: } michael@0: michael@0: if (this._sessionInitialized) { michael@0: this.onLoad(aWindow); michael@0: return; michael@0: } michael@0: michael@0: // The very first window that is opened creates a promise that is then michael@0: // re-used by all subsequent windows. The promise will be used to tell michael@0: // when we're ready for initialization. michael@0: if (!this._promiseReadyForInitialization) { michael@0: let deferred = Promise.defer(); michael@0: michael@0: // Wait for the given window's delayed startup to be finished. michael@0: Services.obs.addObserver(function obs(subject, topic) { michael@0: if (aWindow == subject) { michael@0: Services.obs.removeObserver(obs, topic); michael@0: deferred.resolve(); michael@0: } michael@0: }, "browser-delayed-startup-finished", false); michael@0: michael@0: // We are ready for initialization as soon as the session file has been michael@0: // read from disk and the initial window's delayed startup has finished. michael@0: this._promiseReadyForInitialization = michael@0: Promise.all([deferred.promise, gSessionStartup.onceInitialized]); michael@0: } michael@0: michael@0: // We can't call this.onLoad since initialization michael@0: // hasn't completed, so we'll wait until it is done. michael@0: // Even if additional windows are opened and wait michael@0: // for initialization as well, the first opened michael@0: // window should execute first, and this.onLoad michael@0: // will be called with the initialState. michael@0: this._promiseReadyForInitialization.then(() => { michael@0: if (aWindow.closed) { michael@0: return; michael@0: } michael@0: michael@0: if (this._sessionInitialized) { michael@0: this.onLoad(aWindow); michael@0: } else { michael@0: let initialState = this.initSession(); michael@0: this._sessionInitialized = true; michael@0: this.onLoad(aWindow, initialState); michael@0: michael@0: // Let everyone know we're done. michael@0: this._deferredInitialized.resolve(); michael@0: } michael@0: }, console.error); michael@0: }; michael@0: michael@0: aWindow.addEventListener("load", onload); michael@0: }, michael@0: michael@0: /** michael@0: * On window close... michael@0: * - remove event listeners from tabs michael@0: * - save all window data michael@0: * @param aWindow michael@0: * Window reference michael@0: */ michael@0: onClose: function ssi_onClose(aWindow) { michael@0: // this window was about to be restored - conserve its original data, if any michael@0: let isFullyLoaded = this._isWindowLoaded(aWindow); michael@0: if (!isFullyLoaded) { michael@0: if (!aWindow.__SSi) { michael@0: aWindow.__SSi = this._generateWindowID(); michael@0: } michael@0: michael@0: this._windows[aWindow.__SSi] = this._statesToRestore[aWindow.__SS_restoreID]; michael@0: delete this._statesToRestore[aWindow.__SS_restoreID]; michael@0: delete aWindow.__SS_restoreID; michael@0: } michael@0: michael@0: // ignore windows not tracked by SessionStore michael@0: if (!aWindow.__SSi || !this._windows[aWindow.__SSi]) { michael@0: return; michael@0: } michael@0: michael@0: // notify that the session store will stop tracking this window so that michael@0: // extensions can store any data about this window in session store before michael@0: // that's not possible anymore michael@0: let event = aWindow.document.createEvent("Events"); michael@0: event.initEvent("SSWindowClosing", true, false); michael@0: aWindow.dispatchEvent(event); michael@0: michael@0: if (this.windowToFocus && this.windowToFocus == aWindow) { michael@0: delete this.windowToFocus; michael@0: } michael@0: michael@0: var tabbrowser = aWindow.gBrowser; michael@0: michael@0: TAB_EVENTS.forEach(function(aEvent) { michael@0: tabbrowser.tabContainer.removeEventListener(aEvent, this, true); michael@0: }, this); michael@0: michael@0: let winData = this._windows[aWindow.__SSi]; michael@0: michael@0: // Collect window data only when *not* closed during shutdown. michael@0: if (this._loadState == STATE_RUNNING) { michael@0: // Flush all data queued in the content script before the window is gone. michael@0: TabState.flushWindow(aWindow); michael@0: michael@0: // update all window data for a last time michael@0: this._collectWindowData(aWindow); michael@0: michael@0: if (isFullyLoaded) { michael@0: winData.title = aWindow.content.document.title || tabbrowser.selectedTab.label; michael@0: winData.title = this._replaceLoadingTitle(winData.title, tabbrowser, michael@0: tabbrowser.selectedTab); michael@0: SessionCookies.update([winData]); michael@0: } michael@0: michael@0: #ifndef XP_MACOSX michael@0: // Until we decide otherwise elsewhere, this window is part of a series michael@0: // of closing windows to quit. michael@0: winData._shouldRestore = true; michael@0: #endif michael@0: michael@0: // Store the window's close date to figure out when each individual tab michael@0: // was closed. This timestamp should allow re-arranging data based on how michael@0: // recently something was closed. michael@0: winData.closedAt = Date.now(); michael@0: michael@0: // Save non-private windows if they have at michael@0: // least one saveable tab or are the last window. michael@0: if (!winData.isPrivate) { michael@0: // Remove any open private tabs the window may contain. michael@0: PrivacyFilter.filterPrivateTabs(winData); michael@0: michael@0: // Determine whether the window has any tabs worth saving. michael@0: let hasSaveableTabs = winData.tabs.some(this._shouldSaveTabState); michael@0: michael@0: // When closing windows one after the other until Firefox quits, we michael@0: // will move those closed in series back to the "open windows" bucket michael@0: // before writing to disk. If however there is only a single window michael@0: // with tabs we deem not worth saving then we might end up with a michael@0: // random closed or even a pop-up window re-opened. To prevent that michael@0: // we explicitly allow saving an "empty" window state. michael@0: let isLastWindow = michael@0: Object.keys(this._windows).length == 1 && michael@0: !this._closedWindows.some(win => win._shouldRestore || false); michael@0: michael@0: if (hasSaveableTabs || isLastWindow) { michael@0: // we don't want to save the busy state michael@0: delete winData.busy; michael@0: michael@0: this._closedWindows.unshift(winData); michael@0: this._capClosedWindows(); michael@0: } michael@0: } michael@0: michael@0: // clear this window from the list michael@0: delete this._windows[aWindow.__SSi]; michael@0: michael@0: // save the state without this window to disk michael@0: this.saveStateDelayed(); michael@0: } michael@0: michael@0: for (let i = 0; i < tabbrowser.tabs.length; i++) { michael@0: this.onTabRemove(aWindow, tabbrowser.tabs[i], true); michael@0: } michael@0: michael@0: // Cache the window state until it is completely gone. michael@0: DyingWindowCache.set(aWindow, winData); michael@0: michael@0: let mm = aWindow.messageManager; michael@0: MESSAGES.forEach(msg => mm.removeMessageListener(msg, this)); michael@0: michael@0: delete aWindow.__SSi; michael@0: }, michael@0: michael@0: /** michael@0: * On quit application requested michael@0: */ michael@0: onQuitApplicationRequested: function ssi_onQuitApplicationRequested() { michael@0: // get a current snapshot of all windows michael@0: this._forEachBrowserWindow(function(aWindow) { michael@0: // Flush all data queued in the content script to not lose it when michael@0: // shutting down. michael@0: TabState.flushWindow(aWindow); michael@0: this._collectWindowData(aWindow); michael@0: }); michael@0: // we must cache this because _getMostRecentBrowserWindow will always michael@0: // return null by the time quit-application occurs michael@0: var activeWindow = this._getMostRecentBrowserWindow(); michael@0: if (activeWindow) michael@0: this.activeWindowSSiCache = activeWindow.__SSi || ""; michael@0: DirtyWindows.clear(); michael@0: }, michael@0: michael@0: /** michael@0: * On quit application granted michael@0: */ michael@0: onQuitApplicationGranted: function ssi_onQuitApplicationGranted() { michael@0: // freeze the data at what we've got (ignoring closing windows) michael@0: this._loadState = STATE_QUITTING; michael@0: }, michael@0: michael@0: /** michael@0: * On last browser window close michael@0: */ michael@0: onLastWindowCloseGranted: function ssi_onLastWindowCloseGranted() { michael@0: // last browser window is quitting. michael@0: // remember to restore the last window when another browser window is opened michael@0: // do not account for pref(resume_session_once) at this point, as it might be michael@0: // set by another observer getting this notice after us michael@0: this._restoreLastWindow = true; michael@0: }, michael@0: michael@0: /** michael@0: * On quitting application michael@0: * @param aData michael@0: * String type of quitting michael@0: */ michael@0: onQuitApplication: function ssi_onQuitApplication(aData) { michael@0: if (aData == "restart") { michael@0: this._prefBranch.setBoolPref("sessionstore.resume_session_once", true); michael@0: // The browser:purge-session-history notification fires after the michael@0: // quit-application notification so unregister the michael@0: // browser:purge-session-history notification to prevent clearing michael@0: // session data on disk on a restart. It is also unnecessary to michael@0: // perform any other sanitization processing on a restart as the michael@0: // browser is about to exit anyway. michael@0: Services.obs.removeObserver(this, "browser:purge-session-history"); michael@0: } michael@0: michael@0: if (aData != "restart") { michael@0: // Throw away the previous session on shutdown michael@0: LastSession.clear(); michael@0: } michael@0: michael@0: this._loadState = STATE_QUITTING; // just to be sure michael@0: this._uninit(); michael@0: }, michael@0: michael@0: /** michael@0: * On purge of session history michael@0: */ michael@0: onPurgeSessionHistory: function ssi_onPurgeSessionHistory() { michael@0: SessionFile.wipe(); michael@0: // If the browser is shutting down, simply return after clearing the michael@0: // session data on disk as this notification fires after the michael@0: // quit-application notification so the browser is about to exit. michael@0: if (this._loadState == STATE_QUITTING) michael@0: return; michael@0: LastSession.clear(); michael@0: let openWindows = {}; michael@0: this._forEachBrowserWindow(function(aWindow) { michael@0: Array.forEach(aWindow.gBrowser.tabs, function(aTab) { michael@0: delete aTab.linkedBrowser.__SS_data; michael@0: if (aTab.linkedBrowser.__SS_restoreState) michael@0: this._resetTabRestoringState(aTab); michael@0: }, this); michael@0: openWindows[aWindow.__SSi] = true; michael@0: }); michael@0: // also clear all data about closed tabs and windows michael@0: for (let ix in this._windows) { michael@0: if (ix in openWindows) { michael@0: this._windows[ix]._closedTabs = []; michael@0: } else { michael@0: delete this._windows[ix]; michael@0: } michael@0: } michael@0: // also clear all data about closed windows michael@0: this._closedWindows = []; michael@0: // give the tabbrowsers a chance to clear their histories first michael@0: var win = this._getMostRecentBrowserWindow(); michael@0: if (win) { michael@0: win.setTimeout(() => SessionSaver.run(), 0); michael@0: } else if (this._loadState == STATE_RUNNING) { michael@0: SessionSaver.run(); michael@0: } michael@0: michael@0: this._clearRestoringWindows(); michael@0: }, michael@0: michael@0: /** michael@0: * On purge of domain data michael@0: * @param aData michael@0: * String domain data michael@0: */ michael@0: onPurgeDomainData: function ssi_onPurgeDomainData(aData) { michael@0: // does a session history entry contain a url for the given domain? michael@0: function containsDomain(aEntry) { michael@0: if (Utils.hasRootDomain(aEntry.url, aData)) { michael@0: return true; michael@0: } michael@0: return aEntry.children && aEntry.children.some(containsDomain, this); michael@0: } michael@0: // remove all closed tabs containing a reference to the given domain michael@0: for (let ix in this._windows) { michael@0: let closedTabs = this._windows[ix]._closedTabs; michael@0: for (let i = closedTabs.length - 1; i >= 0; i--) { michael@0: if (closedTabs[i].state.entries.some(containsDomain, this)) michael@0: closedTabs.splice(i, 1); michael@0: } michael@0: } michael@0: // remove all open & closed tabs containing a reference to the given michael@0: // domain in closed windows michael@0: for (let ix = this._closedWindows.length - 1; ix >= 0; ix--) { michael@0: let closedTabs = this._closedWindows[ix]._closedTabs; michael@0: let openTabs = this._closedWindows[ix].tabs; michael@0: let openTabCount = openTabs.length; michael@0: for (let i = closedTabs.length - 1; i >= 0; i--) michael@0: if (closedTabs[i].state.entries.some(containsDomain, this)) michael@0: closedTabs.splice(i, 1); michael@0: for (let j = openTabs.length - 1; j >= 0; j--) { michael@0: if (openTabs[j].entries.some(containsDomain, this)) { michael@0: openTabs.splice(j, 1); michael@0: if (this._closedWindows[ix].selected > j) michael@0: this._closedWindows[ix].selected--; michael@0: } michael@0: } michael@0: if (openTabs.length == 0) { michael@0: this._closedWindows.splice(ix, 1); michael@0: } michael@0: else if (openTabs.length != openTabCount) { michael@0: // Adjust the window's title if we removed an open tab michael@0: let selectedTab = openTabs[this._closedWindows[ix].selected - 1]; michael@0: // some duplication from restoreHistory - make sure we get the correct title michael@0: let activeIndex = (selectedTab.index || selectedTab.entries.length) - 1; michael@0: if (activeIndex >= selectedTab.entries.length) michael@0: activeIndex = selectedTab.entries.length - 1; michael@0: this._closedWindows[ix].title = selectedTab.entries[activeIndex].title; michael@0: } michael@0: } michael@0: michael@0: if (this._loadState == STATE_RUNNING) { michael@0: SessionSaver.run(); michael@0: } michael@0: michael@0: this._clearRestoringWindows(); michael@0: }, michael@0: michael@0: /** michael@0: * On preference change michael@0: * @param aData michael@0: * String preference changed michael@0: */ michael@0: onPrefChange: function ssi_onPrefChange(aData) { michael@0: switch (aData) { michael@0: // if the user decreases the max number of closed tabs they want michael@0: // preserved update our internal states to match that max michael@0: case "sessionstore.max_tabs_undo": michael@0: this._max_tabs_undo = this._prefBranch.getIntPref("sessionstore.max_tabs_undo"); michael@0: for (let ix in this._windows) { michael@0: this._windows[ix]._closedTabs.splice(this._max_tabs_undo, this._windows[ix]._closedTabs.length); michael@0: } michael@0: break; michael@0: case "sessionstore.max_windows_undo": michael@0: this._max_windows_undo = this._prefBranch.getIntPref("sessionstore.max_windows_undo"); michael@0: this._capClosedWindows(); michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * set up listeners for a new tab michael@0: * @param aWindow michael@0: * Window reference michael@0: * @param aTab michael@0: * Tab reference michael@0: * @param aNoNotification michael@0: * bool Do not save state if we're updating an existing tab michael@0: */ michael@0: onTabAdd: function ssi_onTabAdd(aWindow, aTab, aNoNotification) { michael@0: if (!aNoNotification) { michael@0: this.saveStateDelayed(aWindow); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * remove listeners for a tab michael@0: * @param aWindow michael@0: * Window reference michael@0: * @param aTab michael@0: * Tab reference michael@0: * @param aNoNotification michael@0: * bool Do not save state if we're updating an existing tab michael@0: */ michael@0: onTabRemove: function ssi_onTabRemove(aWindow, aTab, aNoNotification) { michael@0: let browser = aTab.linkedBrowser; michael@0: delete browser.__SS_data; michael@0: michael@0: // If this tab was in the middle of restoring or still needs to be restored, michael@0: // we need to reset that state. If the tab was restoring, we will attempt to michael@0: // restore the next tab. michael@0: let previousState = browser.__SS_restoreState; michael@0: if (previousState) { michael@0: this._resetTabRestoringState(aTab); michael@0: if (previousState == TAB_STATE_RESTORING) michael@0: this.restoreNextTab(); michael@0: } michael@0: michael@0: if (!aNoNotification) { michael@0: this.saveStateDelayed(aWindow); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * When a tab closes, collect its properties michael@0: * @param aWindow michael@0: * Window reference michael@0: * @param aTab michael@0: * Tab reference michael@0: */ michael@0: onTabClose: function ssi_onTabClose(aWindow, aTab) { michael@0: // notify the tabbrowser that the tab state will be retrieved for the last time michael@0: // (so that extension authors can easily set data on soon-to-be-closed tabs) michael@0: var event = aWindow.document.createEvent("Events"); michael@0: event.initEvent("SSTabClosing", true, false); michael@0: aTab.dispatchEvent(event); michael@0: michael@0: // don't update our internal state if we don't have to michael@0: if (this._max_tabs_undo == 0) { michael@0: return; michael@0: } michael@0: michael@0: // Flush all data queued in the content script before the tab is gone. michael@0: TabState.flush(aTab.linkedBrowser); michael@0: michael@0: // Get the latest data for this tab (generally, from the cache) michael@0: let tabState = TabState.collect(aTab); michael@0: michael@0: // Don't save private tabs michael@0: let isPrivateWindow = PrivateBrowsingUtils.isWindowPrivate(aWindow); michael@0: if (!isPrivateWindow && tabState.isPrivate) { michael@0: return; michael@0: } michael@0: michael@0: // store closed-tab data for undo michael@0: if (this._shouldSaveTabState(tabState)) { michael@0: let tabTitle = aTab.label; michael@0: let tabbrowser = aWindow.gBrowser; michael@0: tabTitle = this._replaceLoadingTitle(tabTitle, tabbrowser, aTab); michael@0: michael@0: this._windows[aWindow.__SSi]._closedTabs.unshift({ michael@0: state: tabState, michael@0: title: tabTitle, michael@0: image: tabbrowser.getIcon(aTab), michael@0: pos: aTab._tPos, michael@0: closedAt: Date.now() michael@0: }); michael@0: var length = this._windows[aWindow.__SSi]._closedTabs.length; michael@0: if (length > this._max_tabs_undo) michael@0: this._windows[aWindow.__SSi]._closedTabs.splice(this._max_tabs_undo, length - this._max_tabs_undo); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * When a tab is selected, save session data michael@0: * @param aWindow michael@0: * Window reference michael@0: */ michael@0: onTabSelect: function ssi_onTabSelect(aWindow) { michael@0: if (this._loadState == STATE_RUNNING) { michael@0: this._windows[aWindow.__SSi].selected = aWindow.gBrowser.tabContainer.selectedIndex; michael@0: michael@0: let tab = aWindow.gBrowser.selectedTab; michael@0: // If __SS_restoreState is still on the browser and it is michael@0: // TAB_STATE_NEEDS_RESTORE, then then we haven't restored michael@0: // this tab yet. Explicitly call restoreTabContent to kick off the restore. michael@0: if (tab.linkedBrowser.__SS_restoreState && michael@0: tab.linkedBrowser.__SS_restoreState == TAB_STATE_NEEDS_RESTORE) michael@0: this.restoreTabContent(tab); michael@0: } michael@0: }, michael@0: michael@0: onTabShow: function ssi_onTabShow(aWindow, aTab) { michael@0: // If the tab hasn't been restored yet, move it into the right bucket michael@0: if (aTab.linkedBrowser.__SS_restoreState && michael@0: aTab.linkedBrowser.__SS_restoreState == TAB_STATE_NEEDS_RESTORE) { michael@0: TabRestoreQueue.hiddenToVisible(aTab); michael@0: michael@0: // let's kick off tab restoration again to ensure this tab gets restored michael@0: // with "restore_hidden_tabs" == false (now that it has become visible) michael@0: this.restoreNextTab(); michael@0: } michael@0: michael@0: // Default delay of 2 seconds gives enough time to catch multiple TabShow michael@0: // events due to changing groups in Panorama. michael@0: this.saveStateDelayed(aWindow); michael@0: }, michael@0: michael@0: onTabHide: function ssi_onTabHide(aWindow, aTab) { michael@0: // If the tab hasn't been restored yet, move it into the right bucket michael@0: if (aTab.linkedBrowser.__SS_restoreState && michael@0: aTab.linkedBrowser.__SS_restoreState == TAB_STATE_NEEDS_RESTORE) { michael@0: TabRestoreQueue.visibleToHidden(aTab); michael@0: } michael@0: michael@0: // Default delay of 2 seconds gives enough time to catch multiple TabHide michael@0: // events due to changing groups in Panorama. michael@0: this.saveStateDelayed(aWindow); michael@0: }, michael@0: michael@0: onGatherTelemetry: function() { michael@0: // On the first gather-telemetry notification of the session, michael@0: // gather telemetry data. michael@0: Services.obs.removeObserver(this, "gather-telemetry"); michael@0: let stateString = SessionStore.getBrowserState(); michael@0: return SessionFile.gatherTelemetry(stateString); michael@0: }, michael@0: michael@0: /* ........ nsISessionStore API .............. */ michael@0: michael@0: getBrowserState: function ssi_getBrowserState() { michael@0: let state = this.getCurrentState(); michael@0: michael@0: // Don't include the last session state in getBrowserState(). michael@0: delete state.lastSessionState; michael@0: michael@0: // Don't include any deferred initial state. michael@0: delete state.deferredInitialState; michael@0: michael@0: return this._toJSONString(state); michael@0: }, michael@0: michael@0: setBrowserState: function ssi_setBrowserState(aState) { michael@0: this._handleClosedWindows(); michael@0: michael@0: try { michael@0: var state = JSON.parse(aState); michael@0: } michael@0: catch (ex) { /* invalid state object - don't restore anything */ } michael@0: if (!state) { michael@0: throw Components.Exception("Invalid state string: not JSON", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: if (!state.windows) { michael@0: throw Components.Exception("No windows", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: this._browserSetState = true; michael@0: michael@0: // Make sure the priority queue is emptied out michael@0: this._resetRestoringState(); michael@0: michael@0: var window = this._getMostRecentBrowserWindow(); michael@0: if (!window) { michael@0: this._restoreCount = 1; michael@0: this._openWindowWithState(state); michael@0: return; michael@0: } michael@0: michael@0: // close all other browser windows michael@0: this._forEachBrowserWindow(function(aWindow) { michael@0: if (aWindow != window) { michael@0: aWindow.close(); michael@0: this.onClose(aWindow); michael@0: } michael@0: }); michael@0: michael@0: // make sure closed window data isn't kept michael@0: this._closedWindows = []; michael@0: michael@0: // determine how many windows are meant to be restored michael@0: this._restoreCount = state.windows ? state.windows.length : 0; michael@0: michael@0: // global data must be restored before restoreWindow is called so that michael@0: // it happens before observers are notified michael@0: this._globalState.setFromState(state); michael@0: michael@0: // restore to the given state michael@0: this.restoreWindow(window, state, {overwriteTabs: true}); michael@0: }, michael@0: michael@0: getWindowState: function ssi_getWindowState(aWindow) { michael@0: if ("__SSi" in aWindow) { michael@0: return this._toJSONString(this._getWindowState(aWindow)); michael@0: } michael@0: michael@0: if (DyingWindowCache.has(aWindow)) { michael@0: let data = DyingWindowCache.get(aWindow); michael@0: return this._toJSONString({ windows: [data] }); michael@0: } michael@0: michael@0: throw Components.Exception("Window is not tracked", Cr.NS_ERROR_INVALID_ARG); michael@0: }, michael@0: michael@0: setWindowState: function ssi_setWindowState(aWindow, aState, aOverwrite) { michael@0: if (!aWindow.__SSi) { michael@0: throw Components.Exception("Window is not tracked", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: this.restoreWindow(aWindow, aState, {overwriteTabs: aOverwrite}); michael@0: }, michael@0: michael@0: getTabState: function ssi_getTabState(aTab) { michael@0: if (!aTab.ownerDocument) { michael@0: throw Components.Exception("Invalid tab object: no ownerDocument", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: if (!aTab.ownerDocument.defaultView.__SSi) { michael@0: throw Components.Exception("Default view is not tracked", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: let tabState = TabState.collect(aTab); michael@0: michael@0: return this._toJSONString(tabState); michael@0: }, michael@0: michael@0: setTabState: function ssi_setTabState(aTab, aState) { michael@0: // Remove the tab state from the cache. michael@0: // Note that we cannot simply replace the contents of the cache michael@0: // as |aState| can be an incomplete state that will be completed michael@0: // by |restoreTabs|. michael@0: let tabState = JSON.parse(aState); michael@0: if (!tabState) { michael@0: throw Components.Exception("Invalid state string: not JSON", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: if (typeof tabState != "object") { michael@0: throw Components.Exception("Not an object", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: if (!("entries" in tabState)) { michael@0: throw Components.Exception("Invalid state object: no entries", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: if (!aTab.ownerDocument) { michael@0: throw Components.Exception("Invalid tab object: no ownerDocument", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: let window = aTab.ownerDocument.defaultView; michael@0: if (!("__SSi" in window)) { michael@0: throw Components.Exception("Window is not tracked", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: if (aTab.linkedBrowser.__SS_restoreState) { michael@0: this._resetTabRestoringState(aTab); michael@0: } michael@0: michael@0: this._setWindowStateBusy(window); michael@0: this.restoreTabs(window, [aTab], [tabState], 0); michael@0: }, michael@0: michael@0: duplicateTab: function ssi_duplicateTab(aWindow, aTab, aDelta = 0) { michael@0: if (!aTab.ownerDocument) { michael@0: throw Components.Exception("Invalid tab object: no ownerDocument", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: if (!aTab.ownerDocument.defaultView.__SSi) { michael@0: throw Components.Exception("Default view is not tracked", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: if (!aWindow.getBrowser) { michael@0: throw Components.Exception("Invalid window object: no getBrowser", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: // Flush all data queued in the content script because we will need that michael@0: // state to properly duplicate the given tab. michael@0: TabState.flush(aTab.linkedBrowser); michael@0: michael@0: // Duplicate the tab state michael@0: let tabState = TabState.clone(aTab); michael@0: michael@0: tabState.index += aDelta; michael@0: tabState.index = Math.max(1, Math.min(tabState.index, tabState.entries.length)); michael@0: tabState.pinned = false; michael@0: michael@0: this._setWindowStateBusy(aWindow); michael@0: let newTab = aTab == aWindow.gBrowser.selectedTab ? michael@0: aWindow.gBrowser.addTab(null, {relatedToCurrent: true, ownerTab: aTab}) : michael@0: aWindow.gBrowser.addTab(); michael@0: michael@0: this.restoreTabs(aWindow, [newTab], [tabState], 0, michael@0: true /* Load this tab right away. */); michael@0: michael@0: return newTab; michael@0: }, michael@0: michael@0: getClosedTabCount: function ssi_getClosedTabCount(aWindow) { michael@0: if ("__SSi" in aWindow) { michael@0: return this._windows[aWindow.__SSi]._closedTabs.length; michael@0: } michael@0: michael@0: if (!DyingWindowCache.has(aWindow)) { michael@0: throw Components.Exception("Window is not tracked", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: return DyingWindowCache.get(aWindow)._closedTabs.length; michael@0: }, michael@0: michael@0: getClosedTabData: function ssi_getClosedTabDataAt(aWindow) { michael@0: if ("__SSi" in aWindow) { michael@0: return this._toJSONString(this._windows[aWindow.__SSi]._closedTabs); michael@0: } michael@0: michael@0: if (!DyingWindowCache.has(aWindow)) { michael@0: throw Components.Exception("Window is not tracked", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: let data = DyingWindowCache.get(aWindow); michael@0: return this._toJSONString(data._closedTabs); michael@0: }, michael@0: michael@0: undoCloseTab: function ssi_undoCloseTab(aWindow, aIndex) { michael@0: if (!aWindow.__SSi) { michael@0: throw Components.Exception("Window is not tracked", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: var closedTabs = this._windows[aWindow.__SSi]._closedTabs; michael@0: michael@0: // default to the most-recently closed tab michael@0: aIndex = aIndex || 0; michael@0: if (!(aIndex in closedTabs)) { michael@0: throw Components.Exception("Invalid index: not in the closed tabs", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: // fetch the data of closed tab, while removing it from the array michael@0: let closedTab = closedTabs.splice(aIndex, 1).shift(); michael@0: let closedTabState = closedTab.state; michael@0: michael@0: this._setWindowStateBusy(aWindow); michael@0: // create a new tab michael@0: let tabbrowser = aWindow.gBrowser; michael@0: let tab = tabbrowser.addTab(); michael@0: michael@0: // restore tab content michael@0: this.restoreTabs(aWindow, [tab], [closedTabState], 1); michael@0: michael@0: // restore the tab's position michael@0: tabbrowser.moveTabTo(tab, closedTab.pos); michael@0: michael@0: // focus the tab's content area (bug 342432) michael@0: tab.linkedBrowser.focus(); michael@0: michael@0: return tab; michael@0: }, michael@0: michael@0: forgetClosedTab: function ssi_forgetClosedTab(aWindow, aIndex) { michael@0: if (!aWindow.__SSi) { michael@0: throw Components.Exception("Window is not tracked", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: var closedTabs = this._windows[aWindow.__SSi]._closedTabs; michael@0: michael@0: // default to the most-recently closed tab michael@0: aIndex = aIndex || 0; michael@0: if (!(aIndex in closedTabs)) { michael@0: throw Components.Exception("Invalid index: not in the closed tabs", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: // remove closed tab from the array michael@0: closedTabs.splice(aIndex, 1); michael@0: }, michael@0: michael@0: getClosedWindowCount: function ssi_getClosedWindowCount() { michael@0: return this._closedWindows.length; michael@0: }, michael@0: michael@0: getClosedWindowData: function ssi_getClosedWindowData() { michael@0: return this._toJSONString(this._closedWindows); michael@0: }, michael@0: michael@0: undoCloseWindow: function ssi_undoCloseWindow(aIndex) { michael@0: if (!(aIndex in this._closedWindows)) { michael@0: throw Components.Exception("Invalid index: not in the closed windows", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: // reopen the window michael@0: let state = { windows: this._closedWindows.splice(aIndex, 1) }; michael@0: let window = this._openWindowWithState(state); michael@0: this.windowToFocus = window; michael@0: return window; michael@0: }, michael@0: michael@0: forgetClosedWindow: function ssi_forgetClosedWindow(aIndex) { michael@0: // default to the most-recently closed window michael@0: aIndex = aIndex || 0; michael@0: if (!(aIndex in this._closedWindows)) { michael@0: throw Components.Exception("Invalid index: not in the closed windows", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: michael@0: // remove closed window from the array michael@0: this._closedWindows.splice(aIndex, 1); michael@0: }, michael@0: michael@0: getWindowValue: function ssi_getWindowValue(aWindow, aKey) { michael@0: if ("__SSi" in aWindow) { michael@0: var data = this._windows[aWindow.__SSi].extData || {}; michael@0: return data[aKey] || ""; michael@0: } michael@0: michael@0: if (DyingWindowCache.has(aWindow)) { michael@0: let data = DyingWindowCache.get(aWindow).extData || {}; michael@0: return data[aKey] || ""; michael@0: } michael@0: michael@0: throw Components.Exception("Window is not tracked", Cr.NS_ERROR_INVALID_ARG); michael@0: }, michael@0: michael@0: setWindowValue: function ssi_setWindowValue(aWindow, aKey, aStringValue) { michael@0: if (typeof aStringValue != "string") { michael@0: throw new TypeError("setWindowValue only accepts string values"); michael@0: } michael@0: michael@0: if (!("__SSi" in aWindow)) { michael@0: throw Components.Exception("Window is not tracked", Cr.NS_ERROR_INVALID_ARG); michael@0: } michael@0: if (!this._windows[aWindow.__SSi].extData) { michael@0: this._windows[aWindow.__SSi].extData = {}; michael@0: } michael@0: this._windows[aWindow.__SSi].extData[aKey] = aStringValue; michael@0: this.saveStateDelayed(aWindow); michael@0: }, michael@0: michael@0: deleteWindowValue: function ssi_deleteWindowValue(aWindow, aKey) { michael@0: if (aWindow.__SSi && this._windows[aWindow.__SSi].extData && michael@0: this._windows[aWindow.__SSi].extData[aKey]) michael@0: delete this._windows[aWindow.__SSi].extData[aKey]; michael@0: this.saveStateDelayed(aWindow); michael@0: }, michael@0: michael@0: getTabValue: function ssi_getTabValue(aTab, aKey) { michael@0: let data = {}; michael@0: if (aTab.__SS_extdata) { michael@0: data = aTab.__SS_extdata; michael@0: } michael@0: else if (aTab.linkedBrowser.__SS_data && aTab.linkedBrowser.__SS_data.extData) { michael@0: // If the tab hasn't been fully restored, get the data from the to-be-restored data michael@0: data = aTab.linkedBrowser.__SS_data.extData; michael@0: } michael@0: return data[aKey] || ""; michael@0: }, michael@0: michael@0: setTabValue: function ssi_setTabValue(aTab, aKey, aStringValue) { michael@0: if (typeof aStringValue != "string") { michael@0: throw new TypeError("setTabValue only accepts string values"); michael@0: } michael@0: michael@0: // If the tab hasn't been restored, then set the data there, otherwise we michael@0: // could lose newly added data. michael@0: let saveTo; michael@0: if (aTab.__SS_extdata) { michael@0: saveTo = aTab.__SS_extdata; michael@0: } michael@0: else if (aTab.linkedBrowser.__SS_data && aTab.linkedBrowser.__SS_data.extData) { michael@0: saveTo = aTab.linkedBrowser.__SS_data.extData; michael@0: } michael@0: else { michael@0: aTab.__SS_extdata = {}; michael@0: saveTo = aTab.__SS_extdata; michael@0: } michael@0: michael@0: saveTo[aKey] = aStringValue; michael@0: this.saveStateDelayed(aTab.ownerDocument.defaultView); michael@0: }, michael@0: michael@0: deleteTabValue: function ssi_deleteTabValue(aTab, aKey) { michael@0: // We want to make sure that if data is accessed early, we attempt to delete michael@0: // that data from __SS_data as well. Otherwise we'll throw in cases where michael@0: // data can be set or read. michael@0: let deleteFrom; michael@0: if (aTab.__SS_extdata) { michael@0: deleteFrom = aTab.__SS_extdata; michael@0: } michael@0: else if (aTab.linkedBrowser.__SS_data && aTab.linkedBrowser.__SS_data.extData) { michael@0: deleteFrom = aTab.linkedBrowser.__SS_data.extData; michael@0: } michael@0: michael@0: if (deleteFrom && aKey in deleteFrom) { michael@0: delete deleteFrom[aKey]; michael@0: this.saveStateDelayed(aTab.ownerDocument.defaultView); michael@0: } michael@0: }, michael@0: michael@0: getGlobalValue: function ssi_getGlobalValue(aKey) { michael@0: return this._globalState.get(aKey); michael@0: }, michael@0: michael@0: setGlobalValue: function ssi_setGlobalValue(aKey, aStringValue) { michael@0: if (typeof aStringValue != "string") { michael@0: throw new TypeError("setGlobalValue only accepts string values"); michael@0: } michael@0: michael@0: this._globalState.set(aKey, aStringValue); michael@0: this.saveStateDelayed(); michael@0: }, michael@0: michael@0: deleteGlobalValue: function ssi_deleteGlobalValue(aKey) { michael@0: this._globalState.delete(aKey); michael@0: this.saveStateDelayed(); michael@0: }, michael@0: michael@0: persistTabAttribute: function ssi_persistTabAttribute(aName) { michael@0: if (TabAttributes.persist(aName)) { michael@0: this.saveStateDelayed(); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Restores the session state stored in LastSession. This will attempt michael@0: * to merge data into the current session. If a window was opened at startup michael@0: * with pinned tab(s), then the remaining data from the previous session for michael@0: * that window will be opened into that winddow. Otherwise new windows will michael@0: * be opened. michael@0: */ michael@0: restoreLastSession: function ssi_restoreLastSession() { michael@0: // Use the public getter since it also checks PB mode michael@0: if (!this.canRestoreLastSession) { michael@0: throw Components.Exception("Last session can not be restored"); michael@0: } michael@0: michael@0: // First collect each window with its id... michael@0: let windows = {}; michael@0: this._forEachBrowserWindow(function(aWindow) { michael@0: if (aWindow.__SS_lastSessionWindowID) michael@0: windows[aWindow.__SS_lastSessionWindowID] = aWindow; michael@0: }); michael@0: michael@0: let lastSessionState = LastSession.getState(); michael@0: michael@0: // This shouldn't ever be the case... michael@0: if (!lastSessionState.windows.length) { michael@0: throw Components.Exception("lastSessionState has no windows", Cr.NS_ERROR_UNEXPECTED); michael@0: } michael@0: michael@0: // We're technically doing a restore, so set things up so we send the michael@0: // notification when we're done. We want to send "sessionstore-browser-state-restored". michael@0: this._restoreCount = lastSessionState.windows.length; michael@0: this._browserSetState = true; michael@0: michael@0: // We want to re-use the last opened window instead of opening a new one in michael@0: // the case where it's "empty" and not associated with a window in the session. michael@0: // We will do more processing via _prepWindowToRestoreInto if we need to use michael@0: // the lastWindow. michael@0: let lastWindow = this._getMostRecentBrowserWindow(); michael@0: let canUseLastWindow = lastWindow && michael@0: !lastWindow.__SS_lastSessionWindowID; michael@0: michael@0: // global data must be restored before restoreWindow is called so that michael@0: // it happens before observers are notified michael@0: this._globalState.setFromState(lastSessionState); michael@0: michael@0: // Restore into windows or open new ones as needed. michael@0: for (let i = 0; i < lastSessionState.windows.length; i++) { michael@0: let winState = lastSessionState.windows[i]; michael@0: let lastSessionWindowID = winState.__lastSessionWindowID; michael@0: // delete lastSessionWindowID so we don't add that to the window again michael@0: delete winState.__lastSessionWindowID; michael@0: michael@0: // See if we can use an open window. First try one that is associated with michael@0: // the state we're trying to restore and then fallback to the last selected michael@0: // window. michael@0: let windowToUse = windows[lastSessionWindowID]; michael@0: if (!windowToUse && canUseLastWindow) { michael@0: windowToUse = lastWindow; michael@0: canUseLastWindow = false; michael@0: } michael@0: michael@0: let [canUseWindow, canOverwriteTabs] = this._prepWindowToRestoreInto(windowToUse); michael@0: michael@0: // If there's a window already open that we can restore into, use that michael@0: if (canUseWindow) { michael@0: // Since we're not overwriting existing tabs, we want to merge _closedTabs, michael@0: // putting existing ones first. Then make sure we're respecting the max pref. michael@0: if (winState._closedTabs && winState._closedTabs.length) { michael@0: let curWinState = this._windows[windowToUse.__SSi]; michael@0: curWinState._closedTabs = curWinState._closedTabs.concat(winState._closedTabs); michael@0: curWinState._closedTabs.splice(this._max_tabs_undo, curWinState._closedTabs.length); michael@0: } michael@0: michael@0: // Restore into that window - pretend it's a followup since we'll already michael@0: // have a focused window. michael@0: //XXXzpao This is going to merge extData together (taking what was in michael@0: // winState over what is in the window already. The hack we have michael@0: // in _preWindowToRestoreInto will prevent most (all?) Panorama michael@0: // weirdness but we will still merge other extData. michael@0: // Bug 588217 should make this go away by merging the group data. michael@0: let options = {overwriteTabs: canOverwriteTabs, isFollowUp: true}; michael@0: this.restoreWindow(windowToUse, { windows: [winState] }, options); michael@0: } michael@0: else { michael@0: this._openWindowWithState({ windows: [winState] }); michael@0: } michael@0: } michael@0: michael@0: // Merge closed windows from this session with ones from last session michael@0: if (lastSessionState._closedWindows) { michael@0: this._closedWindows = this._closedWindows.concat(lastSessionState._closedWindows); michael@0: this._capClosedWindows(); michael@0: } michael@0: michael@0: if (lastSessionState.scratchpads) { michael@0: ScratchpadManager.restoreSession(lastSessionState.scratchpads); michael@0: } michael@0: michael@0: // Set data that persists between sessions michael@0: this._recentCrashes = lastSessionState.session && michael@0: lastSessionState.session.recentCrashes || 0; michael@0: michael@0: // Update the session start time using the restored session state. michael@0: this._updateSessionStartTime(lastSessionState); michael@0: michael@0: LastSession.clear(); michael@0: }, michael@0: michael@0: /** michael@0: * See if aWindow is usable for use when restoring a previous session via michael@0: * restoreLastSession. If usable, prepare it for use. michael@0: * michael@0: * @param aWindow michael@0: * the window to inspect & prepare michael@0: * @returns [canUseWindow, canOverwriteTabs] michael@0: * canUseWindow: can the window be used to restore into michael@0: * canOverwriteTabs: all of the current tabs are home pages and we michael@0: * can overwrite them michael@0: */ michael@0: _prepWindowToRestoreInto: function ssi_prepWindowToRestoreInto(aWindow) { michael@0: if (!aWindow) michael@0: return [false, false]; michael@0: michael@0: // We might be able to overwrite the existing tabs instead of just adding michael@0: // the previous session's tabs to the end. This will be set if possible. michael@0: let canOverwriteTabs = false; michael@0: michael@0: // Step 1 of processing: michael@0: // Inspect extData for Panorama identifiers. If found, then we want to michael@0: // inspect further. If there is a single group, then we can use this michael@0: // window. If there are multiple groups then we won't use this window. michael@0: let groupsData = this.getWindowValue(aWindow, "tabview-groups"); michael@0: if (groupsData) { michael@0: groupsData = JSON.parse(groupsData); michael@0: michael@0: // If there are multiple groups, we don't want to use this window. michael@0: if (groupsData.totalNumber > 1) michael@0: return [false, false]; michael@0: } michael@0: michael@0: // Step 2 of processing: michael@0: // If we're still here, then the window is usable. Look at the open tabs in michael@0: // comparison to home pages. If all the tabs are home pages then we'll end michael@0: // up overwriting all of them. Otherwise we'll just close the tabs that michael@0: // match home pages. Tabs with the about:blank URI will always be michael@0: // overwritten. michael@0: let homePages = ["about:blank"]; michael@0: let removableTabs = []; michael@0: let tabbrowser = aWindow.gBrowser; michael@0: let normalTabsLen = tabbrowser.tabs.length - tabbrowser._numPinnedTabs; michael@0: let startupPref = this._prefBranch.getIntPref("startup.page"); michael@0: if (startupPref == 1) michael@0: homePages = homePages.concat(aWindow.gHomeButton.getHomePage().split("|")); michael@0: michael@0: for (let i = tabbrowser._numPinnedTabs; i < tabbrowser.tabs.length; i++) { michael@0: let tab = tabbrowser.tabs[i]; michael@0: if (homePages.indexOf(tab.linkedBrowser.currentURI.spec) != -1) { michael@0: removableTabs.push(tab); michael@0: } michael@0: } michael@0: michael@0: if (tabbrowser.tabs.length == removableTabs.length) { michael@0: canOverwriteTabs = true; michael@0: } michael@0: else { michael@0: // If we're not overwriting all of the tabs, then close the home tabs. michael@0: for (let i = removableTabs.length - 1; i >= 0; i--) { michael@0: tabbrowser.removeTab(removableTabs.pop(), { animate: false }); michael@0: } michael@0: } michael@0: michael@0: return [true, canOverwriteTabs]; michael@0: }, michael@0: michael@0: /* ........ Saving Functionality .............. */ michael@0: michael@0: /** michael@0: * Store window dimensions, visibility, sidebar michael@0: * @param aWindow michael@0: * Window reference michael@0: */ michael@0: _updateWindowFeatures: function ssi_updateWindowFeatures(aWindow) { michael@0: var winData = this._windows[aWindow.__SSi]; michael@0: michael@0: WINDOW_ATTRIBUTES.forEach(function(aAttr) { michael@0: winData[aAttr] = this._getWindowDimension(aWindow, aAttr); michael@0: }, this); michael@0: michael@0: var hidden = WINDOW_HIDEABLE_FEATURES.filter(function(aItem) { michael@0: return aWindow[aItem] && !aWindow[aItem].visible; michael@0: }); michael@0: if (hidden.length != 0) michael@0: winData.hidden = hidden.join(","); michael@0: else if (winData.hidden) michael@0: delete winData.hidden; michael@0: michael@0: var sidebar = aWindow.document.getElementById("sidebar-box").getAttribute("sidebarcommand"); michael@0: if (sidebar) michael@0: winData.sidebar = sidebar; michael@0: else if (winData.sidebar) michael@0: delete winData.sidebar; michael@0: }, michael@0: michael@0: /** michael@0: * gather session data as object michael@0: * @param aUpdateAll michael@0: * Bool update all windows michael@0: * @returns object michael@0: */ michael@0: getCurrentState: function (aUpdateAll) { michael@0: this._handleClosedWindows(); michael@0: michael@0: var activeWindow = this._getMostRecentBrowserWindow(); michael@0: michael@0: TelemetryStopwatch.start("FX_SESSION_RESTORE_COLLECT_ALL_WINDOWS_DATA_MS"); michael@0: if (this._loadState == STATE_RUNNING) { michael@0: // update the data for all windows with activities since the last save operation michael@0: this._forEachBrowserWindow(function(aWindow) { michael@0: if (!this._isWindowLoaded(aWindow)) // window data is still in _statesToRestore michael@0: return; michael@0: if (aUpdateAll || DirtyWindows.has(aWindow) || aWindow == activeWindow) { michael@0: this._collectWindowData(aWindow); michael@0: } michael@0: else { // always update the window features (whose change alone never triggers a save operation) michael@0: this._updateWindowFeatures(aWindow); michael@0: } michael@0: }); michael@0: DirtyWindows.clear(); michael@0: } michael@0: TelemetryStopwatch.finish("FX_SESSION_RESTORE_COLLECT_ALL_WINDOWS_DATA_MS"); michael@0: michael@0: // An array that at the end will hold all current window data. michael@0: var total = []; michael@0: // The ids of all windows contained in 'total' in the same order. michael@0: var ids = []; michael@0: // The number of window that are _not_ popups. michael@0: var nonPopupCount = 0; michael@0: var ix; michael@0: michael@0: // collect the data for all windows michael@0: for (ix in this._windows) { michael@0: if (this._windows[ix]._restoring) // window data is still in _statesToRestore michael@0: continue; michael@0: total.push(this._windows[ix]); michael@0: ids.push(ix); michael@0: if (!this._windows[ix].isPopup) michael@0: nonPopupCount++; michael@0: } michael@0: michael@0: TelemetryStopwatch.start("FX_SESSION_RESTORE_COLLECT_COOKIES_MS"); michael@0: SessionCookies.update(total); michael@0: TelemetryStopwatch.finish("FX_SESSION_RESTORE_COLLECT_COOKIES_MS"); michael@0: michael@0: // collect the data for all windows yet to be restored michael@0: for (ix in this._statesToRestore) { michael@0: for each (let winData in this._statesToRestore[ix].windows) { michael@0: total.push(winData); michael@0: if (!winData.isPopup) michael@0: nonPopupCount++; michael@0: } michael@0: } michael@0: michael@0: // shallow copy this._closedWindows to preserve current state michael@0: let lastClosedWindowsCopy = this._closedWindows.slice(); michael@0: michael@0: #ifndef XP_MACOSX michael@0: // If no non-popup browser window remains open, return the state of the last michael@0: // closed window(s). We only want to do this when we're actually "ending" michael@0: // the session. michael@0: //XXXzpao We should do this for _restoreLastWindow == true, but that has michael@0: // its own check for popups. c.f. bug 597619 michael@0: if (nonPopupCount == 0 && lastClosedWindowsCopy.length > 0 && michael@0: this._loadState == STATE_QUITTING) { michael@0: // prepend the last non-popup browser window, so that if the user loads more tabs michael@0: // at startup we don't accidentally add them to a popup window michael@0: do { michael@0: total.unshift(lastClosedWindowsCopy.shift()) michael@0: } while (total[0].isPopup && lastClosedWindowsCopy.length > 0) michael@0: } michael@0: #endif michael@0: michael@0: if (activeWindow) { michael@0: this.activeWindowSSiCache = activeWindow.__SSi || ""; michael@0: } michael@0: ix = ids.indexOf(this.activeWindowSSiCache); michael@0: // We don't want to restore focus to a minimized window or a window which had all its michael@0: // tabs stripped out (doesn't exist). michael@0: if (ix != -1 && total[ix] && total[ix].sizemode == "minimized") michael@0: ix = -1; michael@0: michael@0: let session = { michael@0: lastUpdate: Date.now(), michael@0: startTime: this._sessionStartTime, michael@0: recentCrashes: this._recentCrashes michael@0: }; michael@0: michael@0: // get open Scratchpad window states too michael@0: let scratchpads = ScratchpadManager.getSessionState(); michael@0: michael@0: let state = { michael@0: windows: total, michael@0: selectedWindow: ix + 1, michael@0: _closedWindows: lastClosedWindowsCopy, michael@0: session: session, michael@0: scratchpads: scratchpads, michael@0: global: this._globalState.getState() michael@0: }; michael@0: michael@0: // Persist the last session if we deferred restoring it michael@0: if (LastSession.canRestore) { michael@0: state.lastSessionState = LastSession.getState(); michael@0: } michael@0: michael@0: // If we were called by the SessionSaver and started with only a private michael@0: // window we want to pass the deferred initial state to not lose the michael@0: // previous session. michael@0: if (this._deferredInitialState) { michael@0: state.deferredInitialState = this._deferredInitialState; michael@0: } michael@0: michael@0: return state; michael@0: }, michael@0: michael@0: /** michael@0: * serialize session data for a window michael@0: * @param aWindow michael@0: * Window reference michael@0: * @returns string michael@0: */ michael@0: _getWindowState: function ssi_getWindowState(aWindow) { michael@0: if (!this._isWindowLoaded(aWindow)) michael@0: return this._statesToRestore[aWindow.__SS_restoreID]; michael@0: michael@0: if (this._loadState == STATE_RUNNING) { michael@0: this._collectWindowData(aWindow); michael@0: } michael@0: michael@0: let windows = [this._windows[aWindow.__SSi]]; michael@0: SessionCookies.update(windows); michael@0: michael@0: return { windows: windows }; michael@0: }, michael@0: michael@0: _collectWindowData: function ssi_collectWindowData(aWindow) { michael@0: if (!this._isWindowLoaded(aWindow)) michael@0: return; michael@0: TelemetryStopwatch.start("FX_SESSION_RESTORE_COLLECT_SINGLE_WINDOW_DATA_MS"); michael@0: michael@0: let tabbrowser = aWindow.gBrowser; michael@0: let tabs = tabbrowser.tabs; michael@0: let winData = this._windows[aWindow.__SSi]; michael@0: let tabsData = winData.tabs = []; michael@0: michael@0: // update the internal state data for this window michael@0: for (let tab of tabs) { michael@0: tabsData.push(TabState.collect(tab)); michael@0: } michael@0: winData.selected = tabbrowser.mTabBox.selectedIndex + 1; michael@0: michael@0: this._updateWindowFeatures(aWindow); michael@0: michael@0: // Make sure we keep __SS_lastSessionWindowID around for cases like entering michael@0: // or leaving PB mode. michael@0: if (aWindow.__SS_lastSessionWindowID) michael@0: this._windows[aWindow.__SSi].__lastSessionWindowID = michael@0: aWindow.__SS_lastSessionWindowID; michael@0: michael@0: DirtyWindows.remove(aWindow); michael@0: TelemetryStopwatch.finish("FX_SESSION_RESTORE_COLLECT_SINGLE_WINDOW_DATA_MS"); michael@0: }, michael@0: michael@0: /* ........ Restoring Functionality .............. */ michael@0: michael@0: /** michael@0: * restore features to a single window michael@0: * @param aWindow michael@0: * Window reference michael@0: * @param aState michael@0: * JS object or its eval'able source michael@0: * @param aOptions michael@0: * {overwriteTabs: true} to overwrite existing tabs w/ new ones michael@0: * {isFollowUp: true} if this is not the restoration of the 1st window michael@0: * {firstWindow: true} if this is the first non-private window we're michael@0: * restoring in this session, that might open an michael@0: * external link as well michael@0: */ michael@0: restoreWindow: function ssi_restoreWindow(aWindow, aState, aOptions = {}) { michael@0: let overwriteTabs = aOptions && aOptions.overwriteTabs; michael@0: let isFollowUp = aOptions && aOptions.isFollowUp; michael@0: let firstWindow = aOptions && aOptions.firstWindow; michael@0: michael@0: if (isFollowUp) { michael@0: this.windowToFocus = aWindow; michael@0: } michael@0: // initialize window if necessary michael@0: if (aWindow && (!aWindow.__SSi || !this._windows[aWindow.__SSi])) michael@0: this.onLoad(aWindow); michael@0: michael@0: try { michael@0: var root = typeof aState == "string" ? JSON.parse(aState) : aState; michael@0: if (!root.windows[0]) { michael@0: this._sendRestoreCompletedNotifications(); michael@0: return; // nothing to restore michael@0: } michael@0: } michael@0: catch (ex) { // invalid state object - don't restore anything michael@0: debug(ex); michael@0: this._sendRestoreCompletedNotifications(); michael@0: return; michael@0: } michael@0: michael@0: TelemetryStopwatch.start("FX_SESSION_RESTORE_RESTORE_WINDOW_MS"); michael@0: michael@0: // We're not returning from this before we end up calling restoreTabs michael@0: // for this window, so make sure we send the SSWindowStateBusy event. michael@0: this._setWindowStateBusy(aWindow); michael@0: michael@0: if (root._closedWindows) michael@0: this._closedWindows = root._closedWindows; michael@0: michael@0: var winData; michael@0: if (!root.selectedWindow || root.selectedWindow > root.windows.length) { michael@0: root.selectedWindow = 0; michael@0: } michael@0: michael@0: // open new windows for all further window entries of a multi-window session michael@0: // (unless they don't contain any tab data) michael@0: for (var w = 1; w < root.windows.length; w++) { michael@0: winData = root.windows[w]; michael@0: if (winData && winData.tabs && winData.tabs[0]) { michael@0: var window = this._openWindowWithState({ windows: [winData] }); michael@0: if (w == root.selectedWindow - 1) { michael@0: this.windowToFocus = window; michael@0: } michael@0: } michael@0: } michael@0: winData = root.windows[0]; michael@0: if (!winData.tabs) { michael@0: winData.tabs = []; michael@0: } michael@0: // don't restore a single blank tab when we've had an external michael@0: // URL passed in for loading at startup (cf. bug 357419) michael@0: else if (firstWindow && !overwriteTabs && winData.tabs.length == 1 && michael@0: (!winData.tabs[0].entries || winData.tabs[0].entries.length == 0)) { michael@0: winData.tabs = []; michael@0: } michael@0: michael@0: var tabbrowser = aWindow.gBrowser; michael@0: var openTabCount = overwriteTabs ? tabbrowser.browsers.length : -1; michael@0: var newTabCount = winData.tabs.length; michael@0: var tabs = []; michael@0: michael@0: // disable smooth scrolling while adding, moving, removing and selecting tabs michael@0: var tabstrip = tabbrowser.tabContainer.mTabstrip; michael@0: var smoothScroll = tabstrip.smoothScroll; michael@0: tabstrip.smoothScroll = false; michael@0: michael@0: // unpin all tabs to ensure they are not reordered in the next loop michael@0: if (overwriteTabs) { michael@0: for (let t = tabbrowser._numPinnedTabs - 1; t > -1; t--) michael@0: tabbrowser.unpinTab(tabbrowser.tabs[t]); michael@0: } michael@0: michael@0: // We need to keep track of the initially open tabs so that they michael@0: // can be moved to the end of the restored tabs. michael@0: let initialTabs = []; michael@0: if (!overwriteTabs && firstWindow) { michael@0: initialTabs = Array.slice(tabbrowser.tabs); michael@0: } michael@0: michael@0: // make sure that the selected tab won't be closed in order to michael@0: // prevent unnecessary flickering michael@0: if (overwriteTabs && tabbrowser.selectedTab._tPos >= newTabCount) michael@0: tabbrowser.moveTabTo(tabbrowser.selectedTab, newTabCount - 1); michael@0: michael@0: let numVisibleTabs = 0; michael@0: michael@0: for (var t = 0; t < newTabCount; t++) { michael@0: tabs.push(t < openTabCount ? michael@0: tabbrowser.tabs[t] : michael@0: tabbrowser.addTab("about:blank", {skipAnimation: true})); michael@0: michael@0: if (winData.tabs[t].pinned) michael@0: tabbrowser.pinTab(tabs[t]); michael@0: michael@0: if (winData.tabs[t].hidden) { michael@0: tabbrowser.hideTab(tabs[t]); michael@0: } michael@0: else { michael@0: tabbrowser.showTab(tabs[t]); michael@0: numVisibleTabs++; michael@0: } michael@0: } michael@0: michael@0: if (!overwriteTabs && firstWindow) { michael@0: // Move the originally open tabs to the end michael@0: let endPosition = tabbrowser.tabs.length - 1; michael@0: for (let i = 0; i < initialTabs.length; i++) { michael@0: tabbrowser.moveTabTo(initialTabs[i], endPosition); michael@0: } michael@0: } michael@0: michael@0: // if all tabs to be restored are hidden, make the first one visible michael@0: if (!numVisibleTabs && winData.tabs.length) { michael@0: winData.tabs[0].hidden = false; michael@0: tabbrowser.showTab(tabs[0]); michael@0: } michael@0: michael@0: // If overwriting tabs, we want to reset each tab's "restoring" state. Since michael@0: // we're overwriting those tabs, they should no longer be restoring. The michael@0: // tabs will be rebuilt and marked if they need to be restored after loading michael@0: // state (in restoreTabs). michael@0: if (overwriteTabs) { michael@0: for (let i = 0; i < tabbrowser.tabs.length; i++) { michael@0: let tab = tabbrowser.tabs[i]; michael@0: if (tabbrowser.browsers[i].__SS_restoreState) michael@0: this._resetTabRestoringState(tab); michael@0: } michael@0: } michael@0: michael@0: // We want to correlate the window with data from the last session, so michael@0: // assign another id if we have one. Otherwise clear so we don't do michael@0: // anything with it. michael@0: delete aWindow.__SS_lastSessionWindowID; michael@0: if (winData.__lastSessionWindowID) michael@0: aWindow.__SS_lastSessionWindowID = winData.__lastSessionWindowID; michael@0: michael@0: // when overwriting tabs, remove all superflous ones michael@0: if (overwriteTabs && newTabCount < openTabCount) { michael@0: Array.slice(tabbrowser.tabs, newTabCount, openTabCount) michael@0: .forEach(tabbrowser.removeTab, tabbrowser); michael@0: } michael@0: michael@0: if (overwriteTabs) { michael@0: this.restoreWindowFeatures(aWindow, winData); michael@0: delete this._windows[aWindow.__SSi].extData; michael@0: } michael@0: if (winData.cookies) { michael@0: this.restoreCookies(winData.cookies); michael@0: } michael@0: if (winData.extData) { michael@0: if (!this._windows[aWindow.__SSi].extData) { michael@0: this._windows[aWindow.__SSi].extData = {}; michael@0: } michael@0: for (var key in winData.extData) { michael@0: this._windows[aWindow.__SSi].extData[key] = winData.extData[key]; michael@0: } michael@0: } michael@0: michael@0: let newClosedTabsData = winData._closedTabs || []; michael@0: michael@0: if (overwriteTabs || firstWindow) { michael@0: // Overwrite existing closed tabs data when overwriteTabs=true michael@0: // or we're the first window to be restored. michael@0: this._windows[aWindow.__SSi]._closedTabs = newClosedTabsData; michael@0: } else if (this._max_tabs_undo > 0) { michael@0: // If we merge tabs, we also want to merge closed tabs data. We'll assume michael@0: // the restored tabs were closed more recently and append the current list michael@0: // of closed tabs to the new one... michael@0: newClosedTabsData = michael@0: newClosedTabsData.concat(this._windows[aWindow.__SSi]._closedTabs); michael@0: michael@0: // ... and make sure that we don't exceed the max number of closed tabs michael@0: // we can restore. michael@0: this._windows[aWindow.__SSi]._closedTabs = michael@0: newClosedTabsData.slice(0, this._max_tabs_undo); michael@0: } michael@0: michael@0: this.restoreTabs(aWindow, tabs, winData.tabs, michael@0: (overwriteTabs ? (parseInt(winData.selected || "1")) : 0)); michael@0: michael@0: if (aState.scratchpads) { michael@0: ScratchpadManager.restoreSession(aState.scratchpads); michael@0: } michael@0: michael@0: // set smoothScroll back to the original value michael@0: tabstrip.smoothScroll = smoothScroll; michael@0: michael@0: TelemetryStopwatch.finish("FX_SESSION_RESTORE_RESTORE_WINDOW_MS"); michael@0: michael@0: this._sendRestoreCompletedNotifications(); michael@0: }, michael@0: michael@0: /** michael@0: * Sets the tabs restoring order with the following priority: michael@0: * Selected tab, pinned tabs, optimized visible tabs, other visible tabs and michael@0: * hidden tabs. michael@0: * @param aTabBrowser michael@0: * Tab browser object michael@0: * @param aTabs michael@0: * Array of tab references michael@0: * @param aTabData michael@0: * Array of tab data michael@0: * @param aSelectedTab michael@0: * Index of selected tab (1 is first tab, 0 no selected tab) michael@0: */ michael@0: _setTabsRestoringOrder : function ssi__setTabsRestoringOrder( michael@0: aTabBrowser, aTabs, aTabData, aSelectedTab) { michael@0: michael@0: // Store the selected tab. Need to substract one to get the index in aTabs. michael@0: let selectedTab; michael@0: if (aSelectedTab > 0 && aTabs[aSelectedTab - 1]) { michael@0: selectedTab = aTabs[aSelectedTab - 1]; michael@0: } michael@0: michael@0: // Store the pinned tabs and hidden tabs. michael@0: let pinnedTabs = []; michael@0: let pinnedTabsData = []; michael@0: let hiddenTabs = []; michael@0: let hiddenTabsData = []; michael@0: if (aTabs.length > 1) { michael@0: for (let t = aTabs.length - 1; t >= 0; t--) { michael@0: if (aTabData[t].pinned) { michael@0: pinnedTabs.unshift(aTabs.splice(t, 1)[0]); michael@0: pinnedTabsData.unshift(aTabData.splice(t, 1)[0]); michael@0: } else if (aTabData[t].hidden) { michael@0: hiddenTabs.unshift(aTabs.splice(t, 1)[0]); michael@0: hiddenTabsData.unshift(aTabData.splice(t, 1)[0]); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Optimize the visible tabs only if there is a selected tab. michael@0: if (selectedTab) { michael@0: let selectedTabIndex = aTabs.indexOf(selectedTab); michael@0: if (selectedTabIndex > 0) { michael@0: let scrollSize = aTabBrowser.tabContainer.mTabstrip.scrollClientSize; michael@0: let tabWidth = aTabs[0].getBoundingClientRect().width; michael@0: let maxVisibleTabs = Math.ceil(scrollSize / tabWidth); michael@0: if (maxVisibleTabs < aTabs.length) { michael@0: let firstVisibleTab = 0; michael@0: let nonVisibleTabsCount = aTabs.length - maxVisibleTabs; michael@0: if (nonVisibleTabsCount >= selectedTabIndex) { michael@0: // Selected tab is leftmost since we scroll to it when possible. michael@0: firstVisibleTab = selectedTabIndex; michael@0: } else { michael@0: // Selected tab is rightmost or no more room to scroll right. michael@0: firstVisibleTab = nonVisibleTabsCount; michael@0: } michael@0: aTabs = aTabs.splice(firstVisibleTab, maxVisibleTabs).concat(aTabs); michael@0: aTabData = michael@0: aTabData.splice(firstVisibleTab, maxVisibleTabs).concat(aTabData); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Merge the stored tabs in order. michael@0: aTabs = pinnedTabs.concat(aTabs, hiddenTabs); michael@0: aTabData = pinnedTabsData.concat(aTabData, hiddenTabsData); michael@0: michael@0: // Load the selected tab to the first position and select it. michael@0: if (selectedTab) { michael@0: let selectedTabIndex = aTabs.indexOf(selectedTab); michael@0: if (selectedTabIndex > 0) { michael@0: aTabs = aTabs.splice(selectedTabIndex, 1).concat(aTabs); michael@0: aTabData = aTabData.splice(selectedTabIndex, 1).concat(aTabData); michael@0: } michael@0: aTabBrowser.selectedTab = selectedTab; michael@0: } michael@0: michael@0: return [aTabs, aTabData]; michael@0: }, michael@0: michael@0: /** michael@0: * Manage history restoration for a window michael@0: * @param aWindow michael@0: * Window to restore the tabs into michael@0: * @param aTabs michael@0: * Array of tab references michael@0: * @param aTabData michael@0: * Array of tab data michael@0: * @param aSelectTab michael@0: * Index of selected tab michael@0: * @param aRestoreImmediately michael@0: * Flag to indicate whether the given set of tabs aTabs should be michael@0: * restored/loaded immediately even if restore_on_demand = true michael@0: */ michael@0: restoreTabs: function (aWindow, aTabs, aTabData, aSelectTab, michael@0: aRestoreImmediately = false) michael@0: { michael@0: michael@0: var tabbrowser = aWindow.gBrowser; michael@0: michael@0: if (!this._isWindowLoaded(aWindow)) { michael@0: // from now on, the data will come from the actual window michael@0: delete this._statesToRestore[aWindow.__SS_restoreID]; michael@0: delete aWindow.__SS_restoreID; michael@0: delete this._windows[aWindow.__SSi]._restoring; michael@0: } michael@0: michael@0: // It's important to set the window state to dirty so that michael@0: // we collect their data for the first time when saving state. michael@0: DirtyWindows.add(aWindow); michael@0: michael@0: // Set the state to restore as the window's current state. Normally, this michael@0: // will just be overridden the next time we collect state but we need this michael@0: // as a fallback should Firefox be shutdown early without notifying us michael@0: // beforehand. michael@0: this._windows[aWindow.__SSi].tabs = aTabData.slice(); michael@0: this._windows[aWindow.__SSi].selected = aSelectTab; michael@0: michael@0: if (aTabs.length == 0) { michael@0: // This is normally done later, but as we're returning early michael@0: // here we need to take care of it. michael@0: this._setWindowStateReady(aWindow); michael@0: return; michael@0: } michael@0: michael@0: // Sets the tabs restoring order. michael@0: [aTabs, aTabData] = michael@0: this._setTabsRestoringOrder(tabbrowser, aTabs, aTabData, aSelectTab); michael@0: michael@0: // Prepare the tabs so that they can be properly restored. We'll pin/unpin michael@0: // and show/hide tabs as necessary. We'll also set the labels, user typed michael@0: // value, and attach a copy of the tab's data in case we close it before michael@0: // it's been restored. michael@0: for (let t = 0; t < aTabs.length; t++) { michael@0: let tab = aTabs[t]; michael@0: let browser = tabbrowser.getBrowserForTab(tab); michael@0: let tabData = aTabData[t]; michael@0: michael@0: if (tabData.pinned) michael@0: tabbrowser.pinTab(tab); michael@0: else michael@0: tabbrowser.unpinTab(tab); michael@0: michael@0: if (tabData.hidden) michael@0: tabbrowser.hideTab(tab); michael@0: else michael@0: tabbrowser.showTab(tab); michael@0: michael@0: if (tabData.lastAccessed) { michael@0: tab.lastAccessed = tabData.lastAccessed; michael@0: } michael@0: michael@0: if ("attributes" in tabData) { michael@0: // Ensure that we persist tab attributes restored from previous sessions. michael@0: Object.keys(tabData.attributes).forEach(a => TabAttributes.persist(a)); michael@0: } michael@0: michael@0: if (!tabData.entries) { michael@0: tabData.entries = []; michael@0: } michael@0: if (tabData.extData) { michael@0: tab.__SS_extdata = {}; michael@0: for (let key in tabData.extData) michael@0: tab.__SS_extdata[key] = tabData.extData[key]; michael@0: } else { michael@0: delete tab.__SS_extdata; michael@0: } michael@0: michael@0: // Flush all data from the content script synchronously. This is done so michael@0: // that all async messages that are still on their way to chrome will michael@0: // be ignored and don't override any tab data set when restoring. michael@0: TabState.flush(tab.linkedBrowser); michael@0: michael@0: // Ensure the index is in bounds. michael@0: let activeIndex = (tabData.index || tabData.entries.length) - 1; michael@0: activeIndex = Math.min(activeIndex, tabData.entries.length - 1); michael@0: activeIndex = Math.max(activeIndex, 0); michael@0: michael@0: // Save the index in case we updated it above. michael@0: tabData.index = activeIndex + 1; michael@0: michael@0: // In electrolysis, we may need to change the browser's remote michael@0: // attribute so that it runs in a content process. michael@0: let activePageData = tabData.entries[activeIndex] || null; michael@0: let uri = activePageData ? activePageData.url || null : null; michael@0: tabbrowser.updateBrowserRemoteness(browser, uri); michael@0: michael@0: // Start a new epoch and include the epoch in the restoreHistory michael@0: // message. If a message is received that relates to a previous epoch, we michael@0: // discard it. michael@0: let epoch = this._nextRestoreEpoch++; michael@0: this._browserEpochs.set(browser.permanentKey, epoch); michael@0: michael@0: // keep the data around to prevent dataloss in case michael@0: // a tab gets closed before it's been properly restored michael@0: browser.__SS_data = tabData; michael@0: browser.__SS_restoreState = TAB_STATE_NEEDS_RESTORE; michael@0: browser.setAttribute("pending", "true"); michael@0: tab.setAttribute("pending", "true"); michael@0: michael@0: // Update the persistent tab state cache with |tabData| information. michael@0: TabStateCache.update(browser, { michael@0: history: {entries: tabData.entries, index: tabData.index}, michael@0: scroll: tabData.scroll || null, michael@0: storage: tabData.storage || null, michael@0: formdata: tabData.formdata || null, michael@0: disallow: tabData.disallow || null, michael@0: pageStyle: tabData.pageStyle || null michael@0: }); michael@0: michael@0: browser.messageManager.sendAsyncMessage("SessionStore:restoreHistory", michael@0: {tabData: tabData, epoch: epoch}); michael@0: michael@0: // Restore tab attributes. michael@0: if ("attributes" in tabData) { michael@0: TabAttributes.set(tab, tabData.attributes); michael@0: } michael@0: michael@0: // This could cause us to ignore MAX_CONCURRENT_TAB_RESTORES a bit, but michael@0: // it ensures each window will have its selected tab loaded. michael@0: if (aRestoreImmediately || tabbrowser.selectedBrowser == browser) { michael@0: this.restoreTabContent(tab); michael@0: } else { michael@0: TabRestoreQueue.add(tab); michael@0: this.restoreNextTab(); michael@0: } michael@0: } michael@0: michael@0: this._setWindowStateReady(aWindow); michael@0: }, michael@0: michael@0: /** michael@0: * Restores the specified tab. If the tab can't be restored (eg, no history or michael@0: * calling gotoIndex fails), then state changes will be rolled back. michael@0: * This method will check if gTabsProgressListener is attached to the tab's michael@0: * window, ensuring that we don't get caught without one. michael@0: * This method removes the session history listener right before starting to michael@0: * attempt a load. This will prevent cases of "stuck" listeners. michael@0: * If this method returns false, then it is up to the caller to decide what to michael@0: * do. In the common case (restoreNextTab), we will want to then attempt to michael@0: * restore the next tab. In the other case (selecting the tab, reloading the michael@0: * tab), the caller doesn't actually want to do anything if no page is loaded. michael@0: * michael@0: * @param aTab michael@0: * the tab to restore michael@0: * michael@0: * @returns true/false indicating whether or not a load actually happened michael@0: */ michael@0: restoreTabContent: function (aTab) { michael@0: let window = aTab.ownerDocument.defaultView; michael@0: let browser = aTab.linkedBrowser; michael@0: let tabData = browser.__SS_data; michael@0: michael@0: // Make sure that this tab is removed from the priority queue. michael@0: TabRestoreQueue.remove(aTab); michael@0: michael@0: // Increase our internal count. michael@0: this._tabsRestoringCount++; michael@0: michael@0: // Set this tab's state to restoring michael@0: browser.__SS_restoreState = TAB_STATE_RESTORING; michael@0: browser.removeAttribute("pending"); michael@0: aTab.removeAttribute("pending"); michael@0: michael@0: let activeIndex = tabData.index - 1; michael@0: michael@0: // Attach data that will be restored on "load" event, after tab is restored. michael@0: if (tabData.entries.length) { michael@0: // restore those aspects of the currently active documents which are not michael@0: // preserved in the plain history entries (mainly scroll state and text data) michael@0: browser.__SS_restore_data = tabData.entries[activeIndex] || {}; michael@0: } else { michael@0: browser.__SS_restore_data = {}; michael@0: } michael@0: michael@0: browser.__SS_restore_tab = aTab; michael@0: michael@0: browser.messageManager.sendAsyncMessage("SessionStore:restoreTabContent"); michael@0: }, michael@0: michael@0: /** michael@0: * This _attempts_ to restore the next available tab. If the restore fails, michael@0: * then we will attempt the next one. michael@0: * There are conditions where this won't do anything: michael@0: * if we're in the process of quitting michael@0: * if there are no tabs to restore michael@0: * if we have already reached the limit for number of tabs to restore michael@0: */ michael@0: restoreNextTab: function ssi_restoreNextTab() { michael@0: // If we call in here while quitting, we don't actually want to do anything michael@0: if (this._loadState == STATE_QUITTING) michael@0: return; michael@0: michael@0: // Don't exceed the maximum number of concurrent tab restores. michael@0: if (this._tabsRestoringCount >= MAX_CONCURRENT_TAB_RESTORES) michael@0: return; michael@0: michael@0: let tab = TabRestoreQueue.shift(); michael@0: if (tab) { michael@0: this.restoreTabContent(tab); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Restore visibility and dimension features to a window michael@0: * @param aWindow michael@0: * Window reference michael@0: * @param aWinData michael@0: * Object containing session data for the window michael@0: */ michael@0: restoreWindowFeatures: function ssi_restoreWindowFeatures(aWindow, aWinData) { michael@0: var hidden = (aWinData.hidden)?aWinData.hidden.split(","):[]; michael@0: WINDOW_HIDEABLE_FEATURES.forEach(function(aItem) { michael@0: aWindow[aItem].visible = hidden.indexOf(aItem) == -1; michael@0: }); michael@0: michael@0: if (aWinData.isPopup) { michael@0: this._windows[aWindow.__SSi].isPopup = true; michael@0: if (aWindow.gURLBar) { michael@0: aWindow.gURLBar.readOnly = true; michael@0: aWindow.gURLBar.setAttribute("enablehistory", "false"); michael@0: } michael@0: } michael@0: else { michael@0: delete this._windows[aWindow.__SSi].isPopup; michael@0: if (aWindow.gURLBar) { michael@0: aWindow.gURLBar.readOnly = false; michael@0: aWindow.gURLBar.setAttribute("enablehistory", "true"); michael@0: } michael@0: } michael@0: michael@0: var _this = this; michael@0: aWindow.setTimeout(function() { michael@0: _this.restoreDimensions.apply(_this, [aWindow, michael@0: +aWinData.width || 0, michael@0: +aWinData.height || 0, michael@0: "screenX" in aWinData ? +aWinData.screenX : NaN, michael@0: "screenY" in aWinData ? +aWinData.screenY : NaN, michael@0: aWinData.sizemode || "", aWinData.sidebar || ""]); michael@0: }, 0); michael@0: }, michael@0: michael@0: /** michael@0: * Restore a window's dimensions michael@0: * @param aWidth michael@0: * Window width michael@0: * @param aHeight michael@0: * Window height michael@0: * @param aLeft michael@0: * Window left michael@0: * @param aTop michael@0: * Window top michael@0: * @param aSizeMode michael@0: * Window size mode (eg: maximized) michael@0: * @param aSidebar michael@0: * Sidebar command michael@0: */ michael@0: restoreDimensions: function ssi_restoreDimensions(aWindow, aWidth, aHeight, aLeft, aTop, aSizeMode, aSidebar) { michael@0: var win = aWindow; michael@0: var _this = this; michael@0: function win_(aName) { return _this._getWindowDimension(win, aName); } michael@0: michael@0: // find available space on the screen where this window is being placed michael@0: let screen = gScreenManager.screenForRect(aLeft, aTop, aWidth, aHeight); michael@0: if (screen) { michael@0: let screenLeft = {}, screenTop = {}, screenWidth = {}, screenHeight = {}; michael@0: screen.GetAvailRectDisplayPix(screenLeft, screenTop, screenWidth, screenHeight); michael@0: // constrain the dimensions to the actual space available michael@0: if (aWidth > screenWidth.value) { michael@0: aWidth = screenWidth.value; michael@0: } michael@0: if (aHeight > screenHeight.value) { michael@0: aHeight = screenHeight.value; michael@0: } michael@0: // and then pull the window within the screen's bounds michael@0: if (aLeft < screenLeft.value) { michael@0: aLeft = screenLeft.value; michael@0: } else if (aLeft + aWidth > screenLeft.value + screenWidth.value) { michael@0: aLeft = screenLeft.value + screenWidth.value - aWidth; michael@0: } michael@0: if (aTop < screenTop.value) { michael@0: aTop = screenTop.value; michael@0: } else if (aTop + aHeight > screenTop.value + screenHeight.value) { michael@0: aTop = screenTop.value + screenHeight.value - aHeight; michael@0: } michael@0: } michael@0: michael@0: // only modify those aspects which aren't correct yet michael@0: if (aWidth && aHeight && (aWidth != win_("width") || aHeight != win_("height"))) { michael@0: // Don't resize the window if it's currently maximized and we would michael@0: // maximize it again shortly after. michael@0: if (aSizeMode != "maximized" || win_("sizemode") != "maximized") { michael@0: aWindow.resizeTo(aWidth, aHeight); michael@0: } michael@0: } michael@0: if (!isNaN(aLeft) && !isNaN(aTop) && (aLeft != win_("screenX") || aTop != win_("screenY"))) { michael@0: aWindow.moveTo(aLeft, aTop); michael@0: } michael@0: if (aSizeMode && win_("sizemode") != aSizeMode) michael@0: { michael@0: switch (aSizeMode) michael@0: { michael@0: case "maximized": michael@0: aWindow.maximize(); michael@0: break; michael@0: case "minimized": michael@0: aWindow.minimize(); michael@0: break; michael@0: case "normal": michael@0: aWindow.restore(); michael@0: break; michael@0: } michael@0: } michael@0: var sidebar = aWindow.document.getElementById("sidebar-box"); michael@0: if (sidebar.getAttribute("sidebarcommand") != aSidebar) { michael@0: aWindow.toggleSidebar(aSidebar); michael@0: } michael@0: // since resizing/moving a window brings it to the foreground, michael@0: // we might want to re-focus the last focused window michael@0: if (this.windowToFocus) { michael@0: this.windowToFocus.focus(); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Restores cookies michael@0: * @param aCookies michael@0: * Array of cookie objects michael@0: */ michael@0: restoreCookies: function ssi_restoreCookies(aCookies) { michael@0: // MAX_EXPIRY should be 2^63-1, but JavaScript can't handle that precision michael@0: var MAX_EXPIRY = Math.pow(2, 62); michael@0: for (let i = 0; i < aCookies.length; i++) { michael@0: var cookie = aCookies[i]; michael@0: try { michael@0: Services.cookies.add(cookie.host, cookie.path || "", cookie.name || "", michael@0: cookie.value, !!cookie.secure, !!cookie.httponly, true, michael@0: "expiry" in cookie ? cookie.expiry : MAX_EXPIRY); michael@0: } michael@0: catch (ex) { console.error(ex); } // don't let a single cookie stop recovering michael@0: } michael@0: }, michael@0: michael@0: /* ........ Disk Access .............. */ michael@0: michael@0: /** michael@0: * Save the current session state to disk, after a delay. michael@0: * michael@0: * @param aWindow (optional) michael@0: * Will mark the given window as dirty so that we will recollect its michael@0: * data before we start writing. michael@0: */ michael@0: saveStateDelayed: function (aWindow = null) { michael@0: if (aWindow) { michael@0: DirtyWindows.add(aWindow); michael@0: } michael@0: michael@0: SessionSaver.runDelayed(); michael@0: }, michael@0: michael@0: /* ........ Auxiliary Functions .............. */ michael@0: michael@0: /** michael@0: * Update the session start time and send a telemetry measurement michael@0: * for the number of days elapsed since the session was started. michael@0: * michael@0: * @param state michael@0: * The session state. michael@0: */ michael@0: _updateSessionStartTime: function ssi_updateSessionStartTime(state) { michael@0: // Attempt to load the session start time from the session state michael@0: if (state.session && state.session.startTime) { michael@0: this._sessionStartTime = state.session.startTime; michael@0: michael@0: // ms to days michael@0: let sessionLength = (Date.now() - this._sessionStartTime) / MS_PER_DAY; michael@0: michael@0: if (sessionLength > 0) { michael@0: // Submit the session length telemetry measurement michael@0: Services.telemetry.getHistogramById("FX_SESSION_RESTORE_SESSION_LENGTH").add(sessionLength); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * call a callback for all currently opened browser windows michael@0: * (might miss the most recent one) michael@0: * @param aFunc michael@0: * Callback each window is passed to michael@0: */ michael@0: _forEachBrowserWindow: function ssi_forEachBrowserWindow(aFunc) { michael@0: var windowsEnum = Services.wm.getEnumerator("navigator:browser"); michael@0: michael@0: while (windowsEnum.hasMoreElements()) { michael@0: var window = windowsEnum.getNext(); michael@0: if (window.__SSi && !window.closed) { michael@0: aFunc.call(this, window); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Returns most recent window michael@0: * @returns Window reference michael@0: */ michael@0: _getMostRecentBrowserWindow: function ssi_getMostRecentBrowserWindow() { michael@0: return RecentWindow.getMostRecentBrowserWindow({ allowPopups: true }); michael@0: }, michael@0: michael@0: /** michael@0: * Calls onClose for windows that are determined to be closed but aren't michael@0: * destroyed yet, which would otherwise cause getBrowserState and michael@0: * setBrowserState to treat them as open windows. michael@0: */ michael@0: _handleClosedWindows: function ssi_handleClosedWindows() { michael@0: var windowsEnum = Services.wm.getEnumerator("navigator:browser"); michael@0: michael@0: while (windowsEnum.hasMoreElements()) { michael@0: var window = windowsEnum.getNext(); michael@0: if (window.closed) { michael@0: this.onClose(window); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * open a new browser window for a given session state michael@0: * called when restoring a multi-window session michael@0: * @param aState michael@0: * Object containing session data michael@0: */ michael@0: _openWindowWithState: function ssi_openWindowWithState(aState) { michael@0: var argString = Cc["@mozilla.org/supports-string;1"]. michael@0: createInstance(Ci.nsISupportsString); michael@0: argString.data = ""; michael@0: michael@0: // Build feature string michael@0: let features = "chrome,dialog=no,macsuppressanimation,all"; michael@0: let winState = aState.windows[0]; michael@0: WINDOW_ATTRIBUTES.forEach(function(aFeature) { michael@0: // Use !isNaN as an easy way to ignore sizemode and check for numbers michael@0: if (aFeature in winState && !isNaN(winState[aFeature])) michael@0: features += "," + aFeature + "=" + winState[aFeature]; michael@0: }); michael@0: michael@0: if (winState.isPrivate) { michael@0: features += ",private"; michael@0: } michael@0: michael@0: var window = michael@0: Services.ww.openWindow(null, this._prefBranch.getCharPref("chromeURL"), michael@0: "_blank", features, argString); michael@0: michael@0: do { michael@0: var ID = "window" + Math.random(); michael@0: } while (ID in this._statesToRestore); michael@0: this._statesToRestore[(window.__SS_restoreID = ID)] = aState; michael@0: michael@0: return window; michael@0: }, michael@0: michael@0: /** michael@0: * Gets the tab for the given browser. This should be marginally better michael@0: * than using tabbrowser's getTabForContentWindow. This assumes the browser michael@0: * is the linkedBrowser of a tab, not a dangling browser. michael@0: * michael@0: * @param aBrowser michael@0: * The browser from which to get the tab. michael@0: */ michael@0: _getTabForBrowser: function ssi_getTabForBrowser(aBrowser) { michael@0: let window = aBrowser.ownerDocument.defaultView; michael@0: for (let i = 0; i < window.gBrowser.tabs.length; i++) { michael@0: let tab = window.gBrowser.tabs[i]; michael@0: if (tab.linkedBrowser == aBrowser) michael@0: return tab; michael@0: } michael@0: return undefined; michael@0: }, michael@0: michael@0: /** michael@0: * Whether or not to resume session, if not recovering from a crash. michael@0: * @returns bool michael@0: */ michael@0: _doResumeSession: function ssi_doResumeSession() { michael@0: return this._prefBranch.getIntPref("startup.page") == 3 || michael@0: this._prefBranch.getBoolPref("sessionstore.resume_session_once"); michael@0: }, michael@0: michael@0: /** michael@0: * whether the user wants to load any other page at startup michael@0: * (except the homepage) - needed for determining whether to overwrite the current tabs michael@0: * C.f.: nsBrowserContentHandler's defaultArgs implementation. michael@0: * @returns bool michael@0: */ michael@0: _isCmdLineEmpty: function ssi_isCmdLineEmpty(aWindow, aState) { michael@0: var pinnedOnly = aState.windows && michael@0: aState.windows.every(function (win) michael@0: win.tabs.every(function (tab) tab.pinned)); michael@0: michael@0: let hasFirstArgument = aWindow.arguments && aWindow.arguments[0]; michael@0: if (!pinnedOnly) { michael@0: let defaultArgs = Cc["@mozilla.org/browser/clh;1"]. michael@0: getService(Ci.nsIBrowserHandler).defaultArgs; michael@0: if (aWindow.arguments && michael@0: aWindow.arguments[0] && michael@0: aWindow.arguments[0] == defaultArgs) michael@0: hasFirstArgument = false; michael@0: } michael@0: michael@0: return !hasFirstArgument; michael@0: }, michael@0: michael@0: /** michael@0: * on popup windows, the XULWindow's attributes seem not to be set correctly michael@0: * we use thus JSDOMWindow attributes for sizemode and normal window attributes michael@0: * (and hope for reasonable values when maximized/minimized - since then michael@0: * outerWidth/outerHeight aren't the dimensions of the restored window) michael@0: * @param aWindow michael@0: * Window reference michael@0: * @param aAttribute michael@0: * String sizemode | width | height | other window attribute michael@0: * @returns string michael@0: */ michael@0: _getWindowDimension: function ssi_getWindowDimension(aWindow, aAttribute) { michael@0: if (aAttribute == "sizemode") { michael@0: switch (aWindow.windowState) { michael@0: case aWindow.STATE_FULLSCREEN: michael@0: case aWindow.STATE_MAXIMIZED: michael@0: return "maximized"; michael@0: case aWindow.STATE_MINIMIZED: michael@0: return "minimized"; michael@0: default: michael@0: return "normal"; michael@0: } michael@0: } michael@0: michael@0: var dimension; michael@0: switch (aAttribute) { michael@0: case "width": michael@0: dimension = aWindow.outerWidth; michael@0: break; michael@0: case "height": michael@0: dimension = aWindow.outerHeight; michael@0: break; michael@0: default: michael@0: dimension = aAttribute in aWindow ? aWindow[aAttribute] : ""; michael@0: break; michael@0: } michael@0: michael@0: if (aWindow.windowState == aWindow.STATE_NORMAL) { michael@0: return dimension; michael@0: } michael@0: return aWindow.document.documentElement.getAttribute(aAttribute) || dimension; michael@0: }, michael@0: michael@0: /** michael@0: * Get nsIURI from string michael@0: * @param string michael@0: * @returns nsIURI michael@0: */ michael@0: _getURIFromString: function ssi_getURIFromString(aString) { michael@0: return Services.io.newURI(aString, null, null); michael@0: }, michael@0: michael@0: /** michael@0: * @param aState is a session state michael@0: * @param aRecentCrashes is the number of consecutive crashes michael@0: * @returns whether a restore page will be needed for the session state michael@0: */ michael@0: _needsRestorePage: function ssi_needsRestorePage(aState, aRecentCrashes) { michael@0: const SIX_HOURS_IN_MS = 6 * 60 * 60 * 1000; michael@0: michael@0: // don't display the page when there's nothing to restore michael@0: let winData = aState.windows || null; michael@0: if (!winData || winData.length == 0) michael@0: return false; michael@0: michael@0: // don't wrap a single about:sessionrestore page michael@0: if (this._hasSingleTabWithURL(winData, "about:sessionrestore") || michael@0: this._hasSingleTabWithURL(winData, "about:welcomeback")) { michael@0: return false; michael@0: } michael@0: michael@0: // don't automatically restore in Safe Mode michael@0: if (Services.appinfo.inSafeMode) michael@0: return true; michael@0: michael@0: let max_resumed_crashes = michael@0: this._prefBranch.getIntPref("sessionstore.max_resumed_crashes"); michael@0: let sessionAge = aState.session && aState.session.lastUpdate && michael@0: (Date.now() - aState.session.lastUpdate); michael@0: michael@0: return max_resumed_crashes != -1 && michael@0: (aRecentCrashes > max_resumed_crashes || michael@0: sessionAge && sessionAge >= SIX_HOURS_IN_MS); michael@0: }, michael@0: michael@0: /** michael@0: * @param aWinData is the set of windows in session state michael@0: * @param aURL is the single URL we're looking for michael@0: * @returns whether the window data contains only the single URL passed michael@0: */ michael@0: _hasSingleTabWithURL: function(aWinData, aURL) { michael@0: if (aWinData && michael@0: aWinData.length == 1 && michael@0: aWinData[0].tabs && michael@0: aWinData[0].tabs.length == 1 && michael@0: aWinData[0].tabs[0].entries && michael@0: aWinData[0].tabs[0].entries.length == 1) { michael@0: return aURL == aWinData[0].tabs[0].entries[0].url; michael@0: } michael@0: return false; michael@0: }, michael@0: michael@0: /** michael@0: * Determine if the tab state we're passed is something we should save. This michael@0: * is used when closing a tab or closing a window with a single tab michael@0: * michael@0: * @param aTabState michael@0: * The current tab state michael@0: * @returns boolean michael@0: */ michael@0: _shouldSaveTabState: function ssi_shouldSaveTabState(aTabState) { michael@0: // If the tab has only a transient about: history entry, no other michael@0: // session history, and no userTypedValue, then we don't actually want to michael@0: // store this tab's data. michael@0: return aTabState.entries.length && michael@0: !(aTabState.entries.length == 1 && michael@0: (aTabState.entries[0].url == "about:blank" || michael@0: aTabState.entries[0].url == "about:newtab") && michael@0: !aTabState.userTypedValue); michael@0: }, michael@0: michael@0: /** michael@0: * This is going to take a state as provided at startup (via michael@0: * nsISessionStartup.state) and split it into 2 parts. The first part michael@0: * (defaultState) will be a state that should still be restored at startup, michael@0: * while the second part (state) is a state that should be saved for later. michael@0: * defaultState will be comprised of windows with only pinned tabs, extracted michael@0: * from state. It will contain the cookies that go along with the history michael@0: * entries in those tabs. It will also contain window position information. michael@0: * michael@0: * defaultState will be restored at startup. state will be passed into michael@0: * LastSession and will be kept in case the user explicitly wants michael@0: * to restore the previous session (publicly exposed as restoreLastSession). michael@0: * michael@0: * @param state michael@0: * The state, presumably from nsISessionStartup.state michael@0: * @returns [defaultState, state] michael@0: */ michael@0: _prepDataForDeferredRestore: function ssi_prepDataForDeferredRestore(state) { michael@0: // Make sure that we don't modify the global state as provided by michael@0: // nsSessionStartup.state. michael@0: state = Cu.cloneInto(state, {}); michael@0: michael@0: let defaultState = { windows: [], selectedWindow: 1 }; michael@0: michael@0: state.selectedWindow = state.selectedWindow || 1; michael@0: michael@0: // Look at each window, remove pinned tabs, adjust selectedindex, michael@0: // remove window if necessary. michael@0: for (let wIndex = 0; wIndex < state.windows.length;) { michael@0: let window = state.windows[wIndex]; michael@0: window.selected = window.selected || 1; michael@0: // We're going to put the state of the window into this object michael@0: let pinnedWindowState = { tabs: [], cookies: []}; michael@0: for (let tIndex = 0; tIndex < window.tabs.length;) { michael@0: if (window.tabs[tIndex].pinned) { michael@0: // Adjust window.selected michael@0: if (tIndex + 1 < window.selected) michael@0: window.selected -= 1; michael@0: else if (tIndex + 1 == window.selected) michael@0: pinnedWindowState.selected = pinnedWindowState.tabs.length + 2; michael@0: // + 2 because the tab isn't actually in the array yet michael@0: michael@0: // Now add the pinned tab to our window michael@0: pinnedWindowState.tabs = michael@0: pinnedWindowState.tabs.concat(window.tabs.splice(tIndex, 1)); michael@0: // We don't want to increment tIndex here. michael@0: continue; michael@0: } michael@0: tIndex++; michael@0: } michael@0: michael@0: // At this point the window in the state object has been modified (or not) michael@0: // We want to build the rest of this new window object if we have pinnedTabs. michael@0: if (pinnedWindowState.tabs.length) { michael@0: // First get the other attributes off the window michael@0: WINDOW_ATTRIBUTES.forEach(function(attr) { michael@0: if (attr in window) { michael@0: pinnedWindowState[attr] = window[attr]; michael@0: delete window[attr]; michael@0: } michael@0: }); michael@0: // We're just copying position data into the pinned window. michael@0: // Not copying over: michael@0: // - _closedTabs michael@0: // - extData michael@0: // - isPopup michael@0: // - hidden michael@0: michael@0: // Assign a unique ID to correlate the window to be opened with the michael@0: // remaining data michael@0: window.__lastSessionWindowID = pinnedWindowState.__lastSessionWindowID michael@0: = "" + Date.now() + Math.random(); michael@0: michael@0: // Extract the cookies that belong with each pinned tab michael@0: this._splitCookiesFromWindow(window, pinnedWindowState); michael@0: michael@0: // Actually add this window to our defaultState michael@0: defaultState.windows.push(pinnedWindowState); michael@0: // Remove the window from the state if it doesn't have any tabs michael@0: if (!window.tabs.length) { michael@0: if (wIndex + 1 <= state.selectedWindow) michael@0: state.selectedWindow -= 1; michael@0: else if (wIndex + 1 == state.selectedWindow) michael@0: defaultState.selectedIndex = defaultState.windows.length + 1; michael@0: michael@0: state.windows.splice(wIndex, 1); michael@0: // We don't want to increment wIndex here. michael@0: continue; michael@0: } michael@0: michael@0: michael@0: } michael@0: wIndex++; michael@0: } michael@0: michael@0: return [defaultState, state]; michael@0: }, michael@0: michael@0: /** michael@0: * Splits out the cookies from aWinState into aTargetWinState based on the michael@0: * tabs that are in aTargetWinState. michael@0: * This alters the state of aWinState and aTargetWinState. michael@0: */ michael@0: _splitCookiesFromWindow: michael@0: function ssi_splitCookiesFromWindow(aWinState, aTargetWinState) { michael@0: if (!aWinState.cookies || !aWinState.cookies.length) michael@0: return; michael@0: michael@0: // Get the hosts for history entries in aTargetWinState michael@0: let cookieHosts = SessionCookies.getHostsForWindow(aTargetWinState); michael@0: michael@0: // By creating a regex we reduce overhead and there is only one loop pass michael@0: // through either array (cookieHosts and aWinState.cookies). michael@0: let hosts = Object.keys(cookieHosts).join("|").replace("\\.", "\\.", "g"); michael@0: // If we don't actually have any hosts, then we don't want to do anything. michael@0: if (!hosts.length) michael@0: return; michael@0: let cookieRegex = new RegExp(".*(" + hosts + ")"); michael@0: for (let cIndex = 0; cIndex < aWinState.cookies.length;) { michael@0: if (cookieRegex.test(aWinState.cookies[cIndex].host)) { michael@0: aTargetWinState.cookies = michael@0: aTargetWinState.cookies.concat(aWinState.cookies.splice(cIndex, 1)); michael@0: continue; michael@0: } michael@0: cIndex++; michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Converts a JavaScript object into a JSON string michael@0: * (see http://www.json.org/ for more information). michael@0: * michael@0: * The inverse operation consists of JSON.parse(JSON_string). michael@0: * michael@0: * @param aJSObject is the object to be converted michael@0: * @returns the object's JSON representation michael@0: */ michael@0: _toJSONString: function ssi_toJSONString(aJSObject) { michael@0: return JSON.stringify(aJSObject); michael@0: }, michael@0: michael@0: _sendRestoreCompletedNotifications: function ssi_sendRestoreCompletedNotifications() { michael@0: // not all windows restored, yet michael@0: if (this._restoreCount > 1) { michael@0: this._restoreCount--; michael@0: return; michael@0: } michael@0: michael@0: // observers were already notified michael@0: if (this._restoreCount == -1) michael@0: return; michael@0: michael@0: // This was the last window restored at startup, notify observers. michael@0: Services.obs.notifyObservers(null, michael@0: this._browserSetState ? NOTIFY_BROWSER_STATE_RESTORED : NOTIFY_WINDOWS_RESTORED, michael@0: ""); michael@0: michael@0: this._browserSetState = false; michael@0: this._restoreCount = -1; michael@0: }, michael@0: michael@0: /** michael@0: * Set the given window's busy state michael@0: * @param aWindow the window michael@0: * @param aValue the window's busy state michael@0: */ michael@0: _setWindowStateBusyValue: michael@0: function ssi_changeWindowStateBusyValue(aWindow, aValue) { michael@0: michael@0: this._windows[aWindow.__SSi].busy = aValue; michael@0: michael@0: // Keep the to-be-restored state in sync because that is returned by michael@0: // getWindowState() as long as the window isn't loaded, yet. michael@0: if (!this._isWindowLoaded(aWindow)) { michael@0: let stateToRestore = this._statesToRestore[aWindow.__SS_restoreID].windows[0]; michael@0: stateToRestore.busy = aValue; michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Set the given window's state to 'not busy'. michael@0: * @param aWindow the window michael@0: */ michael@0: _setWindowStateReady: function ssi_setWindowStateReady(aWindow) { michael@0: this._setWindowStateBusyValue(aWindow, false); michael@0: this._sendWindowStateEvent(aWindow, "Ready"); michael@0: }, michael@0: michael@0: /** michael@0: * Set the given window's state to 'busy'. michael@0: * @param aWindow the window michael@0: */ michael@0: _setWindowStateBusy: function ssi_setWindowStateBusy(aWindow) { michael@0: this._setWindowStateBusyValue(aWindow, true); michael@0: this._sendWindowStateEvent(aWindow, "Busy"); michael@0: }, michael@0: michael@0: /** michael@0: * Dispatch an SSWindowState_____ event for the given window. michael@0: * @param aWindow the window michael@0: * @param aType the type of event, SSWindowState will be prepended to this string michael@0: */ michael@0: _sendWindowStateEvent: function ssi_sendWindowStateEvent(aWindow, aType) { michael@0: let event = aWindow.document.createEvent("Events"); michael@0: event.initEvent("SSWindowState" + aType, true, false); michael@0: aWindow.dispatchEvent(event); michael@0: }, michael@0: michael@0: /** michael@0: * Dispatch the SSTabRestored event for the given tab. michael@0: * @param aTab the which has been restored michael@0: */ michael@0: _sendTabRestoredNotification: function ssi_sendTabRestoredNotification(aTab) { michael@0: let event = aTab.ownerDocument.createEvent("Events"); michael@0: event.initEvent("SSTabRestored", true, false); michael@0: aTab.dispatchEvent(event); michael@0: }, michael@0: michael@0: /** michael@0: * @param aWindow michael@0: * Window reference michael@0: * @returns whether this window's data is still cached in _statesToRestore michael@0: * because it's not fully loaded yet michael@0: */ michael@0: _isWindowLoaded: function ssi_isWindowLoaded(aWindow) { michael@0: return !aWindow.__SS_restoreID; michael@0: }, michael@0: michael@0: /** michael@0: * Replace "Loading..." with the tab label (with minimal side-effects) michael@0: * @param aString is the string the title is stored in michael@0: * @param aTabbrowser is a tabbrowser object, containing aTab michael@0: * @param aTab is the tab whose title we're updating & using michael@0: * michael@0: * @returns aString that has been updated with the new title michael@0: */ michael@0: _replaceLoadingTitle : function ssi_replaceLoadingTitle(aString, aTabbrowser, aTab) { michael@0: if (aString == aTabbrowser.mStringBundle.getString("tabs.connecting")) { michael@0: aTabbrowser.setTabTitle(aTab); michael@0: [aString, aTab.label] = [aTab.label, aString]; michael@0: } michael@0: return aString; michael@0: }, michael@0: michael@0: /** michael@0: * Resize this._closedWindows to the value of the pref, except in the case michael@0: * where we don't have any non-popup windows on Windows and Linux. Then we must michael@0: * resize such that we have at least one non-popup window. michael@0: */ michael@0: _capClosedWindows : function ssi_capClosedWindows() { michael@0: if (this._closedWindows.length <= this._max_windows_undo) michael@0: return; michael@0: let spliceTo = this._max_windows_undo; michael@0: #ifndef XP_MACOSX michael@0: let normalWindowIndex = 0; michael@0: // try to find a non-popup window in this._closedWindows michael@0: while (normalWindowIndex < this._closedWindows.length && michael@0: !!this._closedWindows[normalWindowIndex].isPopup) michael@0: normalWindowIndex++; michael@0: if (normalWindowIndex >= this._max_windows_undo) michael@0: spliceTo = normalWindowIndex + 1; michael@0: #endif michael@0: this._closedWindows.splice(spliceTo, this._closedWindows.length); michael@0: }, michael@0: michael@0: /** michael@0: * Clears the set of windows that are "resurrected" before writing to disk to michael@0: * make closing windows one after the other until shutdown work as expected. michael@0: * michael@0: * This function should only be called when we are sure that there has been michael@0: * a user action that indicates the browser is actively being used and all michael@0: * windows that have been closed before are not part of a series of closing michael@0: * windows. michael@0: */ michael@0: _clearRestoringWindows: function ssi_clearRestoringWindows() { michael@0: for (let i = 0; i < this._closedWindows.length; i++) { michael@0: delete this._closedWindows[i]._shouldRestore; michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Reset state to prepare for a new session state to be restored. michael@0: */ michael@0: _resetRestoringState: function ssi_initRestoringState() { michael@0: TabRestoreQueue.reset(); michael@0: this._tabsRestoringCount = 0; michael@0: }, michael@0: michael@0: /** michael@0: * Reset the restoring state for a particular tab. This will be called when michael@0: * removing a tab or when a tab needs to be reset (it's being overwritten). michael@0: * michael@0: * @param aTab michael@0: * The tab that will be "reset" michael@0: */ michael@0: _resetLocalTabRestoringState: function (aTab) { michael@0: let window = aTab.ownerDocument.defaultView; michael@0: let browser = aTab.linkedBrowser; michael@0: michael@0: // Keep the tab's previous state for later in this method michael@0: let previousState = browser.__SS_restoreState; michael@0: michael@0: // The browser is no longer in any sort of restoring state. michael@0: delete browser.__SS_restoreState; michael@0: this._browserEpochs.delete(browser.permanentKey); michael@0: michael@0: aTab.removeAttribute("pending"); michael@0: browser.removeAttribute("pending"); michael@0: michael@0: if (previousState == TAB_STATE_RESTORING) { michael@0: if (this._tabsRestoringCount) michael@0: this._tabsRestoringCount--; michael@0: } else if (previousState == TAB_STATE_NEEDS_RESTORE) { michael@0: // Make sure that the tab is removed from the list of tabs to restore. michael@0: // Again, this is normally done in restoreTabContent, but that isn't being called michael@0: // for this tab. michael@0: TabRestoreQueue.remove(aTab); michael@0: } michael@0: }, michael@0: michael@0: _resetTabRestoringState: function (tab) { michael@0: let browser = tab.linkedBrowser; michael@0: if (browser.__SS_restoreState) { michael@0: browser.messageManager.sendAsyncMessage("SessionStore:resetRestore", {}); michael@0: } michael@0: this._resetLocalTabRestoringState(tab); michael@0: }, michael@0: michael@0: /** michael@0: * Each time a element is restored, we increment its "epoch". To michael@0: * check if a message from content-sessionStore.js is out of date, we can michael@0: * compare the epoch received with the message to the element's michael@0: * epoch. This function does that, and returns true if |epoch| is up-to-date michael@0: * with respect to |browser|. michael@0: */ michael@0: isCurrentEpoch: function (browser, epoch) { michael@0: return this._browserEpochs.get(browser.permanentKey, 0) == epoch; michael@0: }, michael@0: michael@0: }; michael@0: michael@0: /** michael@0: * Priority queue that keeps track of a list of tabs to restore and returns michael@0: * the tab we should restore next, based on priority rules. We decide between michael@0: * pinned, visible and hidden tabs in that and FIFO order. Hidden tabs are only michael@0: * restored with restore_hidden_tabs=true. michael@0: */ michael@0: let TabRestoreQueue = { michael@0: // The separate buckets used to store tabs. michael@0: tabs: {priority: [], visible: [], hidden: []}, michael@0: michael@0: // Preferences used by the TabRestoreQueue to determine which tabs michael@0: // are restored automatically and which tabs will be on-demand. michael@0: prefs: { michael@0: // Lazy getter that returns whether tabs are restored on demand. michael@0: get restoreOnDemand() { michael@0: let updateValue = () => { michael@0: let value = Services.prefs.getBoolPref(PREF); michael@0: let definition = {value: value, configurable: true}; michael@0: Object.defineProperty(this, "restoreOnDemand", definition); michael@0: return value; michael@0: } michael@0: michael@0: const PREF = "browser.sessionstore.restore_on_demand"; michael@0: Services.prefs.addObserver(PREF, updateValue, false); michael@0: return updateValue(); michael@0: }, michael@0: michael@0: // Lazy getter that returns whether pinned tabs are restored on demand. michael@0: get restorePinnedTabsOnDemand() { michael@0: let updateValue = () => { michael@0: let value = Services.prefs.getBoolPref(PREF); michael@0: let definition = {value: value, configurable: true}; michael@0: Object.defineProperty(this, "restorePinnedTabsOnDemand", definition); michael@0: return value; michael@0: } michael@0: michael@0: const PREF = "browser.sessionstore.restore_pinned_tabs_on_demand"; michael@0: Services.prefs.addObserver(PREF, updateValue, false); michael@0: return updateValue(); michael@0: }, michael@0: michael@0: // Lazy getter that returns whether we should restore hidden tabs. michael@0: get restoreHiddenTabs() { michael@0: let updateValue = () => { michael@0: let value = Services.prefs.getBoolPref(PREF); michael@0: let definition = {value: value, configurable: true}; michael@0: Object.defineProperty(this, "restoreHiddenTabs", definition); michael@0: return value; michael@0: } michael@0: michael@0: const PREF = "browser.sessionstore.restore_hidden_tabs"; michael@0: Services.prefs.addObserver(PREF, updateValue, false); michael@0: return updateValue(); michael@0: } michael@0: }, michael@0: michael@0: // Resets the queue and removes all tabs. michael@0: reset: function () { michael@0: this.tabs = {priority: [], visible: [], hidden: []}; michael@0: }, michael@0: michael@0: // Adds a tab to the queue and determines its priority bucket. michael@0: add: function (tab) { michael@0: let {priority, hidden, visible} = this.tabs; michael@0: michael@0: if (tab.pinned) { michael@0: priority.push(tab); michael@0: } else if (tab.hidden) { michael@0: hidden.push(tab); michael@0: } else { michael@0: visible.push(tab); michael@0: } michael@0: }, michael@0: michael@0: // Removes a given tab from the queue, if it's in there. michael@0: remove: function (tab) { michael@0: let {priority, hidden, visible} = this.tabs; michael@0: michael@0: // We'll always check priority first since we don't michael@0: // have an indicator if a tab will be there or not. michael@0: let set = priority; michael@0: let index = set.indexOf(tab); michael@0: michael@0: if (index == -1) { michael@0: set = tab.hidden ? hidden : visible; michael@0: index = set.indexOf(tab); michael@0: } michael@0: michael@0: if (index > -1) { michael@0: set.splice(index, 1); michael@0: } michael@0: }, michael@0: michael@0: // Returns and removes the tab with the highest priority. michael@0: shift: function () { michael@0: let set; michael@0: let {priority, hidden, visible} = this.tabs; michael@0: michael@0: let {restoreOnDemand, restorePinnedTabsOnDemand} = this.prefs; michael@0: let restorePinned = !(restoreOnDemand && restorePinnedTabsOnDemand); michael@0: if (restorePinned && priority.length) { michael@0: set = priority; michael@0: } else if (!restoreOnDemand) { michael@0: if (visible.length) { michael@0: set = visible; michael@0: } else if (this.prefs.restoreHiddenTabs && hidden.length) { michael@0: set = hidden; michael@0: } michael@0: } michael@0: michael@0: return set && set.shift(); michael@0: }, michael@0: michael@0: // Moves a given tab from the 'hidden' to the 'visible' bucket. michael@0: hiddenToVisible: function (tab) { michael@0: let {hidden, visible} = this.tabs; michael@0: let index = hidden.indexOf(tab); michael@0: michael@0: if (index > -1) { michael@0: hidden.splice(index, 1); michael@0: visible.push(tab); michael@0: } else { michael@0: throw new Error("restore queue: hidden tab not found"); michael@0: } michael@0: }, michael@0: michael@0: // Moves a given tab from the 'visible' to the 'hidden' bucket. michael@0: visibleToHidden: function (tab) { michael@0: let {visible, hidden} = this.tabs; michael@0: let index = visible.indexOf(tab); michael@0: michael@0: if (index > -1) { michael@0: visible.splice(index, 1); michael@0: hidden.push(tab); michael@0: } else { michael@0: throw new Error("restore queue: visible tab not found"); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: // A map storing a closed window's state data until it goes aways (is GC'ed). michael@0: // This ensures that API clients can still read (but not write) states of michael@0: // windows they still hold a reference to but we don't. michael@0: let DyingWindowCache = { michael@0: _data: new WeakMap(), michael@0: michael@0: has: function (window) { michael@0: return this._data.has(window); michael@0: }, michael@0: michael@0: get: function (window) { michael@0: return this._data.get(window); michael@0: }, michael@0: michael@0: set: function (window, data) { michael@0: this._data.set(window, data); michael@0: }, michael@0: michael@0: remove: function (window) { michael@0: this._data.delete(window); michael@0: } michael@0: }; michael@0: michael@0: // A weak set of dirty windows. We use it to determine which windows we need to michael@0: // recollect data for when getCurrentState() is called. michael@0: let DirtyWindows = { michael@0: _data: new WeakMap(), michael@0: michael@0: has: function (window) { michael@0: return this._data.has(window); michael@0: }, michael@0: michael@0: add: function (window) { michael@0: return this._data.set(window, true); michael@0: }, michael@0: michael@0: remove: function (window) { michael@0: this._data.delete(window); michael@0: }, michael@0: michael@0: clear: function (window) { michael@0: this._data.clear(); michael@0: } michael@0: }; michael@0: michael@0: // The state from the previous session (after restoring pinned tabs). This michael@0: // state is persisted and passed through to the next session during an app michael@0: // restart to make the third party add-on warning not trash the deferred michael@0: // session michael@0: let LastSession = { michael@0: _state: null, michael@0: michael@0: get canRestore() { michael@0: return !!this._state; michael@0: }, michael@0: michael@0: getState: function () { michael@0: return this._state; michael@0: }, michael@0: michael@0: setState: function (state) { michael@0: this._state = state; michael@0: }, michael@0: michael@0: clear: function () { michael@0: if (this._state) { michael@0: this._state = null; michael@0: Services.obs.notifyObservers(null, NOTIFY_LAST_SESSION_CLEARED, null); michael@0: } michael@0: } michael@0: };