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