2012-08-10 20:20:25 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2006-06-18 00:42:08 +00:00
|
|
|
|
2013-08-10 03:33:07 +00:00
|
|
|
"use strict";
|
|
|
|
|
2006-06-18 00:42:08 +00:00
|
|
|
/**
|
2012-08-10 20:20:25 +00:00
|
|
|
* Session Storage and Restoration
|
|
|
|
*
|
|
|
|
* Overview
|
|
|
|
* This service reads user's session file at startup, and makes a determination
|
|
|
|
* as to whether the session should be restored. It will restore the session
|
|
|
|
* under the circumstances described below. If the auto-start Private Browsing
|
|
|
|
* mode is active, however, the session is never restored.
|
|
|
|
*
|
|
|
|
* Crash Detection
|
2014-01-17 10:40:18 +00:00
|
|
|
* The CrashMonitor is used to check if the final session state was successfully
|
|
|
|
* written at shutdown of the last session. If we did not reach
|
|
|
|
* 'sessionstore-final-state-write-complete', then it's assumed that the browser
|
|
|
|
* has previously crashed and we should restore the session.
|
2012-08-10 20:20:25 +00:00
|
|
|
*
|
|
|
|
* Forced Restarts
|
|
|
|
* In the event that a restart is required due to application update or extension
|
|
|
|
* installation, set the browser.sessionstore.resume_session_once pref to true,
|
|
|
|
* and the session will be restored the next time the browser starts.
|
|
|
|
*
|
|
|
|
* Always Resume
|
|
|
|
* This service will always resume the session if the integer pref
|
|
|
|
* browser.startup.page is set to 3.
|
|
|
|
*/
|
2006-06-18 00:42:08 +00:00
|
|
|
|
|
|
|
/* :::::::: Constants and Helpers ::::::::::::::: */
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
2010-01-22 20:21:41 +00:00
|
|
|
const Cu = Components.utils;
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2010-03-19 22:43:01 +00:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2012-03-16 16:21:19 +00:00
|
|
|
Cu.import("resource://gre/modules/TelemetryStopwatch.jsm");
|
2012-10-24 20:21:33 +00:00
|
|
|
Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
|
2013-07-25 19:50:15 +00:00
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
2012-12-20 01:04:26 +00:00
|
|
|
|
2014-01-06 20:27:25 +00:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "console",
|
2015-10-15 10:45:22 +00:00
|
|
|
"resource://gre/modules/Console.jsm");
|
2013-10-25 09:52:42 +00:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "SessionFile",
|
|
|
|
"resource:///modules/sessionstore/SessionFile.jsm");
|
2014-01-17 10:40:18 +00:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "CrashMonitor",
|
|
|
|
"resource://gre/modules/CrashMonitor.jsm");
|
2006-06-18 00:42:08 +00:00
|
|
|
|
|
|
|
const STATE_RUNNING_STR = "running";
|
|
|
|
|
2013-11-19 19:57:46 +00:00
|
|
|
// 'browser.startup.page' preference value to resume the previous session.
|
|
|
|
const BROWSER_STARTUP_RESUME_SESSION = 3;
|
|
|
|
|
2006-06-18 00:42:08 +00:00
|
|
|
function debug(aMsg) {
|
|
|
|
aMsg = ("SessionStartup: " + aMsg).replace(/\S{80}/g, "$&\n");
|
2010-03-19 22:43:01 +00:00
|
|
|
Services.console.logStringMessage(aMsg);
|
2006-06-18 00:42:08 +00:00
|
|
|
}
|
2014-06-24 14:51:15 +00:00
|
|
|
function warning(aMsg, aException) {
|
|
|
|
let consoleMsg = Cc["@mozilla.org/scripterror;1"].createInstance(Ci.nsIScriptError);
|
|
|
|
consoleMsg.init(aMsg, aException.fileName, null, aException.lineNumber, 0, Ci.nsIScriptError.warningFlag, "component javascript");
|
|
|
|
Services.console.logMessage(consoleMsg);
|
|
|
|
}
|
2006-06-18 00:42:08 +00:00
|
|
|
|
2015-09-15 18:19:45 +00:00
|
|
|
var gOnceInitializedDeferred = (function () {
|
2014-09-25 07:14:53 +00:00
|
|
|
let deferred = {};
|
|
|
|
|
|
|
|
deferred.promise = new Promise((resolve, reject) => {
|
|
|
|
deferred.resolve = resolve;
|
|
|
|
deferred.reject = reject;
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred;
|
|
|
|
})();
|
2012-12-20 01:04:26 +00:00
|
|
|
|
2006-06-18 00:42:08 +00:00
|
|
|
/* :::::::: The Service ::::::::::::::: */
|
|
|
|
|
|
|
|
function SessionStartup() {
|
|
|
|
}
|
|
|
|
|
|
|
|
SessionStartup.prototype = {
|
|
|
|
|
2006-08-03 19:19:03 +00:00
|
|
|
// the state to restore at startup
|
2011-07-15 16:42:21 +00:00
|
|
|
_initialState: null,
|
2008-02-14 21:53:33 +00:00
|
|
|
_sessionType: Ci.nsISessionStartup.NO_SESSION,
|
2012-12-20 01:04:26 +00:00
|
|
|
_initialized: false,
|
2006-08-03 19:19:03 +00:00
|
|
|
|
2014-01-17 10:40:18 +00:00
|
|
|
// Stores whether the previous session crashed.
|
|
|
|
_previousSessionCrashed: null,
|
|
|
|
|
2006-06-18 00:42:08 +00:00
|
|
|
/* ........ Global Event Handlers .............. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the component
|
|
|
|
*/
|
|
|
|
init: function sss_init() {
|
2014-04-04 12:34:24 +00:00
|
|
|
Services.obs.notifyObservers(null, "sessionstore-init-started", null);
|
|
|
|
|
2009-05-02 05:17:22 +00:00
|
|
|
// do not need to initialize anything in auto-started private browsing sessions
|
2013-02-23 06:45:37 +00:00
|
|
|
if (PrivateBrowsingUtils.permanentPrivateBrowsing) {
|
|
|
|
this._initialized = true;
|
|
|
|
gOnceInitializedDeferred.resolve();
|
2012-12-20 22:50:35 +00:00
|
|
|
return;
|
2013-02-23 06:45:37 +00:00
|
|
|
}
|
2012-12-20 22:50:35 +00:00
|
|
|
|
2013-10-25 09:52:42 +00:00
|
|
|
SessionFile.read().then(
|
2013-09-17 15:37:28 +00:00
|
|
|
this._onSessionFileRead.bind(this),
|
2014-01-06 20:27:25 +00:00
|
|
|
console.error
|
2012-12-20 01:04:26 +00:00
|
|
|
);
|
|
|
|
},
|
2009-05-02 05:17:22 +00:00
|
|
|
|
2012-12-20 01:04:26 +00:00
|
|
|
// Wrap a string as a nsISupports
|
|
|
|
_createSupportsString: function ssfi_createSupportsString(aData) {
|
|
|
|
let string = Cc["@mozilla.org/supports-string;1"]
|
|
|
|
.createInstance(Ci.nsISupportsString);
|
|
|
|
string.data = aData;
|
|
|
|
return string;
|
|
|
|
},
|
2010-06-09 20:55:45 +00:00
|
|
|
|
2014-01-17 10:40:18 +00:00
|
|
|
/**
|
|
|
|
* Complete initialization once the Session File has been read
|
|
|
|
*
|
2014-06-24 14:51:15 +00:00
|
|
|
* @param source The Session State string read from disk.
|
|
|
|
* @param parsed The object obtained by parsing |source| as JSON.
|
2014-01-17 10:40:18 +00:00
|
|
|
*/
|
2014-08-12 19:38:55 +00:00
|
|
|
_onSessionFileRead: function ({source, parsed, noFilesFound}) {
|
2014-01-17 10:40:18 +00:00
|
|
|
this._initialized = true;
|
2012-12-15 17:08:04 +00:00
|
|
|
|
2014-01-17 10:40:18 +00:00
|
|
|
// Let observers modify the state before it is used
|
2014-06-24 14:51:15 +00:00
|
|
|
let supportsStateString = this._createSupportsString(source);
|
2014-01-17 10:40:18 +00:00
|
|
|
Services.obs.notifyObservers(supportsStateString, "sessionstore-state-read", "");
|
2014-06-24 14:51:15 +00:00
|
|
|
let stateString = supportsStateString.data;
|
|
|
|
|
|
|
|
if (stateString != source) {
|
|
|
|
// The session has been modified by an add-on, reparse.
|
|
|
|
try {
|
|
|
|
this._initialState = JSON.parse(stateString);
|
|
|
|
} catch (ex) {
|
|
|
|
// That's not very good, an add-on has rewritten the initial
|
|
|
|
// state to something that won't parse.
|
|
|
|
warning("Observer rewrote the state to something that won't parse", ex);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No need to reparse
|
|
|
|
this._initialState = parsed;
|
|
|
|
}
|
2012-12-15 15:44:07 +00:00
|
|
|
|
2014-06-24 14:51:15 +00:00
|
|
|
if (this._initialState == null) {
|
|
|
|
// No valid session found.
|
2014-01-17 10:40:18 +00:00
|
|
|
this._sessionType = Ci.nsISessionStartup.NO_SESSION;
|
|
|
|
Services.obs.notifyObservers(null, "sessionstore-state-finalized", "");
|
|
|
|
gOnceInitializedDeferred.resolve();
|
|
|
|
return;
|
|
|
|
}
|
2012-12-20 01:04:26 +00:00
|
|
|
|
2014-01-17 10:40:18 +00:00
|
|
|
let shouldResumeSessionOnce = Services.prefs.getBoolPref("browser.sessionstore.resume_session_once");
|
|
|
|
let shouldResumeSession = shouldResumeSessionOnce ||
|
|
|
|
Services.prefs.getIntPref("browser.startup.page") == BROWSER_STARTUP_RESUME_SESSION;
|
|
|
|
|
|
|
|
// If this is a normal restore then throw away any previous session
|
2014-04-25 12:34:03 +00:00
|
|
|
if (!shouldResumeSessionOnce && this._initialState) {
|
2014-01-17 10:40:18 +00:00
|
|
|
delete this._initialState.lastSessionState;
|
2014-04-25 12:34:03 +00:00
|
|
|
}
|
2014-01-17 10:40:18 +00:00
|
|
|
|
|
|
|
let resumeFromCrash = Services.prefs.getBoolPref("browser.sessionstore.resume_from_crash");
|
|
|
|
|
|
|
|
CrashMonitor.previousCheckpoints.then(checkpoints => {
|
|
|
|
if (checkpoints) {
|
|
|
|
// If the previous session finished writing the final state, we'll
|
|
|
|
// assume there was no crash.
|
|
|
|
this._previousSessionCrashed = !checkpoints["sessionstore-final-state-write-complete"];
|
2014-08-12 19:38:55 +00:00
|
|
|
|
2014-01-17 10:40:18 +00:00
|
|
|
} else {
|
|
|
|
// If the Crash Monitor could not load a checkpoints file it will
|
|
|
|
// provide null. This could occur on the first run after updating to
|
|
|
|
// a version including the Crash Monitor, or if the checkpoints file
|
2014-07-08 03:58:00 +00:00
|
|
|
// was removed, or on first startup with this profile, or after Firefox Reset.
|
|
|
|
|
2014-08-12 19:38:55 +00:00
|
|
|
if (noFilesFound) {
|
|
|
|
// There was no checkpoints file and no sessionstore.js or its backups
|
|
|
|
// so we will assume that this was a fresh profile.
|
2014-07-08 03:58:00 +00:00
|
|
|
this._previousSessionCrashed = false;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// If this is the first run after an update, sessionstore.js should
|
|
|
|
// still contain the session.state flag to indicate if the session
|
|
|
|
// crashed. If it is not present, we will assume this was not the first
|
|
|
|
// run after update and the checkpoints file was somehow corrupted or
|
|
|
|
// removed by a crash.
|
|
|
|
//
|
|
|
|
// If the session.state flag is present, we will fallback to using it
|
|
|
|
// for crash detection - If the last write of sessionstore.js had it
|
|
|
|
// set to "running", we crashed.
|
|
|
|
let stateFlagPresent = (this._initialState.session &&
|
|
|
|
this._initialState.session.state);
|
|
|
|
|
|
|
|
|
|
|
|
this._previousSessionCrashed = !stateFlagPresent ||
|
|
|
|
(this._initialState.session.state == STATE_RUNNING_STR);
|
|
|
|
}
|
2012-12-20 01:04:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Report shutdown success via telemetry. Shortcoming here are
|
|
|
|
// being-killed-by-OS-shutdown-logic, shutdown freezing after
|
|
|
|
// session restore was written, etc.
|
2014-01-17 10:40:18 +00:00
|
|
|
Services.telemetry.getHistogramById("SHUTDOWN_OK").add(!this._previousSessionCrashed);
|
2012-12-20 01:04:26 +00:00
|
|
|
|
|
|
|
// set the startup type
|
2014-01-17 10:40:18 +00:00
|
|
|
if (this._previousSessionCrashed && resumeFromCrash)
|
2012-12-20 01:04:26 +00:00
|
|
|
this._sessionType = Ci.nsISessionStartup.RECOVER_SESSION;
|
2014-01-17 10:40:18 +00:00
|
|
|
else if (!this._previousSessionCrashed && shouldResumeSession)
|
2012-12-20 01:04:26 +00:00
|
|
|
this._sessionType = Ci.nsISessionStartup.RESUME_SESSION;
|
|
|
|
else if (this._initialState)
|
|
|
|
this._sessionType = Ci.nsISessionStartup.DEFER_SESSION;
|
|
|
|
else
|
|
|
|
this._initialState = null; // reset the state
|
|
|
|
|
|
|
|
Services.obs.addObserver(this, "sessionstore-windows-restored", true);
|
|
|
|
|
|
|
|
if (this._sessionType != Ci.nsISessionStartup.NO_SESSION)
|
|
|
|
Services.obs.addObserver(this, "browser:purge-session-history", true);
|
|
|
|
|
|
|
|
// We're ready. Notify everyone else.
|
|
|
|
Services.obs.notifyObservers(null, "sessionstore-state-finalized", "");
|
|
|
|
gOnceInitializedDeferred.resolve();
|
2014-01-17 10:40:18 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2006-06-18 00:42:08 +00:00
|
|
|
/**
|
|
|
|
* Handle notifications
|
|
|
|
*/
|
|
|
|
observe: function sss_observe(aSubject, aTopic, aData) {
|
|
|
|
switch (aTopic) {
|
2012-05-25 21:39:22 +00:00
|
|
|
case "app-startup":
|
2010-03-19 22:43:01 +00:00
|
|
|
Services.obs.addObserver(this, "final-ui-startup", true);
|
|
|
|
Services.obs.addObserver(this, "quit-application", true);
|
2006-06-18 00:42:08 +00:00
|
|
|
break;
|
2012-05-25 21:39:22 +00:00
|
|
|
case "final-ui-startup":
|
2010-03-19 22:43:01 +00:00
|
|
|
Services.obs.removeObserver(this, "final-ui-startup");
|
|
|
|
Services.obs.removeObserver(this, "quit-application");
|
2006-06-18 00:42:08 +00:00
|
|
|
this.init();
|
|
|
|
break;
|
2008-08-19 17:08:30 +00:00
|
|
|
case "quit-application":
|
2008-09-05 10:05:34 +00:00
|
|
|
// no reason for initializing at this point (cf. bug 409115)
|
2010-03-19 22:43:01 +00:00
|
|
|
Services.obs.removeObserver(this, "final-ui-startup");
|
|
|
|
Services.obs.removeObserver(this, "quit-application");
|
2011-10-25 17:19:28 +00:00
|
|
|
if (this._sessionType != Ci.nsISessionStartup.NO_SESSION)
|
|
|
|
Services.obs.removeObserver(this, "browser:purge-session-history");
|
2008-08-19 17:08:30 +00:00
|
|
|
break;
|
2011-01-20 21:43:32 +00:00
|
|
|
case "sessionstore-windows-restored":
|
|
|
|
Services.obs.removeObserver(this, "sessionstore-windows-restored");
|
2011-07-15 16:42:21 +00:00
|
|
|
// free _initialState after nsSessionStore is done with it
|
|
|
|
this._initialState = null;
|
2011-10-25 17:19:28 +00:00
|
|
|
break;
|
|
|
|
case "browser:purge-session-history":
|
|
|
|
Services.obs.removeObserver(this, "browser:purge-session-history");
|
|
|
|
// reset all state on sanitization
|
2008-10-11 18:58:21 +00:00
|
|
|
this._sessionType = Ci.nsISessionStartup.NO_SESSION;
|
|
|
|
break;
|
2006-06-18 00:42:08 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/* ........ Public API ................*/
|
|
|
|
|
2012-12-20 01:04:26 +00:00
|
|
|
get onceInitialized() {
|
|
|
|
return gOnceInitializedDeferred.promise;
|
|
|
|
},
|
|
|
|
|
2006-06-18 00:42:08 +00:00
|
|
|
/**
|
2011-07-25 23:29:57 +00:00
|
|
|
* Get the session state as a jsval
|
2006-06-18 00:42:08 +00:00
|
|
|
*/
|
|
|
|
get state() {
|
2011-07-15 16:42:21 +00:00
|
|
|
return this._initialState;
|
2006-06-18 00:42:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-19 19:57:46 +00:00
|
|
|
* Determines whether there is a pending session restore. Should only be
|
|
|
|
* called after initialization has completed.
|
2006-06-18 00:42:08 +00:00
|
|
|
* @returns bool
|
|
|
|
*/
|
|
|
|
doRestore: function sss_doRestore() {
|
2013-07-26 11:16:29 +00:00
|
|
|
return this._willRestore();
|
|
|
|
},
|
|
|
|
|
2013-11-19 19:57:46 +00:00
|
|
|
/**
|
|
|
|
* Determines whether automatic session restoration is enabled for this
|
|
|
|
* launch of the browser. This does not include crash restoration. In
|
|
|
|
* particular, if session restore is configured to restore only in case of
|
|
|
|
* crash, this method returns false.
|
|
|
|
* @returns bool
|
|
|
|
*/
|
|
|
|
isAutomaticRestoreEnabled: function () {
|
|
|
|
return Services.prefs.getBoolPref("browser.sessionstore.resume_session_once") ||
|
|
|
|
Services.prefs.getIntPref("browser.startup.page") == BROWSER_STARTUP_RESUME_SESSION;
|
|
|
|
},
|
|
|
|
|
2013-07-26 11:16:29 +00:00
|
|
|
/**
|
|
|
|
* Determines whether there is a pending session restore.
|
|
|
|
* @returns bool
|
|
|
|
*/
|
|
|
|
_willRestore: function () {
|
2010-09-10 17:57:28 +00:00
|
|
|
return this._sessionType == Ci.nsISessionStartup.RECOVER_SESSION ||
|
|
|
|
this._sessionType == Ci.nsISessionStartup.RESUME_SESSION;
|
2008-02-14 21:53:33 +00:00
|
|
|
},
|
|
|
|
|
2013-07-26 11:16:29 +00:00
|
|
|
/**
|
|
|
|
* Returns whether we will restore a session that ends up replacing the
|
|
|
|
* homepage. The browser uses this to not start loading the homepage if
|
|
|
|
* we're going to stop its load anyway shortly after.
|
|
|
|
*
|
|
|
|
* This is meant to be an optimization for the average case that loading the
|
|
|
|
* session file finishes before we may want to start loading the default
|
|
|
|
* homepage. Should this be called before the session file has been read it
|
|
|
|
* will just return false.
|
|
|
|
*
|
|
|
|
* @returns bool
|
|
|
|
*/
|
|
|
|
get willOverrideHomepage() {
|
|
|
|
if (this._initialState && this._willRestore()) {
|
|
|
|
let windows = this._initialState.windows || null;
|
|
|
|
// If there are valid windows with not only pinned tabs, signal that we
|
|
|
|
// will override the default homepage by restoring a session.
|
|
|
|
return windows && windows.some(w => w.tabs.some(t => !t.pinned));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2008-02-14 21:53:33 +00:00
|
|
|
/**
|
|
|
|
* Get the type of pending session store, if any.
|
|
|
|
*/
|
|
|
|
get sessionType() {
|
|
|
|
return this._sessionType;
|
2006-06-18 00:42:08 +00:00
|
|
|
},
|
|
|
|
|
2014-01-17 10:40:18 +00:00
|
|
|
/**
|
|
|
|
* Get whether the previous session crashed.
|
|
|
|
*/
|
|
|
|
get previousSessionCrashed() {
|
|
|
|
return this._previousSessionCrashed;
|
|
|
|
},
|
|
|
|
|
2007-09-18 16:36:17 +00:00
|
|
|
/* ........ QueryInterface .............. */
|
|
|
|
QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference,
|
|
|
|
Ci.nsISessionStartup]),
|
2013-02-10 22:08:32 +00:00
|
|
|
classID: Components.ID("{ec7a6c20-e081-11da-8ad9-0800200c9a66}")
|
2006-06-18 00:42:08 +00:00
|
|
|
};
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SessionStartup]);
|