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 // Change for readability |
| 6 |
5 #include "components/rappor/log_uploader.h" | 7 #include "components/rappor/log_uploader.h" |
6 | 8 |
7 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
8 #include "base/metrics/sparse_histogram.h" | 10 #include "base/metrics/sparse_histogram.h" |
9 #include "net/base/load_flags.h" | 11 #include "net/base/load_flags.h" |
10 #include "net/url_request/url_fetcher.h" | 12 #include "net/url_request/url_fetcher.h" |
11 | 13 |
12 namespace { | 14 namespace { |
13 | 15 |
14 // The delay, in seconds, between uploading when there are queued logs to send. | 16 // The delay, in seconds, between uploading when there are queued logs to send. |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 return interval > max_interval ? max_interval : interval; | 97 return interval > max_interval ? max_interval : interval; |
96 } | 98 } |
97 | 99 |
98 void LogUploader::OnURLFetchComplete(const net::URLFetcher* source) { | 100 void LogUploader::OnURLFetchComplete(const net::URLFetcher* source) { |
99 // We're not allowed to re-use the existing |URLFetcher|s, so free them here. | 101 // We're not allowed to re-use the existing |URLFetcher|s, so free them here. |
100 // Note however that |source| is aliased to the fetcher, so we should be | 102 // Note however that |source| is aliased to the fetcher, so we should be |
101 // careful not to delete it too early. | 103 // careful not to delete it too early. |
102 DCHECK_EQ(current_fetch_.get(), source); | 104 DCHECK_EQ(current_fetch_.get(), source); |
103 scoped_ptr<net::URLFetcher> fetch(current_fetch_.Pass()); | 105 scoped_ptr<net::URLFetcher> fetch(current_fetch_.Pass()); |
104 | 106 |
105 int response_code = source->GetResponseCode(); | 107 const int response_code = source->GetResponseCode(); |
106 | 108 |
107 // Log a histogram to track response success vs. failure rates. | 109 // Log a histogram to track response success vs. failure rates. |
108 UMA_HISTOGRAM_SPARSE_SLOWLY("Rappor.UploadResponseCode", response_code); | 110 UMA_HISTOGRAM_SPARSE_SLOWLY("Rappor.UploadResponseCode", response_code); |
109 | 111 |
110 bool upload_succeeded = response_code == 200; | 112 const bool upload_succeeded = response_code == 200; |
111 | 113 |
112 // Determine whether this log should be retransmitted. | 114 // Determine whether this log should be retransmitted. |
113 DiscardReason reason = NUM_DISCARD_REASONS; | 115 DiscardReason reason = NUM_DISCARD_REASONS; |
114 if (upload_succeeded) { | 116 if (upload_succeeded) { |
115 reason = UPLOAD_SUCCESS; | 117 reason = UPLOAD_SUCCESS; |
116 } else if (response_code == 400) { | 118 } else if (response_code == 400) { |
117 reason = UPLOAD_REJECTED; | 119 reason = UPLOAD_REJECTED; |
118 } else if (queued_logs_.size() > kMaxQueuedLogs) { | 120 } else if (queued_logs_.size() > kMaxQueuedLogs) { |
119 reason = QUEUE_OVERFLOW; | 121 reason = QUEUE_OVERFLOW; |
120 } | 122 } |
121 | 123 |
122 if (reason != NUM_DISCARD_REASONS) { | 124 if (reason != NUM_DISCARD_REASONS) { |
123 UMA_HISTOGRAM_ENUMERATION("Rappor.DiscardReason", | 125 UMA_HISTOGRAM_ENUMERATION("Rappor.DiscardReason", |
124 reason, | 126 reason, |
125 NUM_DISCARD_REASONS); | 127 NUM_DISCARD_REASONS); |
126 queued_logs_.pop(); | 128 queued_logs_.pop(); |
127 } | 129 } |
128 | 130 |
129 // Error 400 indicates a problem with the log, not with the server, so | 131 // Error 400 indicates a problem with the log, not with the server, so |
130 // don't consider that a sign that the server is in trouble. | 132 // don't consider that a sign that the server is in trouble. |
131 bool server_is_healthy = upload_succeeded || response_code == 400; | 133 const bool server_is_healthy = upload_succeeded || response_code == 400; |
132 OnUploadFinished(server_is_healthy, !queued_logs_.empty()); | 134 OnUploadFinished(server_is_healthy, !queued_logs_.empty()); |
133 } | 135 } |
134 | 136 |
135 void LogUploader::OnUploadFinished(bool server_is_healthy, | 137 void LogUploader::OnUploadFinished(bool server_is_healthy, |
136 bool more_logs_remaining) { | 138 bool more_logs_remaining) { |
137 DCHECK(has_callback_pending_); | 139 DCHECK(has_callback_pending_); |
138 has_callback_pending_ = false; | 140 has_callback_pending_ = false; |
139 // If the server is having issues, back off. Otherwise, reset to default. | 141 // If the server is having issues, back off. Otherwise, reset to default. |
140 if (!server_is_healthy) | 142 if (!server_is_healthy) |
141 upload_interval_ = BackOffUploadInterval(upload_interval_); | 143 upload_interval_ = BackOffUploadInterval(upload_interval_); |
142 else | 144 else |
143 upload_interval_ = base::TimeDelta::FromSeconds(kUnsentLogsIntervalSeconds); | 145 upload_interval_ = base::TimeDelta::FromSeconds(kUnsentLogsIntervalSeconds); |
144 | 146 |
145 if (more_logs_remaining) | 147 if (more_logs_remaining) |
146 ScheduleNextUpload(upload_interval_); | 148 ScheduleNextUpload(upload_interval_); |
147 } | 149 } |
148 | 150 |
149 } // namespace rappor | 151 } // namespace rappor |
OLD | NEW |