| 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 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/metrics/sparse_histogram.h" | 13 #include "base/metrics/sparse_histogram.h" |
| 14 #include "base/rand_util.h" |
| 14 #include "base/values.h" | 15 #include "base/values.h" |
| 15 #include "components/domain_reliability/dispatcher.h" | 16 #include "components/domain_reliability/dispatcher.h" |
| 16 #include "components/domain_reliability/uploader.h" | 17 #include "components/domain_reliability/uploader.h" |
| 17 #include "components/domain_reliability/util.h" | 18 #include "components/domain_reliability/util.h" |
| 18 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 19 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
| 20 | 21 |
| 21 using base::DictionaryValue; | 22 using base::DictionaryValue; |
| 22 using base::ListValue; | 23 using base::ListValue; |
| 23 using base::Value; | 24 using base::Value; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 weak_factory_(this) { | 63 weak_factory_(this) { |
| 63 } | 64 } |
| 64 | 65 |
| 65 DomainReliabilityContext::~DomainReliabilityContext() { | 66 DomainReliabilityContext::~DomainReliabilityContext() { |
| 66 ClearBeacons(); | 67 ClearBeacons(); |
| 67 } | 68 } |
| 68 | 69 |
| 69 void DomainReliabilityContext::OnBeacon( | 70 void DomainReliabilityContext::OnBeacon( |
| 70 scoped_ptr<DomainReliabilityBeacon> beacon) { | 71 scoped_ptr<DomainReliabilityBeacon> beacon) { |
| 71 bool success = (beacon->status == "ok"); | 72 bool success = (beacon->status == "ok"); |
| 72 | 73 double sample_rate = config().GetSampleRate(success); |
| 73 bool reported = config().DecideIfShouldReportRequest(success); | 74 bool should_report = base::RandDouble() < sample_rate; |
| 74 UMA_HISTOGRAM_BOOLEAN("DomainReliability.BeaconReported", reported); | 75 UMA_HISTOGRAM_BOOLEAN("DomainReliability.BeaconReported", should_report); |
| 75 if (!reported) { | 76 if (!should_report) { |
| 76 // If the beacon isn't queued to be reported, it definitely cannot evict | 77 // If the beacon isn't queued to be reported, it definitely cannot evict |
| 77 // an older beacon. (This histogram is also logged below based on whether | 78 // an older beacon. (This histogram is also logged below based on whether |
| 78 // an older beacon was actually evicted.) | 79 // an older beacon was actually evicted.) |
| 79 LogOnBeaconDidEvictHistogram(false); | 80 LogOnBeaconDidEvictHistogram(false); |
| 80 return; | 81 return; |
| 81 } | 82 } |
| 83 beacon->sample_rate = sample_rate; |
| 82 | 84 |
| 83 UMA_HISTOGRAM_SPARSE_SLOWLY("DomainReliability.ReportedBeaconError", | 85 UMA_HISTOGRAM_SPARSE_SLOWLY("DomainReliability.ReportedBeaconError", |
| 84 -beacon->chrome_error); | 86 -beacon->chrome_error); |
| 85 if (!beacon->server_ip.empty()) { | 87 if (!beacon->server_ip.empty()) { |
| 86 UMA_HISTOGRAM_SPARSE_SLOWLY( | 88 UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 87 "DomainReliability.ReportedBeaconError_HasServerIP", | 89 "DomainReliability.ReportedBeaconError_HasServerIP", |
| 88 -beacon->chrome_error); | 90 -beacon->chrome_error); |
| 89 } | 91 } |
| 90 // TODO(ttuttle): Histogram HTTP response code? | 92 // TODO(ttuttle): Histogram HTTP response code? |
| 91 | 93 |
| 92 // Allow beacons about reports, but don't schedule an upload for more than | 94 // Allow beacons about reports, but don't schedule an upload for more than |
| 93 // one layer of recursion, to avoid infinite report loops. | 95 // one layer of recursion, to avoid infinite report loops. |
| 94 if (beacon->upload_depth <= kMaxUploadDepthToSchedule) | 96 if (beacon->upload_depth <= kMaxUploadDepthToSchedule) |
| 95 scheduler_.OnBeaconAdded(); | 97 scheduler_.OnBeaconAdded(); |
| 96 beacons_.push_back(beacon.release()); | 98 beacons_.push_back(beacon.release()); |
| 97 bool evicted = beacons_.size() > kMaxQueuedBeacons; | 99 bool should_evict = beacons_.size() > kMaxQueuedBeacons; |
| 98 if (evicted) | 100 if (should_evict) |
| 99 RemoveOldestBeacon(); | 101 RemoveOldestBeacon(); |
| 100 | 102 |
| 101 LogOnBeaconDidEvictHistogram(evicted); | 103 LogOnBeaconDidEvictHistogram(should_evict); |
| 102 } | 104 } |
| 103 | 105 |
| 104 void DomainReliabilityContext::ClearBeacons() { | 106 void DomainReliabilityContext::ClearBeacons() { |
| 105 STLDeleteElements(&beacons_); | 107 STLDeleteElements(&beacons_); |
| 106 beacons_.clear(); | 108 beacons_.clear(); |
| 107 uploading_beacons_size_ = 0; | 109 uploading_beacons_size_ = 0; |
| 108 } | 110 } |
| 109 | 111 |
| 110 scoped_ptr<Value> DomainReliabilityContext::GetWebUIData() const { | 112 scoped_ptr<Value> DomainReliabilityContext::GetWebUIData() const { |
| 111 DictionaryValue* context_value = new DictionaryValue(); | 113 DictionaryValue* context_value = new DictionaryValue(); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 delete beacons_.front(); | 248 delete beacons_.front(); |
| 247 beacons_.pop_front(); | 249 beacons_.pop_front(); |
| 248 | 250 |
| 249 // If that just removed a beacon counted in uploading_beacons_size_, decrement | 251 // If that just removed a beacon counted in uploading_beacons_size_, decrement |
| 250 // that. | 252 // that. |
| 251 if (uploading_beacons_size_ > 0) | 253 if (uploading_beacons_size_ > 0) |
| 252 --uploading_beacons_size_; | 254 --uploading_beacons_size_; |
| 253 } | 255 } |
| 254 | 256 |
| 255 } // namespace domain_reliability | 257 } // namespace domain_reliability |
| OLD | NEW |