8

How can an interprocess communication be estabilished between a browser extension and a native application? Is there any cross-platform (Linux and Mac OS X) and cross-browser solution (Firefox, Chrome, Safari)?

The only idea that comes to my mind is using native Web technologies, i.e. embed a HTTP server in native application and use XmlHttpRequest or WebSockets. However, this sounds like clunky overkill with handful of issues (e.g. security). Is there a better alternative?

Céline Aussourd
  • 10,214
  • 4
  • 32
  • 36
  • Cross-browser? Probably not. A browser is designed to talk to web servers, and the more cross-browser a solution you want, the less are the chances that you have something extra to that. – Xan May 17 '14 at 21:20
  • Actually, some time ago NPAPI would probably be a way, but at least in Chrome it's deprecated completely for extensions. – Xan May 17 '14 at 21:22
  • By cross-browser I mean solution that allows me to have one "protocol" and one "server" component, even if "browser-side" implementations would be different. – el.pescado - нет войне May 18 '14 at 10:22
  • NPAPI is also deprecated when it comes to Firefox (well, kinda). addons.mozilla.org does not accept submissions of NPAPI plugins or add-ons containing such plugins since quite some time. – nmaier May 18 '14 at 20:42

1 Answers1

11

I believe the most commonly used method is websocket connections. Two examples I can think of are 1Password and LiveReload (source code available).

As far as I know, you need to open the websocket connection from within your global page to avoid cross-domain restrictions.

Also, in the past I have seen other apps watch and modify an extension's settings file. The extension just reads and writes from it's own settings store, while the other process watches the preferences file for changes. I believe this is less reliable and doesn't conform to sandboxing requirements for the Mac App Store so I would recommend the websockets method.

Matt Swain
  • 3,827
  • 4
  • 25
  • 36
  • Is it possible to run WebSockets over AF_UNIX sockets instead of TCP? – el.pescado - нет войне May 18 '14 at 10:23
  • `AF_UNIX` would exclude Windows (but you seem to be only after Linux and OSX anyway)... However, you [cannot even use *raw* TCP in chrome extensions](http://stackoverflow.com/questions/13921970/google-chrome-socket-api-in-extensions), only HTTP/XHR and WebSockets (to some extend). You [can use domain sockets in Firefox](http://mxr.mozilla.org/mozilla-central/source/netwerk/base/public/nsISocketTransportService.idl) relatively hassle-free, though. – nmaier May 18 '14 at 20:53
  • Using raw sockets is not necessary. Constructing WebSocket URI that points to unix domain socket instead of HTTP host would be fine. – el.pescado - нет войне May 19 '14 at 08:35
  • 1
    @el.pescado: why do you need Unix domain socket? why isn't TCP loopback as transport "good enough"? – oberstet May 19 '14 at 10:46
  • 1
    @oberstet: Unix domain sockets "live" on filesystem, wchich is convienient, as 1) sockets can be "placed" in users' home directories in multiuser systems, 2) filesystem permissions are enforced on them – el.pescado - нет войне May 19 '14 at 11:39
  • I think your question about UNIX sockets deserves a separate question. – Xan May 19 '14 at 14:49