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 "CookieStorage.h"
29 #include "CookieStorageCFNet.h"
33 #include "LoaderRunLoopCF.h"
34 #include "ResourceHandle.h"
35 #include <CFNetwork/CFHTTPCookiesPriv.h>
36 #include <WebKitSystemInterface/WebKitSystemInterface.h>
37 #include <wtf/MainThread.h>
38 #include <wtf/RetainPtr.h>
40 #if USE(PLATFORM_STRATEGIES)
41 #include "CookiesStrategy.h"
42 #include "PlatformStrategies.h"
47 #if USE(CFNETWORK) || (USE(CFURLSTORAGESESSIONS) && PLATFORM(MAC))
48 #include <wtf/StdLibExtras.h>
53 #if USE(CFNETWORK) || (USE(CFURLSTORAGESESSIONS) && PLATFORM(MAC))
55 RetainPtr<CFHTTPCookieStorageRef>& privateBrowsingCookieStorage()
57 DEFINE_STATIC_LOCAL(RetainPtr<CFHTTPCookieStorageRef>, cookieStorage, ());
65 static RetainPtr<CFHTTPCookieStorageRef>& defaultSessionCookieStorage()
67 DEFINE_STATIC_LOCAL(RetainPtr<CFHTTPCookieStorageRef>, cookieStorage, ());
68 #if USE(CFURLSTORAGESESSIONS) && PLATFORM(WIN)
69 if (!cookieStorage && ResourceHandle::defaultStorageSession())
70 cookieStorage.adoptCF(wkCopyHTTPCookieStorage(ResourceHandle::defaultStorageSession()));
75 CFHTTPCookieStorageRef currentCookieStorage()
77 ASSERT(isMainThread());
79 if (CFHTTPCookieStorageRef cookieStorage = privateBrowsingCookieStorage().get())
81 return defaultCookieStorage();
84 void setCurrentCookieStorage(CFHTTPCookieStorageRef cookieStorage)
86 ASSERT(isMainThread());
88 privateBrowsingCookieStorage().adoptCF(cookieStorage);
91 void setCookieStoragePrivateBrowsingEnabled(bool enabled)
93 ASSERT(isMainThread());
96 privateBrowsingCookieStorage() = nullptr;
100 #if USE(CFURLSTORAGESESSIONS)
101 if (CFURLStorageSessionRef privateStorageSession = ResourceHandle::privateBrowsingStorageSession())
102 privateBrowsingCookieStorage().adoptCF(wkCopyHTTPCookieStorage(privateStorageSession));
105 privateBrowsingCookieStorage().adoptCF(wkCreateInMemoryHTTPCookieStorage());
108 CFHTTPCookieStorageRef defaultCookieStorage()
110 if (CFHTTPCookieStorageRef defaultCookieStorage = defaultSessionCookieStorage().get())
111 return defaultCookieStorage;
112 return wkGetDefaultHTTPCookieStorage();
115 static void notifyCookiesChangedOnMainThread(void*)
117 ASSERT(isMainThread());
119 #if USE(PLATFORM_STRATEGIES)
120 platformStrategies()->cookiesStrategy()->notifyCookiesChanged();
124 static void notifyCookiesChanged(CFHTTPCookieStorageRef, void *)
126 callOnMainThread(notifyCookiesChangedOnMainThread, 0);
129 static inline CFRunLoopRef cookieStorageObserverRunLoop()
131 // We're using the loader run loop because we need a CFRunLoop to
132 // call the CFNetwork cookie storage APIs with. Re-using the loader
133 // run loop is less overhead than starting a new thread to just listen
134 // for changes in cookies.
136 // FIXME: The loaderRunLoop function name should be a little more generic.
137 return loaderRunLoop();
140 void startObservingCookieChanges()
142 ASSERT(isMainThread());
144 CFRunLoopRef runLoop = cookieStorageObserverRunLoop();
147 CFHTTPCookieStorageRef cookieStorage = currentCookieStorage();
148 ASSERT(cookieStorage);
150 CFHTTPCookieStorageScheduleWithRunLoop(cookieStorage, runLoop, kCFRunLoopCommonModes);
151 CFHTTPCookieStorageAddObserver(cookieStorage, runLoop, kCFRunLoopDefaultMode, notifyCookiesChanged, 0);
154 void stopObservingCookieChanges()
156 ASSERT(isMainThread());
158 CFRunLoopRef runLoop = cookieStorageObserverRunLoop();
161 CFHTTPCookieStorageRef cookieStorage = currentCookieStorage();
162 ASSERT(cookieStorage);
164 CFHTTPCookieStorageRemoveObserver(cookieStorage, runLoop, kCFRunLoopDefaultMode, notifyCookiesChanged, 0);
165 CFHTTPCookieStorageUnscheduleFromRunLoop(cookieStorage, runLoop, kCFRunLoopCommonModes);
168 #endif // USE(CFNETWORK)
170 } // namespace WebCore