Akbar
Akbar

Reputation: 1499

firefox addon: not able to listen firefox "quit-application" event

I have the following code snippet to enable extension after firefox quit,

observe: function (subject, topic, data) {

     if (topic == "quit-application") {
         LOG("inside quit-application Testing ");
         var os = Components.classes["@mozilla.org/observer-service;1"]
             .getService(Components.interfaces.nsIObserverService);

         //os.addObserver(this, "http-on-examine-cached-response", false);
         os.addObserver(this, "quit-application", false);
         var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
             .getService(Components.interfaces.nsIXULAppInfo);
         var tempappVersion = appInfo.version;
         var appVersion = tempappVersion.split(".");
         // adding add-on listener dsable event on add-on for FF4 and later versions.
         if (appVersion[0] >= 4) {
             setAddonEnableListener();
             LOG("\napp-startup Testing from javascript file....");
         }
         return;
     }
}

And inside the setAddonEnableListener I am trying to enable the extension like this:

function setAddonEnableListener() {
    try {
    alert("setAddonEnableListener akbar nsListener called from ");
        Components.utils.import("resource://gre/modules/AddonManager.jsm");
    AddonManager.getAddonByID("[email protected]", function(addon)
    {
    if (addon.userDisabled)
        addon.userDisabled = false;
    });

    } catch (ex) {
    }
}

And I am registering the quit-application event var like this:

myModule = {
    registerSelf: function (compMgr, fileSpec, location, type) {
        var compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
        compMgr.registerFactoryLocation(this.myCID,
                                        this.myName,
                                        this.myProgID,
                                        fileSpec,
                                        location,
                                        type);

        var catMgr = Components.classes["@mozilla.org/categorymanager;1"].getService(Components.interfaces.nsICategoryManager);
        catMgr.addCategoryEntry("app-startup", this.myName, this.myProgID, true, true);
        catMgr.addCategoryEntry("quit-application", this.myName, this.myProgID, true, true);
    }

When Firefox quits, inside quit-application Testing message is not getting displayed. What am I doing wrong here?

Upvotes: 4

Views: 683

Answers (1)

Wladimir Palant
Wladimir Palant

Reputation: 57651

There is no category quit-application. You should receive app-startup notification (or rather profile-after-change starting with Firefox 4) and register your observer for quit-application:

observe: function (subject, topic, data) {
   if (topic == "app-startup" || topic == "profile-after-change") {
     var observerService = Components.classes["@mozilla.org/observer-service;1"]
                                     .getService(Components.interfaces.nsIObserverService);
     observerService.addObserver(this, "quit-application", false);
   }
   else if (topic == "quit-application") {
     var observerService = Components.classes["@mozilla.org/observer-service;1"]
                                     .getService(Components.interfaces.nsIObserverService);
     observerService.removeObserver(this, "quit-application");
     ...
   }
}

To quote the documentation:

Unless otherwise noted you register for the topics using the nsIObserverService.

And there is no note about registration via category manager on any shutdown notifications.

Btw, I sincerely recommend XPCOMUtils for component registration. You shouldn't need to write the module definition yourself.

Upvotes: 2

Related Questions