peetonn
peetonn

Reputation: 3052

Firefox add-on in C++

Firefox provides XPCOM API interface for writing add-on classes in C++ and allow third-party web applications to access them. I'm wondering - is there any other way of achieving these benefits (i.e. write an add-on in C++ and provide a JavaScript interface, so any JavaScript app can use this interface and eventually C++ class functionality)?

Upvotes: 3

Views: 2031

Answers (1)

nmaier
nmaier

Reputation: 33162

Yes, it is possible to write XPCOM components in C++ (or Javascript for that matter) and expose them to websites. You would register them in the JavaScript-global-property, JavaScript-global-constructor, etc. category or categories. This stuff is generally not documented very well, if at all. If you'd like to know more about it, then read the code (e.g. on mxr).

But doing so is strongly discouraged for a lot of reasons:

  • It is very hard to ensure such things are actually secure and make it work reliable.
  • It is hard to control what website may actually use the new API. JavaScript-global-property and friends will be available to all websites!
  • Your add-on would be introducing new names into the global javascript namespace, which might clash with web site code. (Because of this, many new WebAPIs aren't introduced directly into the global namespace, but are made a property of navigator).
  • Due to these reasons, the addons.mozilla.org site will only accept add-ons using this mechanism as an exception to the rule and only when the author demonstrates that the use is required and without real alternatives.

MDN has a couple of articles regarding webpage-to-extension communication and vice versa you could use instead, e.g. "Interaction between privileged and non-privileged pages".

Also, it is discouraged to ship binary XPCOM components within extensions:

  • You'd need to re-compile your binaries against each Gecko version (every six weeks), because binary components are version tagged since Firefox 4.
  • Binary APIs/ABIs between (point) releases are not as stable as you would hope. Actually APIs/ABIs where inadvertently broken late in the game quite often, causing lots of unnecessary crashes for a lot of users, e.g. bug 828296.
  • The platform developers officially said binary components in extensions are a hassle.

Instead, you should try to use Javascript where possible, and/or create a normal binary library (.so, .dll, .dylib) with a C-API/ABI and use js-ctypes. You could implement your XPCOM components in javascript, which would then call into your library with js-ctypes. AFAIK most add-ons switched to js-ctypes by now.

Upvotes: 1

Related Questions