Missing cookie on request from service worker

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.

Missing cookie on request from service worker

Postby lwttai » Fri Oct 25, 2019 2:20 am

Problem:
Request from service worker canot get any cookies when using a custom CookieManager created by CefCookieManager::CreateManager("E:\Tmp\cefclientcache", true, NULL);
Seems there is a differnt CookieStore for service worker when using a custom CookieManager. How to make request from service worker bings up the cookies?
CefVersion:
70, 3.3538.1852.gcb937fc cefclient
Reproduce step:
using cefclient as sample
1. Create a cookie_manager in ClientRequestContextHandler() in "chromium_git\chromium\src\cef\tests\cefclient\browser\root_window_manager.cc"
Code: Select all
  ClientRequestContextHandler() {
    CefRefPtr<CefCommandLine> command_line =
        CefCommandLine::GetGlobalCommandLine();
    if (command_line->HasSwitch(switches::kRequestContextBlockCookies)) {
      // Use a cookie manager that neither stores nor retrieves cookies.
      cookie_manager_ = CefCookieManager::GetBlockingManager();
    }
        // ※ Create a new cookie manager which will cause the problem
        cookie_manager_ = CefCookieManager::CreateManager("E:\Tmp\cefclientcache", true, NULL);
  }

2. launch cefclient and load a service worker page(See "Simple service worker page" below)
3. Open Dev tool and create a domain cookie witch match the webpage domain(the cookie will add to the cookiestore associate to our custom CookieManager)
4. Check the "Disable chache" in Dev tool - Network
5. Reflresh the page and using tool(Fiddler or something) to capture the requst
final: all the request to server did not have the cookie we set in step3
Remark:
I have did some research on this. I found something strange at "void CefCookieStoreProxy::GetCookieListWithOptionsAsync(...)" in "chromium_git\chromium\src\cef\libcef\browser\net\cookie_store_proxy.cc"
Code: Select all
void CefCookieStoreProxy::GetCookieListWithOptionsAsync(
    const GURL& url,
    const net::CookieOptions& options,
    GetCookieListCallback callback) {
  net::CookieStore* cookie_store = GetCookieStore();
  if (cookie_store) {
    cookie_store->GetCookieListWithOptionsAsync(url, options,
                                                std::move(callback));
  } else if (!callback.is_null()) {
    std::move(callback).Run(net::CookieList());
  }
}

The CookieStore returned by GetCookieStore() is hole different bettween requset frome page to servie worker and request frome service worker to web server;
page to servie worker: GetCookieStore return the CookieStore associte to our CookieManager whitch has the cookie we set
service worker: GetCookieStore return a CookieStore(still not figure out where it came from) which don't have the cookie we set
Simple service worker page:
1. Setup a simple service worker page, like https://github.com/mdn/sw-test
2. Modify that sw.js to always request from server in "Fetch" hook
Code: Select all
self.addEventListener('fetch', function(event) {
  event.respondWith(caches.match(event.request).then(function(response) {
    // caches.match() always resolves
    // but in case of success response will have value
    //if (response !== undefined) {
    //  return response;
    //} else {
      return fetch(event.request).then(function (response) {
        // response may be used only once
        // we need to save clone to put one copy in cache
        // and serve second one
        let responseClone = response.clone();
       
        //caches.open('v1').then(function (cache) {
        //  cache.put(event.request, responseClone);
        //});
        return response;
      }).catch(function () {
        return caches.match('/sw-test/gallery/myLittleVader.jpg');
      });
    //}
  }));
});
lwttai
Techie
 
Posts: 24
Joined: Mon Oct 14, 2019 12:18 am

Re: Missing cookie on request from service worker

Postby amaitland » Fri Oct 25, 2019 3:28 am

Does the problem reproduce with a supported version?

See https://bitbucket.org/chromiumembedded/ ... -supported

Service worker support was greatly improved in version 75
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1292
Joined: Wed Jan 14, 2015 2:35 am

Re: Missing cookie on request from service worker

Postby lwttai » Mon Oct 28, 2019 9:02 pm

amaitland wrote:Does the problem reproduce with a supported version?

See https://bitbucket.org/chromiumembedded/ ... -supported

Service worker support was greatly improved in version 75


try cef 71-74, same thing happend.
From 75 begin, problem solved.
But the interface of CefCookieMannager、CefRequestContextHandler have a great change.
SetStoragePath()、CreateManager() nolonger exists in CefCookieMannager; GetCookieManager() nolonger exists in CefRequestContextHandler.
lwttai
Techie
 
Posts: 24
Joined: Mon Oct 14, 2019 12:18 am

Re: Missing cookie on request from service worker

Postby amaitland » Mon Oct 28, 2019 10:01 pm

You can no longer create an instance of CefCookieManager, you can create a new instance of CefRequestContext and specify a different cache path to have a separate cookie store.

The network service changes are detailed at https://bitbucket.org/chromiumembedded/ ... orkservice

Version 77 is the current supported version.
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1292
Joined: Wed Jan 14, 2015 2:35 am

Re: Missing cookie on request from service worker

Postby lwttai » Wed Oct 30, 2019 5:16 am

Thanks, that's helpful on 75 and above.
lwttai
Techie
 
Posts: 24
Joined: Mon Oct 14, 2019 12:18 am


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 25 guests