Hi all,

Bug 989046 has landed in fx-team, which means you should start using
native JS objects when handling Gecko-to-Java events. We will also be
gradually converting existing event handlers from using JSON to using
the native object. Here are the changes in particular,


* To send events in JS, make sure you use Messaging.jsm. For example,

> Components.utils.import("resource://gre/modules/Messaging.jsm");

> Messaging.sendMessageToJava({type: "Hello:World"});


* To receive native events in Java, you need to implement
NativeEventListener instead of GeckoEventListener. NativeEventListener
has the same handleMessage method with a NativeJSObject argument instead
of a JSONObject argument. For example,

> import org.mozilla.gecko.util.NativeEventListener;
> import org.mozilla.gecko.util.NativeJSObject;

> class EventConsumer implements NativeEventListener
> {
>     @Override
>     public void handleMessage(String event, NativeJSObject message) {
>         // ...
>     }
> }

You can implement both NativeEventListener and GeckoEventListener in the
same class.


* EventDispatcher.registerEventListener() is now deprecated; use
EventDispatcher.registerGeckoThreadListener() instead. The new method
has a different signature than before,

> void registerGeckoThreadListener(NativeJSObject listener, String... events)

With the new method you can register multiple events at the same time
instead of registering separately. For example,

> GeckoAppShell.getEventDispatcher().registerGeckoThreadListener(this, 
> "Event:Foo", "Event:Bar");

However, once an event is registered with native listeners, it cannot be
registered with JSON listeners anymore. You will get an exception when
you try to register both types for the same event.

Unregistering works similarly,

> GeckoAppShell.getEventDispatcher().unregisterGeckoThreadListener(this, 
> "Event:Foo", "Event:Bar");


* NativeJSObject has a very similar API as JSONObject, so switching to
NativeJSObject should be relatively straightforward. Most of the usual
get* and opt* methods are available, with the exception of array support
(bug 992357). getObject() replaces getJSONObject(). Also, the opt*
methods always take two arguments.

Make sure your get* method matches the type of the value. A type
mismatch will throw an exception (e.g. use getInt() on a floating point
value).


* One caveat when using NativeJSObject is that it can only be used on
the Gecko thread. If you need its data outside of the Gecko thread,
please fetch the data from the object and store the data elsewhere.
Better support for other threads will be added in bug 992359.


Let me know if you have questions/concerns!

Cheers,
Jim
_______________________________________________
mobile-firefox-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/mobile-firefox-dev

Reply via email to