1 | /* |
2 | * Copyright (C) 2019 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 | #if ENABLE(RESOURCE_LOAD_STATISTICS) |
29 | |
30 | #include "ResourceLoadStatisticsClassifier.h" |
31 | #include "WebResourceLoadStatisticsStore.h" |
32 | #include <wtf/CompletionHandler.h> |
33 | #include <wtf/Vector.h> |
34 | #include <wtf/WeakPtr.h> |
35 | #include <wtf/WorkQueue.h> |
36 | |
37 | #if HAVE(CORE_PREDICTION) |
38 | #include "ResourceLoadStatisticsClassifierCocoa.h" |
39 | #endif |
40 | |
41 | namespace WebCore { |
42 | class KeyedDecoder; |
43 | class KeyedEncoder; |
44 | enum class StorageAccessPromptWasShown : bool; |
45 | enum class StorageAccessWasGranted : bool; |
46 | struct ResourceLoadStatistics; |
47 | } |
48 | |
49 | namespace WebKit { |
50 | |
51 | class ResourceLoadStatisticsPersistentStorage; |
52 | |
53 | class OperatingDate { |
54 | public: |
55 | OperatingDate() = default; |
56 | |
57 | static OperatingDate fromWallTime(WallTime); |
58 | static OperatingDate today(); |
59 | Seconds secondsSinceEpoch() const; |
60 | bool operator==(const OperatingDate& other) const; |
61 | bool operator<(const OperatingDate& other) const; |
62 | bool operator<=(const OperatingDate& other) const; |
63 | |
64 | private: |
65 | OperatingDate(int year, int month, int monthDay) |
66 | : m_year(year) |
67 | , m_month(month) |
68 | , m_monthDay(monthDay) |
69 | { } |
70 | |
71 | int m_year { 0 }; |
72 | int m_month { 0 }; // [0, 11]. |
73 | int m_monthDay { 0 }; // [1, 31]. |
74 | }; |
75 | |
76 | enum class OperatingDatesWindow : bool { Long, Short }; |
77 | |
78 | // This is always constructed / used / destroyed on the WebResourceLoadStatisticsStore's statistics queue. |
79 | class ResourceLoadStatisticsStore : public CanMakeWeakPtr<ResourceLoadStatisticsStore> { |
80 | public: |
81 | using ResourceLoadStatistics = WebCore::ResourceLoadStatistics; |
82 | using RegistrableDomain = WebCore::RegistrableDomain; |
83 | using TopFrameDomain = WebCore::RegistrableDomain; |
84 | using SubFrameDomain = WebCore::RegistrableDomain; |
85 | using SubResourceDomain = WebCore::RegistrableDomain; |
86 | using RedirectDomain = WebCore::RegistrableDomain; |
87 | using RedirectedFromDomain = WebCore::RegistrableDomain; |
88 | using RedirectedToDomain = WebCore::RegistrableDomain; |
89 | using NavigatedFromDomain = WebCore::RegistrableDomain; |
90 | using NavigatedToDomain = WebCore::RegistrableDomain; |
91 | using DomainInNeedOfStorageAccess = WebCore::RegistrableDomain; |
92 | using OpenerDomain = WebCore::RegistrableDomain; |
93 | using FrameID = uint64_t; |
94 | |
95 | virtual ~ResourceLoadStatisticsStore(); |
96 | |
97 | virtual void clear(CompletionHandler<void()>&&) = 0; |
98 | virtual bool isEmpty() const = 0; |
99 | |
100 | virtual void updateCookieBlocking(CompletionHandler<void()>&&) = 0; |
101 | void updateCookieBlockingForDomains(const Vector<RegistrableDomain>& domainsToBlock, CompletionHandler<void()>&&); |
102 | void clearBlockingStateForDomains(const Vector<RegistrableDomain>& domains, CompletionHandler<void()>&&); |
103 | |
104 | void includeTodayAsOperatingDateIfNecessary(); |
105 | void processStatisticsAndDataRecords(); |
106 | |
107 | virtual void classifyPrevalentResources() = 0; |
108 | virtual void syncStorageIfNeeded() = 0; |
109 | virtual void syncStorageImmediately() = 0; |
110 | |
111 | virtual void requestStorageAccessUnderOpener(DomainInNeedOfStorageAccess&&, WebCore::PageIdentifier openerID, OpenerDomain&&) = 0; |
112 | void removeAllStorageAccess(CompletionHandler<void()>&&); |
113 | |
114 | void grandfatherExistingWebsiteData(CompletionHandler<void()>&&); |
115 | void cancelPendingStatisticsProcessingRequest(); |
116 | |
117 | virtual bool isRegisteredAsSubresourceUnder(const SubResourceDomain&, const TopFrameDomain&) const = 0; |
118 | virtual bool isRegisteredAsSubFrameUnder(const SubFrameDomain&, const TopFrameDomain&) const = 0; |
119 | virtual bool isRegisteredAsRedirectingTo(const RedirectedFromDomain&, const RedirectedToDomain&) const = 0; |
120 | |
121 | virtual void clearPrevalentResource(const RegistrableDomain&) = 0; |
122 | virtual String dumpResourceLoadStatistics() const = 0; |
123 | virtual bool isPrevalentResource(const RegistrableDomain&) const = 0; |
124 | virtual bool isVeryPrevalentResource(const RegistrableDomain&) const = 0; |
125 | virtual void setPrevalentResource(const RegistrableDomain&) = 0; |
126 | virtual void setVeryPrevalentResource(const RegistrableDomain&) = 0; |
127 | |
128 | virtual void setGrandfathered(const RegistrableDomain&, bool value) = 0; |
129 | virtual bool isGrandfathered(const RegistrableDomain&) const = 0; |
130 | |
131 | virtual void incrementRecordsDeletedCountForDomains(HashSet<RegistrableDomain>&&) = 0; |
132 | virtual void grandfatherDataForDomains(const HashSet<RegistrableDomain>&) = 0; |
133 | |
134 | virtual void setSubframeUnderTopFrameDomain(const SubFrameDomain&, const TopFrameDomain&) = 0; |
135 | virtual void setSubresourceUnderTopFrameDomain(const SubResourceDomain&, const TopFrameDomain&) = 0; |
136 | virtual void setSubresourceUniqueRedirectTo(const SubResourceDomain&, const RedirectDomain&) = 0; |
137 | virtual void setSubresourceUniqueRedirectFrom(const SubResourceDomain&, const RedirectDomain&) = 0; |
138 | virtual void setTopFrameUniqueRedirectTo(const TopFrameDomain&, const RedirectDomain&) = 0; |
139 | virtual void setTopFrameUniqueRedirectFrom(const TopFrameDomain&, const RedirectDomain&) = 0; |
140 | |
141 | void logTestingEvent(const String&); |
142 | |
143 | void setMaxStatisticsEntries(size_t maximumEntryCount); |
144 | void setPruneEntriesDownTo(size_t pruneTargetCount); |
145 | void resetParametersToDefaultValues(); |
146 | Optional<Seconds> statisticsEpirationTime() const; |
147 | |
148 | virtual void calculateAndSubmitTelemetry() const = 0; |
149 | |
150 | void setNotifyPagesWhenDataRecordsWereScanned(bool); |
151 | void setIsRunningTest(bool); |
152 | bool shouldSkip(const RegistrableDomain&) const; |
153 | void setShouldClassifyResourcesBeforeDataRecordsRemoval(bool); |
154 | void setShouldSubmitTelemetry(bool); |
155 | void setTimeToLiveUserInteraction(Seconds); |
156 | void setMinimumTimeBetweenDataRecordsRemoval(Seconds); |
157 | void setGrandfatheringTime(Seconds); |
158 | void setResourceLoadStatisticsDebugMode(bool); |
159 | bool isDebugModeEnabled() const { return m_debugModeEnabled; }; |
160 | void setPrevalentResourceForDebugMode(const RegistrableDomain&); |
161 | |
162 | virtual void hasStorageAccess(const SubFrameDomain&, const TopFrameDomain&, Optional<FrameID>, WebCore::PageIdentifier, CompletionHandler<void(bool)>&&) = 0; |
163 | virtual void requestStorageAccess(SubFrameDomain&&, TopFrameDomain&&, FrameID, WebCore::PageIdentifier, CompletionHandler<void(StorageAccessStatus)>&&) = 0; |
164 | virtual void grantStorageAccess(SubFrameDomain&&, TopFrameDomain&&, FrameID, WebCore::PageIdentifier, WebCore::StorageAccessPromptWasShown, CompletionHandler<void(WebCore::StorageAccessWasGranted)>&&) = 0; |
165 | |
166 | virtual void logFrameNavigation(const NavigatedToDomain&, const TopFrameDomain&, const NavigatedFromDomain&, bool isRedirect, bool isMainFrame) = 0; |
167 | virtual void logUserInteraction(const TopFrameDomain&) = 0; |
168 | virtual void logSubresourceLoading(const SubResourceDomain&, const TopFrameDomain&, WallTime lastSeen) = 0; |
169 | virtual void logSubresourceRedirect(const RedirectedFromDomain&, const RedirectedToDomain&) = 0; |
170 | virtual void logCrossSiteLoadWithLinkDecoration(const NavigatedFromDomain&, const NavigatedToDomain&) = 0; |
171 | |
172 | virtual void clearUserInteraction(const RegistrableDomain&) = 0; |
173 | virtual bool hasHadUserInteraction(const RegistrableDomain&, OperatingDatesWindow) = 0; |
174 | |
175 | virtual void setLastSeen(const RegistrableDomain& primaryDomain, Seconds) = 0; |
176 | |
177 | void didCreateNetworkProcess(); |
178 | |
179 | const WebResourceLoadStatisticsStore& store() const { return m_store; } |
180 | |
181 | static constexpr unsigned maxImportance { 3 }; |
182 | |
183 | virtual bool isMemoryStore() const { return false; } |
184 | virtual bool isDatabaseStore()const { return false; } |
185 | |
186 | bool dataRecordsBeingRemoved() const { return m_dataRecordsBeingRemoved; } |
187 | |
188 | protected: |
189 | static unsigned computeImportance(const WebCore::ResourceLoadStatistics&); |
190 | static Vector<OperatingDate> mergeOperatingDates(const Vector<OperatingDate>& existingDates, Vector<OperatingDate>&& newDates); |
191 | static void debugLogDomainsInBatches(const char* action, const Vector<RegistrableDomain>& domains); |
192 | |
193 | ResourceLoadStatisticsStore(WebResourceLoadStatisticsStore&, WorkQueue&, ShouldIncludeLocalhost); |
194 | |
195 | bool hasStatisticsExpired(const ResourceLoadStatistics&, OperatingDatesWindow) const; |
196 | bool hasStatisticsExpired(WallTime mostRecentUserInteractionTime, OperatingDatesWindow) const; |
197 | void scheduleStatisticsProcessingRequestIfNecessary(); |
198 | void mergeOperatingDates(Vector<OperatingDate>&&); |
199 | virtual Vector<RegistrableDomain> ensurePrevalentResourcesForDebugMode() = 0; |
200 | virtual HashMap<RegistrableDomain, WebsiteDataToRemove> registrableDomainsToRemoveWebsiteDataFor() = 0; |
201 | virtual void pruneStatisticsIfNeeded() = 0; |
202 | |
203 | WebResourceLoadStatisticsStore& store() { return m_store; } |
204 | Ref<WorkQueue>& workQueue() { return m_workQueue; } |
205 | #if HAVE(CORE_PREDICTION) |
206 | ResourceLoadStatisticsClassifierCocoa& classifier() { return m_resourceLoadStatisticsClassifier; } |
207 | #else |
208 | ResourceLoadStatisticsClassifier& classifier() { return m_resourceLoadStatisticsClassifier; } |
209 | #endif |
210 | |
211 | struct Parameters { |
212 | size_t pruneEntriesDownTo { 800 }; |
213 | size_t maxStatisticsEntries { 1000 }; |
214 | Optional<Seconds> timeToLiveUserInteraction; |
215 | Seconds minimumTimeBetweenDataRecordsRemoval { 1_h }; |
216 | Seconds grandfatheringTime { 24_h * 7 }; |
217 | Seconds cacheMaxAgeCapTime { 24_h * 7 }; |
218 | Seconds clientSideCookiesAgeCapTime { 24_h * 7 }; |
219 | bool shouldNotifyPagesWhenDataRecordsWereScanned { false }; |
220 | bool shouldClassifyResourcesBeforeDataRecordsRemoval { true }; |
221 | bool shouldSubmitTelemetry { true }; |
222 | bool isRunningTest { false }; |
223 | }; |
224 | const Parameters& parameters() const { return m_parameters; } |
225 | const Vector<OperatingDate>& operatingDates() const { return m_operatingDates; } |
226 | void clearOperatingDates() { m_operatingDates.clear(); } |
227 | WallTime& endOfGrandfatheringTimestamp() { return m_endOfGrandfatheringTimestamp; } |
228 | const WallTime& endOfGrandfatheringTimestamp() const { return m_endOfGrandfatheringTimestamp; } |
229 | void setEndOfGrandfatheringTimestamp(const WallTime& grandfatheringTime) { m_endOfGrandfatheringTimestamp = grandfatheringTime; } |
230 | void clearEndOfGrandfatheringTimeStamp() { m_endOfGrandfatheringTimestamp = { }; } |
231 | const RegistrableDomain& debugManualPrevalentResource() const { return m_debugManualPrevalentResource; } |
232 | const RegistrableDomain& debugStaticPrevalentResource() const { return m_debugStaticPrevalentResource; } |
233 | bool debugLoggingEnabled() const { return m_debugLoggingEnabled; }; |
234 | bool debugModeEnabled() const { return m_debugModeEnabled; } |
235 | |
236 | static constexpr unsigned maxNumberOfRecursiveCallsInRedirectTraceBack { 50 }; |
237 | |
238 | private: |
239 | bool shouldRemoveDataRecords() const; |
240 | void setDebugLogggingEnabled(bool enabled) { m_debugLoggingEnabled = enabled; } |
241 | void setDataRecordsBeingRemoved(bool); |
242 | void removeDataRecords(CompletionHandler<void()>&&); |
243 | void setCacheMaxAgeCap(Seconds); |
244 | void updateCacheMaxAgeCap(); |
245 | void setAgeCapForClientSideCookies(Seconds); |
246 | void updateClientSideCookiesAgeCap(); |
247 | |
248 | WebResourceLoadStatisticsStore& m_store; |
249 | Ref<WorkQueue> m_workQueue; |
250 | #if HAVE(CORE_PREDICTION) |
251 | ResourceLoadStatisticsClassifierCocoa m_resourceLoadStatisticsClassifier; |
252 | #else |
253 | ResourceLoadStatisticsClassifier m_resourceLoadStatisticsClassifier; |
254 | #endif |
255 | #if ENABLE(NETSCAPE_PLUGIN_API) |
256 | HashSet<uint64_t> m_activePluginTokens; |
257 | #endif |
258 | Parameters m_parameters; |
259 | Vector<OperatingDate> m_operatingDates; |
260 | WallTime m_endOfGrandfatheringTimestamp; |
261 | RegistrableDomain m_debugManualPrevalentResource; |
262 | MonotonicTime m_lastTimeDataRecordsWereRemoved; |
263 | uint64_t m_lastStatisticsProcessingRequestIdentifier { 0 }; |
264 | Optional<uint64_t> m_pendingStatisticsProcessingRequestIdentifier; |
265 | const RegistrableDomain m_debugStaticPrevalentResource { URL { URL(), "https://3rdpartytestwebkit.org"_s } }; |
266 | bool m_debugLoggingEnabled { false }; |
267 | bool m_debugModeEnabled { false }; |
268 | bool m_dataRecordsBeingRemoved { false }; |
269 | ShouldIncludeLocalhost m_shouldIncludeLocalhost { ShouldIncludeLocalhost::Yes }; |
270 | }; |
271 | |
272 | } // namespace WebKit |
273 | |
274 | #endif |
275 | |