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 michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: const Cr = Components.results; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/WindowsPrefSync.jsm"); michael@0: michael@0: #ifdef MOZ_CRASHREPORTER michael@0: XPCOMUtils.defineLazyServiceGetter(this, "CrashReporter", michael@0: "@mozilla.org/xre/app-info;1", "nsICrashReporter"); michael@0: #endif michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "CrashMonitor", michael@0: "resource://gre/modules/CrashMonitor.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyServiceGetter(this, "gUUIDGenerator", michael@0: "@mozilla.org/uuid-generator;1", "nsIUUIDGenerator"); michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "NetUtil", function() { michael@0: Cu.import("resource://gre/modules/NetUtil.jsm"); michael@0: return NetUtil; michael@0: }); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "UITelemetry", michael@0: "resource://gre/modules/UITelemetry.jsm"); michael@0: michael@0: // ----------------------------------------------------------------------- michael@0: // Session Store michael@0: // ----------------------------------------------------------------------- michael@0: michael@0: const STATE_STOPPED = 0; michael@0: const STATE_RUNNING = 1; michael@0: const STATE_QUITTING = -1; michael@0: michael@0: function SessionStore() { } michael@0: michael@0: SessionStore.prototype = { michael@0: classID: Components.ID("{8c1f07d6-cba3-4226-a315-8bd43d67d032}"), michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsISessionStore, michael@0: Ci.nsIDOMEventListener, michael@0: Ci.nsIObserver, michael@0: Ci.nsISupportsWeakReference]), michael@0: michael@0: _windows: {}, michael@0: _tabsFromOtherGroups: [], michael@0: _selectedWindow: 1, michael@0: _orderedWindows: [], michael@0: _lastSaveTime: 0, michael@0: _lastSessionTime: 0, michael@0: _interval: 10000, michael@0: _maxTabsUndo: 1, michael@0: _shouldRestore: false, michael@0: michael@0: // Tab telemetry variables michael@0: _maxTabsOpen: 1, michael@0: michael@0: init: function ss_init() { michael@0: // Get file references michael@0: this._sessionFile = Services.dirsvc.get("ProfD", Ci.nsILocalFile); michael@0: this._sessionFileBackup = this._sessionFile.clone(); michael@0: this._sessionCache = this._sessionFile.clone(); michael@0: this._sessionFile.append("sessionstore.js"); michael@0: this._sessionFileBackup.append("sessionstore.bak"); michael@0: this._sessionCache.append("sessionstoreCache"); michael@0: michael@0: this._loadState = STATE_STOPPED; michael@0: michael@0: try { michael@0: UITelemetry.addSimpleMeasureFunction("metro-tabs", michael@0: this._getTabStats.bind(this)); michael@0: } catch (ex) { michael@0: // swallow exception that occurs if metro-tabs measure is already set up michael@0: } michael@0: michael@0: CrashMonitor.previousCheckpoints.then(checkpoints => { michael@0: let previousSessionCrashed = false; michael@0: michael@0: if (checkpoints) { michael@0: // If the previous session finished writing the final state, we'll michael@0: // assume there was no crash. michael@0: previousSessionCrashed = !checkpoints["sessionstore-final-state-write-complete"]; michael@0: } else { michael@0: // If no checkpoints are saved, this is the first run with CrashMonitor or the michael@0: // metroSessionCheckpoints file was corrupted/deleted, so fallback to defining michael@0: // a crash as init-ing with an unexpected previousExecutionState michael@0: // 1 == RUNNING, 2 == SUSPENDED michael@0: previousSessionCrashed = Services.metro.previousExecutionState == 1 || michael@0: Services.metro.previousExecutionState == 2; michael@0: } michael@0: michael@0: Services.telemetry.getHistogramById("SHUTDOWN_OK").add(!previousSessionCrashed); michael@0: }); michael@0: michael@0: try { michael@0: let shutdownWasUnclean = false; michael@0: michael@0: if (this._sessionFileBackup.exists()) { michael@0: this._sessionFileBackup.remove(false); michael@0: shutdownWasUnclean = true; michael@0: } michael@0: michael@0: if (this._sessionFile.exists()) { michael@0: this._sessionFile.copyTo(null, this._sessionFileBackup.leafName); michael@0: michael@0: switch(Services.metro.previousExecutionState) { michael@0: // 0 == NotRunning michael@0: case 0: michael@0: // Disable crash recovery if we have exceeded the timeout michael@0: this._lastSessionTime = this._sessionFile.lastModifiedTime; michael@0: let delta = Date.now() - this._lastSessionTime; michael@0: let timeout = michael@0: Services.prefs.getIntPref( michael@0: "browser.sessionstore.resume_from_crash_timeout"); michael@0: this._shouldRestore = shutdownWasUnclean michael@0: && (delta < (timeout * 60000)); michael@0: break; michael@0: // 1 == Running michael@0: case 1: michael@0: // We should never encounter this situation michael@0: Components.utils.reportError("SessionRestore.init called with " michael@0: + "previous execution state 'Running'"); michael@0: this._shouldRestore = true; michael@0: break; michael@0: // 2 == Suspended michael@0: case 2: michael@0: // We should never encounter this situation michael@0: Components.utils.reportError("SessionRestore.init called with " michael@0: + "previous execution state 'Suspended'"); michael@0: this._shouldRestore = true; michael@0: break; michael@0: // 3 == Terminated michael@0: case 3: michael@0: // Terminated means that Windows terminated our already-suspended michael@0: // process to get back some resources. When we re-launch, we want michael@0: // to provide the illusion that our process was suspended the michael@0: // whole time, and never terminated. michael@0: this._shouldRestore = true; michael@0: break; michael@0: // 4 == ClosedByUser michael@0: case 4: michael@0: // ClosedByUser indicates that the user performed a "close" gesture michael@0: // on our tile. We should act as if the browser closed normally, michael@0: // even if we were closed from a suspended state (in which case michael@0: // we'll have determined that it was an unclean shtudown) michael@0: this._shouldRestore = false; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (!this._sessionCache.exists() || !this._sessionCache.isDirectory()) { michael@0: this._sessionCache.create(Ci.nsIFile.DIRECTORY_TYPE, 0700); michael@0: } michael@0: } catch (ex) { michael@0: Cu.reportError(ex); // file was write-locked? michael@0: } michael@0: michael@0: this._interval = Services.prefs.getIntPref("browser.sessionstore.interval"); michael@0: this._maxTabsUndo = Services.prefs.getIntPref("browser.sessionstore.max_tabs_undo"); michael@0: michael@0: // Disable crash recovery if it has been turned off michael@0: if (!Services.prefs.getBoolPref("browser.sessionstore.resume_from_crash")) michael@0: this._shouldRestore = false; michael@0: michael@0: // Do we need to restore session just this once, in case of a restart? michael@0: if (Services.prefs.getBoolPref("browser.sessionstore.resume_session_once")) { michael@0: Services.prefs.setBoolPref("browser.sessionstore.resume_session_once", false); michael@0: this._shouldRestore = true; michael@0: } michael@0: }, michael@0: michael@0: _clearDisk: function ss_clearDisk() { michael@0: if (this._sessionFile.exists()) { michael@0: try { michael@0: this._sessionFile.remove(false); michael@0: } catch (ex) { dump(ex + '\n'); } // couldn't remove the file - what now? michael@0: } michael@0: if (this._sessionFileBackup.exists()) { michael@0: try { michael@0: this._sessionFileBackup.remove(false); michael@0: } catch (ex) { dump(ex + '\n'); } // couldn't remove the file - what now? michael@0: } michael@0: michael@0: this._clearCache(); michael@0: }, michael@0: michael@0: _clearCache: function ss_clearCache() { michael@0: // First, let's get a list of files we think should be active michael@0: let activeFiles = []; michael@0: this._forEachBrowserWindow(function(aWindow) { michael@0: let tabs = aWindow.Browser.tabs; michael@0: for (let i = 0; i < tabs.length; i++) { michael@0: let browser = tabs[i].browser; michael@0: if (browser.__SS_extdata && "thumbnail" in browser.__SS_extdata) michael@0: activeFiles.push(browser.__SS_extdata.thumbnail); michael@0: } michael@0: }); michael@0: michael@0: // Now, let's find the stale files in the cache folder michael@0: let staleFiles = []; michael@0: let cacheFiles = this._sessionCache.directoryEntries; michael@0: while (cacheFiles.hasMoreElements()) { michael@0: let file = cacheFiles.getNext().QueryInterface(Ci.nsILocalFile); michael@0: let fileURI = Services.io.newFileURI(file); michael@0: if (activeFiles.indexOf(fileURI) == -1) michael@0: staleFiles.push(file); michael@0: } michael@0: michael@0: // Remove the stale files in a separate step to keep the enumerator from michael@0: // messing up if we remove the files as we collect them. michael@0: staleFiles.forEach(function(aFile) { michael@0: aFile.remove(false); michael@0: }) michael@0: }, michael@0: michael@0: _getTabStats: function() { michael@0: return { michael@0: currTabCount: this._currTabCount, michael@0: maxTabCount: this._maxTabsOpen michael@0: }; michael@0: }, michael@0: michael@0: observe: function ss_observe(aSubject, aTopic, aData) { michael@0: let self = this; michael@0: let observerService = Services.obs; michael@0: switch (aTopic) { michael@0: case "app-startup": michael@0: observerService.addObserver(this, "final-ui-startup", true); michael@0: observerService.addObserver(this, "domwindowopened", true); michael@0: observerService.addObserver(this, "domwindowclosed", true); michael@0: observerService.addObserver(this, "browser-lastwindow-close-granted", true); michael@0: observerService.addObserver(this, "browser:purge-session-history", true); michael@0: observerService.addObserver(this, "quit-application-requested", true); michael@0: observerService.addObserver(this, "quit-application-granted", true); michael@0: observerService.addObserver(this, "quit-application", true); michael@0: observerService.addObserver(this, "reset-telemetry-vars", true); michael@0: break; michael@0: case "final-ui-startup": michael@0: observerService.removeObserver(this, "final-ui-startup"); michael@0: if (WindowsPrefSync) { michael@0: // Pulls in Desktop controlled prefs and pushes out Metro controlled prefs michael@0: WindowsPrefSync.init(); michael@0: } michael@0: this.init(); michael@0: break; michael@0: case "domwindowopened": michael@0: let window = aSubject; michael@0: window.addEventListener("load", function() { michael@0: self.onWindowOpen(window); michael@0: window.removeEventListener("load", arguments.callee, false); michael@0: }, false); michael@0: break; michael@0: case "domwindowclosed": // catch closed windows michael@0: this.onWindowClose(aSubject); michael@0: break; michael@0: case "browser-lastwindow-close-granted": michael@0: // If a save has been queued, kill the timer and save state now michael@0: if (this._saveTimer) { michael@0: this._saveTimer.cancel(); michael@0: this._saveTimer = null; michael@0: this.saveState(); michael@0: } michael@0: michael@0: // Freeze the data at what we've got (ignoring closing windows) michael@0: this._loadState = STATE_QUITTING; michael@0: break; michael@0: case "quit-application-requested": michael@0: // Get a current snapshot of all windows michael@0: this._forEachBrowserWindow(function(aWindow) { michael@0: self._collectWindowData(aWindow); michael@0: }); michael@0: break; michael@0: case "quit-application-granted": michael@0: // Get a current snapshot of all windows michael@0: this._forEachBrowserWindow(function(aWindow) { michael@0: self._collectWindowData(aWindow); michael@0: }); michael@0: michael@0: // Freeze the data at what we've got (ignoring closing windows) michael@0: this._loadState = STATE_QUITTING; michael@0: break; michael@0: case "quit-application": michael@0: // If we are restarting, lets restore the tabs michael@0: if (aData == "restart") { michael@0: Services.prefs.setBoolPref("browser.sessionstore.resume_session_once", true); michael@0: michael@0: // Ignore purges when restarting. The notification is fired after "quit-application". michael@0: Services.obs.removeObserver(this, "browser:purge-session-history"); michael@0: } michael@0: michael@0: // Freeze the data at what we've got (ignoring closing windows) michael@0: this._loadState = STATE_QUITTING; michael@0: michael@0: // No need for this back up, we are shutting down just fine michael@0: if (this._sessionFileBackup.exists()) michael@0: this._sessionFileBackup.remove(false); michael@0: michael@0: observerService.removeObserver(this, "domwindowopened"); michael@0: observerService.removeObserver(this, "domwindowclosed"); michael@0: observerService.removeObserver(this, "browser-lastwindow-close-granted"); michael@0: observerService.removeObserver(this, "quit-application-requested"); michael@0: observerService.removeObserver(this, "quit-application-granted"); michael@0: observerService.removeObserver(this, "quit-application"); michael@0: observerService.removeObserver(this, "reset-telemetry-vars"); michael@0: michael@0: // If a save has been queued, kill the timer and save state now michael@0: if (this._saveTimer) { michael@0: this._saveTimer.cancel(); michael@0: this._saveTimer = null; michael@0: } michael@0: this.saveState(); michael@0: break; michael@0: case "browser:purge-session-history": // catch sanitization michael@0: this._clearDisk(); michael@0: 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: michael@0: // Clear all data about closed tabs michael@0: for (let [ssid, win] in Iterator(this._windows)) michael@0: win._closedTabs = []; michael@0: michael@0: if (this._loadState == STATE_RUNNING) { michael@0: // Save the purged state immediately michael@0: this.saveStateNow(); michael@0: } michael@0: break; michael@0: case "timer-callback": michael@0: // Timer call back for delayed saving michael@0: this._saveTimer = null; michael@0: this.saveState(); michael@0: break; michael@0: case "reset-telemetry-vars": michael@0: // Used in mochitests only. michael@0: this._maxTabsOpen = 1; michael@0: } michael@0: }, michael@0: michael@0: updateTabTelemetryVars: function(window) { michael@0: this._currTabCount = window.Browser.tabs.length; michael@0: if (this._currTabCount > this._maxTabsOpen) { michael@0: this._maxTabsOpen = this._currTabCount; michael@0: } michael@0: }, michael@0: michael@0: handleEvent: function ss_handleEvent(aEvent) { michael@0: let window = aEvent.currentTarget.ownerDocument.defaultView; michael@0: switch (aEvent.type) { michael@0: case "load": michael@0: browser = aEvent.currentTarget; michael@0: if (aEvent.target == browser.contentDocument && browser.__SS_tabFormData) { michael@0: browser.messageManager.sendAsyncMessage("SessionStore:restoreSessionTabData", { michael@0: formdata: browser.__SS_tabFormData.formdata, michael@0: scroll: browser.__SS_tabFormData.scroll michael@0: }); michael@0: } michael@0: break; michael@0: case "TabOpen": michael@0: this.updateTabTelemetryVars(window); michael@0: let browser = aEvent.originalTarget.linkedBrowser; michael@0: browser.addEventListener("load", this, true); michael@0: case "TabClose": { michael@0: let browser = aEvent.originalTarget.linkedBrowser; michael@0: if (aEvent.type == "TabOpen") { michael@0: this.onTabAdd(window, browser); michael@0: } michael@0: else { michael@0: this.onTabClose(window, browser); michael@0: this.onTabRemove(window, browser); michael@0: } michael@0: break; michael@0: } michael@0: case "TabRemove": michael@0: this.updateTabTelemetryVars(window); michael@0: break; michael@0: case "TabSelect": { michael@0: let browser = aEvent.originalTarget.linkedBrowser; michael@0: this.onTabSelect(window, browser); michael@0: break; michael@0: } michael@0: } michael@0: }, michael@0: michael@0: receiveMessage: function ss_receiveMessage(aMessage) { michael@0: let browser = aMessage.target; michael@0: switch (aMessage.name) { michael@0: case "SessionStore:collectFormdata": michael@0: browser.__SS_data.formdata = aMessage.json.data; michael@0: break; michael@0: case "SessionStore:collectScrollPosition": michael@0: browser.__SS_data.scroll = aMessage.json.data; michael@0: break; michael@0: default: michael@0: let window = aMessage.target.ownerDocument.defaultView; michael@0: this.onTabLoad(window, aMessage.target, aMessage); michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: onWindowOpen: function ss_onWindowOpen(aWindow) { michael@0: // Return if window has already been initialized michael@0: if (aWindow && aWindow.__SSID && this._windows[aWindow.__SSID]) michael@0: return; michael@0: michael@0: // Ignore non-browser windows and windows opened while shutting down michael@0: if (aWindow.document.documentElement.getAttribute("windowtype") != "navigator:browser" || this._loadState == STATE_QUITTING) michael@0: return; michael@0: michael@0: // Assign it a unique identifier and create its data object michael@0: aWindow.__SSID = "window" + gUUIDGenerator.generateUUID().toString(); michael@0: this._windows[aWindow.__SSID] = { tabs: [], selected: 0, _closedTabs: [] }; michael@0: this._orderedWindows.push(aWindow.__SSID); 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: this._lastSaveTime = Date.now(); michael@0: michael@0: // Nothing to restore, notify observers things are complete michael@0: if (!this.shouldRestore()) { michael@0: this._clearCache(); michael@0: Services.obs.notifyObservers(null, "sessionstore-windows-restored", ""); michael@0: } michael@0: } michael@0: michael@0: // Add tab change listeners to all already existing tabs michael@0: let tabs = aWindow.Browser.tabs; michael@0: for (let i = 0; i < tabs.length; i++) michael@0: this.onTabAdd(aWindow, tabs[i].browser, true); michael@0: michael@0: // Notification of tab add/remove/selection michael@0: let tabContainer = aWindow.document.getElementById("tabs"); michael@0: tabContainer.addEventListener("TabOpen", this, true); michael@0: tabContainer.addEventListener("TabClose", this, true); michael@0: tabContainer.addEventListener("TabRemove", this, true); michael@0: tabContainer.addEventListener("TabSelect", this, true); michael@0: }, michael@0: michael@0: onWindowClose: function ss_onWindowClose(aWindow) { michael@0: // Ignore windows not tracked by SessionStore michael@0: if (!aWindow.__SSID || !this._windows[aWindow.__SSID]) michael@0: return; michael@0: michael@0: let tabContainer = aWindow.document.getElementById("tabs"); michael@0: tabContainer.removeEventListener("TabOpen", this, true); michael@0: tabContainer.removeEventListener("TabClose", this, true); michael@0: tabContainer.removeEventListener("TabRemove", this, true); michael@0: tabContainer.removeEventListener("TabSelect", this, true); michael@0: michael@0: if (this._loadState == STATE_RUNNING) { michael@0: // Update all window data for a last time michael@0: this._collectWindowData(aWindow); michael@0: michael@0: // Clear this window from the list michael@0: delete this._windows[aWindow.__SSID]; michael@0: michael@0: // Save the state without this window to disk michael@0: this.saveStateDelayed(); michael@0: } michael@0: michael@0: let tabs = aWindow.Browser.tabs; michael@0: for (let i = 0; i < tabs.length; i++) michael@0: this.onTabRemove(aWindow, tabs[i].browser, true); michael@0: michael@0: delete aWindow.__SSID; michael@0: }, michael@0: michael@0: onTabAdd: function ss_onTabAdd(aWindow, aBrowser, aNoNotification) { michael@0: aBrowser.messageManager.addMessageListener("pageshow", this); michael@0: aBrowser.messageManager.addMessageListener("Content:SessionHistory", this); michael@0: aBrowser.messageManager.addMessageListener("SessionStore:collectFormdata", this); michael@0: aBrowser.messageManager.addMessageListener("SessionStore:collectScrollPosition", this); michael@0: michael@0: if (!aNoNotification) michael@0: this.saveStateDelayed(); michael@0: this._updateCrashReportURL(aWindow); michael@0: }, michael@0: michael@0: onTabRemove: function ss_onTabRemove(aWindow, aBrowser, aNoNotification) { michael@0: aBrowser.messageManager.removeMessageListener("pageshow", this); michael@0: aBrowser.messageManager.removeMessageListener("Content:SessionHistory", this); michael@0: aBrowser.messageManager.removeMessageListener("SessionStore:collectFormdata", this); michael@0: aBrowser.messageManager.removeMessageListener("SessionStore:collectScrollPosition", this); michael@0: michael@0: // If this browser is being restored, skip any session save activity michael@0: if (aBrowser.__SS_restore) michael@0: return; michael@0: michael@0: delete aBrowser.__SS_data; michael@0: michael@0: if (!aNoNotification) michael@0: this.saveStateDelayed(); michael@0: }, michael@0: michael@0: onTabClose: function ss_onTabClose(aWindow, aBrowser) { michael@0: if (this._maxTabsUndo == 0) michael@0: return; michael@0: michael@0: if (aWindow.Browser.tabs.length > 0) { michael@0: // Bundle this browser's data and extra data and save in the closedTabs michael@0: // window property michael@0: // michael@0: // NB: The access to aBrowser.__SS_extdata throws during automation (in michael@0: // browser_msgmgr_01). See bug 888736. michael@0: let data = aBrowser.__SS_data; michael@0: if (!data) { michael@0: return; // Cannot restore an empty tab. michael@0: } michael@0: try { data.extData = aBrowser.__SS_extdata; } catch (e) { } michael@0: michael@0: this._windows[aWindow.__SSID]._closedTabs.unshift({ state: data }); michael@0: let length = this._windows[aWindow.__SSID]._closedTabs.length; michael@0: if (length > this._maxTabsUndo) michael@0: this._windows[aWindow.__SSID]._closedTabs.splice(this._maxTabsUndo, length - this._maxTabsUndo); michael@0: } michael@0: }, michael@0: michael@0: onTabLoad: function ss_onTabLoad(aWindow, aBrowser, aMessage) { michael@0: // If this browser is being restored, skip any session save activity michael@0: if (aBrowser.__SS_restore) michael@0: return; michael@0: michael@0: // Ignore a transient "about:blank" michael@0: if (!aBrowser.canGoBack && aBrowser.currentURI.spec == "about:blank") michael@0: return; michael@0: michael@0: if (aMessage.name == "Content:SessionHistory") { michael@0: delete aBrowser.__SS_data; michael@0: this._collectTabData(aBrowser, aMessage.json); michael@0: } michael@0: michael@0: // Save out the state as quickly as possible michael@0: if (aMessage.name == "pageshow") michael@0: this.saveStateNow(); michael@0: michael@0: this._updateCrashReportURL(aWindow); michael@0: }, michael@0: michael@0: onTabSelect: function ss_onTabSelect(aWindow, aBrowser) { michael@0: if (this._loadState != STATE_RUNNING) michael@0: return; michael@0: michael@0: let index = aWindow.Elements.browsers.selectedIndex; michael@0: this._windows[aWindow.__SSID].selected = parseInt(index) + 1; // 1-based michael@0: michael@0: // Restore the resurrected browser michael@0: if (aBrowser.__SS_restore) { michael@0: let data = aBrowser.__SS_data; michael@0: if (data.entries.length > 0) { michael@0: let json = { michael@0: uri: data.entries[data.index - 1].url, michael@0: flags: null, michael@0: entries: data.entries, michael@0: index: data.index michael@0: }; michael@0: aBrowser.messageManager.sendAsyncMessage("WebNavigation:LoadURI", json); michael@0: } michael@0: michael@0: delete aBrowser.__SS_restore; michael@0: } michael@0: michael@0: this._updateCrashReportURL(aWindow); michael@0: }, michael@0: michael@0: saveStateDelayed: function ss_saveStateDelayed() { michael@0: if (!this._saveTimer) { michael@0: // Interval until the next disk operation is allowed michael@0: let minimalDelay = this._lastSaveTime + this._interval - Date.now(); michael@0: michael@0: // If we have to wait, set a timer, otherwise saveState directly michael@0: let delay = Math.max(minimalDelay, 2000); michael@0: if (delay > 0) { michael@0: this._saveTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); michael@0: this._saveTimer.init(this, delay, Ci.nsITimer.TYPE_ONE_SHOT); michael@0: } else { michael@0: this.saveState(); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: saveStateNow: function ss_saveStateNow() { michael@0: // Kill any queued timer and save immediately michael@0: if (this._saveTimer) { michael@0: this._saveTimer.cancel(); michael@0: this._saveTimer = null; michael@0: } michael@0: this.saveState(); michael@0: }, michael@0: michael@0: saveState: function ss_saveState() { michael@0: let data = this._getCurrentState(); michael@0: // sanity check before we overwrite the session file michael@0: if (data.windows && data.windows.length && data.selectedWindow) { michael@0: this._writeFile(this._sessionFile, JSON.stringify(data)); michael@0: michael@0: this._lastSaveTime = Date.now(); michael@0: } else { michael@0: dump("SessionStore: Not saving state with invalid data: " + JSON.stringify(data) + "\n"); michael@0: } michael@0: }, michael@0: michael@0: _getCurrentState: function ss_getCurrentState() { michael@0: let self = this; michael@0: this._forEachBrowserWindow(function(aWindow) { michael@0: self._collectWindowData(aWindow); michael@0: }); michael@0: michael@0: let data = { windows: [] }; michael@0: for (let i = 0; i < this._orderedWindows.length; i++) michael@0: data.windows.push(this._windows[this._orderedWindows[i]]); michael@0: data.selectedWindow = this._selectedWindow; michael@0: return data; michael@0: }, michael@0: michael@0: _collectTabData: function ss__collectTabData(aBrowser, aHistory) { michael@0: // If this browser is being restored, skip any session save activity michael@0: if (aBrowser.__SS_restore) michael@0: return; michael@0: michael@0: let aHistory = aHistory || { entries: [{ url: aBrowser.currentURI.spec, title: aBrowser.contentTitle }], index: 1 }; michael@0: michael@0: let tabData = {}; michael@0: tabData.entries = aHistory.entries; michael@0: tabData.index = aHistory.index; michael@0: tabData.attributes = { image: aBrowser.mIconURL }; michael@0: michael@0: aBrowser.__SS_data = tabData; michael@0: }, michael@0: michael@0: _getTabData: function(aWindow) { michael@0: return aWindow.Browser.tabs michael@0: .filter(tab => !tab.isPrivate && tab.browser.__SS_data) michael@0: .map(tab => { michael@0: let browser = tab.browser; michael@0: let tabData = browser.__SS_data; michael@0: if (browser.__SS_extdata) michael@0: tabData.extData = browser.__SS_extdata; michael@0: return tabData; michael@0: }); michael@0: }, michael@0: michael@0: _collectWindowData: function ss__collectWindowData(aWindow) { michael@0: // Ignore windows not tracked by SessionStore michael@0: if (!aWindow.__SSID || !this._windows[aWindow.__SSID]) michael@0: return; michael@0: michael@0: let winData = this._windows[aWindow.__SSID]; michael@0: michael@0: let index = aWindow.Elements.browsers.selectedIndex; michael@0: winData.selected = parseInt(index) + 1; // 1-based michael@0: michael@0: let tabData = this._getTabData(aWindow); michael@0: winData.tabs = tabData.concat(this._tabsFromOtherGroups); michael@0: }, michael@0: michael@0: _forEachBrowserWindow: function ss_forEachBrowserWindow(aFunc) { michael@0: let windowsEnum = Services.wm.getEnumerator("navigator:browser"); michael@0: while (windowsEnum.hasMoreElements()) { michael@0: let window = windowsEnum.getNext(); michael@0: if (window.__SSID && !window.closed) michael@0: aFunc.call(this, window); michael@0: } michael@0: }, michael@0: michael@0: _writeFile: function ss_writeFile(aFile, aData) { michael@0: let stateString = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString); michael@0: stateString.data = aData; michael@0: Services.obs.notifyObservers(stateString, "sessionstore-state-write", ""); michael@0: michael@0: // Don't touch the file if an observer has deleted all state data michael@0: if (!stateString.data) michael@0: return; michael@0: michael@0: // Initialize the file output stream. michael@0: let ostream = Cc["@mozilla.org/network/safe-file-output-stream;1"].createInstance(Ci.nsIFileOutputStream); michael@0: ostream.init(aFile, 0x02 | 0x08 | 0x20, 0600, ostream.DEFER_OPEN); michael@0: michael@0: // Obtain a converter to convert our data to a UTF-8 encoded input stream. michael@0: let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter); michael@0: converter.charset = "UTF-8"; michael@0: michael@0: // Asynchronously copy the data to the file. michael@0: let istream = converter.convertToInputStream(aData); michael@0: NetUtil.asyncCopy(istream, ostream, function(rc) { michael@0: if (Components.isSuccessCode(rc)) { michael@0: if (Services.startup.shuttingDown) { michael@0: Services.obs.notifyObservers(null, "sessionstore-final-state-write-complete", ""); michael@0: } michael@0: Services.obs.notifyObservers(null, "sessionstore-state-write-complete", ""); michael@0: } michael@0: }); michael@0: }, michael@0: michael@0: _updateCrashReportURL: function ss_updateCrashReportURL(aWindow) { michael@0: #ifdef MOZ_CRASHREPORTER michael@0: try { michael@0: let currentURI = aWindow.Browser.selectedBrowser.currentURI.clone(); michael@0: // if the current URI contains a username/password, remove it michael@0: try { michael@0: currentURI.userPass = ""; michael@0: } michael@0: catch (ex) { } // ignore failures on about: URIs michael@0: michael@0: CrashReporter.annotateCrashReport("URL", currentURI.spec); michael@0: } michael@0: catch (ex) { michael@0: // don't make noise when crashreporter is built but not enabled michael@0: if (ex.result != Components.results.NS_ERROR_NOT_INITIALIZED) michael@0: Components.utils.reportError("SessionStore:" + ex); michael@0: } michael@0: #endif michael@0: }, michael@0: michael@0: getBrowserState: function ss_getBrowserState() { michael@0: let data = this._getCurrentState(); michael@0: return JSON.stringify(data); michael@0: }, michael@0: michael@0: getClosedTabCount: function ss_getClosedTabCount(aWindow) { michael@0: if (!aWindow || !aWindow.__SSID) michael@0: return 0; // not a browser window, or not otherwise tracked by SS. michael@0: michael@0: return this._windows[aWindow.__SSID]._closedTabs.length; michael@0: }, michael@0: michael@0: getClosedTabData: function ss_getClosedTabData(aWindow) { michael@0: if (!aWindow.__SSID) michael@0: throw (Components.returnCode = Cr.NS_ERROR_INVALID_ARG); michael@0: michael@0: return JSON.stringify(this._windows[aWindow.__SSID]._closedTabs); michael@0: }, michael@0: michael@0: undoCloseTab: function ss_undoCloseTab(aWindow, aIndex) { michael@0: if (!aWindow.__SSID) michael@0: throw (Components.returnCode = Cr.NS_ERROR_INVALID_ARG); michael@0: michael@0: let closedTabs = this._windows[aWindow.__SSID]._closedTabs; michael@0: if (!closedTabs) michael@0: return null; 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.returnCode = Cr.NS_ERROR_INVALID_ARG); 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: michael@0: // create a new tab and bring to front michael@0: let tab = aWindow.Browser.addTab(closedTab.state.entries[closedTab.state.index - 1].url, true); michael@0: michael@0: tab.browser.messageManager.sendAsyncMessage("WebNavigation:LoadURI", { michael@0: uri: closedTab.state.entries[closedTab.state.index - 1].url, michael@0: flags: null, michael@0: entries: closedTab.state.entries, michael@0: index: closedTab.state.index michael@0: }); michael@0: michael@0: // Put back the extra data michael@0: tab.browser.__SS_extdata = closedTab.extData; michael@0: michael@0: return tab.chromeTab; michael@0: }, michael@0: michael@0: forgetClosedTab: function ss_forgetClosedTab(aWindow, aIndex) { michael@0: if (!aWindow.__SSID) michael@0: throw (Components.returnCode = Cr.NS_ERROR_INVALID_ARG); michael@0: michael@0: let closedTabs = this._windows[aWindow.__SSID]._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.returnCode = Cr.NS_ERROR_INVALID_ARG); michael@0: michael@0: // remove closed tab from the array michael@0: closedTabs.splice(aIndex, 1); michael@0: }, michael@0: michael@0: getTabValue: function ss_getTabValue(aTab, aKey) { michael@0: let browser = aTab.linkedBrowser; michael@0: let data = browser.__SS_extdata || {}; michael@0: return data[aKey] || ""; michael@0: }, michael@0: michael@0: setTabValue: function ss_setTabValue(aTab, aKey, aStringValue) { michael@0: let browser = aTab.linkedBrowser; michael@0: michael@0: // Thumbnails are actually stored in the cache, so do the save and update the URI michael@0: if (aKey == "thumbnail") { michael@0: let file = this._sessionCache.clone(); michael@0: file.append("thumbnail-" + browser.contentWindowId); michael@0: file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600); michael@0: michael@0: let source = Services.io.newURI(aStringValue, "UTF8", null); michael@0: let target = Services.io.newFileURI(file) michael@0: michael@0: let persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Ci.nsIWebBrowserPersist); michael@0: persist.persistFlags = Ci.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES | Ci.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION; michael@0: persist.saveURI(source, null, null, null, null, file); michael@0: michael@0: aStringValue = target.spec; michael@0: } michael@0: michael@0: if (!browser.__SS_extdata) michael@0: browser.__SS_extdata = {}; michael@0: browser.__SS_extdata[aKey] = aStringValue; michael@0: this.saveStateDelayed(); michael@0: }, michael@0: michael@0: deleteTabValue: function ss_deleteTabValue(aTab, aKey) { michael@0: let browser = aTab.linkedBrowser; michael@0: if (browser.__SS_extdata && browser.__SS_extdata[aKey]) michael@0: delete browser.__SS_extdata[aKey]; michael@0: else michael@0: throw (Components.returnCode = Cr.NS_ERROR_INVALID_ARG); michael@0: }, michael@0: michael@0: shouldRestore: function ss_shouldRestore() { michael@0: return this._shouldRestore || (3 == Services.prefs.getIntPref("browser.startup.page")); michael@0: }, michael@0: michael@0: restoreLastSession: function ss_restoreLastSession(aBringToFront) { michael@0: let self = this; michael@0: function notifyObservers(aMessage) { michael@0: self._clearCache(); michael@0: Services.obs.notifyObservers(null, "sessionstore-windows-restored", aMessage || ""); michael@0: } michael@0: michael@0: // The previous session data has already been renamed to the backup file michael@0: if (!this._sessionFileBackup.exists()) { michael@0: notifyObservers("fail") michael@0: return; michael@0: } michael@0: michael@0: try { michael@0: let channel = NetUtil.newChannel(this._sessionFileBackup); michael@0: channel.contentType = "application/json"; michael@0: NetUtil.asyncFetch(channel, function(aStream, aResult) { michael@0: if (!Components.isSuccessCode(aResult)) { michael@0: Cu.reportError("SessionStore: Could not read from sessionstore.bak file"); michael@0: notifyObservers("fail"); michael@0: return; michael@0: } michael@0: michael@0: // Read session state file into a string and let observers modify the state before it's being used michael@0: let state = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString); michael@0: state.data = NetUtil.readInputStreamToString(aStream, aStream.available(), { charset : "UTF-8" }) || ""; michael@0: aStream.close(); michael@0: michael@0: Services.obs.notifyObservers(state, "sessionstore-state-read", ""); michael@0: michael@0: let data = null; michael@0: try { michael@0: data = JSON.parse(state.data); michael@0: } catch (ex) { michael@0: Cu.reportError("SessionStore: Could not parse JSON: " + ex); michael@0: } michael@0: michael@0: if (!data || data.windows.length == 0) { michael@0: notifyObservers("fail"); michael@0: return; michael@0: } michael@0: michael@0: let window = Services.wm.getMostRecentWindow("navigator:browser"); michael@0: michael@0: if (typeof data.selectedWindow == "number") { michael@0: this._selectedWindow = data.selectedWindow; michael@0: } michael@0: let windowIndex = this._selectedWindow - 1; michael@0: let tabs = data.windows[windowIndex].tabs; michael@0: let selected = data.windows[windowIndex].selected; michael@0: michael@0: let currentGroupId; michael@0: try { michael@0: currentGroupId = JSON.parse(data.windows[windowIndex].extData["tabview-groups"]).activeGroupId; michael@0: } catch (ex) { /* currentGroupId is undefined if user has no tab groups */ } michael@0: michael@0: // Move all window data from sessionstore.js to this._windows. michael@0: this._orderedWindows = []; michael@0: for (let i = 0; i < data.windows.length; i++) { michael@0: let SSID; michael@0: if (i != windowIndex) { michael@0: SSID = "window" + gUUIDGenerator.generateUUID().toString(); michael@0: this._windows[SSID] = data.windows[i]; michael@0: } else { michael@0: SSID = window.__SSID; michael@0: this._windows[SSID].extData = data.windows[i].extData; michael@0: this._windows[SSID]._closedTabs = michael@0: this._windows[SSID]._closedTabs.concat(data.windows[i]._closedTabs); michael@0: } michael@0: this._orderedWindows.push(SSID); michael@0: } michael@0: michael@0: if (selected > tabs.length) // Clamp the selected index if it's bogus michael@0: selected = 1; michael@0: michael@0: for (let i=0; i