DeviceID hash salt manager can be NULL
[WebKit-https.git] / Source / WebKit / UIProcess / WebsiteData / WebsiteDataStore.h
1 /*
2  * Copyright (C) 2014-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 "NetworkSessionCreationParameters.h"
29 #include "WebProcessLifetimeObserver.h"
30 #include "WebsiteDataStoreConfiguration.h"
31 #include <WebCore/Cookie.h>
32 #include <WebCore/SecurityOriginData.h>
33 #include <WebCore/SecurityOriginHash.h>
34 #include <pal/SessionID.h>
35 #include <wtf/Function.h>
36 #include <wtf/HashSet.h>
37 #include <wtf/Identified.h>
38 #include <wtf/OptionSet.h>
39 #include <wtf/RefCounted.h>
40 #include <wtf/RefPtr.h>
41 #include <wtf/UniqueRef.h>
42 #include <wtf/WeakPtr.h>
43 #include <wtf/WorkQueue.h>
44 #include <wtf/text/WTFString.h>
45
46 #if PLATFORM(COCOA)
47 #include <pal/spi/cf/CFNetworkSPI.h>
48 #endif
49
50 #if USE(CURL)
51 #include <WebCore/CurlProxySettings.h>
52 #endif
53
54 namespace WebCore {
55 class SecurityOrigin;
56 }
57
58 namespace WebKit {
59
60 class AuthenticatorManager;
61 class SecKeyProxyStore;
62 class StorageManager;
63 class DeviceIdHashSaltStorage;
64 class WebPageProxy;
65 class WebProcessPool;
66 class WebResourceLoadStatisticsStore;
67 enum class WebsiteDataFetchOption;
68 enum class WebsiteDataType;
69 struct MockWebAuthenticationConfiguration;
70 struct WebsiteDataRecord;
71 struct WebsiteDataStoreParameters;
72
73 #if ENABLE(RESOURCE_LOAD_STATISTICS)
74 enum class StorageAccessStatus;
75 enum class StorageAccessPromptStatus;
76 #endif
77
78 #if ENABLE(NETSCAPE_PLUGIN_API)
79 struct PluginModuleInfo;
80 #endif
81
82 class WebsiteDataStore : public RefCounted<WebsiteDataStore>, public WebProcessLifetimeObserver, public Identified<WebsiteDataStore>, public CanMakeWeakPtr<WebsiteDataStore>  {
83 public:
84     static Ref<WebsiteDataStore> createNonPersistent();
85     static Ref<WebsiteDataStore> create(Ref<WebsiteDataStoreConfiguration>&&, PAL::SessionID);
86     virtual ~WebsiteDataStore();
87
88     static WebsiteDataStore* existingNonDefaultDataStoreForSessionID(PAL::SessionID);
89
90     bool isPersistent() const { return !m_sessionID.isEphemeral(); }
91     PAL::SessionID sessionID() const { return m_sessionID; }
92
93     bool resourceLoadStatisticsEnabled() const;
94     void setResourceLoadStatisticsEnabled(bool);
95     bool resourceLoadStatisticsDebugMode() const;
96     void setResourceLoadStatisticsDebugMode(bool);
97     void setResourceLoadStatisticsDebugMode(bool, CompletionHandler<void()>&&);
98
99     uint64_t cacheStoragePerOriginQuota() const { return m_resolvedConfiguration->cacheStoragePerOriginQuota(); }
100     void setCacheStoragePerOriginQuota(uint64_t quota) { m_resolvedConfiguration->setCacheStoragePerOriginQuota(quota); }
101     const String& cacheStorageDirectory() const { return m_resolvedConfiguration->cacheStorageDirectory(); }
102     void setCacheStorageDirectory(String&& directory) { m_resolvedConfiguration->setCacheStorageDirectory(WTFMove(directory)); }
103     const String& serviceWorkerRegistrationDirectory() const { return m_resolvedConfiguration->serviceWorkerRegistrationDirectory(); }
104     void setServiceWorkerRegistrationDirectory(String&& directory) { m_resolvedConfiguration->setServiceWorkerRegistrationDirectory(WTFMove(directory)); }
105
106     WebResourceLoadStatisticsStore* resourceLoadStatistics() const { return m_resourceLoadStatistics.get(); }
107     void clearResourceLoadStatisticsInWebProcesses(CompletionHandler<void()>&&);
108
109     static void cloneSessionData(WebPageProxy& sourcePage, WebPageProxy& newPage);
110
111     void fetchData(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, Function<void(Vector<WebsiteDataRecord>)>&& completionHandler);
112     void fetchDataForTopPrivatelyControlledDomains(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, const Vector<String>& topPrivatelyControlledDomains, Function<void(Vector<WebsiteDataRecord>&&, HashSet<String>&&)>&& completionHandler);
113     void topPrivatelyControlledDomainsWithWebsiteData(OptionSet<WebsiteDataType> dataTypes, OptionSet<WebsiteDataFetchOption> fetchOptions, Function<void(HashSet<String>&&)>&& completionHandler);
114     void removeData(OptionSet<WebsiteDataType>, WallTime modifiedSince, Function<void()>&& completionHandler);
115     void removeData(OptionSet<WebsiteDataType>, const Vector<WebsiteDataRecord>&, Function<void()>&& completionHandler);
116     void removeDataForTopPrivatelyControlledDomains(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, const Vector<String>& topPrivatelyControlledDomains, Function<void(HashSet<String>&&)>&& completionHandler);
117
118 #if ENABLE(RESOURCE_LOAD_STATISTICS)
119     void updatePrevalentDomainsToBlockCookiesFor(const Vector<String>& domainsToBlock, CompletionHandler<void()>&&);
120     void setAgeCapForClientSideCookies(Optional<Seconds>, CompletionHandler<void()>&&);
121     void hasStorageAccessForFrameHandler(const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID, CompletionHandler<void(bool hasAccess)>&&);
122     void getAllStorageAccessEntries(uint64_t pageID, CompletionHandler<void(Vector<String>&& domains)>&&);
123     void grantStorageAccessHandler(const String& resourceDomain, const String& firstPartyDomain, Optional<uint64_t> frameID, uint64_t pageID, CompletionHandler<void(bool wasGranted)>&&);
124     void removeAllStorageAccessHandler(CompletionHandler<void()>&&);
125     void removePrevalentDomains(const Vector<String>& domains);
126     void hasStorageAccess(String&& subFrameHost, String&& topFrameHost, uint64_t frameID, uint64_t pageID, CompletionHandler<void(bool)>&&);
127     void requestStorageAccess(String&& subFrameHost, String&& topFrameHost, uint64_t frameID, uint64_t pageID, bool promptEnabled, CompletionHandler<void(StorageAccessStatus)>&&);
128     void grantStorageAccess(String&& subFrameHost, String&& topFrameHost, uint64_t frameID, uint64_t pageID, bool userWasPrompted, CompletionHandler<void(bool)>&&);
129 #endif
130     void setCacheMaxAgeCapForPrevalentResources(Seconds, CompletionHandler<void()>&&);
131     void resetCacheMaxAgeCapForPrevalentResources(CompletionHandler<void()>&&);
132     void resolveDirectoriesIfNecessary();
133     const String& resolvedApplicationCacheDirectory() const { return m_resolvedConfiguration->applicationCacheDirectory(); }
134     const String& resolvedMediaCacheDirectory() const { return m_resolvedConfiguration->mediaCacheDirectory(); }
135     const String& resolvedMediaKeysDirectory() const { return m_resolvedConfiguration->mediaKeysStorageDirectory(); }
136     const String& resolvedDatabaseDirectory() const { return m_resolvedConfiguration->webSQLDatabaseDirectory(); }
137     const String& resolvedJavaScriptConfigurationDirectory() const { return m_resolvedConfiguration->javaScriptConfigurationDirectory(); }
138     const String& resolvedCookieStorageFile() const { return m_resolvedConfiguration->cookieStorageFile(); }
139     const String& resolvedIndexedDatabaseDirectory() const { return m_resolvedConfiguration->indexedDBDatabaseDirectory(); }
140     const String& resolvedServiceWorkerRegistrationDirectory() const { return m_resolvedConfiguration->serviceWorkerRegistrationDirectory(); }
141     const String& resolvedResourceLoadStatisticsDirectory() const { return m_resolvedConfiguration->resourceLoadStatisticsDirectory(); }
142
143     StorageManager* storageManager() { return m_storageManager.get(); }
144
145     DeviceIdHashSaltStorage& deviceIdHashSaltStorage() { return m_deviceIdHashSaltStorage.get(); }
146
147     WebProcessPool* processPoolForCookieStorageOperations();
148     bool isAssociatedProcessPool(WebProcessPool&) const;
149
150     WebsiteDataStoreParameters parameters();
151
152     Vector<WebCore::Cookie> pendingCookies() const;
153     void addPendingCookie(const WebCore::Cookie&);
154     void removePendingCookie(const WebCore::Cookie&);
155     void clearPendingCookies();
156
157     void enableResourceLoadStatisticsAndSetTestingCallback(Function<void (const String&)>&& callback);
158
159     void setBoundInterfaceIdentifier(String&& identifier) { m_boundInterfaceIdentifier = WTFMove(identifier); }
160     const String& boundInterfaceIdentifier() { return m_boundInterfaceIdentifier; }
161     
162     void setAllowsCellularAccess(AllowsCellularAccess allows) { m_allowsCellularAccess = allows; }
163     AllowsCellularAccess allowsCellularAccess() { return m_allowsCellularAccess; }
164
165 #if PLATFORM(COCOA)
166     void setProxyConfiguration(CFDictionaryRef configuration) { m_proxyConfiguration = configuration; }
167     CFDictionaryRef proxyConfiguration() { return m_proxyConfiguration.get(); }
168 #endif
169
170 #if USE(CURL)
171     void platformSetParameters(WebsiteDataStoreParameters&);
172     void setNetworkProxySettings(WebCore::CurlProxySettings&&);
173     const WebCore::CurlProxySettings& networkProxySettings() const { return m_proxySettings; }
174 #endif
175
176     static void allowWebsiteDataRecordsForAllOrigins();
177
178 #if HAVE(SEC_KEY_PROXY)
179     void addSecKeyProxyStore(Ref<SecKeyProxyStore>&&);
180 #endif
181
182 #if ENABLE(WEB_AUTHN)
183     AuthenticatorManager& authenticatorManager() { return m_authenticatorManager.get(); }
184     void setMockWebAuthenticationConfiguration(MockWebAuthenticationConfiguration&&);
185 #endif
186
187     void didCreateNetworkProcess();
188
189     const WebsiteDataStoreConfiguration& configuration() { return m_configuration.get(); }
190
191 private:
192     explicit WebsiteDataStore(PAL::SessionID);
193     explicit WebsiteDataStore(Ref<WebsiteDataStoreConfiguration>&&, PAL::SessionID);
194
195     void fetchDataAndApply(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, RefPtr<WorkQueue>&&, Function<void(Vector<WebsiteDataRecord>)>&& apply);
196
197     // WebProcessLifetimeObserver.
198     void webPageWasAdded(WebPageProxy&) override;
199     void webPageWasInvalidated(WebPageProxy&) override;
200     void webProcessWillOpenConnection(WebProcessProxy&, IPC::Connection&) override;
201     void webPageWillOpenConnection(WebPageProxy&, IPC::Connection&) override;
202     void webPageDidCloseConnection(WebPageProxy&, IPC::Connection&) override;
203     void webProcessDidCloseConnection(WebProcessProxy&, IPC::Connection&) override;
204
205     void platformInitialize();
206     void platformDestroy();
207     static void platformRemoveRecentSearches(WallTime);
208
209     void registerWebResourceLoadStatisticsStoreAsMessageReceiver();
210     void unregisterWebResourceLoadStatisticsStoreAsMessageReceiver();
211
212     HashSet<RefPtr<WebProcessPool>> processPools(size_t count = std::numeric_limits<size_t>::max(), bool ensureAPoolExists = true) const;
213
214 #if ENABLE(NETSCAPE_PLUGIN_API)
215     Vector<PluginModuleInfo> plugins() const;
216 #endif
217
218     static Vector<WebCore::SecurityOriginData> mediaKeyOrigins(const String& mediaKeysStorageDirectory);
219     static void removeMediaKeys(const String& mediaKeysStorageDirectory, WallTime modifiedSince);
220     static void removeMediaKeys(const String& mediaKeysStorageDirectory, const HashSet<WebCore::SecurityOriginData>&);
221
222     void maybeRegisterWithSessionIDMap();
223
224     const PAL::SessionID m_sessionID;
225
226     Ref<WebsiteDataStoreConfiguration> m_resolvedConfiguration;
227     Ref<const WebsiteDataStoreConfiguration> m_configuration;
228     bool m_hasResolvedDirectories { false };
229
230     const RefPtr<StorageManager> m_storageManager;
231     const Ref<DeviceIdHashSaltStorage> m_deviceIdHashSaltStorage;
232     RefPtr<WebResourceLoadStatisticsStore> m_resourceLoadStatistics;
233     bool m_resourceLoadStatisticsDebugMode { false };
234
235     Ref<WorkQueue> m_queue;
236
237 #if PLATFORM(COCOA)
238     Vector<uint8_t> m_uiProcessCookieStorageIdentifier;
239     RetainPtr<CFHTTPCookieStorageRef> m_cfCookieStorage;
240     RetainPtr<CFDictionaryRef> m_proxyConfiguration;
241 #endif
242
243 #if USE(CURL)
244     WebCore::CurlProxySettings m_proxySettings;
245 #endif
246
247     HashSet<WebCore::Cookie> m_pendingCookies;
248
249     String m_boundInterfaceIdentifier;
250     AllowsCellularAccess m_allowsCellularAccess { AllowsCellularAccess::Yes };
251
252 #if HAVE(SEC_KEY_PROXY)
253     Vector<Ref<SecKeyProxyStore>> m_secKeyProxyStores;
254 #endif
255
256 #if ENABLE(WEB_AUTHN)
257     UniqueRef<AuthenticatorManager> m_authenticatorManager;
258 #endif
259 };
260
261 }