1 /*
2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26 #include "config.h"
27 #include "CookieStorageCFNet.h"
28
29 #if USE(CFNETWORK)
30
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>
37
38 #if USE(PLATFORM_STRATEGIES)
39 #include "CookiesStrategy.h"
40 #include "PlatformStrategies.h"
41 #endif
42
43 namespace WebCore {
44
privateCookieStorage()45 static RetainPtr<CFHTTPCookieStorageRef>& privateCookieStorage()
46 {
47 DEFINE_STATIC_LOCAL(RetainPtr<CFHTTPCookieStorageRef>, cookieStorage, ());
48 return cookieStorage;
49 }
50
currentCookieStorage()51 CFHTTPCookieStorageRef currentCookieStorage()
52 {
53 ASSERT(isMainThread());
54
55 if (CFHTTPCookieStorageRef cookieStorage = privateCookieStorage().get())
56 return cookieStorage;
57 return wkGetDefaultHTTPCookieStorage();
58 }
59
privateBrowsingCookieStorage()60 CFHTTPCookieStorageRef privateBrowsingCookieStorage()
61 {
62 return privateCookieStorage().get();
63 }
64
setCurrentCookieStorage(CFHTTPCookieStorageRef cookieStorage)65 void setCurrentCookieStorage(CFHTTPCookieStorageRef cookieStorage)
66 {
67 ASSERT(isMainThread());
68
69 privateCookieStorage().adoptCF(cookieStorage);
70 }
71
setCookieStoragePrivateBrowsingEnabled(bool enabled)72 void setCookieStoragePrivateBrowsingEnabled(bool enabled)
73 {
74 ASSERT(isMainThread());
75
76 if (!enabled) {
77 privateCookieStorage() = nullptr;
78 return;
79 }
80
81 #if USE(CFURLSTORAGESESSIONS)
82 if (CFURLStorageSessionRef privateStorageSession = ResourceHandle::privateBrowsingStorageSession())
83 privateCookieStorage().adoptCF(wkCopyHTTPCookieStorage(privateStorageSession));
84 else
85 #endif
86 privateCookieStorage().adoptCF(wkCreateInMemoryHTTPCookieStorage());
87 }
88
defaultCookieStorage()89 CFHTTPCookieStorageRef defaultCookieStorage()
90 {
91 return wkGetDefaultHTTPCookieStorage();
92 }
93
notifyCookiesChangedOnMainThread(void * context)94 static void notifyCookiesChangedOnMainThread(void* context)
95 {
96 ASSERT(isMainThread());
97
98 #if USE(PLATFORM_STRATEGIES)
99 platformStrategies()->cookiesStrategy()->notifyCookiesChanged();
100 #endif
101 }
102
notifyCookiesChanged(CFHTTPCookieStorageRef inStorage,void * context)103 static void notifyCookiesChanged(CFHTTPCookieStorageRef inStorage, void *context)
104 {
105 callOnMainThread(notifyCookiesChangedOnMainThread, 0);
106 }
107
cookieStorageObserverRunLoop()108 static inline CFRunLoopRef cookieStorageObserverRunLoop()
109 {
110 // We're using the loader run loop because we need a CFRunLoop to
111 // call the CFNetwork cookie storage APIs with. Re-using the loader
112 // run loop is less overhead than starting a new thread to just listen
113 // for changes in cookies.
114
115 // FIXME: The loaderRunLoop function name should be a little more generic.
116 return loaderRunLoop();
117 }
118
startObservingCookieChanges()119 void startObservingCookieChanges()
120 {
121 ASSERT(isMainThread());
122
123 CFRunLoopRef runLoop = cookieStorageObserverRunLoop();
124 ASSERT(runLoop);
125
126 CFHTTPCookieStorageRef cookieStorage = currentCookieStorage();
127 ASSERT(cookieStorage);
128
129 CFHTTPCookieStorageScheduleWithRunLoop(cookieStorage, runLoop, kCFRunLoopCommonModes);
130 CFHTTPCookieStorageAddObserver(cookieStorage, runLoop, kCFRunLoopDefaultMode, notifyCookiesChanged, 0);
131 }
132
stopObservingCookieChanges()133 void stopObservingCookieChanges()
134 {
135 ASSERT(isMainThread());
136
137 CFRunLoopRef runLoop = cookieStorageObserverRunLoop();
138 ASSERT(runLoop);
139
140 CFHTTPCookieStorageRef cookieStorage = currentCookieStorage();
141 ASSERT(cookieStorage);
142
143 CFHTTPCookieStorageRemoveObserver(cookieStorage, runLoop, kCFRunLoopDefaultMode, notifyCookiesChanged, 0);
144 CFHTTPCookieStorageUnscheduleFromRunLoop(cookieStorage, runLoop, kCFRunLoopCommonModes);
145 }
146
147 } // namespace WebCore
148
149 #endif // USE(CFNETWORK)
150