Chromium Code Reviews| Index: net/reporting/reporting_cache.cc |
| diff --git a/net/reporting/reporting_cache.cc b/net/reporting/reporting_cache.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e8073660a1dca0f2a067587ad920ef5fb81a5d3e |
| --- /dev/null |
| +++ b/net/reporting/reporting_cache.cc |
| @@ -0,0 +1,153 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/reporting/reporting_cache.h" |
| + |
| +#include "base/memory/ptr_util.h" |
| + |
| +namespace net { |
| + |
| +ReportingCache::Report::Report() : attempts(0), pending(false), doomed(false) {} |
|
jkarlin
2017/03/15 18:54:31
nit: I prefer to put default member values in the
Julia Tuttle
2017/03/16 14:50:37
Done.
|
| +ReportingCache::Report::~Report() {} |
| + |
| +ReportingCache::Client::Client() {} |
| +ReportingCache::Client::Client(const Client& other) |
| + : origin(other.origin), |
| + endpoint(other.endpoint), |
| + subdomains(other.subdomains), |
| + group(other.group), |
| + expires(other.expires) {} |
| +ReportingCache::Client::~Client() {} |
| + |
| +ReportingCache::ReportingCache() {} |
| +ReportingCache::~ReportingCache() {} |
| + |
| +void ReportingCache::AddReport(const GURL& url, |
| + const std::string& group, |
| + const std::string& type, |
| + std::unique_ptr<const base::Value> body, |
| + base::TimeTicks queued, |
| + int attempts) { |
| + auto report = base::MakeUnique<Report>(); |
| + |
| + report->url = url; |
| + report->group = group; |
| + report->type = type; |
| + report->body = std::move(body); |
| + report->queued = queued; |
| + report->attempts = attempts; |
| + |
| + DCHECK_EQ(0u, reports_.count(report.get())); |
| + reports_[report.get()] = std::move(report); |
| +} |
| + |
| +void ReportingCache::GetReports(std::vector<const Report*>* reports_out) const { |
| + reports_out->clear(); |
| + for (auto& it : reports_) { |
|
jkarlin
2017/03/15 18:54:31
const auto&
Julia Tuttle
2017/03/16 14:50:37
Done.
|
| + if (!it.second->doomed) |
| + reports_out->push_back(it.second.get()); |
| + } |
| +} |
| + |
| +void ReportingCache::SetReportsPending( |
| + const std::vector<const Report*>& reports) { |
| + for (const Report* report : reports) { |
| + DCHECK(!report->pending); |
| + reports_[report]->pending = true; |
| + } |
| +} |
| + |
| +void ReportingCache::ClearReportsPending( |
| + const std::vector<const Report*>& reports) { |
| + std::vector<const Report*> doomed_reports; |
| + |
| + for (const Report* report : reports) { |
| + DCHECK(report->pending); |
| + reports_[report]->pending = false; |
| + if (report->doomed) |
| + doomed_reports.push_back(report); |
| + } |
| + |
| + RemoveReports(doomed_reports); |
| +} |
| + |
| +void ReportingCache::IncrementReportsAttempts( |
| + const std::vector<const Report*>& reports) { |
| + for (const Report* report : reports) { |
| + DCHECK_EQ(1u, reports_.count(report)); |
|
jkarlin
2017/03/15 18:54:31
DCHECK(base::ContainsKey(reports, report))
Julia Tuttle
2017/03/16 14:50:37
Done.
|
| + reports_[report]->attempts++; |
| + } |
| +} |
| + |
| +void ReportingCache::RemoveReports(const std::vector<const Report*>& reports) { |
| + for (const Report* report : reports) { |
| + DCHECK_EQ(1u, reports_.count(report)); |
|
jkarlin
2017/03/15 18:54:31
DCHECK(base::ContainsKey(reports, report))
Julia Tuttle
2017/03/16 14:50:37
Done.
|
| + if (report->pending) |
| + reports_[report]->doomed = true; |
| + else |
| + reports_.erase(report); |
| + } |
| +} |
| + |
| +void ReportingCache::GetClients(std::vector<const Client*>* clients_out) const { |
| + clients_out->clear(); |
| + for (auto& it : clients_) |
|
jkarlin
2017/03/15 18:54:31
const auto&
Julia Tuttle
2017/03/16 14:50:37
Done.
|
| + for (auto& jt : it.second) |
|
jkarlin
2017/03/15 18:54:31
const auto&
Julia Tuttle
2017/03/16 14:50:37
Done.
|
| + clients_out->push_back(&jt.second); |
| +} |
| + |
| +void ReportingCache::GetClientsForOriginAndGroup( |
| + const url::Origin& origin, |
| + const std::string& group, |
| + std::vector<const Client*>* clients_out) const { |
| + clients_out->clear(); |
| + |
| + auto it = clients_.find(origin); |
| + if (it == clients_.end()) |
| + return; |
| + |
| + for (auto& jt : it->second) { |
|
jkarlin
2017/03/15 18:54:32
const auto&
Julia Tuttle
2017/03/16 14:50:37
Done.
|
| + if (jt.second.group == group) |
| + clients_out->push_back(&jt.second); |
| + } |
| +} |
| + |
| +void ReportingCache::SetClient(const url::Origin& origin, |
| + const GURL& endpoint, |
| + bool subdomains, |
| + const std::string& group, |
| + base::TimeTicks expires) { |
| + if (!endpoint.SchemeIsCryptographic()) { |
| + NOTREACHED(); |
|
jkarlin
2017/03/15 18:54:32
Best not to handle an exceptional case with a DCHE
Julia Tuttle
2017/03/16 14:50:37
Hrm.
My feeling here is that:
1. This shouldn't
|
| + return; |
| + } |
| + |
| + Client* client = &clients_[origin][endpoint]; |
| + client->origin = origin; |
| + client->endpoint = endpoint; |
| + client->subdomains = subdomains; |
| + client->group = group; |
| + client->expires = expires; |
| +} |
| + |
| +void ReportingCache::RemoveClients( |
| + const std::vector<const Client*>& clients_to_remove) { |
| + for (const Client* client : clients_to_remove) { |
| + DCHECK_EQ(1u, clients_[client->origin].count(client->endpoint)); |
|
jkarlin
2017/03/15 18:54:31
DCHECK(base::ContainsKey(clients_[client->origin],
Julia Tuttle
2017/03/16 14:50:36
Done.
|
| + DCHECK(&clients_[client->origin][client->endpoint] == client); |
| + clients_[client->origin].erase(client->endpoint); |
| + } |
| +} |
| + |
| +void ReportingCache::RemoveClientForOriginAndEndpoint(const url::Origin& origin, |
| + const GURL& endpoint) { |
|
jkarlin
2017/03/15 18:54:31
DCHECK that the origin exists in clients?
Julia Tuttle
2017/03/16 14:50:37
Done.
|
| + clients_[origin].erase(endpoint); |
| +} |
| + |
| +void ReportingCache::RemoveClientsForEndpoint(const GURL& endpoint) { |
| + for (auto& it : clients_) |
| + it.second.erase(endpoint); |
| +} |
| + |
| +} // namespace net |