我正在使用Webkit插件工具在嵌入式环境中实现<video>
标记。 (对于那些感兴趣的人,我的灵感来自this)
我成功连接了我的插件的方法和属性,以映射HTML5标签应该在MediaPlayerPrivate Webkit类中公开的那些(我知道如何查询属性和调用简单方法),但我现在想知道我的插件如何将数据传回MediaPlayer界面。
我的插件在JavaScript中实现了addEventListener()
接口,所以我想我可以用它来将MediaPlayer客户端注册为EventListener,但我无法弄清楚如何。
我想做的是这样的:
Plugin WebKit
+--------------+ +-----------------------------+
| | | |
| <-------------|-+addEventListener(callback) |
| | | |
|+----------+ | | |
|| | | | |
|| onEvent | | | |
|+----------+--------------> callback( EventData ) |
| | | |
+--------------+ +-----------------------------+
我不知道的是,我是如何调用插件的方法addEventListener
并将其传递给我的C ++中引用静态回调的JSObject。
您对如何做到这一点有任何想法吗?
(感谢Asciiflow的ASCII图纸)
答案 0 :(得分:3)
对于那些感兴趣的人,解决方案是这样的:
// event name is the name of the event I want to subscribe to
// callback is a static function with the 'JSObjectCallAsFunctionCallback' prototype
JSObjectRef callbackObject = JSObjectMakeFunctionWithCallback(ctx, JSStringRef(), callback);
JSValue js_cb[3] = {
toJS(state, (const JSValueRef)JSValueMakeString (ctx, JSStringCreateWithUTF8CString(eventName))),
toJS(state, (const JSValueRef)callbackObject),
toJS(state, (const JSValueRef)JSValueMakeBoolean(ctx, false))
};
ArgList args(js_cb, 3);
return invokeMethod("addEventListener", args);
使用此代码,每当我的插件对象广播事件时,我都会看到代码登陆callback
静态函数。
现在我必须找到一种方法来传递我需要的私有数据,以便我可以从这个静态函数修改运行时值。