0

I am trying to pass an array from plugin to Javascript. But I am unable to do it. I have referred to other post of similar kind where the solution given was to first get the Dom window object and then to invoke the function using NPN_Invoke with NPN_GetStringIdentifier("array") and then Push the array elements. But when I tried it, its crashing on NPN_Invoke. I am unable to find out why, Is it because NPObject of Dom is not having any method related to 'array' or some other reason?

Here is my plugin code...(My array data is 0,1,2,...9 The value of i in loop)

    bool ScriptableObject::method_process_getarray_intval(const NPVariant* args, uint32_t argCount, NPVariant* result)
    {
            printf("\n DATATYPE method_process_getarray_intval");
            NPObject  DomWin;
            NPVariant Array;
            NPError   l_RetErr;
            bool      l_RetBool;
            NPIdentifier l_NPId;

            //Get the Window Object        
            l_RetErr = NPN_GetValue(m_npp,NPNVWindowNPObject,&DomWin);
            if(l_RetErr == NPERR_NO_ERROR)
                    printf("\n \t Got the Dom Window Object");
            else
                    printf("\n \t Error occured while getting the Dom Window Object");

            //Get the Array by invoking DomWin using ARRAY method of browser

            //Get string Identifier for Array
            l_NPId = NPN_GetStringIdentifier("Array");

            l_RetBool = NPN_Invoke(m_npp,&DomWin,l_NPId,NULL,0,&Array);
            if(l_RetBool)
                    printf("\n \t Invoked for Array");
            else
                    printf("\n \t Error while Invoking for Array");

            //Fill the array elements using PUSH method of Array NPVariant
            l_NPId = NPN_GetStringIdentifier("push");
            for(int i = 0;i < 10; i++)
            {
                    NPVariant *arg =(NPVariant *) g_browser->memalloc(sizeof(NPVariant));
                    INT32_TO_NPVARIANT(i,*arg);
                    NPVariant result;
                    l_RetBool= NPN_Invoke(m_npp,Array.value.objectValue,l_NPId,arg,1,&result);
                    if(l_RetBool)
                            printf("\n \t Invoked for Array i : %d",i);
                    else
                            printf("\n \t Error while Invoking for Array i :%d",i);
            }
            return true;

    }

And my html page look like this. I dont know if this is right. Please let me know if there are any corrections...

    <html>
    <script type="text/javascript">
    var inarray ;

    function handleEvent(e) {
            ...
            if (e.keyCode == 55)
            {
                            document.getElementById('arrayint_ele_get').innerHTML = inarray;
                            process_getintarray(inarray);
            }
    }

    </script>
    <script type="text/javascript">
    function  process_getintarray(inarray)
    {
            if(obj)
            {
                    obj.process_getarray_intval(inarray);
            }
    }
    </script>

    <body onload="init()" onkeydown="handleEvent(event)">

    <div id="sq" style="width:50px;height:50px;position:relative;left:0px;border:1px solid #333333;background-color:#FF0000"></div>
    ......
    <div style="position:absolute;left:100px;top:10px"> PLUGIN-SCRIPTS </div>
    <div id ="arrayint_ele_get" style="position:absolute;left:100px;top:300px"> ARRAY_GETINT VAL </div>
    .....
    </body>

    </html>

Any suggestions are welcome....

