Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Observe application on shutdown

123 views
Skip to first unread message

Ivan Enderlin @ Hoa

unread,
Mar 11, 2012, 2:47:29 PM3/11/12
to dev-platform
Hi there :-),

I am currently working with Gecko SDK/XULRunner 10.* and I am trying to
observe application on shutdown thanks to nsIObserverService. Well,
according to [1], it is possible to observe the topic
“quit-application*” but none of them seems to work with me (and it is a
little bit frustrating ;-)). Here is the code I use:

var applicationQuitObserver = {

get observerService ( ) {

return Cc['@mozilla.org/observer-service;1'].getService(
Ci.nsIObserverService
);
},

observe: function ( subject, topic, data ) {

dump('Here?');
// Just as an example.
},

register: function ( ) {

dump('Register');
this.observerService.addObserver(this, 'quit-application-granted',
false);
},

unregister: function ( ) {

this.observerService.removeObserver(this,
'quit-application-granted', false);
}
};
applicationQuitObserver.register();


And when I close the window with the “close button” or with the
nsIAppStartup [2], the dump “Here?” does not appear in console whereas
the dump “Register” appears as expected. If you want to try, you can
start with the sample-app [3] proposed by Mark Finkle with the simple
following prefs.js:

pref("toolkit.defaultChromeURI", "chrome://myapp/content/main.xul");
pref("browser.dom.window.dump.enabled", true);
pref("javascript.options.showInConsole", true);
pref("javascript.options.strict", true);
pref("nglayout.debug.disable_xul_cache", true);
pref("nglayout.debug.disable_xul_fastload", true);


Immediatly, I thought the I/O was closed to early for me. So I have
tried to open a new file and writing in it. It works when I was
observing the topic “xul-window-visible” (for a test) but still not with
“quit-application*”.

So. Any thoughts?
Cheers.

[1] <https://developer.mozilla.org/en/Observer_Notifications>
[2]
<https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIAppStartup>
[3] <https://developer.mozilla.org/en/Getting_started_with_XULRunner>

--
Ivan Enderlin
Developer of Hoa
http://hoa.42/ or http://hoa-project.net/

PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis)
http://lifc.univ-fcomte.fr/ and http://www.inria.fr/

Member of HTML and WebApps Working Group of W3C
http://w3.org/


Benjamin Smedberg

unread,
Mar 12, 2012, 2:28:27 PM3/12/12
to ivan.e...@hoa-project.net, dev-platform
On 3/11/2012 2:47 PM, Ivan Enderlin @ Hoa wrote:
> I am currently working with Gecko SDK/XULRunner 10.* and I am trying
> to observe application on shutdown thanks to nsIObserverService. Well,
> according to [1], it is possible to observe the topic
> “quit-application*” but none of them seems to work with me (and it is
> a little bit frustrating ;-)). Here is the code I use:
What are you actually trying to accomplish? If you have unsaved user
data, you should be observing profile-before-change and persisting the
data then.

The quit-application-requested notification is fired in the following cases:

* when the user is logging out of Windows
* when the user chooses "quit" from the mac application menu
* when the addons service or update services wishes to restart the
application
* when gnome on Linux tells Firefox that the desktop is logging out
* when something in the XUL code calls "goQuitApplication" on
globalOverlay.js (which is used by the "Quit Firefox" menu item
* when something calls "closeWindow" on globalOverlay.js (which is used
by the Firefox frontend)

But as you'll note, if you have just written your own XUL app and close
a window, the application will quit without any of those conditions
being triggered, so no quit-application-requested will ever be fired.
Quit-application-granted is fired by the app runner, so it should be
fired in almost all shutdown cases:
http://mxr.mozilla.org/mozilla-central/source/toolkit/components/startup/nsAppStartup.cpp#388

In any case, I'm not sure who maintains that Devmo page, and it looks
pretty woeful: I would recommend using
https://wiki.mozilla.org/XPCOM_Shutdown instead.

--BDS

Ivan Enderlin @ Hoa

unread,
Mar 13, 2012, 3:12:04 AM3/13/12
to Benjamin Smedberg, dev-platform
Hi Benjamin :-),

On 12/03/12 19:28, Benjamin Smedberg wrote:
> On 3/11/2012 2:47 PM, Ivan Enderlin @ Hoa wrote:
>> I am currently working with Gecko SDK/XULRunner 10.* and I am trying
>> to observe application on shutdown thanks to nsIObserverService.
>> Well, according to [1], it is possible to observe the topic
>> “quit-application*” but none of them seems to work with me (and it is
>> a little bit frustrating ;-)). Here is the code I use:
> What are you actually trying to accomplish? If you have unsaved user
> data, you should be observing profile-before-change and persisting the
> data then.
>
> The quit-application-requested notification is fired in the following
> cases:
>
> * when the user is logging out of Windows
> * when the user chooses "quit" from the mac application menu
> * when the addons service or update services wishes to restart the
> application
> * when gnome on Linux tells Firefox that the desktop is logging out
> * when something in the XUL code calls "goQuitApplication" on
> globalOverlay.js (which is used by the "Quit Firefox" menu item
> * when something calls "closeWindow" on globalOverlay.js (which is
> used by the Firefox frontend)
Hmm, interesting. I didn't notice the last one.

> But as you'll note, if you have just written your own XUL app and
> close a window, the application will quit without any of those
> conditions being triggered, so no quit-application-requested will ever
> be fired. Quit-application-granted is fired by the app runner, so it
> should be fired in almost all shutdown cases:
> http://mxr.mozilla.org/mozilla-central/source/toolkit/components/startup/nsAppStartup.cpp#388
Yup.

> In any case, I'm not sure who maintains that Devmo page, and it looks
> pretty woeful: I would recommend using
> https://wiki.mozilla.org/XPCOM_Shutdown instead.
Actually, I have succeeded yesterday by writing an XPCOM component and
by listening the topic “quit-application-granted”. Your last link is
very interesting. Why these informations do not figure in the MDN ?

Thank you :-).
Best regards.
0 new messages