生成异步从浏览器插件,JavaScript事件(NPAPI) [英] Generating async Javascript events from browser plugin (NPAPI)

查看:397
本文介绍了生成异步从浏览器插件,JavaScript事件(NPAPI)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个网络浏览器插件(NPAPI)。

I'm writing a web browser plugin (NPAPI.)

我的插件启动一个工作线程,并作为工人的进展,我想传递的事件回的JavaScript。但由于NPAPI线程模型,这是不合法的工作线程直接调用回NPAPI,所以工作者线程不能调用JavaScript。

My plugin starts a worker thread, and as the worker progresses, I'd like to pass events back to Javascript. But because of the NPAPI threading model, it's not legal for the worker thread to call back into NPAPI directly, so the worker thread can't invoke Javascript.

的一个解决方案,这是NPN_PluginThreadAsyncCall功能。但是这是一个相对较新的功能。例如,它只是从Firefox 3支持。

One solution to this is the NPN_PluginThreadAsyncCall function. But this is a relatively new function. For example, it's supported only from Firefox 3 on.

有没有什么办法让异步事件传递/从NPAPI插件的JavaScript执行,而无需使用NPN_PluginThreadAsyncCall?什么加入此功能之前,人呢?

Is there any way to get async event delivery/javascript execution from an NPAPI plugin without using NPN_PluginThreadAsyncCall? What did people do before this function was added?

推荐答案

答案是肯定的...没有...

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

如果您需要支持旧的浏览器(pre Firefox 3中),您可以实现NPN_PluginThreadAsyncCall运作自己。在Windows中,你可以通过创建一个数据结构,可容纳函数指针与void *的不透明指针,然后上传自定义消息到主窗口的指针数据结构的LPARAM。

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.

主窗口WINPPROC UI线程,这是可以谈的JavaScript线程上运行。所以,当你在你的Winproc传的消息,你只投了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.

在Mac上,你可以做一个类似的事情在队列中存储的事件,然后在空事件(这被Mac操作系统大约每滴答发送)检查,看看是否有什么在里面。如果是这样,弹出它关闭,调用该方法,释放它,并坚持下去。

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.

有可能是一个办法做到这一点在Linux上一样好,但我不知道它是什么。

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

您可以找到在 firebreath项目 Windows版本的例子。

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

Winproc传消息的处理是在这个文件中:
https://github.com/firebreath/FireBreath/blob/master/src/PluginWindow/Win/PluginWindowWin.cpp

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

事件和数据结构在它的头文件中定义:
https://github.com/firebreath/FireBreath/blob/master/src/PluginWindow/Win/PluginWindowWin.h

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));
}

这篇关于生成异步从浏览器插件,JavaScript事件(NPAPI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