Resource Load Statistics: Block cookies for prevalent resources without user interaction
[WebKit-https.git] / Source / WebCore / platform / network / NetworkStorageSession.h
1 /*
2  * Copyright (C) 2012-2017 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 #ifdef __OBJC__
47 #include <objc/objc.h>
48 #endif
49
50 #if PLATFORM(COCOA)
51 #include "CookieStorageObserver.h"
52 #endif
53
54 namespace WebCore {
55
56 class NetworkingContext;
57 class ResourceRequest;
58 class SoupNetworkSession;
59
60 struct Cookie;
61
62 class NetworkStorageSession {
63     WTF_MAKE_NONCOPYABLE(NetworkStorageSession); WTF_MAKE_FAST_ALLOCATED;
64 public:
65     WEBCORE_EXPORT static NetworkStorageSession& defaultStorageSession();
66     WEBCORE_EXPORT static NetworkStorageSession* storageSession(PAL::SessionID);
67     WEBCORE_EXPORT static void ensureSession(PAL::SessionID, const String& identifierBase = String());
68     WEBCORE_EXPORT static void destroySession(PAL::SessionID);
69     WEBCORE_EXPORT static void forEach(const WTF::Function<void(const WebCore::NetworkStorageSession&)>&);
70
71     WEBCORE_EXPORT static void switchToNewTestingSession();
72
73     PAL::SessionID sessionID() const { return m_sessionID; }
74     CredentialStorage& credentialStorage() { return m_credentialStorage; }
75
76 #ifdef __OBJC__
77     WEBCORE_EXPORT NSHTTPCookieStorage *nsCookieStorage() const;
78 #endif
79
80     const String& cacheStorageDirectory() const { return m_cacheStorageDirectory; }
81     void setCacheStorageDirectory(String&& path) { m_cacheStorageDirectory = WTFMove(path); }
82     uint64_t cacheStoragePerOriginQuota() const { return m_cacheStoragePerOriginQuota; }
83     void setCacheStoragePerOriginQuota(uint64_t quota) { m_cacheStoragePerOriginQuota = quota; }
84
85 #if PLATFORM(COCOA) || USE(CFURLCONNECTION)
86     WEBCORE_EXPORT static void ensureSession(PAL::SessionID, const String& identifierBase, RetainPtr<CFHTTPCookieStorageRef>&&);
87     NetworkStorageSession(PAL::SessionID, RetainPtr<CFURLStorageSessionRef>&&, RetainPtr<CFHTTPCookieStorageRef>&&);
88
89     // May be null, in which case a Foundation default should be used.
90     CFURLStorageSessionRef platformSession() { return m_platformSession.get(); }
91     WEBCORE_EXPORT RetainPtr<CFHTTPCookieStorageRef> cookieStorage() const;
92     WEBCORE_EXPORT static void setCookieStoragePartitioningEnabled(bool);
93     WEBCORE_EXPORT static void setStorageAccessAPIEnabled(bool);
94 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
95     WEBCORE_EXPORT String cookieStoragePartition(const ResourceRequest&, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID) const;
96     WEBCORE_EXPORT bool shouldBlockCookies(const ResourceRequest&) const;
97     bool shouldBlockCookies(const URL& firstPartyForCookies, const URL& resource) const;
98     String cookieStoragePartition(const URL& firstPartyForCookies, const URL& resource, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID) const;
99     WEBCORE_EXPORT void setPrevalentDomainsToPartitionOrBlockCookies(const Vector<String>& domainsToPartition, const Vector<String>& domainsToBlock, const Vector<String>& domainsToNeitherPartitionNorBlock, bool clearFirst);
100     WEBCORE_EXPORT void removePrevalentDomains(const Vector<String>& domains);
101     WEBCORE_EXPORT bool hasStorageAccessForFrame(const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID) const;
102     WEBCORE_EXPORT void grantStorageAccessForFrame(const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID);
103     WEBCORE_EXPORT void removeStorageAccessForFrame(uint64_t frameID, uint64_t pageID);
104     WEBCORE_EXPORT void removeStorageAccessForAllFramesOnPage(uint64_t pageID);
105 #endif
106 #elif USE(SOUP)
107     NetworkStorageSession(PAL::SessionID, std::unique_ptr<SoupNetworkSession>&&);
108     ~NetworkStorageSession();
109
110     SoupNetworkSession* soupNetworkSession() const { return m_session.get(); };
111     SoupNetworkSession& getOrCreateSoupNetworkSession() const;
112     void clearSoupNetworkSessionAndCookieStorage();
113     SoupCookieJar* cookieStorage() const;
114     void setCookieStorage(SoupCookieJar*);
115     void setCookieObserverHandler(Function<void ()>&&);
116     void getCredentialFromPersistentStorage(const ProtectionSpace&, Function<void (Credential&&)> completionHandler);
117     void saveCredentialToPersistentStorage(const ProtectionSpace&, const Credential&);
118 #else
119     NetworkStorageSession(PAL::SessionID, NetworkingContext*);
120     ~NetworkStorageSession();
121
122     NetworkingContext* context() const;
123 #endif
124
125     WEBCORE_EXPORT void setCookie(const Cookie&);
126     WEBCORE_EXPORT void setCookies(const Vector<Cookie>&, const URL&, const URL& mainDocumentURL);
127     WEBCORE_EXPORT void deleteCookie(const Cookie&);
128     WEBCORE_EXPORT Vector<Cookie> getAllCookies();
129     WEBCORE_EXPORT Vector<Cookie> getCookies(const URL&);
130     WEBCORE_EXPORT void flushCookieStore();
131
132 private:
133     static HashMap<PAL::SessionID, std::unique_ptr<NetworkStorageSession>>& globalSessionMap();
134     PAL::SessionID m_sessionID;
135
136 #if PLATFORM(COCOA) || USE(CFURLCONNECTION)
137     RetainPtr<CFURLStorageSessionRef> m_platformSession;
138     RetainPtr<CFHTTPCookieStorageRef> m_platformCookieStorage;
139 #elif USE(SOUP)
140     static void cookiesDidChange(NetworkStorageSession*);
141
142     mutable std::unique_ptr<SoupNetworkSession> m_session;
143     GRefPtr<SoupCookieJar> m_cookieStorage;
144     Function<void ()> m_cookieObserverHandler;
145 #if USE(LIBSECRET)
146     Function<void (Credential&&)> m_persisentStorageCompletionHandler;
147     GRefPtr<GCancellable> m_persisentStorageCancellable;
148 #endif
149 #else
150     RefPtr<NetworkingContext> m_context;
151 #endif
152
153     CredentialStorage m_credentialStorage;
154
155     String m_cacheStorageDirectory;
156     uint64_t m_cacheStoragePerOriginQuota { 0 };
157
158 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
159     bool shouldPartitionCookies(const String& topPrivatelyControlledDomain) const;
160     bool shouldBlockThirdPartyCookies(const String& topPrivatelyControlledDomain) const;
161     HashSet<String> m_topPrivatelyControlledDomainsToPartition;
162     HashSet<String> m_topPrivatelyControlledDomainsToBlock;
163     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;
164 #endif
165
166 #if PLATFORM(COCOA)
167 public:
168     CookieStorageObserver& cookieStorageObserver() const;
169
170 private:
171     mutable RefPtr<CookieStorageObserver> m_cookieStorageObserver;
172 #endif
173 };
174
175 #if PLATFORM(COCOA)
176 WEBCORE_EXPORT CFURLStorageSessionRef createPrivateStorageSession(CFStringRef identifier);
177 #endif
178
179 }