| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/reporting/reporting_cache.h" | 5 #include "net/reporting/reporting_cache.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "net/reporting/reporting_client.h" | 16 #include "net/reporting/reporting_client.h" |
| 17 #include "net/reporting/reporting_context.h" |
| 17 #include "net/reporting/reporting_report.h" | 18 #include "net/reporting/reporting_report.h" |
| 18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 19 | 20 |
| 20 namespace net { | 21 namespace net { |
| 21 | 22 |
| 22 ReportingCache::ReportingCache(ReportingContext* context) : context_(context) { | 23 ReportingCache::ReportingCache(ReportingContext* context) : context_(context) { |
| 23 DCHECK(context_); | 24 DCHECK(context_); |
| 24 } | 25 } |
| 25 | 26 |
| 26 ReportingCache::~ReportingCache() {} | 27 ReportingCache::~ReportingCache() {} |
| 27 | 28 |
| 28 void ReportingCache::AddReport(const GURL& url, | 29 void ReportingCache::AddReport(const GURL& url, |
| 29 const std::string& group, | 30 const std::string& group, |
| 30 const std::string& type, | 31 const std::string& type, |
| 31 std::unique_ptr<const base::Value> body, | 32 std::unique_ptr<const base::Value> body, |
| 32 base::TimeTicks queued, | 33 base::TimeTicks queued, |
| 33 int attempts) { | 34 int attempts) { |
| 34 auto report = base::MakeUnique<ReportingReport>( | 35 auto report = base::MakeUnique<ReportingReport>( |
| 35 url, group, type, std::move(body), queued, attempts); | 36 url, group, type, std::move(body), queued, attempts); |
| 36 | 37 |
| 37 auto inserted = | 38 auto inserted = |
| 38 reports_.insert(std::make_pair(report.get(), std::move(report))); | 39 reports_.insert(std::make_pair(report.get(), std::move(report))); |
| 39 DCHECK(inserted.second); | 40 DCHECK(inserted.second); |
| 41 |
| 42 context_->NotifyCacheUpdated(); |
| 40 } | 43 } |
| 41 | 44 |
| 42 void ReportingCache::GetReports( | 45 void ReportingCache::GetReports( |
| 43 std::vector<const ReportingReport*>* reports_out) const { | 46 std::vector<const ReportingReport*>* reports_out) const { |
| 44 reports_out->clear(); | 47 reports_out->clear(); |
| 45 for (const auto& it : reports_) { | 48 for (const auto& it : reports_) { |
| 46 if (!base::ContainsKey(doomed_reports_, it.first)) | 49 if (!base::ContainsKey(doomed_reports_, it.first)) |
| 47 reports_out->push_back(it.second.get()); | 50 reports_out->push_back(it.second.get()); |
| 48 } | 51 } |
| 49 } | 52 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 71 | 74 |
| 72 RemoveReports(reports_to_remove); | 75 RemoveReports(reports_to_remove); |
| 73 } | 76 } |
| 74 | 77 |
| 75 void ReportingCache::IncrementReportsAttempts( | 78 void ReportingCache::IncrementReportsAttempts( |
| 76 const std::vector<const ReportingReport*>& reports) { | 79 const std::vector<const ReportingReport*>& reports) { |
| 77 for (const ReportingReport* report : reports) { | 80 for (const ReportingReport* report : reports) { |
| 78 DCHECK(base::ContainsKey(reports_, report)); | 81 DCHECK(base::ContainsKey(reports_, report)); |
| 79 reports_[report]->attempts++; | 82 reports_[report]->attempts++; |
| 80 } | 83 } |
| 84 |
| 85 context_->NotifyCacheUpdated(); |
| 81 } | 86 } |
| 82 | 87 |
| 83 void ReportingCache::RemoveReports( | 88 void ReportingCache::RemoveReports( |
| 84 const std::vector<const ReportingReport*>& reports) { | 89 const std::vector<const ReportingReport*>& reports) { |
| 85 for (const ReportingReport* report : reports) { | 90 for (const ReportingReport* report : reports) { |
| 86 DCHECK(base::ContainsKey(reports_, report)); | 91 DCHECK(base::ContainsKey(reports_, report)); |
| 87 if (base::ContainsKey(pending_reports_, report)) | 92 if (base::ContainsKey(pending_reports_, report)) |
| 88 doomed_reports_.insert(report); | 93 doomed_reports_.insert(report); |
| 89 else { | 94 else { |
| 90 DCHECK(!base::ContainsKey(doomed_reports_, report)); | 95 DCHECK(!base::ContainsKey(doomed_reports_, report)); |
| 91 reports_.erase(report); | 96 reports_.erase(report); |
| 92 } | 97 } |
| 93 } | 98 } |
| 99 |
| 100 context_->NotifyCacheUpdated(); |
| 94 } | 101 } |
| 95 | 102 |
| 96 void ReportingCache::RemoveAllReports() { | 103 void ReportingCache::RemoveAllReports() { |
| 97 std::vector<std::unordered_map<const ReportingReport*, | 104 std::vector<std::unordered_map<const ReportingReport*, |
| 98 std::unique_ptr<ReportingReport>>::iterator> | 105 std::unique_ptr<ReportingReport>>::iterator> |
| 99 reports_to_remove; | 106 reports_to_remove; |
| 100 for (auto it = reports_.begin(); it != reports_.end(); ++it) { | 107 for (auto it = reports_.begin(); it != reports_.end(); ++it) { |
| 101 ReportingReport* report = it->second.get(); | 108 ReportingReport* report = it->second.get(); |
| 102 if (!base::ContainsKey(pending_reports_, report)) | 109 if (!base::ContainsKey(pending_reports_, report)) |
| 103 reports_to_remove.push_back(it); | 110 reports_to_remove.push_back(it); |
| 104 else | 111 else |
| 105 doomed_reports_.insert(report); | 112 doomed_reports_.insert(report); |
| 106 } | 113 } |
| 107 | 114 |
| 108 for (auto& it : reports_to_remove) | 115 for (auto& it : reports_to_remove) |
| 109 reports_.erase(it); | 116 reports_.erase(it); |
| 117 |
| 118 context_->NotifyCacheUpdated(); |
| 110 } | 119 } |
| 111 | 120 |
| 112 void ReportingCache::GetClients( | 121 void ReportingCache::GetClients( |
| 113 std::vector<const ReportingClient*>* clients_out) const { | 122 std::vector<const ReportingClient*>* clients_out) const { |
| 114 clients_out->clear(); | 123 clients_out->clear(); |
| 115 for (const auto& it : clients_) | 124 for (const auto& it : clients_) |
| 116 for (const auto& endpoint_and_client : it.second) | 125 for (const auto& endpoint_and_client : it.second) |
| 117 clients_out->push_back(endpoint_and_client.second.get()); | 126 clients_out->push_back(endpoint_and_client.second.get()); |
| 118 } | 127 } |
| 119 | 128 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 135 | 144 |
| 136 void ReportingCache::SetClient(const url::Origin& origin, | 145 void ReportingCache::SetClient(const url::Origin& origin, |
| 137 const GURL& endpoint, | 146 const GURL& endpoint, |
| 138 ReportingClient::Subdomains subdomains, | 147 ReportingClient::Subdomains subdomains, |
| 139 const std::string& group, | 148 const std::string& group, |
| 140 base::TimeTicks expires) { | 149 base::TimeTicks expires) { |
| 141 DCHECK(endpoint.SchemeIsCryptographic()); | 150 DCHECK(endpoint.SchemeIsCryptographic()); |
| 142 | 151 |
| 143 clients_[origin][endpoint] = base::MakeUnique<ReportingClient>( | 152 clients_[origin][endpoint] = base::MakeUnique<ReportingClient>( |
| 144 origin, endpoint, subdomains, group, expires); | 153 origin, endpoint, subdomains, group, expires); |
| 154 |
| 155 context_->NotifyCacheUpdated(); |
| 145 } | 156 } |
| 146 | 157 |
| 147 void ReportingCache::RemoveClients( | 158 void ReportingCache::RemoveClients( |
| 148 const std::vector<const ReportingClient*>& clients_to_remove) { | 159 const std::vector<const ReportingClient*>& clients_to_remove) { |
| 149 for (const ReportingClient* client : clients_to_remove) { | 160 for (const ReportingClient* client : clients_to_remove) { |
| 150 DCHECK(base::ContainsKey(clients_[client->origin], client->endpoint)); | 161 DCHECK(base::ContainsKey(clients_[client->origin], client->endpoint)); |
| 151 DCHECK(clients_[client->origin][client->endpoint].get() == client); | 162 DCHECK(clients_[client->origin][client->endpoint].get() == client); |
| 152 clients_[client->origin].erase(client->endpoint); | 163 clients_[client->origin].erase(client->endpoint); |
| 153 } | 164 } |
| 165 |
| 166 context_->NotifyCacheUpdated(); |
| 154 } | 167 } |
| 155 | 168 |
| 156 void ReportingCache::RemoveClientForOriginAndEndpoint(const url::Origin& origin, | 169 void ReportingCache::RemoveClientForOriginAndEndpoint(const url::Origin& origin, |
| 157 const GURL& endpoint) { | 170 const GURL& endpoint) { |
| 158 DCHECK(base::ContainsKey(clients_, origin)); | 171 DCHECK(base::ContainsKey(clients_, origin)); |
| 159 DCHECK(base::ContainsKey(clients_[origin], endpoint)); | 172 DCHECK(base::ContainsKey(clients_[origin], endpoint)); |
| 160 clients_[origin].erase(endpoint); | 173 clients_[origin].erase(endpoint); |
| 174 |
| 175 context_->NotifyCacheUpdated(); |
| 161 } | 176 } |
| 162 | 177 |
| 163 void ReportingCache::RemoveClientsForEndpoint(const GURL& endpoint) { | 178 void ReportingCache::RemoveClientsForEndpoint(const GURL& endpoint) { |
| 164 for (auto& it : clients_) | 179 for (auto& it : clients_) |
| 165 it.second.erase(endpoint); | 180 it.second.erase(endpoint); |
| 181 |
| 182 context_->NotifyCacheUpdated(); |
| 166 } | 183 } |
| 167 | 184 |
| 168 void ReportingCache::RemoveAllClients() { | 185 void ReportingCache::RemoveAllClients() { |
| 169 clients_.clear(); | 186 clients_.clear(); |
| 187 |
| 188 context_->NotifyCacheUpdated(); |
| 170 } | 189 } |
| 171 | 190 |
| 172 } // namespace net | 191 } // namespace net |
| OLD | NEW |