4297407079a75b5a7eabed757cf2d9844b16fe79
[WebKit.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 "WebProcessLifetimeObserver.h"
29 #include <WebCore/Cookie.h>
30 #include <WebCore/SecurityOriginData.h>
31 #include <WebCore/SecurityOriginHash.h>
32 #include <WebCore/SessionID.h>
33 #include <wtf/Function.h>
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 StorageProcessCreationParameters;
58 struct WebsiteDataRecord;
59 struct WebsiteDataStoreParameters;
60
61 #if ENABLE(NETSCAPE_PLUGIN_API)
62 struct PluginModuleInfo;
63 #endif
64
65 enum class ShouldClearFirst { No, Yes };
66
67 class WebsiteDataStore : public RefCounted<WebsiteDataStore>, public WebProcessLifetimeObserver {
68 public:
69     struct Configuration {
70         String networkCacheDirectory;
71         String applicationCacheDirectory;
72         String applicationCacheFlatFileSubdirectoryName;
73
74         String mediaCacheDirectory;
75         String indexedDBDatabaseDirectory;
76         String webSQLDatabaseDirectory;
77         String localStorageDirectory;
78         String mediaKeysStorageDirectory;
79         String resourceLoadStatisticsDirectory;
80         String javaScriptConfigurationDirectory;
81         String cookieStorageFile;
82     };
83     static Ref<WebsiteDataStore> createNonPersistent();
84     static Ref<WebsiteDataStore> create(Configuration, WebCore::SessionID);
85     virtual ~WebsiteDataStore();
86
87     uint64_t identifier() const { return m_identifier; }
88
89     bool isPersistent() const { return !m_sessionID.isEphemeral(); }
90     WebCore::SessionID sessionID() const { return m_sessionID; }
91
92     bool resourceLoadStatisticsEnabled() const;
93     void setResourceLoadStatisticsEnabled(bool);
94     WebResourceLoadStatisticsStore* resourceLoadStatistics() const { return m_resourceLoadStatistics.get(); }
95
96     static void cloneSessionData(WebPageProxy& sourcePage, WebPageProxy& newPage);
97
98     void fetchData(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, Function<void(Vector<WebsiteDataRecord>)>&& completionHandler);
99     void fetchDataForTopPrivatelyControlledDomains(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, const Vector<String>& topPrivatelyControlledDomains, Function<void(Vector<WebsiteDataRecord>&&, HashSet<String>&&)>&& completionHandler);
100     void topPrivatelyControlledDomainsWithWebsiteData(OptionSet<WebsiteDataType> dataTypes, OptionSet<WebsiteDataFetchOption> fetchOptions, Function<void(HashSet<String>&&)>&& completionHandler);
101     void removeData(OptionSet<WebsiteDataType>, std::chrono::system_clock::time_point modifiedSince, Function<void()>&& completionHandler);
102     void removeData(OptionSet<WebsiteDataType>, const Vector<WebsiteDataRecord>&, Function<void()>&& completionHandler);
103     void removeDataForTopPrivatelyControlledDomains(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, const Vector<String>& topPrivatelyControlledDomains, Function<void(HashSet<String>&&)>&& completionHandler);
104
105 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
106     void updateCookiePartitioningForTopPrivatelyOwnedDomains(const Vector<String>& domainsToRemove, const Vector<String>& domainsToAdd, ShouldClearFirst);
107 #endif
108     void networkProcessDidCrash();
109     void resolveDirectoriesIfNecessary();
110     const String& resolvedApplicationCacheDirectory() const { return m_resolvedConfiguration.applicationCacheDirectory; }
111     const String& resolvedMediaCacheDirectory() const { return m_resolvedConfiguration.mediaCacheDirectory; }
112     const String& resolvedMediaKeysDirectory() const { return m_resolvedConfiguration.mediaKeysStorageDirectory; }
113     const String& resolvedDatabaseDirectory() const { return m_resolvedConfiguration.webSQLDatabaseDirectory; }
114     const String& resolvedJavaScriptConfigurationDirectory() const { return m_resolvedConfiguration.javaScriptConfigurationDirectory; }
115     const String& resolvedCookieStorageFile() const { return m_resolvedConfiguration.cookieStorageFile; }
116     const String& resolvedIndexedDatabaseDirectory() const { return m_resolvedConfiguration.indexedDBDatabaseDirectory; }
117
118     StorageManager* storageManager() { return m_storageManager.get(); }
119
120     WebProcessPool* processPoolForCookieStorageOperations();
121     bool isAssociatedProcessPool(WebProcessPool&) const;
122
123     WebsiteDataStoreParameters parameters();
124     StorageProcessCreationParameters storageProcessParameters();
125
126     Vector<WebCore::Cookie> pendingCookies() const;
127     void addPendingCookie(const WebCore::Cookie&);
128     void removePendingCookie(const WebCore::Cookie&);
129
130     void enableResourceLoadStatisticsAndSetTestingCallback(Function<void (const String&)>&& callback);
131
132 private:
133     explicit WebsiteDataStore(WebCore::SessionID);
134     explicit WebsiteDataStore(Configuration, WebCore::SessionID);
135
136     void fetchDataAndApply(OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, RefPtr<WorkQueue>&&, Function<void(Vector<WebsiteDataRecord>)>&& apply);
137
138     // WebProcessLifetimeObserver.
139     void webPageWasAdded(WebPageProxy&) override;
140     void webPageWasRemoved(WebPageProxy&) override;
141     void webProcessWillOpenConnection(WebProcessProxy&, IPC::Connection&) override;
142     void webPageWillOpenConnection(WebPageProxy&, IPC::Connection&) override;
143     void webPageDidCloseConnection(WebPageProxy&, IPC::Connection&) override;
144     void webProcessDidCloseConnection(WebProcessProxy&, IPC::Connection&) override;
145
146     void platformInitialize();
147     void platformDestroy();
148     static void platformRemoveRecentSearches(std::chrono::system_clock::time_point);
149
150     HashSet<RefPtr<WebProcessPool>> processPools(size_t count = std::numeric_limits<size_t>::max(), bool ensureAPoolExists = true) const;
151
152 #if ENABLE(NETSCAPE_PLUGIN_API)
153     Vector<PluginModuleInfo> plugins() const;
154 #endif
155
156     static Vector<WebCore::SecurityOriginData> mediaKeyOrigins(const String& mediaKeysStorageDirectory);
157     static void removeMediaKeys(const String& mediaKeysStorageDirectory, std::chrono::system_clock::time_point modifiedSince);
158     static void removeMediaKeys(const String& mediaKeysStorageDirectory, const HashSet<WebCore::SecurityOriginData>&);
159
160     const uint64_t m_identifier;
161     const WebCore::SessionID m_sessionID;
162
163     const Configuration m_configuration;
164     Configuration m_resolvedConfiguration;
165     bool m_hasResolvedDirectories { false };
166
167     const RefPtr<StorageManager> m_storageManager;
168     RefPtr<WebResourceLoadStatisticsStore> m_resourceLoadStatistics;
169
170     Ref<WorkQueue> m_queue;
171
172 #if PLATFORM(COCOA)
173     Vector<uint8_t> m_uiProcessCookieStorageIdentifier;
174     RetainPtr<CFHTTPCookieStorageRef> m_cfCookieStorage;
175 #endif
176     HashSet<WebCore::Cookie> m_pendingCookies;
177 };
178
179 }