Adopt WebCore::RegistrableDomain in WebCore::ResourceLoadStatistics and WebKit::Netwo...
[WebKit-https.git] / Source / WebKit / UIProcess / WebsiteData / WebsiteDataStore.h
1 /*
2  * Copyright (C) 2014-2019 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 "NetworkSessionCreationParameters.h"
29 #include "WebProcessLifetimeObserver.h"
30 #include "WebsiteDataStoreClient.h"
31 #include "WebsiteDataStoreConfiguration.h"
32 #include <WebCore/Cookie.h>
33 #include <WebCore/SecurityOriginData.h>
34 #include <WebCore/SecurityOriginHash.h>
35 #include <pal/SessionID.h>
36 #include <wtf/Function.h>
37 #include <wtf/HashSet.h>
38 #include <wtf/Identified.h>
39 #include <wtf/OptionSet.h>
40 #include <wtf/RefCounted.h>
41 #include <wtf/RefPtr.h>
42 #include <wtf/UniqueRef.h>
43 #include <wtf/WeakPtr.h>
44 #include <wtf/WorkQueue.h>
45 #include <wtf/text/WTFString.h>
46
47 #if PLATFORM(COCOA)
48 #include <pal/spi/cf/CFNetworkSPI.h>
49 #endif
50
51 #if USE(CURL)
52 #include <WebCore/CurlProxySettings.h>
53 #endif
54
55 #if USE(APPLE_INTERNAL_SDK)
56 #include <WebKitAdditions/WebsiteDataStoreAdditions.h>
57 #endif
58
59 namespace API {
60 class HTTPCookieStore;
61 }
62
63 namespace WebCore {
64 class SecurityOrigin;
65 }
66
67 namespace WebKit {
68
69 class AuthenticatorManager;
70 class SecKeyProxyStore;
71 class StorageManager;
72 class DeviceIdHashSaltStorage;
73 class WebPageProxy;
74 class WebProcessPool;
75 class WebResourceLoadStatisticsStore;
76 enum class WebsiteDataFetchOption;
77 enum class WebsiteDataType;
78 struct MockWebAuthenticationConfiguration;
79 struct WebsiteDataRecord;
80 struct WebsiteDataStoreParameters;
81
82 #if ENABLE(RESOURCE_LOAD_STATISTICS)
83 enum class ShouldGrandfatherStatistics : bool;
84 enum class StorageAccessStatus : uint8_t;
85 enum class StorageAccessPromptStatus;
86 #endif
87
88 #if ENABLE(NETSCAPE_PLUGIN_API)
89 struct PluginModuleInfo;
90 #endif
91
92 class WebsiteDataStore : public RefCounted<WebsiteDataStore>, public WebProcessLifetimeObserver, public Identified<WebsiteDataStore>, public CanMakeWeakPtr<WebsiteDataStore>  {
93 public:
94     static Ref<WebsiteDataStore> createNonPersistent();
95     static Ref<WebsiteDataStore> create(Ref<WebsiteDataStoreConfiguration>&&, PAL::SessionID);
96     virtual ~WebsiteDataStore();
97
98     static WebsiteDataStore* existingNonDefaultDataStoreForSessionID(PAL::SessionID);
99
100     bool isPersistent() const { return !m_sessionID.isEphemeral(); }
101     PAL::SessionID sessionID() const { return m_sessionID; }
102
103     bool resourceLoadStatisticsEnabled() const;
104     void setResourceLoadStatisticsEnabled(bool);
105     bool resourceLoadStatisticsDebugMode() const;
106     void setResourceLoadStatisticsDebugMode(bool);
107     void setResourceLoadStatisticsDebugMode(bool, CompletionHandler<void()>&&);
108
109     uint64_t cacheStoragePerOriginQuota() const { return m_resolvedConfiguration->cacheStoragePerOriginQuota(); }
110     void setCacheStoragePerOriginQuota(uint64_t quota) { m_resolvedConfiguration->setCacheStoragePerOriginQuota(quota); }
111     const String& cacheStorageDirectory() const { return m_resolvedConfiguration->cacheStorageDirectory(); }
112     void setCacheStorageDirectory(String&& directory) { m_resolvedConfiguration->setCacheStorageDirectory(WTFMove(directory)); }
113     const String& serviceWorkerRegistrationDirectory() const { return m_resolvedConfiguration->serviceWorkerRegistrationDirectory(); }
114     void setServiceWorkerRegistrationDirectory(String&& directory) { m_resolvedConfiguration->setServiceWorkerRegistrationDirectory(WTFMove(directory)); }
115
116 #if ENABLE(RESOURCE_LOAD_STATISTICS)
117     WebResourceLoadStatisticsStore* resourceLoadStatistics() const { return m_resourceLoadStatistics.get(); }
118     void clearResourceLoadStatisticsInWebProcesses(CompletionHandler<void()>&&);
119 #endif
120
121     static void cloneSessionData(WebPageProxy& sourcePage, WebPageProxy& newPage);
122
123     void fetchData(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, Function<void(Vector<WebsiteDataRecord>)>&& completionHandler);
124     void removeData(OptionSet<WebsiteDataType>, WallTime modifiedSince, Function<void()>&& completionHandler);
125     void removeData(OptionSet<WebsiteDataType>, const Vector<WebsiteDataRecord>&, Function<void()>&& completionHandler);
126
127 #if ENABLE(RESOURCE_LOAD_STATISTICS)
128     void clearPrevalentResource(const URL&, CompletionHandler<void()>&&);
129     void clearUserInteraction(const URL&, CompletionHandler<void()>&&);
130     void dumpResourceLoadStatistics(CompletionHandler<void(const String&)>&&);
131     void logTestingEvent(const String&);
132     void logUserInteraction(const URL&, CompletionHandler<void()>&&);
133     void getAllStorageAccessEntries(uint64_t pageID, CompletionHandler<void(Vector<String>&& domains)>&&);
134     void hasHadUserInteraction(const URL&, CompletionHandler<void(bool)>&&);
135     void isPrevalentResource(const URL&, CompletionHandler<void(bool)>&&);
136     void isRegisteredAsRedirectingTo(const URL& hostRedirectedFrom, const URL& hostRedirectedTo, CompletionHandler<void(bool)>&&);
137     void isRegisteredAsSubresourceUnder(const URL& subresource, const URL& topFrame, CompletionHandler<void(bool)>&&);
138     void isRegisteredAsSubFrameUnder(const URL& subFrame, const URL& topFrame, CompletionHandler<void(bool)>&&);
139     void isVeryPrevalentResource(const URL&, CompletionHandler<void(bool)>&&);
140     void resetParametersToDefaultValues(CompletionHandler<void()>&&);
141     void scheduleCookieBlockingUpdate(CompletionHandler<void()>&&);
142     void scheduleClearInMemoryAndPersistent(WallTime modifiedSince, ShouldGrandfatherStatistics, CompletionHandler<void()>&&);
143     void scheduleClearInMemoryAndPersistent(ShouldGrandfatherStatistics, CompletionHandler<void()>&&);
144     void scheduleStatisticsAndDataRecordsProcessing(CompletionHandler<void()>&&);
145     void submitTelemetry();
146     void setGrandfathered(const URL&, bool, CompletionHandler<void()>&&);
147     void setGrandfatheringTime(Seconds, CompletionHandler<void()>&&);
148     void setLastSeen(const URL&, Seconds, CompletionHandler<void()>&&);
149     void setNotifyPagesWhenDataRecordsWereScanned(bool, CompletionHandler<void()>&&);
150     void setPruneEntriesDownTo(size_t, CompletionHandler<void()>&&);
151     void setSubframeUnderTopFrameDomain(const URL& subframe, const URL& topFrame, CompletionHandler<void()>&&);
152     void setSubresourceUnderTopFrameDomain(const URL& subresource, const URL& topFrame, CompletionHandler<void()>&&);
153     void setSubresourceUniqueRedirectTo(const URL& subresource, const URL& hostNameRedirectedTo, CompletionHandler<void()>&&);
154     void setSubresourceUniqueRedirectFrom(const URL& subresource, const URL& hostNameRedirectedFrom, CompletionHandler<void()>&&);
155     void setTimeToLiveUserInteraction(Seconds, CompletionHandler<void()>&&);
156     void setTopFrameUniqueRedirectTo(const URL& topFrameHostName, const URL& hostNameRedirectedTo, CompletionHandler<void()>&&);
157     void setTopFrameUniqueRedirectFrom(const URL& topFrameHostName, const URL& hostNameRedirectedFrom, CompletionHandler<void()>&&);
158     void setMaxStatisticsEntries(size_t, CompletionHandler<void()>&&);
159     void setMinimumTimeBetweenDataRecordsRemoval(Seconds, CompletionHandler<void()>&&);
160     void setNotifyPagesWhenTelemetryWasCaptured(bool, CompletionHandler<void()>&&);
161     void setPrevalentResource(const URL&, CompletionHandler<void()>&&);
162     void setPrevalentResourceForDebugMode(const URL&, CompletionHandler<void()>&&);
163     void setShouldClassifyResourcesBeforeDataRecordsRemoval(bool, CompletionHandler<void()>&&);
164     void setStatisticsTestingCallback(WTF::Function<void(const String&)>&& callback) { m_statisticsTestingCallback = WTFMove(callback); }
165     void setVeryPrevalentResource(const URL&, CompletionHandler<void()>&&);
166     void hasStorageAccess(const String& subFrameHost, const String& topFrameHost, uint64_t frameID, uint64_t pageID, CompletionHandler<void(bool)>&&);
167     void requestStorageAccess(const String& subFrameHost, const String& topFrameHost, uint64_t frameID, uint64_t pageID, bool promptEnabled, CompletionHandler<void(StorageAccessStatus)>&&);
168     void grantStorageAccess(String&& subFrameHost, String&& topFrameHost, uint64_t frameID, uint64_t pageID, bool userWasPrompted, CompletionHandler<void(bool)>&&);
169     void setSubframeUnderTopFrameDomain(const URL& subframe, const URL& topFrame);
170 #endif
171     void setCacheMaxAgeCapForPrevalentResources(Seconds, CompletionHandler<void()>&&);
172     void resetCacheMaxAgeCapForPrevalentResources(CompletionHandler<void()>&&);
173     void resolveDirectoriesIfNecessary();
174     const String& resolvedApplicationCacheDirectory() const { return m_resolvedConfiguration->applicationCacheDirectory(); }
175     const String& resolvedMediaCacheDirectory() const { return m_resolvedConfiguration->mediaCacheDirectory(); }
176     const String& resolvedMediaKeysDirectory() const { return m_resolvedConfiguration->mediaKeysStorageDirectory(); }
177     const String& resolvedDatabaseDirectory() const { return m_resolvedConfiguration->webSQLDatabaseDirectory(); }
178     const String& resolvedJavaScriptConfigurationDirectory() const { return m_resolvedConfiguration->javaScriptConfigurationDirectory(); }
179     const String& resolvedCookieStorageFile() const { return m_resolvedConfiguration->cookieStorageFile(); }
180     const String& resolvedIndexedDatabaseDirectory() const { return m_resolvedConfiguration->indexedDBDatabaseDirectory(); }
181     const String& resolvedServiceWorkerRegistrationDirectory() const { return m_resolvedConfiguration->serviceWorkerRegistrationDirectory(); }
182     const String& resolvedResourceLoadStatisticsDirectory() const { return m_resolvedConfiguration->resourceLoadStatisticsDirectory(); }
183
184     StorageManager* storageManager() { return m_storageManager.get(); }
185
186     DeviceIdHashSaltStorage& deviceIdHashSaltStorage() { return m_deviceIdHashSaltStorage.get(); }
187
188     WebProcessPool* processPoolForCookieStorageOperations();
189     bool isAssociatedProcessPool(WebProcessPool&) const;
190
191     WebsiteDataStoreParameters parameters();
192
193     Vector<WebCore::Cookie> pendingCookies() const;
194     void addPendingCookie(const WebCore::Cookie&);
195     void removePendingCookie(const WebCore::Cookie&);
196     void clearPendingCookies();
197
198     void enableResourceLoadStatisticsAndSetTestingCallback(Function<void (const String&)>&& callback);
199
200     void setBoundInterfaceIdentifier(String&& identifier) { m_boundInterfaceIdentifier = WTFMove(identifier); }
201     const String& boundInterfaceIdentifier() { return m_boundInterfaceIdentifier; }
202     
203     void setAllowsCellularAccess(AllowsCellularAccess allows) { m_allowsCellularAccess = allows; }
204     AllowsCellularAccess allowsCellularAccess() { return m_allowsCellularAccess; }
205
206 #if PLATFORM(COCOA)
207     void setProxyConfiguration(CFDictionaryRef configuration) { m_proxyConfiguration = configuration; }
208     CFDictionaryRef proxyConfiguration() { return m_proxyConfiguration.get(); }
209 #endif
210
211 #if USE(CURL)
212     void setNetworkProxySettings(WebCore::CurlProxySettings&&);
213     const WebCore::CurlProxySettings& networkProxySettings() const { return m_proxySettings; }
214 #endif
215
216     static void allowWebsiteDataRecordsForAllOrigins();
217
218 #if HAVE(SEC_KEY_PROXY)
219     void addSecKeyProxyStore(Ref<SecKeyProxyStore>&&);
220 #endif
221
222 #if ENABLE(WEB_AUTHN)
223     AuthenticatorManager& authenticatorManager() { return m_authenticatorManager.get(); }
224     void setMockWebAuthenticationConfiguration(MockWebAuthenticationConfiguration&&);
225 #endif
226
227     void didCreateNetworkProcess();
228
229     const WebsiteDataStoreConfiguration& configuration() { return m_configuration.get(); }
230
231     WebsiteDataStoreClient& client() { return m_client.get(); }
232     void setClient(UniqueRef<WebsiteDataStoreClient>&& client) { m_client = WTFMove(client); }
233
234     API::HTTPCookieStore& cookieStore();
235
236 #if HAVE(LOAD_OPTIMIZER)
237 WEBSITEDATASTORE_LOADOPTIMIZER_ADDITIONS_1
238 #endif
239
240 private:
241     explicit WebsiteDataStore(PAL::SessionID);
242     explicit WebsiteDataStore(Ref<WebsiteDataStoreConfiguration>&&, PAL::SessionID);
243
244     void fetchDataAndApply(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, RefPtr<WorkQueue>&&, Function<void(Vector<WebsiteDataRecord>)>&& apply);
245
246     // WebProcessLifetimeObserver.
247     void webPageWasAdded(WebPageProxy&) override;
248     void webPageWasInvalidated(WebPageProxy&) override;
249     void webProcessWillOpenConnection(WebProcessProxy&, IPC::Connection&) override;
250     void webPageWillOpenConnection(WebPageProxy&, IPC::Connection&) override;
251     void webPageDidCloseConnection(WebPageProxy&, IPC::Connection&) override;
252     void webProcessDidCloseConnection(WebProcessProxy&, IPC::Connection&) override;
253
254     void platformInitialize();
255     void platformDestroy();
256     static void platformRemoveRecentSearches(WallTime);
257
258 #if USE(CURL) || USE(SOUP)
259     void platformSetNetworkParameters(WebsiteDataStoreParameters&);
260 #endif
261
262     HashSet<RefPtr<WebProcessPool>> processPools(size_t count = std::numeric_limits<size_t>::max(), bool ensureAPoolExists = true) const;
263
264 #if ENABLE(NETSCAPE_PLUGIN_API)
265     Vector<PluginModuleInfo> plugins() const;
266 #endif
267
268     static Vector<WebCore::SecurityOriginData> mediaKeyOrigins(const String& mediaKeysStorageDirectory);
269     static void removeMediaKeys(const String& mediaKeysStorageDirectory, WallTime modifiedSince);
270     static void removeMediaKeys(const String& mediaKeysStorageDirectory, const HashSet<WebCore::SecurityOriginData>&);
271
272     void maybeRegisterWithSessionIDMap();
273
274     const PAL::SessionID m_sessionID;
275
276     Ref<WebsiteDataStoreConfiguration> m_resolvedConfiguration;
277     Ref<const WebsiteDataStoreConfiguration> m_configuration;
278     bool m_hasResolvedDirectories { false };
279
280     const RefPtr<StorageManager> m_storageManager;
281     const Ref<DeviceIdHashSaltStorage> m_deviceIdHashSaltStorage;
282
283 #if ENABLE(RESOURCE_LOAD_STATISTICS)
284     RefPtr<WebResourceLoadStatisticsStore> m_resourceLoadStatistics;
285     bool m_resourceLoadStatisticsDebugMode { false };
286     bool m_resourceLoadStatisticsEnabled { false };
287     WTF::Function<void(const String&)> m_statisticsTestingCallback;
288 #endif
289
290     Ref<WorkQueue> m_queue;
291
292 #if PLATFORM(COCOA)
293     Vector<uint8_t> m_uiProcessCookieStorageIdentifier;
294     RetainPtr<CFHTTPCookieStorageRef> m_cfCookieStorage;
295     RetainPtr<CFDictionaryRef> m_proxyConfiguration;
296 #endif
297
298 #if USE(CURL)
299     WebCore::CurlProxySettings m_proxySettings;
300 #endif
301
302     HashSet<WebCore::Cookie> m_pendingCookies;
303
304     String m_boundInterfaceIdentifier;
305     AllowsCellularAccess m_allowsCellularAccess { AllowsCellularAccess::Yes };
306
307 #if HAVE(SEC_KEY_PROXY)
308     Vector<Ref<SecKeyProxyStore>> m_secKeyProxyStores;
309 #endif
310
311 #if ENABLE(WEB_AUTHN)
312     UniqueRef<AuthenticatorManager> m_authenticatorManager;
313 #endif
314
315     UniqueRef<WebsiteDataStoreClient> m_client;
316
317     RefPtr<API::HTTPCookieStore> m_cookieStore;
318 };
319
320 }