3

I'm work on a project recently, which need to pass a binary-stream from npapi plugin to javascript, I've tried following ways:

  1. use NPN_InvokeDefault, i created a string variant which store the binary-stream, and invoke it to javascript, it failed. (i've tried to pass binary-stream read from XXX.txt file, it works!)

  2. i tried to use NPN_NewStream, the example listed in http://www.terraluna.org/dgp/cvsweb/PluginSDK/Documentation/pi3.htm#npnnewstream workes, but the pic is loaded in a new browser tab, i don't know how to recieve it in javascript.

Is there any one have ever met similar problem before? or maybe npapi can't support such kind of data transfering?

looking forward to your suggestiongs, thanks a lot.

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
brianchai
  • 33
  • 2

1 Answers1

5

Unfortunately, NPAPI was never designed with this purpose in mind. There are a couple of ways you can do it, and none of them are really ideal:

  • You can create a javascript array and pass the data in small 1-4 byte chunks (this is really very inefficient)
  • You could create a webserver embedded in the plugin and request the data from there (I have done this and it can work quite well, but keep in mind that if you use this from an SSL website you'll get security warnings when the embedded webserver isn't SSL)
  • You can base64 encode the binary data and send it as a string.

Those are the ways I have seen it done. The reason you can't send the actual binary data directly as a string is that NPAPI requires string data to be UTF8, but if you base64 encode it then it works fine.

Sorry I can't give you a "happier" solution :-/

taxilian
  • 14,229
  • 4
  • 34
  • 73
  • Thank you taxilian, your suggestion is really helpfull! Actually i tried your 3rd solution, and Base64 works well -- I can pass a pic to js page now. And here's another problem: I can pass a 100 K pic, but when I try a 1 M pic, the plugin-container of firefox became busy(25% cpu) and crashed finally... I wonder is there any size limitation in this kind of data passing? – brianchai May 16 '12 at 08:14
  • There may be; I've never done a string that big. Also, welcome to stackoverflow =] Remember to always upvote helpful answers or comments and flag answers as "correct" if they are the best answer. You can wait to see if a better answer comes along if you'd like, of course. – taxilian May 16 '12 at 15:39
  • @brianchai: Not exactly an expected use-case, but if that's reproducible, please [file a bug](https://bugzilla.mozilla.org/) :) – Georg Fritzsche May 16 '12 at 16:35