GeckoEventResponder is dead

Wesley Johnston wjohnston at mozilla.com
Fri Feb 14 11:10:18 PST 2014


I finally landed patches today to kill off the GeckoEventResponder class in Java. You can no longer synchronously return values from the Java UI to the JS frontend. This is part of a bigger effort to move all of the Java event handling off of the Gecko thread. It also adds some facilities for error handling that isn't used yet, but is available if you want/need it.

=== Details ===

You could previously write code in Javascript that looked something like:

var data = sendMessageToJava({});
// data is a string of response information

These patches remove the return value you could receive from this function. Since sendMesageToJava is something we'd defined in multiples through the tree. I also moved it into its own helper .jsm called Messaging.jsm. It now takes an optional second callback argument. i.e. the above code would be written:

Cu.import("resource://gre/modules/Messaging.jsm");
sendMessageToJava({}, (data, error) => {
  // if successful data is a JSON string of response information. Otherwise data is null
  // if failed, error is a JSON string of response information. Otherwise error is null
});

If you love promises, you can still use them, but you'll have to wrap them yourself. On the Java side, return values are sent using a static sendResponse method in EventDispatcher.java that takes the original message (so that it knows who to respond to), and a second object with data to send back to the caller. i.e. a flow might look like:

public MyClass implements GeckoEventListener {

  @Override
  public void handleMessage(String event, JSONObject message) {
    try {
      JSONObject ret = new JSONObject();
      // add whatever you want to return to the ret object
      EventDispatcher.sendResponse(message, ret);
    } catch(Exception ex) {
      try {
        JSONObject err = new JSONObject();
        // fill in error details
        EventDispatcher.sendError(message, err);
      } catch(Exception ex) { }
    }
  }

}

Note, as part of this I also moved EventDispatcher out of the org.mozilla.gecko.utils package and into the org.mozilla.gecko one. You can (currently) still access and make calls into it through GeckoAppShell.getEventDispatcher() (although I'd like to remove that at some point). There are probably still a few places in the codebase where we've reimplemented asynchronous response handlers. It would be nice to move them all to the new system if can. If you know of anything, please file bugs and cc me.

Thanks,
- Wes


More information about the mobile-firefox-dev mailing list