javascriptgoogle-chromegoogle-nativeclientppapi

How to pass JavaScript function between Chrome browser and PNaCl plugin correctly


I wrote a simple echoer PNaCl plugin whose message handler just post the incoming message back unchanged:

class Instance : public pp::Instance {
public:
    virtual void HandleMessage(const pp::Var& message_data) {
        PostMessage(message_data);
    }
};

And, in JavaScript side, I post a message whose data is a function, expect to get the same function back and execute the responded function:

var funcobj = {
    tocall: function() { alert('tocall called'); }
}
document.getElementById('echoFunc').addEventListener('click', function() {
    console.log(funcobj);
    // Post a function to plugin
    common.naclModule.postMessage(funcobj);
});
function handleMessage(message_event) {
    console.log(message_event);
    message_event.data.tocall();
}

Unfortunately, in handleMessage(), message_event.data.tocall() is no longer a function, but an Object with fields defineGetter, defineSetter, lookupGetter, lookupSetter, etc.

How could I pass JavaScript function between Chrome browser and PNaCl plugin through PPAPI correctly?


Solution

  • Sorry, this is not possible. The only values that can be passed between JavaScript and Native Client via PostMessage are defined here: https://developer.chrome.com/native-client/pepper_stable/c/group___enums#ga9815041477d810724e44da862f9852ed

    That is: undefined, null, Bool, Number, String, Array, Dictionary, ArrayBuffer, and Resource (or some combination of these).

    Object is listed in that document but it is not supported. Dictionary is like a JSON object; it is just a string-value mapping. Resource currently only supports FileSystem objects.