Revision 350165 of Bootstrapped extensions

  • Revision slug: Extensions/Bootstrapped_extensions
  • Revision title: Bootstrapped extensions
  • Revision id: 350165
  • Created:
  • Creator: Oeekker
  • Is current revision? No
  • Comment Fix typo

Revision Content

{{ gecko_minversion_header("2.0") }}

Traditional extensions include overlays, wherein the application can load up XUL from the extension's package and automatically apply it atop its own UI. While this makes creating extensions that add to the application's user interface relatively easy, it means that updating, installing, or disabling an extension requires an application restart.

Gecko 2.0 {{ geckoRelease("2.0") }} introduces bootstrapped extensions. These are special extensions that, instead of using overlays to apply their user interface to the application, programmatically insert themselves into the application. This is done using a special script file that's included in the extension that contains functions the browser calls to command the extension to install, uninstall, start up, and shut down.

All the application does is call into this script file; the extension is responsible for adding and removing its user interface and handling any other setup and shutdown tasks it requires.

This article discusses how bootstrapped extensions work.

As an aside, all extensions created using the Add-on SDK or Add-on Builder are bootstrapped; however, because all the bootstrapping code is generated for you, you don't really need to think about it.

The startup and shutdown process

A key feature of bootstrapped extensions is that they must be able to be started up and shut down on demand by the application. When the extension's startup() function is called, it must manually inject its user interface and other behavior into the application. Similarly, when its shutdown() function is called, it must remove anything it's added to the application, as well as all references to any of its objects.

There are several scenarios in which the startup() function may be called; for example:

  • When the extension is first installed, assuming that it's both compatible with the application and is enabled.
  • When the extension becomes enabled using the add-ons manager window.
  • When the application is started up, if the extension is enabled and compatible with the application.

Some examples of when the shutdown() function may be called:

  • When the extension is uninstalled, if it's currently enabled.
  • When the extension becomes disabled.
  • When the user quits the application, if the extension is enabled.

Notes on modifying the application user interface

chrome.manifest in bootstrapped add-ons

You can use a chrome.manifest file in bootstrapped add-ons to:

  1. make your add-on's content available via a chrome:// URL (using the content, locale, and skin instructions in the manifest);
  2. replace existing chrome:// URIs with your content (using the override instruction).

Not all chrome.manifest instructions are supported in bootstrapped add-ons, for example you still cannot register XUL Overlays from a bootstrapped add-on. See the chrome.manifest documentation for details.

In Firefox 10 and later the chrome.manifest file located in the root of the add-on's XPI (i.e. a sibling of the install.rdf) is loaded automatically. In Firefox 8 and 9 you had to load/unload the manifest manually using {{ ifmethod("nsIComponentManager", "addBootstrappedManifestLocation") }} and {{ ifmethod("nsIComponentManager", "removeBootstrappedManifestLocation") }}. This feature was unavailable in Firefox versions before 8.

Adding user interface manually

If you decide to go ahead and try to develop a bootstrapped extension that modifies the application's user interface, here are a few suggestions to get you started.

You need to look up the relevant application UI elements by their ID by calling {{ domxref("document.getElementById()") }}, then manipulate them to inject your UI. For example, you can get access to the menu bar in Firefox with document.getElementById("main-menubar").

Be sure that at shutdown time, you remove any user interface you've added.

Creating a bootstrapped extension

To mark an extension as bootstrappable, you need to add the following element to its install manifest:

<em:bootstrap>true</em:bootstrap>

Then you need to add a bootstrap.js file that contains the required functions; this should be alongside the install.rdf file in the extension's package.

Backward compatibility

Because older versions of Firefox don't know about the bootstrap property or bootstrap.js file, it's not overly difficult to create an XPI that will work on both as a bootstrappable extension and as a traditional extension. Create your extension as a bootstrappable extension, then add the traditional overlays as well. Newer versions of Firefox will use the bootstrap.js script, ignoring the components and overlays, while older versions will use the overlays.

Bootstrap entry points

