I've been working on a browser extension for Firefox and Chrome that needs to invoke some native code. It turns out that the only way to do this in Chrome is through an NPAPI plugin (NaCl is not an option here because the native code needs to perform some privileged operations). Luckily there is the Firebreath project which makes it pretty easy to create a cross-platform NPAPI plugin for Mac, Linux, and Windows. You can load the plugin once in the background page of your Chrome plugin and then your content scripts can communicate with it through message passing.
In Firefox, however, there are challenges to calling an NPAPI plugin from an extension. The two options are (a) to load the plugin in to the DOM of the browser itself; or (b) to inject the plugin in to the DOM of each page as it is loaded. Option (a) seems like a bit of a hack, and (b) has security issues (since scripts on the page could then invoke the plugin).
A better option in Firefox is to use the js-ctypes API to call in to a shared library written in C. The shared library only needs to be loaded once, and is not accessible to scripts on web pages. The library is just a vanilla C shared library, you don't have all the overhead of conforming to the NPAPI plugin interface. The drawback, of course, is that you have to create both an NPAPI plugin and a js-ctypes shared library if you want to support Chrome and Firefox. But I felt it was worth the extra effort, given the limitations of calling NPAPI plugins from Firefox extensions.
To minimize code duplication, I created a static library which contains most of my functionality, and then linked it into the NPAPI plugin and the js-ctypes shared library.
3 comments:
Out of curiosity, how do you load the plugin in the browser's DOM (i.e. method A)?
I haven't actually tried it, but this link shows the basic idea:
http://stackoverflow.com/questions/7458530/cannot-access-npapi-functions-in-firefox-extension
Hi, i am also building a NPAPI and js-ctypes plugin for firefox. it works fine on Windows, but on MAC facing problems. Am not able to call any plugin function from javascript. Would you have any idea?
document.getElementById('myPlugin').testFunc(); fails
Post a Comment