How do you extend the JavaScript API with custom methods that have access to native code in Chromium

StackOverflow https://stackoverflow.com/questions/4720753

Question

I am making a custom build of Chromium to get access to speech to text functionality in a custom way and am not sure how to expose access to a new native method through JavaScript.

In other versions of WebKit there are methods like addToJavaScriptWindowObject (that is in Qt) but I am not sure how to do the same thing in Chromium.

Was it helpful?

Solution

The interprocess API is a narrow interface, so has essentially the minimal number of calls.

First off, I would like to point you to the documented way to add new features to the cross-browser API (you can search for "carnitas" on the list to learn more about this): https://sites.google.com/a/chromium.org/dev/developers/design-documents/multi-process-architecture/how-to-add-new-features

In my case, I came up with a solution which hooks into existing API calls, basically, injecting javascript code into the page to call javascript's "prompt()" function for synchronous calls (freezing the whole tab until complete), and/or "externalHost.postMessage" for asynchronous messages. On the browser side, I hook into these two calls, and handle them, optionally returning a response.

You can see the code I wrote for Berkelium, a Chromium wrapper library here: https://github.com/sirikata/berkelium/blob/chromium8/src/WindowImpl.cpp (search for all the places where "javascriptCall" is referenced, to see how I hook into these APIs) The same technique can be applied to Chromium itself if you are willing to modify render_view_host.cc or the TabContents.

You should consider what level of integration is right/necessary for your API, and if you are interested in getting your speech-to-text functionality accepted into the Chrome codebase, you might be better off following the way that other features are implemented.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top