Resource Load Statistics: Grandfather domains for existing data records
[WebKit-https.git] / Source / WebKit2 / 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 "WebProcessLifetimeObserver.h"
29 #include <WebCore/Cookie.h>
30 #include <WebCore/SecurityOriginData.h>
31 #include <WebCore/SecurityOriginHash.h>
32 #include <WebCore/SessionID.h>
33 #include <functional>
34 #include <wtf/HashSet.h>
35 #include <wtf/OptionSet.h>
36 #include <wtf/RefCounted.h>
37 #include <wtf/RefPtr.h>
38 #include <wtf/WorkQueue.h>
39 #include <wtf/text/WTFString.h>
40
41 #if PLATFORM(COCOA)
42 #include <WebCore/CFNetworkSPI.h>
43 #endif
44
45 namespace WebCore {
46 class SecurityOrigin;
47 }
48
49 namespace WebKit {
50
51 class StorageManager;
52 class WebPageProxy;
53 class WebProcessPool;
54 class WebResourceLoadStatisticsStore;
55 enum class WebsiteDataFetchOption;
56 enum class WebsiteDataType;
57 struct DatabaseProcessCreationParameters;
58 struct WebsiteDataRecord;
59 struct WebsiteDataStoreParameters;
60
61 #if ENABLE(NETSCAPE_PLUGIN_API)
62 struct PluginModuleInfo;
63 #endif
64
65 class WebsiteDataStore : public RefCounted<WebsiteDataStore>, public WebProcessLifetimeObserver {
66 public:
67     struct Configuration {
68         String networkCacheDirectory;
69         String applicationCacheDirectory;
70         String applicationCacheFlatFileSubdirectoryName;
71
72         String mediaCacheDirectory;
73         String indexedDBDatabaseDirectory;
74         String webSQLDatabaseDirectory;
75         String localStorageDirectory;
76         String mediaKeysStorageDirectory;
77         String resourceLoadStatisticsDirectory;
78         String javaScriptConfigurationDirectory;
79         String cookieStorageFile;
80     };
81     static Ref<WebsiteDataStore> createNonPersistent();
82     static Ref<WebsiteDataStore> create(Configuration, WebCore::SessionID);
83     virtual ~WebsiteDataStore();
84
85     uint64_t identifier() const { return m_identifier; }
86
87     bool isPersistent() const { return !m_sessionID.isEphemeral(); }
88     WebCore::SessionID sessionID() const { return m_sessionID; }
89
90     bool resourceLoadStatisticsEnabled() const;
91     void setResourceLoadStatisticsEnabled(bool);
92     void registerSharedResourceLoadObserver();
93
94     static void cloneSessionData(WebPageProxy& sourcePage, WebPageProxy& newPage);
95
96     void fetchData(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, std::function<void (Vector<WebsiteDataRecord>)> completionHandler);
97     void fetchDataForTopPrivatelyControlledDomains(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, Vector<String>&& topPrivatelyControlledDomains, std::function<void(Vector<WebsiteDataRecord>&&, Vector<String>&&)> completionHandler);
98     void topPrivatelyControlledDomainsWithWebsiteData(OptionSet<WebsiteDataType> dataTypes, OptionSet<WebsiteDataFetchOption> fetchOptions, std::function<void(HashSet<String>&&)> completionHandler);
99     void removeData(OptionSet<WebsiteDataType>, std::chrono::system_clock::time_point modifiedSince, std::function<void ()> completionHandler);
100     void removeData(OptionSet<WebsiteDataType>, const Vector<WebsiteDataRecord>&, std::function<void ()> completionHandler);
101     void removeDataForTopPrivatelyControlledDomains(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, Vector<String>&& topPrivatelyControlledDomains, std::function<void(Vector<String>)> completionHandler);
102
103 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
104     void shouldPartitionCookiesForTopPrivatelyOwnedDomains(const Vector<String>& domainsToRemove, const Vector<String>& domainsToAdd, bool clearFirst);
105 #endif
106     void resolveDirectoriesIfNecessary();
107     const String& resolvedApplicationCacheDirectory() const { return m_resolvedConfiguration.applicationCacheDirectory; }
108     const String& resolvedMediaCacheDirectory() const { return m_resolvedConfiguration.mediaCacheDirectory; }
109     const String& resolvedMediaKeysDirectory() const { return m_resolvedConfiguration.mediaKeysStorageDirectory; }
110     const String& resolvedDatabaseDirectory() const { return m_resolvedConfiguration.webSQLDatabaseDirectory; }
111     const String& resolvedJavaScriptConfigurationDirectory() const { return m_resolvedConfiguration.javaScriptConfigurationDirectory; }
112     const String& resolvedCookieStorageFile() const { return m_resolvedConfiguration.cookieStorageFile; }
113     const String& resolvedIndexedDatabaseDirectory() const { return m_resolvedConfiguration.indexedDBDatabaseDirectory; }
114
115     StorageManager* storageManager() { return m_storageManager.get(); }
116
117     WebProcessPool* processPoolForCookieStorageOperations();
118     bool isAssociatedProcessPool(WebProcessPool&) const;
119
120     WebsiteDataStoreParameters parameters();
121     DatabaseProcessCreationParameters databaseProcessParameters();
122
123     Vector<WebCore::Cookie> pendingCookies() const;
124     void addPendingCookie(const WebCore::Cookie&);
125     void removePendingCookie(const WebCore::Cookie&);
126
127 private:
128     explicit WebsiteDataStore(WebCore::SessionID);
129     explicit WebsiteDataStore(Configuration, WebCore::SessionID);
130
131     // WebProcessLifetimeObserver.
132     void webPageWasAdded(WebPageProxy&) override;
133     void webPageWasRemoved(WebPageProxy&) override;
134     void webProcessWillOpenConnection(WebProcessProxy&, IPC::Connection&) override;
135     void webPageWillOpenConnection(WebPageProxy&, IPC::Connection&) override;
136     void webPageDidCloseConnection(WebPageProxy&, IPC::Connection&) override;
137     void webProcessDidCloseConnection(WebProcessProxy&, IPC::Connection&) override;
138
139     void platformInitialize();
140     void platformDestroy();
141     static void platformRemoveRecentSearches(std::chrono::system_clock::time_point);
142
143     HashSet<RefPtr<WebProcessPool>> processPools(size_t count = std::numeric_limits<size_t>::max(), bool ensureAPoolExists = true) const;
144
145 #if ENABLE(NETSCAPE_PLUGIN_API)
146     Vector<PluginModuleInfo> plugins() const;
147 #endif
148
149     static Vector<WebCore::SecurityOriginData> mediaKeyOrigins(const String& mediaKeysStorageDirectory);
150     static void removeMediaKeys(const String& mediaKeysStorageDirectory, std::chrono::system_clock::time_point modifiedSince);
151     static void removeMediaKeys(const String& mediaKeysStorageDirectory, const HashSet<WebCore::SecurityOriginData>&);
152
153     const uint64_t m_identifier;
154     const WebCore::SessionID m_sessionID;
155
156     const Configuration m_configuration;
157     Configuration m_resolvedConfiguration;
158     bool m_hasResolvedDirectories { false };
159
160     const RefPtr<StorageManager> m_storageManager;
161     const RefPtr<WebResourceLoadStatisticsStore> m_resourceLoadStatistics;
162
163     Ref<WorkQueue> m_queue;
164
165 #if PLATFORM(COCOA)
166     Vector<uint8_t> m_uiProcessCookieStorageIdentifier;
167     RetainPtr<CFHTTPCookieStorageRef> m_cfCookieStorage;
168 #endif
169     HashSet<WebCore::Cookie> m_pendingCookies;
170 };
171
172 }