When building an Firefox Plugin, it may be necessary to catch the event fired when Firefox closes.

To catch that event, register this listener in your XPCOM component constructor:

  try
  {
    var Application = Components.classes["@mozilla.org/fuel/application;1"].getService(Components.interfaces.fuelIApplication);
    Application.events.addListener('quit', function() {
      // Do something on application exit
    }.bind(this));
  }
  catch(e)
  {
    dump("Exception in close event registration" + e);
  }

Note the call to prototype's bind function on the callback. This can be achieved by adding the bind function to the Function prototype by adding this code to the top of your JavaScript XPCOM component js file:

Function.prototype.bind = function () {
    if (arguments.length < 2 && arguments[0] === undefined) {
        return this;
    }
    var thisObj = this,
    args = Array.prototype.slice.call(arguments),
    obj = args.shift();
    return function () {
        return thisObj.apply(obj, args.concat(Array.prototype.slice.call(arguments)));
    };
};

Function.bind = function() {
    var args = Array.prototype.slice.call(arguments);
    return Function.prototype.bind.apply(args.shift(), args);
}

Read more about the FUEL Application interface here: https://developer.mozilla.org/en/Toolkit_API/FUEL