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

Side by Side Diff: net/reporting/reporting_cache.cc

Issue 2708503002: Reporting: Implement cache. (Closed)
Patch Set: rebase Created 3 years, 9 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "net/reporting/reporting_cache.h"
6
7 #include "base/memory/ptr_util.h"
8
9 namespace net {
10
11 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.
12 ReportingCache::Report::~Report() {}
13
14 ReportingCache::Client::Client() {}
15 ReportingCache::Client::Client(const Client& other)
16 : origin(other.origin),
17 endpoint(other.endpoint),
18 subdomains(other.subdomains),
19 group(other.group),
20 expires(other.expires) {}
21 ReportingCache::Client::~Client() {}
22
23 ReportingCache::ReportingCache() {}
24 ReportingCache::~ReportingCache() {}
25
26 void ReportingCache::AddReport(const GURL& url,
27 const std::string& group,
28 const std::string& type,
29 std::unique_ptr<const base::Value> body,
30 base::TimeTicks queued,
31 int attempts) {
32 auto report = base::MakeUnique<Report>();
33
34 report->url = url;
35 report->group = group;
36 report->type = type;
37 report->body = std::move(body);
38 report->queued = queued;
39 report->attempts = attempts;
40
41 DCHECK_EQ(0u, reports_.count(report.get()));
42 reports_[report.get()] = std::move(report);
43 }
44
45 void ReportingCache::GetReports(std::vector<const Report*>* reports_out) const {
46 reports_out->clear();
47 for (auto& it : reports_) {
jkarlin 2017/03/15 18:54:31 const auto&
Julia Tuttle 2017/03/16 14:50:37 Done.
48 if (!it.second->doomed)
49 reports_out->push_back(it.second.get());
50 }
51 }
52
53 void ReportingCache::SetReportsPending(
54 const std::vector<const Report*>& reports) {
55 for (const Report* report : reports) {
56 DCHECK(!report->pending);
57 reports_[report]->pending = true;
58 }
59 }
60
61 void ReportingCache::ClearReportsPending(
62 const std::vector<const Report*>& reports) {
63 std::vector<const Report*> doomed_reports;
64
65 for (const Report* report : reports) {
66 DCHECK(report->pending);
67 reports_[report]->pending = false;
68 if (report->doomed)
69 doomed_reports.push_back(report);
70 }
71
72 RemoveReports(doomed_reports);
73 }
74
75 void ReportingCache::IncrementReportsAttempts(
76 const std::vector<const Report*>& reports) {
77 for (const Report* report : reports) {
78 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.
79 reports_[report]->attempts++;
80 }
81 }
82
83 void ReportingCache::RemoveReports(const std::vector<const Report*>& reports) {
84 for (const Report* report : reports) {
85 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.
86 if (report->pending)
87 reports_[report]->doomed = true;
88 else
89 reports_.erase(report);
90 }
91 }
92
93 void ReportingCache::GetClients(std::vector<const Client*>* clients_out) const {
94 clients_out->clear();
95 for (auto& it : clients_)
jkarlin 2017/03/15 18:54:31 const auto&
Julia Tuttle 2017/03/16 14:50:37 Done.
96 for (auto& jt : it.second)
jkarlin 2017/03/15 18:54:31 const auto&
Julia Tuttle 2017/03/16 14:50:37 Done.
97 clients_out->push_back(&jt.second);
98 }
99
100 void ReportingCache::GetClientsForOriginAndGroup(
101 const url::Origin& origin,
102 const std::string& group,
103 std::vector<const Client*>* clients_out) const {
104 clients_out->clear();
105
106 auto it = clients_.find(origin);
107 if (it == clients_.end())
108 return;
109
110 for (auto& jt : it->second) {
jkarlin 2017/03/15 18:54:32 const auto&
Julia Tuttle 2017/03/16 14:50:37 Done.
111 if (jt.second.group == group)
112 clients_out->push_back(&jt.second);
113 }
114 }
115
116 void ReportingCache::SetClient(const url::Origin& origin,
117 const GURL& endpoint,
118 bool subdomains,
119 const std::string& group,
120 base::TimeTicks expires) {
121 if (!endpoint.SchemeIsCryptographic()) {
122 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
123 return;
124 }
125
126 Client* client = &clients_[origin][endpoint];
127 client->origin = origin;
128 client->endpoint = endpoint;
129 client->subdomains = subdomains;
130 client->group = group;
131 client->expires = expires;
132 }
133
134 void ReportingCache::RemoveClients(
135 const std::vector<const Client*>& clients_to_remove) {
136 for (const Client* client : clients_to_remove) {
137 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.
138 DCHECK(&clients_[client->origin][client->endpoint] == client);
139 clients_[client->origin].erase(client->endpoint);
140 }
141 }
142
143 void ReportingCache::RemoveClientForOriginAndEndpoint(const url::Origin& origin,
144 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.
145 clients_[origin].erase(endpoint);
146 }
147
148 void ReportingCache::RemoveClientsForEndpoint(const GURL& endpoint) {
149 for (auto& it : clients_)
150 it.second.erase(endpoint);
151 }
152
153 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698