Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(495)

Unified Diff: net/reporting/reporting_cache.cc

Issue 2847813002: Reporting: Cap number of reports in cache. (Closed)
Patch Set: Make requested change. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/reporting/reporting_cache.h ('k') | net/reporting/reporting_cache_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/reporting/reporting_cache.cc
diff --git a/net/reporting/reporting_cache.cc b/net/reporting/reporting_cache.cc
index b3b60101eb864652a7436de2338d1198786d2b7f..b8034c860c3c7acb16de9d1d20752e027e7a4895 100644
--- a/net/reporting/reporting_cache.cc
+++ b/net/reporting/reporting_cache.cc
@@ -61,6 +61,18 @@ void ReportingCache::AddReport(const GURL& url,
reports_.insert(std::make_pair(report.get(), std::move(report)));
DCHECK(inserted.second);
+ if (reports_.size() > context_->policy().max_report_count) {
+ // There should be at most one extra report (the one added above).
+ DCHECK_EQ(context_->policy().max_report_count + 1, reports_.size());
+ const ReportingReport* to_evict = FindReportToEvict();
+ DCHECK_NE(nullptr, to_evict);
+ // The newly-added report isn't pending, so even if all other reports are
+ // pending, the cache should have a report to evict.
+ DCHECK(!base::ContainsKey(pending_reports_, to_evict));
+ size_t erased = reports_.erase(to_evict);
+ DCHECK_EQ(1u, erased);
+ }
+
context_->NotifyCacheUpdated();
}
@@ -233,6 +245,21 @@ void ReportingCache::RemoveAllClients() {
context_->NotifyCacheUpdated();
}
+const ReportingReport* ReportingCache::FindReportToEvict() const {
+ const ReportingReport* earliest_queued = nullptr;
+
+ for (const auto& it : reports_) {
+ const ReportingReport* report = it.first;
+ if (base::ContainsKey(pending_reports_, report))
+ continue;
+ if (!earliest_queued || report->queued < earliest_queued->queued) {
+ earliest_queued = report;
+ }
+ }
+
+ return earliest_queued;
+}
+
void ReportingCache::MaybeAddWildcardClient(const ReportingClient* client) {
if (client->subdomains != ReportingClient::Subdomains::INCLUDE)
return;
« no previous file with comments | « net/reporting/reporting_cache.h ('k') | net/reporting/reporting_cache_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698