Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

XPCOM Component Written in Java

86 views
Skip to first unread message

Derrick Rice

unread,
Jan 23, 2009, 5:16:04 PM1/23/09
to
I've also posted this to the mozswing mailing list, since that's what
I'm using. But this seems to be a core XPCOM problem I'm having.

Let me just say that this I am not looking to start a VM from
mozilla... my entire application is java, with mozilla used via JNI
(mozswing/JavaXPCOM). What I want to know is why my components cannot
be directly access by the javascript, yet they seem to interact in
other ways.

------------------------------------------

What is so confusing is that I see the javascript
successfully interacting with the java, but as soon as I attempt to
explicitly call a function, it falls.

Goal: Create a new XPCOM component which will be written in Java and
installed in my mozswing application. Use it from javascript.

Steps and results:

1. Create a new interface idl and compile it into an *.xpt and java
interface. [OK]
2. On mozswing initialization, install a factory for this interface
that will generate java objects. This is no different than how I've
installed java factories for other already existing interfaces.[OK]
3. After installing the factory, add the component to the "JavaScript
global property" category, so that it is accessible to website
javascript. [OK]
4. Since the interface extends nsIClassInfo and my component (which I
generate in the factory) can also queryInterface to
nsISecurityCheckedComponent, I can allow it to be used from the web
page.

*** At this point I can successfully create and invoke my component
through XPCOM with JAVA:
XPCOMUtils.create("<contract id>", myInterface.class).callFunction()

5. Generate a test HTML page with javascript that accesses the item
[This is where things go bad]

var instance = window.myComponent.QueryInterface
(Components.interface.nsIMyNewInterface);
alert(instance); // yields [xpconnect wrapped
nsIMyNewInterface]
alert(instance.member); // yields the value of the member
alert(instance.callFunction); // yields function
callFunction() { [native code] }
instance.callFunction(); // generates an error, see below

[Exception... "Component returned failure code: 0x80004001
(NS_ERROR_NOT_IMPLEMENTED) [nsIMyNewInterface.callFunction]"
nsresult: "0x80004001 (NS_ERROR_NOT_IMPLEMENTED)" location: "JS frame
:: webpage.html :: run :: line 15" data: no]

If I throw print statements into my java component, I see it being
access (QueryInterface is called and so are the
nsISecurityCheckedComponent functions). This indicates to me that
it's clearly possible for the javascript to communicate with the java.
It's already taking place when I call QueryInterface. But if I try
to call something else, it fails.

Am I running into a natural limitation of JavaXPCOM? How can I
communicate back to my existing java application from javascript
XPCOM?

Problems solved:
* accessing XPCOM from javascript
* installing XPCOM factories from java
* making components visible and accessible to webpages

Problems existing:
* using my components from javascript

I saw someone else had this problem but saw no solution:
http://osdir.com/ml/mozilla.devel.java/2006-01/msg00007.html

Again, any help here would be seriously appreciated. Even if it's
just a little insight.

Alberto

unread,
Jan 26, 2009, 9:41:39 AM1/26/09
to


Even if i'm some step behind you i guess I could probably help you.
I succeded (from java,not javascript yet) to register the XPcom object
created implementing the interface and to access its method.
What I'm still not able to do is register the XPcom object
independently from running the java code,either in a dynamic way or in
a static one.
Since I guess you succeeded in doing that can you help me out?
Thanks in advance

Derrick Rice

unread,
Jan 26, 2009, 11:11:14 AM1/26/09
to

I don't know if I 100% understand your question, but I'll give 2
answers, because I think 1 of them might be what you're looking for.

1) If your intention is to run mozilla embedded in a java application,
then you can register the java component in java code. It sounds like
this is what you've been able to do. This is done by installing a
factory. This is my mozswing code... I don't know if any of it is
specific to mozswing or is just javaxpcom (which mozswing is built on
top of)

ObjectFactory factory = new ObjectFactory(); // implements nsIFactory
XPCOMUtils.register(myContractID, factory); // this calls
nsIComponentRegistrar.registerFactory for me

2) What it sounds like you want to do, though, is register a java
component without having to originally run mozilla in java. That is
"independently" from java, as you put it. My understanding is that to
do this, you need to use LiveConnect[1]. Read this page about 'Java
in Firefox Extensions' [2].

