Created
February 10, 2010 23:12
-
-
Save chetan/300959 to your computer and use it in GitHub Desktop.
firefox shutdown hook example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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