3

I want to return array of string from NPAPI plugin to Javascript. Currently I am using only plain NPAPI. I have read the following links:

I am able to return alert() from plugin to javascript and I can get the NPNVWindowObject, but I am stuck right now on figuring out how to push elements onto the array and return it to javascript.

Working code samples would be appreciated, thanks

Community
  • 1
  • 1
r_tex
  • 77
  • 9

1 Answers1

5

You're already close; you just need to fill in a few details. The FireBreath codebase has examples of doing this, but the actual implementation is a bit abstracted. I don't have any raw NPAPI code that does this; I build plugins in FireBreath and it's almost ridiculously simple there. I can tell you what you need to do, however.

The problem gets simpler if you break it down into a few steps:

  1. Get the NPObject for the window (sounds like you have this)
  2. Create a new array and get the NPObject for that array
  3. Invoke "push" on that NPObject for each item you want to send to the DOM
  4. Retain the NPObject for the array and return it in the return value NPVariant

I'll take a stab at the code you'd use for these; there might be some minor errors.

1) Get the NPObject for the window

// Get window object.
NPObject* window = NULL;
NPN_GetValue(npp_, NPNVWindowNPObject, &window);
// Remember that when we're done we need to NPN_ReleaseObject the window!

2) Create a new array and get the NPObject for that array

Basically we do this by calling window.Array(), which you do by invoking Array on the window.

// Get the array object
NPObject* array = NULL;
NPVariant arrayVar;
NPN_Invoke(_npp, window, NPN_GetStringIdentifier("Array"), NULL, 0, &arrayVar);
array = arrayVar.value.objectValue;
// Note that we don't release the arrayVar because we'll be holding onto the pointer and returning it later

3) Invoke "push" on that NPObject for each item you want to send to the DOM

NPIdentifier pushId = NPN_GetStringIdentifier("push");
for (std::vector<std::string>::iterator it = stringList.begin(); it != stringList.end(); ++it) {
    NPVariant argToPush;
    NPVariant res;
    STRINGN_TO_NPVARIANT(it->c_str(), it->size(), argToPush);
    NPN_Invoke(_npp, array, pushId, &argToPush, 1, &res);
    // Discard the result
    NPN_ReleaseVariantValue(&res);
}

4) Retain the NPObject for the array and return it in the return value NPVariant

// Actually we don't need to retain the NPObject; we just won't release it. Same thing.
OBJECT_TO_NPVARIANT(array, *retVal);
// We're assuming that the NPVariant* param passed into this function is called retVal

That should pretty much do it. Make sure you understand how memory management works; read http://npapi.com/memory if you haven't.

Good luck

taxilian
  • 14,229
  • 4
  • 34
  • 73
  • Thanks a lot taxilian , it works i am iterating my arraylist and returning it to javascript. One issue i have is first time it sends array contents seemlesly , next time array's some contents are sended to javascript, after then it crashes.don't know why i tried NPN_RetainObject(array). Problem is same first time no problem, second time array's 2-3 contents browser crashes. In Javascript i am iterating over array and displaying it's contents. Thanks in advance. – r_tex Aug 23 '12 at 16:44
  • can you attach a debugger and find out where the crash is occurring? – taxilian Aug 23 '12 at 16:59
  • 1
    one simple solution i have implemented also -> std::vector intList; intList.push_back(10); intList.push_back(20); for(int i=0;i – r_tex Aug 23 '12 at 18:36
  • 1
    // Javascript side var test = obj.testArray(); //testArray will give the object of my class where i am implementing the Array returning code inside plugin. for(var i=0;i – r_tex Aug 23 '12 at 18:37
  • Add another question, don't ask totally different questions in the comments. However, the principle is the same. Get the NPObject and make calls. also, flag the answer as correct; people like to help you more if you are polite about thanking them, and on stackoverflow that means upvotes and flagging correct answers. – taxilian Aug 23 '12 at 19:12
  • 1
    Upvotes requires >15 reputation points , i have only 1 right now , but i flagged ur answer . Thanks for ur suggestion . – r_tex Aug 24 '12 at 18:03
  • My answer has not been flagged... but mainly I'm making sure you know. many users who join to ask fb questions dont' seem to – taxilian Aug 24 '12 at 18:52
  • I'll remember it taxilian . To flag the post i had just clicked on flag , one dialog came and filled info. – r_tex Aug 25 '12 at 17:30
  • Thanks taxilian , now i am able to pass array from javascript to NPAPI plugin and viceversa. – r_tex Aug 25 '12 at 17:45
  • you don't click of "flag", you click on the checkmark next to the question. – taxilian Aug 25 '12 at 20:23