Skip to content

Instantly share code, notes, and snippets.

@chetan
Created February 10, 2010 23:12
Show Gist options
  • Save chetan/300959 to your computer and use it in GitHub Desktop.
Save chetan/300959 to your computer and use it in GitHub Desktop.
firefox shutdown hook example
var ShutdownHook = {
prefs: PrefsService, // wrapper around XPCOM preferences API
register: function() {
if (this.prefs.shutdownRegistered) {
return;
}
var observerService =
Components.classes["@mozilla.org/observer-service;1"]
.getService (Components.interfaces.nsIObserverService);
observerService.addObserver(this, "quit-application-granted", false);
this.prefs.shutdownRegistered = true;
this.prefs.set("shutdownRegistered", true);
},
observe: function(subject, topic, data) {
if (topic != "quit-application-granted" ) {
return;
}
// do something at shutdown here...
// ... code ...
// ... code ...
// reset var so it can be hooked again the next time
// firefox is launched
this.prefs.set("shutdownRegistered", false);
},
unregister: function() {
try {
if (!this.prefs.shutdownRegistered) {
return;
}
var observerService =
Components.classes["@mozilla.org/observer-service;1"]
.getService (Components.interfaces.nsIObserverService);
observerService.removeObserver(this, "quit-application-granted");
this.prefs.shutdownRegistered = false;
this.prefs.set("shutdownRegistered", false);
} catch (err) {
}
},
};
ShutdownHook.register();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment