2007-09-18 16:36:17 +00:00
|
|
|
/*
|
|
|
|
# ***** BEGIN LICENSE BLOCK *****
|
|
|
|
# * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
# *
|
|
|
|
# * The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
# * 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
# * the License. You may obtain a copy of the License at
|
|
|
|
# * http://www.mozilla.org/MPL/
|
|
|
|
# *
|
|
|
|
# * Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
# * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
# * for the specific language governing rights and limitations under the
|
|
|
|
# * License.
|
|
|
|
# *
|
|
|
|
# * The Original Code is the nsSessionStore component.
|
|
|
|
# *
|
|
|
|
# * The Initial Developer of the Original Code is
|
|
|
|
# * Simon Bünzli <zeniko@gmail.com>
|
|
|
|
# * Portions created by the Initial Developer are Copyright (C) 2006
|
|
|
|
# * the Initial Developer. All Rights Reserved.
|
|
|
|
# *
|
|
|
|
# * Contributor(s):
|
|
|
|
# * Dietrich Ayala <autonome@gmail.com>
|
|
|
|
# *
|
|
|
|
# * Alternatively, the contents of this file may be used under the terms of
|
|
|
|
# * either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
# * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
# * in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
# * of those above. If you wish to allow use of your version of this file only
|
|
|
|
# * under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
# * use your version of this file under the terms of the MPL, indicate your
|
|
|
|
# * decision by deleting the provisions above and replace them with the notice
|
|
|
|
# * and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
# * the provisions above, a recipient may use your version of this file under
|
|
|
|
# * the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
# *
|
|
|
|
# * ***** END LICENSE BLOCK *****
|
|
|
|
*/
|
2006-06-18 00:42:08 +00:00
|
|
|
|
|
|
|
/**
|
2007-09-18 16:36:17 +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.
|
|
|
|
# *
|
|
|
|
# * Crash Detection
|
|
|
|
# * The session file stores a session.state property, that
|
|
|
|
# * indicates whether the browser is currently running. When the browser shuts
|
|
|
|
# * down, the field is changed to "stopped". At startup, this field is read, and
|
|
|
|
# * if it's value is "running", then it's assumed that the browser had previously
|
|
|
|
# * crashed, or at the very least that something bad happened, and that we should
|
|
|
|
# * restore the session.
|
|
|
|
# *
|
|
|
|
# * 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;
|
2007-09-18 16:36:17 +00:00
|
|
|
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
|
2006-06-18 00:42:08 +00:00
|
|
|
|
|
|
|
const STATE_RUNNING_STR = "running";
|
|
|
|
|
|
|
|
function debug(aMsg) {
|
|
|
|
aMsg = ("SessionStartup: " + aMsg).replace(/\S{80}/g, "$&\n");
|
|
|
|
Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService)
|
|
|
|
.logStringMessage(aMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* :::::::: The Service ::::::::::::::: */
|
|
|
|
|
|
|
|
function SessionStartup() {
|
|
|
|
}
|
|
|
|
|
|
|
|
SessionStartup.prototype = {
|
|
|
|
|
2006-08-03 19:19:03 +00:00
|
|
|
// the state to restore at startup
|
|
|
|
_iniString: null,
|
2008-02-14 21:53:33 +00:00
|
|
|
_sessionType: Ci.nsISessionStartup.NO_SESSION,
|
2006-08-03 19:19:03 +00:00
|
|
|
|
2006-06-18 00:42:08 +00:00
|
|
|
/* ........ Global Event Handlers .............. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the component
|
|
|
|
*/
|
|
|
|
init: function sss_init() {
|
|
|
|
this._prefBranch = Cc["@mozilla.org/preferences-service;1"].
|
2007-01-05 17:09:56 +00:00
|
|
|
getService(Ci.nsIPrefService).getBranch("browser.");
|
2006-06-18 00:42:08 +00:00
|
|
|
|
|
|
|
// get file references
|
|
|
|
var dirService = Cc["@mozilla.org/file/directory_service;1"].
|
|
|
|
getService(Ci.nsIProperties);
|
2008-09-07 12:33:26 +00:00
|
|
|
let sessionFile = dirService.get("ProfD", Ci.nsILocalFile);
|
|
|
|
sessionFile.append("sessionstore.js");
|
|
|
|
|
|
|
|
let doResumeSession = this._prefBranch.getBoolPref("sessionstore.resume_session_once") ||
|
|
|
|
this._prefBranch.getIntPref("startup.page") == 3;
|
2006-09-02 04:35:54 +00:00
|
|
|
|
2006-06-18 00:42:08 +00:00
|
|
|
// only read the session file if config allows possibility of restoring
|
2007-01-05 17:09:56 +00:00
|
|
|
var resumeFromCrash = this._prefBranch.getBoolPref("sessionstore.resume_from_crash");
|
2008-09-07 12:33:26 +00:00
|
|
|
if (!resumeFromCrash && !doResumeSession || !sessionFile.exists())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// get string containing session state
|
|
|
|
this._iniString = this._readStateFile(sessionFile);
|
|
|
|
if (!this._iniString)
|
|
|
|
return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// parse the session state into JS objects
|
|
|
|
var s = new Components.utils.Sandbox("about:blank");
|
|
|
|
var initialState = Components.utils.evalInSandbox(this._iniString, s);
|
2006-06-18 00:42:08 +00:00
|
|
|
}
|
2008-09-07 12:33:26 +00:00
|
|
|
catch (ex) { debug("The session file is invalid: " + ex); }
|
|
|
|
|
|
|
|
let lastSessionCrashed =
|
|
|
|
initialState && initialState.session && initialState.session.state &&
|
|
|
|
initialState.session.state == STATE_RUNNING_STR;
|
|
|
|
|
|
|
|
// set the startup type
|
|
|
|
if (lastSessionCrashed && resumeFromCrash && this._doRecoverSession())
|
|
|
|
this._sessionType = Ci.nsISessionStartup.RECOVER_SESSION;
|
|
|
|
else if (!lastSessionCrashed && doResumeSession)
|
|
|
|
this._sessionType = Ci.nsISessionStartup.RESUME_SESSION;
|
|
|
|
else
|
|
|
|
this._iniString = null; // reset the state string
|
2008-02-14 21:53:33 +00:00
|
|
|
|
|
|
|
if (this._sessionType != Ci.nsISessionStartup.NO_SESSION) {
|
2006-07-20 16:31:59 +00:00
|
|
|
// wait for the first browser window to open
|
|
|
|
var observerService = Cc["@mozilla.org/observer-service;1"].
|
|
|
|
getService(Ci.nsIObserverService);
|
|
|
|
observerService.addObserver(this, "domwindowopened", true);
|
|
|
|
}
|
2006-06-18 00:42:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle notifications
|
|
|
|
*/
|
|
|
|
observe: function sss_observe(aSubject, aTopic, aData) {
|
|
|
|
var observerService = Cc["@mozilla.org/observer-service;1"].
|
|
|
|
getService(Ci.nsIObserverService);
|
|
|
|
|
|
|
|
switch (aTopic) {
|
|
|
|
case "app-startup":
|
|
|
|
observerService.addObserver(this, "final-ui-startup", true);
|
2008-08-19 17:08:30 +00:00
|
|
|
observerService.addObserver(this, "quit-application", true);
|
2006-06-18 00:42:08 +00:00
|
|
|
break;
|
|
|
|
case "final-ui-startup":
|
|
|
|
observerService.removeObserver(this, "final-ui-startup");
|
2008-08-19 17:08:30 +00:00
|
|
|
observerService.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)
|
2008-08-19 17:08:30 +00:00
|
|
|
observerService.removeObserver(this, "final-ui-startup");
|
|
|
|
observerService.removeObserver(this, "quit-application");
|
|
|
|
break;
|
2006-07-20 16:31:59 +00:00
|
|
|
case "domwindowopened":
|
|
|
|
var window = aSubject;
|
|
|
|
var self = this;
|
|
|
|
window.addEventListener("load", function() {
|
|
|
|
self._onWindowOpened(window);
|
|
|
|
window.removeEventListener("load", arguments.callee, false);
|
|
|
|
}, false);
|
|
|
|
break;
|
2006-06-18 00:42:08 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2006-07-20 16:31:59 +00:00
|
|
|
/**
|
|
|
|
* Removes the default arguments from the first browser window
|
2007-03-26 14:22:14 +00:00
|
|
|
* (and removes the "domwindowopened" observer afterwards).
|
2006-07-20 16:31:59 +00:00
|
|
|
*/
|
|
|
|
_onWindowOpened: function sss_onWindowOpened(aWindow) {
|
|
|
|
var wType = aWindow.document.documentElement.getAttribute("windowtype");
|
|
|
|
if (wType != "navigator:browser")
|
|
|
|
return;
|
|
|
|
|
2007-03-26 14:22:14 +00:00
|
|
|
/**
|
|
|
|
* Note: this relies on the fact that nsBrowserContentHandler will return
|
2007-03-26 14:23:21 +00:00
|
|
|
* a different value the first time its getter is called after an update,
|
2007-03-26 14:22:14 +00:00
|
|
|
* due to its needHomePageOverride() logic. We don't want to remove the
|
|
|
|
* default arguments in the update case, since they include the "What's
|
|
|
|
* New" page.
|
|
|
|
*
|
|
|
|
* Since we're garanteed to be at least the second caller of defaultArgs
|
|
|
|
* (nsBrowserContentHandler calls it to determine which arguments to pass
|
|
|
|
* at startup), we know that if the window's arguments don't match the
|
|
|
|
* current defaultArguments, we're either in the update case, or we're
|
|
|
|
* launching a non-default browser window, so we shouldn't remove the
|
|
|
|
* window's arguments.
|
|
|
|
*/
|
2006-07-20 16:31:59 +00:00
|
|
|
var defaultArgs = Cc["@mozilla.org/browser/clh;1"].
|
|
|
|
getService(Ci.nsIBrowserHandler).defaultArgs;
|
|
|
|
if (aWindow.arguments && aWindow.arguments[0] &&
|
|
|
|
aWindow.arguments[0] == defaultArgs)
|
|
|
|
aWindow.arguments[0] = null;
|
|
|
|
|
|
|
|
var observerService = Cc["@mozilla.org/observer-service;1"].
|
|
|
|
getService(Ci.nsIObserverService);
|
|
|
|
observerService.removeObserver(this, "domwindowopened");
|
|
|
|
},
|
|
|
|
|
2006-06-18 00:42:08 +00:00
|
|
|
/* ........ Public API ................*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the session state as a string
|
|
|
|
*/
|
|
|
|
get state() {
|
|
|
|
return this._iniString;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2007-03-26 14:22:14 +00:00
|
|
|
* Determine whether there is a pending session restore.
|
2006-06-18 00:42:08 +00:00
|
|
|
* @returns bool
|
|
|
|
*/
|
|
|
|
doRestore: function sss_doRestore() {
|
2008-02-14 21:53:33 +00:00
|
|
|
return this._sessionType != Ci.nsISessionStartup.NO_SESSION;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the type of pending session store, if any.
|
|
|
|
*/
|
|
|
|
get sessionType() {
|
|
|
|
return this._sessionType;
|
2006-06-18 00:42:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/* ........ Auxiliary Functions .............. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* prompt user whether or not to restore the previous session,
|
|
|
|
* if the browser crashed
|
|
|
|
* @returns bool
|
|
|
|
*/
|
|
|
|
_doRecoverSession: function sss_doRecoverSession() {
|
|
|
|
// if the prompt fails, recover anyway
|
|
|
|
var recover = true;
|
2007-01-05 17:09:56 +00:00
|
|
|
|
2006-06-18 00:42:08 +00:00
|
|
|
// allow extensions to hook in a more elaborate restore prompt
|
2006-09-02 04:35:54 +00:00
|
|
|
// XXXzeniko drop this when we're using our own dialog instead of a standard prompt
|
2007-01-05 17:09:56 +00:00
|
|
|
var dialogURI = null;
|
|
|
|
try {
|
|
|
|
dialogURI = this._prefBranch.getCharPref("sessionstore.restore_prompt_uri");
|
|
|
|
}
|
|
|
|
catch (ex) { }
|
2006-06-18 00:42:08 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (dialogURI) { // extension provided dialog
|
|
|
|
var params = Cc["@mozilla.org/embedcomp/dialogparam;1"].
|
|
|
|
createInstance(Ci.nsIDialogParamBlock);
|
|
|
|
// default to recovering
|
|
|
|
params.SetInt(0, 0);
|
|
|
|
Cc["@mozilla.org/embedcomp/window-watcher;1"].
|
|
|
|
getService(Ci.nsIWindowWatcher).
|
|
|
|
openWindow(null, dialogURI, "_blank",
|
|
|
|
"chrome,modal,centerscreen,titlebar", params);
|
|
|
|
recover = params.GetInt(0) == 0;
|
|
|
|
}
|
|
|
|
else { // basic prompt with no options
|
|
|
|
// get app name from branding properties
|
2008-07-12 09:56:02 +00:00
|
|
|
const brandShortName = this._getStringBundle("chrome://branding/locale/brand.properties")
|
|
|
|
.GetStringFromName("brandShortName");
|
2006-06-18 00:42:08 +00:00
|
|
|
// create prompt strings
|
|
|
|
var ssStringBundle = this._getStringBundle("chrome://browser/locale/sessionstore.properties");
|
2006-07-06 03:03:01 +00:00
|
|
|
var restoreTitle = ssStringBundle.formatStringFromName("restoredTitle", [brandShortName], 1);
|
2006-08-01 21:52:42 +00:00
|
|
|
var restoreText = ssStringBundle.formatStringFromName("restoredMsg", [brandShortName], 1);
|
2007-08-12 14:03:26 +00:00
|
|
|
var okTitle = ssStringBundle.GetStringFromName("okTitle");
|
2006-07-01 22:09:18 +00:00
|
|
|
var cancelTitle = ssStringBundle.GetStringFromName("cancelTitle");
|
2006-06-18 00:42:08 +00:00
|
|
|
|
|
|
|
var promptService = Cc["@mozilla.org/embedcomp/prompt-service;1"].
|
|
|
|
getService(Ci.nsIPromptService);
|
|
|
|
// set the buttons that will appear on the dialog
|
2006-07-01 22:09:18 +00:00
|
|
|
var flags = promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_0 +
|
|
|
|
promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_1 +
|
|
|
|
promptService.BUTTON_POS_0_DEFAULT;
|
2006-06-18 00:42:08 +00:00
|
|
|
var buttonChoice = promptService.confirmEx(null, restoreTitle, restoreText,
|
2007-08-12 14:03:26 +00:00
|
|
|
flags, okTitle, cancelTitle, null,
|
2006-06-18 00:42:08 +00:00
|
|
|
null, {});
|
2006-07-01 22:09:18 +00:00
|
|
|
recover = (buttonChoice == 0);
|
2006-06-18 00:42:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ex) { dump(ex + "\n"); } // if the prompt fails, recover anyway
|
|
|
|
return recover;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience method to get localized string bundles
|
|
|
|
* @param aURI
|
|
|
|
* @returns nsIStringBundle
|
|
|
|
*/
|
|
|
|
_getStringBundle: function sss_getStringBundle(aURI) {
|
2008-07-12 09:56:02 +00:00
|
|
|
return Cc["@mozilla.org/intl/stringbundle;1"].
|
|
|
|
getService(Ci.nsIStringBundleService).createBundle(aURI);
|
2006-06-18 00:42:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/* ........ Storage API .............. */
|
|
|
|
|
2008-08-20 06:39:09 +00:00
|
|
|
/**
|
|
|
|
* Reads a session state file into a string and lets
|
|
|
|
* observers modify the state before it's being used
|
|
|
|
*
|
|
|
|
* @param aFile is any nsIFile
|
|
|
|
* @returns a session state string
|
|
|
|
*/
|
|
|
|
_readStateFile: function sss_readStateFile(aFile) {
|
|
|
|
var stateString = Cc["@mozilla.org/supports-string;1"].
|
|
|
|
createInstance(Ci.nsISupportsString);
|
|
|
|
stateString.data = this._readFile(aFile) || "";
|
|
|
|
|
|
|
|
var observerService = Cc["@mozilla.org/observer-service;1"].
|
|
|
|
getService(Ci.nsIObserverService);
|
|
|
|
observerService.notifyObservers(stateString, "sessionstore-state-read", "");
|
|
|
|
|
|
|
|
return stateString.data;
|
|
|
|
},
|
|
|
|
|
2006-06-18 00:42:08 +00:00
|
|
|
/**
|
|
|
|
* reads a file into a string
|
|
|
|
* @param aFile
|
|
|
|
* nsIFile
|
|
|
|
* @returns string
|
|
|
|
*/
|
|
|
|
_readFile: function sss_readFile(aFile) {
|
|
|
|
try {
|
|
|
|
var stream = Cc["@mozilla.org/network/file-input-stream;1"].
|
|
|
|
createInstance(Ci.nsIFileInputStream);
|
|
|
|
stream.init(aFile, 0x01, 0, 0);
|
|
|
|
var cvstream = Cc["@mozilla.org/intl/converter-input-stream;1"].
|
|
|
|
createInstance(Ci.nsIConverterInputStream);
|
|
|
|
cvstream.init(stream, "UTF-8", 1024, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
|
|
|
|
|
|
|
|
var content = "";
|
|
|
|
var data = {};
|
|
|
|
while (cvstream.readString(4096, data)) {
|
|
|
|
content += data.value;
|
|
|
|
}
|
|
|
|
cvstream.close();
|
|
|
|
|
|
|
|
return content.replace(/\r\n?/g, "\n");
|
|
|
|
}
|
2008-09-07 12:33:26 +00:00
|
|
|
catch (ex) { Components.utils.reportError(ex); }
|
2006-06-18 00:42:08 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2007-09-18 16:36:17 +00:00
|
|
|
/* ........ QueryInterface .............. */
|
|
|
|
QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference,
|
|
|
|
Ci.nsISessionStartup]),
|
|
|
|
classDescription: "Browser Session Startup Service",
|
|
|
|
classID: Components.ID("{ec7a6c20-e081-11da-8ad9-0800200c9a66}"),
|
|
|
|
contractID: "@mozilla.org/browser/sessionstartup;1",
|
2006-06-18 00:42:08 +00:00
|
|
|
|
2007-09-18 16:36:17 +00:00
|
|
|
// get this contractID registered for certain categories via XPCOMUtils
|
|
|
|
_xpcom_categories: [
|
|
|
|
// make ourselves a startup observer
|
|
|
|
{ category: "app-startup", service: true }
|
|
|
|
]
|
2006-06-18 00:42:08 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2008-09-07 12:33:26 +00:00
|
|
|
function NSGetModule(aCompMgr, aFileSpec)
|
|
|
|
XPCOMUtils.generateModule([SessionStartup]);
|