The difference is that in 2) I don't think you have a single JVM. I
think a JVM is created on demand and kept open for your extension.
Other extensions might not use the same JVM. And if you ran mozilla
embedded in Java, you would probably still create a new JVM. Not at
all what I'm after.

[1]https://developer.mozilla.org/en/LiveConnect
[2]https://developer.mozilla.org/en/Java_in_Firefox_Extensions


For reference, what I followed was the instructions in these 3 posts,
but adapted it for Java.

"Exposing XPCOM Components in Javascript"
http://weblogs.mozillazine.org/weirdal/archives/017188.html
http://weblogs.mozillazine.org/weirdal/archives/017202.html
http://weblogs.mozillazine.org/weirdal/archives/017211.html


If I didn't answer your question, please do clarify and I'll try to
help.

Derrick Rice

unread,
Jan 26, 2009, 11:19:24 AM1/26/09
to
> "Exposing XPCOM Components in Javascript"http://weblogs.mozillazine.org/weirdal/archives/017188.htmlhttp://weblogs.mozillazine.org/weirdal/archives/017202.htmlhttp://weblogs.mozillazine.org/weirdal/archives/017211.html

>
> If I didn't answer your question, please do clarify and I'll try to
> help.

and for clarity, I can do the following:

* write an xpcom component in java, and access it from java
* write an xpcom component in javascript, and access it from
javascript
* write an xpcom component in javascript, and access it from java

I CANNOT

* write an xpcom component in java, and access it from javascript.

Alberto

unread,
Jan 26, 2009, 11:50:58 AM1/26/09
to
> > "Exposing XPCOM Components in Javascript"http://weblogs.mozillazine.org/weirdal/archives/017188.htmlhttp://web...

>
> > If I didn't answer your question, please do clarify and I'll try to
> > help.
>
> and for clarity, I can do the following:
>
> * write an xpcom component in java, and access it from java
> * write an xpcom component in javascript, and access it from
> javascript
> * write an xpcom component in javascript, and access it from java
>
> I CANNOT
>
> * write an xpcom component in java, and access it from javascript.

This answer perfectly to what I was looking for. I red a lot of post
about javaXpcom but in no one of them I could find an answer to the
question if was already possible to write an xpcom component in java
following the same procedure you would follow writing it either in c++
or javascript.
I guess this is not possible yet,so i'll take a look to the
liveconnect strategy.
Thanks a lot to sort my doubts out.

Alberto

unread,
Jan 26, 2009, 12:00:42 PM1/26/09
to

I have another question: by any chance will the javaXpcom reach the
same functionalities offered by his c++ brother? It will be possible
sooner or later,register full Java xpcom objects and access them
through javascript?
Thanks in advance

g.j.b...@googlemail.com

unread,
Jan 26, 2009, 1:18:31 PM1/26/09
to
> > "Exposing XPCOM Components in Javascript"http://weblogs.mozillazine.org/weirdal/archives/017188.htmlhttp://web...

>
> > If I didn't answer your question, please do clarify and I'll try to
> > help.
>
> and for clarity, I can do the following:
>
> * write an xpcom component in java, and access it from java
> * write an xpcom component in javascript, and access it from
> javascript
> * write an xpcom component in javascript, and access it from java
>
> I CANNOT
>
> * write an xpcom component in java, and access it from javascript.

I am assuming that you are using the java embedding, the fact that you
can see it calling the QueryInterface is a good sign that you are
almost there, chances are there is a small mistake stopping the JS
talking to the java.

Reading through your message you appear to be attempting to access the
XPCom component from a webpage. I am pretty sure what you are
experiencing is not so much a problem with Java but rather the XPCom
security subsystem getting in the way, this is done on purpose to stop
webpages from being able to use arbitrary XPCom interfaces (imagine
the fun if say all the Local File stuff could be used by someone
malicious).

Can you try getting / using the component from a Xul page ?

