Connect C ++ function to Javascript event listener using Webkit / JavascriptCore

I am using the Webkit plugin facility to implement a tag <video>

in an embedded environment. (for those interested, I was inspired by this )

I have successfully linked my plugin methods and properties to map the ones the HTML tag should show in the MediaPlayerPrivate Webkit class (I know how to query properties and call simple methods) but now I am wondering how my plugin can pass data to the MediaPlayer interface ...

My plugin implements the interface addEventListener()

in JavaScript, so I decided to use it to register the MediaPlayer client as an EventListener, but I can't figure out how.

I would like to do the following:

  Plugin                    WebKit
+--------------+         +-----------------------------+
|              |         |                             |
|          <-------------|-+addEventListener(callback) |
|              |         |                             |
|+----------+  |         |                             |
||          |  |         |                             |
|| onEvent  |  |         |                             |
|+----------+--------------> callback( EventData )     |
|              |         |                             |
+--------------+         +-----------------------------+

      

I don't know how I can call a method of addEventListener

my plugin and pass it a JSObject that references a static callback in my C ++.

Do you have any ideas on how to do this?

(ASCII drawings thanks to Asciiflow )

+3


source to share


1 answer


For those who are interested, the solution is this:

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

      



With this code, whenever my plugin object dispatches an event, I can see that the code is in a static function callback

.

Now I just need to find a way to pass the personal data that I need so that I can change the runtime values ​​from this static function.

+3


source







All Articles