The bootstrap.js script should contain several specific functions, which are called by the browser to manage the extension. The script gets executed in a privileged sandbox, which is cached until the extension is shut down.

startup

Called when the extension needs to start itself up. This happens at application launch time or when the extension is enabled after being disabled (or after it has been shut down in order to install an update. As such, this can be called many times during the lifetime of the application.

This is when your add-on should inject its UI, start up any tasks it may need running, and so forth.

void startup(
  data,
  reason
); 
Parameters
data
A bootstrap data structure.
reason
One of the reason constants, indicating why the extension is being started up. This will be one of APP_STARTUP, ADDON_ENABLE, ADDON_INSTALL, ADDON_UPGRADE, or ADDON_DOWNGRADE.

shutdown

Called when the extension needs to shut itself down, such as when the application is quitting or when the extension is about to be upgraded or disabled. Any user interface that has been injected must be removed, tasks shut down, and objects disposed of.

void shutdown(
  data,
  reason
);
Parameters
data
A bootstrap data structure.
reason
One of the reason constants, indicating why the extension is being shut down. This will be one of APP_SHUTDOWN, ADDON_DISABLE, ADDON_UNINSTALL, ADDON_UPGRADE, or ADDON_DOWNGRADE.

install

Your bootstrap script must include an install() function, which the application calls before the first call to startup() after the extension is installed, upgraded, or downgraded.

Note: This method is never called if the extension has never been started; for example, if an extension is installed but isn't compatible with the current version of the application, install() never gets called if it is uninstalled before becoming compatible. However, if the extension gets upgraded to a version that's compatible with the application, its install() function will be called at that time, before the first startup() call.
void install(
  data,
  reason
); 
Parameters
data
A bootstrap data structure.
reason
One of the reason constants, indicating why the extension is being installed. This will be one of ADDON_INSTALL, ADDON_UPGRADE, or ADDON_DOWNGRADE.

uninstall

This function is called after the last call to shutdown() before a particular version of an extension is uninstalled. This will not be called if install() was never called.

Note: It's important to keep in mind that uninstall() can be called even on extensions that are currently disabled, or are not compatible with the current application. Because of this, it's crucial that the function be implemented to gracefully handle APIs that may not be present in the application. This function will also not be called if a third-party application removes the extension while Firefox isn't running.
void uninstall(
  data,
  reason
); 
Parameters
data
A bootstrap data structure.
reason
One of the reason constants, indicating why the extension is being uninstalled. This will be one of ADDON_UNINSTALL, ADDON_UPGRADE, or ADDON_DOWNGRADE.

Reason constants

The bootstrap functions accept a reason parameter, which explains to the extension why it's being called. The reason constants are:

Constant Value Description
APP_STARTUP 1 The application is starting up.
APP_SHUTDOWN 2 The application is shutting down.
ADDON_ENABLE 3 The add-on is being enabled.
ADDON_DISABLE 4 The add-on is being disabled. (Also sent during uninstallation)
ADDON_INSTALL 5 The add-on is being installed.
ADDON_UNINSTALL 6 The add-on is being uninstalled.
ADDON_UPGRADE 7 The add-on is being upgraded.
ADDON_DOWNGRADE 8 The add-on is being downgraded.

Bootstrap data

Each of the entry points is passed a simple data structure containing some useful information about the add-on being bootstrapped. More information about the add-on can be obtained by calling AddonManager.getAddonByID(). The data is a simple JavaScript object with the following properties:

Property Type Description
id string The ID of the add-on being bootstrapped.
version string The version of the add-on being bootstrapped.
installPath nsIFile The installation location of the add-on being bootstrapped. This may be a directory or an XPI file depending on whether the add-on is installed unpacked or not.
resourceURI nsIURI A URI pointing at the root of the add-ons files, this may be a jar: or file: URI depending on whether the add-on is installed unpacked or not. {{ gecko_minversion_inline("7.0") }}

Further reading

Revision Source

<p>{{ gecko_minversion_header("2.0") }}</p>
<p>Traditional extensions include <strong>overlays</strong>, wherein the application can load up XUL from the extension's package and automatically apply it atop its own UI. While this makes creating extensions that add to the application's user interface relatively easy, it means that updating, installing, or disabling an extension requires an application restart.</p>
<p>Gecko 2.0 {{ geckoRelease("2.0") }} introduces <strong>bootstrapped extensions</strong>. These are special extensions that, instead of using overlays to apply their user interface to the application, programmatically insert themselves into the application. This is done using a special script file that's included in the extension that contains functions the browser calls to command the extension to install, uninstall, start up, and shut down.</p>
<p>All the application does is call into this script file; the extension is responsible for adding and removing its user interface and handling any other setup and shutdown tasks it requires.</p>
<p>This article discusses how bootstrapped extensions work.</p>
<p>As an aside, all extensions created using the <a class="link-https" href="https://addons.mozilla.org/en-US/developers/docs/sdk/latest/">Add-on SDK</a> or <a class="link-https" href="https://builder.addons.mozilla.org/">Add-on Builder</a> are bootstrapped; however, because all the bootstrapping code is generated for you, you don't really need to think about it.</p>
<h2 id="The_startup_and_shutdown_process">The startup and shutdown process</h2>
<p>A&nbsp;key feature of bootstrapped extensions is that they must be able to be started up and shut down on demand by the application. When the extension's <code>startup()</code>&nbsp;function is called, it must manually inject its user interface and other behavior into the application. Similarly, when its <code>shutdown()</code>&nbsp;function is called, it must remove anything it's added to the application, as well as all references to any of its objects.</p>
<p>There are several scenarios in which the <code>startup()</code>&nbsp;function may be called; for example:</p>
<ul>
  <li>When the extension is first installed, assuming that it's both compatible with the application and is enabled.</li>
  <li>When the extension becomes enabled using the add-ons manager window.</li>
  <li>When the application is started up, if the extension is enabled and compatible with the application.</li>
</ul>
<p>Some examples of when the <code>shutdown()</code>&nbsp;function may be called:</p>
<ul>
  <li>When the extension is uninstalled, if it's currently enabled.</li>
  <li>When the extension becomes disabled.</li>
  <li>When the user quits the application, if the extension is enabled.</li>
</ul>
<h2 id="Notes_on_modifying_the_application_user_interface">Notes on modifying the application user interface</h2>
<h3 id="chrome.manifest_in_bootstrapped_add-ons">chrome.manifest in bootstrapped add-ons</h3>
<p>You can use a <a href="/en-US/docs/Chrome_Registration"><code>chrome.manifest</code></a> file in bootstrapped add-ons to:</p>
<ol>
  <li>make your add-on's content available via a <code>chrome://</code> URL (using the <code>content</code>, <code>locale</code>, and <code>skin</code> instructions in the manifest);</li>
  <li>replace existing <code>chrome://</code> URIs with your content (using the <code>override</code> instruction).</li>
</ol>
<p>Not all <code>chrome.manifest</code> instructions are supported in bootstrapped add-ons, for example you still cannot register <a href="/en-US/docs/XUL_Overlays">XUL Overlays</a> from a bootstrapped add-on. See the <a href="/en-US/docs/Chrome_Registration"><code>chrome.manifest</code></a> documentation for details.</p>
<p>In Firefox 10 and later the <code>chrome.manifest</code> file located in the root of the add-on's XPI (i.e. a sibling of the <code>install.rdf</code>) is loaded automatically. In Firefox 8 and 9 you had to load/unload the manifest manually using {{ ifmethod("nsIComponentManager", "addBootstrappedManifestLocation") }} and {{ ifmethod("nsIComponentManager", "removeBootstrappedManifestLocation") }}. This feature was unavailable in Firefox versions before 8.</p>
<h3 id="Adding_user_interface_manually">Adding user interface manually</h3>
<p>If you decide to go ahead and try to develop a bootstrapped extension that modifies the application's user interface, here are a few suggestions to get you started.</p>
<p>You need to look up the relevant application UI&nbsp;elements by their ID&nbsp;by calling {{ domxref("document.getElementById()") }}, then manipulate them to inject your UI. For example, you can get access to the menu bar in Firefox with <code>document.getElementById("main-menubar")</code>.</p>
<p>Be sure that at shutdown time, you remove any user interface you've added.</p>
<h2 id="Creating_a_boostrapped_extension">Creating a bootstrapped extension</h2>
<p>To mark an extension as bootstrappable, you need to add the following element to its <a href="/en-US/docs/Install_Manifests">install manifest</a>:</p>
<pre>
<code>&lt;em:bootstrap&gt;true&lt;/em:bootstrap&gt;</code></pre>
<p>Then you need to add a <a href="/en-US/docs/Extensions/bootstrap.js"><code><strong>bootstrap.js</strong></code> file</a> that contains the required functions; this should be alongside the <a href="/en-US/docs/Install_Manifests"><code>install.rdf</code> file</a> in the extension's package.</p>
<h3 id="Backward_compatibility">Backward compatibility</h3>
<p>Because older versions of Firefox don't know about the <code>bootstrap</code> property or <code>bootstrap.js</code> file, it's not overly difficult to create an XPI&nbsp;that will work on both as a bootstrappable extension and as a traditional extension. Create your extension as a bootstrappable extension, then add the traditional overlays as well. Newer versions of Firefox will use the <code>bootstrap.js</code> script, ignoring the components and overlays, while older versions will use the overlays.</p>
<h2 id="Bootstrap_entry_points">Bootstrap entry points</h2>
<p>The <code>bootstrap.js</code> script should contain several specific functions, which are called by the browser to manage the extension. The script gets executed in a privileged sandbox, which is cached until the extension is shut down.</p>
<h3 id="startup">startup</h3>
<p>Called when the extension needs to start itself up. This happens at application launch time or when the extension is enabled after being disabled (or after it has been shut down in order to install an update. As such, this can be called many times during the lifetime of the application.</p>
<p>This is when your add-on should inject its UI, start up any tasks it may need running, and so forth.</p>
<pre>
void startup(
&nbsp; data,
&nbsp; reason
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl>
  <dt>
    <code>data</code></dt>
  <dd>
    A <a href="#Bootstrap_data">bootstrap data structure</a>.</dd>
  <dt>
    <code>reason</code></dt>
  <dd>
    One of the <a href="#Reason_constants">reason constants</a>, indicating why the extension is being started up. This will be one of <code>APP_STARTUP</code>, <code>ADDON_ENABLE</code>, <code>ADDON_INSTALL</code>, <code>ADDON_UPGRADE</code>, or <code>ADDON_DOWNGRADE</code>.</dd>
</dl>
<h3 id="shutdown">shutdown</h3>
<p>Called when the extension needs to shut itself down, such as when the application is quitting or when the extension is about to be upgraded or disabled. Any user interface that has been injected must be removed, tasks shut down, and objects disposed of.</p>
<pre>
void shutdown(
&nbsp; data,
&nbsp; reason
);
</pre>
<h6 id="Parameters">Parameters</h6>
<dl>
  <dt>
    <code>data</code></dt>
  <dd>
    A <a href="#Bootstrap_data">bootstrap data structure</a>.</dd>
  <dt>
    <code>reason</code></dt>
  <dd>
    One of the <a href="#Reason_constants">reason constants</a>, indicating why the extension is being shut down. This will be one of <code>APP_SHUTDOWN</code>, <code>ADDON_DISABLE</code>, <code>ADDON_UNINSTALL</code>, <code>ADDON_UPGRADE</code>, or <code>ADDON_DOWNGRADE</code>.</dd>
</dl>
<h3 id="install">install</h3>
<p>Your bootstrap script must include an <code>install()</code>&nbsp;function, which the application calls before the first call to <code>startup()</code>&nbsp;after the extension is installed, upgraded, or downgraded.</p>
<div class="note">
  <strong>Note:</strong> This method is never called if the extension has never been started; for example, if an extension is installed but isn't compatible with the current version of the application, <code>install()</code> never gets called if it is uninstalled before becoming compatible. However, if the extension gets upgraded to a version that's compatible with the application, its <code>install()</code>&nbsp;function will be called at that time, before the first <code>startup()</code>&nbsp;call.</div>
<pre>
void install(
&nbsp; data,
&nbsp; reason
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl>
  <dt>
    <code>data</code></dt>
  <dd>
    A <a href="#Bootstrap_data">bootstrap data structure</a>.</dd>
  <dt>
    <code>reason</code></dt>
  <dd>
    One of the <a href="#Reason_constants">reason constants</a>, indicating why the extension is being installed. This will be one of <code>ADDON_INSTALL</code>, <code>ADDON_UPGRADE</code>, or <code>ADDON_DOWNGRADE</code>.</dd>
</dl>
<h3 id="uninstall">uninstall</h3>
<p>This function is called after the last call to <code>shutdown()</code>&nbsp;before a particular version of an extension is uninstalled. This will not be called if <code>install()</code>&nbsp;was never called.</p>
<div class="note">
  <strong>Note:</strong> It's important to keep in mind that <code>uninstall()</code>&nbsp;can be called even on extensions that are currently disabled, or are not compatible with the current application. Because of this, it's crucial that the function be implemented to gracefully handle APIs that may not be present in the application. This function will also not be called if a third-party application removes the extension while Firefox isn't running.</div>
<pre>
void uninstall(
&nbsp; data,
&nbsp; reason
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl>
  <dt>
    <code>data</code></dt>
  <dd>
    A <a href="#Bootstrap_data">bootstrap data structure</a>.</dd>
  <dt>
    <code>reason</code></dt>
  <dd>
    One of the <a href="#Reason_constants">reason constants</a>, indicating why the extension is being uninstalled. This will be one of <code>ADDON_UNINSTALL</code>, <code>ADDON_UPGRADE</code>, or <code>ADDON_DOWNGRADE</code>.</dd>
</dl>
<h2 id="Reason_constants">Reason constants</h2>
<p>The bootstrap functions accept a <code>reason</code> parameter, which explains to the extension why it's being called. The reason constants are:</p>
<table class="standard-table">
  <tbody>
    <tr>
      <td class="header">Constant</td>
      <td class="header">Value</td>
      <td class="header">Description</td>
    </tr>
    <tr>
      <td><code>APP_STARTUP</code></td>
      <td>1</td>
      <td>The application is starting up.</td>
    </tr>
    <tr>
      <td><code>APP_SHUTDOWN</code></td>
      <td>2</td>
      <td>The application is shutting down.</td>
    </tr>
    <tr>
      <td><code>ADDON_ENABLE</code></td>
      <td>3</td>
      <td>The add-on is being enabled.</td>
    </tr>
    <tr>
      <td><code>ADDON_DISABLE</code></td>
      <td>4</td>
      <td>The add-on is being disabled. (Also <a class="link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=620541">sent during uninstallation</a>)</td>
    </tr>
    <tr>
      <td><code>ADDON_INSTALL</code></td>
      <td>5</td>
      <td>The add-on is being installed.</td>
    </tr>
    <tr>
      <td><code>ADDON_UNINSTALL</code></td>
      <td>6</td>
      <td>The add-on is being uninstalled.</td>
    </tr>
    <tr>
      <td><code>ADDON_UPGRADE</code></td>
      <td>7</td>
      <td>The add-on is being upgraded.</td>
    </tr>
    <tr>
      <td><code>ADDON_DOWNGRADE</code></td>
      <td>8</td>
      <td>The add-on is being downgraded.</td>
    </tr>
  </tbody>
</table>
<h2 id="Bootstrap_data">Bootstrap data</h2>
<p>Each of the entry points is passed a simple data structure containing some useful information about the add-on being bootstrapped. More information about the add-on can be obtained by calling <code><a href="/en-US/docs/Addons/Add-on_Manager/AddonManager#getAddonByID()">AddonManager.getAddonByID()</a></code>. The data is a simple JavaScript object with the following properties:</p>
<table class="standard-table">
  <tbody>
    <tr>
      <td class="header">Property</td>
      <td class="header">Type</td>
      <td class="header">Description</td>
    </tr>
    <tr>
      <td><code>id</code></td>
      <td><code>string</code></td>
      <td>The ID of the add-on being bootstrapped.</td>
    </tr>
    <tr>
      <td><code>version</code></td>
      <td><code>string</code></td>
      <td>The version of the add-on being bootstrapped.</td>
    </tr>
    <tr>
      <td><code>installPath</code></td>
      <td><code>nsIFile</code></td>
      <td>The installation location of the add-on being bootstrapped. This may be a directory or an XPI file depending on whether the add-on is installed unpacked or not.</td>
    </tr>
    <tr>
      <td><code>resourceURI</code></td>
      <td><code>nsIURI</code></td>
      <td>A URI pointing at the root of the add-ons files, this may be a <code>jar:</code> or <code>file:</code> URI depending on whether the add-on is installed unpacked or not. {{ gecko_minversion_inline("7.0") }}</td>
    </tr>
  </tbody>
</table>
<h2 id="Further_reading">Further reading</h2>
<ul>
  <li>Wladimir Palant explains <a class="external" href="http://adblockplus.org/blog/how-many-hacks-does-it-take-to-make-your-extension-install-without-a-restart">problems and bugs found when converting an existing extension</a>, including some solutions and workarounds.</li>
  <li>Mark Finkle provides some simple example code for <a class="external" href="http://starkravingfinkle.org/blog/2011/01/bootstrap-jones-adventures-in-restartless-add-ons/">restartless add-ons in mobile Firefox</a>, <a class="external" href="http://starkravingfinkle.org/blog/2011/01/restartless-add-ons-more-resources/">adding resources (like the options window)</a>&nbsp;to bootstrapped extensions and <a class="external" href="http://starkravingfinkle.org/blog/2011/01/restartless-add-ons-%e2%80%93-default-preferences/">using default preferences</a> without a <code>default/preferences/prefs.js</code> file.</li>
  <li>Kris Maglione writes about <a class="external" href="http://maglione-k.users.sourceforge.net/bootstrapped.xhtml">the requirements for the cleanup procedures</a> in bootstrapped extensions.</li>
  <li>Edward Lee shows off some <a class="external" href="http://ed.agadak.net/2011/01/restartless-add-on-example-code">helpful coding patterns and examples</a> you can use in your bootstrapped add-on.</li>
  <li>Erik Vold's quick tutorial on restartless addons:
    <ul>
      <li><a class="external" href="http://erikvold.com/blog/index.cfm/2010/11/3/restartless-restart-addon-for-firefox">Part 1: Introduction</a></li>
      <li><a class="external" href="http://erikvold.com/blog/index.cfm/2011/1/2/restartless-firefox-addons-part-2-includes">Part 2: Includes</a></li>
      <li><a class="external" href="http://erikvold.com/blog/index.cfm/2011/2/7/restartless-firefox-addons-part-3-icons">Part 3: Icons</a></li>
      <li><a class="external" href="http://erikvold.com/blog/index.cfm/2011/2/18/restartless-firefox-addons-part-4-localization-l10n">Part 4: Localization (l10n)</a></li>
      <li><a class="external" href="http://erikvold.com/blog/index.cfm/2011/4/1/restartless-firefox-addons-part-5-logging">Part 5: Logging</a></li>
      <li><a class="external" href="http://erikvold.com/blog/index.cfm/2011/6/14/restartless-firefox-addons-part-6-better-includes">Part 6: Better Includes</a></li>
      <li><a class="external" href="http://erikvold.com/blog/index.cfm/2011/6/19/restartless-firefox-addons-part-7-css">Part 7: CSS</a></li>
      <li><a class="external" href="http://erikvold.com/blog/index.cfm/2011/6/19/restartless-firefox-addons-part-8-require-commonjs-and-jetpack">Part 8: Require, CommonJS, and Jetpack</a></li>
    </ul>
  </li>
  <li>Documentation for <a href="/en-US/docs/Extensions/Inline_Options">Inline Options</a> in Firefox 7 and later.</li>
</ul>
Revert to this revision