CEF - Scheme Handler and XMLHttpRequest

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.

CEF - Scheme Handler and XMLHttpRequest

Postby Staxcelrom » Thu Aug 25, 2022 9:48 am

Please explain how to create a "Scheme Handler" in CEF, in order to be able to send data from Javascript code to C++ by calling XMLHttpRequest.

I'm trying to follow this enter link description here:

CefRegisterSchemeHandlerFactory:
Code: Select all
class my_CEF__application_callback_class : public CefApp, public CefBrowserProcessHandler, public CefRenderProcessHandler
{

    void OnContextInitialized() override
    {
        CefRefPtr<MySchemeHandlerFactory>my_SchemeHandlerFactory_ptr;
        my_SchemeHandlerFactory_ptr = new my_SchemeHandlerFactory_class();

        CefRegisterSchemeHandlerFactory("http", "my_binary_app", my_SchemeHandlerFactory_ptr);
    }

}



my_SchemeHandlerFactory_class:
Code: Select all
class my_SchemeHandlerFactory_class: public CefSchemeHandlerFactory
{
public:

    virtual CefRefPtr<CefResourceHandler> Create(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, const CefString& scheme_name, CefRefPtr<CefRequest> request) override
    {
        std::cout << "Create" << std::endl;


        CefRefPtr<CefResourceHandler>my_CefResourceHandler_ptr;
        my_CefResourceHandler_ptr = new my_ResourceHandler_class();

        return my_CefResourceHandler_ptr;
    }

    IMPLEMENT_REFCOUNTING(MySchemeHandlerFactory);
};



my_ResourceHandler_class:
Code: Select all
class my_ResourceHandler_class: public CefResourceHandler
{

public:

    virtual bool Open(CefRefPtr<CefRequest> request, bool& handle_request, CefRefPtr<CefCallback> callback) override
    {
        std::cout << "Open" << std::endl;

        handle_request = false;
        return false;
    }

    virtual void GetResponseHeaders(CefRefPtr<CefResponse> response, int64& response_length, CefString& redirectUrl) override
    {
        std::cout << "GetResponseHeaders" << std::endl;
    }


    virtual bool Read(void* data_out, int bytes_to_read, int& bytes_read, CefRefPtr<CefResourceReadCallback> callback) override
    {
        std::cout << "Read" << std::endl;

        return true;
    }


private:
    IMPLEMENT_REFCOUNTING(MyResourceHandler);
};




And from javascript I call the following code:

Code: Select all
std::string my_JS_code =

"let URL = \"http://my_binary_app\";"         

"let my_req = new XMLHttpRequest();"
"my_req.open(\"GET\", URL);"

"let my_let = \"Hello\";"
"my_req.send(my_let);";

(browser)->GetMainFrame()->ExecuteJavaScript(my_JS_code, "", 1);



