Firefox Bootstrapped extensions
Note: All extensions created using the firefox-addon-sdk are bootstrapped! All the bootstrapping code is generated for you, so you don't really need to think about it
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 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1) 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
, startup
, and shutdown
.
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.
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.
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.
Bootstrap entry points:
aData A bootstrap data structure
{id, version, installPath, resourceURI, ...}
.aReason One of the reason constants
APP_STARTUP
,ADDON_ENABLE
,ADDON_INSTALL
,...
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:
function startup(aData, aReason) { }
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.
function shutdown(aData, aReason) { }
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.
function install(aData, aReason) { }
uninstall
This function is called after the last call to shutdown() before a particular version of an extension is uninstalled.
function uninstall(aData, aReason) { }
Check more about The bootstrap.js scope