Resource Load Statistics: Immediately forward cookie access at user interaction when...
[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 = 50 * 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         explicit Configuration();
91     };
92     static Ref<WebsiteDataStore> createNonPersistent();
93     static Ref<WebsiteDataStore> create(Configuration, PAL::SessionID);
94     virtual ~WebsiteDataStore();
95
96     static WebsiteDataStore* existingNonDefaultDataStoreForSessionID(PAL::SessionID);
97
98     bool isPersistent() const { return !m_sessionID.isEphemeral(); }
99     PAL::SessionID sessionID() const { return m_sessionID; }
100
101     bool resourceLoadStatisticsEnabled() const;
102     void setResourceLoadStatisticsEnabled(bool);
103     bool resourceLoadStatisticsDebugMode() const;
104     void setResourceLoadStatisticsDebugMode(bool);
105
106     uint64_t cacheStoragePerOriginQuota() const { return m_resolvedConfiguration.cacheStoragePerOriginQuota; }
107     void setCacheStoragePerOriginQuota(uint64_t quota) { m_resolvedConfiguration.cacheStoragePerOriginQuota = quota; }
108     const String& cacheStorageDirectory() const { return m_resolvedConfiguration.cacheStorageDirectory; }
109     void setCacheStorageDirectory(String&& directory) { m_resolvedConfiguration.cacheStorageDirectory = WTFMove(directory); }
110     const String& serviceWorkerRegistrationDirectory() const { return m_resolvedConfiguration.serviceWorkerRegistrationDirectory; }
111     void setServiceWorkerRegistrationDirectory(String&& directory) { m_resolvedConfiguration.serviceWorkerRegistrationDirectory = WTFMove(directory); }
112
113     WebResourceLoadStatisticsStore* resourceLoadStatistics() const { return m_resourceLoadStatistics.get(); }
114     void clearResourceLoadStatisticsInWebProcesses(CompletionHandler<void()>&&);
115
116     static void cloneSessionData(WebPageProxy& sourcePage, WebPageProxy& newPage);
117
118     void fetchData(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, Function<void(Vector<WebsiteDataRecord>)>&& completionHandler);
119     void fetchDataForTopPrivatelyControlledDomains(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, const Vector<String>& topPrivatelyControlledDomains, Function<void(Vector<WebsiteDataRecord>&&, HashSet<String>&&)>&& completionHandler);
120     void topPrivatelyControlledDomainsWithWebsiteData(OptionSet<WebsiteDataType> dataTypes, OptionSet<WebsiteDataFetchOption> fetchOptions, Function<void(HashSet<String>&&)>&& completionHandler);
121     void removeData(OptionSet<WebsiteDataType>, WallTime modifiedSince, Function<void()>&& completionHandler);
122     void removeData(OptionSet<WebsiteDataType>, const Vector<WebsiteDataRecord>&, Function<void()>&& completionHandler);
123     void removeDataForTopPrivatelyControlledDomains(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, const Vector<String>& topPrivatelyControlledDomains, Function<void(HashSet<String>&&)>&& completionHandler);
124
125 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
126     void updatePrevalentDomainsToPartitionOrBlockCookies(const Vector<String>& domainsToPartition, const Vector<String>& domainsToBlock, const Vector<String>& domainsToNeitherPartitionNorBlock, ShouldClearFirst);
127     void hasStorageAccessForFrameHandler(const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID, WTF::CompletionHandler<void(bool hasAccess)>&& callback);
128     void getAllStorageAccessEntries(CompletionHandler<void(Vector<String>&& domains)>&&);
129     void grantStorageAccessHandler(const String& resourceDomain, const String& firstPartyDomain, std::optional<uint64_t> frameID, uint64_t pageID, WTF::CompletionHandler<void(bool wasGranted)>&& callback);
130     void removePrevalentDomains(const Vector<String>& domains);
131     void hasStorageAccess(String&& subFrameHost, String&& topFrameHost, uint64_t frameID, uint64_t pageID, WTF::CompletionHandler<void (bool)>&& callback);
132     void requestStorageAccess(String&& subFrameHost, String&& topFrameHost, uint64_t frameID, uint64_t pageID, WTF::CompletionHandler<void (bool)>&& callback);
133 #endif
134     void networkProcessDidCrash();
135     void resolveDirectoriesIfNecessary();
136     const String& resolvedApplicationCacheDirectory() const { return m_resolvedConfiguration.applicationCacheDirectory; }
137     const String& resolvedMediaCacheDirectory() const { return m_resolvedConfiguration.mediaCacheDirectory; }
138     const String& resolvedMediaKeysDirectory() const { return m_resolvedConfiguration.mediaKeysStorageDirectory; }
139     const String& resolvedDatabaseDirectory() const { return m_resolvedConfiguration.webSQLDatabaseDirectory; }
140     const String& resolvedJavaScriptConfigurationDirectory() const { return m_resolvedConfiguration.javaScriptConfigurationDirectory; }
141     const String& resolvedCookieStorageFile() const { return m_resolvedConfiguration.cookieStorageFile; }
142     const String& resolvedIndexedDatabaseDirectory() const { return m_resolvedConfiguration.indexedDBDatabaseDirectory; }
143     const String& resolvedServiceWorkerRegistrationDirectory() const { return m_resolvedConfiguration.serviceWorkerRegistrationDirectory; }
144
145     StorageManager* storageManager() { return m_storageManager.get(); }
146
147     WebProcessPool* processPoolForCookieStorageOperations();
148     bool isAssociatedProcessPool(WebProcessPool&) const;
149
150     WebsiteDataStoreParameters parameters();
151     StorageProcessCreationParameters storageProcessParameters();
152
153     Vector<WebCore::Cookie> pendingCookies() const;
154     void addPendingCookie(const WebCore::Cookie&);
155     void removePendingCookie(const WebCore::Cookie&);
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     static void allowWebsiteDataRecordsForAllOrigins();
166
167 private:
168     explicit WebsiteDataStore(PAL::SessionID);
169     explicit WebsiteDataStore(Configuration, PAL::SessionID);
170
171     void fetchDataAndApply(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, RefPtr<WorkQueue>&&, Function<void(Vector<WebsiteDataRecord>)>&& apply);
172
173     // WebProcessLifetimeObserver.
174     void webPageWasAdded(WebPageProxy&) override;
175     void webPageWasRemoved(WebPageProxy&) override;
176     void webProcessWillOpenConnection(WebProcessProxy&, IPC::Connection&) override;
177     void webPageWillOpenConnection(WebPageProxy&, IPC::Connection&) override;
178     void webPageDidCloseConnection(WebPageProxy&, IPC::Connection&) override;
179     void webProcessDidCloseConnection(WebProcessProxy&, IPC::Connection&) override;
180
181     void platformInitialize();
182     void platformDestroy();
183     static void platformRemoveRecentSearches(WallTime);
184
185     HashSet<RefPtr<WebProcessPool>> processPools(size_t count = std::numeric_limits<size_t>::max(), bool ensureAPoolExists = true) const;
186
187 #if ENABLE(NETSCAPE_PLUGIN_API)
188     Vector<PluginModuleInfo> plugins() const;
189 #endif
190
191     static Vector<WebCore::SecurityOriginData> mediaKeyOrigins(const String& mediaKeysStorageDirectory);
192     static void removeMediaKeys(const String& mediaKeysStorageDirectory, WallTime modifiedSince);
193     static void removeMediaKeys(const String& mediaKeysStorageDirectory, const HashSet<WebCore::SecurityOriginData>&);
194
195     void maybeRegisterWithSessionIDMap();
196
197     const PAL::SessionID m_sessionID;
198
199     const Configuration m_configuration;
200     Configuration m_resolvedConfiguration;
201     bool m_hasResolvedDirectories { false };
202
203     const RefPtr<StorageManager> m_storageManager;
204     RefPtr<WebResourceLoadStatisticsStore> m_resourceLoadStatistics;
205     bool m_resourceLoadStatisticsDebugMode { false };
206
207     Ref<WorkQueue> m_queue;
208
209 #if PLATFORM(COCOA)
210     Vector<uint8_t> m_uiProcessCookieStorageIdentifier;
211     RetainPtr<CFHTTPCookieStorageRef> m_cfCookieStorage;
212 #endif
213     HashSet<WebCore::Cookie> m_pendingCookies;
214     
215     String m_boundInterfaceIdentifier;
216     AllowsCellularAccess m_allowsCellularAccess { AllowsCellularAccess::Yes };
217 };
218
219 }