4d49091a787b7fd5a83ef03f76fcfcdc9cd6e1e5
[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 "LocalStorageDatabaseTracker.h"
29 #include "NetworkSessionCreationParameters.h"
30 #include "WebDeviceOrientationAndMotionAccessController.h"
31 #include "WebPageProxyIdentifier.h"
32 #include "WebsiteDataStoreClient.h"
33 #include "WebsiteDataStoreConfiguration.h"
34 #include <WebCore/Cookie.h>
35 #include <WebCore/DeviceOrientationOrMotionPermissionState.h>
36 #include <WebCore/PageIdentifier.h>
37 #include <WebCore/SecurityOriginData.h>
38 #include <WebCore/SecurityOriginHash.h>
39 #include <pal/SessionID.h>
40 #include <wtf/Function.h>
41 #include <wtf/HashSet.h>
42 #include <wtf/Identified.h>
43 #include <wtf/OptionSet.h>
44 #include <wtf/RefCounted.h>
45 #include <wtf/RefPtr.h>
46 #include <wtf/UniqueRef.h>
47 #include <wtf/WeakHashSet.h>
48 #include <wtf/WeakPtr.h>
49 #include <wtf/WorkQueue.h>
50 #include <wtf/text/WTFString.h>
51
52 #if PLATFORM(COCOA)
53 #include <pal/spi/cf/CFNetworkSPI.h>
54 #endif
55
56 #if USE(CURL)
57 #include <WebCore/CurlProxySettings.h>
58 #endif
59
60 namespace API {
61 class HTTPCookieStore;
62 }
63
64 namespace WebCore {
65 class RegistrableDomain;
66 class SecurityOrigin;
67 }
68
69 namespace WebKit {
70
71 class AuthenticatorManager;
72 class SecKeyProxyStore;
73 class DeviceIdHashSaltStorage;
74 class SOAuthorizationCoordinator;
75 class WebPageProxy;
76 class WebProcessPool;
77 class WebProcessProxy;
78 class WebResourceLoadStatisticsStore;
79 enum class WebsiteDataFetchOption;
80 enum class WebsiteDataType;
81 struct MockWebAuthenticationConfiguration;
82 struct WebsiteDataRecord;
83 struct WebsiteDataStoreParameters;
84
85 #if ENABLE(RESOURCE_LOAD_STATISTICS)
86 enum class ShouldGrandfatherStatistics : bool;
87 enum class StorageAccessStatus : uint8_t;
88 enum class StorageAccessPromptStatus;
89 #endif
90
91 #if ENABLE(NETSCAPE_PLUGIN_API)
92 struct PluginModuleInfo;
93 #endif
94
95 class WebsiteDataStore : public API::ObjectImpl<API::Object::Type::WebsiteDataStore>, public Identified<WebsiteDataStore>, public CanMakeWeakPtr<WebsiteDataStore>  {
96 public:
97     static Ref<WebsiteDataStore> defaultDataStore();
98     static bool defaultDataStoreExists();
99     static void deleteDefaultDataStoreForTesting();
100     
101     static Ref<WebsiteDataStore> createNonPersistent();
102     static Ref<WebsiteDataStore> create(Ref<WebsiteDataStoreConfiguration>&&, PAL::SessionID);
103
104     WebsiteDataStore(Ref<WebsiteDataStoreConfiguration>&&, PAL::SessionID);
105     ~WebsiteDataStore();
106
107     static WebsiteDataStore* existingNonDefaultDataStoreForSessionID(PAL::SessionID);
108
109     bool isPersistent() const { return !m_sessionID.isEphemeral(); }
110     PAL::SessionID sessionID() const { return m_sessionID; }
111     
112     void registerProcess(WebProcessProxy&);
113     void unregisterProcess(WebProcessProxy&);
114     
115     const WeakHashSet<WebProcessProxy>& processes() const { return m_processes; }
116
117     bool resourceLoadStatisticsEnabled() const;
118     void setResourceLoadStatisticsEnabled(bool);
119     bool resourceLoadStatisticsDebugMode() const;
120     void setResourceLoadStatisticsDebugMode(bool);
121     void setResourceLoadStatisticsDebugMode(bool, CompletionHandler<void()>&&);
122
123     uint64_t perOriginStorageQuota() const { return m_resolvedConfiguration->perOriginStorageQuota(); }
124     uint64_t perThirdPartyOriginStorageQuota() const;
125     const String& cacheStorageDirectory() const { return m_resolvedConfiguration->cacheStorageDirectory(); }
126     void setCacheStorageDirectory(String&& directory) { m_resolvedConfiguration->setCacheStorageDirectory(WTFMove(directory)); }
127     const String& serviceWorkerRegistrationDirectory() const { return m_resolvedConfiguration->serviceWorkerRegistrationDirectory(); }
128     void setServiceWorkerRegistrationDirectory(String&& directory) { m_resolvedConfiguration->setServiceWorkerRegistrationDirectory(WTFMove(directory)); }
129
130 #if ENABLE(RESOURCE_LOAD_STATISTICS)
131     WebResourceLoadStatisticsStore* resourceLoadStatistics() const { return m_resourceLoadStatistics.get(); }
132     void clearResourceLoadStatisticsInWebProcesses(CompletionHandler<void()>&&);
133 #endif
134
135     void fetchData(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, Function<void(Vector<WebsiteDataRecord>)>&& completionHandler);
136     void removeData(OptionSet<WebsiteDataType>, WallTime modifiedSince, Function<void()>&& completionHandler);
137     void removeData(OptionSet<WebsiteDataType>, const Vector<WebsiteDataRecord>&, Function<void()>&& completionHandler);
138
139     void getLocalStorageDetails(Function<void(Vector<LocalStorageDatabaseTracker::OriginDetails>&&)>&&);
140
141 #if ENABLE(RESOURCE_LOAD_STATISTICS)
142     void fetchDataForRegistrableDomains(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, const Vector<WebCore::RegistrableDomain>&, CompletionHandler<void(Vector<WebsiteDataRecord>&&, HashSet<WebCore::RegistrableDomain>&&)>&&);
143     void clearPrevalentResource(const URL&, CompletionHandler<void()>&&);
144     void clearUserInteraction(const URL&, CompletionHandler<void()>&&);
145     void dumpResourceLoadStatistics(CompletionHandler<void(const String&)>&&);
146     void logTestingEvent(const String&);
147     void logUserInteraction(const URL&, CompletionHandler<void()>&&);
148     void getAllStorageAccessEntries(WebPageProxyIdentifier, CompletionHandler<void(Vector<String>&& domains)>&&);
149     void hasHadUserInteraction(const URL&, CompletionHandler<void(bool)>&&);
150     void isRelationshipOnlyInDatabaseOnce(const URL& subUrl, const URL& topUrl, CompletionHandler<void(bool)>&&);
151     void isPrevalentResource(const URL&, CompletionHandler<void(bool)>&&);
152     void isRegisteredAsRedirectingTo(const URL& hostRedirectedFrom, const URL& hostRedirectedTo, CompletionHandler<void(bool)>&&);
153     void isRegisteredAsSubresourceUnder(const URL& subresource, const URL& topFrame, CompletionHandler<void(bool)>&&);
154     void isRegisteredAsSubFrameUnder(const URL& subFrame, const URL& topFrame, CompletionHandler<void(bool)>&&);
155     void isVeryPrevalentResource(const URL&, CompletionHandler<void(bool)>&&);
156     void resetParametersToDefaultValues(CompletionHandler<void()>&&);
157     void scheduleCookieBlockingUpdate(CompletionHandler<void()>&&);
158     void scheduleClearInMemoryAndPersistent(WallTime modifiedSince, ShouldGrandfatherStatistics, CompletionHandler<void()>&&);
159     void scheduleClearInMemoryAndPersistent(ShouldGrandfatherStatistics, CompletionHandler<void()>&&);
160     void scheduleStatisticsAndDataRecordsProcessing(CompletionHandler<void()>&&);
161     void submitTelemetry();
162     void setGrandfathered(const URL&, bool, CompletionHandler<void()>&&);
163     void isGrandfathered(const URL&, CompletionHandler<void(bool)>&&);
164     void setUseITPDatabase(bool);
165     void setGrandfatheringTime(Seconds, CompletionHandler<void()>&&);
166     void setLastSeen(const URL&, Seconds, CompletionHandler<void()>&&);
167     void mergeStatisticForTesting(const URL&, const URL& topFrameUrl1, const URL& topFrameUrl2, Seconds lastSeen, bool hadUserInteraction, Seconds mostRecentUserInteraction, bool isGrandfathered, bool isPrevalent, bool isVeryPrevalent, unsigned dataRecordsRemoved, CompletionHandler<void()>&&);
168     void setNotifyPagesWhenDataRecordsWereScanned(bool, CompletionHandler<void()>&&);
169     void setIsRunningResourceLoadStatisticsTest(bool, CompletionHandler<void()>&&);
170     void setPruneEntriesDownTo(size_t, CompletionHandler<void()>&&);
171     void setSubframeUnderTopFrameDomain(const URL& subframe, const URL& topFrame, CompletionHandler<void()>&&);
172     void setSubresourceUnderTopFrameDomain(const URL& subresource, const URL& topFrame, CompletionHandler<void()>&&);
173     void setSubresourceUniqueRedirectTo(const URL& subresource, const URL& hostNameRedirectedTo, CompletionHandler<void()>&&);
174     void setSubresourceUniqueRedirectFrom(const URL& subresource, const URL& hostNameRedirectedFrom, CompletionHandler<void()>&&);
175     void setTimeToLiveUserInteraction(Seconds, CompletionHandler<void()>&&);
176     void setTopFrameUniqueRedirectTo(const URL& topFrameHostName, const URL& hostNameRedirectedTo, CompletionHandler<void()>&&);
177     void setTopFrameUniqueRedirectFrom(const URL& topFrameHostName, const URL& hostNameRedirectedFrom, CompletionHandler<void()>&&);
178     void setMaxStatisticsEntries(size_t, CompletionHandler<void()>&&);
179     void setMinimumTimeBetweenDataRecordsRemoval(Seconds, CompletionHandler<void()>&&);
180     void setNotifyPagesWhenTelemetryWasCaptured(bool, CompletionHandler<void()>&&);
181     void setPrevalentResource(const URL&, CompletionHandler<void()>&&);
182     void setPrevalentResourceForDebugMode(const URL&, CompletionHandler<void()>&&);
183     void setShouldClassifyResourcesBeforeDataRecordsRemoval(bool, CompletionHandler<void()>&&);
184     void setStatisticsTestingCallback(Function<void(const String&)>&&);
185     bool hasStatisticsTestingCallback() const { return !!m_statisticsTestingCallback; }
186     void setVeryPrevalentResource(const URL&, CompletionHandler<void()>&&);
187     void setSubframeUnderTopFrameDomain(const URL& subframe, const URL& topFrame);
188     void setCrossSiteLoadWithLinkDecorationForTesting(const URL& fromURL, const URL& toURL, CompletionHandler<void()>&&);
189     void resetCrossSiteLoadsWithLinkDecorationForTesting(CompletionHandler<void()>&&);
190     void deleteCookiesForTesting(const URL&, bool includeHttpOnlyCookies, CompletionHandler<void()>&&);
191     void hasLocalStorageForTesting(const URL&, CompletionHandler<void(bool)>&&) const;
192     void hasIsolatedSessionForTesting(const URL&, CompletionHandler<void(bool)>&&) const;
193     void setResourceLoadStatisticsShouldDowngradeReferrerForTesting(bool, CompletionHandler<void()>&&);
194 #endif
195     void setCacheMaxAgeCapForPrevalentResources(Seconds, CompletionHandler<void()>&&);
196     void resetCacheMaxAgeCapForPrevalentResources(CompletionHandler<void()>&&);
197     void resolveDirectoriesIfNecessary();
198     const String& applicationCacheFlatFileSubdirectoryName() const { return m_configuration->applicationCacheFlatFileSubdirectoryName(); }
199     const String& resolvedApplicationCacheDirectory() const { return m_resolvedConfiguration->applicationCacheDirectory(); }
200     const String& resolvedLocalStorageDirectory() const { return m_resolvedConfiguration->localStorageDirectory(); }
201     const String& resolvedNetworkCacheDirectory() const { return m_resolvedConfiguration->networkCacheDirectory(); }
202     const String& resolvedMediaCacheDirectory() const { return m_resolvedConfiguration->mediaCacheDirectory(); }
203     const String& resolvedMediaKeysDirectory() const { return m_resolvedConfiguration->mediaKeysStorageDirectory(); }
204     const String& resolvedDatabaseDirectory() const { return m_resolvedConfiguration->webSQLDatabaseDirectory(); }
205     const String& resolvedJavaScriptConfigurationDirectory() const { return m_resolvedConfiguration->javaScriptConfigurationDirectory(); }
206     const String& resolvedCookieStorageFile() const { return m_resolvedConfiguration->cookieStorageFile(); }
207     const String& resolvedIndexedDatabaseDirectory() const { return m_resolvedConfiguration->indexedDBDatabaseDirectory(); }
208     const String& resolvedServiceWorkerRegistrationDirectory() const { return m_resolvedConfiguration->serviceWorkerRegistrationDirectory(); }
209     const String& resolvedResourceLoadStatisticsDirectory() const { return m_resolvedConfiguration->resourceLoadStatisticsDirectory(); }
210
211     DeviceIdHashSaltStorage& deviceIdHashSaltStorage() { return m_deviceIdHashSaltStorage.get(); }
212
213     WebProcessPool* processPoolForCookieStorageOperations();
214     bool isAssociatedProcessPool(WebProcessPool&) const;
215
216     WebsiteDataStoreParameters parameters();
217
218     Vector<WebCore::Cookie> pendingCookies() const;
219     void addPendingCookie(const WebCore::Cookie&);
220     void removePendingCookie(const WebCore::Cookie&);
221     void clearPendingCookies();
222
223     void setBoundInterfaceIdentifier(String&& identifier) { m_resolvedConfiguration->setBoundInterfaceIdentifier(WTFMove(identifier)); }
224     const String& boundInterfaceIdentifier() { return m_resolvedConfiguration->boundInterfaceIdentifier(); }
225
226     const String& sourceApplicationBundleIdentifier() const { return m_resolvedConfiguration->sourceApplicationBundleIdentifier(); }
227     bool setSourceApplicationBundleIdentifier(String&&);
228
229     const String& sourceApplicationSecondaryIdentifier() const { return m_resolvedConfiguration->sourceApplicationSecondaryIdentifier(); }
230     bool setSourceApplicationSecondaryIdentifier(String&&);
231
232     void networkingHasBegun() { m_networkingHasBegun = true; }
233     
234     void setAllowsCellularAccess(AllowsCellularAccess allows) { m_resolvedConfiguration->setAllowsCellularAccess(allows == AllowsCellularAccess::Yes); }
235     AllowsCellularAccess allowsCellularAccess() { return m_resolvedConfiguration->allowsCellularAccess() ? AllowsCellularAccess::Yes : AllowsCellularAccess::No; }
236
237 #if PLATFORM(COCOA)
238     void setProxyConfiguration(CFDictionaryRef configuration) { m_resolvedConfiguration->setProxyConfiguration(configuration); }
239     CFDictionaryRef proxyConfiguration() { return m_resolvedConfiguration->proxyConfiguration(); }
240 #endif
241
242 #if USE(CURL)
243     void setNetworkProxySettings(WebCore::CurlProxySettings&&);
244     const WebCore::CurlProxySettings& networkProxySettings() const { return m_proxySettings; }
245 #endif
246
247     static void allowWebsiteDataRecordsForAllOrigins();
248
249 #if HAVE(SEC_KEY_PROXY)
250     void addSecKeyProxyStore(Ref<SecKeyProxyStore>&&);
251 #endif
252
253 #if ENABLE(WEB_AUTHN)
254     AuthenticatorManager& authenticatorManager() { return m_authenticatorManager.get(); }
255     void setMockWebAuthenticationConfiguration(MockWebAuthenticationConfiguration&&);
256 #endif
257
258     void didCreateNetworkProcess();
259
260     const WebsiteDataStoreConfiguration& configuration() { return m_configuration.get(); }
261
262     WebsiteDataStoreClient& client() { return m_client.get(); }
263     void setClient(UniqueRef<WebsiteDataStoreClient>&& client) { m_client = WTFMove(client); }
264
265     API::HTTPCookieStore& cookieStore();
266
267 #if ENABLE(DEVICE_ORIENTATION)
268     WebDeviceOrientationAndMotionAccessController& deviceOrientationAndMotionAccessController() { return m_deviceOrientationAndMotionAccessController; }
269 #endif
270
271 #if HAVE(APP_SSO)
272     SOAuthorizationCoordinator& soAuthorizationCoordinator() { return m_soAuthorizationCoordinator.get(); }
273 #endif
274
275     static WTF::String defaultServiceWorkerRegistrationDirectory();
276     static WTF::String defaultLocalStorageDirectory();
277     static WTF::String defaultResourceLoadStatisticsDirectory();
278     static WTF::String defaultNetworkCacheDirectory();
279     static WTF::String defaultApplicationCacheDirectory();
280     static WTF::String defaultWebSQLDatabaseDirectory();
281 #if USE(GLIB)
282     static WTF::String defaultHSTSDirectory();
283 #endif
284     static WTF::String defaultIndexedDBDatabaseDirectory();
285     static WTF::String defaultCacheStorageDirectory();
286     static WTF::String defaultMediaCacheDirectory();
287     static WTF::String defaultMediaKeysStorageDirectory();
288     static WTF::String defaultDeviceIdHashSaltsStorageDirectory();
289     static WTF::String defaultJavaScriptConfigurationDirectory();
290     
291 private:
292     void fetchDataAndApply(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, RefPtr<WorkQueue>&&, Function<void(Vector<WebsiteDataRecord>)>&& apply);
293
294     void platformInitialize();
295     void platformDestroy();
296     static void platformRemoveRecentSearches(WallTime);
297
298 #if USE(CURL) || USE(SOUP)
299     void platformSetNetworkParameters(WebsiteDataStoreParameters&);
300 #endif
301
302     WebsiteDataStore();
303
304     enum class ShouldCreateDirectory { No, Yes };
305     static WTF::String tempDirectoryFileSystemRepresentation(const WTF::String& directoryName, ShouldCreateDirectory = ShouldCreateDirectory::Yes);
306     static WTF::String cacheDirectoryFileSystemRepresentation(const WTF::String& directoryName);
307     static WTF::String websiteDataDirectoryFileSystemRepresentation(const WTF::String& directoryName);
308
309     HashSet<RefPtr<WebProcessPool>> processPools(size_t count = std::numeric_limits<size_t>::max(), bool ensureAPoolExists = true) const;
310
311 #if ENABLE(NETSCAPE_PLUGIN_API)
312     Vector<PluginModuleInfo> plugins() const;
313 #endif
314
315     static Vector<WebCore::SecurityOriginData> mediaKeyOrigins(const String& mediaKeysStorageDirectory);
316     static void removeMediaKeys(const String& mediaKeysStorageDirectory, WallTime modifiedSince);
317     static void removeMediaKeys(const String& mediaKeysStorageDirectory, const HashSet<WebCore::SecurityOriginData>&);
318
319     void maybeRegisterWithSessionIDMap();
320
321     const PAL::SessionID m_sessionID;
322
323     Ref<WebsiteDataStoreConfiguration> m_resolvedConfiguration;
324     Ref<const WebsiteDataStoreConfiguration> m_configuration;
325     bool m_hasResolvedDirectories { false };
326
327     const Ref<DeviceIdHashSaltStorage> m_deviceIdHashSaltStorage;
328
329 #if ENABLE(RESOURCE_LOAD_STATISTICS)
330     RefPtr<WebResourceLoadStatisticsStore> m_resourceLoadStatistics;
331     bool m_resourceLoadStatisticsDebugMode { false };
332     bool m_resourceLoadStatisticsEnabled { false };
333     WTF::Function<void(const String&)> m_statisticsTestingCallback;
334 #endif
335
336     Ref<WorkQueue> m_queue;
337
338 #if PLATFORM(COCOA)
339     Vector<uint8_t> m_uiProcessCookieStorageIdentifier;
340     RetainPtr<CFHTTPCookieStorageRef> m_cfCookieStorage;
341 #endif
342
343 #if USE(CURL)
344     WebCore::CurlProxySettings m_proxySettings;
345 #endif
346
347     HashSet<WebCore::Cookie> m_pendingCookies;
348     
349     WeakHashSet<WebProcessProxy> m_processes;
350
351     bool m_networkingHasBegun { false };
352
353 #if HAVE(SEC_KEY_PROXY)
354     Vector<Ref<SecKeyProxyStore>> m_secKeyProxyStores;
355 #endif
356
357 #if ENABLE(WEB_AUTHN)
358     UniqueRef<AuthenticatorManager> m_authenticatorManager;
359 #endif
360
361 #if ENABLE(DEVICE_ORIENTATION)
362     WebDeviceOrientationAndMotionAccessController m_deviceOrientationAndMotionAccessController;
363 #endif
364
365     UniqueRef<WebsiteDataStoreClient> m_client;
366
367     RefPtr<API::HTTPCookieStore> m_cookieStore;
368
369 #if HAVE(APP_SSO)
370     UniqueRef<SOAuthorizationCoordinator> m_soAuthorizationCoordinator;
371 #endif
372 };
373
374 }