Add telemetry to track storage access API adoption
[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 #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 bool hasStorageAccessForFrame(const ResourceRequest&, uint64_t frameID, uint64_t pageID) const;
103     WEBCORE_EXPORT void grantStorageAccessForFrame(const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID);
104     WEBCORE_EXPORT void removeStorageAccessForFrame(uint64_t frameID, uint64_t pageID);
105     WEBCORE_EXPORT void removeStorageAccessForAllFramesOnPage(uint64_t pageID);
106 #endif
107 #elif USE(SOUP)
108     NetworkStorageSession(PAL::SessionID, std::unique_ptr<SoupNetworkSession>&&);
109     ~NetworkStorageSession();
110
111     SoupNetworkSession* soupNetworkSession() const { return m_session.get(); };
112     SoupNetworkSession& getOrCreateSoupNetworkSession() const;
113     void clearSoupNetworkSessionAndCookieStorage();
114     SoupCookieJar* cookieStorage() const;
115     void setCookieStorage(SoupCookieJar*);
116     void setCookieObserverHandler(Function<void ()>&&);
117     void getCredentialFromPersistentStorage(const ProtectionSpace&, Function<void (Credential&&)> completionHandler);
118     void saveCredentialToPersistentStorage(const ProtectionSpace&, const Credential&);
119 #else
120     NetworkStorageSession(PAL::SessionID, NetworkingContext*);
121     ~NetworkStorageSession();
122
123     NetworkingContext* context() const;
124 #endif
125
126     WEBCORE_EXPORT void setCookie(const Cookie&);
127     WEBCORE_EXPORT void setCookies(const Vector<Cookie>&, const URL&, const URL& mainDocumentURL);
128     WEBCORE_EXPORT void deleteCookie(const Cookie&);
129     WEBCORE_EXPORT Vector<Cookie> getAllCookies();
130     WEBCORE_EXPORT Vector<Cookie> getCookies(const URL&);
131     WEBCORE_EXPORT void flushCookieStore();
132
133 private:
134     static HashMap<PAL::SessionID, std::unique_ptr<NetworkStorageSession>>& globalSessionMap();
135     PAL::SessionID m_sessionID;
136
137 #if PLATFORM(COCOA) || USE(CFURLCONNECTION)
138     RetainPtr<CFURLStorageSessionRef> m_platformSession;
139     RetainPtr<CFHTTPCookieStorageRef> m_platformCookieStorage;
140 #elif USE(SOUP)
141     static void cookiesDidChange(NetworkStorageSession*);
142
143     mutable std::unique_ptr<SoupNetworkSession> m_session;
144     GRefPtr<SoupCookieJar> m_cookieStorage;
145     Function<void ()> m_cookieObserverHandler;
146 #if USE(LIBSECRET)
147     Function<void (Credential&&)> m_persisentStorageCompletionHandler;
148     GRefPtr<GCancellable> m_persisentStorageCancellable;
149 #endif
150 #else
151     RefPtr<NetworkingContext> m_context;
152 #endif
153
154     CredentialStorage m_credentialStorage;
155
156     String m_cacheStorageDirectory;
157     uint64_t m_cacheStoragePerOriginQuota { 0 };
158
159 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
160     bool shouldPartitionCookies(const String& topPrivatelyControlledDomain) const;
161     bool shouldBlockThirdPartyCookies(const String& topPrivatelyControlledDomain) const;
162     HashSet<String> m_topPrivatelyControlledDomainsToPartition;
163     HashSet<String> m_topPrivatelyControlledDomainsToBlock;
164     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;
165 #endif
166
167 #if PLATFORM(COCOA)
168 public:
169     CookieStorageObserver& cookieStorageObserver() const;
170
171 private:
172     mutable RefPtr<CookieStorageObserver> m_cookieStorageObserver;
173 #endif
174 };
175
176 #if PLATFORM(COCOA)
177 WEBCORE_EXPORT CFURLStorageSessionRef createPrivateStorageSession(CFStringRef identifier);
178 #endif
179
180 }