Extension Manager:Bootstrapped Extensions: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
(Created page with 'Bootstrapped extensions exist as a means of restricting what is available to an extension in order to allow it to be loaded and unloaded with restarting the application. ==Ratio…')
 
(link to MDC docs)
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Bootstrapped extensions exist as a means of restricting what is available to an extension in order to allow it to be loaded and unloaded with restarting the application.
{{Note|This page contains preliminary documentation of the feature and is mainly of historical interest. The documentation is at [https://developer.mozilla.org/en/Extensions/Bootstrapped_extensions MDC].}}


==Rationale==
__NOTOC__Bootstrapped add-ons are a special kind of add-on that developers can choose to use instead of normal add-ons in order to allow users to install and use the add-on without needing to restart the application. In order to do this, rather than the application loading chrome and components from the add-on it is up to the add-on developer to do all of this manually.


Current XPI extensions cannot be unloaded without restarting the application. This is because the features that we give them (XPCOM components, chrome registration and overlays etc.) cannot be removed in a managed fashion. Loading without a restart could possibly be implemented however that really only solves a small part of the problem as updates etc. would still require restarts.
All the application will do is execute a bootstrap script that the add-on provides, telling it to startup. The add-on can do whatever it likes at that point however it must undo anything it has done when we call the bootstrap script to tell it to shutdown.


The approach taken here is to simplify what it means to be an extension. Instead of registering XPCOM components and chrome in bootstrapped extension we instead do nothing except call a bootstrap script when loading the extension. The extension can do whatever it likes at that point however it must undo anything it has done when we call the bootstrap script to tell it to unload.
==Starting up and Shutting down==


==Declaring an Extension as Bootstrappable==
A key feature of bootstrapped add-ons is that they must be able to be started and shutdown on demand by the application. When told to startup it is up to the add-on's bootstrap code to do anything necessary to inject UI and behaviour into the application. When told to shutdown it is required that the add-on remove anything it has added and removing all references to its objects. Some examples of when startup and shutdown happen:


Extensions are declared as being bootstrappable using the special em:type property with a value of 64 in the install manifest. They should also include a bootstrap.js file alongside the install.rdf.
* When first installed (if the add-on is compatible and enabled) the application will tell the add-on to start up.
* When uninstalled (if the add-on is currently enabled) the application will tell the add-on to shutdown.
* When enabled or disabled while the application is running the application will tell the add-on to startup or shutdown as appropriate.
* When the application is started the application will tell any enabled add-ons to startup.
* When the user quits the application the application will tell any enabled add-ons to shutdown.
 
==Declaring an Add-on as Bootstrappable==
 
Add-ons are declared as being bootstrappable using the special em:bootstrap="true" property in the install manifest. Although any type of add-on can include this, for now it only makes sense for add-ons with em:type="2" (the default extension type) to include it. They should also include a bootstrap.js file alongside the install.rdf.
 
==Backwards Compatibility==
 
Older versions of Firefox do not know about the em:bootstrap flag or bootstrap.js file but with care it is possible to make the same XPI usable in both cases. Older versions would just treat it as a normal add-on requiring a restart to install and uninstall and loading components and chrome from the normal places. Newer versions will ignore the components and chrome and just use the bootstrap.js.


==Bootstrap Events==
==Bootstrap Events==


When an extension is loaded the bootstrap.js file is loaded into a privileged sandbox which is cached until the extension is unloaded. One of the following functions will be called:
Bootstrapped add-ons must provide a bootstrap.js file which defines main entry functions as defined below. When the application needs to call a function the script will be executed in a privileged sandbox which is cached until the add-on is shutdown.
 
===Startup===
 
Whenever an add-on needs to be started the application will call a function named <code>startup</code> in the bootstrap script. <code>startup</code> may be called at any point during an application run. It will be passed the following arguments:
 
;data :A JS object containing basic information about the add-on, <code>id</code>, <code>version</code> and <code>installPath</code>.
;reason :A number representing the reason for starting the add-on. This may be APP_STARTUP, ADDON_ENABLE, ADDON_INSTALL, ADDON_UPGRADE or ADDON_DOWNGRADE.
 
The add-on must at this point do anything necessary to make its functionality available, the application will not execute any other part of the add-on.
 
===Shutdown===
 
Whenever an add-on needs to be shutdown the application will call a function named <code>shutdown</code> in the bootstrap script. <code>shutdown</code> may be called at any point during an application run. It will be passed the following arguments:
 
;data :A JS object containing basic information about the add-on, <code>id</code>, <code>version</code> and <code>installPath</code>.
;reason :A number representing the reason for shutting down the add-on. This may be APP_SHUTDOWN, ADDON_DISABLE, ADDON_UNINSTALL, ADDON_UPGRADE or ADDON_DOWNGRADE.
 
The add-on must at this point remove anything it has added to the application so its memory can be reclaimed.
 
===Install===
 
The bootstrap script may include an <code>install</code> method and if so it will be called before the first call to <code>startup</code> after a particular version of an add-on is installed. It will be passed the following arguments:
 
;data :A JS object containing basic information about the add-on, <code>id</code>, <code>version</code> and <code>installPath</code>.
;reason :A number representing the reason for installing the add-on. This may be ADDON_INSTALL, ADDON_UPGRADE or ADDON_DOWNGRADE.
 
Note that <code>install</code> will never be called if the add-on has never been started. For example if an add-on is installed but is not compatible then <code>install</code> will never be called if the add-on is uninstalled before it becomes compatible. If on the other hand the add-on becomes compatible at a later point <code>install</code> will be called before the call to <code>startup</code>.


* <code>install</code> is called if the extension is installed while Firefox is running.
===Uninstall===
* <code>startup</code> is called when Firefox starts up and the extension is enabled (occurs during profile-after-change currently).
* <code>enable</code> is called if the user enables the extension.


If the <code>install</code> or <code>startup</code> functions do not exist then the <code>enable</code> function will be called in their place.
The bootstrap script may include an <code>uninstall</code> method and if so it will be called after the last call to <code>shutdown</code> before a particular version of an add-on is uninstalled. <code>uninstall</code> will only ever be called if <code>install</code> has been called at some point previously. It will be passed the following arguments:
Each function will be passed two arguments, the ID of the extension and the nsIFile of the directory the extension is installed in.


When an extension is unloaded one of the following functions will be called from the cached sandbox:
;data :A JS object containing basic information about the add-on, <code>id</code>, <code>version</code> and <code>installPath</code>.
;reason :A number representing the reason for installing the add-on. This may be ADDON_UNINSTALL, ADDON_UPGRADE or ADDON_DOWNGRADE.


* <code>uninstall</code> is called if the extension is uninstalled while Firefox is running.
Note that <code>uninstall</code> may be called even when add-ons are disabled or incompatible with the current application. It is important that the <code>uninstall</code> script be carefully written to handle APIs that may no longer be present in the application.
* <code>shutdown</code> is called if Firefox shuts down while the extension is enabled (occurs during quit-application-granted currently).
* <code>disable</code> is called if the user disables the extension.


If the <code>uninstall</code> or <code>shutdown</code> functions do not exist then the <code>disable</code> function will be called in their place.
In some cases <code>uninstall</code> may never be called for an add-on. This happens when a third-party application removes the add-on while Firefox is not running.
After the function has been called all references to the sandbox will be dropped allowing it to be garbage collected unless the extension has failed to unload any part of itself.

Latest revision as of 21:54, 1 November 2010

Note: This page contains preliminary documentation of the feature and is mainly of historical interest. The documentation is at MDC.

Bootstrapped add-ons are a special kind of add-on that developers can choose to use instead of normal add-ons in order to allow users to install and use the add-on without needing to restart the application. In order to do this, rather than the application loading chrome and components from the add-on it is up to the add-on developer to do all of this manually.

All the application will do is execute a bootstrap script that the add-on provides, telling it to startup. The add-on can do whatever it likes at that point however it must undo anything it has done when we call the bootstrap script to tell it to shutdown.

Starting up and Shutting down

A key feature of bootstrapped add-ons is that they must be able to be started and shutdown on demand by the application. When told to startup it is up to the add-on's bootstrap code to do anything necessary to inject UI and behaviour into the application. When told to shutdown it is required that the add-on remove anything it has added and removing all references to its objects. Some examples of when startup and shutdown happen:

  • When first installed (if the add-on is compatible and enabled) the application will tell the add-on to start up.
  • When uninstalled (if the add-on is currently enabled) the application will tell the add-on to shutdown.
  • When enabled or disabled while the application is running the application will tell the add-on to startup or shutdown as appropriate.
  • When the application is started the application will tell any enabled add-ons to startup.
  • When the user quits the application the application will tell any enabled add-ons to shutdown.

Declaring an Add-on as Bootstrappable

Add-ons are declared as being bootstrappable using the special em:bootstrap="true" property in the install manifest. Although any type of add-on can include this, for now it only makes sense for add-ons with em:type="2" (the default extension type) to include it. They should also include a bootstrap.js file alongside the install.rdf.

Backwards Compatibility

Older versions of Firefox do not know about the em:bootstrap flag or bootstrap.js file but with care it is possible to make the same XPI usable in both cases. Older versions would just treat it as a normal add-on requiring a restart to install and uninstall and loading components and chrome from the normal places. Newer versions will ignore the components and chrome and just use the bootstrap.js.

Bootstrap Events

Bootstrapped add-ons must provide a bootstrap.js file which defines main entry functions as defined below. When the application needs to call a function the script will be executed in a privileged sandbox which is cached until the add-on is shutdown.

Startup

Whenever an add-on needs to be started the application will call a function named startup in the bootstrap script. startup may be called at any point during an application run. It will be passed the following arguments:

data
A JS object containing basic information about the add-on, id, version and installPath.
reason
A number representing the reason for starting the add-on. This may be APP_STARTUP, ADDON_ENABLE, ADDON_INSTALL, ADDON_UPGRADE or ADDON_DOWNGRADE.

The add-on must at this point do anything necessary to make its functionality available, the application will not execute any other part of the add-on.

Shutdown

Whenever an add-on needs to be shutdown the application will call a function named shutdown in the bootstrap script. shutdown may be called at any point during an application run. It will be passed the following arguments:

data
A JS object containing basic information about the add-on, id, version and installPath.
reason
A number representing the reason for shutting down the add-on. This may be APP_SHUTDOWN, ADDON_DISABLE, ADDON_UNINSTALL, ADDON_UPGRADE or ADDON_DOWNGRADE.

The add-on must at this point remove anything it has added to the application so its memory can be reclaimed.

Install

The bootstrap script may include an install method and if so it will be called before the first call to startup after a particular version of an add-on is installed. It will be passed the following arguments:

data
A JS object containing basic information about the add-on, id, version and installPath.
reason
A number representing the reason for installing the add-on. This may be ADDON_INSTALL, ADDON_UPGRADE or ADDON_DOWNGRADE.

Note that install will never be called if the add-on has never been started. For example if an add-on is installed but is not compatible then install will never be called if the add-on is uninstalled before it becomes compatible. If on the other hand the add-on becomes compatible at a later point install will be called before the call to startup.

Uninstall

The bootstrap script may include an uninstall method and if so it will be called after the last call to shutdown before a particular version of an add-on is uninstalled. uninstall will only ever be called if install has been called at some point previously. It will be passed the following arguments:

data
A JS object containing basic information about the add-on, id, version and installPath.
reason
A number representing the reason for installing the add-on. This may be ADDON_UNINSTALL, ADDON_UPGRADE or ADDON_DOWNGRADE.

Note that uninstall may be called even when add-ons are disabled or incompatible with the current application. It is important that the uninstall script be carefully written to handle APIs that may no longer be present in the application.

In some cases uninstall may never be called for an add-on. This happens when a third-party application removes the add-on while Firefox is not running.