Fennec/NativeUI/Messages

From MozillaWiki
< Fennec‎ | NativeUI
Revision as of 19:58, 17 October 2011 by Dolske (talk | contribs) (→‎Gecko -> Java: clarify Gecko-to-Java message passing)
Jump to navigation Jump to search

Gecko -> Java

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

sendMessageToJava(message);

The message object is serialized into JSON, sent to Java, and then deserialized in handleGeckoMessage(). Right now, it is just a big switch that dispatches to the right handler based on the (required) "type" property.

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.    
      }
  },
};