1

What is the code for knowing any add-on installation (Firefox browser) with an SDK add-on?

I know it should be written using AddonManager.addInstallListener() and onNewInstall() methods. I cannot combine them and write the code. Please help me with the code.

Makyen
  • 31,849
  • 12
  • 86
  • 121
  • I have assumed that you are really asking about how to listen for the installation of add-ons. If your question really was how to listen to the *progress* of add-on installations, which is what `addInstallListener()` is for, then please [edit] your question to explicitly state that you want to listen to the installation *progress*. – Makyen Nov 29 '16 at 20:25
  • It is also possible that you are wanting to ask about how to determine that the installation is actually the installation of a *new* add-on (i.e. not an update to an add-on which is already installed). if so, you need to ask [a new/additional question](http://stackoverflow.com/questions/ask) which is specifically that. That is not what `onNewInstall()` provides (at least based on my understanding). `onNewInstall()` is fired when a new `AddonInstall` is created. An `AddonInstall` is created for add-on updates. Thus, `onNewInstall()` does not indicate that a *new* add-on is being installed. – Makyen Nov 29 '16 at 20:36
  • My intention was to know both events new add-on installing and old add-on updates. – Seelam Venkatanagasiva Nov 29 '16 at 20:55

1 Answers1

0

If you want to know the detailed progress of add-on installations (not uninstalls), you can use listeners which you add with AddonManager.addInstallListener(). However, for what you have asked, receiving events when an and-on is installed (i.e. not monitoring the progress of the installation, just that it happened), you can use AddonManager.addAddonListener() and the onInstalled event.

This other answer of mine contains a complete Add-on SDK extension which shows the various events available through the AddonManager's addAddonListener() method. It also shows the order the events are fired for an add-on which is already installed and one which is being installed and uninstalled (showing both what events you get for your own install/uninstall and when the add-on being installed/uninstalled is not your own add-on).

Editing that code down to just what is needed for what you asked results in the following code (Note: I hand edited the code here, but did not test it (i.e. there may be errors). The code in the answer I linked above was completely tested):

const { AddonManager } = require("resource://gre/modules/AddonManager.jsm");

var addonListener = {
    onInstalled: function(addon){
        console.log('AddonManager Event: Installed addon ID: ' + addon.id 
                    + ' ::addon object:', addon);
    }
}

exports.onUnload = function (reason) {
    //Your add-on listeners are NOT automatically removed when
    //  your add-on is disabled/uninstalled. 
    //You MUST remove them in exports.onUnload if the reason is
    //  not 'shutdown'.  If you don't, errors will be shown in the
    //  console for all events for which you registered a listener.
    if(reason !== 'shutdown') {
        uninstallAddonListener();
    }
};

function installAddonListener(){
    //Using an AddonManager listener is not effective to listen for your own add-on's
    //  install event.  The event happens prior to you adding the listener.
    //console.log('In installAddonListener: Adding add-on listener');
    AddonManager.addAddonListener(addonListener);
}

function uninstallAddonListener(){
    //console.log('In removeAddonListener: Removing add-on listener');
    AddonManager.removeAddonListener(addonListener);
}

installAddonListener();
Community
  • 1
  • 1
Makyen
  • 31,849
  • 12
  • 86
  • 121
  • Thanks for the answer. I really want to know how to write listeners for the installing events and updating events. I got the answer from you and edit it as per my requirement. – Seelam Venkatanagasiva Nov 29 '16 at 20:57
  • @SeelamVenkatanagasiva, I'm happy to update it to be what you want, but I am unsure as to *exactly* what you desire. There are multiple events you could be listening to. All of them should fire on both installs of *new* add-ons and installs of *updates* to existing add-ons. `addAddonListener()` can be used to add listeners which fire `onInstalling` and `onInstalled` (and other add-on state changes). `addInstallListener()` can be used to get multiple events during the install process, providing more detailed information as to the progress of the install, which is not what is usually desired. – Makyen Nov 29 '16 at 21:13