User Tools

Site Tools


developer_center:recipe_book:extensions_and_core:uninstall

Uninstall

Cleaning up after yourself via uninstall is a good thing, especially if you do things like add servicepane nodes, etc. Two add-ons that do this are the Concerts & SHOUTcast add-ons. Uninstallation actually triggers two events, one when the user clicks “Uninstall” in the Add-ons. This first event is “em-action-requested”, and is cancellable. The second event is when the application is actually shutdown.

What we generally do is, when the first event happens is to set a boolean indicating the user wants to uninstall our add-on. At application shutdown, we check that boolean to see if it's true. If it is, then we cleanup after ourselves. One thing to be aware of is that XPCOM can become unavailable during shutdown, so some shortcuts like FUEL are unavailable.

To register our observer, we need to do “myUninstallObserver.register()” somewhere inside your initialisation routines (such as your onload handler)

var myUninstallObserver = {
	_uninstall : false,
	_tabs : null,

	observe : function(subject, topic, data) {
		if (topic == "em-action-requested") {
			// Extension has been flagged to be uninstalled
			subject.QueryInterface(Ci.nsIUpdateItem);
			if (subject.id == "myaddonID@getnightingale.com") {
				if (data == "item-uninstalled") {
					this._uninstall = true;
				} else if (data == "item-cancel-action") {
					this._uninstall = false;
				}
			}
		} else if (topic == "quit-application-granted") {
			// We're shutting down, so check to see if we were flagged
			// for uninstall - if we were, then cleanup here
			if (this._uninstall) {
				// Do your cleanup stuff here
				// such as deleting preferences, servicepane nodes,
				// lists, libraries, etc.
				dump("Uninstalled!\n");
			}
			this.unregister();
		}
	},

	register : function() {
		var observerService = Cc["@mozilla.org/observer-service;1"]
			.getService(Ci.nsIObserverService);
		observerService.addObserver(this, "em-action-requested", false);
		observerService.addObserver(this, "quit-application-granted", false);
	},

	unregister : function() {
		var observerService = Cc["@mozilla.org/observer-service;1"]
			.getService(Ci.nsIObserverService);
		observerService.removeObserver(this, "em-action-requested");
		observerService.removeObserver(this, "quit-application-granted");
	}
}
developer_center/recipe_book/extensions_and_core/uninstall.txt · Last modified: 2013/12/28 10:34 by geekshadow