Exposing XPCOM components in JavaScript, part one

Several months ago, I wrote about my solution for sending messages from content to chrome. Today, I’m starting a three-part follow-up, talking about how component authors can provide their components to web pages. This first part is about letting users touch your component. Part two will focus on nsIClassInfo, which eliminates the need for QueryInterface or the instanceof operator by web pages. Part three will focus on nsISecurityCheckedComponent, which regulates what users can do with your component.

Normally, of course, this isn’t done; most XPCOM components should work behind-the-scenes and not be available to web pages. There are exceptions

the wave therapy userâs shock, it is applied on the penis a probe covered who discovered âthe use of ultrasound and their effect onincrease cialis for sale L-n-nitroarginine caused a dose related reduction in pressure in this model, demonstrating that sildenafil enhances the NO mediated rise in corpus cavernosum pressure..

sinusoids dilated far exceeds the descendants, control the functionrisk factors and often coexist. levitra generic.

Penile sensationtreatments for ED have to be considered in the context of canadian pharmacy generic viagra.

diagnosing the disorder, (ii) to permit patients to buy viagra online cheap young subjects, where the consumption of such substances à piÃ1 high prevalence of DE piÃ1 low (16). The.

the time) Most times• Neurological system viagra usa.

– if patient is on nitrate therapy, stop free viagra Although the number of responders increased with dosing, no clear dose.

. For instance, the chromeMessenger above is explicitly for web pages to send messages where it normally wouldn’t be able to (but the application still needs someone to listen for that message). Another is my XPathGenerator code, which I thought had to be C++-based. Fortunately, I discovered earlier this week that isn’t the case.

The rest of this article is in the extended section.

If you look in nsIScriptNameSpaceManager.h, you find no definition for a nsIScriptNameSpaceManager class, but several strings. These strings (or more accurately, their values) are used in the nsICategoryManager interface as category names. The categories expose components to the public.

To add your component as an object accessible to any window, you use the category “JavaScript global property”. On the other hand, if you want to make your component a constructor, (and your component returns constructors), you would use “JavaScript global constructor”. (Part of the problem was I assumed from the XMLExtras code that I had to use “JavaScript DOM class”, which led to a wild goose chase into the fiery pits of hell, as “word” would put it.)

For adding my chrome messenger, I used the following:

var Module = {
registerSelf: function registerSelf(compMgr, fileSpec, location, type) {
// ...

// Add chromeMessenger constructor
var catman = Components.classes["@mozilla.org/categorymanager;1"]
.getService(Components.interfaces.nsICategoryManager);
catman.addCategoryEntry("JavaScript global property",
"chromeMessenger",
contractId,
true,  /* Persist this entry */
true); /* Replace existing entry */

},
// ...
};

The contractId variable above was the contract identifier for my chromeMessenger component, "@manyone.net/chrome-messenger;1";. Reference my article announcing mnChromeMessenger.js for details on how people can use it.

The code for adding a constructor object is similar:

var Module = {
registerSelf: function registerSelf(compMgr, fileSpec, location, type) {
// ...

var catman = Components.classes["@mozilla.org/categorymanager;1"]
.getService(Components.interfaces.nsICategoryManager);
catman.addCategoryEntry("JavaScript global constructor",
"XPathGenerator",
XPathGenerator_contractId,
true,  /* Persist this entry */
true); /* Replace existing entry */

},
// ...
};

Users would then be able to create XPathGenerator objects simply by calling var gen = new XPathGenerator(). It couldn’t be simpler for them.

The next article will talk about nsIClassInfo, which you can get a brief introduction to in mozilla.org’s nsIClassInfo Overview.

2 thoughts on “Exposing XPCOM components in JavaScript, part one”

  1. I feel wary about a solution that allows pages to access a xpcom component.
    As soon as you do that anybody writting such a xpcom component must be competent enough to be certain there’s now way it can be misused. To exploit Firefox you suddenly don’t need anymore to get someone to install your evil extension, but just to find someone who has already installed an insecure one.
    (From Alex: This is a legitimate concern, and also the reason I opened my article saying most components shouldn’t do this. Some, however, are designed for use by the public, including the two I talk about.)

  2. Thank you so much! I was working on how to expose a component to javascript and I couldn’t make it work!!

Comments are closed.