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