And at the end - after executing the JS code - I expect to call the methods of the my_ResourceHandler_class class, but absolutely nothing happens. :(

I'm obviously doing something wrong, but I can't figure out what exactly I'm doing wrong.
Staxcelrom
Expert
 
Posts: 206
Joined: Wed Jan 26, 2022 8:20 am

Re: CEF - Scheme Handler and XMLHttpRequest

Postby magreenblatt » Thu Aug 25, 2022 11:02 am

magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: CEF - Scheme Handler and XMLHttpRequest

Postby Staxcelrom » Thu Aug 25, 2022 12:11 pm

Works. Apparently the browser does not pass requests over HTTP, only over HTTPS.

But, for some reason, not calling XMLHttpRequest:

Code: Select all
"let URL = \"https://my_binary_app\";"       

"let my_req = new XMLHttpRequest();"
"my_req.open(\"GET\", URL);"

"let my_let = \"Hello\";"
//or
"let blob = new Blob(['abc123'], { type: 'text/plain' });"

"my_req.send(blob);";


But why is strange data coming into the Read method:

Code: Select all
class MyResourceHandler : public CefResourceHandler
{

public:


   virtual bool Read(void* data_out, int bytes_to_read, int& bytes_read, CefRefPtr<CefResourceReadCallback> callback) override
   {
      bytes_read = bytes_to_read;                              //Always 65536

      std::string my_string;
                my_string.resize(bytes_read);

      memcpy(&my_string[0], data_out, bytes_to_read);

      std::cout << string_GLOBALity << std::endl;     //EMPTY - All bytes == null

      return true;
   }
}
Staxcelrom
Expert
 
Posts: 206
Joined: Wed Jan 26, 2022 8:20 am

Re: CEF - Scheme Handler and XMLHttpRequest

Postby magreenblatt » Thu Aug 25, 2022 1:07 pm

But why is strange data coming into the Read method:

You are supposed to return your data by writing into |data_out|. See the docs.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: CEF - Scheme Handler and XMLHttpRequest

Postby Staxcelrom » Thu Aug 25, 2022 1:16 pm

magreenblatt wrote:
But why is strange data coming into the Read method:

You are supposed to return your data by writing into |data_out|. See the docs.


I have already read the description for "Read" about 20 times.

And I can't figure out what it says::

READ response data. If data is available...


How I know - what data is available? by what parameter?

copy up to |bytes_to_read| bytes INTO |data_out|


Copy FROM ?
Staxcelrom
Expert
 
Posts: 206
Joined: Wed Jan 26, 2022 8:20 am

Re: CEF - Scheme Handler and XMLHttpRequest

Postby magreenblatt » Thu Aug 25, 2022 1:23 pm

The browser is reading response data from your CefResourceHandler. What did you think CefResourceHandler was for?
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: CEF - Scheme Handler and XMLHttpRequest

Postby Staxcelrom » Thu Aug 25, 2022 1:41 pm

magreenblatt wrote:The browser is reading response data from your CefResourceHandler. What did you think CefResourceHandler was for?


For handling scheme requests.
Staxcelrom
Expert
 
Posts: 206
Joined: Wed Jan 26, 2022 8:20 am

Re: CEF - Scheme Handler and XMLHttpRequest

Postby magreenblatt » Thu Aug 25, 2022 2:19 pm

Staxcelrom wrote:
magreenblatt wrote:The browser is reading response data from your CefResourceHandler. What did you think CefResourceHandler was for?


For handling scheme requests.

That’s correct. And what does “handling” mean?
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: CEF - Scheme Handler and XMLHttpRequest

Postby Staxcelrom » Thu Aug 25, 2022 3:05 pm

magreenblatt wrote:That’s correct. And what does “handling” mean?


I thought that this was an "interception" of a request with a signature specified in CefRegisterSchemeHandlerFactory(...).
And in Read/ReadResponse - I can get the data that I passed in the XMLHttpRequest request. I'm probably thinking wrong.

After your post - I thought that CefResourceHandler - "intercepts" my XMLHttpRequest request, which I made in js code, and in the Read/ReadResponse method - I have to set the data RESPONSE to this XMLHttpRequest - request.
I tried like this:
Code: Select all
function my_func()
{
let my_text_response = this.responseText; //I thought that here I should get a response, which I set to "Read/ReadResponse", but there was no response. The function "my_func" - was not called.
alert(my_text_response);
}



let URL = "https://my_js_request";     

let my_req = new XMLHttpRequest();
my_req.open(\"GET\", URL);

my_req.onload = my_func;
my_req.send();



Code: Select all
   virtual bool Read(void* data_out, int bytes_to_read, int& bytes_read, CefRefPtr<CefResourceReadCallback> callback) override
   {
      std::cout << "Read" << std::endl;

      bytes_read = sizeof("Hello_JS");

      memcpy(data_out, "Hello_JS", sizeof("Hello_JS"));

      return true;
   }


But that doesn't work either.


I also tried to "catch" my XMLHttpRequest in GetResourceResponseFilter() - but also without success.


And now I don't understand how it should work at all.
Staxcelrom
Expert
 
Posts: 206
Joined: Wed Jan 26, 2022 8:20 am

Re: CEF - Scheme Handler and XMLHttpRequest

Postby magreenblatt » Thu Aug 25, 2022 4:45 pm

I suggest you simplify things and just use CefResourceRequestHandler::GetResourceHandler instead. See https://bitbucket.org/chromiumembedded/ ... terception and the many examples in tests code.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Next

Return to Support Forum

Who is online

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