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 = ["SessionSaver"]; michael@0: michael@0: const Cu = Components.utils; michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: michael@0: Cu.import("resource://gre/modules/Timer.jsm", this); 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/TelemetryStopwatch.jsm", this); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "console", michael@0: "resource://gre/modules/devtools/Console.jsm"); michael@0: XPCOMUtils.defineLazyModuleGetter(this, "PrivacyFilter", michael@0: "resource:///modules/sessionstore/PrivacyFilter.jsm"); michael@0: XPCOMUtils.defineLazyModuleGetter(this, "SessionStore", michael@0: "resource:///modules/sessionstore/SessionStore.jsm"); michael@0: XPCOMUtils.defineLazyModuleGetter(this, "SessionFile", michael@0: "resource:///modules/sessionstore/SessionFile.jsm"); michael@0: XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils", michael@0: "resource://gre/modules/PrivateBrowsingUtils.jsm"); michael@0: michael@0: // Minimal interval between two save operations (in milliseconds). michael@0: XPCOMUtils.defineLazyGetter(this, "gInterval", function () { michael@0: const PREF = "browser.sessionstore.interval"; michael@0: michael@0: // Observer that updates the cached value when the preference changes. michael@0: Services.prefs.addObserver(PREF, () => { michael@0: this.gInterval = Services.prefs.getIntPref(PREF); michael@0: michael@0: // Cancel any pending runs and call runDelayed() with michael@0: // zero to apply the newly configured interval. michael@0: SessionSaverInternal.cancel(); michael@0: SessionSaverInternal.runDelayed(0); michael@0: }, false); michael@0: michael@0: return Services.prefs.getIntPref(PREF); michael@0: }); michael@0: michael@0: // Notify observers about a given topic with a given subject. michael@0: function notify(subject, topic) { michael@0: Services.obs.notifyObservers(subject, topic, ""); michael@0: } michael@0: michael@0: // TelemetryStopwatch helper functions. michael@0: function stopWatch(method) { michael@0: return function (...histograms) { michael@0: for (let hist of histograms) { michael@0: TelemetryStopwatch[method]("FX_SESSION_RESTORE_" + hist); michael@0: } michael@0: }; michael@0: } michael@0: michael@0: let stopWatchStart = stopWatch("start"); michael@0: let stopWatchCancel = stopWatch("cancel"); michael@0: let stopWatchFinish = stopWatch("finish"); michael@0: michael@0: /** michael@0: * The external API implemented by the SessionSaver module. michael@0: */ michael@0: this.SessionSaver = Object.freeze({ michael@0: /** michael@0: * Immediately saves the current session to disk. michael@0: */ michael@0: run: function () { michael@0: return SessionSaverInternal.run(); michael@0: }, michael@0: michael@0: /** michael@0: * Saves the current session to disk delayed by a given amount of time. Should michael@0: * another delayed run be scheduled already, we will ignore the given delay michael@0: * and state saving may occur a little earlier. michael@0: */ michael@0: runDelayed: function () { michael@0: SessionSaverInternal.runDelayed(); michael@0: }, michael@0: michael@0: /** michael@0: * Sets the last save time to the current time. This will cause us to wait for michael@0: * at least the configured interval when runDelayed() is called next. michael@0: */ michael@0: updateLastSaveTime: function () { michael@0: SessionSaverInternal.updateLastSaveTime(); michael@0: }, michael@0: michael@0: /** michael@0: * Sets the last save time to zero. This will cause us to michael@0: * immediately save the next time runDelayed() is called. michael@0: */ michael@0: clearLastSaveTime: function () { michael@0: SessionSaverInternal.clearLastSaveTime(); michael@0: }, michael@0: michael@0: /** michael@0: * Cancels all pending session saves. michael@0: */ michael@0: cancel: function () { michael@0: SessionSaverInternal.cancel(); michael@0: } michael@0: }); michael@0: michael@0: /** michael@0: * The internal API. michael@0: */ michael@0: let SessionSaverInternal = { michael@0: /** michael@0: * The timeout ID referencing an active timer for a delayed save. When no michael@0: * save is pending, this is null. michael@0: */ michael@0: _timeoutID: null, michael@0: michael@0: /** michael@0: * A timestamp that keeps track of when we saved the session last. We will michael@0: * this to determine the correct interval between delayed saves to not deceed michael@0: * the configured session write interval. michael@0: */ michael@0: _lastSaveTime: 0, michael@0: michael@0: /** michael@0: * Immediately saves the current session to disk. michael@0: */ michael@0: run: function () { michael@0: return this._saveState(true /* force-update all windows */); michael@0: }, michael@0: michael@0: /** michael@0: * Saves the current session to disk delayed by a given amount of time. Should michael@0: * another delayed run be scheduled already, we will ignore the given delay michael@0: * and state saving may occur a little earlier. michael@0: * michael@0: * @param delay (optional) michael@0: * The minimum delay in milliseconds to wait for until we collect and michael@0: * save the current session. michael@0: */ michael@0: runDelayed: function (delay = 2000) { michael@0: // Bail out if there's a pending run. michael@0: if (this._timeoutID) { michael@0: return; michael@0: } michael@0: michael@0: // Interval until the next disk operation is allowed. michael@0: delay = Math.max(this._lastSaveTime + gInterval - Date.now(), delay, 0); michael@0: michael@0: // Schedule a state save. michael@0: this._timeoutID = setTimeout(() => this._saveStateAsync(), delay); michael@0: }, michael@0: michael@0: /** michael@0: * Sets the last save time to the current time. This will cause us to wait for michael@0: * at least the configured interval when runDelayed() is called next. michael@0: */ michael@0: updateLastSaveTime: function () { michael@0: this._lastSaveTime = Date.now(); michael@0: }, michael@0: michael@0: /** michael@0: * Sets the last save time to zero. This will cause us to michael@0: * immediately save the next time runDelayed() is called. michael@0: */ michael@0: clearLastSaveTime: function () { michael@0: this._lastSaveTime = 0; michael@0: }, michael@0: michael@0: /** michael@0: * Cancels all pending session saves. michael@0: */ michael@0: cancel: function () { michael@0: clearTimeout(this._timeoutID); michael@0: this._timeoutID = null; michael@0: }, michael@0: michael@0: /** michael@0: * Saves the current session state. Collects data and writes to disk. michael@0: * michael@0: * @param forceUpdateAllWindows (optional) michael@0: * Forces us to recollect data for all windows and will bypass and michael@0: * update the corresponding caches. michael@0: */ michael@0: _saveState: function (forceUpdateAllWindows = false) { michael@0: // Cancel any pending timeouts. michael@0: this.cancel(); michael@0: michael@0: if (PrivateBrowsingUtils.permanentPrivateBrowsing) { michael@0: // Don't save (or even collect) anything in permanent private michael@0: // browsing mode michael@0: michael@0: this.updateLastSaveTime(); michael@0: return Promise.resolve(); michael@0: } michael@0: michael@0: stopWatchStart("COLLECT_DATA_MS", "COLLECT_DATA_LONGEST_OP_MS"); michael@0: let state = SessionStore.getCurrentState(forceUpdateAllWindows); michael@0: PrivacyFilter.filterPrivateWindowsAndTabs(state); michael@0: michael@0: // Make sure that we keep the previous session if we started with a single michael@0: // private window and no non-private windows have been opened, yet. michael@0: if (state.deferredInitialState) { michael@0: state.windows = state.deferredInitialState.windows || []; michael@0: delete state.deferredInitialState; michael@0: } michael@0: michael@0: #ifndef XP_MACOSX michael@0: // We want to restore closed windows that are marked with _shouldRestore. michael@0: // We're doing this here because we want to control this only when saving michael@0: // the file. michael@0: while (state._closedWindows.length) { michael@0: let i = state._closedWindows.length - 1; michael@0: michael@0: if (!state._closedWindows[i]._shouldRestore) { michael@0: // We only need to go until _shouldRestore michael@0: // is falsy since we're going in reverse. michael@0: break; michael@0: } michael@0: michael@0: delete state._closedWindows[i]._shouldRestore; michael@0: state.windows.unshift(state._closedWindows.pop()); michael@0: } michael@0: #endif michael@0: michael@0: stopWatchFinish("COLLECT_DATA_MS", "COLLECT_DATA_LONGEST_OP_MS"); michael@0: return this._writeState(state); michael@0: }, michael@0: michael@0: /** michael@0: * Saves the current session state. Collects data asynchronously and calls michael@0: * _saveState() to collect data again (with a cache hit rate of hopefully michael@0: * 100%) and write to disk afterwards. michael@0: */ michael@0: _saveStateAsync: function () { michael@0: // Allow scheduling delayed saves again. michael@0: this._timeoutID = null; michael@0: michael@0: // Write to disk. michael@0: this._saveState(); michael@0: }, michael@0: michael@0: /** michael@0: * Write the given state object to disk. michael@0: */ michael@0: _writeState: function (state) { michael@0: // Inform observers michael@0: notify(null, "sessionstore-state-write"); michael@0: michael@0: stopWatchStart("SERIALIZE_DATA_MS", "SERIALIZE_DATA_LONGEST_OP_MS", "WRITE_STATE_LONGEST_OP_MS"); michael@0: let data = JSON.stringify(state); michael@0: stopWatchFinish("SERIALIZE_DATA_MS", "SERIALIZE_DATA_LONGEST_OP_MS"); michael@0: michael@0: // We update the time stamp before writing so that we don't write again michael@0: // too soon, if saving is requested before the write completes. Without michael@0: // this update we may save repeatedly if actions cause a runDelayed michael@0: // before writing has completed. See Bug 902280 michael@0: this.updateLastSaveTime(); michael@0: michael@0: // Write (atomically) to a session file, using a tmp file. Once the session michael@0: // file is successfully updated, save the time stamp of the last save and michael@0: // notify the observers. michael@0: stopWatchStart("SEND_SERIALIZED_STATE_LONGEST_OP_MS"); michael@0: let promise = SessionFile.write(data); michael@0: stopWatchFinish("WRITE_STATE_LONGEST_OP_MS", michael@0: "SEND_SERIALIZED_STATE_LONGEST_OP_MS"); michael@0: promise = promise.then(() => { michael@0: this.updateLastSaveTime(); michael@0: notify(null, "sessionstore-state-write-complete"); michael@0: }, console.error); michael@0: michael@0: return promise; michael@0: }, michael@0: };