Firefox addon: unable to listen to firefox "quit-application" event

I have the following code snippet to enable the extension after exiting Firefox,

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 setAddonEnableListener I am trying to enable the extension as follows:

 function setAddonEnableListener() { try { alert("setAddonEnableListener akbar nsListener called from "); Components.utils.import("resource://gre/modules/AddonManager.jsm"); AddonManager.getAddonByID(" somename@extension.com ", function(addon) { if (addon.userDisabled) addon.userDisabled = false; }); } catch (ex) { } } 

And I am logging the quit-application var event as follows:

 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 shuts down, the inside quit-application Testing message is not displayed. What am I doing wrong here?

+4
source share
1 answer

There is no quit-application category. You should receive an 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 specified, you register for topics using nsIObserverService.

And there are no notifications of registration through the category manager for any notifications of completion of work.

By the way, I sincerely recommend XPCOMUtils for registering components. You do not need to write your own module definition.

+2
source

Source: https://habr.com/ru/post/1385919/


All Articles