You will probably want to look at the docs for XPConnect (XPConnect is
the JS XPCom binding glue docs are here https://developer.mozilla.org/en/XPConnect)
particularly those about XPConnect wrappers (here
https://developer.mozilla.org/en/XPConnect_wrappers)

Derrick Rice

unread,
Jan 26, 2009, 2:02:03 PM1/26/09
to
> the JS XPCom binding glue docs are herehttps://developer.mozilla.org/en/XPConnect)

> particularly those about XPConnect wrappers (herehttps://developer.mozilla.org/en/XPConnect_wrappers)

Hmmm, some of my updates aren't getting posted here. Maybe I'm
replying wrong, but I'm trying again. Sorry if this spams.

Yes, I'm using java embedded mozilla.

I'll try that, but I'm skeptical. I don't have any trouble when I
implement in javascript with what should be the same logic. I only
have trouble when it's implemented in Java.

Also, I just noticed: "wrapJavaObject" in the JavaXPCOM code base. Do
I need to call this explicitly? It supposedly returns a C++ pointer
for my Java object, which XPConnect might be able to handle more
appropriately?

Brett Zamir

unread,
Jan 28, 2009, 12:55:22 AM1/28/09
to Derrick Rice, dev-te...@lists.mozilla.org
Sorry if this doesn't pertain to your needs (and my own knowledge of Java is quite limited), but for anyone confused about using LiveConnect in Mozilla, this thread was very helpful, as an apparent developer working on LiveConnect answered questions about its status. Basically, we have to wait for update 12 to be released (in Q1) to fix a significant bug for loading classes.


Brett

_______________________________________________
dev-tech-java mailing list
dev-te...@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-tech-java

Derrick Rice

unread,
Jan 28, 2009, 9:57:45 AM1/28/09
to
> dev-tech-j...@lists.mozilla.orghttps://lists.mozilla.org/listinfo/dev-tech-java

FYI Adding my interface into the Mozilla-Interfaces.jar does not fix
this. I'm going to try using my component from xulrunner though. If
anyone has some real brain-dead examples I could use, that'd be
helpful, as I haven't written any xul stuff before (and likely won't
again).

Alberto

unread,
Mar 5, 2009, 8:41:49 AM3/5/09
to
Hi all, i probably have been a bit misunderstood, so i'll try to
explain what i'm trying to do.
I already wrote a firefox extension using the LiveConnect work around
(as suggested by https://developer.mozilla.org/en/Java_in_Firefox_Extensions).
But LiveConnect has some unsolved issues (such as it's not working
properly on mac osx) and it's gonna not to be supported anymore, so i
was trying to figure out if there was a way (searching the web i
already discovered that it's not gonna be an easy one!!) to implement
an XPCom completely in java so that it would be accessible from XUL/
Javascript framework running either on firefox or on xulrunner itself.
AFAICS the big problem is the absense of the Java Component Loader (as
the project maintainer Pedemonte said in http://markmail.org/message/vxlaqmdmx2pk3to4)
so that the registration of a component written in java seems to be
impossible, and it seems that the official development driven by
Pedemonte stopped 2 years ago...
Did anyone try to download the sources of the project and to build it?
Which are the issues still uncovered? Is there anybody but Pedemonte
working on this component? If no solution have been found yet where I
can find the sourcecode so that i could try to fix/update it on my
own?

Brett Zamir

unread,
Mar 5, 2009, 9:09:18 AM3/5/09
to Alberto, dev-te...@lists.mozilla.org
Hopefully someone can answer your other question, but LiveConnect IS
going to be supported (through the generic plugin architecture, NPAPI,
along with the Java plugin, not through the old Mozilla code), and IS
already supported with extension code as well as applets--but the user
must now run Java 1.6 update 12+ (and when Firefox 3.1 is released, I
understand the old Mozilla code may be removed, so those using older
versions of Java cannot be accommodated then). But the API is identical
to how it was before.

Another bonus is that the new LiveConnect fixes a number of very
troublesome bugs that crept into LiveConnect over time: try-catch not
working with Java exceptions and problems with array conversion and
java.lang.String() are now working smoothly. I did notice one small
problem when attempting to use the 'java' global from a sidebar--it
wasn't available even after waiting for a load event, until I gave it a
little delay.

See these links for those interested in LiveConnect, including the forum
thread (which addresses newInstance() not working):
https://jdk6.dev.java.net/plugin2/#LIVECONNECT
https://jdk6.dev.java.net/plugin2/liveconnect/
http://forums.java.net/jive/thread.jspa?threadID=45933&tstart=0

However, you're right about Mac OSX, which is seriously lagging in Java
support.

Brett

0 new messages