Techtotie
  • 33
  • 5
  • 1
    What platform are you on? What line is it crashing on? – taxilian Aug 02 '12 at 16:17
  • I doubt this would be causing a crash, but you do have a memory leak. NPVariant *arg =(NPVariant *) g_browser->memalloc(sizeof(NPVariant)); is unneeded; just create a local "NPVariant arg;" and then pass it in as "&arg" to get the pointer. When you call memalloc but never call memfree you're leaking memory – taxilian Aug 02 '12 at 17:36
  • For more info on NPAPI memory management: http://npapi.com/memory – taxilian Aug 02 '12 at 19:22
  • @ Taxalian: Thanks for your inputs.. I am using qtwebkit on ubuntu. I am also trying the same plugin with Firefox also as it is NPAPI plugin. It's crashing on NPN_Invoke function call for NPN_GetStringIdentifier("array") and similarly on the other NPN_Invoke for NPN_GetStringIdentifier("push"). Unable to figure out why ... – Techtotie Aug 03 '12 at 04:50
  • I even tried to see if the method exists but instead of returning a false its crashing. The code is as follows...... //Get string Identifier for Array l_NPId = NPN_GetStringIdentifier("array"); if(g_browser->hasmethod != NULL) printf("\n \t Browser hasMethod is NOT NULL"); else printf("\n \t Browser hasMethod is NULL"); l_RetBool = NPN_HasMethod(m_npp,&DomWin,l_NPId); printf("\n l_RetBool : %d",l_RetBool); ..It is crashing on NPN_HasMethod. But NPN_HasMethod has to return a false if method UNable to figure out why its crashing at this point..... – Techtotie Aug 03 '12 at 05:06

1 Answers1

0

From what little I can gather from what you've given us, I would guess that either your NPNFuncs function pointer isn't set up correctly (and thus you're calling a bad function) or somehow your m_npp is corrupt. Of course, if either of those were the case I'd think requesting the DOM Window object would die as well.

I'd probably need more code to speculate beyond that; you might want to consider using FireBreath or nixysa rather than doing everything yourself; it'd let you concentrate on your actual plugin rather than dealing with NPAPI. Just a thought.

Next thought, is NPN_Invoke calling g_browser->invoke? 'cause you mentioned that you're checking that for hasmethod but then you turn around and call NPN_HasMethod; if that's not all wired up correctly it would definitely cause an issue like that.

What it comes down to is that somewhere, one of your pointers is off; the possibilities are:

  • NPN_Invoke and NPN_HasMethod aren't calling the correct method on g_browser
  • g_browser (your NPNFuncs struct) doesn't have valid function pointers
  • Your npp is not valid (not correctly saved, etc)

I can't see how it could be much of anything else.

taxilian
  • 14,229
  • 4
  • 34
  • 73
  • @Taxalian : Thanks for your help. I am sorry to ask a very basic question as I am new to plugin. From the above answer, I understand that NPN_Invoke should not call g_browser->invoke, if I am not wrong. My understanding is that NPN->invoke or NPN_HasMethod should operate on object passed to it. I am passing DomWin NPObject which is got by l_RetErr = NPN_GetValue(m_npp,NPNVWindowNPObject,&DomWin);. So NPN_*functions should call the methods that are defined with this object which internally is defined in browser Dom Wimdow Object. Is my understanding right... – Techtotie Aug 03 '12 at 06:20
  • NPN_Invoke *should* call g_browser->invoke, assuming that g_browser is a NPNFuncs structure that has been correctly filled out during the initialization of the plugin library. the NPN_* functions will do various things that happen to coincide with what you mention, but you don't have anything to do with making sure that happens; your job is just to make sure that your NPN_ functions call the appropriate function pointer on g_browser. See http://npapi.com/tutorial – taxilian Aug 03 '12 at 15:21
  • Ya you were right Taxalian. There was a problem with DomWin object and m_npp. I just followed this link http://stackoverflow.com/questions/10446766/safari-plugin-crashes-on-npn-getvalue and got it upto some point. Now After getting the DomWin object and the m_npp I am just checking if it has a method to handle array using .... l_RetBool = g_browser->hasmethod(l_npp,DomWin,g_browser->getstringidentifier("array")); but it returns a zero. So does it mean that DomWin object does not have any method for handling arrays. If it does not then how should arrays be handled. – Techtotie Aug 06 '12 at 10:16
  • it would have to be "Array" not "array" – taxilian Aug 06 '12 at 15:02