Native JS objects in event handling

Brian Nicholson bnicholson at mozilla.com
Tue Apr 8 13:46:50 PDT 2014


> * 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) {
>>         // ...
>>     }
>> }

As of bug 993195, the method signature now looks like this:

public void handleMessage(String event, NativeJSObject message,
EventCallback callback) {...}

For messages sent from Gecko with a callback included (the second
argument in sendMessageToJava), the EventCallback passed by
handleMessage is the interface used to send a response. If the
originating sendMessageToJava was not given a callback, the
EventCallback object given to handleMessage will be null.

Similar to the previous EventDispatcher implementation, your options
are `callback.sendSuccess(object)` or `callback.sendError(object)`.
Also like the previous implementation (even though this wasn't
explicitly mentioned): only one of sendSuccess/sendError can be
called, and it must be called exactly once to prevent observer leaks.
Note that the same callback object is shared for all Java-side
listeners for a given message, so that also means that exactly one
listener must make the sendSuccess/sendError call.

Brian


More information about the mobile-firefox-dev mailing list