Fennec Native UI 101

Margaret Leibovic
April 25, 2012

High-Level UI Overview

Java Bits

JS Bits (browser.js)

Message Passing

Java to JS

Create a GeckoEvent in Java:

GeckoAppShell.sendEventToGecko(
  GeckoEvent.createBroadcastEvent("MessageToJS", "data"));

Add an observer in JS:

Services.obs.addObserver(observer, "MessageToJS", false);

Your listener just needs to implement nsIObserver

JS to Java

Create a JSON message in JS:

let message = {
  gecko: {
    type: "MessageToJava",
    ...
  }
};
sendMessageToJava(message);

Register a listener in Java:

GeckoAppShell.registerGeckoEventListener("MessageToJava", listener);

Your listener just needs to implement GeckoEventListener:

public interface GeckoEventListener {
    public void handleMessage(String event, JSONObject message);
}

Want to learn more?