C++ how to use CefURLRequest::Create

Discussion of current events and participation in intelligent conversation with other CEF users.

C++ how to use CefURLRequest::Create

Postby Moreno77 » Thu Mar 10, 2016 12:01 pm

Hi,

I'm trying to find a way to use this CEF method. CefURLRequest::Create without any success.
I found in the file urlrequest_test.cc these calls.

Code: Select all
// Create a CefRequest for the specified URL.
      CefRefPtr<CefRequest> cef_request = CefRequest::Create();
      cef_request->SetURL(url);
      cef_request->SetMethod("GET");

      // Callback to be executed on request completion.
      // It's safe to use base::Unretained() here because there is only one
      // RequestClient pending at any given time and we explicitly detach the
      // callback in the Handler destructor.
      const RequestClient::Callback& request_callback =
          base::Bind(&Handler::OnRequestComplete, base::Unretained(this));

      // Create and start the new CefURLRequest.
      urlrequest_ = CefURLRequest::Create(cef_request,
                                          new RequestClient(request_callback),
                                          NULL);


How can I get a call to my own callback of ::OnRequestComplete.
Any idea of a simple implementation how to use this.

The test code seems way to complicated to use.

Thanks
Moreno77
Techie
 
Posts: 13
Joined: Wed Mar 09, 2016 3:37 am

Re: C++ how to use CefURLRequest::Create

Postby magreenblatt » Thu Mar 10, 2016 12:10 pm

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

Re: C++ how to use CefURLRequest::Create

Postby Moreno77 » Mon Mar 14, 2016 6:35 pm

I have looked at the link now and tried the example code

Code: Select all
class MyRequestClient : public CefURLRequestClient {
 public:
  MyRequestClient()
    : upload_total_(0),
      download_total_(0) {}

  virtual void OnRequestComplete(CefRefPtr<CefURLRequest> request) OVERRIDE {
    CefURLRequest::Status status = request->GetRequestStatus();
    CefURLRequest::ErrorCode error_code = request->GetRequestError();
    CefRefPtr<CefResponse> response = request->GetResponse();

    // Do something with the response...
  }

  virtual void OnUploadProgress(CefRefPtr<CefURLRequest> request,
                                uint64 current,
                                uint64 total) OVERRIDE {
    upload_total_ = total;
  }

  virtual void OnDownloadProgress(CefRefPtr<CefURLRequest> request,
                                  uint64 current,
                                  uint64 total) OVERRIDE {
    download_total_ = total;
  }

  virtual void OnDownloadData(CefRefPtr<CefURLRequest> request,
                              const void* data,
                              size_t data_length) OVERRIDE {
    download_data_ += std::string(static_cast<const char*>(data), data_length);
  }

 private:
  uint64 upload_total_;
  uint64 download_total_;
  std::string download_data_;

 private:
  IMPLEMENT_REFCOUNTING(MyRequestClient);
};


The CefRequest call.

Code: Select all
// Set up the CefRequest object.
   CefRefPtr<CefRequest> request = CefRequest::Create();
   // Populate |request| as shown above...
   request->SetURL("http://www.idg.se");
   request->SetMethod("GET");

// Create the client instance.
   CefRefPtr<MyRequestClient> client = new MyRequestClient();

   // Start the request. MyRequestClient callbacks will be executed asynchronously.
   CefRefPtr<CefURLRequest> url_request = CefURLRequest::Create(request, new MyRequestClient(), NULL);
   // To cancel the request: url_request->Cancel();



The problem I'm facing is that url_request is NULL after calling Create(...).
Do I need to add something to get it working?
Moreno77
Techie
 
Posts: 13
Joined: Wed Mar 09, 2016 3:37 am

Re: C++ how to use CefURLRequest::Create

Postby magreenblatt » Mon Mar 14, 2016 6:47 pm

Where are you calling CefURLRequest::Create from? It must be called on a valid CEF thread.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: C++ how to use CefURLRequest::Create

Postby Moreno77 » Mon Mar 14, 2016 7:10 pm

Is it correct to do the calls after CefInitialize(...) ?

Or maybe just after CefBrowserHost::CreateBrowser(...)

I just recently got it working if I do the Create call inside the, OnAfterCreated(...) method.

Is this the way to go if I want to do a http request to a certain page before any actual navigation is done?
Moreno77
Techie
 
Posts: 13
Joined: Wed Mar 09, 2016 3:37 am

Re: C++ how to use CefURLRequest::Create

Postby amaitland » Mon Mar 14, 2016 7:42 pm

Try in `CefBrowserProcessHandler::OnContextInitialized`. The callback should fire shortly after CEF has finished initializing.

apidocs3/projects/%28default%29/CefBrowserProcessHandler.html#OnContextInitialized%28%29
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1290
Joined: Wed Jan 14, 2015 2:35 am

Re: C++ how to use CefURLRequest::Create

Postby Moreno77 » Tue Mar 15, 2016 10:22 am

That works fine. Thanks

I guess CefURLRequest::Create works asynchronously.

Any idea how to continue with the CEF rendering after the HTTP request response has arrived ?
Maybe using a State Machine ?
Moreno77
Techie
 
Posts: 13
Joined: Wed Mar 09, 2016 3:37 am


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 4 guests