Page 6 of 6
If you are writing an extension for FireFox then you might never have to get involved in XPCOM because you can do a lot just by interacting and modifying the user interface. However, with Thunderbird in particular there often comes a time when you want to work with the raw data in or some non-user interface service. In this case you need to learn and master XPCOM. Essentially this provides you with access to the internal workings of FireFox and Thunderbird. The XPCOM components are identified by their "contract IDs" which in this case all start with @mozilla.org. To create a component you use the Components object as in:
If the component implements a standard service then you shouldn't create a new instance but use the existing one:
Once you have a reference to an XPCOM object you can ask it to return a reference to any of the, possibly many, Interfaces it supports using:
As an example of using XPCOM let's look at the "prompt service". Although we used an alert window to popup the "hello world" message this isn't the proper way to do the job. Extensions shouldn't use alerts because they are non-privileged HTML content code and using them provides a potential security loophole. Instead it we should use the XPCOM prompt service. First we get a reference to the currently running prompt service:
var PromptService = Components.classes[
Next we ask the prompt service to return a reference to the nsIPromptService interface:
Now PromptService is a reference to a promptService interface or object and we can use the alert method to popup a window with the appropriate message:
"Hello Extension","Hello World");
If you add this to the end of the onMenuCommand method in the JS file then when you select the menu item you will see an additional alert style dialog box popup.
Using other XPCOM components follows the same pattern - create or obtain a reference to the object, use the QueryInterface method to obtain the Interface you want to use, and use the methods it provides. There are XPCOM components that let you access the email database, make a sound, work with files and access preferences, passwords and so on. Just look them up in the reference section at:
You might find the review of Programming Firefox: Building Rich Internet Applications with XUL of interest.