From 4bfeba868df5926df90a4829f7383c54d3cba2c8 Mon Sep 17 00:00:00 2001 From: "dietrich%mozilla.com" Date: Sat, 2 Sep 2006 04:35:54 +0000 Subject: [PATCH] Bug 344860 [SessionStore] Minor code cleanup - for zeniko (r=dietrich) git-svn-id: svn://10.0.0.236/trunk@209054 18797224-902f-48f8-a5cc-f745e15eee43 --- .../sessionstore/src/nsSessionStartup.js | 44 ++++--------------- .../sessionstore/src/nsSessionStore.js | 41 +++++++---------- 2 files changed, 23 insertions(+), 62 deletions(-) diff --git a/mozilla/browser/components/sessionstore/src/nsSessionStartup.js b/mozilla/browser/components/sessionstore/src/nsSessionStartup.js index bec00075952..d5e6cbc706f 100644 --- a/mozilla/browser/components/sessionstore/src/nsSessionStartup.js +++ b/mozilla/browser/components/sessionstore/src/nsSessionStartup.js @@ -72,11 +72,6 @@ const CID = Components.ID("{ec7a6c20-e081-11da-8ad9-0800200c9a66}"); const CONTRACT_ID = "@mozilla.org/browser/sessionstartup;1"; const CLASS_NAME = "Browser Session Startup Service"; -const STATE_STOPPED = 0; -const STATE_RUNNING = 1; -const STATE_QUITTING = -1; - -const STATE_STOPPED_STR = "stopped"; const STATE_RUNNING_STR = "running"; /* :::::::: Pref Defaults :::::::::::::::::::: */ @@ -103,9 +98,6 @@ function SessionStartup() { SessionStartup.prototype = { - // set default load state - _loadState: STATE_STOPPED, - // the state to restore at startup _iniString: null, @@ -128,29 +120,23 @@ SessionStartup.prototype = { var dirService = Cc["@mozilla.org/file/directory_service;1"]. getService(Ci.nsIProperties); this._sessionFile = dirService.get("ProfD", Ci.nsILocalFile); - this._sessionFileBackup = this._sessionFile.clone(); this._sessionFile.append("sessionstore.js"); - this._sessionFileBackup.append("sessionstore.bak"); - + // only read the session file if config allows possibility of restoring var resumeFromCrash = this._getPref("sessionstore.resume_from_crash", DEFAULT_RESUME_FROM_CRASH); if (resumeFromCrash || this._doResumeSession()) { // get string containing session state - this._iniString = this._readFile(this._getSessionFile()); + this._iniString = this._readFile(this._sessionFile); if (this._iniString) { try { - // get uri for file path - var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); - var uri = ioService.newFileURI(this._sessionFile, null, null); - // parse the session state into JS objects - var s = new Components.utils.Sandbox(uri.spec); - this._initialState = Components.utils.evalInSandbox(this._iniString, s); + var s = new Components.utils.Sandbox("about:blank"); + var initialState = Components.utils.evalInSandbox(this._iniString, s); // set bool detecting crash this._lastSessionCrashed = - this._initialState.session && this._initialState.session.state && - this._initialState.session.state == STATE_RUNNING_STR; + initialState.session && initialState.session.state && + initialState.session.state == STATE_RUNNING_STR; // invalid .INI file - nothing can be restored } catch (ex) { debug("The session file is invalid: " + ex); } @@ -159,8 +145,7 @@ SessionStartup.prototype = { // prompt and check prefs this._doRestore = this._lastSessionCrashed ? this._doRecoverSession() : this._doResumeSession(); - if (this._initialState && !this._doRestore) { - delete this._initialState; // delete state + if (this._iniString && !this._doRestore) { this._iniString = null; // reset the state string } if (this._getPref("sessionstore.resume_session_once", DEFAULT_RESUME_SESSION_ONCE)) { @@ -182,9 +167,6 @@ SessionStartup.prototype = { var observerService = Cc["@mozilla.org/observer-service;1"]. getService(Ci.nsIObserverService); - // for event listeners - var _this = this; - switch (aTopic) { case "app-startup": observerService.addObserver(this, "final-ui-startup", true); @@ -241,16 +223,6 @@ SessionStartup.prototype = { return this._doRestore && this._iniString != null; }, -/* ........ Disk Access .............. */ - - /** - * get session datafile (or its backup) - * @returns nsIFile - */ - _getSessionFile: function sss_getSessionFile(aBackup) { - return aBackup ? this._sessionFileBackup : this._sessionFile; - }, - /* ........ Auxiliary Functions .............. */ /** @@ -275,7 +247,7 @@ SessionStartup.prototype = { // if the prompt fails, recover anyway var recover = true; // allow extensions to hook in a more elaborate restore prompt - //zeniko: drop this when we're using our own dialog instead of a standard prompt + // XXXzeniko drop this when we're using our own dialog instead of a standard prompt var dialogURI = this._getPref("sessionstore.restore_prompt_uri"); try { diff --git a/mozilla/browser/components/sessionstore/src/nsSessionStore.js b/mozilla/browser/components/sessionstore/src/nsSessionStore.js index e53682189d7..f6a96522eca 100644 --- a/mozilla/browser/components/sessionstore/src/nsSessionStore.js +++ b/mozilla/browser/components/sessionstore/src/nsSessionStore.js @@ -42,9 +42,10 @@ * Overview * This service keeps track of a user's session, storing the various bits * required to return the browser to it's current state. The relevant data is - * stored in memory, and is periodically saved to disk in an ini file in the - * profile directory. The service is started at first window load, in delayedStartup, and will restore - * the session from the data received from the nsSessionStartup service. + * stored in memory, and is periodically saved to disk in a file in the + * profile directory. The service is started at first window load, in + * delayedStartup, and will restore the session from the data received from + * the nsSessionStartup service. */ /* :::::::: Constants and Helpers ::::::::::::::: */ @@ -229,13 +230,13 @@ SessionStoreService.prototype = { }, this); delete this._initialState.windows[0].hidden; } - catch (ex) { debug("The session file is invalid: " + ex); } // invalid .INI file - nothing can be restored + catch (ex) { debug("The session file is invalid: " + ex); } } // if last session crashed, backup the session if (this._lastSessionCrashed) { try { - this._writeFile(this._getSessionFile(true), iniString); + this._writeFile(this._sessionFileBackup, iniString); } catch (ex) { } // nothing else we can do here } @@ -1145,7 +1146,7 @@ SessionStoreService.prototype = { * @param aWindow * Window reference * @param aState - * Ini formatted string, or object + * JS object or its eval'able source * @param aOverwriteTabs * bool overwrite existing tabs w/ new ones */ @@ -1682,7 +1683,7 @@ SessionStoreService.prototype = { this._dirty = aUpdateAll; var oState = this._getCurrentState(); oState.session = { state: ((this._loadState == STATE_RUNNING) ? STATE_RUNNING_STR : STATE_STOPPED_STR) }; - this._writeFile(this._getSessionFile(), oState.toSource()); + this._writeFile(this._sessionFile, oState.toSource()); this._lastSaveTime = Date.now(); }, @@ -1690,30 +1691,18 @@ SessionStoreService.prototype = { * delete session datafile and backup */ _clearDisk: function sss_clearDisk() { - var file = this._getSessionFile(); - - if (file.exists()) { + if (this._sessionFile.exists()) { try { - file.remove(false); + this._sessionFile.remove(false); } catch (ex) { dump(ex + '\n'); } // couldn't remove the file - what now? } - - if (!this._lastSessionCrashed) - return; - - try { - this._getSessionFile(true).remove(false); + if (this._sessionFileBackup.exists()) { + try { + this._sessionFileBackup.remove(false); + } + catch (ex) { dump(ex + '\n'); } // couldn't remove the file - what now? } - catch (ex) { dump(ex + '\n'); } // couldn't remove the file - what now? - }, - - /** - * get session datafile (or its backup) - * @returns nsIFile - */ - _getSessionFile: function sss_getSessionFile(aBackup) { - return aBackup ? this._sessionFileBackup : this._sessionFile; }, /* ........ Auxiliary Functions .............. */