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); | |
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)); | |
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.
| |
52 pending_reports_.insert(report); | |
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); | |
63 if (base::ContainsKey(doomed_reports_, report)) | |
64 reports_to_remove.push_back(report); | |
65 } | |
66 | |
67 RemoveReports(reports_to_remove); | |
68 } | |
69 | |
70 void ReportingCache::IncrementReportsAttempts( | |
71 const std::vector<const ReportingReport*>& reports) { | |
72 for (const ReportingReport* report : reports) { | |
73 DCHECK(base::ContainsKey(reports_, report)); | |
74 reports_[report]->attempts++; | |
75 } | |
76 } | |
77 | |
78 void ReportingCache::RemoveReports( | |
79 const std::vector<const ReportingReport*>& reports) { | |
80 for (const ReportingReport* report : reports) { | |
81 DCHECK(base::ContainsKey(reports_, report)); | |
82 if (base::ContainsKey(pending_reports_, report)) | |
83 doomed_reports_.insert(report); | |
84 else | |
85 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
| |
86 } | |
87 } | |
88 | |
89 void ReportingCache::GetClients( | |
90 std::vector<const ReportingClient*>* clients_out) const { | |
91 clients_out->clear(); | |
92 for (const auto& it : clients_) | |
93 for (const auto& jt : it.second) | |
94 clients_out->push_back(jt.second.get()); | |
95 } | |
96 | |
97 void ReportingCache::GetClientsForOriginAndGroup( | |
98 const url::Origin& origin, | |
99 const std::string& group, | |
100 std::vector<const ReportingClient*>* clients_out) const { | |
101 clients_out->clear(); | |
102 | |
103 const auto it = clients_.find(origin); | |
104 if (it == clients_.end()) | |
105 return; | |
106 | |
107 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/
| |
108 if (jt.second->group == group) | |
109 clients_out->push_back(jt.second.get()); | |
110 } | |
111 } | |
112 | |
113 void ReportingCache::SetClient(const url::Origin& origin, | |
114 const GURL& endpoint, | |
115 ReportingClient::Subdomains subdomains, | |
116 const std::string& group, | |
117 base::TimeTicks expires) { | |
118 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
| |
119 return; | |
120 | |
121 clients_[origin][endpoint] = base::MakeUnique<ReportingClient>( | |
122 origin, endpoint, subdomains, group, expires); | |
123 } | |
124 | |
125 void ReportingCache::RemoveClients( | |
126 const std::vector<const ReportingClient*>& clients_to_remove) { | |
127 for (const ReportingClient* client : clients_to_remove) { | |
128 DCHECK(base::ContainsKey(clients_[client->origin], client->endpoint)); | |
129 DCHECK(clients_[client->origin][client->endpoint].get() == client); | |
130 clients_[client->origin].erase(client->endpoint); | |
131 } | |
132 } | |
133 | |
134 void ReportingCache::RemoveClientForOriginAndEndpoint(const url::Origin& origin, | |
135 const GURL& endpoint) { | |
136 DCHECK(base::ContainsKey(clients_, origin)); | |
137 DCHECK(base::ContainsKey(clients_[origin], endpoint)); | |
138 clients_[origin].erase(endpoint); | |
139 } | |
140 | |
141 void ReportingCache::RemoveClientsForEndpoint(const GURL& endpoint) { | |
142 for (auto& it : clients_) | |
143 it.second.erase(endpoint); | |
144 } | |
145 | |
146 } // namespace net | |
OLD | NEW |