[Curl] Use SQLite database in cookie jar implementation for Curl port
[WebKit-https.git] / Source / WebCore / platform / network / NetworkStorageSession.h
1 /*
2  * Copyright (C) 2012-2018 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 INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #pragma once
27
28 #include "CredentialStorage.h"
29 #include <pal/SessionID.h>
30 #include <wtf/Function.h>
31 #include <wtf/HashMap.h>
32 #include <wtf/HashSet.h>
33 #include <wtf/text/WTFString.h>
34
35 #if PLATFORM(COCOA) || USE(CFURLCONNECTION)
36 #include <pal/spi/cf/CFNetworkSPI.h>
37 #include <wtf/RetainPtr.h>
38 #endif
39
40 #if USE(SOUP)
41 #include <wtf/Function.h>
42 #include <wtf/glib/GRefPtr.h>
43 typedef struct _SoupCookieJar SoupCookieJar;
44 #endif
45
46 #if USE(CURL)
47 #include "CookieJarCurl.h"
48 #include "CookieJarDB.h"
49 #include <wtf/UniqueRef.h>
50 #endif
51
52 #ifdef __OBJC__
53 #include <objc/objc.h>
54 #endif
55
56 #if PLATFORM(COCOA)
57 #include "CookieStorageObserver.h"
58 #endif
59
60 namespace WebCore {
61
62 class NetworkingContext;
63 class ResourceRequest;
64 class SoupNetworkSession;
65
66 struct Cookie;
67
68 class NetworkStorageSession {
69     WTF_MAKE_NONCOPYABLE(NetworkStorageSession); WTF_MAKE_FAST_ALLOCATED;
70 public:
71     WEBCORE_EXPORT static NetworkStorageSession& defaultStorageSession();
72     WEBCORE_EXPORT static NetworkStorageSession* storageSession(PAL::SessionID);
73     WEBCORE_EXPORT static void ensureSession(PAL::SessionID, const String& identifierBase = String());
74     WEBCORE_EXPORT static void destroySession(PAL::SessionID);
75     WEBCORE_EXPORT static void forEach(const WTF::Function<void(const WebCore::NetworkStorageSession&)>&);
76
77     WEBCORE_EXPORT static void switchToNewTestingSession();
78
79     PAL::SessionID sessionID() const { return m_sessionID; }
80     CredentialStorage& credentialStorage() { return m_credentialStorage; }
81
82 #ifdef __OBJC__
83     WEBCORE_EXPORT NSHTTPCookieStorage *nsCookieStorage() const;
84 #endif
85
86     const String& cacheStorageDirectory() const { return m_cacheStorageDirectory; }
87     void setCacheStorageDirectory(String&& path) { m_cacheStorageDirectory = WTFMove(path); }
88     uint64_t cacheStoragePerOriginQuota() const { return m_cacheStoragePerOriginQuota; }
89     void setCacheStoragePerOriginQuota(uint64_t quota) { m_cacheStoragePerOriginQuota = quota; }
90
91 #if PLATFORM(COCOA) || USE(CFURLCONNECTION)
92     WEBCORE_EXPORT static void ensureSession(PAL::SessionID, const String& identifierBase, RetainPtr<CFHTTPCookieStorageRef>&&);
93     NetworkStorageSession(PAL::SessionID, RetainPtr<CFURLStorageSessionRef>&&, RetainPtr<CFHTTPCookieStorageRef>&&);
94
95     // May be null, in which case a Foundation default should be used.
96     CFURLStorageSessionRef platformSession() { return m_platformSession.get(); }
97     WEBCORE_EXPORT RetainPtr<CFHTTPCookieStorageRef> cookieStorage() const;
98     WEBCORE_EXPORT static void setCookieStoragePartitioningEnabled(bool);
99     WEBCORE_EXPORT static void setStorageAccessAPIEnabled(bool);
100 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
101     WEBCORE_EXPORT String cookieStoragePartition(const ResourceRequest&, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID) const;
102     WEBCORE_EXPORT bool shouldBlockCookies(const ResourceRequest&) const;
103     bool shouldBlockCookies(const URL& firstPartyForCookies, const URL& resource) const;
104     String cookieStoragePartition(const URL& firstPartyForCookies, const URL& resource, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID) const;
105     WEBCORE_EXPORT void setPrevalentDomainsToPartitionOrBlockCookies(const Vector<String>& domainsToPartition, const Vector<String>& domainsToBlock, const Vector<String>& domainsToNeitherPartitionNorBlock, bool clearFirst);
106     WEBCORE_EXPORT void removePrevalentDomains(const Vector<String>& domains);
107     WEBCORE_EXPORT bool hasStorageAccessForFrame(const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID) const;
108     WEBCORE_EXPORT bool hasStorageAccessForFrame(const ResourceRequest&, uint64_t frameID, uint64_t pageID) const;
109     WEBCORE_EXPORT void grantStorageAccessForFrame(const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID);
110     WEBCORE_EXPORT void removeStorageAccessForFrame(uint64_t frameID, uint64_t pageID);
111     WEBCORE_EXPORT void removeStorageAccessForAllFramesOnPage(uint64_t pageID);
112 #endif
113 #elif USE(SOUP)
114     NetworkStorageSession(PAL::SessionID, std::unique_ptr<SoupNetworkSession>&&);
115     ~NetworkStorageSession();
116
117     SoupNetworkSession* soupNetworkSession() const { return m_session.get(); };
118     SoupNetworkSession& getOrCreateSoupNetworkSession() const;
119     void clearSoupNetworkSessionAndCookieStorage();
120     SoupCookieJar* cookieStorage() const;
121     void setCookieStorage(SoupCookieJar*);
122     void setCookieObserverHandler(Function<void ()>&&);
123     void getCredentialFromPersistentStorage(const ProtectionSpace&, Function<void (Credential&&)> completionHandler);
124     void saveCredentialToPersistentStorage(const ProtectionSpace&, const Credential&);
125 #elif USE(CURL)
126     NetworkStorageSession(PAL::SessionID, NetworkingContext*);
127     ~NetworkStorageSession();
128
129     const CookieJarCurl& cookieStorage() const { return m_cookieStorage; };
130     CookieJarDB& cookieDatabase() const { return m_cookieDatabase; };
131
132     NetworkingContext* context() const;
133 #else
134     NetworkStorageSession(PAL::SessionID, NetworkingContext*);
135     ~NetworkStorageSession();
136
137     NetworkingContext* context() const;
138 #endif
139
140     WEBCORE_EXPORT void setCookie(const Cookie&);
141     WEBCORE_EXPORT void setCookies(const Vector<Cookie>&, const URL&, const URL& mainDocumentURL);
142     WEBCORE_EXPORT void deleteCookie(const Cookie&);
143     WEBCORE_EXPORT Vector<Cookie> getAllCookies();
144     WEBCORE_EXPORT Vector<Cookie> getCookies(const URL&);
145     WEBCORE_EXPORT void flushCookieStore();
146
147 private:
148     static HashMap<PAL::SessionID, std::unique_ptr<NetworkStorageSession>>& globalSessionMap();
149     PAL::SessionID m_sessionID;
150
151 #if PLATFORM(COCOA) || USE(CFURLCONNECTION)
152     RetainPtr<CFURLStorageSessionRef> m_platformSession;
153     RetainPtr<CFHTTPCookieStorageRef> m_platformCookieStorage;
154 #elif USE(SOUP)
155     static void cookiesDidChange(NetworkStorageSession*);
156
157     mutable std::unique_ptr<SoupNetworkSession> m_session;
158     GRefPtr<SoupCookieJar> m_cookieStorage;
159     Function<void ()> m_cookieObserverHandler;
160 #if USE(LIBSECRET)
161     Function<void (Credential&&)> m_persisentStorageCompletionHandler;
162     GRefPtr<GCancellable> m_persisentStorageCancellable;
163 #endif
164 #elif USE(CURL)
165     UniqueRef<CookieJarCurl> m_cookieStorage;
166     mutable CookieJarDB m_cookieDatabase;
167
168     RefPtr<NetworkingContext> m_context;
169 #else
170     RefPtr<NetworkingContext> m_context;
171 #endif
172
173     CredentialStorage m_credentialStorage;
174
175     String m_cacheStorageDirectory;
176     uint64_t m_cacheStoragePerOriginQuota { 0 };
177
178 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
179     bool shouldPartitionCookies(const String& topPrivatelyControlledDomain) const;
180     bool shouldBlockThirdPartyCookies(const String& topPrivatelyControlledDomain) const;
181     HashSet<String> m_topPrivatelyControlledDomainsToPartition;
182     HashSet<String> m_topPrivatelyControlledDomainsToBlock;
183     HashMap<uint64_t, HashMap<uint64_t, String, DefaultHash<uint64_t>::Hash, WTF::UnsignedWithZeroKeyHashTraits<uint64_t>>, DefaultHash<uint64_t>::Hash, WTF::UnsignedWithZeroKeyHashTraits<uint64_t>> m_framesGrantedStorageAccess;
184 #endif
185
186 #if PLATFORM(COCOA)
187 public:
188     CookieStorageObserver& cookieStorageObserver() const;
189
190 private:
191     mutable RefPtr<CookieStorageObserver> m_cookieStorageObserver;
192 #endif
193 };
194
195 #if PLATFORM(COCOA)
196 WEBCORE_EXPORT CFURLStorageSessionRef createPrivateStorageSession(CFStringRef identifier);
197 #endif
198
199 }