Generating async Javascript events from browser plugin (NPAPI)

元气小坏坏 提交于 2019-11-30 07:21:05

The answer is yes... and no...

If you need to support older browsers (pre firefox 3), you can implement the NPN_PluginThreadAsyncCall function yourself. On windows, you can do that by creating a data structure that can hold the function pointer and the void* opaque pointer, and then post a custom message to the main window with a pointer to your data structure as the LPARAM.

The main window WINPPROC runs on the UI thread, which is the thread that can talk to Javascript. So, when you get that message in your WINPROC, you simply cast the LPARAM back to the pointer, call the method with the opaque data, and then free the data structure.

On Mac, you can do a similar thing with a queue to store the events in, and then on the NULL event (which gets sent by Mac OS about every tick) check to see if anything is in it. If so, pop it off, call the method, free it, and keep going.

There is probably a way to do it on linux as well, but I don't know what it is.

You can find an example of the windows version in the firebreath project.

The handling of the winproc message is in this file: https://github.com/firebreath/FireBreath/blob/master/src/PluginWindow/Win/PluginWindowWin.cpp

The event and data structure are defined in its header file: https://github.com/firebreath/FireBreath/blob/master/src/PluginWindow/Win/PluginWindowWin.h

And the method for firing that event is here:

void ActiveXBrowserHost::ScheduleAsyncCall(void (*func)(void *), void *userData)
{
    if (m_hWnd != NULL) 
        ::PostMessage(m_hWnd, WM_ASYNCTHREADINVOKE, NULL, 
            (LPARAM)new FB::WINDOWS_ASYNC_EVENT(func, userData));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!