7f8dea2739ee23e62b6648dc443f8022e73609c0
[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::logUserInteraction(const URL& url)
369 {
370     if (url.isBlankURL() || url.isEmpty())
371         return;
372
373     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryDomain = isolatedPrimaryDomain(url)] {
374         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primaryDomain);
375         statistics.hadUserInteraction = true;
376         statistics.mostRecentUserInteractionTime = WallTime::now();
377
378         updateCookiePartitioningForDomains({ }, { }, { primaryDomain }, ShouldClearFirst::No);
379     });
380 }
381
382 void WebResourceLoadStatisticsStore::logNonRecentUserInteraction(const URL& url)
383 {
384     if (url.isBlankURL() || url.isEmpty())
385         return;
386     
387     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryDomain = isolatedPrimaryDomain(url)] {
388         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primaryDomain);
389         statistics.hadUserInteraction = true;
390         statistics.mostRecentUserInteractionTime = WallTime::now() - (m_parameters.timeToLiveCookiePartitionFree + Seconds::fromHours(1));
391
392         updateCookiePartitioningForDomains({ primaryDomain }, { }, { }, ShouldClearFirst::No);
393     });
394 }
395
396 void WebResourceLoadStatisticsStore::clearUserInteraction(const URL& url)
397 {
398     if (url.isBlankURL() || url.isEmpty())
399         return;
400
401     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryDomain = isolatedPrimaryDomain(url)] {
402         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primaryDomain);
403         statistics.hadUserInteraction = false;
404         statistics.mostRecentUserInteractionTime = { };
405     });
406 }
407
408 void WebResourceLoadStatisticsStore::hasHadUserInteraction(const URL& url, WTF::Function<void (bool)>&& completionHandler)
409 {
410     if (url.isBlankURL() || url.isEmpty()) {
411         completionHandler(false);
412         return;
413     }
414
415     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryDomain = isolatedPrimaryDomain(url), completionHandler = WTFMove(completionHandler)] () mutable {
416         auto mapEntry = m_resourceStatisticsMap.find(primaryDomain);
417         bool hadUserInteraction = mapEntry == m_resourceStatisticsMap.end() ? false: hasHadUnexpiredRecentUserInteraction(mapEntry->value);
418         RunLoop::main().dispatch([hadUserInteraction, completionHandler = WTFMove(completionHandler)] {
419             completionHandler(hadUserInteraction);
420         });
421     });
422 }
423
424 void WebResourceLoadStatisticsStore::setLastSeen(const URL& url, Seconds seconds)
425 {
426     if (url.isBlankURL() || url.isEmpty())
427         return;
428     
429     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryDomain = isolatedPrimaryDomain(url), seconds] {
430         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primaryDomain);
431         statistics.lastSeen = WallTime::fromRawSeconds(seconds.seconds());
432     });
433 }
434     
435 void WebResourceLoadStatisticsStore::setPrevalentResource(const URL& url)
436 {
437     if (url.isBlankURL() || url.isEmpty())
438         return;
439
440     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryDomain = isolatedPrimaryDomain(url)] {
441         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primaryDomain);
442         statistics.isPrevalentResource = true;
443     });
444 }
445
446 void WebResourceLoadStatisticsStore::isPrevalentResource(const URL& url, WTF::Function<void (bool)>&& completionHandler)
447 {
448     if (url.isBlankURL() || url.isEmpty()) {
449         completionHandler(false);
450         return;
451     }
452
453     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryDomain = isolatedPrimaryDomain(url), completionHandler = WTFMove(completionHandler)] () mutable {
454         auto mapEntry = m_resourceStatisticsMap.find(primaryDomain);
455         bool isPrevalentResource = mapEntry == m_resourceStatisticsMap.end() ? false : mapEntry->value.isPrevalentResource;
456         RunLoop::main().dispatch([isPrevalentResource, completionHandler = WTFMove(completionHandler)] {
457             completionHandler(isPrevalentResource);
458         });
459     });
460 }
461
462 void WebResourceLoadStatisticsStore::isRegisteredAsSubFrameUnder(const URL& subFrame, const URL& topFrame, WTF::Function<void (bool)>&& completionHandler)
463 {
464     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), subFramePrimaryDomain = isolatedPrimaryDomain(subFrame), topFramePrimaryDomain = isolatedPrimaryDomain(topFrame), completionHandler = WTFMove(completionHandler)] () mutable {
465         auto mapEntry = m_resourceStatisticsMap.find(subFramePrimaryDomain);
466         bool isRegisteredAsSubFrameUnder = mapEntry == m_resourceStatisticsMap.end() ? false : mapEntry->value.subframeUnderTopFrameOrigins.contains(topFramePrimaryDomain);
467         RunLoop::main().dispatch([isRegisteredAsSubFrameUnder, completionHandler = WTFMove(completionHandler)] {
468             completionHandler(isRegisteredAsSubFrameUnder);
469         });
470     });
471 }
472
473 void WebResourceLoadStatisticsStore::isRegisteredAsRedirectingTo(const URL& hostRedirectedFrom, const URL& hostRedirectedTo, WTF::Function<void (bool)>&& completionHandler)
474 {
475     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), hostRedirectedFromPrimaryDomain = isolatedPrimaryDomain(hostRedirectedFrom), hostRedirectedToPrimaryDomain = isolatedPrimaryDomain(hostRedirectedTo), completionHandler = WTFMove(completionHandler)] () mutable {
476         auto mapEntry = m_resourceStatisticsMap.find(hostRedirectedFromPrimaryDomain);
477         bool isRegisteredAsRedirectingTo = mapEntry == m_resourceStatisticsMap.end() ? false : mapEntry->value.subresourceUniqueRedirectsTo.contains(hostRedirectedToPrimaryDomain);
478         RunLoop::main().dispatch([isRegisteredAsRedirectingTo, completionHandler = WTFMove(completionHandler)] {
479             completionHandler(isRegisteredAsRedirectingTo);
480         });
481     });
482 }
483
484 void WebResourceLoadStatisticsStore::clearPrevalentResource(const URL& url)
485 {
486     if (url.isBlankURL() || url.isEmpty())
487         return;
488
489     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryDomain = isolatedPrimaryDomain(url)] {
490         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primaryDomain);
491         statistics.isPrevalentResource = false;
492     });
493 }
494
495 void WebResourceLoadStatisticsStore::setGrandfathered(const URL& url, bool value)
496 {
497     if (url.isBlankURL() || url.isEmpty())
498         return;
499
500     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryDomain = isolatedPrimaryDomain(url), value] {
501         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primaryDomain);
502         statistics.grandfathered = value;
503     });
504 }
505
506 void WebResourceLoadStatisticsStore::isGrandfathered(const URL& url, WTF::Function<void (bool)>&& completionHandler)
507 {
508     if (url.isBlankURL() || url.isEmpty()) {
509         completionHandler(false);
510         return;
511     }
512
513     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), completionHandler = WTFMove(completionHandler), primaryDomain = isolatedPrimaryDomain(url)] () mutable {
514         auto mapEntry = m_resourceStatisticsMap.find(primaryDomain);
515         bool isGrandFathered = mapEntry == m_resourceStatisticsMap.end() ? false : mapEntry->value.grandfathered;
516         RunLoop::main().dispatch([isGrandFathered, completionHandler = WTFMove(completionHandler)] {
517             completionHandler(isGrandFathered);
518         });
519     });
520 }
521
522 void WebResourceLoadStatisticsStore::setSubframeUnderTopFrameOrigin(const URL& subframe, const URL& topFrame)
523 {
524     if (subframe.isBlankURL() || subframe.isEmpty() || topFrame.isBlankURL() || topFrame.isEmpty())
525         return;
526
527     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryTopFrameDomain = isolatedPrimaryDomain(topFrame), primarySubFrameDomain = isolatedPrimaryDomain(subframe)] {
528         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primarySubFrameDomain);
529         statistics.subframeUnderTopFrameOrigins.add(primaryTopFrameDomain);
530     });
531 }
532
533 void WebResourceLoadStatisticsStore::setSubresourceUnderTopFrameOrigin(const URL& subresource, const URL& topFrame)
534 {
535     if (subresource.isBlankURL() || subresource.isEmpty() || topFrame.isBlankURL() || topFrame.isEmpty())
536         return;
537
538     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryTopFrameDomain = isolatedPrimaryDomain(topFrame), primarySubresourceDomain = isolatedPrimaryDomain(subresource)] {
539         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primarySubresourceDomain);
540         statistics.subresourceUnderTopFrameOrigins.add(primaryTopFrameDomain);
541     });
542 }
543
544 void WebResourceLoadStatisticsStore::setSubresourceUniqueRedirectTo(const URL& subresource, const URL& hostNameRedirectedTo)
545 {
546     if (subresource.isBlankURL() || subresource.isEmpty() || hostNameRedirectedTo.isBlankURL() || hostNameRedirectedTo.isEmpty())
547         return;
548
549     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), primaryRedirectDomain = isolatedPrimaryDomain(hostNameRedirectedTo), primarySubresourceDomain = isolatedPrimaryDomain(subresource)] {
550         auto& statistics = ensureResourceStatisticsForPrimaryDomain(primarySubresourceDomain);
551         statistics.subresourceUniqueRedirectsTo.add(primaryRedirectDomain);
552     });
553 }
554
555 void WebResourceLoadStatisticsStore::scheduleCookiePartitioningUpdate()
556 {
557     // Helper function used by testing system. Should only be called from the main thread.
558     ASSERT(RunLoop::isMain());
559
560     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this)] {
561         updateCookiePartitioning();
562     });
563 }
564
565 void WebResourceLoadStatisticsStore::scheduleCookiePartitioningUpdateForDomains(const Vector<String>& domainsToPartition, const Vector<String>& domainsToBlock, const Vector<String>& domainsToNeitherPartitionNorBlock, ShouldClearFirst shouldClearFirst)
566 {
567     // Helper function used by testing system. Should only be called from the main thread.
568     ASSERT(RunLoop::isMain());
569     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), domainsToPartition = crossThreadCopy(domainsToPartition), domainsToBlock = crossThreadCopy(domainsToBlock), domainsToNeitherPartitionNorBlock = crossThreadCopy(domainsToNeitherPartitionNorBlock), shouldClearFirst] {
570         updateCookiePartitioningForDomains(domainsToPartition, domainsToBlock, domainsToNeitherPartitionNorBlock, shouldClearFirst);
571     });
572 }
573
574 void WebResourceLoadStatisticsStore::scheduleClearPartitioningStateForDomains(const Vector<String>& domains)
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), domains = crossThreadCopy(domains)] {
579         clearPartitioningStateForDomains(domains);
580     });
581 }
582
583 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
584 void WebResourceLoadStatisticsStore::scheduleCookiePartitioningStateReset()
585 {
586     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this)] {
587         resetCookiePartitioningState();
588     });
589 }
590 #endif
591
592 void WebResourceLoadStatisticsStore::scheduleClearInMemory()
593 {
594     ASSERT(RunLoop::isMain());
595     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this)] {
596         clearInMemory();
597     });
598 }
599
600 void WebResourceLoadStatisticsStore::scheduleClearInMemoryAndPersistent(ShouldGrandfather shouldGrandfather, CompletionHandler<void()>&& callback)
601 {
602     ASSERT(RunLoop::isMain());
603     m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this), shouldGrandfather, callback = WTFMove(callback)] () mutable {
604         clearInMemory();
605         m_persistentStorage.clear();
606         
607         if (shouldGrandfather == ShouldGrandfather::Yes)
608             grandfatherExistingWebsiteData([protectedThis = makeRef(*this), callback = WTFMove(callback)]() {
609                 callback();
610             });
611         else {
612             callback();
613         }
614     });
615 }
616
617 void WebResourceLoadStatisticsStore::scheduleClearInMemoryAndPersistent(WallTime modifiedSince, ShouldGrandfather shouldGrandfather, CompletionHandler<void()>&& callback)
618 {
619     // For now, be conservative and clear everything regardless of modifiedSince.
620     UNUSED_PARAM(modifiedSince);
621     scheduleClearInMemoryAndPersistent(shouldGrandfather, WTFMove(callback));
622 }
623
624 void WebResourceLoadStatisticsStore::setTimeToLiveUserInteraction(Seconds seconds)
625 {
626     ASSERT(seconds >= 0_s);
627     m_parameters.timeToLiveUserInteraction = seconds;
628 }
629
630 void WebResourceLoadStatisticsStore::setTimeToLiveCookiePartitionFree(Seconds seconds)
631 {
632     ASSERT(seconds >= 0_s);
633     m_parameters.timeToLiveCookiePartitionFree = seconds;
634 }
635
636 void WebResourceLoadStatisticsStore::setMinimumTimeBetweenDataRecordsRemoval(Seconds seconds)
637 {
638     ASSERT(seconds >= 0_s);
639     m_parameters.minimumTimeBetweenDataRecordsRemoval = seconds;
640 }
641
642 void WebResourceLoadStatisticsStore::setGrandfatheringTime(Seconds seconds)
643 {
644     ASSERT(seconds >= 0_s);
645     m_parameters.grandfatheringTime = seconds;
646 }
647
648 bool WebResourceLoadStatisticsStore::shouldRemoveDataRecords() const
649 {
650     ASSERT(!RunLoop::isMain());
651     if (m_dataRecordsBeingRemoved)
652         return false;
653
654 #if ENABLE(NETSCAPE_PLUGIN_API)
655     for (auto plugin : PluginProcessManager::singleton().pluginProcesses()) {
656         if (!m_activePluginTokens.contains(plugin->pluginProcessToken()))
657             return true;
658     }
659 #endif
660
661     return !m_lastTimeDataRecordsWereRemoved || MonotonicTime::now() >= (m_lastTimeDataRecordsWereRemoved + m_parameters.minimumTimeBetweenDataRecordsRemoval);
662 }
663
664 void WebResourceLoadStatisticsStore::setDataRecordsBeingRemoved(bool value)
665 {
666     ASSERT(!RunLoop::isMain());
667     m_dataRecordsBeingRemoved = value;
668     if (m_dataRecordsBeingRemoved)
669         m_lastTimeDataRecordsWereRemoved = MonotonicTime::now();
670 }
671
672 ResourceLoadStatistics& WebResourceLoadStatisticsStore::ensureResourceStatisticsForPrimaryDomain(const String& primaryDomain)
673 {
674     ASSERT(!RunLoop::isMain());
675     return m_resourceStatisticsMap.ensure(primaryDomain, [&primaryDomain] {
676         return ResourceLoadStatistics(primaryDomain);
677     }).iterator->value;
678 }
679
680 std::unique_ptr<KeyedEncoder> WebResourceLoadStatisticsStore::createEncoderFromData() const
681 {
682     ASSERT(!RunLoop::isMain());
683     auto encoder = KeyedEncoder::encoder();
684     encoder->encodeUInt32("version", statisticsModelVersion);
685     encoder->encodeDouble("endOfGrandfatheringTimestamp", m_endOfGrandfatheringTimestamp.secondsSinceEpoch().value());
686
687     encoder->encodeObjects("browsingStatistics", m_resourceStatisticsMap.begin(), m_resourceStatisticsMap.end(), [](KeyedEncoder& encoderInner, const auto& origin) {
688         origin.value.encode(encoderInner);
689     });
690
691     encoder->encodeObjects("operatingDates", m_operatingDates.begin(), m_operatingDates.end(), [](KeyedEncoder& encoderInner, OperatingDate date) {
692         encoderInner.encodeDouble("date", date.secondsSinceEpoch().value());
693     });
694
695     return encoder;
696 }
697
698 void WebResourceLoadStatisticsStore::mergeWithDataFromDecoder(KeyedDecoder& decoder)
699 {
700     ASSERT(!RunLoop::isMain());
701
702     unsigned versionOnDisk;
703     if (!decoder.decodeUInt32("version", versionOnDisk))
704         return;
705
706     if (versionOnDisk != statisticsModelVersion)
707         return;
708
709     double endOfGrandfatheringTimestamp;
710     if (decoder.decodeDouble("endOfGrandfatheringTimestamp", endOfGrandfatheringTimestamp))
711         m_endOfGrandfatheringTimestamp = WallTime::fromRawSeconds(endOfGrandfatheringTimestamp);
712     else
713         m_endOfGrandfatheringTimestamp = { };
714
715     Vector<ResourceLoadStatistics> loadedStatistics;
716     bool succeeded = decoder.decodeObjects("browsingStatistics", loadedStatistics, [](KeyedDecoder& decoderInner, ResourceLoadStatistics& statistics) {
717         return statistics.decode(decoderInner);
718     });
719
720     if (!succeeded)
721         return;
722
723     mergeStatistics(WTFMove(loadedStatistics));
724     updateCookiePartitioning();
725
726     Vector<OperatingDate> operatingDates;
727     succeeded = decoder.decodeObjects("operatingDates", operatingDates, [](KeyedDecoder& decoder, OperatingDate& date) {
728         double value;
729         if (!decoder.decodeDouble("date", value))
730             return false;
731
732         date = OperatingDate::fromWallTime(WallTime::fromRawSeconds(value));
733         return true;
734     });
735
736     if (!succeeded)
737         return;
738
739     m_operatingDates = mergeOperatingDates(m_operatingDates, WTFMove(operatingDates));
740 }
741
742 void WebResourceLoadStatisticsStore::clearInMemory()
743 {
744     ASSERT(!RunLoop::isMain());
745     m_resourceStatisticsMap.clear();
746     m_operatingDates.clear();
747
748     updateCookiePartitioningForDomains({ }, { }, { }, ShouldClearFirst::Yes);
749 }
750
751 void WebResourceLoadStatisticsStore::mergeStatistics(Vector<ResourceLoadStatistics>&& statistics)
752 {
753     ASSERT(!RunLoop::isMain());
754     for (auto& statistic : statistics) {
755         auto result = m_resourceStatisticsMap.ensure(statistic.highLevelDomain, [&statistic] {
756             return WTFMove(statistic);
757         });
758         if (!result.isNewEntry)
759             result.iterator->value.merge(statistic);
760     }
761 }
762
763 bool WebResourceLoadStatisticsStore::shouldPartitionCookies(const ResourceLoadStatistics& statistic) const
764 {
765     return statistic.isPrevalentResource && statistic.hadUserInteraction && WallTime::now() > statistic.mostRecentUserInteractionTime + m_parameters.timeToLiveCookiePartitionFree;
766 }
767
768 bool WebResourceLoadStatisticsStore::shouldBlockCookies(const ResourceLoadStatistics& statistic) const
769 {
770     return statistic.isPrevalentResource && !statistic.hadUserInteraction;
771 }
772
773 void WebResourceLoadStatisticsStore::updateCookiePartitioning()
774 {
775     ASSERT(!RunLoop::isMain());
776
777     Vector<String> domainsToPartition;
778     Vector<String> domainsToBlock;
779     Vector<String> domainsToNeitherPartitionNorBlock;
780     for (auto& resourceStatistic : m_resourceStatisticsMap.values()) {
781
782         bool shouldPartition = shouldPartitionCookies(resourceStatistic);
783         bool shouldBlock = shouldBlockCookies(resourceStatistic);
784
785         if (shouldPartition && !resourceStatistic.isMarkedForCookiePartitioning) {
786             domainsToPartition.append(resourceStatistic.highLevelDomain);
787             resourceStatistic.isMarkedForCookiePartitioning = true;
788         } else if (shouldBlock && !resourceStatistic.isMarkedForCookieBlocking) {
789             domainsToBlock.append(resourceStatistic.highLevelDomain);
790             resourceStatistic.isMarkedForCookieBlocking = true;
791         } else if (!shouldPartition && !shouldBlock && resourceStatistic.isPrevalentResource) {
792             domainsToNeitherPartitionNorBlock.append(resourceStatistic.highLevelDomain);
793             resourceStatistic.isMarkedForCookiePartitioning = false;
794             resourceStatistic.isMarkedForCookieBlocking = false;
795         }
796     }
797
798     if (domainsToPartition.isEmpty() && domainsToBlock.isEmpty() && domainsToNeitherPartitionNorBlock.isEmpty())
799         return;
800
801     RunLoop::main().dispatch([this, protectedThis = makeRef(*this), domainsToPartition = crossThreadCopy(domainsToPartition), domainsToBlock = crossThreadCopy(domainsToBlock), domainsToNeitherPartitionNorBlock = crossThreadCopy(domainsToNeitherPartitionNorBlock)] () {
802         m_updatePrevalentDomainsToPartitionOrBlockCookiesHandler(domainsToPartition, domainsToBlock, domainsToNeitherPartitionNorBlock, ShouldClearFirst::No);
803     });
804 }
805
806 void WebResourceLoadStatisticsStore::updateCookiePartitioningForDomains(const Vector<String>& domainsToPartition, const Vector<String>& domainsToBlock, const Vector<String>& domainsToNeitherPartitionNorBlock, ShouldClearFirst shouldClearFirst)
807 {
808     ASSERT(!RunLoop::isMain());
809     if (domainsToPartition.isEmpty() && domainsToBlock.isEmpty() && domainsToNeitherPartitionNorBlock.isEmpty() && shouldClearFirst == ShouldClearFirst::No)
810         return;
811
812     RunLoop::main().dispatch([this, shouldClearFirst, protectedThis = makeRef(*this), domainsToPartition = crossThreadCopy(domainsToPartition), domainsToBlock = crossThreadCopy(domainsToBlock), domainsToNeitherPartitionNorBlock = crossThreadCopy(domainsToNeitherPartitionNorBlock)] () {
813         m_updatePrevalentDomainsToPartitionOrBlockCookiesHandler(domainsToPartition, domainsToBlock, domainsToNeitherPartitionNorBlock, shouldClearFirst);
814     });
815
816     if (shouldClearFirst == ShouldClearFirst::Yes)
817         resetCookiePartitioningState();
818     else {
819         for (auto& domain : domainsToNeitherPartitionNorBlock) {
820             auto& statistic = ensureResourceStatisticsForPrimaryDomain(domain);
821             statistic.isMarkedForCookiePartitioning = false;
822             statistic.isMarkedForCookieBlocking = false;
823         }
824     }
825
826     for (auto& domain : domainsToPartition)
827         ensureResourceStatisticsForPrimaryDomain(domain).isMarkedForCookiePartitioning = true;
828
829     for (auto& domain : domainsToBlock)
830         ensureResourceStatisticsForPrimaryDomain(domain).isMarkedForCookieBlocking = true;
831 }
832
833 void WebResourceLoadStatisticsStore::clearPartitioningStateForDomains(const Vector<String>& domains)
834 {
835     ASSERT(!RunLoop::isMain());
836     if (domains.isEmpty())
837         return;
838
839     RunLoop::main().dispatch([this, protectedThis = makeRef(*this), domains = crossThreadCopy(domains)] () {
840         m_removeDomainsHandler(domains);
841     });
842
843     for (auto& domain : domains) {
844         auto& statistic = ensureResourceStatisticsForPrimaryDomain(domain);
845         statistic.isMarkedForCookiePartitioning = false;
846         statistic.isMarkedForCookieBlocking = false;
847     }
848 }
849
850 void WebResourceLoadStatisticsStore::resetCookiePartitioningState()
851 {
852     ASSERT(!RunLoop::isMain());
853     for (auto& resourceStatistic : m_resourceStatisticsMap.values()) {
854         resourceStatistic.isMarkedForCookiePartitioning = false;
855         resourceStatistic.isMarkedForCookieBlocking = false;
856     }
857 }
858
859 void WebResourceLoadStatisticsStore::processStatistics(const WTF::Function<void (const ResourceLoadStatistics&)>& processFunction) const
860 {
861     ASSERT(!RunLoop::isMain());
862     for (auto& resourceStatistic : m_resourceStatisticsMap.values())
863         processFunction(resourceStatistic);
864 }
865
866 bool WebResourceLoadStatisticsStore::hasHadUnexpiredRecentUserInteraction(ResourceLoadStatistics& resourceStatistic) const
867 {
868     if (resourceStatistic.hadUserInteraction && hasStatisticsExpired(resourceStatistic)) {
869         // Drop privacy sensitive data because we no longer need it.
870         // Set timestamp to 0 so that statistics merge will know
871         // it has been reset as opposed to its default -1.
872         resourceStatistic.mostRecentUserInteractionTime = { };
873         resourceStatistic.hadUserInteraction = false;
874     }
875
876     return resourceStatistic.hadUserInteraction;
877 }
878
879 Vector<String> WebResourceLoadStatisticsStore::topPrivatelyControlledDomainsToRemoveWebsiteDataFor()
880 {
881     ASSERT(!RunLoop::isMain());
882
883     bool shouldCheckForGrandfathering = m_endOfGrandfatheringTimestamp > WallTime::now();
884     bool shouldClearGrandfathering = !shouldCheckForGrandfathering && m_endOfGrandfatheringTimestamp;
885
886     if (shouldClearGrandfathering)
887         m_endOfGrandfatheringTimestamp = { };
888
889     Vector<String> prevalentResources;
890     for (auto& statistic : m_resourceStatisticsMap.values()) {
891         if (statistic.isPrevalentResource && !hasHadUnexpiredRecentUserInteraction(statistic) && (!shouldCheckForGrandfathering || !statistic.grandfathered))
892             prevalentResources.append(statistic.highLevelDomain);
893
894         if (shouldClearGrandfathering && statistic.grandfathered)
895             statistic.grandfathered = false;
896     }
897
898     return prevalentResources;
899 }
900
901 void WebResourceLoadStatisticsStore::includeTodayAsOperatingDateIfNecessary()
902 {
903     ASSERT(!RunLoop::isMain());
904
905     auto today = OperatingDate::today();
906     if (!m_operatingDates.isEmpty() && today <= m_operatingDates.last())
907         return;
908
909     while (m_operatingDates.size() >= operatingDatesWindow)
910         m_operatingDates.remove(0);
911
912     m_operatingDates.append(today);
913 }
914
915 bool WebResourceLoadStatisticsStore::hasStatisticsExpired(const ResourceLoadStatistics& resourceStatistic) const
916 {
917     if (m_operatingDates.size() >= operatingDatesWindow) {
918         if (OperatingDate::fromWallTime(resourceStatistic.mostRecentUserInteractionTime) < m_operatingDates.first())
919             return true;
920     }
921
922     // If we don't meet the real criteria for an expired statistic, check the user setting for a tighter restriction (mainly for testing).
923     if (m_parameters.timeToLiveUserInteraction) {
924         if (WallTime::now() > resourceStatistic.mostRecentUserInteractionTime + m_parameters.timeToLiveUserInteraction.value())
925             return true;
926     }
927
928     return false;
929 }
930     
931 void WebResourceLoadStatisticsStore::setMaxStatisticsEntries(size_t maximumEntryCount)
932 {
933     m_parameters.maxStatisticsEntries = maximumEntryCount;
934 }
935     
936 void WebResourceLoadStatisticsStore::setPruneEntriesDownTo(size_t pruneTargetCount)
937 {
938     m_parameters.pruneEntriesDownTo = pruneTargetCount;
939 }
940     
941 struct StatisticsLastSeen {
942     String topPrivatelyOwnedDomain;
943     WallTime lastSeen;
944 };
945     
946 static void pruneResources(HashMap<String, WebCore::ResourceLoadStatistics>& statisticsMap, Vector<StatisticsLastSeen>& statisticsToPrune, size_t& numberOfEntriesToPrune)
947 {
948     if (statisticsToPrune.size() > numberOfEntriesToPrune) {
949         std::sort(statisticsToPrune.begin(), statisticsToPrune.end(), [](const StatisticsLastSeen& a, const StatisticsLastSeen& b) {
950             return a.lastSeen < b.lastSeen;
951         });
952     }
953
954     for (size_t i = 0, end = std::min(numberOfEntriesToPrune, statisticsToPrune.size()); i != end; ++i, --numberOfEntriesToPrune)
955         statisticsMap.remove(statisticsToPrune[i].topPrivatelyOwnedDomain);
956 }
957     
958 static unsigned computeImportance(const ResourceLoadStatistics& resourceStatistic)
959 {
960     unsigned importance = maxImportance;
961     if (!resourceStatistic.isPrevalentResource)
962         importance -= 1;
963     if (!resourceStatistic.hadUserInteraction)
964         importance -= 2;
965     return importance;
966 }
967     
968 void WebResourceLoadStatisticsStore::pruneStatisticsIfNeeded()
969 {
970     ASSERT(!RunLoop::isMain());
971     if (m_resourceStatisticsMap.size() <= m_parameters.maxStatisticsEntries)
972         return;
973
974     ASSERT(m_parameters.pruneEntriesDownTo <= m_parameters.maxStatisticsEntries);
975
976     size_t numberOfEntriesLeftToPrune = m_resourceStatisticsMap.size() - m_parameters.pruneEntriesDownTo;
977     ASSERT(numberOfEntriesLeftToPrune);
978     
979     Vector<StatisticsLastSeen> resourcesToPrunePerImportance[maxImportance + 1];
980     for (auto& resourceStatistic : m_resourceStatisticsMap.values())
981         resourcesToPrunePerImportance[computeImportance(resourceStatistic)].append({ resourceStatistic.highLevelDomain, resourceStatistic.lastSeen });
982     
983     for (unsigned importance = 0; numberOfEntriesLeftToPrune && importance <= maxImportance; ++importance)
984         pruneResources(m_resourceStatisticsMap, resourcesToPrunePerImportance[importance], numberOfEntriesLeftToPrune);
985
986     ASSERT(!numberOfEntriesLeftToPrune);
987 }
988
989 void WebResourceLoadStatisticsStore::resetParametersToDefaultValues()
990 {
991     m_parameters = { };
992 }
993
994 void WebResourceLoadStatisticsStore::logTestingEvent(const String& event)
995 {
996     if (!m_statisticsTestingCallback)
997         return;
998
999     if (RunLoop::isMain())
1000         m_statisticsTestingCallback(event);
1001     else {
1002         RunLoop::main().dispatch([this, protectedThis = makeRef(*this), event = event.isolatedCopy()] {
1003             if (m_statisticsTestingCallback)
1004                 m_statisticsTestingCallback(event);
1005         });
1006     }
1007 }
1008
1009 } // namespace WebKit