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;1Important: 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
or xpcom-startup
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论