Mohamed Mansour's Personal Website
Articles
Comments on How to communicate back to JavaScript from NPAPI plugin
16 Replies: (add yours)
Mohamed
2Hi simurg,
I believe you want an async event so the renderer will not blocked for running a long process within the plugin.
Take a look at:
developer.mozilla.org/en/NPN_PluginThreadAsyncCall
And when you know your plugin finished processing, you can send a plugin back.
Another way to do stuff is to implement a callback mechanism for code. Similar to addEventListener. To do that, you have to manage the callback in the plugin and destroy it when your done sending (via Async)
› Posted on December 13, 2010, 6:17 pm
Mohamed
3I might write an article on that, asynchronous NPAPI communication, it is pretty cool :)
› Posted on December 13, 2010, 6:17 pm
simurg
4Thanks for the tip Mohamed!
Looking forward to your next article.
Regards.
› Posted on December 15, 2010, 9:28 am
fedi smaoui
5thank you Mohamed for the article, it has been useful.
i'am doing a web application based on the npapi, it looks like i need the function NPN_PluginThreadAsyncCall to implement asynchronous calls.
i searched for a tutorial how to use this function but in vain.
thank you for your help
› Posted on December 20, 2010, 3:47 am
fedi smaoui
6thank you Mohamed for the article, it has been useful.
i'am doing a web application based on the npapi, it looks like i need the function NPN_PluginThreadAsyncCall to implement asynchronous calls.
i searched for a tutorial how to use this function but in vain.
thank you for your help
› Posted on December 20, 2010, 3:48 am
fedi smaoui
7/**
in this example i defined a button in an HTML page that calls this plugin. this button
calls the function asynchTest(). this function simulates a process that lasts a long time.
in an ordinary case if a process takes a long time to execute, it will block the mmi.
to avoid this, i used the function NPN_PluginThreadAsyncCall. However, mmi behavior is the same.
it is blocked in these 5 seconds (clicking on another button does nothing).
*/
void myfunc () //function called from mythread()
{
sleep(5);//simulate a process that takes a long time to be executed
npnfuncs->geturl(inst, "javascript:jsFunc(0)", "_self");//call a js function which alert a message
}
void * mythread (void* data)//function called from asynchTest()
{
NPP instance = (NPP) data;//plugin instance
//structure npnfuncs contains a certain number of the browser functions
//among these functions NPN_PluginThreadAsyncCall
npnfuncs->pluginthreadasynccall (instance, myfunc, NULL);//async call of myfunc in this thread
return NULL;
}
void asynchTest()//function called from invoke()
{
pthread_t thread1;//thread calling function mythread
int iret1 = pthread_create( &thread1, NULL, mythread, (void*) inst);
}
static bool invoke(NPObject* obj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result) //npapi function that executes methods passed as argument
{
char *name = npnfuncs->utf8fromidentifier(methodName);//in this case methodName is asynchTest (from js)
if(!strcmp(name, "asynchTest")) {
asynchTest();//call asynchTest
return true;
}
}
what is wrong with this code ? Is this the correct behavior of the plugin ? thank you for your help
› Posted on December 22, 2010, 12:17 pm
Mohamed Mansour
8Hi Fedi,
It would be best if you could use stackoverflow.com or quora.com for questions like this big :)Once you do it, it would be easier for me to respond back with syntax hilighting and examples.
Let me know the link once you submit that!
Thanks,
Mohamed
› Posted on January 4, 2011, 11:07 am
Steven
9Hi Mohamed,
Thanks for the wonderful article! I've just started learning NPAPI recently, my question is how to pass an unsigned char array from the plugin to my javascript, I spent hours searching but cannot find any helpful info, any help from you will be appreciated, cheers :)
Steve
› Posted on January 22, 2011, 6:40 am
m0
10Does STRINGZ_TO_NPVARIANT work for you? If you want to return an array, you would have to create an Array object in NPAPI. You can get that variant by executing NPN_Evaluate for the NPString "new Array()". Using that variant, you can extract the NPObject from it with NPVARIANT_TO_OBJECT. The NPObject is needed so you can set each property "NPN_SetProperty" successfully. Maybe I can create a blog post about that.
› Posted on January 22, 2011, 11:58 am
Steven
11Thank you very much Mohamed, I'll give it a go and let you the result.
Cheers,
Steve
› Posted on January 22, 2011, 10:03 pm
Steven
12Hi Mohamed,
My original purpose is to pass an array of unsigned char from the C++ plugin to the javascript, however, I cannot find any unsigned char data type in NPAPI, and my data is not a string(e.g, the data from am image), I even tried to make it a string by adding NULL terminator to the end of my binary array, it doesn't seems to work ...
After hours of struggle I fixed my problem. I use NPN_Invoke and NPN_SetProperty to directly manipulate the javascript objects in the plugin code (so I have to made some changes in my javascript code), in this way I can pass the array data to the javascript object method, in the plugin code. I'm not sure this is a good way but my problem is solved.
Your suggestion led me to the solution. Thank you very much :)
Regards,
Jingjing
› Posted on January 24, 2011, 6:11 am
Mohamed
13Jingjing: no problem :) I realized that I have done it in an extension I did last month. If you want more info, you can take a look how its done on one of my NPAPI Haptics extensions:
github.com/mohamedmansour/haptics-chrome-extension/blob/master/source/haptics_service.cc#L114
Basically, I want to fetch the device position into a 3 dimensional point, hence represented in an array.
If you have more question, let me know! And thanks for getting back to me with your solution :)
› Posted on January 24, 2011, 6:45 pm
taehoon
14Hi Mohamed, Thank you very much for this helpful article.
I'm trying to make a NPAPI plug-in which can transmit random values(generated by plug-in itself) to browser.
Once the plug-in called by a "start" javascript function, it should send values periodically until the "stop" function executed.
I've tried to get random values from plug-in by using
bool
ScriptablePluginObject::Invoke(NPIdentifier name, const NPVariant *args,
uint32_t argCount, NPVariant *result)
, but I could not acheive what I want.
Thank you for your help.
› Posted on August 7, 2011, 9:19 pm
Akshay Sahu
15Hey dude,
Can u please share a book on step by step procedure to learn NPAPI. I didn't find any ebook available.
Can u please elaborate the step-by-step process to load npapi files from javsscript.
Thanks,
Akshay Sahu
› Posted on September 5, 2012, 2:41 am
Ivan
16I make the plugin based on code.google.com/p/npapi-chrome-plugin-helloworld-example/
Where can i to place this code to take callback?
Thank you.
› Posted on April 22, 2013, 8:16 am

simurg
1Hi Mohamed,
Thanks for the great article!
I'm kind of interested in sending an event to the browser to be handled within the JavaScript code. i.e. when the plugin has finished doing something, I want the JavaScript to display a popup on the page?
Thanks in advance.
› Posted on December 13, 2010, 7:18 am