OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/reporting/reporting_cache.h" | 5 #include "net/reporting/reporting_cache.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <memory> | 8 #include <memory> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 std::unique_ptr<const base::Value> body, | 54 std::unique_ptr<const base::Value> body, |
55 base::TimeTicks queued, | 55 base::TimeTicks queued, |
56 int attempts) { | 56 int attempts) { |
57 auto report = base::MakeUnique<ReportingReport>( | 57 auto report = base::MakeUnique<ReportingReport>( |
58 url, group, type, std::move(body), queued, attempts); | 58 url, group, type, std::move(body), queued, attempts); |
59 | 59 |
60 auto inserted = | 60 auto inserted = |
61 reports_.insert(std::make_pair(report.get(), std::move(report))); | 61 reports_.insert(std::make_pair(report.get(), std::move(report))); |
62 DCHECK(inserted.second); | 62 DCHECK(inserted.second); |
63 | 63 |
| 64 if (reports_.size() > context_->policy().max_report_count) { |
| 65 // There should be at most one extra report (the one added above). |
| 66 DCHECK_EQ(context_->policy().max_report_count + 1, reports_.size()); |
| 67 const ReportingReport* to_evict = FindReportToEvict(); |
| 68 DCHECK_NE(nullptr, to_evict); |
| 69 // The newly-added report isn't pending, so even if all other reports are |
| 70 // pending, the cache should have a report to evict. |
| 71 DCHECK(!base::ContainsKey(pending_reports_, to_evict)); |
| 72 size_t erased = reports_.erase(to_evict); |
| 73 DCHECK_EQ(1u, erased); |
| 74 } |
| 75 |
64 context_->NotifyCacheUpdated(); | 76 context_->NotifyCacheUpdated(); |
65 } | 77 } |
66 | 78 |
67 void ReportingCache::GetReports( | 79 void ReportingCache::GetReports( |
68 std::vector<const ReportingReport*>* reports_out) const { | 80 std::vector<const ReportingReport*>* reports_out) const { |
69 reports_out->clear(); | 81 reports_out->clear(); |
70 for (const auto& it : reports_) { | 82 for (const auto& it : reports_) { |
71 if (!base::ContainsKey(doomed_reports_, it.first)) | 83 if (!base::ContainsKey(doomed_reports_, it.first)) |
72 reports_out->push_back(it.second.get()); | 84 reports_out->push_back(it.second.get()); |
73 } | 85 } |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 context_->NotifyCacheUpdated(); | 238 context_->NotifyCacheUpdated(); |
227 } | 239 } |
228 | 240 |
229 void ReportingCache::RemoveAllClients() { | 241 void ReportingCache::RemoveAllClients() { |
230 clients_.clear(); | 242 clients_.clear(); |
231 wildcard_clients_.clear(); | 243 wildcard_clients_.clear(); |
232 | 244 |
233 context_->NotifyCacheUpdated(); | 245 context_->NotifyCacheUpdated(); |
234 } | 246 } |
235 | 247 |
| 248 const ReportingReport* ReportingCache::FindReportToEvict() const { |
| 249 const ReportingReport* earliest_queued = nullptr; |
| 250 |
| 251 for (const auto& it : reports_) { |
| 252 const ReportingReport* report = it.first; |
| 253 if (base::ContainsKey(pending_reports_, report)) |
| 254 continue; |
| 255 if (!earliest_queued || report->queued < earliest_queued->queued) { |
| 256 earliest_queued = report; |
| 257 } |
| 258 } |
| 259 |
| 260 return earliest_queued; |
| 261 } |
| 262 |
236 void ReportingCache::MaybeAddWildcardClient(const ReportingClient* client) { | 263 void ReportingCache::MaybeAddWildcardClient(const ReportingClient* client) { |
237 if (client->subdomains != ReportingClient::Subdomains::INCLUDE) | 264 if (client->subdomains != ReportingClient::Subdomains::INCLUDE) |
238 return; | 265 return; |
239 | 266 |
240 const std::string& domain = client->origin.host(); | 267 const std::string& domain = client->origin.host(); |
241 auto inserted = wildcard_clients_[domain].insert(client); | 268 auto inserted = wildcard_clients_[domain].insert(client); |
242 DCHECK(inserted.second); | 269 DCHECK(inserted.second); |
243 } | 270 } |
244 | 271 |
245 void ReportingCache::MaybeRemoveWildcardClient(const ReportingClient* client) { | 272 void ReportingCache::MaybeRemoveWildcardClient(const ReportingClient* client) { |
(...skipping 16 matching lines...) Expand all Loading... |
262 return; | 289 return; |
263 | 290 |
264 for (const ReportingClient* client : it->second) { | 291 for (const ReportingClient* client : it->second) { |
265 DCHECK_EQ(ReportingClient::Subdomains::INCLUDE, client->subdomains); | 292 DCHECK_EQ(ReportingClient::Subdomains::INCLUDE, client->subdomains); |
266 if (client->group == group) | 293 if (client->group == group) |
267 clients_out->push_back(client); | 294 clients_out->push_back(client); |
268 } | 295 } |
269 } | 296 } |
270 | 297 |
271 } // namespace net | 298 } // namespace net |
OLD | NEW |