JS XPCOM

Here are a few useful snippets of code for dealing with XPCOM components in JavaScript.

Contract IDs

A contract ID is a unique name for an XPCOM object. They are used to create or access well-known objects in XPCOM.

Interfaces

Every XPCOM object implements one or more interfaces. An interface is simply a list of constants and methods that can be called on the object, an example is nsIFile. Every XPCOM object must implement the nsISupports interface.

Accessing XPCOM components from JavaScript

XPCOM objects are either created as new instances (each creation gives you a completely new COM object) or as services (each access gives you the same COM object, often called a singleton). Whether you must create a new instance or access as a service depends on the contract. In order to get an XPCOM object you need to know the contract ID of the object and the interface that you wish to use on it.

Creating an instance of a component

The preferred method of creating XPCOM instances is via the Components.Constructor helper. For example,

var nsFile = Components.Constructor("@mozilla.org/file/local;1", "nsIFile", "initWithPath");

var file = new nsFile(filePath);

They can also be created and initialized manually:

var file = Components.classes["@mozilla.org/file/local;1"]
                     .createInstance(Components.interfaces.nsIFile);
file.initWithPath(filePath);

This creates a new instance of the object with contract ID @mozilla.org/file/local;1 and allows you to call methods from the nsIFile interface on it.

Getting an XPCOM service

var preferences = Components.classes["@mozilla.org/preferences-service;1"]
                            .getService(Components.interfaces.nsIPrefService);

You can then call any methods in the nsIPrefService interface on the preferences object.

Getting a different interface for a component

Some components implement more than one interface. Sometimes JavaScript is clever enough to know all the interfaces available on a component, but in most cases you will have to explicitly check for an interface. With the preferences service from the previous example we can do the following:

var preferences = preferences.QueryInterface(Components.interfaces.nsIPrefBranch2);

This allows you to use the methods in the nsIPrefBranch2 interface.

Determining which interfaces an XPCOM component supports

To display a list of all interfaces that an XPCOM component supports, do the following:

// |c| is the XPCOM component instance
for each (i in Components.interfaces) { if (c instanceof i) { alert(i); } }

In this context, instanceof is the same as QueryInterface except that it returns false instead of throwing an exception when |c| doesn't support interface |i|. Another difference is that QueryInterface returns an object, where as instanceof returns a boolean.

XPCOMUtils - About protocol handler

This example implements a quick about protocol handler in JS using XPCOMUtils.jsm.

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
const Cc = Components.classes;
const Ci = Components.interfaces;

function AboutHandler() {}
AboutHandler.prototype = {
    newChannel: function(uri) {
        var channel = Services.io.newChannel("chrome://mystuff/content/mystuff.xul", null, null);
        channel.originalURI = uri;
        return channel;
    },
    getURIFlags: function(uri) {
        // Do NOT return Ci.nsIAboutModule.URI_SAFE_FOR_UNTRUSTED_CONTENT unless
        // you make sure to set a non-system principal in newChannel.
        return 0;
    },

    classDescription: "About MyStuff Page",
    classID: Components.ID("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"),
    contractID: "@mozilla.org/network/protocol/about;1?what=mystuff",
    QueryInterface: XPCOMUtils.generateQI([Ci.nsIAboutModule])
}

var NSGetModule = XPCOMUtils.generateNSGetModule([AboutHandler]);
}