CookieStorageCFNet.cpp [plain text]
#include "config.h"
#include "CookieStorage.h"
#include "CookieStorageCFNet.h"
#if USE(CFNETWORK)
#include "LoaderRunLoopCF.h"
#include "ResourceHandle.h"
#include <CFNetwork/CFHTTPCookiesPriv.h>
#include <WebKitSystemInterface/WebKitSystemInterface.h>
#include <wtf/MainThread.h>
#include <wtf/RetainPtr.h>
#if USE(PLATFORM_STRATEGIES)
#include "CookiesStrategy.h"
#include "PlatformStrategies.h"
#endif
#endif
#if USE(CFNETWORK) || (USE(CFURLSTORAGESESSIONS) && PLATFORM(MAC))
#include <wtf/StdLibExtras.h>
#endif
namespace WebCore {
#if USE(CFNETWORK) || (USE(CFURLSTORAGESESSIONS) && PLATFORM(MAC))
RetainPtr<CFHTTPCookieStorageRef>& privateBrowsingCookieStorage()
{
DEFINE_STATIC_LOCAL(RetainPtr<CFHTTPCookieStorageRef>, cookieStorage, ());
return cookieStorage;
}
#endif
#if USE(CFNETWORK)
static RetainPtr<CFHTTPCookieStorageRef>& defaultSessionCookieStorage()
{
DEFINE_STATIC_LOCAL(RetainPtr<CFHTTPCookieStorageRef>, cookieStorage, ());
#if USE(CFURLSTORAGESESSIONS) && PLATFORM(WIN)
if (!cookieStorage && ResourceHandle::defaultStorageSession())
cookieStorage.adoptCF(wkCopyHTTPCookieStorage(ResourceHandle::defaultStorageSession()));
#endif
return cookieStorage;
}
CFHTTPCookieStorageRef currentCookieStorage()
{
ASSERT(isMainThread());
if (CFHTTPCookieStorageRef cookieStorage = privateBrowsingCookieStorage().get())
return cookieStorage;
return defaultCookieStorage();
}
void setCurrentCookieStorage(CFHTTPCookieStorageRef cookieStorage)
{
ASSERT(isMainThread());
privateBrowsingCookieStorage().adoptCF(cookieStorage);
}
void setCookieStoragePrivateBrowsingEnabled(bool enabled)
{
ASSERT(isMainThread());
if (!enabled) {
privateBrowsingCookieStorage() = nullptr;
return;
}
#if USE(CFURLSTORAGESESSIONS)
if (CFURLStorageSessionRef privateStorageSession = ResourceHandle::privateBrowsingStorageSession())
privateBrowsingCookieStorage().adoptCF(wkCopyHTTPCookieStorage(privateStorageSession));
else
#endif
privateBrowsingCookieStorage().adoptCF(wkCreateInMemoryHTTPCookieStorage());
}
CFHTTPCookieStorageRef defaultCookieStorage()
{
if (CFHTTPCookieStorageRef defaultCookieStorage = defaultSessionCookieStorage().get())
return defaultCookieStorage;
return wkGetDefaultHTTPCookieStorage();
}
static void notifyCookiesChangedOnMainThread(void*)
{
ASSERT(isMainThread());
#if USE(PLATFORM_STRATEGIES)
platformStrategies()->cookiesStrategy()->notifyCookiesChanged();
#endif
}
static void notifyCookiesChanged(CFHTTPCookieStorageRef, void *)
{
callOnMainThread(notifyCookiesChangedOnMainThread, 0);
}
static inline CFRunLoopRef cookieStorageObserverRunLoop()
{
return loaderRunLoop();
}
void startObservingCookieChanges()
{
ASSERT(isMainThread());
CFRunLoopRef runLoop = cookieStorageObserverRunLoop();
ASSERT(runLoop);
CFHTTPCookieStorageRef cookieStorage = currentCookieStorage();
ASSERT(cookieStorage);
CFHTTPCookieStorageScheduleWithRunLoop(cookieStorage, runLoop, kCFRunLoopCommonModes);
CFHTTPCookieStorageAddObserver(cookieStorage, runLoop, kCFRunLoopDefaultMode, notifyCookiesChanged, 0);
}
void stopObservingCookieChanges()
{
ASSERT(isMainThread());
CFRunLoopRef runLoop = cookieStorageObserverRunLoop();
ASSERT(runLoop);
CFHTTPCookieStorageRef cookieStorage = currentCookieStorage();
ASSERT(cookieStorage);
CFHTTPCookieStorageRemoveObserver(cookieStorage, runLoop, kCFRunLoopDefaultMode, notifyCookiesChanged, 0);
CFHTTPCookieStorageUnscheduleFromRunLoop(cookieStorage, runLoop, kCFRunLoopCommonModes);
}
#endif // USE(CFNETWORK)
}