Mozilla + Firefox extension development

Thursday, November 25, 2004

Passing objects between a Javascript-based XPCOM component and its client

XPCOM IDL allows you to pass parameters of certain types only (e.g., string, integer), in addition to objects with XPCOM interfaces (e.g., nsISupports). In order to pass anything else (e.g., a Javascript array, a Java object) from the client of a Javascript-based XPCOM component into the component, you need to wrap the object to be passed in another XPCOM object implemented using Javascript:

function WrapperClass(object) {
this.wrappedJSObject = this;
this.object = object;
}

WrapperClass.prototype = {
QueryInterface : function(iid) {
if (!iid.equals(Components.interfaces.nsISupports)) {
throw Components.results.NS_ERROR_NO_INTERFACE;
}
return this;
}
}

Now you can call the first XPCOM component passing in any kind of object by wrapping it first:
theComponent.foo(new WrapperClass(theObject))
And inside foo(), the wrapped object can be retrieved as follows:
foo : function(arg) {
var theActualObject = arg.wrappedJSObject.object;
}
In the interface definition of theComponent (in your IDL file), you need to declare that foo() takes an nsISupports parameter.

1 Comments:

  • I've set up an experimental wiki for this kind of info-

    http://oddwiki.taoriver.net/wiki.pl/MozillaJavascript/HomePage

    I'm not sure if it'll be useful, or if it'll catch anybody, but who knows?

    I at least intend to park my notes there. I'm astonished at how little useful info is out there on the web, re: Javascripting Mozilla.

    -- Lion Kimbro

    By Anonymous Anonymous, at 5:36 PM  

Post a Comment

<< Home