Passing binary data to browser plug-in


#1

I have a working browser plug-in.  I'd like to pass some binary data (byte[], or void *, length) to the plug-in. I can't seem to find the right type such that var::isBinaryData() is true, as I'd like to use var::getBinaryData() to retreive a MemoryBlock * to the data.  It should be noted that all var:isXXX methods, except var::isObject(), are returning false;

If I know the data is binary is there a brute force mechanism I could use to get a MemoryBlock * to the data?

Here's a code snippet from my plug-in...

static var sendData(const var::NativeFunctionArgs &args)
{
    if (MyBrowserObject* b = 
        dynamic_cast<MyBrowserObject*> (args.thisObject.getObject()))
    {
        if (args.numArguments == 1)
        {
            bool binData = args.arguments[0].isBinaryData(); // <-------ALWAYS FALSE
            if (!binData)
            {
                Logger::outputDebugString("[sendData] arg[0] is not binary data");
                return var(-3);
            }
            MemoryBlock *data = args.arguments[0].getBinaryData();
            // do something with data...
        }
        
        Logger::outputDebugString("[sendData] invalid number of args");
        return var(-4);
    }
    
    Logger::outputDebugString("[sendData] thisObject is null");
    return var(-5);
}

In my javascript, I've tried passing ArrayBuffer, Uint8Array and Blob, but all three fail the .isBinary().

var buf = new ArrayBuffer(256);
var view = new Uint8Array(buf);
view[0] = 64; // '@'
view[1] = 65; // 'A'
view[2] = 66; // 'B'
view[4] = 67; // 'C'
view[5] = 68; // 'D'

var myBlob = new Blob([buf], { type: 'application/octet-binary' });

console.log("buf == " + buf.toString()); // 'object ArrayBuffer'
console.log("view == " + view.toString()); // 'object Uint8Array'
console.log("myBlob == " + myBlob.toString()); // 'object Blob'
var xx = getPlugin().sendData (buf);
xx = getPlugin().sendData(view);
xx = getPlugin().sendData(myBlob);

All other interfaces on my plug-in work (they accept int, string or double/float).


#2

Using a javascript array is probably the best plan, with var::getArray()


#3

None of the three native JavaScript 'classes' (ArrayBuffer, UInt8Array or Blob) return true from var::isArray(), so I assume var::getArray() would fail as well, right? 

 

Is there some means for me to access the native 'varenum' if a juce::var on windows?


#4

I just ran a test with this additional code...

Array<var> *theArray = args.arguments[0].getArray();
Logger::outputDebugString(String::formatted("DEBUG: theArray: %i", theArray ? true : false));

theArray is always NULL for (ArrayBuffer, uint8arry and Blob);


#5

TBH I can't remember whether the browser plugin classes handle arrays..

Actually, thinking about it, uuencoding it as a string is probably the most foolproof way.


#6

Thanks.  I'll use base64 strings and convert to MemoryBlock within the plug-in.