Fennec/NativeUI/Messages

From MozillaWiki
Jump to: navigation, search

Gecko -> Java

let message = {
  type: "MyMessage",
  title: "Cool Thing",
  rating: aSomeValue
};

sendMessageToJava(message);

The message object is serialized into JSON, sent to Java, and received at handleGeckoMessage(). The particular activity in need of events need to register and unregister for the required events. GeckoApp.java's onCreate() and onDestroy() are good examples.

Java -> Gecko

From java we can post message that can be recieved by the nsIObserver. Subject and Data can be anything here:

// JAVA:
GeckoEvent e = new GeckoEvent("subject", "data");
GeckoAppShell.sendEventToGecko(e);

any registered nsIObserver can see this:

// Javascript

let SomeEventListener = {

  init: function() {
    Services.obs.addObserver(this, "subject", false);
  },

  observe: function(aSubject, aTopic, aData) {

      if (aTopic == "subject") {
         // data could be anything, think json.    
      }
  },
};