b=338039, p=zeniko, r=enndeakin. Provide easy, right way to restart an application.
authorSimon Bunzli <zeniko@gmail.com
Sat, 06 Sep 2008 23:30:13 -0500
changeset 18916 551b4e4c78135bb25e9304453bb61475a6724eb6
parent 18915 c989ea4a711965e63552a19bb66a5cef28c6e0bf
child 18917 49bbede5acd0c996ddb2c68efab03d5b60d22d3e
push idunknown
push userunknown
push dateunknown
reviewersenndeakin
bugs338039
milestone1.9.1b1pre
b=338039, p=zeniko, r=enndeakin. Provide easy, right way to restart an application.
browser/fuel/test/Makefile.in
browser/fuel/test/browser_ApplicationQuitting.js
toolkit/components/exthelper/extApplication.js
toolkit/components/exthelper/extIApplication.idl
--- a/browser/fuel/test/Makefile.in
+++ b/browser/fuel/test/Makefile.in
@@ -42,16 +42,17 @@ VPATH          = @srcdir@
 relativesrcdir = browser/fuel/test
 
 include $(DEPTH)/config/autoconf.mk
 include $(topsrcdir)/config/rules.mk
 
 _BROWSER_FILES =browser_Application.js \
 		browser_ApplicationPrefs.js \
 		browser_ApplicationStorage.js \
+		browser_ApplicationQuitting.js \
 		browser_Browser.js \
 		browser_Bookmarks.js \
 		ContentA.html \
 		ContentB.html \
 		ContentWithFrames.html \
 		$(NULL)
 
 libs::	$(_BROWSER_FILES)
new file mode 100644
--- /dev/null
+++ b/browser/fuel/test/browser_ApplicationQuitting.js
@@ -0,0 +1,21 @@
+function test() {
+  let quitRequestObserver = {
+    observe: function(aSubject, aTopic, aData) {
+      ok(aTopic == "quit-application-requested" &&
+         aSubject instanceof Components.interfaces.nsISupportsPRBool,
+         "Received a quit request we're going to deny");
+      aSubject.data = true;
+    }
+  };
+  
+  // ensure that we don't accidentally quit
+  let os = Components.classes["@mozilla.org/observer-service;1"]
+                     .getService(Components.interfaces.nsIObserverService);
+  os.addObserver(quitRequestObserver, "quit-application-requested", false);
+  
+  ok(!Application.quit(),    "Tried to quit - and didn't succeed");
+  ok(!Application.restart(), "Tried to restart - and didn't succeed");
+  
+  // clean up
+  os.removeObserver(quitRequestObserver, "quit-application-requested", false);
+}
--- a/toolkit/components/exthelper/extApplication.js
+++ b/toolkit/components/exthelper/extApplication.js
@@ -680,10 +680,35 @@ extApplication.prototype = {
 
   get events() {
     if (this._events == null)
         this._events = new Events();
 
     return this._events;
   },
 
+  // helper method for correct quitting/restarting
+  _quitWithFlags: function app__quitWithFlags(aFlags) {
+    let os = Components.classes["@mozilla.org/observer-service;1"]
+                       .getService(Components.interfaces.nsIObserverService);
+    let cancelQuit = Components.classes["@mozilla.org/supports-PRBool;1"]
+                               .createInstance(Components.interfaces.nsISupportsPRBool);
+    os.notifyObservers(cancelQuit, "quit-application-requested", null);
+    if (cancelQuit.data)
+      return false; // somebody canceled our quit request
+    
+    let appStartup = Components.classes['@mozilla.org/toolkit/app-startup;1']
+                               .getService(Components.interfaces.nsIAppStartup);
+    appStartup.quit(aFlags);
+    return true;
+  },
+
+  quit: function app_quit() {
+    return this._quitWithFlags(Components.interfaces.nsIAppStartup.eAttemptQuit);
+  },
+
+  restart: function app_restart() {
+    return this._quitWithFlags(Components.interfaces.nsIAppStartup.eAttemptQuit |
+                               Components.interfaces.nsIAppStartup.eRestart);
+  },
+
   QueryInterface : XPCOMUtils.generateQI([Ci.extIApplication, Ci.nsISupportsWeakReference])
 };
--- a/toolkit/components/exthelper/extIApplication.idl
+++ b/toolkit/components/exthelper/extIApplication.idl
@@ -377,17 +377,17 @@ interface extISessionStorage : nsISuppor
    * @param   aDefaultValue
    *          The value to return if no item exists with the given name
    * @returns value of the item or the given default value if no item
    *          exists with the given name.
    */
   nsIVariant get(in AString aName, in nsIVariant aDefaultValue);
 }; 
 
-[scriptable, uuid(ba9442ee-7070-44fb-8157-c111e1fa70b6)]
+[scriptable, uuid(e53d6610-7468-11dd-ad8b-0800200c9a66)]
 interface extIApplication : nsISupports
 {
   /**
    * The id of the application.
    */
   readonly attribute AString id;
 
   /**
@@ -422,9 +422,21 @@ interface extIApplication : nsISupports
    */
   readonly attribute extISessionStorage storage;
 
   /**
    * The events object for the application.
    * supports: "load", "ready", "quit", "unload"
    */
   readonly attribute extIEvents events;
+
+  /**
+   * Quits the application (if nobody objects to quit-application-requested).
+   * @returns whether quitting will proceed
+   */
+  boolean quit();
+
+  /**
+   * Restarts the application (if nobody objects to quit-application-requested).
+   * @returns whether restarting will proceed
+   */
+  boolean restart();
 };