Receiving startup notifications 编辑

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:73 次

字数:5972

最后编辑:6年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文