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