Extending Firefox and Thunderbird
Written by Mike James   
Saturday, 20 June 2009
Article Index
Extending Firefox and Thunderbird
Getting started
Hello Toolbar
Trying it out
Debugging
XPCOM


XPCOM

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:

Var MyXPCOM=Components.Classes[
"@mozilla.org/componentname].
createInstance();

If the component implements a standard service then you shouldn't create a new instance but use the existing one:

Var MyXPCOM=Components.Classes[
"@mozilla.org/componentname].
getService();

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:

MyXPCOM.QueryInterface(
Components.interfaces.requiredinterface);

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[
"@mozilla.org/embedcomp/prompt-service;1"]
.getService()

Next we ask the prompt service to return a reference to the nsIPromptService interface:

PromptService.QueryInterface.(
Components.interfaces.nsIPromptService);

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:

PromptService.alert(null, 
"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:

www.xulplanet.com

From here you should be able to develop extensions to do whatever you want. All it takes is a little patience searching the web to discover the bits of information that are available and then making use of the DOM Inspector and the JavaScript debugger to find out what isn't easily discovered in the documentation or completely undocumented.

You might find the review of Programming Firefox: Building Rich Internet Applications with XUL of interest.

<ASIN:0596102437>

 

 



Last Updated ( Saturday, 27 June 2009 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.