Sometimes it's necessary for XPCOM components to receive notifications as to the progress of the application's startup process, so they can start new services at appropriate times, for example.

Receiving startup notifications in Gecko 2.0 (Firefox 4) and later

In order to improve startup times, changes were made to the XPCOM startup process. See The startup process for details on how this now works, if you're interested in specifics.

The important thing to note is that now instead of registering with the Category Manager programmatically as was done in the past, you add lines to your chrome.manifest to let the application handle it for you. For example:

category profile-after-change MyComponent @foobar/mycomponent;1
Important: In the past, the contract ID of the category entry started with "service," if the component was implemented as a service. This is no longer the case, so be sure to remove that when migrating existing code.

Additionally, the earliest startup time notification you can receive is now profile-after-change. Your add-on will not receive xpcom-startup or app-startup notifications.

The startup process

During application startup, the application's manifest is used to get a list of the components it needs to register, and those components are loaded. This gets enough of XPCOM and the application loaded and running that the Extension Manager can then be loaded and handle installing, uninstalling, and updating any installed extensions.

Once that process is completed, extensions can then be loaded by simply reading their manifests, loading their components, and continuing with application startup, all without having to restart the browser.

Receiving startup notifications prior to Gecko 2.0 (Firefox 4)

To receive startup notifications, one needs to register with the "app-startup" category using nsICategoryManager; having done so, the component will receive these startup notifications, including:

xpcom-startup
Sent when XPCOM finishes starting up. Most application features are not yet available at this point, but XPCOM itself is.
app-startup
Sent when the application has finished starting up.
final-ui-startup
Sent just before the first application window is displayed.

Registering with the Category Manager

To register with the Category Manager, simply call its nsICategoryManager.AddCategoryEntry() method:

categoryManager->AddCategoryEntry(APPSTARTUP_CATEGORY,
                                  "mycomponentname",
                                  "contract-id",
                                  PR_TRUE, PR_TRUE,
                                  getter_Copies(previous));

This causes your component to be instantiated using nsIComponentManager.createInstance().

If you want your component to be started as a service, prepend "service," to the contract ID:

categoryManager->AddCategoryEntry(APPSTARTUP_CATEGORY,
                                  "mycomponentname",
                                  "service,contract-id",
                                  PR_TRUE, PR_TRUE,
                                  getter_Copies(previous));

With "service," specified, the component is instantiated using nsIComponentManager.getService().

In either case, there's no need to additionally register for the startup notifications. Simply registering with the Category Manager is enough.

What happens next

Once you've registered with the Category Manager, at Mozilla startup time (or when the embedding application's NS_InitEmbedding() function is called), the AppStartupNotifier component is instantiated, and its Observe() method is called; this in turn enumerates all components in the app-startup category and sends them the appropriate notifications.

See also