Search code examples
javascriptpluginsbrowsersafarinpapi

How to callback plugin thread on Safari 5.1 on OSX?


The Gecko functions NPN_PluginThreadAsyncCall / pluginthreadasynccall are not working for me on Safari 5.1 (Firefox works) - my function does not get called. I'm trying to make a function call back to JavaScript after performing a long-running task on another thread. How can I make this work?


Solution

  • Yeah, we had the same problem with Safari 5.1 in FireBreath (which you should definitely look at if you're not familiar with it); NPN_PluginThreadAsyncCall no longer works. You haven't specified which platform you're on, so I'll show you what we use on both Mac and Windows.

    On windows, we create a message HWND and we post a message to that HWND passing a void* in the event data as the LPARAM; the void* is converted back to a pointer to a data structure holding the params that pluginthreadasynccall would normally take, the call is made, and the data structure is freed. The message window class we use is here, and here is where the call is made. (Note that we use the same mechanism for cross-thread calls in IE)

    On Mac, we basically just use the objective-c performSelectorOnMainThread; we wrapped it into a class to make it easier to use from c++. The scheduler class we use is here, and here is where the call is made. Note that you need to be really careful because there are lots of possible race conditions with these and occasionally you end up with re-entrant code. FireBreath has dozens of hours of work put into perfecting the cross-thread calls; it's a deceptively tricky problem.

    Hopefully this helps!