Resource Load Statistics: Introduce debug mode as experimental feature
[WebKit-https.git] / Source / WebKit / UIProcess / WebResourceLoadStatisticsStore.cpp
1 /*
2  * Copyright (C) 2016-2018 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 #include "config.h"
27 #include "WebResourceLoadStatisticsStore.h"
28
29 #include "Logging.h"
30 #include "PluginProcessManager.h"
31 #include "PluginProcessProxy.h"
32 #include "WebProcessMessages.h"
33 #include "WebProcessProxy.h"
34 #include "WebResourceLoadStatisticsStoreMessages.h"
35 #include "WebResourceLoadStatisticsTelemetry.h"
36 #include "WebsiteDataFetchOption.h"
37 #include "WebsiteDataStore.h"
38 #include <WebCore/KeyedCoding.h>
39 #include <WebCore/ResourceLoadStatistics.h>
40 #include <wtf/CrossThreadCopier.h>
41 #include <wtf/DateMath.h>
42 #include <wtf/MathExtras.h>
43 #include <wtf/NeverDestroyed.h>
44
45 using namespace WebCore;
46
47 namespace WebKit {
48
49 constexpr unsigned operatingDatesWindow { 30 };
50 constexpr unsigned statisticsModelVersion { 10 };
51 constexpr unsigned maxImportance { 3 };
52
53 template<typename T> static inline String isolatedPrimaryDomain(const T& value)
54 {
55     return ResourceLoadStatistics::primaryDomain(value).isolatedCopy();
56 }
57
58 const OptionSet<WebsiteDataType>& WebResourceLoadStatisticsStore::monitoredDataTypes()
59 {
60     static NeverDestroyed<OptionSet<WebsiteDataType>> dataTypes(std::initializer_list<WebsiteDataType>({
61         WebsiteDataType::Cookies,
62         WebsiteDataType::DOMCache,
63         WebsiteDataType::IndexedDBDatabases,
64         WebsiteDataType::LocalStorage,
65         WebsiteDataType::MediaKeys,
66         WebsiteDataType::OfflineWebApplicationCache,
67 #if ENABLE(NETSCAPE_PLUGIN_API)
68         WebsiteDataType::PlugInData,
69 #endif
70         WebsiteDataType::SearchFieldRecentSearches,
71         WebsiteDataType::SessionStorage,
72 #if ENABLE(SERVICE_WORKER)
73         WebsiteDataType::ServiceWorkerRegistrations,
74 #endif
75         WebsiteDataType::WebSQLDatabases,
76     }));
77
78     ASSERT(RunLoop::isMain());
79
80     return dataTypes;
81 }
82
83 class OperatingDate {
84 public:
85     OperatingDate() = default;
86
87     static OperatingDate fromWallTime(WallTime time)
88     {
89         double ms = time.secondsSinceEpoch().milliseconds();
90         int year = msToYear(ms);
91         int yearDay = dayInYear(ms, year);
92         int month = monthFromDayInYear(yearDay, isLeapYear(year));
93         int monthDay = dayInMonthFromDayInYear(yearDay, isLeapYear(year));
94
95         return OperatingDate { year, month, monthDay };
96     }
97
98     static OperatingDate today()
99     {
100         return OperatingDate::fromWallTime(WallTime::now());
101     }
102
103     Seconds secondsSinceEpoch() const
104     {
105         return Seconds { dateToDaysFrom1970(m_year, m_month, m_monthDay) * secondsPerDay };
106     }
107
108     bool operator==(const OperatingDate& other) const
109     {
110         return m_monthDay == other.m_monthDay && m_month == other.m_month && m_year == other.m_year;
111     }
112
113     bool operator<(const OperatingDate& other) const
114     {
115         return secondsSinceEpoch() < other.secondsSinceEpoch();
116     }
117
118     bool operator<=(const OperatingDate& other) const
119     {
120         return secondsSinceEpoch() <= other.secondsSinceEpoch();
121     }
122
123 private:
124     OperatingDate(int year, int month, int monthDay)
125         : m_year(year)
126         , m_month(month)
127         , m_monthDay(monthDay)
128     { }
129
130     int m_year { 0 };
131     int m_month { 0 }; // [0, 11].
132     int m_monthDay { 0 }; // [1, 31].
133 };
134
135 static Vector<OperatingDate> mergeOperatingDates(const Vector<OperatingDate>& existingDates, Vector<OperatingDate>&& newDates)
136 {
137     if (existingDates.isEmpty())
138         return WTFMove(newDates);
139
140     Vector<OperatingDate> mergedDates(existingDates.size() + newDates.size());
141
142     // Merge the two sorted vectors of dates.
143     std::merge(existingDates.begin(), existingDates.end(), newDates.begin(), newDates.end(), mergedDates.begin());
144     // Remove duplicate dates.
145     removeRepeatedElements(mergedDates);
146
147     // Drop old dates until the Vector size reaches operatingDatesWindow.
148     while (mergedDates.size() > operatingDatesWindow)
149         mergedDates.remove(0);
150
151     return mergedDates;
152 }
153
154 WebResourceLoadStatisticsStore::WebResourceLoadStatisticsStore(const String& resourceLoadStatisticsDirectory, Function<void(const String&)>&& testingCallback, bool isEphemeral, UpdatePrevalentDomainsToPartitionOrBlockCookiesHandler&& updatePrevalentDomainsToPartitionOrBlockCookiesHandler, HasStorageAccessForFrameHandler&& hasStorageAccessForFrameHandler, GrantStorageAccessForFrameHandler&& grantStorageAccessForFrameHandler, RemovePrevalentDomainsHandler&& removeDomainsHandler)
155     : m_statisticsQueue(WorkQueue::create("WebResourceLoadStatisticsStore Process Data Queue", WorkQueue::Type::Serial, WorkQueue::QOS::Utility))
156     , m_persistentStorage(*this, resourceLoadStatisticsDirectory, isEphemeral ? ResourceLoadStatisticsPersistentStorage::IsReadOnly::Yes : ResourceLoadStatisticsPersistentStorage::IsReadOnly::No)
157     , m_updatePrevalentDomainsToPartitionOrBlockCookiesHandler(WTFMove(updatePrevalentDomainsToPartitionOrBlockCookiesHandler))
158     , m_hasStorageAccessForFrameHandler(WTFMove(hasStorageAccessForFrameHandler))
159     , m_grantStorageAccessForFrameHandler(WTFMove(grantStorageAccessForFrameHandler))
160     , m_removeDomainsHandler(WTFMove(removeDomainsHandler))
161     , m_dailyTasksTimer(RunLoop::main(), this, &WebResourceLoadStatisticsStore::performDailyTasks)
162     , m_statisticsTestingCallback(WTFMove(testingCallback))
163 {
164     ASSERT(RunLoop::isMain());
165
166 #if PLATFORM(COCOA)
167     registerUserDefaultsIfNeeded();
168 #endif
169
170     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this)] {
171         m_persistentStorage.initialize();
172         includeTodayAsOperatingDateIfNecessary();
173     });
174
175     m_statisticsQueue->dispatchAfter(5_s, [this, protectedThis = makeRef(*this)] {
176         if (m_parameters.shouldSubmitTelemetry)
177             WebResourceLoadStatisticsTelemetry::calculateAndSubmit(*this);
178     });
179
180     m_dailyTasksTimer.startRepeating(24_h);
181 }
182
183 WebResourceLoadStatisticsStore::~WebResourceLoadStatisticsStore()
184 {
185     m_persistentStorage.finishAllPendingWorkSynchronously();
186 }
187     
188 void WebResourceLoadStatisticsStore::removeDataRecords()
189 {
190     ASSERT(!RunLoop::isMain());
191     
192     if (!shouldRemoveDataRecords())
193         return;
194
195 #if ENABLE(NETSCAPE_PLUGIN_API)
196     m_activePluginTokens.clear();
197     for (auto plugin : PluginProcessManager::singleton().pluginProcesses())
198         m_activePluginTokens.add(plugin->pluginProcessToken());
199 #endif
200
201     auto prevalentResourceDomains = topPrivatelyControlledDomainsToRemoveWebsiteDataFor();
202     if (prevalentResourceDomains.isEmpty())
203         return;
204     
205     setDataRecordsBeingRemoved(true);
206
207     RunLoop::main().dispatch([prevalentResourceDomains = crossThreadCopy(prevalentResourceDomains), this, protectedThis = makeRef(*this)] () mutable {
208         WebProcessProxy::deleteWebsiteDataForTopPrivatelyControlledDomainsInAllPersistentDataStores(WebResourceLoadStatisticsStore::monitoredDataTypes(), WTFMove(prevalentResourceDomains), m_parameters.shouldNotifyPagesWhenDataRecordsWereScanned, [this, protectedThis = WTFMove(protectedThis)](const HashSet<String>& domainsWithDeletedWebsiteData) mutable {
209             m_statisticsQueue->dispatch([this, protectedThis = WTFMove(protectedThis), topDomains = crossThreadCopy(domainsWithDeletedWebsiteData)] () mutable {
210                 for (auto& prevalentResourceDomain : topDomains) {
211                     auto& statistic = ensureResourceStatisticsForPrimaryDomain(prevalentResourceDomain);
212                     ++statistic.dataRecordsRemoved;
213                 }
214                 setDataRecordsBeingRemoved(false);
215             });
216         });
217     });
218 }
219
220 void WebResourceLoadStatisticsStore::scheduleStatisticsAndDataRecordsProcessing()
221 {
222     ASSERT(RunLoop::isMain());
223     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this)] {
224         processStatisticsAndDataRecords();
225     });
226 }
227
228 void WebResourceLoadStatisticsStore::processStatisticsAndDataRecords()
229 {
230     ASSERT(!RunLoop::isMain());
231
232     if (m_parameters.shouldClassifyResourcesBeforeDataRecordsRemoval) {
233         for (auto& resourceStatistic : m_resourceStatisticsMap.values()) {
234             if (!resourceStatistic.isPrevalentResource && m_resourceLoadStatisticsClassifier.hasPrevalentResourceCharacteristics(resourceStatistic))
235                 resourceStatistic.isPrevalentResource = true;
236         }
237     }
238     removeDataRecords();
239
240     pruneStatisticsIfNeeded();
241
242     if (m_parameters.shouldNotifyPagesWhenDataRecordsWereScanned) {
243         RunLoop::main().dispatch([] {
244             WebProcessProxy::notifyPageStatisticsAndDataRecordsProcessed();
245         });
246     }
247
248     m_persistentStorage.scheduleOrWriteMemoryStore(ResourceLoadStatisticsPersistentStorage::ForceImmediateWrite::No);
249 }
250
251 void WebResourceLoadStatisticsStore::resourceLoadStatisticsUpdated(Vector<WebCore::ResourceLoadStatistics>&& origins)
252 {
253     ASSERT(!RunLoop::isMain());
254
255     mergeStatistics(WTFMove(origins));
256     // Fire before processing statistics to propagate user interaction as fast as possible to the network process.
257     updateCookiePartitioning();
258     processStatisticsAndDataRecords();
259 }
260
261 void WebResourceLoadStatisticsStore::hasStorageAccess(String&& subFrameHost, String&& topFrameHost, uint64_t frameID, uint64_t pageID, WTF::CompletionHandler<void (bool)>&& callback)
262 {
263     ASSERT(subFrameHost != topFrameHost);
264     ASSERT(RunLoop::isMain());
265
266     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), subFramePrimaryDomain = isolatedPrimaryDomain(subFrameHost), topFramePrimaryDomain = isolatedPrimaryDomain(topFrameHost), frameID, pageID, callback = WTFMove(callback)] () mutable {
267         
268         auto& subFrameStatistic = ensureResourceStatisticsForPrimaryDomain(subFramePrimaryDomain);
269         if (shouldBlockCookies(subFrameStatistic)) {
270             callback(false);
271             return;
272         }
273
274         if (!shouldPartitionCookies(subFrameStatistic)) {
275             callback(true);
276             return;
277         }
278
279         m_hasStorageAccessForFrameHandler(subFramePrimaryDomain, topFramePrimaryDomain, frameID, pageID, WTFMove(callback));
280     });
281 }
282
283 void WebResourceLoadStatisticsStore::requestStorageAccess(String&& subFrameHost, String&& topFrameHost, uint64_t frameID, uint64_t pageID, WTF::CompletionHandler<void (bool)>&& callback)
284 {
285     ASSERT(subFrameHost != topFrameHost);
286     ASSERT(RunLoop::isMain());
287
288     auto subFramePrimaryDomain = isolatedPrimaryDomain(subFrameHost);
289     auto topFramePrimaryDomain = isolatedPrimaryDomain(topFrameHost);
290     if (subFramePrimaryDomain == topFramePrimaryDomain) {
291         callback(true);
292         return;
293     }
294
295     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), subFramePrimaryDomain = crossThreadCopy(subFramePrimaryDomain), topFramePrimaryDomain = crossThreadCopy(topFramePrimaryDomain), frameID, pageID, callback = WTFMove(callback)] () mutable {
296
297         auto& subFrameStatistic = ensureResourceStatisticsForPrimaryDomain(subFramePrimaryDomain);
298         if (shouldBlockCookies(subFrameStatistic)) {
299             callback(false);
300             return;
301         }
302         
303         if (!shouldPartitionCookies(subFrameStatistic)) {
304             callback(true);
305             return;
306         }
307         
308         m_grantStorageAccessForFrameHandler(subFramePrimaryDomain, topFramePrimaryDomain, frameID, pageID, WTFMove(callback));
309     });
310 }
311     
312 void WebResourceLoadStatisticsStore::grandfatherExistingWebsiteData(CompletionHandler<void()>&& callback)
313 {
314     ASSERT(!RunLoop::isMain());
315
316     RunLoop::main().dispatch([this, protectedThis = makeRef(*this), callback = WTFMove(callback)] () mutable {
317         // FIXME: This method being a static call on WebProcessProxy is wrong.
318         // It should be on the data store that this object belongs to.
319         WebProcessProxy::topPrivatelyControlledDomainsWithWebsiteData(WebResourceLoadStatisticsStore::monitoredDataTypes(), m_parameters.shouldNotifyPagesWhenDataRecordsWereScanned, [this, protectedThis = WTFMove(protectedThis), callback = WTFMove(callback)] (HashSet<String>&& topPrivatelyControlledDomainsWithWebsiteData) mutable {
320             m_statisticsQueue->dispatch([this, protectedThis = WTFMove(protectedThis), topDomains = crossThreadCopy(topPrivatelyControlledDomainsWithWebsiteData), callback = WTFMove(callback)] () mutable {
321                 for (auto& topPrivatelyControlledDomain : topDomains) {
322                     auto& statistic = ensureResourceStatisticsForPrimaryDomain(topPrivatelyControlledDomain);
323                     statistic.grandfathered = true;
324                 }
325                 m_endOfGrandfatheringTimestamp = WallTime::now() + m_parameters.grandfatheringTime;
326                 m_persistentStorage.scheduleOrWriteMemoryStore(ResourceLoadStatisticsPersistentStorage::ForceImmediateWrite::Yes);
327                 callback();
328                 logTestingEvent(ASCIILiteral("Grandfathered"));
329             });
330         });
331     });
332 }
333     
334 void WebResourceLoadStatisticsStore::processWillOpenConnection(WebProcessProxy&, IPC::Connection& connection)
335 {
336     connection.addWorkQueueMessageReceiver(Messages::WebResourceLoadStatisticsStore::messageReceiverName(), m_statisticsQueue.get(), this);
337 }
338
339 void WebResourceLoadStatisticsStore::processDidCloseConnection(WebProcessProxy&, IPC::Connection& connection)
340 {
341     connection.removeWorkQueueMessageReceiver(Messages::WebResourceLoadStatisticsStore::messageReceiverName());
342 }
343
344 void WebResourceLoadStatisticsStore::applicationWillTerminate()
345 {
346     m_persistentStorage.finishAllPendingWorkSynchronously();
347 }
348
349 void WebResourceLoadStatisticsStore::performDailyTasks()
350 {
351     ASSERT(RunLoop::isMain());
352
353     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this)] {
354         includeTodayAsOperatingDateIfNecessary();
355     });
356     if (m_parameters.shouldSubmitTelemetry)
357         submitTelemetry();
358 }
359
360 void WebResourceLoadStatisticsStore::submitTelemetry()
361 {
362     ASSERT(RunLoop::isMain());
363     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this)] {
364         WebResourceLoadStatisticsTelemetry::calculateAndSubmit(*this);
365     });
366 }
367
368 void WebResourceLoadStatisticsStore::setResourceLoadStatisticsDebugMode(bool enable)
369 {
370     if (enable)
371         setTimeToLiveCookiePartitionFree(30_s);
372     else
373         resetParametersToDefaultValues();
374 }
375
376 void WebResourceLoadStatisticsStore::logUserInteraction(const URL& url)
377 {
378     if (url.isBlankURL() || url.isEmpty())
379         return;
380
381     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryDomain = isolatedPrimaryDomain(url)] {
382         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primaryDomain);
383         statistics.hadUserInteraction = true;
384         statistics.mostRecentUserInteractionTime = WallTime::now();
385
386         if (statistics.isMarkedForCookiePartitioning || statistics.isMarkedForCookieBlocking)
387             updateCookiePartitioningForDomains({ }, { }, { primaryDomain }, ShouldClearFirst::No);
388     });
389 }
390
391 void WebResourceLoadStatisticsStore::logNonRecentUserInteraction(const URL& url)
392 {
393     if (url.isBlankURL() || url.isEmpty())
394         return;
395     
396     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryDomain = isolatedPrimaryDomain(url)] {
397         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primaryDomain);
398         statistics.hadUserInteraction = true;
399         statistics.mostRecentUserInteractionTime = WallTime::now() - (m_parameters.timeToLiveCookiePartitionFree + Seconds::fromHours(1));
400
401         updateCookiePartitioningForDomains({ primaryDomain }, { }, { }, ShouldClearFirst::No);
402     });
403 }
404
405 void WebResourceLoadStatisticsStore::clearUserInteraction(const URL& url)
406 {
407     if (url.isBlankURL() || url.isEmpty())
408         return;
409
410     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryDomain = isolatedPrimaryDomain(url)] {
411         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primaryDomain);
412         statistics.hadUserInteraction = false;
413         statistics.mostRecentUserInteractionTime = { };
414     });
415 }
416
417 void WebResourceLoadStatisticsStore::hasHadUserInteraction(const URL& url, WTF::Function<void (bool)>&& completionHandler)
418 {
419     if (url.isBlankURL() || url.isEmpty()) {
420         completionHandler(false);
421         return;
422     }
423
424     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryDomain = isolatedPrimaryDomain(url), completionHandler = WTFMove(completionHandler)] () mutable {
425         auto mapEntry = m_resourceStatisticsMap.find(primaryDomain);
426         bool hadUserInteraction = mapEntry == m_resourceStatisticsMap.end() ? false: hasHadUnexpiredRecentUserInteraction(mapEntry->value);
427         RunLoop::main().dispatch([hadUserInteraction, completionHandler = WTFMove(completionHandler)] {
428             completionHandler(hadUserInteraction);
429         });
430     });
431 }
432
433 void WebResourceLoadStatisticsStore::setLastSeen(const URL& url, Seconds seconds)
434 {
435     if (url.isBlankURL() || url.isEmpty())
436         return;
437     
438     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryDomain = isolatedPrimaryDomain(url), seconds] {
439         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primaryDomain);
440         statistics.lastSeen = WallTime::fromRawSeconds(seconds.seconds());
441     });
442 }
443     
444 void WebResourceLoadStatisticsStore::setPrevalentResource(const URL& url)
445 {
446     if (url.isBlankURL() || url.isEmpty())
447         return;
448
449     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryDomain = isolatedPrimaryDomain(url)] {
450         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primaryDomain);
451         statistics.isPrevalentResource = true;
452     });
453 }
454
455 void WebResourceLoadStatisticsStore::isPrevalentResource(const URL& url, WTF::Function<void (bool)>&& completionHandler)
456 {
457     if (url.isBlankURL() || url.isEmpty()) {
458         completionHandler(false);
459         return;
460     }
461
462     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryDomain = isolatedPrimaryDomain(url), completionHandler = WTFMove(completionHandler)] () mutable {
463         auto mapEntry = m_resourceStatisticsMap.find(primaryDomain);
464         bool isPrevalentResource = mapEntry == m_resourceStatisticsMap.end() ? false : mapEntry->value.isPrevalentResource;
465         RunLoop::main().dispatch([isPrevalentResource, completionHandler = WTFMove(completionHandler)] {
466             completionHandler(isPrevalentResource);
467         });
468     });
469 }
470
471 void WebResourceLoadStatisticsStore::isRegisteredAsSubFrameUnder(const URL& subFrame, const URL& topFrame, WTF::Function<void (bool)>&& completionHandler)
472 {
473     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), subFramePrimaryDomain = isolatedPrimaryDomain(subFrame), topFramePrimaryDomain = isolatedPrimaryDomain(topFrame), completionHandler = WTFMove(completionHandler)] () mutable {
474         auto mapEntry = m_resourceStatisticsMap.find(subFramePrimaryDomain);
475         bool isRegisteredAsSubFrameUnder = mapEntry == m_resourceStatisticsMap.end() ? false : mapEntry->value.subframeUnderTopFrameOrigins.contains(topFramePrimaryDomain);
476         RunLoop::main().dispatch([isRegisteredAsSubFrameUnder, completionHandler = WTFMove(completionHandler)] {
477             completionHandler(isRegisteredAsSubFrameUnder);
478         });
479     });
480 }
481
482 void WebResourceLoadStatisticsStore::isRegisteredAsRedirectingTo(const URL& hostRedirectedFrom, const URL& hostRedirectedTo, WTF::Function<void (bool)>&& completionHandler)
483 {
484     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), hostRedirectedFromPrimaryDomain = isolatedPrimaryDomain(hostRedirectedFrom), hostRedirectedToPrimaryDomain = isolatedPrimaryDomain(hostRedirectedTo), completionHandler = WTFMove(completionHandler)] () mutable {
485         auto mapEntry = m_resourceStatisticsMap.find(hostRedirectedFromPrimaryDomain);
486         bool isRegisteredAsRedirectingTo = mapEntry == m_resourceStatisticsMap.end() ? false : mapEntry->value.subresourceUniqueRedirectsTo.contains(hostRedirectedToPrimaryDomain);
487         RunLoop::main().dispatch([isRegisteredAsRedirectingTo, completionHandler = WTFMove(completionHandler)] {
488             completionHandler(isRegisteredAsRedirectingTo);
489         });
490     });
491 }
492
493 void WebResourceLoadStatisticsStore::clearPrevalentResource(const URL& url)
494 {
495     if (url.isBlankURL() || url.isEmpty())
496         return;
497
498     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryDomain = isolatedPrimaryDomain(url)] {
499         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primaryDomain);
500         statistics.isPrevalentResource = false;
501     });
502 }
503
504 void WebResourceLoadStatisticsStore::setGrandfathered(const URL& url, bool value)
505 {
506     if (url.isBlankURL() || url.isEmpty())
507         return;
508
509     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryDomain = isolatedPrimaryDomain(url), value] {
510         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primaryDomain);
511         statistics.grandfathered = value;
512     });
513 }
514
515 void WebResourceLoadStatisticsStore::isGrandfathered(const URL& url, WTF::Function<void (bool)>&& completionHandler)
516 {
517     if (url.isBlankURL() || url.isEmpty()) {
518         completionHandler(false);
519         return;
520     }
521
522     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), completionHandler = WTFMove(completionHandler), primaryDomain = isolatedPrimaryDomain(url)] () mutable {
523         auto mapEntry = m_resourceStatisticsMap.find(primaryDomain);
524         bool isGrandFathered = mapEntry == m_resourceStatisticsMap.end() ? false : mapEntry->value.grandfathered;
525         RunLoop::main().dispatch([isGrandFathered, completionHandler = WTFMove(completionHandler)] {
526             completionHandler(isGrandFathered);
527         });
528     });
529 }
530
531 void WebResourceLoadStatisticsStore::setSubframeUnderTopFrameOrigin(const URL& subframe, const URL& topFrame)
532 {
533     if (subframe.isBlankURL() || subframe.isEmpty() || topFrame.isBlankURL() || topFrame.isEmpty())
534         return;
535
536     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryTopFrameDomain = isolatedPrimaryDomain(topFrame), primarySubFrameDomain = isolatedPrimaryDomain(subframe)] {
537         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primarySubFrameDomain);
538         statistics.subframeUnderTopFrameOrigins.add(primaryTopFrameDomain);
539     });
540 }
541
542 void WebResourceLoadStatisticsStore::setSubresourceUnderTopFrameOrigin(const URL& subresource, const URL& topFrame)
543 {
544     if (subresource.isBlankURL() || subresource.isEmpty() || topFrame.isBlankURL() || topFrame.isEmpty())
545         return;
546
547     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryTopFrameDomain = isolatedPrimaryDomain(topFrame), primarySubresourceDomain = isolatedPrimaryDomain(subresource)] {
548         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primarySubresourceDomain);
549         statistics.subresourceUnderTopFrameOrigins.add(primaryTopFrameDomain);
550     });
551 }
552
553 void WebResourceLoadStatisticsStore::setSubresourceUniqueRedirectTo(const URL& subresource, const URL& hostNameRedirectedTo)
554 {
555     if (subresource.isBlankURL() || subresource.isEmpty() || hostNameRedirectedTo.isBlankURL() || hostNameRedirectedTo.isEmpty())
556         return;
557
558     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryRedirectDomain = isolatedPrimaryDomain(hostNameRedirectedTo), primarySubresourceDomain = isolatedPrimaryDomain(subresource)] {
559         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primarySubresourceDomain);
560         statistics.subresourceUniqueRedirectsTo.add(primaryRedirectDomain);
561     });
562 }
563
564 void WebResourceLoadStatisticsStore::scheduleCookiePartitioningUpdate()
565 {
566     // Helper function used by testing system. Should only be called from the main thread.
567     ASSERT(RunLoop::isMain());
568
569     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this)] {
570         updateCookiePartitioning();
571     });
572 }
573
574 void WebResourceLoadStatisticsStore::scheduleCookiePartitioningUpdateForDomains(const Vector<String>& domainsToPartition, const Vector<String>& domainsToBlock, const Vector<String>& domainsToNeitherPartitionNorBlock, ShouldClearFirst shouldClearFirst)
575 {
576     // Helper function used by testing system. Should only be called from the main thread.
577     ASSERT(RunLoop::isMain());
578     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), domainsToPartition = crossThreadCopy(domainsToPartition), domainsToBlock = crossThreadCopy(domainsToBlock), domainsToNeitherPartitionNorBlock = crossThreadCopy(domainsToNeitherPartitionNorBlock), shouldClearFirst] {
579         updateCookiePartitioningForDomains(domainsToPartition, domainsToBlock, domainsToNeitherPartitionNorBlock, shouldClearFirst);
580     });
581 }
582
583 void WebResourceLoadStatisticsStore::scheduleClearPartitioningStateForDomains(const Vector<String>& domains)
584 {
585     // Helper function used by testing system. Should only be called from the main thread.
586     ASSERT(RunLoop::isMain());
587     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), domains = crossThreadCopy(domains)] {
588         clearPartitioningStateForDomains(domains);
589     });
590 }
591
592 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
593 void WebResourceLoadStatisticsStore::scheduleCookiePartitioningStateReset()
594 {
595     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this)] {
596         resetCookiePartitioningState();
597     });
598 }
599 #endif
600
601 void WebResourceLoadStatisticsStore::scheduleClearInMemory()
602 {
603     ASSERT(RunLoop::isMain());
604     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this)] {
605         clearInMemory();
606     });
607 }
608
609 void WebResourceLoadStatisticsStore::scheduleClearInMemoryAndPersistent(ShouldGrandfather shouldGrandfather, CompletionHandler<void()>&& callback)
610 {
611     ASSERT(RunLoop::isMain());
612     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), shouldGrandfather, callback = WTFMove(callback)] () mutable {
613         clearInMemory();
614         m_persistentStorage.clear();
615         
616         if (shouldGrandfather == ShouldGrandfather::Yes)
617             grandfatherExistingWebsiteData([protectedThis = makeRef(*this), callback = WTFMove(callback)]() {
618                 callback();
619             });
620         else {
621             callback();
622         }
623     });
624 }
625
626 void WebResourceLoadStatisticsStore::scheduleClearInMemoryAndPersistent(WallTime modifiedSince, ShouldGrandfather shouldGrandfather, CompletionHandler<void()>&& callback)
627 {
628     // For now, be conservative and clear everything regardless of modifiedSince.
629     UNUSED_PARAM(modifiedSince);
630     scheduleClearInMemoryAndPersistent(shouldGrandfather, WTFMove(callback));
631 }
632
633 void WebResourceLoadStatisticsStore::setTimeToLiveUserInteraction(Seconds seconds)
634 {
635     ASSERT(seconds >= 0_s);
636     m_parameters.timeToLiveUserInteraction = seconds;
637 }
638
639 void WebResourceLoadStatisticsStore::setTimeToLiveCookiePartitionFree(Seconds seconds)
640 {
641     ASSERT(seconds >= 0_s);
642     m_parameters.timeToLiveCookiePartitionFree = seconds;
643 }
644
645 void WebResourceLoadStatisticsStore::setMinimumTimeBetweenDataRecordsRemoval(Seconds seconds)
646 {
647     ASSERT(seconds >= 0_s);
648     m_parameters.minimumTimeBetweenDataRecordsRemoval = seconds;
649 }
650
651 void WebResourceLoadStatisticsStore::setGrandfatheringTime(Seconds seconds)
652 {
653     ASSERT(seconds >= 0_s);
654     m_parameters.grandfatheringTime = seconds;
655 }
656
657 bool WebResourceLoadStatisticsStore::shouldRemoveDataRecords() const
658 {
659     ASSERT(!RunLoop::isMain());
660     if (m_dataRecordsBeingRemoved)
661         return false;
662
663 #if ENABLE(NETSCAPE_PLUGIN_API)
664     for (auto plugin : PluginProcessManager::singleton().pluginProcesses()) {
665         if (!m_activePluginTokens.contains(plugin->pluginProcessToken()))
666             return true;
667     }
668 #endif
669
670     return !m_lastTimeDataRecordsWereRemoved || MonotonicTime::now() >= (m_lastTimeDataRecordsWereRemoved + m_parameters.minimumTimeBetweenDataRecordsRemoval);
671 }
672
673 void WebResourceLoadStatisticsStore::setDataRecordsBeingRemoved(bool value)
674 {
675     ASSERT(!RunLoop::isMain());
676     m_dataRecordsBeingRemoved = value;
677     if (m_dataRecordsBeingRemoved)
678         m_lastTimeDataRecordsWereRemoved = MonotonicTime::now();
679 }
680
681 ResourceLoadStatistics& WebResourceLoadStatisticsStore::ensureResourceStatisticsForPrimaryDomain(const String& primaryDomain)
682 {
683     ASSERT(!RunLoop::isMain());
684     return m_resourceStatisticsMap.ensure(primaryDomain, [&primaryDomain] {
685         return ResourceLoadStatistics(primaryDomain);
686     }).iterator->value;
687 }
688
689 std::unique_ptr<KeyedEncoder> WebResourceLoadStatisticsStore::createEncoderFromData() const
690 {
691     ASSERT(!RunLoop::isMain());
692     auto encoder = KeyedEncoder::encoder();
693     encoder->encodeUInt32("version", statisticsModelVersion);
694     encoder->encodeDouble("endOfGrandfatheringTimestamp", m_endOfGrandfatheringTimestamp.secondsSinceEpoch().value());
695
696     encoder->encodeObjects("browsingStatistics", m_resourceStatisticsMap.begin(), m_resourceStatisticsMap.end(), [](KeyedEncoder& encoderInner, const auto& origin) {
697         origin.value.encode(encoderInner);
698     });
699
700     encoder->encodeObjects("operatingDates", m_operatingDates.begin(), m_operatingDates.end(), [](KeyedEncoder& encoderInner, OperatingDate date) {
701         encoderInner.encodeDouble("date", date.secondsSinceEpoch().value());
702     });
703
704     return encoder;
705 }
706
707 void WebResourceLoadStatisticsStore::mergeWithDataFromDecoder(KeyedDecoder& decoder)
708 {
709     ASSERT(!RunLoop::isMain());
710
711     unsigned versionOnDisk;
712     if (!decoder.decodeUInt32("version", versionOnDisk))
713         return;
714
715     if (versionOnDisk != statisticsModelVersion)
716         return;
717
718     double endOfGrandfatheringTimestamp;
719     if (decoder.decodeDouble("endOfGrandfatheringTimestamp", endOfGrandfatheringTimestamp))
720         m_endOfGrandfatheringTimestamp = WallTime::fromRawSeconds(endOfGrandfatheringTimestamp);
721     else
722         m_endOfGrandfatheringTimestamp = { };
723
724     Vector<ResourceLoadStatistics> loadedStatistics;
725     bool succeeded = decoder.decodeObjects("browsingStatistics", loadedStatistics, [](KeyedDecoder& decoderInner, ResourceLoadStatistics& statistics) {
726         return statistics.decode(decoderInner);
727     });
728
729     if (!succeeded)
730         return;
731
732     mergeStatistics(WTFMove(loadedStatistics));
733     updateCookiePartitioning();
734
735     Vector<OperatingDate> operatingDates;
736     succeeded = decoder.decodeObjects("operatingDates", operatingDates, [](KeyedDecoder& decoder, OperatingDate& date) {
737         double value;
738         if (!decoder.decodeDouble("date", value))
739             return false;
740
741         date = OperatingDate::fromWallTime(WallTime::fromRawSeconds(value));
742         return true;
743     });
744
745     if (!succeeded)
746         return;
747
748     m_operatingDates = mergeOperatingDates(m_operatingDates, WTFMove(operatingDates));
749 }
750
751 void WebResourceLoadStatisticsStore::clearInMemory()
752 {
753     ASSERT(!RunLoop::isMain());
754     m_resourceStatisticsMap.clear();
755     m_operatingDates.clear();
756
757     updateCookiePartitioningForDomains({ }, { }, { }, ShouldClearFirst::Yes);
758 }
759
760 void WebResourceLoadStatisticsStore::mergeStatistics(Vector<ResourceLoadStatistics>&& statistics)
761 {
762     ASSERT(!RunLoop::isMain());
763     for (auto& statistic : statistics) {
764         auto result = m_resourceStatisticsMap.ensure(statistic.highLevelDomain, [&statistic] {
765             return WTFMove(statistic);
766         });
767         if (!result.isNewEntry)
768             result.iterator->value.merge(statistic);
769     }
770 }
771
772 bool WebResourceLoadStatisticsStore::shouldPartitionCookies(const ResourceLoadStatistics& statistic) const
773 {
774     return statistic.isPrevalentResource && statistic.hadUserInteraction && WallTime::now() > statistic.mostRecentUserInteractionTime + m_parameters.timeToLiveCookiePartitionFree;
775 }
776
777 bool WebResourceLoadStatisticsStore::shouldBlockCookies(const ResourceLoadStatistics& statistic) const
778 {
779     return statistic.isPrevalentResource && !statistic.hadUserInteraction;
780 }
781
782 void WebResourceLoadStatisticsStore::updateCookiePartitioning()
783 {
784     ASSERT(!RunLoop::isMain());
785
786     Vector<String> domainsToPartition;
787     Vector<String> domainsToBlock;
788     Vector<String> domainsToNeitherPartitionNorBlock;
789     for (auto& resourceStatistic : m_resourceStatisticsMap.values()) {
790
791         bool shouldPartition = shouldPartitionCookies(resourceStatistic);
792         bool shouldBlock = shouldBlockCookies(resourceStatistic);
793
794         if (shouldPartition && !resourceStatistic.isMarkedForCookiePartitioning) {
795             domainsToPartition.append(resourceStatistic.highLevelDomain);
796             resourceStatistic.isMarkedForCookiePartitioning = true;
797         } else if (shouldBlock && !resourceStatistic.isMarkedForCookieBlocking) {
798             domainsToBlock.append(resourceStatistic.highLevelDomain);
799             resourceStatistic.isMarkedForCookieBlocking = true;
800         } else if (!shouldPartition && !shouldBlock && resourceStatistic.isPrevalentResource) {
801             domainsToNeitherPartitionNorBlock.append(resourceStatistic.highLevelDomain);
802             resourceStatistic.isMarkedForCookiePartitioning = false;
803             resourceStatistic.isMarkedForCookieBlocking = false;
804         }
805     }
806
807     if (domainsToPartition.isEmpty() && domainsToBlock.isEmpty() && domainsToNeitherPartitionNorBlock.isEmpty())
808         return;
809
810     RunLoop::main().dispatch([this, protectedThis = makeRef(*this), domainsToPartition = crossThreadCopy(domainsToPartition), domainsToBlock = crossThreadCopy(domainsToBlock), domainsToNeitherPartitionNorBlock = crossThreadCopy(domainsToNeitherPartitionNorBlock)] () {
811         m_updatePrevalentDomainsToPartitionOrBlockCookiesHandler(domainsToPartition, domainsToBlock, domainsToNeitherPartitionNorBlock, ShouldClearFirst::No);
812     });
813 }
814
815 void WebResourceLoadStatisticsStore::updateCookiePartitioningForDomains(const Vector<String>& domainsToPartition, const Vector<String>& domainsToBlock, const Vector<String>& domainsToNeitherPartitionNorBlock, ShouldClearFirst shouldClearFirst)
816 {
817     ASSERT(!RunLoop::isMain());
818     if (domainsToPartition.isEmpty() && domainsToBlock.isEmpty() && domainsToNeitherPartitionNorBlock.isEmpty() && shouldClearFirst == ShouldClearFirst::No)
819         return;
820
821     RunLoop::main().dispatch([this, shouldClearFirst, protectedThis = makeRef(*this), domainsToPartition = crossThreadCopy(domainsToPartition), domainsToBlock = crossThreadCopy(domainsToBlock), domainsToNeitherPartitionNorBlock = crossThreadCopy(domainsToNeitherPartitionNorBlock)] () {
822         m_updatePrevalentDomainsToPartitionOrBlockCookiesHandler(domainsToPartition, domainsToBlock, domainsToNeitherPartitionNorBlock, shouldClearFirst);
823     });
824
825     if (shouldClearFirst == ShouldClearFirst::Yes)
826         resetCookiePartitioningState();
827     else {
828         for (auto& domain : domainsToNeitherPartitionNorBlock) {
829             auto& statistic = ensureResourceStatisticsForPrimaryDomain(domain);
830             statistic.isMarkedForCookiePartitioning = false;
831             statistic.isMarkedForCookieBlocking = false;
832         }
833     }
834
835     for (auto& domain : domainsToPartition)
836         ensureResourceStatisticsForPrimaryDomain(domain).isMarkedForCookiePartitioning = true;
837
838     for (auto& domain : domainsToBlock)
839         ensureResourceStatisticsForPrimaryDomain(domain).isMarkedForCookieBlocking = true;
840 }
841
842 void WebResourceLoadStatisticsStore::clearPartitioningStateForDomains(const Vector<String>& domains)
843 {
844     ASSERT(!RunLoop::isMain());
845     if (domains.isEmpty())
846         return;
847
848     RunLoop::main().dispatch([this, protectedThis = makeRef(*this), domains = crossThreadCopy(domains)] () {
849         m_removeDomainsHandler(domains);
850     });
851
852     for (auto& domain : domains) {
853         auto& statistic = ensureResourceStatisticsForPrimaryDomain(domain);
854         statistic.isMarkedForCookiePartitioning = false;
855         statistic.isMarkedForCookieBlocking = false;
856     }
857 }
858
859 void WebResourceLoadStatisticsStore::resetCookiePartitioningState()
860 {
861     ASSERT(!RunLoop::isMain());
862     for (auto& resourceStatistic : m_resourceStatisticsMap.values()) {
863         resourceStatistic.isMarkedForCookiePartitioning = false;
864         resourceStatistic.isMarkedForCookieBlocking = false;
865     }
866 }
867
868 void WebResourceLoadStatisticsStore::processStatistics(const WTF::Function<void (const ResourceLoadStatistics&)>& processFunction) const
869 {
870     ASSERT(!RunLoop::isMain());
871     for (auto& resourceStatistic : m_resourceStatisticsMap.values())
872         processFunction(resourceStatistic);
873 }
874
875 bool WebResourceLoadStatisticsStore::hasHadUnexpiredRecentUserInteraction(ResourceLoadStatistics& resourceStatistic) const
876 {
877     if (resourceStatistic.hadUserInteraction && hasStatisticsExpired(resourceStatistic)) {
878         // Drop privacy sensitive data because we no longer need it.
879         // Set timestamp to 0 so that statistics merge will know
880         // it has been reset as opposed to its default -1.
881         resourceStatistic.mostRecentUserInteractionTime = { };
882         resourceStatistic.hadUserInteraction = false;
883     }
884
885     return resourceStatistic.hadUserInteraction;
886 }
887
888 Vector<String> WebResourceLoadStatisticsStore::topPrivatelyControlledDomainsToRemoveWebsiteDataFor()
889 {
890     ASSERT(!RunLoop::isMain());
891
892     bool shouldCheckForGrandfathering = m_endOfGrandfatheringTimestamp > WallTime::now();
893     bool shouldClearGrandfathering = !shouldCheckForGrandfathering && m_endOfGrandfatheringTimestamp;
894
895     if (shouldClearGrandfathering)
896         m_endOfGrandfatheringTimestamp = { };
897
898     Vector<String> prevalentResources;
899     for (auto& statistic : m_resourceStatisticsMap.values()) {
900         if (statistic.isPrevalentResource && !hasHadUnexpiredRecentUserInteraction(statistic) && (!shouldCheckForGrandfathering || !statistic.grandfathered))
901             prevalentResources.append(statistic.highLevelDomain);
902
903         if (shouldClearGrandfathering && statistic.grandfathered)
904             statistic.grandfathered = false;
905     }
906
907     return prevalentResources;
908 }
909
910 void WebResourceLoadStatisticsStore::includeTodayAsOperatingDateIfNecessary()
911 {
912     ASSERT(!RunLoop::isMain());
913
914     auto today = OperatingDate::today();
915     if (!m_operatingDates.isEmpty() && today <= m_operatingDates.last())
916         return;
917
918     while (m_operatingDates.size() >= operatingDatesWindow)
919         m_operatingDates.remove(0);
920
921     m_operatingDates.append(today);
922 }
923
924 bool WebResourceLoadStatisticsStore::hasStatisticsExpired(const ResourceLoadStatistics& resourceStatistic) const
925 {
926     if (m_operatingDates.size() >= operatingDatesWindow) {
927         if (OperatingDate::fromWallTime(resourceStatistic.mostRecentUserInteractionTime) < m_operatingDates.first())
928             return true;
929     }
930
931     // If we don't meet the real criteria for an expired statistic, check the user setting for a tighter restriction (mainly for testing).
932     if (m_parameters.timeToLiveUserInteraction) {
933         if (WallTime::now() > resourceStatistic.mostRecentUserInteractionTime + m_parameters.timeToLiveUserInteraction.value())
934             return true;
935     }
936
937     return false;
938 }
939     
940 void WebResourceLoadStatisticsStore::setMaxStatisticsEntries(size_t maximumEntryCount)
941 {
942     m_parameters.maxStatisticsEntries = maximumEntryCount;
943 }
944     
945 void WebResourceLoadStatisticsStore::setPruneEntriesDownTo(size_t pruneTargetCount)
946 {
947     m_parameters.pruneEntriesDownTo = pruneTargetCount;
948 }
949     
950 struct StatisticsLastSeen {
951     String topPrivatelyOwnedDomain;
952     WallTime lastSeen;
953 };
954     
955 static void pruneResources(HashMap<String, WebCore::ResourceLoadStatistics>& statisticsMap, Vector<StatisticsLastSeen>& statisticsToPrune, size_t& numberOfEntriesToPrune)
956 {
957     if (statisticsToPrune.size() > numberOfEntriesToPrune) {
958         std::sort(statisticsToPrune.begin(), statisticsToPrune.end(), [](const StatisticsLastSeen& a, const StatisticsLastSeen& b) {
959             return a.lastSeen < b.lastSeen;
960         });
961     }
962
963     for (size_t i = 0, end = std::min(numberOfEntriesToPrune, statisticsToPrune.size()); i != end; ++i, --numberOfEntriesToPrune)
964         statisticsMap.remove(statisticsToPrune[i].topPrivatelyOwnedDomain);
965 }
966     
967 static unsigned computeImportance(const ResourceLoadStatistics& resourceStatistic)
968 {
969     unsigned importance = maxImportance;
970     if (!resourceStatistic.isPrevalentResource)
971         importance -= 1;
972     if (!resourceStatistic.hadUserInteraction)
973         importance -= 2;
974     return importance;
975 }
976     
977 void WebResourceLoadStatisticsStore::pruneStatisticsIfNeeded()
978 {
979     ASSERT(!RunLoop::isMain());
980     if (m_resourceStatisticsMap.size() <= m_parameters.maxStatisticsEntries)
981         return;
982
983     ASSERT(m_parameters.pruneEntriesDownTo <= m_parameters.maxStatisticsEntries);
984
985     size_t numberOfEntriesLeftToPrune = m_resourceStatisticsMap.size() - m_parameters.pruneEntriesDownTo;
986     ASSERT(numberOfEntriesLeftToPrune);
987     
988     Vector<StatisticsLastSeen> resourcesToPrunePerImportance[maxImportance + 1];
989     for (auto& resourceStatistic : m_resourceStatisticsMap.values())
990         resourcesToPrunePerImportance[computeImportance(resourceStatistic)].append({ resourceStatistic.highLevelDomain, resourceStatistic.lastSeen });
991     
992     for (unsigned importance = 0; numberOfEntriesLeftToPrune && importance <= maxImportance; ++importance)
993         pruneResources(m_resourceStatisticsMap, resourcesToPrunePerImportance[importance], numberOfEntriesLeftToPrune);
994
995     ASSERT(!numberOfEntriesLeftToPrune);
996 }
997
998 void WebResourceLoadStatisticsStore::resetParametersToDefaultValues()
999 {
1000     m_parameters = { };
1001 }
1002
1003 void WebResourceLoadStatisticsStore::logTestingEvent(const String& event)
1004 {
1005     if (!m_statisticsTestingCallback)
1006         return;
1007
1008     if (RunLoop::isMain())
1009         m_statisticsTestingCallback(event);
1010     else {
1011         RunLoop::main().dispatch([this, protectedThis = makeRef(*this), event = event.isolatedCopy()] {
1012             if (m_statisticsTestingCallback)
1013                 m_statisticsTestingCallback(event);
1014         });
1015     }
1016 }
1017
1018 } // namespace WebKit