Firefox for Android
Margaret Leibovic @mleibovic
JSConf EU, Berlin, 6 Oct 2012
Margaret Leibovic @mleibovic
JSConf EU, Berlin, 6 Oct 2012
Mozilla's mission is to promote openness, innovation and opportunity on the web.
Create a GeckoEvent in Java:
GeckoEvent e = GeckoEvent.createBroadcastEvent("my-event", "data"); GeckoAppShell.sendEventToGecko(e);
Add an observer in JS:
Services.obs.addObserver(observer, "my-event", false); function observer(aSubject, aTopic, aData) { // Do some stuff };
Create a JSON message in JS:
let message = { gecko: { type: "another-event", ... } }; sendMessageToJava(message);
Register a listener in Java:
GeckoAppShell.registerGeckoEventListener("another-event", listener); GeckoEventListener listener = new GeckoEventListener() { public void handleMessage(String event, JSONObject message) { // Do some stuff } }
In Java, use an Android API to listen for a back button press:
@Override public void onBackPressed() { GeckoEvent e = GeckoEvent.createBroadcastEvent("Session:Back", ""); GeckoAppShell.sendEventToGecko(e); }
In JS, use a Gecko API to navigate back in session history:
Services.obs.addObserver(handleBackPressed, "Session:Back", false); function handleBackPressed(aSubject, aTopic, aData) { BrowserApp.selectedBrowser.goBack(); };
Listen for DOMTitleChanged events in JS, and pass them along to Java:
Tab.prototype = { create: function(...) { this.browser.addEventListener("DOMTitleChanged", this, true); }, handleEvent: function(aEvent) { let message = { gecko: { type: "DOMTitleChanged", tabID: this.id, title: aEvent.target.title } } sendMessageToJava(message); } }
Update the tab's title in the native Java UI:
GeckoAppShell.registerGeckoEventListener("DOMTitleChanged", listener); GeckoEventListener listener = new GeckoEventListener() { public void handleMessage(String event, JSONObject message) { int tabId = message.getInt("tabID"); Tab tab = Tabs.getInstance().getTab(tabId); String title = message.getString("title"); tab.updateTitle(title); } }
install.rdf
bootstrap.js
bootstrap.js
boilerplate, we just need to implement two functions
loadIntoWindow
intializes the add-on
unloadFromWindow
cleans up any changes we made
var menuId; function loadIntoWindow(window) { function viewSource() { let url = window.content.location.href; window.BrowserApp.addTab("view-source:" + url); } let [label, icon, cb] = ["View Source", null, viewSource]; menuId = window.NativeWindow.menu.add(label, icon, cb); } function unloadFromWindow(window) { window.NativeWindow.menu.remove(menuId); }
margaret@mozilla.com
http://margaretleibovic.com/talks/jsconf/firefox-for-android.html