Firefox Addon: Revert Preference to their original value

77 Views Asked by At

If an add-on changes a preference outside its own preference branch, then it should revert them back to their previous state (not necessarily the default state) on uninstalling the add-on.

I imagine, the current preferences have to be saved and then at uninstall used to revert back.
Running AddonManager.addAddonListener() seems to be an option (but running a process continuously!). Is there any other option?

In a bootstrapped addon, there is a function uninstall() {}, but how is it done in an overlay add-on?

Other useful topics on the subject (but not fully answering this question):
Firefox Addon: how to remove preferences when addon is being uninstalled?
Set preferences in the user branch and unset them on uninstall

Update By running a process continuously I meant, running a listener/process for example for an event that may happen once a year. I have already thought of another way which could be more efficient but since that was a hypothetical situation, I don't have the anything to test it on.

2

There are 2 best solutions below

2
On

Using AddonManager.addAddonListener() is the correct path to take when using an old school overlay add-on.

I'm not sure what you mean when you say but running a process continuously! the listener is just added to an array of listeners, and there is no new process running.

0
On

No, you should not run a process continually, nor have a listener on any preferences you changed. There is no reason for it. For the application that you described, you have no need to know when a preference changes. You only need to know what the preference is:

  1. Prior to you changing it
  2. What you changed it to
  3. What it is at the time you uninstall

You have no need to keep track of intermediate changes. Thus, no listener.

When you uninstall, if the preference is the value to which you changed it, change it back to the value which it was prior to you changing it. If it is some value you did not set, then either the user, or another extension, changed it and you should not change it back to the value it was prior to you installing your extension (i.e. you should not undo something the user has hand set, or was set via another extension).

However, if you are an overlay extension, you should add a AddonListener with AddonManager in order to detect when your add-on has been (or will be) disabled/uninstalled. This is so you can know when you should be changing the preferences back.

Bootstrapped (restartless) extensions have their own uninstall notification.

Add-on SDK extensions also have their own way.

It would probably be a good idea to read Appendix B: Install and Uninstall Scripts.

This question has some good information: Firefox Addon: how to remove preferences when addon is being uninstalled?