How to return array buffer in resource handler

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

How to return array buffer in resource handler

Postby Tonygeek » Fri Feb 14, 2020 3:48 pm

I have resource handler working as expected - i.e. for my fake URL I get to the point where handler gets created:

Code: Select all
bool OnRequest(scoped_refptr<CefResourceManager::Request> request) OVERRIDE {
    CEF_REQUIRE_IO_THREAD();

    const std::string& url = request->url();
    if (url != "https://myfakeurl") {
            // Not handled by this provider.
         return false;
    }

    CefRefPtr<CefResourceHandler> handler;

   ... What to do here? ...

    request->Continue(handler);
    return true;
  }


but I am unable to figure out what to do with the handler in order to return an array buffer to JavaScript.

JavaScript side is:

Code: Select all
function myFunction()
{
var oReq = new XMLHttpRequest();
oReq.open("GET", "https://myfakeurl", true);
oReq.responseType = "arraybuffer";

oReq.onload = function (oEvent) {
  var arrayBuffer = oReq.response;
  if (arrayBuffer) {
    var byteArray = new Uint8Array(arrayBuffer);
    for (var i = 0; i < byteArray.byteLength; i++) {
          ....
    }
  }
;
oReq.send(null);
}


My simplistic attempt:

Code: Select all
       char buffer[3] = { 5,8,33 };
        CefRefPtr<CefStreamReader> stream = CefStreamReader::CreateForData(
            static_cast<void*>(buffer), sizeof buffer);
        request->Continue(new CefStreamResourceHandler("application/octet-stream", stream));


doesn't result in JavaScript onload called.
Tonygeek
Techie
 
Posts: 16
Joined: Mon Jan 13, 2020 7:39 pm

Re: How to return array buffer in resource handler

Postby Tonygeek » Fri Feb 14, 2020 6:12 pm

Solved.
The problem was CORS.

I changed JavaScript to say:

Code: Select all
oReq.open("GET", "/somedummyname.jpg", true)

and then resource handler to:

Code: Select all
bool OnRequest(scoped_refptr<CefResourceManager::Request> request) OVERRIDE {
        CEF_REQUIRE_IO_THREAD();

        const std::string& url = request->url();
        if (url.find("somedummyname.jpg") == std::string::npos)
        {
            // Not handled by this provider.
            return false;
        }

and everything worked like a charm :)
Tonygeek
Techie
 
Posts: 16
Joined: Mon Jan 13, 2020 7:39 pm


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 10 guests