Hi All, 

I finally got around to working on ways for the Java embedding/host application 
to inject JavaScript into the GeckoView context [1]. Coupled with injecting 
scripts is a mechanism to communicate between the JavaScript in GeckoView and 
the Java in the host application. 

I have a working prototype that looks like this: 

class GeckoView { 
... 
importScript(String url); 
} 

interface GeckoViewChrome { 
... 
onScriptMessage(GeckoView view, JSONObject data, GeckoView.MessageResult 
result); 
} 

The importScript method takes a resource://android/assets/ URL to a JavaScript 
file bundled with the host application. Only scripts in the bundled assets 
folder will be imported. The scripts are imported in almost exactly the same 
way restartless add-on bootstrap.js files are loaded in Firefox. If the script 
has a global "load" function, it is called after the import happens. The "load" 
function is passed a "params" object which has some properties, one of which is 
"window" - the main Gecko window used to host the browsers. 

So a script might look like this: 

function load(params) { 
console.log("Got loaded!"); 

params.window.BrowserApp.deck.addEventListener("load", function(event) { 
console.log("Loaded a web page in a tab!"); 
}, true); 
} 

The scripts can send messages and data back to the Java host application using 
a simple GeckoView (name and features subject to change) helper: 

function load(params) { 
function callback(response) { ... }; 
GeckoView.sendRequest({ json data }, callback); 
} 

The request is sent to the host and handled via the onScriptMessage method. If 
the optional callback is used in the request, you can signal success (and send 
back a response) or failure using the MessageResult object. 

Thoughts? 

Finkle 

[1] https://bugzilla.mozilla.org/show_bug.cgi?id=1035420 
_______________________________________________
mobile-firefox-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/mobile-firefox-dev

Reply via email to