passing/getting data from/to custom xpcom service

15 views
Skip to first unread message

jonathan

unread,
Mar 13, 2007, 10:50:36 AM3/13/07
to
I currently wish to have a global xpcom service that can get and set a
complex javascript data type.
this data type is defined and used as the following:
var a=new Array();
a['param']='val';
here's my xpcom interface idl file:
#include "nsISupports.idl"

interface nsIMutableArray;
interface nsIPropertyBag;
interface nsIArray;

[scriptable, uuid(45be03e1-7fe3-4467-9ea5-c80d0e2b1234)]
interface nsISD : nsISupports
{
nsIArray getData();
void setData(in nsIArray a);
};

1.what's the right data structure that I can use to pass my custom data(var
a)? can nsIArray do the job?
2. if it's not possible to pass my custom data(var a) around to xpcoms as
it is defined now, what's the suitable data structure that I can use to
redefine and populate my custom data?
3.also I read somewhere that nsIArray should be passed by pointer, so what's
the standard way of defining function signatures? can nsIArray be a return
value?

thanks for the help!

jonathan

unread,
Mar 13, 2007, 7:04:51 PM3/13/07
to
actually the custom object that I need global access to is mroe complicated
than I mentioned, it is something like this:
var a=new Array();
a['param']=new Array();
a['param'].push('val1');
a['param'].push('val2');

"jonathan" <topc...@gmail.com> wrote in message
news:foydnWk8z4qgJ2vY...@mozilla.org...

jonathan

unread,
Mar 13, 2007, 7:18:35 PM3/13/07
to
To make my question more clear I think what I want to know is how do I wrapp
the complex data I described into a xpcom?
thanks

"jonathan" <topc...@gmail.com> wrote in message

news:o72dnawmUrGFs2rY...@mozilla.org...

eric...@yahoo.com

unread,
Mar 13, 2007, 8:26:37 PM3/13/07
to dev-extensions
If the XPCOM service is built with JS, you can use wrappedJSObject on your XPCOM service to pass any kind of data you like; e.g.,

var myService = Components.classes["@jonathan.com/myExtension/myService;1"]
.getService(Components.interfaces.nsISupports).wrappedJSObject;

myService.setData([param1:"foo", param2:"bar"]);

You don't need an IDL for the service in this case. However, if you're writing the XPCOM service with C++, I don't know if you can use wrappedJSObject. Given that it has "JS" in the name, I doubt it :) If that's the case, ignore this email.


_______________________________________________
dev-extensions mailing list
dev-ext...@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-extensions

jonathan

unread,
Mar 13, 2007, 9:11:43 PM3/13/07
to
eric,
can wrappedJSObjet.setData take none arbitrary javascript objects? or just
strings. I still need to define the the getData and setData function along
with rest of the class right?

thanks

-jonathan

<eric...@yahoo.com> wrote in message
news:mailman.1.1173832008...@lists.mozilla.org...

jonathan

unread,
Mar 13, 2007, 9:40:29 PM3/13/07
to
And also how do I register the component so that it can be found if I don't
compile a IDL?
I got a TypeError: Component.class['@mycom.com/myService;1"] has no
properties...
thanks

<eric...@yahoo.com> wrote in message
news:mailman.1.1173832008...@lists.mozilla.org...

eric...@yahoo.com

unread,
Mar 13, 2007, 11:38:27 PM3/13/07
to jonathan, dev-ext...@lists.mozilla.org
>>can wrappedJSObjet.setData take none arbitrary javascript objects? or just strings.<<
It can take anything.

>>I still need to define the the getData and setData function along with rest of the class right?<<

Yes, but in the myService object itself... there's no interface anymore

>>And also how do I register the component so that it can be found if I don't compile a IDL?<<

There are tutorials on this at developer.mozilla.org. Also, Ted has a webpage which auto-generates the XPCOM component service code for you automatically after you fill in a few things (like service name, etc). The URL for the webpage was posted in the last few days on this mailing list.

jonathan

unread,
Mar 14, 2007, 12:23:36 AM3/14/07
to

what do you mean by in the myService object itself?right now this is how I
retrieve the service, right?
var myService = Components.classes["@my.com/myService;1"]
.getService(Components.interfaces.nsISupports).wrappedJSObject;
myService.setData(data);
so do you mean I can just define a javascript class called myService like
this? how does that become a service?

function myService() {
this.wrappedJSObject = this;
}
myService.prototype = {
data:null,
getData:function(){
return this.data;
},
setData: function(d){
this.data=d;
},
}

>>I still need to define the the getData and setData function along with
>>rest of the class right?<<
>Yes, but in the myService object itself... there's no interface anymore

all the tutorial I went through said I need to compile an IDL in order to
create the xpcom component. is this correct?


>>And also how do I register the component so that it can be found if I
>>don't compile a IDL?<<
>There are tutorials on this at developer.mozilla.org. Also, Ted has a

>webpage which auto->generates the XPCOM component service code for you

>automatically after you fill in a few things >(like service name, etc). The
>URL for the webpage was posted in the last few days on this mailing >list.

thanks!

Nickolay Ponomarev

unread,
Mar 15, 2007, 4:23:43 AM3/15/07
to jonathan, dev-ext...@lists.mozilla.org
On 3/14/07, jonathan <topc...@gmail.com> wrote:
>
> what do you mean by in the myService object itself?right now this is how I
> retrieve the service, right?
> var myService = Components.classes["@my.com/myService;1"]
> .getService(Components.interfaces.nsISupports).wrappedJSObject;

No need to pass Components.interfaces.nsISupports explicitly to
getService. Just use .getService().

> myService.setData(data);
> so do you mean I can just define a javascript class called myService like
> this?

Yes.

> how does that become a service?
>

Just like a component implementing interfaces does. You put this code
in a JS file along with XPCOM registration code, which the above
mentioned Ted's tool can generate for you. Then you put the JS file in
components/

http://ted.mielczarek.org/code/mozilla/jscomponentwiz/

> function myService() {
> this.wrappedJSObject = this;
> }
> myService.prototype = {
> data:null,
> getData:function(){
> return this.data;
> },
> setData: function(d){
> this.data=d;
> },
> }
>

BTW, no-one forces you to use the setData/getData functions. You can
as well set the property directly.

> >>I still need to define the the getData and setData function along with
> >>rest of the class right?<<
> >Yes, but in the myService object itself... there's no interface anymore
> all the tutorial I went through said I need to compile an IDL in order to
> create the xpcom component. is this correct?

It is incorrect. The simplest counter-example is that your component
may only implement existing interfaces, like nsISupports and
nsIObserver. Or just nsISupports, which is what Eric suggested you do.

Which tutorial is that? Please always post the URLs, so that we can
fix the confusing docs (if they are under our control).

Nickolay

Reply all
Reply to author
Forward
0 new messages