browser/components/sessionstore/src/SessionSaver.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 this.EXPORTED_SYMBOLS = ["SessionSaver"];
michael@0 8
michael@0 9 const Cu = Components.utils;
michael@0 10 const Cc = Components.classes;
michael@0 11 const Ci = Components.interfaces;
michael@0 12
michael@0 13 Cu.import("resource://gre/modules/Timer.jsm", this);
michael@0 14 Cu.import("resource://gre/modules/Services.jsm", this);
michael@0 15 Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
michael@0 16 Cu.import("resource://gre/modules/TelemetryStopwatch.jsm", this);
michael@0 17
michael@0 18 XPCOMUtils.defineLazyModuleGetter(this, "console",
michael@0 19 "resource://gre/modules/devtools/Console.jsm");
michael@0 20 XPCOMUtils.defineLazyModuleGetter(this, "PrivacyFilter",
michael@0 21 "resource:///modules/sessionstore/PrivacyFilter.jsm");
michael@0 22 XPCOMUtils.defineLazyModuleGetter(this, "SessionStore",
michael@0 23 "resource:///modules/sessionstore/SessionStore.jsm");
michael@0 24 XPCOMUtils.defineLazyModuleGetter(this, "SessionFile",
michael@0 25 "resource:///modules/sessionstore/SessionFile.jsm");
michael@0 26 XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
michael@0 27 "resource://gre/modules/PrivateBrowsingUtils.jsm");
michael@0 28
michael@0 29 // Minimal interval between two save operations (in milliseconds).
michael@0 30 XPCOMUtils.defineLazyGetter(this, "gInterval", function () {
michael@0 31 const PREF = "browser.sessionstore.interval";
michael@0 32
michael@0 33 // Observer that updates the cached value when the preference changes.
michael@0 34 Services.prefs.addObserver(PREF, () => {
michael@0 35 this.gInterval = Services.prefs.getIntPref(PREF);
michael@0 36
michael@0 37 // Cancel any pending runs and call runDelayed() with
michael@0 38 // zero to apply the newly configured interval.
michael@0 39 SessionSaverInternal.cancel();
michael@0 40 SessionSaverInternal.runDelayed(0);
michael@0 41 }, false);
michael@0 42
michael@0 43 return Services.prefs.getIntPref(PREF);
michael@0 44 });
michael@0 45
michael@0 46 // Notify observers about a given topic with a given subject.
michael@0 47 function notify(subject, topic) {
michael@0 48 Services.obs.notifyObservers(subject, topic, "");
michael@0 49 }
michael@0 50
michael@0 51 // TelemetryStopwatch helper functions.
michael@0 52 function stopWatch(method) {
michael@0 53 return function (...histograms) {
michael@0 54 for (let hist of histograms) {
michael@0 55 TelemetryStopwatch[method]("FX_SESSION_RESTORE_" + hist);
michael@0 56 }
michael@0 57 };
michael@0 58 }
michael@0 59
michael@0 60 let stopWatchStart = stopWatch("start");
michael@0 61 let stopWatchCancel = stopWatch("cancel");
michael@0 62 let stopWatchFinish = stopWatch("finish");
michael@0 63
michael@0 64 /**
michael@0 65 * The external API implemented by the SessionSaver module.
michael@0 66 */
michael@0 67 this.SessionSaver = Object.freeze({
michael@0 68 /**
michael@0 69 * Immediately saves the current session to disk.
michael@0 70 */
michael@0 71 run: function () {
michael@0 72 return SessionSaverInternal.run();
michael@0 73 },
michael@0 74
michael@0 75 /**
michael@0 76 * Saves the current session to disk delayed by a given amount of time. Should
michael@0 77 * another delayed run be scheduled already, we will ignore the given delay
michael@0 78 * and state saving may occur a little earlier.
michael@0 79 */
michael@0 80 runDelayed: function () {
michael@0 81 SessionSaverInternal.runDelayed();
michael@0 82 },
michael@0 83
michael@0 84 /**
michael@0 85 * Sets the last save time to the current time. This will cause us to wait for
michael@0 86 * at least the configured interval when runDelayed() is called next.
michael@0 87 */
michael@0 88 updateLastSaveTime: function () {
michael@0 89 SessionSaverInternal.updateLastSaveTime();
michael@0 90 },
michael@0 91
michael@0 92 /**
michael@0 93 * Sets the last save time to zero. This will cause us to
michael@0 94 * immediately save the next time runDelayed() is called.
michael@0 95 */
michael@0 96 clearLastSaveTime: function () {
michael@0 97 SessionSaverInternal.clearLastSaveTime();
michael@0 98 },
michael@0 99
michael@0 100 /**
michael@0 101 * Cancels all pending session saves.
michael@0 102 */
michael@0 103 cancel: function () {
michael@0 104 SessionSaverInternal.cancel();
michael@0 105 }
michael@0 106 });
michael@0 107
michael@0 108 /**
michael@0 109 * The internal API.
michael@0 110 */
michael@0 111 let SessionSaverInternal = {
michael@0 112 /**
michael@0 113 * The timeout ID referencing an active timer for a delayed save. When no
michael@0 114 * save is pending, this is null.
michael@0 115 */
michael@0 116 _timeoutID: null,
michael@0 117
michael@0 118 /**
michael@0 119 * A timestamp that keeps track of when we saved the session last. We will
michael@0 120 * this to determine the correct interval between delayed saves to not deceed
michael@0 121 * the configured session write interval.
michael@0 122 */
michael@0 123 _lastSaveTime: 0,
michael@0 124
michael@0 125 /**
michael@0 126 * Immediately saves the current session to disk.
michael@0 127 */
michael@0 128 run: function () {
michael@0 129 return this._saveState(true /* force-update all windows */);
michael@0 130 },
michael@0 131
michael@0 132 /**
michael@0 133 * Saves the current session to disk delayed by a given amount of time. Should
michael@0 134 * another delayed run be scheduled already, we will ignore the given delay
michael@0 135 * and state saving may occur a little earlier.
michael@0 136 *
michael@0 137 * @param delay (optional)
michael@0 138 * The minimum delay in milliseconds to wait for until we collect and
michael@0 139 * save the current session.
michael@0 140 */
michael@0 141 runDelayed: function (delay = 2000) {
michael@0 142 // Bail out if there's a pending run.
michael@0 143 if (this._timeoutID) {
michael@0 144 return;
michael@0 145 }
michael@0 146
michael@0 147 // Interval until the next disk operation is allowed.
michael@0 148 delay = Math.max(this._lastSaveTime + gInterval - Date.now(), delay, 0);
michael@0 149
michael@0 150 // Schedule a state save.
michael@0 151 this._timeoutID = setTimeout(() => this._saveStateAsync(), delay);
michael@0 152 },
michael@0 153
michael@0 154 /**
michael@0 155 * Sets the last save time to the current time. This will cause us to wait for
michael@0 156 * at least the configured interval when runDelayed() is called next.
michael@0 157 */
michael@0 158 updateLastSaveTime: function () {
michael@0 159 this._lastSaveTime = Date.now();
michael@0 160 },
michael@0 161
michael@0 162 /**
michael@0 163 * Sets the last save time to zero. This will cause us to
michael@0 164 * immediately save the next time runDelayed() is called.
michael@0 165 */
michael@0 166 clearLastSaveTime: function () {
michael@0 167 this._lastSaveTime = 0;
michael@0 168 },
michael@0 169
michael@0 170 /**
michael@0 171 * Cancels all pending session saves.
michael@0 172 */
michael@0 173 cancel: function () {
michael@0 174 clearTimeout(this._timeoutID);
michael@0 175 this._timeoutID = null;
michael@0 176 },
michael@0 177
michael@0 178 /**
michael@0 179 * Saves the current session state. Collects data and writes to disk.
michael@0 180 *
michael@0 181 * @param forceUpdateAllWindows (optional)
michael@0 182 * Forces us to recollect data for all windows and will bypass and
michael@0 183 * update the corresponding caches.
michael@0 184 */
michael@0 185 _saveState: function (forceUpdateAllWindows = false) {
michael@0 186 // Cancel any pending timeouts.
michael@0 187 this.cancel();
michael@0 188
michael@0 189 if (PrivateBrowsingUtils.permanentPrivateBrowsing) {
michael@0 190 // Don't save (or even collect) anything in permanent private
michael@0 191 // browsing mode
michael@0 192
michael@0 193 this.updateLastSaveTime();
michael@0 194 return Promise.resolve();
michael@0 195 }
michael@0 196
michael@0 197 stopWatchStart("COLLECT_DATA_MS", "COLLECT_DATA_LONGEST_OP_MS");
michael@0 198 let state = SessionStore.getCurrentState(forceUpdateAllWindows);
michael@0 199 PrivacyFilter.filterPrivateWindowsAndTabs(state);
michael@0 200
michael@0 201 // Make sure that we keep the previous session if we started with a single
michael@0 202 // private window and no non-private windows have been opened, yet.
michael@0 203 if (state.deferredInitialState) {
michael@0 204 state.windows = state.deferredInitialState.windows || [];
michael@0 205 delete state.deferredInitialState;
michael@0 206 }
michael@0 207
michael@0 208 #ifndef XP_MACOSX
michael@0 209 // We want to restore closed windows that are marked with _shouldRestore.
michael@0 210 // We're doing this here because we want to control this only when saving
michael@0 211 // the file.
michael@0 212 while (state._closedWindows.length) {
michael@0 213 let i = state._closedWindows.length - 1;
michael@0 214
michael@0 215 if (!state._closedWindows[i]._shouldRestore) {
michael@0 216 // We only need to go until _shouldRestore
michael@0 217 // is falsy since we're going in reverse.
michael@0 218 break;
michael@0 219 }
michael@0 220
michael@0 221 delete state._closedWindows[i]._shouldRestore;
michael@0 222 state.windows.unshift(state._closedWindows.pop());
michael@0 223 }
michael@0 224 #endif
michael@0 225
michael@0 226 stopWatchFinish("COLLECT_DATA_MS", "COLLECT_DATA_LONGEST_OP_MS");
michael@0 227 return this._writeState(state);
michael@0 228 },
michael@0 229
michael@0 230 /**
michael@0 231 * Saves the current session state. Collects data asynchronously and calls
michael@0 232 * _saveState() to collect data again (with a cache hit rate of hopefully
michael@0 233 * 100%) and write to disk afterwards.
michael@0 234 */
michael@0 235 _saveStateAsync: function () {
michael@0 236 // Allow scheduling delayed saves again.
michael@0 237 this._timeoutID = null;
michael@0 238
michael@0 239 // Write to disk.
michael@0 240 this._saveState();
michael@0 241 },
michael@0 242
michael@0 243 /**
michael@0 244 * Write the given state object to disk.
michael@0 245 */
michael@0 246 _writeState: function (state) {
michael@0 247 // Inform observers
michael@0 248 notify(null, "sessionstore-state-write");
michael@0 249
michael@0 250 stopWatchStart("SERIALIZE_DATA_MS", "SERIALIZE_DATA_LONGEST_OP_MS", "WRITE_STATE_LONGEST_OP_MS");
michael@0 251 let data = JSON.stringify(state);
michael@0 252 stopWatchFinish("SERIALIZE_DATA_MS", "SERIALIZE_DATA_LONGEST_OP_MS");
michael@0 253
michael@0 254 // We update the time stamp before writing so that we don't write again
michael@0 255 // too soon, if saving is requested before the write completes. Without
michael@0 256 // this update we may save repeatedly if actions cause a runDelayed
michael@0 257 // before writing has completed. See Bug 902280
michael@0 258 this.updateLastSaveTime();
michael@0 259
michael@0 260 // Write (atomically) to a session file, using a tmp file. Once the session
michael@0 261 // file is successfully updated, save the time stamp of the last save and
michael@0 262 // notify the observers.
michael@0 263 stopWatchStart("SEND_SERIALIZED_STATE_LONGEST_OP_MS");
michael@0 264 let promise = SessionFile.write(data);
michael@0 265 stopWatchFinish("WRITE_STATE_LONGEST_OP_MS",
michael@0 266 "SEND_SERIALIZED_STATE_LONGEST_OP_MS");
michael@0 267 promise = promise.then(() => {
michael@0 268 this.updateLastSaveTime();
michael@0 269 notify(null, "sessionstore-state-write-complete");
michael@0 270 }, console.error);
michael@0 271
michael@0 272 return promise;
michael@0 273 },
michael@0 274 };

mercurial