How to handle cookie?

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 handle cookie?

Postby tiplip » Tue Nov 07, 2017 8:32 am

Hi,

I am on CEF3 2623.1401 (to work with XP).

I create CefURLRequest on render process, after that, I want to set cookie at runtime. So I use SendProcessMessage to browser.

I handle the message in ClientHandler::OnProcessMessageReceived

Code: Select all
// render process
void HappSyncHandler::SetCookies(const CefString& domain, const CefString& name, const CefString& value) {
  CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create(kSetCookies);
 
  // arguments IPC
  CefRefPtr<CefListValue> args = message->GetArgumentList();
  args->SetString(0, domain);
  args->SetString(1, name);
  args->SetString(2, value);

  browser_->SendProcessMessage(PID_BROWSER, message);
}

SetCookies("http://www.meituan.com", "test", "test.my.cookies");

// browser process
bool ClientHandler::OnProcessMessageReceived(
    CefRefPtr<CefBrowser> browser,
    CefProcessId source_process,
    CefRefPtr<CefProcessMessage> message) {
  CEF_REQUIRE_UI_THREAD();
...
if (message_name == kSetCookies) {
    CefRefPtr<CefListValue> args = message->GetArgumentList();
    const CefString& domain = args->GetString(0);
    const CefString& name = args->GetString(1);
    const CefString& value = args->GetString(2);
    //CefRefPtr<CefCookieManager> manager =
    //    CefCookieManager::GetGlobalManager(NULL);

    //CefCookie cookie;
    //CefString(&cookie.name).FromString(name);
    //CefString(&cookie.value).FromString(value);
    //manager->SetCookie(domain, cookie, NULL);
    CefPostTask(TID_IO, base::Bind(&ClientHandler::SetCookie, this, domain, name, value));

    return true;
  }

void ClientHandler::SetCookie(const CefString& domain, const CefString& name, const CefString& value) {
    CefRefPtr<CefCookieManager> manager =
        CefCookieManager::GetGlobalManager(NULL);

    //manager->DeleteCookies(domain, name, NULL);

    CefCookie cookie;
    CefString(&cookie.name).FromString(name);
    CefString(&cookie.domain).FromString(domain);

    manager->SetCookie(domain, cookie, NULL); // return true
}



SetCookie returns true, but I find no 'test' cookie in request body with fiddler, what's the possible reason?
tiplip
Mentor
 
Posts: 76
Joined: Thu Mar 26, 2015 3:09 am

Re: How to handle cookie?

Postby HarmlessDave » Tue Nov 07, 2017 12:28 pm

Since CefPostTask is asynchronous it might not have finished setting the cookie before the URL request was made.

You could have a SetCookieAndTriggerNavigation() function that sets the cookie and only then sends a message back to the render process to do the navigation.
HarmlessDave
Expert
 
Posts: 370
Joined: Fri Jul 11, 2014 2:02 pm

Re: How to handle cookie?

Postby tiplip » Wed Nov 08, 2017 7:30 am

HarmlessDave wrote:Since CefPostTask is asynchronous it might not have finished setting the cookie before the URL request was made.

You could have a SetCookieAndTriggerNavigation() function that sets the cookie and only then sends a message back to the render process to do the navigation.

thanks, I find the reason. I should specify the right domain and right url because the site I visit has many sub sites.

for instance:
Code: Select all
  CefCookie cookie;
  CefString(&cookie.name).FromString(name);
  CefString(&cookie.value).FromString(value);
  CefString(&cookie.domain).FromString(domain); // sample domain:     .domain.com
  CefString(&cookie.path).FromASCII("/");
  cookie.has_expires = false;

  manager->SetCookie(url, cookie, NULL);    // sample url:    http://www.domain.com



if I set domain as xxx.domain.com, the coolkie is only visible in xxx.domain.com.
If I use a domain of ".domain.com", the cookie is stored correctly for fr.domain.com, and is visible in other subdomains.
tiplip
Mentor
 
Posts: 76
Joined: Thu Mar 26, 2015 3:09 am


Return to Support Forum

Who is online

Users browsing this forum: reito, salvadordf and 43 guests