home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / firefox / components / nsSessionStartup.js < prev    next >
Encoding:
Text File  |  2006-08-18  |  15.0 KB  |  446 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is the nsSessionStore component.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Simon B├╝nzli <zeniko@gmail.com>
  18.  *
  19.  * Portions created by the Initial Developer are Copyright (C) 2006
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  * Dietrich Ayala <autonome@gmail.com>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. /**
  40.  * Session Storage and Restoration
  41.  * 
  42.  * Overview
  43.  * This service user's session file at startup, and makes a determination as to
  44.  * whether the session should be restored. It fill restore the session under 
  45.  * the circumstances described below.
  46.  * 
  47.  * Crash Detection
  48.  * The initial segment of the INI file is has a single field, "state", that 
  49.  * indicates whether the browser is currently running. When the browser shuts 
  50.  * down, the field is changed to "stopped". At startup, this field is read, and
  51.  * if it's value is "running", then it's assumed that the browser had previously
  52.  * crashed, or at the very least that something bad happened, and that we should
  53.  * restore the session.
  54.  * 
  55.  * Forced Restarts
  56.  * In the event that a restart is required due to application update or extension
  57.  * installation, set the browser.sessionstore.resume_session_once pref to true,
  58.  * and the session will be restored the next time the browser starts.
  59.  * 
  60.  * Always Resume
  61.  * This service will always resume the session if the boolean pref 
  62.  * browser.sessionstore.resume_session is set to true.
  63.  */
  64.  
  65. /* :::::::: Constants and Helpers ::::::::::::::: */
  66.  
  67. const Cc = Components.classes;
  68. const Ci = Components.interfaces;
  69. const Cr = Components.results;
  70.  
  71. const CID = Components.ID("{ec7a6c20-e081-11da-8ad9-0800200c9a66}");
  72. const CONTRACT_ID = "@mozilla.org/browser/sessionstartup;1";
  73. const CLASS_NAME = "Browser Session Startup Service";
  74.  
  75. const STATE_STOPPED = 0;
  76. const STATE_RUNNING = 1;
  77. const STATE_QUITTING = -1;
  78.  
  79. const STATE_STOPPED_STR = "stopped";
  80. const STATE_RUNNING_STR = "running";
  81.  
  82. /* :::::::: Pref Defaults :::::::::::::::::::: */
  83.  
  84. // whether the service is enabled
  85. const DEFAULT_ENABLED = true;
  86.  
  87. // resume the current session at startup (otherwise just recover)
  88. const DEFAULT_RESUME_SESSION = false;
  89.  
  90. // resume the current session at startup just this once
  91. const DEFAULT_RESUME_SESSION_ONCE = false;
  92.  
  93. // resume the current session at startup if it had previously crashed
  94. const DEFAULT_RESUME_FROM_CRASH = true;
  95.  
  96. function debug(aMsg) {
  97.   aMsg = ("SessionStartup: " + aMsg).replace(/\S{80}/g, "$&\n");
  98.   Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService)
  99.                                      .logStringMessage(aMsg);
  100. }
  101.  
  102. /* :::::::: The Service ::::::::::::::: */
  103.  
  104. function SessionStartup() {
  105. }
  106.  
  107. SessionStartup.prototype = {
  108.  
  109.   // set default load state
  110.   _loadState: STATE_STOPPED,
  111.  
  112. /* ........ Global Event Handlers .............. */
  113.  
  114.   /**
  115.    * Initialize the component
  116.    */
  117.   init: function sss_init() {
  118.     this._prefBranch = Cc["@mozilla.org/preferences-service;1"].
  119.                        getService(Ci.nsIPrefService).
  120.                        getBranch("browser.");
  121.     this._prefBranch.QueryInterface(Ci.nsIPrefBranch2);
  122.  
  123.     // if the service is disabled, do not init 
  124.     if (!this._getPref("sessionstore.enabled", DEFAULT_ENABLED))
  125.       return;
  126.  
  127.     var observerService = Cc["@mozilla.org/observer-service;1"].
  128.                           getService(Ci.nsIObserverService);
  129.     
  130.     // get file references
  131.     var dirService = Cc["@mozilla.org/file/directory_service;1"].
  132.                      getService(Ci.nsIProperties);
  133.     this._sessionFile = dirService.get("ProfD", Ci.nsILocalFile);
  134.     this._sessionFileBackup = this._sessionFile.clone();
  135.     this._sessionFile.append("sessionstore.js");
  136.     this._sessionFileBackup.append("sessionstore.bak");
  137.    
  138.     // only read the session file if config allows possibility of restoring
  139.     var resumeFromCrash = this._getPref("sessionstore.resume_from_crash", DEFAULT_RESUME_FROM_CRASH);
  140.     if (resumeFromCrash || this._doResumeSession()) {
  141.       // get string containing session state
  142.       this._iniString = this._readFile(this._getSessionFile());
  143.       if (this._iniString) {
  144.         try {
  145.           // get uri for file path
  146.           var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
  147.           var uri = ioService.newFileURI(this._sessionFile, null, null);
  148.  
  149.           // parse the session state into JS objects
  150.           var s = new Components.utils.Sandbox(uri.spec);
  151.           this._initialState = Components.utils.evalInSandbox(this._iniString, s);
  152.  
  153.           // set bool detecting crash
  154.           this._lastSessionCrashed =
  155.             this._initialState.session && this._initialState.session.state &&
  156.             this._initialState.session.state == STATE_RUNNING_STR;
  157.         // invalid .INI file - nothing can be restored
  158.         }
  159.         catch (ex) { debug("The session file is invalid: " + ex); } 
  160.       }
  161.     }
  162.     
  163.     // prompt and check prefs
  164.     this._doRestore = this._lastSessionCrashed ? this._doRecoverSession() : this._doResumeSession();
  165.     if (this._initialState && !this._doRestore) {
  166.       delete this._iniString; // delete state string
  167.       delete this._initialState; // delete state
  168.     }
  169.     if (this._getPref("sessionstore.resume_session_once", DEFAULT_RESUME_SESSION_ONCE)) {
  170.       this._prefBranch.setBoolPref("sessionstore.resume_session_once", false);
  171.     }
  172.   },
  173.  
  174.   /**
  175.    * Handle notifications
  176.    */
  177.   observe: function sss_observe(aSubject, aTopic, aData) {
  178.     var observerService = Cc["@mozilla.org/observer-service;1"].
  179.                           getService(Ci.nsIObserverService);
  180.  
  181.     // for event listeners
  182.     var _this = this;
  183.  
  184.     switch (aTopic) {
  185.     case "app-startup": 
  186.       observerService.addObserver(this, "final-ui-startup", true);
  187.       break;
  188.     case "final-ui-startup": 
  189.       observerService.removeObserver(this, "final-ui-startup");
  190.       this.init();
  191.       break;
  192.     }
  193.   },
  194.  
  195. /* ........ Public API ................*/
  196.  
  197.   /**
  198.    * Get the session state as a string
  199.    */
  200.   get state() {
  201.     return this._iniString;
  202.   },
  203.  
  204.   /**
  205.    * Determine if session should be restored
  206.    * @returns bool
  207.    */
  208.   doRestore: function sss_doRestore() {
  209.     return this._doRestore;
  210.   },
  211.  
  212. /* ........ Disk Access .............. */
  213.  
  214.   /**
  215.    * get session datafile (or its backup)
  216.    * @returns nsIFile 
  217.    */
  218.   _getSessionFile: function sss_getSessionFile(aBackup) {
  219.     return aBackup ? this._sessionFileBackup : this._sessionFile;
  220.   },
  221.  
  222. /* ........ Auxiliary Functions .............. */
  223.  
  224.   /**
  225.    * Whether or not to resume session, if not recovering from a crash
  226.    * Returns true if:
  227.    * - pref is set to always resume sessions
  228.    * - pref is set to resume session once
  229.    * - user configured startup page to be the last-visited page
  230.    * @returns bool
  231.    */
  232.   _doResumeSession: function sss_doResumeSession() {
  233.     return this._getPref("sessionstore.resume_session", DEFAULT_RESUME_SESSION) || 
  234.       this._getPref("sessionstore.resume_session_once", DEFAULT_RESUME_SESSION_ONCE) || 
  235.       this._getPref("startup.page", 1) == 2;
  236.   },
  237.  
  238.   /**
  239.    * prompt user whether or not to restore the previous session,
  240.    * if the browser crashed
  241.    * @returns bool
  242.    */
  243.   _doRecoverSession: function sss_doRecoverSession() {
  244.     // do not prompt or resume, post-crash
  245.     if (!this._getPref("sessionstore.resume_from_crash", DEFAULT_RESUME_FROM_CRASH))
  246.       return false;
  247.  
  248.     // if the prompt fails, recover anyway
  249.     var recover = true;
  250.     // allow extensions to hook in a more elaborate restore prompt
  251.     //zeniko: drop this when we're using our own dialog instead of a standard prompt
  252.     var dialogURI = this._getPref("sessionstore.restore_prompt_uri");
  253.     
  254.     try {
  255.       if (dialogURI) { // extension provided dialog 
  256.         var params = Cc["@mozilla.org/embedcomp/dialogparam;1"].
  257.                      createInstance(Ci.nsIDialogParamBlock);
  258.         // default to recovering
  259.         params.SetInt(0, 0);
  260.         Cc["@mozilla.org/embedcomp/window-watcher;1"].
  261.         getService(Ci.nsIWindowWatcher).
  262.         openWindow(null, dialogURI, "_blank", 
  263.                    "chrome,modal,centerscreen,titlebar", params);
  264.         recover = params.GetInt(0) == 0;
  265.       }
  266.       else { // basic prompt with no options
  267.         // get app name from branding properties
  268.         var brandStringBundle = this._getStringBundle("chrome://branding/locale/brand.properties");
  269.         var brandShortName = brandStringBundle.GetStringFromName("brandShortName");
  270.  
  271.         // create prompt strings
  272.         var ssStringBundle = this._getStringBundle("chrome://browser/locale/sessionstore.properties");
  273.         var restoreTitle = ssStringBundle.formatStringFromName("restoredTitle", [brandShortName], 1);
  274.         var restoreText = ssStringBundle.formatStringFromName("restoredText", [brandShortName], 1);
  275.         var buttonTitle = ssStringBundle.GetStringFromName("buttonTitle");
  276.         var cancelTitle = ssStringBundle.GetStringFromName("cancelTitle");
  277.  
  278.         var promptService = Cc["@mozilla.org/embedcomp/prompt-service;1"].
  279.                             getService(Ci.nsIPromptService);
  280.  
  281.         // set the buttons that will appear on the dialog
  282.         var flags = promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_0 +
  283.                     promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_1 +
  284.                     promptService.BUTTON_POS_0_DEFAULT;
  285.         
  286.         var buttonChoice = promptService.confirmEx(null, restoreTitle, restoreText, 
  287.                                           flags, buttonTitle, cancelTitle, null, 
  288.                                           null, {});
  289.         recover = (buttonChoice == 0);
  290.       }
  291.     }
  292.     catch (ex) { dump(ex + "\n"); } // if the prompt fails, recover anyway
  293.     return recover;
  294.   },
  295.  
  296.   /**
  297.    * Convenience method to get localized string bundles
  298.    * @param aURI
  299.    * @returns nsIStringBundle
  300.    */
  301.   _getStringBundle: function sss_getStringBundle(aURI) {
  302.     var bundleService = Cc["@mozilla.org/intl/stringbundle;1"].
  303.                         getService(Ci.nsIStringBundleService);
  304.     var appLocale = Cc["@mozilla.org/intl/nslocaleservice;1"].
  305.                     getService(Ci.nsILocaleService).getApplicationLocale();
  306.     return bundleService.createBundle(aURI, appLocale);
  307.   },
  308.  
  309. /* ........ Storage API .............. */
  310.  
  311.   /**
  312.    * basic pref reader
  313.    * @param aName
  314.    * @param aDefault
  315.    * @param aUseRootBranch
  316.    */
  317.   _getPref: function sss_getPref(aName, aDefault) {
  318.     var pb = this._prefBranch;
  319.     try {
  320.       switch (pb.getPrefType(aName)) {
  321.       case pb.PREF_STRING:
  322.         return pb.getCharPref(aName);
  323.       case pb.PREF_BOOL:
  324.         return pb.getBoolPref(aName);
  325.       case pb.PREF_INT:
  326.         return pb.getIntPref(aName);
  327.       default:
  328.         return aDefault;
  329.       }
  330.     }
  331.     catch(ex) {
  332.       return aDefault;
  333.     }
  334.   },
  335.  
  336.   /**
  337.    * reads a file into a string
  338.    * @param aFile
  339.    *        nsIFile
  340.    * @returns string
  341.    */
  342.   _readFile: function sss_readFile(aFile) {
  343.     try {
  344.       var stream = Cc["@mozilla.org/network/file-input-stream;1"].
  345.                    createInstance(Ci.nsIFileInputStream);
  346.       stream.init(aFile, 0x01, 0, 0);
  347.       var cvstream = Cc["@mozilla.org/intl/converter-input-stream;1"].
  348.                      createInstance(Ci.nsIConverterInputStream);
  349.       cvstream.init(stream, "UTF-8", 1024, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
  350.       
  351.       var content = "";
  352.       var data = {};
  353.       while (cvstream.readString(4096, data)) {
  354.         content += data.value;
  355.       }
  356.       cvstream.close();
  357.       
  358.       return content.replace(/\r\n?/g, "\n");
  359.     }
  360.     catch (ex) { } // inexisting file?
  361.     
  362.     return null;
  363.   },
  364.  
  365. /* ........ QueryInterface .............. */
  366.  
  367.   QueryInterface: function(aIID) {
  368.     if (!aIID.equals(Ci.nsISupports) && !aIID.equals(Ci.nsIObserver) && 
  369.       !aIID.equals(Ci.nsISupportsWeakReference) && 
  370.       !aIID.equals(Ci.nsISessionStartup)) {
  371.       Components.returnCode = Cr.NS_ERROR_NO_INTERFACE;
  372.       return null;
  373.     }
  374.     
  375.     return this;
  376.   }
  377. };
  378.  
  379. /* :::::::: Service Registration & Initialization ::::::::::::::: */
  380.  
  381. /* ........ nsIModule .............. */
  382.  
  383. const SessionStartupModule = {
  384.  
  385.   getClassObject: function(aCompMgr, aCID, aIID) {
  386.     if (aCID.equals(CID)) {
  387.       return SessionStartupFactory;
  388.     }
  389.     
  390.     Components.returnCode = Cr.NS_ERROR_NOT_REGISTERED;
  391.     return null;
  392.   },
  393.  
  394.   registerSelf: function(aCompMgr, aFileSpec, aLocation, aType) {
  395.     aCompMgr.QueryInterface(Ci.nsIComponentRegistrar);
  396.     aCompMgr.registerFactoryLocation(CID, CLASS_NAME, CONTRACT_ID, aFileSpec, aLocation, aType);
  397.  
  398.     var catMan = Cc["@mozilla.org/categorymanager;1"].
  399.                  getService(Ci.nsICategoryManager);
  400.     catMan.addCategoryEntry("app-startup", CLASS_NAME, "service," + CONTRACT_ID, true, true);
  401.   },
  402.  
  403.   unregisterSelf: function(aCompMgr, aLocation, aType) {
  404.     aCompMgr.QueryInterface(Ci.nsIComponentRegistrar);
  405.     aCompMgr.unregisterFactoryLocation(CID, aLocation);
  406.  
  407.     var catMan = Cc["@mozilla.org/categorymanager;1"].
  408.                  getService(Ci.nsICategoryManager);
  409.     catMan.deleteCategoryEntry( "app-startup", "service," + CONTRACT_ID, true);
  410.   },
  411.  
  412.   canUnload: function(aCompMgr) {
  413.     return true;
  414.   }
  415. }
  416.  
  417. /* ........ nsIFactory .............. */
  418.  
  419. const SessionStartupFactory = {
  420.  
  421.   createInstance: function(aOuter, aIID) {
  422.     if (aOuter != null) {
  423.       Components.returnCode = Cr.NS_ERROR_NO_AGGREGATION;
  424.       return null;
  425.     }
  426.     
  427.     return (new SessionStartup()).QueryInterface(aIID);
  428.   },
  429.  
  430.   lockFactory: function(aLock) { },
  431.  
  432.   QueryInterface: function(aIID) {
  433.     if (!aIID.equals(Ci.nsISupports) && !aIID.equals(Ci.nsIModule) &&
  434.         !aIID.equals(Ci.nsIFactory) && !aIID.equals(Ci.nsISessionStartup)) {
  435.       Components.returnCode = Cr.NS_ERROR_NO_INTERFACE;
  436.       return null;
  437.     }
  438.     
  439.     return this;
  440.   }
  441. };
  442.  
  443. function NSGetModule(aComMgr, aFileSpec) {
  444.   return SessionStartupModule;
  445. }
  446.