frame->LoadRequest in OnLoadingStateChange

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.

frame->LoadRequest in OnLoadingStateChange

Postby payalord » Sun Jun 03, 2018 2:40 am

Here is scenario I'm trying to achieve:
1. Create Browser with initial "about:blank" page.
2. After browser created and "about:blank" is loaded navigate to let say "google.com".
3. When "google.com: page is loaded run javascript on it and navigate to another page.
4. When this another page is loaded run javascript on it and navigate via post request with params to 3rd one.

So to find out has the page loading been completed - I'm using OnLoadingStateChange with "!isLoading" param. Then when it is ready, I'm trying to call browser->GetMainFrame()->LoadRequest() to navigate to next page(which can be "post" and has headers):
Code: Select all
void RequestClientHandler::OnLoadingStateChange(CefRefPtr<CefBrowser> browser, bool isLoading, bool canGoBack, bool canGoForward) {
   if (!isLoading) {
      //Get action object
      ActionsInterface * actionObject = GetActionObject(browser->GetIdentifier());

      if (actionObject != nullptr) {
         //Get Frame
         CefRefPtr<CefFrame> frame = browser->GetMainFrame();

         //Get action query
         ActionQuery query = actionObject->Action();

         //Make sure, we are on the right page
         std::string url = frame->GetURL();
         if (url != query.url && actionObject->Prepare()) {
            CefRefPtr<CefRequest> request = CefRequest::Create();
            request->SetURL(query.url);
            request->SetMethod(query.method);

            if (!query.headers.empty()) {
               CefRequest::HeaderMap headerMap;
               for (auto x : query.headers) {
                  headerMap.insert(x);
               }
               request->SetHeaderMap(headerMap);
            }

            if (query.upload_data != "") {
               CefRefPtr<CefPostData> postData = CefPostData::Create();
               CefRefPtr<CefPostDataElement> element = CefPostDataElement::Create();
               element->SetToBytes(query.upload_data.size(), query.upload_data.c_str());
               postData->AddElement(element);
               request->SetPostData(postData);
            }

            frame->LoadRequest(request);
         }
         else if (query.javascript != "") {
            //Run Javascript
            RunJavaScript(query.javascript, browser);
         }
      }
   }
}


But the problem is LoadRequest looks like not working. Browser not navigating to the page that has been setup in request, because it is still at "about:blank" again and again. While query.url is "https://www.google.com/"
payalord
Techie
 
Posts: 21
Joined: Sun Jan 07, 2018 8:21 am

Re: frame->LoadRequest in OnLoadingStateChange

Postby magreenblatt » Sun Jun 03, 2018 11:14 am

LoadRequest can only be used if a renderer process already exists. It's possible that initially loading about:blank no longer creates a renderer process.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: frame->LoadRequest in OnLoadingStateChange

Postby payalord » Mon Jun 04, 2018 3:08 am

I see, thank you for explanation. I decided to use frame->LoadURL() for the first call. And then after that LoadRequest started to work.
payalord
Techie
 
Posts: 21
Joined: Sun Jan 07, 2018 8:21 am


Return to Support Forum

Who is online

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