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