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');
});
//}
}));
});