CookieStorageCFNet.cpp [plain text]
#include "config.h"
#include "CookieStorage.h"
#include "LoaderRunLoopCF.h"
#include "NetworkStorageSession.h"
#include <CFNetwork/CFHTTPCookiesPriv.h>
#include <pal/spi/cf/CFNetworkSPI.h>
#include <wtf/Function.h>
#include <wtf/HashMap.h>
#include <wtf/MainThread.h>
#include <wtf/NeverDestroyed.h>
namespace WebCore {
static HashMap<CFHTTPCookieStorageRef, WTF::Function<void ()>>& cookieChangeCallbackMap()
{
static NeverDestroyed<HashMap<CFHTTPCookieStorageRef, WTF::Function<void ()>>> map;
return map;
}
static void notifyCookiesChanged(CFHTTPCookieStorageRef cookieStorage, void *)
{
callOnMainThread([cookieStorage] {
auto it = cookieChangeCallbackMap().find(cookieStorage);
if (it != cookieChangeCallbackMap().end())
it->value();
});
}
static inline CFRunLoopRef cookieStorageObserverRunLoop()
{
return loaderRunLoop();
}
void startObservingCookieChanges(const NetworkStorageSession& storageSession, WTF::Function<void ()>&& callback)
{
ASSERT(isMainThread());
CFRunLoopRef runLoop = cookieStorageObserverRunLoop();
ASSERT(runLoop);
RetainPtr<CFHTTPCookieStorageRef> cookieStorage = storageSession.cookieStorage();
ASSERT(cookieStorage);
ASSERT(cookieChangeCallbackMap().contains(cookieStorage.get()));
cookieChangeCallbackMap().add(cookieStorage.get(), WTFMove(callback));
CFHTTPCookieStorageScheduleWithRunLoop(cookieStorage.get(), runLoop, kCFRunLoopCommonModes);
CFHTTPCookieStorageAddObserver(cookieStorage.get(), runLoop, kCFRunLoopDefaultMode, notifyCookiesChanged, 0);
}
void stopObservingCookieChanges(const NetworkStorageSession& storageSession)
{
ASSERT(isMainThread());
CFRunLoopRef runLoop = cookieStorageObserverRunLoop();
ASSERT(runLoop);
RetainPtr<CFHTTPCookieStorageRef> cookieStorage = storageSession.cookieStorage();
ASSERT(cookieStorage);
cookieChangeCallbackMap().remove(cookieStorage.get());
CFHTTPCookieStorageRemoveObserver(cookieStorage.get(), runLoop, kCFRunLoopDefaultMode, notifyCookiesChanged, 0);
CFHTTPCookieStorageUnscheduleFromRunLoop(cookieStorage.get(), runLoop, kCFRunLoopCommonModes);
}
}