diff --git a/browser/fuel/test/Makefile.in b/browser/fuel/test/Makefile.in index 114c2a86c53..d7f9345bf7f 100644 --- a/browser/fuel/test/Makefile.in +++ b/browser/fuel/test/Makefile.in @@ -47,6 +47,7 @@ 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 \ diff --git a/browser/fuel/test/browser_ApplicationQuitting.js b/browser/fuel/test/browser_ApplicationQuitting.js new file mode 100644 index 00000000000..3c0251715c4 --- /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); +} diff --git a/toolkit/components/exthelper/extApplication.js b/toolkit/components/exthelper/extApplication.js index 700f9a88bbb..dceda1bd09a 100644 --- a/toolkit/components/exthelper/extApplication.js +++ b/toolkit/components/exthelper/extApplication.js @@ -685,5 +685,30 @@ extApplication.prototype = { 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]) }; diff --git a/toolkit/components/exthelper/extIApplication.idl b/toolkit/components/exthelper/extIApplication.idl index 02d41f81d39..c0066f83532 100644 --- a/toolkit/components/exthelper/extIApplication.idl +++ b/toolkit/components/exthelper/extIApplication.idl @@ -382,7 +382,7 @@ interface extISessionStorage : nsISupports 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 { /** @@ -427,4 +427,16 @@ interface extIApplication : nsISupports * 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(); };