| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/domain_reliability/context.h" | 5 #include "components/domain_reliability/context.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 UMA_HISTOGRAM_SPARSE_SLOWLY( | 90 UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 91 "DomainReliability.ReportedBeaconError_HasServerIP", | 91 "DomainReliability.ReportedBeaconError_HasServerIP", |
| 92 -beacon->chrome_error); | 92 -beacon->chrome_error); |
| 93 } | 93 } |
| 94 // TODO(juliatuttle): Histogram HTTP response code? | 94 // TODO(juliatuttle): Histogram HTTP response code? |
| 95 | 95 |
| 96 // Allow beacons about reports, but don't schedule an upload for more than | 96 // Allow beacons about reports, but don't schedule an upload for more than |
| 97 // one layer of recursion, to avoid infinite report loops. | 97 // one layer of recursion, to avoid infinite report loops. |
| 98 if (beacon->upload_depth <= kMaxUploadDepthToSchedule) | 98 if (beacon->upload_depth <= kMaxUploadDepthToSchedule) |
| 99 scheduler_.OnBeaconAdded(); | 99 scheduler_.OnBeaconAdded(); |
| 100 beacons_.push_back(beacon.release()); | 100 beacons_.push_back(std::move(beacon)); |
| 101 bool should_evict = beacons_.size() > kMaxQueuedBeacons; | 101 bool should_evict = beacons_.size() > kMaxQueuedBeacons; |
| 102 if (should_evict) | 102 if (should_evict) |
| 103 RemoveOldestBeacon(); | 103 RemoveOldestBeacon(); |
| 104 | 104 |
| 105 LogOnBeaconDidEvictHistogram(should_evict); | 105 LogOnBeaconDidEvictHistogram(should_evict); |
| 106 } | 106 } |
| 107 | 107 |
| 108 void DomainReliabilityContext::ClearBeacons() { | 108 void DomainReliabilityContext::ClearBeacons() { |
| 109 base::STLDeleteElements(&beacons_); | |
| 110 beacons_.clear(); | 109 beacons_.clear(); |
| 111 uploading_beacons_size_ = 0; | 110 uploading_beacons_size_ = 0; |
| 112 } | 111 } |
| 113 | 112 |
| 114 std::unique_ptr<Value> DomainReliabilityContext::GetWebUIData() const { | 113 std::unique_ptr<Value> DomainReliabilityContext::GetWebUIData() const { |
| 115 DictionaryValue* context_value = new DictionaryValue(); | 114 DictionaryValue* context_value = new DictionaryValue(); |
| 116 | 115 |
| 117 context_value->SetString("origin", config().origin.spec()); | 116 context_value->SetString("origin", config().origin.spec()); |
| 118 context_value->SetInteger("beacon_count", static_cast<int>(beacons_.size())); | 117 context_value->SetInteger("beacon_count", static_cast<int>(beacons_.size())); |
| 119 context_value->SetInteger("uploading_beacon_count", | 118 context_value->SetInteger("uploading_beacon_count", |
| 120 static_cast<int>(uploading_beacons_size_)); | 119 static_cast<int>(uploading_beacons_size_)); |
| 121 context_value->Set("scheduler", scheduler_.GetWebUIData()); | 120 context_value->Set("scheduler", scheduler_.GetWebUIData()); |
| 122 | 121 |
| 123 return std::unique_ptr<Value>(context_value); | 122 return std::unique_ptr<Value>(context_value); |
| 124 } | 123 } |
| 125 | 124 |
| 126 void DomainReliabilityContext::GetQueuedBeaconsForTesting( | 125 void DomainReliabilityContext::GetQueuedBeaconsForTesting( |
| 127 std::vector<const DomainReliabilityBeacon*>* beacons_out) const { | 126 std::vector<const DomainReliabilityBeacon*>* beacons_out) const { |
| 128 DCHECK(this); | 127 DCHECK(this); |
| 129 DCHECK(beacons_out); | 128 DCHECK(beacons_out); |
| 130 beacons_out->assign(beacons_.begin(), beacons_.end()); | 129 beacons_out->clear(); |
| 130 for (const auto& beacon : beacons_) |
| 131 beacons_out->push_back(beacon.get()); |
| 131 } | 132 } |
| 132 | 133 |
| 133 void DomainReliabilityContext::ScheduleUpload( | 134 void DomainReliabilityContext::ScheduleUpload( |
| 134 base::TimeDelta min_delay, | 135 base::TimeDelta min_delay, |
| 135 base::TimeDelta max_delay) { | 136 base::TimeDelta max_delay) { |
| 136 dispatcher_->ScheduleTask( | 137 dispatcher_->ScheduleTask( |
| 137 base::Bind( | 138 base::Bind( |
| 138 &DomainReliabilityContext::StartUpload, | 139 &DomainReliabilityContext::StartUpload, |
| 139 weak_factory_.GetWeakPtr()), | 140 weak_factory_.GetWeakPtr()), |
| 140 min_delay, | 141 min_delay, |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 upload_time_ = base::TimeTicks(); | 198 upload_time_ = base::TimeTicks(); |
| 198 } | 199 } |
| 199 | 200 |
| 200 std::unique_ptr<const Value> DomainReliabilityContext::CreateReport( | 201 std::unique_ptr<const Value> DomainReliabilityContext::CreateReport( |
| 201 base::TimeTicks upload_time, | 202 base::TimeTicks upload_time, |
| 202 const GURL& collector_url, | 203 const GURL& collector_url, |
| 203 int* max_upload_depth_out) const { | 204 int* max_upload_depth_out) const { |
| 204 int max_upload_depth = 0; | 205 int max_upload_depth = 0; |
| 205 | 206 |
| 206 std::unique_ptr<ListValue> beacons_value(new ListValue()); | 207 std::unique_ptr<ListValue> beacons_value(new ListValue()); |
| 207 for (const auto* beacon : beacons_) { | 208 for (const auto& beacon : beacons_) { |
| 208 beacons_value->Append(beacon->ToValue(upload_time, | 209 beacons_value->Append(beacon->ToValue(upload_time, |
| 209 *last_network_change_time_, | 210 *last_network_change_time_, |
| 210 collector_url, | 211 collector_url, |
| 211 config().path_prefixes)); | 212 config().path_prefixes)); |
| 212 if (beacon->upload_depth > max_upload_depth) | 213 if (beacon->upload_depth > max_upload_depth) |
| 213 max_upload_depth = beacon->upload_depth; | 214 max_upload_depth = beacon->upload_depth; |
| 214 } | 215 } |
| 215 | 216 |
| 216 std::unique_ptr<DictionaryValue> report_value(new DictionaryValue()); | 217 std::unique_ptr<DictionaryValue> report_value(new DictionaryValue()); |
| 217 report_value->SetString("reporter", upload_reporter_string_); | 218 report_value->SetString("reporter", upload_reporter_string_); |
| 218 report_value->Set("entries", beacons_value.release()); | 219 report_value->Set("entries", beacons_value.release()); |
| 219 | 220 |
| 220 *max_upload_depth_out = max_upload_depth; | 221 *max_upload_depth_out = max_upload_depth; |
| 221 return std::move(report_value); | 222 return std::move(report_value); |
| 222 } | 223 } |
| 223 | 224 |
| 224 void DomainReliabilityContext::MarkUpload() { | 225 void DomainReliabilityContext::MarkUpload() { |
| 225 DCHECK_EQ(0u, uploading_beacons_size_); | 226 DCHECK_EQ(0u, uploading_beacons_size_); |
| 226 uploading_beacons_size_ = beacons_.size(); | 227 uploading_beacons_size_ = beacons_.size(); |
| 227 DCHECK_NE(0u, uploading_beacons_size_); | 228 DCHECK_NE(0u, uploading_beacons_size_); |
| 228 } | 229 } |
| 229 | 230 |
| 230 void DomainReliabilityContext::CommitUpload() { | 231 void DomainReliabilityContext::CommitUpload() { |
| 231 auto begin = beacons_.begin(); | 232 auto begin = beacons_.begin(); |
| 232 auto end = begin + uploading_beacons_size_; | 233 auto end = begin + uploading_beacons_size_; |
| 233 base::STLDeleteContainerPointers(begin, end); | |
| 234 beacons_.erase(begin, end); | 234 beacons_.erase(begin, end); |
| 235 DCHECK_NE(0u, uploading_beacons_size_); | 235 DCHECK_NE(0u, uploading_beacons_size_); |
| 236 uploading_beacons_size_ = 0; | 236 uploading_beacons_size_ = 0; |
| 237 } | 237 } |
| 238 | 238 |
| 239 void DomainReliabilityContext::RollbackUpload() { | 239 void DomainReliabilityContext::RollbackUpload() { |
| 240 DCHECK_NE(0u, uploading_beacons_size_); | 240 DCHECK_NE(0u, uploading_beacons_size_); |
| 241 uploading_beacons_size_ = 0; | 241 uploading_beacons_size_ = 0; |
| 242 } | 242 } |
| 243 | 243 |
| 244 void DomainReliabilityContext::RemoveOldestBeacon() { | 244 void DomainReliabilityContext::RemoveOldestBeacon() { |
| 245 DCHECK(!beacons_.empty()); | 245 DCHECK(!beacons_.empty()); |
| 246 | 246 |
| 247 VLOG(1) << "Beacon queue for " << config().origin << " full; " | 247 VLOG(1) << "Beacon queue for " << config().origin << " full; " |
| 248 << "removing oldest beacon"; | 248 << "removing oldest beacon"; |
| 249 | 249 |
| 250 delete beacons_.front(); | |
| 251 beacons_.pop_front(); | 250 beacons_.pop_front(); |
| 252 | 251 |
| 253 // If that just removed a beacon counted in uploading_beacons_size_, decrement | 252 // If that just removed a beacon counted in uploading_beacons_size_, decrement |
| 254 // that. | 253 // that. |
| 255 if (uploading_beacons_size_ > 0) | 254 if (uploading_beacons_size_ > 0) |
| 256 --uploading_beacons_size_; | 255 --uploading_beacons_size_; |
| 257 } | 256 } |
| 258 | 257 |
| 259 } // namespace domain_reliability | 258 } // namespace domain_reliability |
| OLD | NEW |