2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "CookieStorageCFNet.h"
31 #include "LoaderRunLoopCF.h"
32 #include "ResourceHandle.h"
33 #include <CFNetwork/CFHTTPCookiesPriv.h>
34 #include <WebKitSystemInterface/WebKitSystemInterface.h>
35 #include <wtf/MainThread.h>
36 #include <wtf/RetainPtr.h>
38 #if USE(PLATFORM_STRATEGIES)
39 #include "CookiesStrategy.h"
40 #include "PlatformStrategies.h"
45 static RetainPtr<CFHTTPCookieStorageRef>& privateBrowsingCookieStorage()
47 DEFINE_STATIC_LOCAL(RetainPtr<CFHTTPCookieStorageRef>, cookieStorage, ());
51 CFHTTPCookieStorageRef currentCookieStorage()
53 ASSERT(isMainThread());
55 if (CFHTTPCookieStorageRef privateCookieStorage = privateBrowsingCookieStorage().get())
56 return privateCookieStorage;
57 return wkGetDefaultHTTPCookieStorage();
60 void setCurrentCookieStorage(CFHTTPCookieStorageRef cookieStorage)
62 ASSERT(isMainThread());
64 privateBrowsingCookieStorage().adoptCF(cookieStorage);
67 void setCookieStoragePrivateBrowsingEnabled(bool enabled)
69 ASSERT(isMainThread());
72 privateBrowsingCookieStorage() = nullptr;
76 #if USE(CFURLSTORAGESESSIONS)
77 privateBrowsingCookieStorage().adoptCF(wkCreatePrivateInMemoryHTTPCookieStorage(ResourceHandle::privateBrowsingStorageSession()));
79 privateBrowsingCookieStorage().adoptCF(wkCreatePrivateInMemoryHTTPCookieStorage(0));
83 static void notifyCookiesChangedOnMainThread(void* context)
85 ASSERT(isMainThread());
87 #if USE(PLATFORM_STRATEGIES)
88 platformStrategies()->cookiesStrategy()->notifyCookiesChanged();
92 static void notifyCookiesChanged(CFHTTPCookieStorageRef inStorage, void *context)
94 callOnMainThread(notifyCookiesChangedOnMainThread, 0);
97 static inline CFRunLoopRef cookieStorageObserverRunLoop()
99 // We're using the loader run loop because we need a CFRunLoop to
100 // call the CFNetwork cookie storage APIs with. Re-using the loader
101 // run loop is less overhead than starting a new thread to just listen
102 // for changes in cookies.
104 // FIXME: The loaderRunLoop function name should be a little more generic.
105 return loaderRunLoop();
108 void startObservingCookieChanges()
110 ASSERT(isMainThread());
112 CFRunLoopRef runLoop = cookieStorageObserverRunLoop();
115 CFHTTPCookieStorageRef cookieStorage = currentCookieStorage();
116 ASSERT(cookieStorage);
118 CFHTTPCookieStorageScheduleWithRunLoop(cookieStorage, runLoop, kCFRunLoopCommonModes);
119 CFHTTPCookieStorageAddObserver(cookieStorage, runLoop, kCFRunLoopDefaultMode, notifyCookiesChanged, 0);
122 void stopObservingCookieChanges()
124 ASSERT(isMainThread());
126 CFRunLoopRef runLoop = cookieStorageObserverRunLoop();
129 CFHTTPCookieStorageRef cookieStorage = currentCookieStorage();
130 ASSERT(cookieStorage);
132 CFHTTPCookieStorageRemoveObserver(cookieStorage, runLoop, kCFRunLoopDefaultMode, notifyCookiesChanged, 0);
133 CFHTTPCookieStorageUnscheduleFromRunLoop(cookieStorage, runLoop, kCFRunLoopCommonModes);
136 } // namespace WebCore
138 #endif // USE(CFNETWORK)