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..0fb38a3734b9b5e2e1241afedd37c382b1d5e8a6 |
| --- /dev/null |
| +++ b/net/reporting/reporting_cache.cc |
| @@ -0,0 +1,146 @@ |
| +// 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 <map> |
| +#include <memory> |
| +#include <set> |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "base/stl_util.h" |
| +#include "base/time/time.h" |
| +#include "net/reporting/reporting_client.h" |
| +#include "net/reporting/reporting_report.h" |
| +#include "url/gurl.h" |
| + |
| +namespace net { |
| + |
| +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<ReportingReport>( |
| + url, group, type, std::move(body), queued, attempts); |
| + |
| + DCHECK(!base::ContainsKey(reports_, report.get())); |
| + reports_[report.get()] = std::move(report); |
| +} |
| + |
| +void ReportingCache::GetReports( |
| + std::vector<const ReportingReport*>* reports_out) const { |
| + reports_out->clear(); |
| + for (const auto& it : reports_) { |
| + if (!base::ContainsKey(doomed_reports_, it.first)) |
| + reports_out->push_back(it.second.get()); |
| + } |
| +} |
| + |
| +void ReportingCache::SetReportsPending( |
| + const std::vector<const ReportingReport*>& reports) { |
| + for (const ReportingReport* report : reports) { |
| + DCHECK(!base::ContainsKey(pending_reports_, report)); |
|
jkarlin
2017/03/17 15:07:01
As it's unclear that this would be bad to the call
Julia Tuttle
2017/03/21 18:41:02
Done.
|
| + pending_reports_.insert(report); |
| + } |
| +} |
| + |
| +void ReportingCache::ClearReportsPending( |
| + const std::vector<const ReportingReport*>& reports) { |
| + std::vector<const ReportingReport*> reports_to_remove; |
| + |
| + for (const ReportingReport* report : reports) { |
| + DCHECK(base::ContainsKey(pending_reports_, report)); |
| + pending_reports_.erase(report); |
| + if (base::ContainsKey(doomed_reports_, report)) |
| + reports_to_remove.push_back(report); |
| + } |
| + |
| + RemoveReports(reports_to_remove); |
| +} |
| + |
| +void ReportingCache::IncrementReportsAttempts( |
| + const std::vector<const ReportingReport*>& reports) { |
| + for (const ReportingReport* report : reports) { |
| + DCHECK(base::ContainsKey(reports_, report)); |
| + reports_[report]->attempts++; |
| + } |
| +} |
| + |
| +void ReportingCache::RemoveReports( |
| + const std::vector<const ReportingReport*>& reports) { |
| + for (const ReportingReport* report : reports) { |
| + DCHECK(base::ContainsKey(reports_, report)); |
| + if (base::ContainsKey(pending_reports_, report)) |
| + doomed_reports_.insert(report); |
| + else |
| + reports_.erase(report); |
|
jkarlin
2017/03/17 15:07:01
Also remove from doomed_reports_? Seems like we sh
Julia Tuttle
2017/03/21 18:41:02
If it's not in pending_reports_, it can't be in do
|
| + } |
| +} |
| + |
| +void ReportingCache::GetClients( |
| + std::vector<const ReportingClient*>* clients_out) const { |
| + clients_out->clear(); |
| + for (const auto& it : clients_) |
| + for (const auto& jt : it.second) |
| + clients_out->push_back(jt.second.get()); |
| +} |
| + |
| +void ReportingCache::GetClientsForOriginAndGroup( |
| + const url::Origin& origin, |
| + const std::string& group, |
| + std::vector<const ReportingClient*>* clients_out) const { |
| + clients_out->clear(); |
| + |
| + const auto it = clients_.find(origin); |
| + if (it == clients_.end()) |
| + return; |
| + |
| + for (const auto& jt : it->second) { |
|
jkarlin
2017/03/17 15:07:01
prefer s/jt/endpoint_and_client/ then you can say
Julia Tuttle
2017/03/21 18:41:02
On 2017/03/17 15:07:01, jkarlin wrote:
> prefer s/
|
| + if (jt.second->group == group) |
| + clients_out->push_back(jt.second.get()); |
| + } |
| +} |
| + |
| +void ReportingCache::SetClient(const url::Origin& origin, |
| + const GURL& endpoint, |
| + ReportingClient::Subdomains subdomains, |
| + const std::string& group, |
| + base::TimeTicks expires) { |
| + if (!endpoint.SchemeIsCryptographic()) |
|
jkarlin
2017/03/17 15:07:01
You could LOG() this, or return an error that's re
Julia Tuttle
2017/03/21 18:41:02
I'm expecting the caller to be checking it already
|
| + return; |
| + |
| + clients_[origin][endpoint] = base::MakeUnique<ReportingClient>( |
| + origin, endpoint, subdomains, group, expires); |
| +} |
| + |
| +void ReportingCache::RemoveClients( |
| + const std::vector<const ReportingClient*>& clients_to_remove) { |
| + for (const ReportingClient* client : clients_to_remove) { |
| + DCHECK(base::ContainsKey(clients_[client->origin], client->endpoint)); |
| + DCHECK(clients_[client->origin][client->endpoint].get() == client); |
| + clients_[client->origin].erase(client->endpoint); |
| + } |
| +} |
| + |
| +void ReportingCache::RemoveClientForOriginAndEndpoint(const url::Origin& origin, |
| + const GURL& endpoint) { |
| + DCHECK(base::ContainsKey(clients_, origin)); |
| + DCHECK(base::ContainsKey(clients_[origin], endpoint)); |
| + clients_[origin].erase(endpoint); |
| +} |
| + |
| +void ReportingCache::RemoveClientsForEndpoint(const GURL& endpoint) { |
| + for (auto& it : clients_) |
| + it.second.erase(endpoint); |
| +} |
| + |
| +} // namespace net |