d1deec72d8eec91166a30b81d01918ca8f8081d4
[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/WorkQueue.h>
41 #include <wtf/text/WTFString.h>
42
43 #if PLATFORM(COCOA)
44 #include <pal/spi/cf/CFNetworkSPI.h>
45 #endif
46
47 namespace WebCore {
48 class SecurityOrigin;
49 }
50
51 namespace WebKit {
52
53 class StorageManager;
54 class WebPageProxy;
55 class WebProcessPool;
56 class WebResourceLoadStatisticsStore;
57 enum class WebsiteDataFetchOption;
58 enum class WebsiteDataType;
59 struct StorageProcessCreationParameters;
60 struct WebsiteDataRecord;
61 struct WebsiteDataStoreParameters;
62
63 #if ENABLE(NETSCAPE_PLUGIN_API)
64 struct PluginModuleInfo;
65 #endif
66
67 enum class ShouldClearFirst { No, Yes };
68
69 class WebsiteDataStore : public RefCounted<WebsiteDataStore>, public WebProcessLifetimeObserver, public Identified<WebsiteDataStore>  {
70 public:
71     constexpr static uint64_t defaultCacheStoragePerOriginQuota = 20 * 1024 * 1024;
72
73     struct Configuration {
74         String cacheStorageDirectory;
75         uint64_t cacheStoragePerOriginQuota { defaultCacheStoragePerOriginQuota };
76         String networkCacheDirectory;
77         String applicationCacheDirectory;
78         String applicationCacheFlatFileSubdirectoryName;
79
80         String mediaCacheDirectory;
81         String indexedDBDatabaseDirectory;
82         String serviceWorkerRegistrationDirectory;
83         String webSQLDatabaseDirectory;
84         String localStorageDirectory;
85         String mediaKeysStorageDirectory;
86         String resourceLoadStatisticsDirectory;
87         String javaScriptConfigurationDirectory;
88         String cookieStorageFile;
89     };
90     static Ref<WebsiteDataStore> createNonPersistent();
91     static Ref<WebsiteDataStore> create(Configuration, PAL::SessionID);
92     virtual ~WebsiteDataStore();
93
94     bool isPersistent() const { return !m_sessionID.isEphemeral(); }
95     PAL::SessionID sessionID() const { return m_sessionID; }
96
97     bool resourceLoadStatisticsEnabled() const;
98     void setResourceLoadStatisticsEnabled(bool);
99
100     uint64_t cacheStoragePerOriginQuota() const { return m_resolvedConfiguration.cacheStoragePerOriginQuota; }
101     void setCacheStoragePerOriginQuota(uint64_t quota) { m_resolvedConfiguration.cacheStoragePerOriginQuota = quota; }
102     const String& cacheStorageDirectory() const { return m_resolvedConfiguration.cacheStorageDirectory; }
103     void setCacheStorageDirectory(String&& directory) { m_resolvedConfiguration.cacheStorageDirectory = WTFMove(directory); }
104
105     WebResourceLoadStatisticsStore* resourceLoadStatistics() const { return m_resourceLoadStatistics.get(); }
106     void clearResourceLoadStatisticsInWebProcesses();
107
108     static void cloneSessionData(WebPageProxy& sourcePage, WebPageProxy& newPage);
109
110     void fetchData(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, Function<void(Vector<WebsiteDataRecord>)>&& completionHandler);
111     void fetchDataForTopPrivatelyControlledDomains(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, const Vector<String>& topPrivatelyControlledDomains, Function<void(Vector<WebsiteDataRecord>&&, HashSet<String>&&)>&& completionHandler);
112     void topPrivatelyControlledDomainsWithWebsiteData(OptionSet<WebsiteDataType> dataTypes, OptionSet<WebsiteDataFetchOption> fetchOptions, Function<void(HashSet<String>&&)>&& completionHandler);
113     void removeData(OptionSet<WebsiteDataType>, std::chrono::system_clock::time_point modifiedSince, Function<void()>&& completionHandler);
114     void removeData(OptionSet<WebsiteDataType>, const Vector<WebsiteDataRecord>&, Function<void()>&& completionHandler);
115     void removeDataForTopPrivatelyControlledDomains(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, const Vector<String>& topPrivatelyControlledDomains, Function<void(HashSet<String>&&)>&& completionHandler);
116
117 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
118     void updatePrevalentDomainsToPartitionOrBlockCookies(const Vector<String>& domainsToPartition, const Vector<String>& domainsToBlock, const Vector<String>& domainsToNeitherPartitionNorBlock, ShouldClearFirst);
119     void updateStorageAccessForPrevalentDomainsHandler(const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID, bool value, WTF::CompletionHandler<void(bool wasGranted)>&& callback);
120     void removePrevalentDomains(const Vector<String>& domains);
121 #endif
122     void networkProcessDidCrash();
123     void resolveDirectoriesIfNecessary();
124     const String& resolvedApplicationCacheDirectory() const { return m_resolvedConfiguration.applicationCacheDirectory; }
125     const String& resolvedMediaCacheDirectory() const { return m_resolvedConfiguration.mediaCacheDirectory; }
126     const String& resolvedMediaKeysDirectory() const { return m_resolvedConfiguration.mediaKeysStorageDirectory; }
127     const String& resolvedDatabaseDirectory() const { return m_resolvedConfiguration.webSQLDatabaseDirectory; }
128     const String& resolvedJavaScriptConfigurationDirectory() const { return m_resolvedConfiguration.javaScriptConfigurationDirectory; }
129     const String& resolvedCookieStorageFile() const { return m_resolvedConfiguration.cookieStorageFile; }
130     const String& resolvedIndexedDatabaseDirectory() const { return m_resolvedConfiguration.indexedDBDatabaseDirectory; }
131     const String& resolvedServiceWorkerRegistrationDirectory() const { return m_resolvedConfiguration.serviceWorkerRegistrationDirectory; }
132
133     StorageManager* storageManager() { return m_storageManager.get(); }
134
135     WebProcessPool* processPoolForCookieStorageOperations();
136     bool isAssociatedProcessPool(WebProcessPool&) const;
137
138     WebsiteDataStoreParameters parameters();
139     StorageProcessCreationParameters storageProcessParameters();
140
141     Vector<WebCore::Cookie> pendingCookies() const;
142     void addPendingCookie(const WebCore::Cookie&);
143     void removePendingCookie(const WebCore::Cookie&);
144
145     void enableResourceLoadStatisticsAndSetTestingCallback(Function<void (const String&)>&& callback);
146
147     void hasStorageAccess(String&& subFrameHost, String&& topFrameHost, WTF::CompletionHandler<void (bool)>&& callback);
148     void requestStorageAccess(String&& subFrameHost, String&& topFrameHost, uint64_t frameID, uint64_t pageID, WTF::CompletionHandler<void (bool)>&& callback);
149     
150     void setBoundInterfaceIdentifier(String&& identifier) { m_boundInterfaceIdentifier = WTFMove(identifier); }
151     const String& boundInterfaceIdentifier() { return m_boundInterfaceIdentifier; }
152     
153     void setAllowsCellularAccess(AllowsCellularAccess allows) { m_allowsCellularAccess = allows; }
154     AllowsCellularAccess allowsCellularAccess() { return m_allowsCellularAccess; }
155
156 private:
157     explicit WebsiteDataStore(PAL::SessionID);
158     explicit WebsiteDataStore(Configuration, PAL::SessionID);
159
160     void fetchDataAndApply(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, RefPtr<WorkQueue>&&, Function<void(Vector<WebsiteDataRecord>)>&& apply);
161
162     // WebProcessLifetimeObserver.
163     void webPageWasAdded(WebPageProxy&) override;
164     void webPageWasRemoved(WebPageProxy&) override;
165     void webProcessWillOpenConnection(WebProcessProxy&, IPC::Connection&) override;
166     void webPageWillOpenConnection(WebPageProxy&, IPC::Connection&) override;
167     void webPageDidCloseConnection(WebPageProxy&, IPC::Connection&) override;
168     void webProcessDidCloseConnection(WebProcessProxy&, IPC::Connection&) override;
169
170     void platformInitialize();
171     void platformDestroy();
172     static void platformRemoveRecentSearches(std::chrono::system_clock::time_point);
173
174     HashSet<RefPtr<WebProcessPool>> processPools(size_t count = std::numeric_limits<size_t>::max(), bool ensureAPoolExists = true) const;
175
176 #if ENABLE(NETSCAPE_PLUGIN_API)
177     Vector<PluginModuleInfo> plugins() const;
178 #endif
179
180     static Vector<WebCore::SecurityOriginData> mediaKeyOrigins(const String& mediaKeysStorageDirectory);
181     static void removeMediaKeys(const String& mediaKeysStorageDirectory, std::chrono::system_clock::time_point modifiedSince);
182     static void removeMediaKeys(const String& mediaKeysStorageDirectory, const HashSet<WebCore::SecurityOriginData>&);
183
184     const PAL::SessionID m_sessionID;
185
186     const Configuration m_configuration;
187     Configuration m_resolvedConfiguration;
188     bool m_hasResolvedDirectories { false };
189
190     const RefPtr<StorageManager> m_storageManager;
191     RefPtr<WebResourceLoadStatisticsStore> m_resourceLoadStatistics;
192
193     Ref<WorkQueue> m_queue;
194
195 #if PLATFORM(COCOA)
196     Vector<uint8_t> m_uiProcessCookieStorageIdentifier;
197     RetainPtr<CFHTTPCookieStorageRef> m_cfCookieStorage;
198 #endif
199     HashSet<WebCore::Cookie> m_pendingCookies;
200     
201     String m_boundInterfaceIdentifier;
202     AllowsCellularAccess m_allowsCellularAccess { AllowsCellularAccess::Yes };
203 };
204
205 }