0

I have Firefox Jetpack Add-on-SDK based extension that is built not in the best way - after reinstall some old XPCOM components are hanging in memory and it is unable to register new one. So, I need to force browser restart on extension update/install. I know older version does this by default, but this one is restartless.

So my question is: how can I force the browser to restart on extension upgrade? I have looked into package.json and found nothing.

Makyen
  • 31,849
  • 12
  • 86
  • 121
msangel
  • 9,895
  • 3
  • 50
  • 69
  • Are you looking for a way to mark your extension as non-restartless, or programmatically cause a restart from within your code? If you are going to do it programmatically, without having marked your extension as non-restartless, then you should warn the user and ask for confirmation that it is acceptable to the user to restart at this time. – Makyen Jun 03 '16 at 04:00
  • If you are wanting to mark your code as non-restartless you are probably SOL. I would expect any restartless add-on to become non-functional if marked in *install.rdf* as `false`. A restartless add-on uses a significantly different start-up sequence which I would expect not to be followed if marked as non-bootstrap. In other words, I would expect *bootstrap.js* to not be loaded and nothing in it to be called. Thus, I would expect you to need to force the restart yourself. – Makyen Jun 03 '16 at 04:06
  • Have you considered doing proper cleanup instead? In my experience it can be a bit tedious to hunt down all the dangling bits, but it's generally possible – the8472 Jun 03 '16 at 11:38

1 Answers1

2

My expectation is that you are basically SOL if your desire is to try to mark your Add-on SDK extension as non-restartless. The Add-on SDK produces a restartless extension that uses the standard restartless startup methods. So, effectively anytime you create an Add-on SDK add-on you have really created a restartless add-on that is wrapped by the Add-on SDK.

You could change, in the install.rdf file created when you jpm xpi, the following line:
<em:bootstrap>true</em:bootstrap>
to
<em:bootstrap>false</em:bootstrap>
which would cause your extension to be seen as a non-restartless add-on. It would be treated by Firefox and Mozilla Add-ons as an extension which requires a restart to be enabled/disabled.

However, doing so would cause your add-on to not function. The entry points to your add-on would never be called.

Thus, you must force the restart on your own.

You will need to store a preference that indicates that you have, or have not, restarted (i.e. so that you do not end up restarting continuously). See:

You should open a dialog/panel (MDN add-on SDK panel API) with the user to inform them that a restart is required in order for your add-on to function and provide them with the option to restart at that time, or to wait. See: Addon SDK way to make a dialog

In one of my add-ons, Change Profile's Window Icons, there is the need to restart in order to have changes take full effect (not restarting only results in partial effect). I inform the user of that need and provide the option for them to do so from the options dialog. The dialog looks like:
Change Profile's Icon Options Dialog

In this dialog, the button Accept changes and Restart Firefox will do what it says, and restart Firefox. The code I use to restart Firefox is:

window.opener.content.document.getElementById('cmd_restartApp').doCommand();

This uses the restart capability that is built into Firefox to perform the restart. Thus, there is no need to keep that code updated if there are changes in the exact methodology of how a restart must be performed, or any housekeeping associated with doing so.

How, exactly, you would call that function depends on the context you are in when you want to do so. In part, this will depend on how you implement informing the user that a restart is required and provide them the option to do so now or later. Given that you have not specified this in the question, it is not possible to tell you exactly the code that you need to use in order to cause the restart. However, in general, you will need a reference to one of the base Firefox windows. You can obtain that reference by any of a variety of methods depending on how you are implementing the interaction with the user.

One way would be:

if (window === null || typeof window !== "object") {
    //If you do not already have a window reference, you need to obtain one:
    //  Add/remove a "/" to comment/un-comment the code appropriate for your add-on type.
    //* Add-on SDK:
    var window = require('sdk/window/utils').getMostRecentBrowserWindow();
    //*/
    /* Overlay and bootstrap (from almost any context/scope):
    var window=Components.classes["@mozilla.org/appshell/window-mediator;1"]
                         .getService(Components.interfaces.nsIWindowMediator)
                         .getMostRecentWindow("navigator:browser");        
    //*/
}
if (typeof document === "undefined") {
    //If there is no document defined, get it
    var document = window.content.document;
}

//Then restart:
document.getElementById('cmd_restartApp').doCommand();

Portions of the above code were copied from other answers of mine, including this one, or from an extension I wrote.

Community
  • 1
  • 1
Makyen
  • 31,849
  • 12
  • 86
  • 121
  • Thank. You exactly point the situation I have here. Can you provide a link to your extension so I can look how it works? – msangel Jun 03 '16 at 09:33
  • @msangel, The add-on is [Change Profile's Window Icons](https://addons.mozilla.org/en-US/firefox/addon/change-window-icons/). It is an XUL based add-on that changes the window icon for that profile. It is non-restartless, because Firefox does not allow changing window icons from restartless add-ons. If the icon chosen is changed in the options dialog, the icon for any already existent windows will not change. To have those change it is necessary to restart Firefox. The button exists so the user has an easy way to do so. See *chrome/content/options.js* and *chrome/content/options.xul*. – Makyen Jun 03 '16 at 10:55
  • @msangel, I added links to other questions, searches and MDN re panels/dialogs using Add-on SDK. In addition, linked a question on using a preference to perform action only once upon install and the XUL based info on the concept of using a preference to determine if a do once on install action has been performed. – Makyen Jun 03 '16 at 17:32