Introduce and adopt new class RegistrableDomain for eTLD+1
[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 "RegistrableDomain.h"
30 #include <pal/SessionID.h>
31 #include <wtf/Function.h>
32 #include <wtf/HashMap.h>
33 #include <wtf/HashSet.h>
34 #include <wtf/WallTime.h>
35 #include <wtf/text/WTFString.h>
36
37 #if PLATFORM(COCOA) || USE(CFURLCONNECTION)
38 #include <pal/spi/cf/CFNetworkSPI.h>
39 #include <wtf/RetainPtr.h>
40 #endif
41
42 #if USE(SOUP)
43 #include <wtf/Function.h>
44 #include <wtf/glib/GRefPtr.h>
45 typedef struct _SoupCookieJar SoupCookieJar;
46 #endif
47
48 #if USE(CURL)
49 #include "CookieJarCurl.h"
50 #include "CookieJarDB.h"
51 #include <wtf/UniqueRef.h>
52 #endif
53
54 #ifdef __OBJC__
55 #include <objc/objc.h>
56 #endif
57
58 #if PLATFORM(COCOA)
59 #include "CookieStorageObserver.h"
60 #endif
61
62 namespace WebCore {
63
64 class CurlProxySettings;
65 class NetworkingContext;
66 class ResourceRequest;
67 class SoupNetworkSession;
68
69 struct Cookie;
70 struct CookieRequestHeaderFieldProxy;
71 struct SameSiteInfo;
72
73 enum class IncludeSecureCookies : bool;
74
75 class NetworkStorageSession {
76     WTF_MAKE_NONCOPYABLE(NetworkStorageSession); WTF_MAKE_FAST_ALLOCATED;
77 public:
78     WEBCORE_EXPORT static void permitProcessToUseCookieAPI(bool);
79     WEBCORE_EXPORT static bool processMayUseCookieAPI();
80
81     PAL::SessionID sessionID() const { return m_sessionID; }
82     CredentialStorage& credentialStorage() { return m_credentialStorage; }
83
84 #ifdef __OBJC__
85     WEBCORE_EXPORT NSHTTPCookieStorage *nsCookieStorage() const;
86 #endif
87
88 #if PLATFORM(COCOA) || USE(CFURLCONNECTION)
89     WEBCORE_EXPORT static RetainPtr<CFURLStorageSessionRef> createCFStorageSessionForIdentifier(CFStringRef identifier);
90     WEBCORE_EXPORT NetworkStorageSession(PAL::SessionID, RetainPtr<CFURLStorageSessionRef>&&, RetainPtr<CFHTTPCookieStorageRef>&&);
91     WEBCORE_EXPORT explicit NetworkStorageSession(PAL::SessionID);
92
93     // May be null, in which case a Foundation default should be used.
94     CFURLStorageSessionRef platformSession() { return m_platformSession.get(); }
95     WEBCORE_EXPORT RetainPtr<CFHTTPCookieStorageRef> cookieStorage() const;
96     WEBCORE_EXPORT static void setStorageAccessAPIEnabled(bool);
97 #elif USE(SOUP)
98     WEBCORE_EXPORT NetworkStorageSession(PAL::SessionID, std::unique_ptr<SoupNetworkSession>&&);
99     ~NetworkStorageSession();
100
101     SoupNetworkSession& soupNetworkSession() const;
102     void clearSoupNetworkSession();
103     SoupCookieJar* cookieStorage() const;
104     void setCookieStorage(SoupCookieJar*);
105     void setCookieObserverHandler(Function<void ()>&&);
106     void getCredentialFromPersistentStorage(const ProtectionSpace&, GCancellable*, Function<void (Credential&&)>&& completionHandler);
107     void saveCredentialToPersistentStorage(const ProtectionSpace&, const Credential&);
108 #elif USE(CURL)
109     WEBCORE_EXPORT NetworkStorageSession(PAL::SessionID);
110     ~NetworkStorageSession();
111
112     const CookieJarCurl& cookieStorage() const { return m_cookieStorage; };
113     CookieJarDB& cookieDatabase() const;
114     WEBCORE_EXPORT void setCookieDatabase(UniqueRef<CookieJarDB>&&);
115
116     WEBCORE_EXPORT void setProxySettings(CurlProxySettings&&);
117 #else
118     WEBCORE_EXPORT NetworkStorageSession(PAL::SessionID, NetworkingContext*);
119     ~NetworkStorageSession();
120
121     NetworkingContext* context() const;
122 #endif
123
124     WEBCORE_EXPORT bool cookiesEnabled() const;
125     WEBCORE_EXPORT void setCookie(const Cookie&);
126     WEBCORE_EXPORT void setCookies(const Vector<Cookie>&, const URL&, const URL& mainDocumentURL);
127     WEBCORE_EXPORT void setCookiesFromDOM(const URL& firstParty, const SameSiteInfo&, const URL&, Optional<uint64_t> frameID, Optional<uint64_t> pageID, const String&) const;
128     WEBCORE_EXPORT void deleteCookie(const Cookie&);
129     WEBCORE_EXPORT void deleteCookie(const URL&, const String&) const;
130     WEBCORE_EXPORT void deleteAllCookies();
131     WEBCORE_EXPORT void deleteAllCookiesModifiedSince(WallTime);
132     WEBCORE_EXPORT void deleteCookiesForHostnames(const Vector<String>& cookieHostNames);
133     WEBCORE_EXPORT Vector<Cookie> getAllCookies();
134     WEBCORE_EXPORT Vector<Cookie> getCookies(const URL&);
135     WEBCORE_EXPORT bool getRawCookies(const URL& firstParty, const SameSiteInfo&, const URL&, Optional<uint64_t> frameID, Optional<uint64_t> pageID, Vector<Cookie>&) const;
136     WEBCORE_EXPORT void flushCookieStore();
137     WEBCORE_EXPORT void getHostnamesWithCookies(HashSet<String>& hostnames);
138     WEBCORE_EXPORT std::pair<String, bool> cookiesForDOM(const URL& firstParty, const SameSiteInfo&, const URL&, Optional<uint64_t> frameID, Optional<uint64_t> pageID, IncludeSecureCookies) const;
139     WEBCORE_EXPORT std::pair<String, bool> cookieRequestHeaderFieldValue(const URL& firstParty, const SameSiteInfo&, const URL&, Optional<uint64_t> frameID, Optional<uint64_t> pageID, IncludeSecureCookies) const;
140     WEBCORE_EXPORT std::pair<String, bool> cookieRequestHeaderFieldValue(const CookieRequestHeaderFieldProxy&) const;
141
142 #if ENABLE(RESOURCE_LOAD_STATISTICS)
143     WEBCORE_EXPORT bool shouldBlockCookies(const ResourceRequest&, Optional<uint64_t> frameID, Optional<uint64_t> pageID) const;
144     WEBCORE_EXPORT bool shouldBlockCookies(const URL& firstPartyForCookies, const URL& resource, Optional<uint64_t> frameID, Optional<uint64_t> pageID) const;
145     WEBCORE_EXPORT void setPrevalentDomainsToBlockCookiesFor(const Vector<RegistrableDomain>&);
146     WEBCORE_EXPORT void setAgeCapForClientSideCookies(Optional<Seconds>);
147     WEBCORE_EXPORT void removePrevalentDomains(const Vector<RegistrableDomain>& domains);
148     WEBCORE_EXPORT bool hasStorageAccess(const RegistrableDomain& resourceDomain, const RegistrableDomain& firstPartyDomain, Optional<uint64_t> frameID, uint64_t pageID) const;
149     WEBCORE_EXPORT Vector<String> getAllStorageAccessEntries() const;
150     WEBCORE_EXPORT void grantStorageAccess(const RegistrableDomain& resourceDomain, const RegistrableDomain& firstPartyDomain, Optional<uint64_t> frameID, uint64_t pageID);
151     WEBCORE_EXPORT void removeStorageAccessForFrame(uint64_t frameID, uint64_t pageID);
152     WEBCORE_EXPORT void removeStorageAccessForAllFramesOnPage(uint64_t pageID);
153     WEBCORE_EXPORT void removeAllStorageAccess();
154     WEBCORE_EXPORT void setCacheMaxAgeCapForPrevalentResources(Seconds);
155     WEBCORE_EXPORT void resetCacheMaxAgeCapForPrevalentResources();
156     WEBCORE_EXPORT Optional<Seconds> maxAgeCacheCap(const ResourceRequest&);
157 #endif
158
159 private:
160     PAL::SessionID m_sessionID;
161
162 #if PLATFORM(COCOA) || USE(CFURLCONNECTION)
163     RetainPtr<CFURLStorageSessionRef> m_platformSession;
164     RetainPtr<CFHTTPCookieStorageRef> m_platformCookieStorage;
165 #elif USE(SOUP)
166     static void cookiesDidChange(NetworkStorageSession*);
167
168     mutable std::unique_ptr<SoupNetworkSession> m_session;
169     Function<void ()> m_cookieObserverHandler;
170 #elif USE(CURL)
171     UniqueRef<CookieJarCurl> m_cookieStorage;
172     mutable UniqueRef<CookieJarDB> m_cookieDatabase;
173 #else
174     RefPtr<NetworkingContext> m_context;
175 #endif
176
177     CredentialStorage m_credentialStorage;
178
179 #if ENABLE(RESOURCE_LOAD_STATISTICS)
180     bool shouldBlockThirdPartyCookies(const RegistrableDomain&) const;
181     HashSet<RegistrableDomain> m_registrableDomainsToBlockCookieFor;
182     HashMap<uint64_t, HashMap<uint64_t, RegistrableDomain, DefaultHash<uint64_t>::Hash, WTF::UnsignedWithZeroKeyHashTraits<uint64_t>>, DefaultHash<uint64_t>::Hash, WTF::UnsignedWithZeroKeyHashTraits<uint64_t>> m_framesGrantedStorageAccess;
183     HashMap<uint64_t, HashMap<RegistrableDomain, RegistrableDomain>, DefaultHash<uint64_t>::Hash, WTF::UnsignedWithZeroKeyHashTraits<uint64_t>> m_pagesGrantedStorageAccess;
184     Optional<Seconds> m_cacheMaxAgeCapForPrevalentResources { };
185     Optional<Seconds> m_ageCapForClientSideCookies { };
186 #endif
187
188 #if PLATFORM(COCOA)
189 public:
190     CookieStorageObserver& cookieStorageObserver() const;
191
192 private:
193     mutable RefPtr<CookieStorageObserver> m_cookieStorageObserver;
194 #endif
195     static bool m_processMayUseCookieAPI;
196 };
197
198 #if PLATFORM(COCOA) || USE(CFURLCONNECTION)
199 WEBCORE_EXPORT CFURLStorageSessionRef createPrivateStorageSession(CFStringRef identifier);
200 #endif
201
202 }