OLD | NEW |
---|---|
(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 <map> | |
8 #include <memory> | |
9 #include <set> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/memory/ptr_util.h" | |
14 #include "base/stl_util.h" | |
15 #include "base/time/time.h" | |
16 #include "net/reporting/reporting_client.h" | |
17 #include "net/reporting/reporting_report.h" | |
18 #include "url/gurl.h" | |
19 | |
20 namespace net { | |
21 | |
22 ReportingCache::ReportingCache() {} | |
23 | |
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<ReportingReport>( | |
33 url, group, type, std::move(body), queued, attempts); | |
34 | |
35 DCHECK(!base::ContainsKey(reports_, report.get())); | |
36 reports_[report.get()] = std::move(report); | |
shivanisha
2017/03/23 19:11:59
The dcheck can be done on the return value of inse
Julia Tuttle
2017/03/23 19:24:04
Done.
| |
37 } | |
38 | |
39 void ReportingCache::GetReports( | |
40 std::vector<const ReportingReport*>* reports_out) const { | |
41 reports_out->clear(); | |
42 for (const auto& it : reports_) { | |
43 if (!base::ContainsKey(doomed_reports_, it.first)) | |
44 reports_out->push_back(it.second.get()); | |
45 } | |
46 } | |
47 | |
48 void ReportingCache::SetReportsPending( | |
49 const std::vector<const ReportingReport*>& reports) { | |
50 for (const ReportingReport* report : reports) { | |
51 DCHECK(!base::ContainsKey(pending_reports_, report)); | |
52 pending_reports_.insert(report); | |
shivanisha
2017/03/23 19:11:59
The dcheck can be done on the return value of inse
Julia Tuttle
2017/03/23 19:24:04
Done.
| |
53 } | |
54 } | |
55 | |
56 void ReportingCache::ClearReportsPending( | |
57 const std::vector<const ReportingReport*>& reports) { | |
58 std::vector<const ReportingReport*> reports_to_remove; | |
59 | |
60 for (const ReportingReport* report : reports) { | |
61 DCHECK(base::ContainsKey(pending_reports_, report)); | |
62 pending_reports_.erase(report); | |
shivanisha
2017/03/23 19:11:59
dcheck can be done on the return value of erase.
Julia Tuttle
2017/03/23 19:24:04
Done.
| |
63 if (base::ContainsKey(doomed_reports_, report)) { | |
64 reports_to_remove.push_back(report); | |
65 doomed_reports_.erase(report); | |
66 } | |
67 } | |
68 | |
69 RemoveReports(reports_to_remove); | |
70 } | |
71 | |
72 void ReportingCache::IncrementReportsAttempts( | |
73 const std::vector<const ReportingReport*>& reports) { | |
74 for (const ReportingReport* report : reports) { | |
75 DCHECK(base::ContainsKey(reports_, report)); | |
76 reports_[report]->attempts++; | |
77 } | |
78 } | |
79 | |
80 void ReportingCache::RemoveReports( | |
81 const std::vector<const ReportingReport*>& reports) { | |
82 for (const ReportingReport* report : reports) { | |
83 DCHECK(base::ContainsKey(reports_, report)); | |
84 if (base::ContainsKey(pending_reports_, report)) | |
85 doomed_reports_.insert(report); | |
86 else { | |
87 DCHECK(!base::ContainsKey(doomed_reports_, report)); | |
88 reports_.erase(report); | |
89 } | |
90 } | |
91 } | |
92 | |
93 void ReportingCache::RemoveAllReports() { | |
94 std::vector<std::unordered_map<const ReportingReport*, | |
95 std::unique_ptr<ReportingReport>>::iterator> | |
96 reports_to_remove; | |
97 for (auto it = reports_.begin(); it != reports_.end(); ++it) { | |
98 ReportingReport* report = it->second.get(); | |
99 if (!base::ContainsKey(pending_reports_, report)) | |
100 reports_to_remove.push_back(it); | |
101 else | |
102 doomed_reports_.insert(report); | |
103 } | |
104 | |
105 for (auto& it : reports_to_remove) | |
106 reports_.erase(it); | |
107 } | |
108 | |
109 void ReportingCache::GetClients( | |
110 std::vector<const ReportingClient*>* clients_out) const { | |
111 clients_out->clear(); | |
112 for (const auto& it : clients_) | |
113 for (const auto& endpoint_and_client : it.second) | |
114 clients_out->push_back(endpoint_and_client.second.get()); | |
115 } | |
116 | |
117 void ReportingCache::GetClientsForOriginAndGroup( | |
118 const url::Origin& origin, | |
119 const std::string& group, | |
120 std::vector<const ReportingClient*>* clients_out) const { | |
121 clients_out->clear(); | |
122 | |
123 const auto it = clients_.find(origin); | |
124 if (it == clients_.end()) | |
125 return; | |
126 | |
127 for (const auto& endpoint_and_client : it->second) { | |
128 if (endpoint_and_client.second->group == group) | |
129 clients_out->push_back(endpoint_and_client.second.get()); | |
130 } | |
131 } | |
132 | |
133 void ReportingCache::SetClient(const url::Origin& origin, | |
134 const GURL& endpoint, | |
135 ReportingClient::Subdomains subdomains, | |
136 const std::string& group, | |
137 base::TimeTicks expires) { | |
138 DCHECK(endpoint.SchemeIsCryptographic()); | |
139 | |
140 clients_[origin][endpoint] = base::MakeUnique<ReportingClient>( | |
141 origin, endpoint, subdomains, group, expires); | |
142 } | |
143 | |
144 void ReportingCache::RemoveClients( | |
145 const std::vector<const ReportingClient*>& clients_to_remove) { | |
146 for (const ReportingClient* client : clients_to_remove) { | |
147 DCHECK(base::ContainsKey(clients_[client->origin], client->endpoint)); | |
148 DCHECK(clients_[client->origin][client->endpoint].get() == client); | |
149 clients_[client->origin].erase(client->endpoint); | |
150 } | |
151 } | |
152 | |
153 void ReportingCache::RemoveClientForOriginAndEndpoint(const url::Origin& origin, | |
154 const GURL& endpoint) { | |
155 DCHECK(base::ContainsKey(clients_, origin)); | |
156 DCHECK(base::ContainsKey(clients_[origin], endpoint)); | |
157 clients_[origin].erase(endpoint); | |
158 } | |
159 | |
160 void ReportingCache::RemoveClientsForEndpoint(const GURL& endpoint) { | |
161 for (auto& it : clients_) | |
162 it.second.erase(endpoint); | |
163 } | |
164 | |
165 void ReportingCache::RemoveAllClients() { | |
166 clients_.clear(); | |
167 } | |
168 | |
169 } // namespace net | |
OLD | NEW |