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/data_reduction_proxy/browser/data_reduction_proxy_metrics.h
" | 5 #include "components/data_reduction_proxy/browser/data_reduction_proxy_metrics.h
" |
6 | 6 |
7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
8 #include "base/prefs/pref_service.h" | |
9 #include "base/prefs/scoped_user_pref_update.h" | |
10 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
12 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings.
h" | 10 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings.
h" |
| 11 #include "components/data_reduction_proxy/browser/data_reduction_proxy_statistic
s_prefs.h" |
13 #include "components/data_reduction_proxy/common/data_reduction_proxy_headers.h" | 12 #include "components/data_reduction_proxy/common/data_reduction_proxy_headers.h" |
14 #include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names
.h" | 13 #include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names
.h" |
15 #include "net/base/host_port_pair.h" | 14 #include "net/base/host_port_pair.h" |
16 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
17 #include "net/proxy/proxy_retry_info.h" | 16 #include "net/proxy/proxy_retry_info.h" |
18 #include "net/proxy/proxy_service.h" | 17 #include "net/proxy/proxy_service.h" |
19 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
20 | 19 |
21 namespace data_reduction_proxy { | 20 namespace data_reduction_proxy { |
22 | 21 |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 DCHECK_EQ(length, list->GetSize()); | 186 DCHECK_EQ(length, list->GetSize()); |
188 } | 187 } |
189 | 188 |
190 // DailyContentLengthUpdate maintains a data saving pref. The pref is a list | 189 // DailyContentLengthUpdate maintains a data saving pref. The pref is a list |
191 // of |kNumDaysInHistory| elements of daily total content lengths for the past | 190 // of |kNumDaysInHistory| elements of daily total content lengths for the past |
192 // |kNumDaysInHistory| days. | 191 // |kNumDaysInHistory| days. |
193 class DailyContentLengthUpdate { | 192 class DailyContentLengthUpdate { |
194 public: | 193 public: |
195 DailyContentLengthUpdate( | 194 DailyContentLengthUpdate( |
196 const char* pref, | 195 const char* pref, |
197 PrefService* pref_service) | 196 DataReductionProxyStatisticsPrefs* pref_service) |
198 : update_(pref_service, pref) { | 197 : update_(pref_service->GetList(pref)) { |
199 } | 198 } |
200 | 199 |
201 void UpdateForDataChange(int days_since_last_update) { | 200 void UpdateForDataChange(int days_since_last_update) { |
202 // New empty lists may have been created. Maintain the invariant that | 201 // New empty lists may have been created. Maintain the invariant that |
203 // there should be exactly |kNumDaysInHistory| days in the histories. | 202 // there should be exactly |kNumDaysInHistory| days in the histories. |
204 MaintainContentLengthPrefsWindow(update_.Get(), kNumDaysInHistory); | 203 MaintainContentLengthPrefsWindow(update_, kNumDaysInHistory); |
205 if (days_since_last_update) { | 204 if (days_since_last_update) { |
206 MaintainContentLengthPrefForDateChange(days_since_last_update); | 205 MaintainContentLengthPrefForDateChange(days_since_last_update); |
207 } | 206 } |
208 } | 207 } |
209 | 208 |
210 // Update the lengths for the current day. | 209 // Update the lengths for the current day. |
211 void Add(int content_length) { | 210 void Add(int content_length) { |
212 AddInt64ToListPref(kNumDaysInHistory - 1, content_length, update_.Get()); | 211 AddInt64ToListPref(kNumDaysInHistory - 1, content_length, update_); |
213 } | 212 } |
214 | 213 |
215 int64 GetListPrefValue(size_t index) { | 214 int64 GetListPrefValue(size_t index) { |
216 return ListPrefInt64Value(*update_, index); | 215 return ListPrefInt64Value(*update_, index); |
217 } | 216 } |
218 | 217 |
219 private: | 218 private: |
220 // Update the list for date change and ensure the list has exactly |length| | 219 // Update the list for date change and ensure the list has exactly |length| |
221 // elements. The last entry in the list will be for the current day after | 220 // elements. The last entry in the list will be for the current day after |
222 // the update. | 221 // the update. |
(...skipping 18 matching lines...) Expand all Loading... |
241 // lists longer than kNumDaysInHistory. The additional items will be cut off | 240 // lists longer than kNumDaysInHistory. The additional items will be cut off |
242 // from the head of the lists by |MaintainContentLengthPrefsWindow|, below. | 241 // from the head of the lists by |MaintainContentLengthPrefsWindow|, below. |
243 for (int i = 0; | 242 for (int i = 0; |
244 i < days_since_last_update && i < static_cast<int>(kNumDaysInHistory); | 243 i < days_since_last_update && i < static_cast<int>(kNumDaysInHistory); |
245 ++i) { | 244 ++i) { |
246 update_->AppendString(base::Int64ToString(0)); | 245 update_->AppendString(base::Int64ToString(0)); |
247 } | 246 } |
248 | 247 |
249 // Entries for new days may have been appended. Maintain the invariant that | 248 // Entries for new days may have been appended. Maintain the invariant that |
250 // there should be exactly |kNumDaysInHistory| days in the histories. | 249 // there should be exactly |kNumDaysInHistory| days in the histories. |
251 MaintainContentLengthPrefsWindow(update_.Get(), kNumDaysInHistory); | 250 MaintainContentLengthPrefsWindow(update_, kNumDaysInHistory); |
252 } | 251 } |
253 | 252 |
254 ListPrefUpdate update_; | 253 base::ListValue* update_; |
255 }; | 254 }; |
256 | 255 |
257 // DailyDataSavingUpdate maintains a pair of data saving prefs, original_update_ | 256 // DailyDataSavingUpdate maintains a pair of data saving prefs, original_update_ |
258 // and received_update_. pref_original is a list of |kNumDaysInHistory| elements | 257 // and received_update_. pref_original is a list of |kNumDaysInHistory| elements |
259 // of daily total original content lengths for the past |kNumDaysInHistory| | 258 // of daily total original content lengths for the past |kNumDaysInHistory| |
260 // days. pref_received is the corresponding list of the daily total received | 259 // days. pref_received is the corresponding list of the daily total received |
261 // content lengths. | 260 // content lengths. |
262 class DailyDataSavingUpdate { | 261 class DailyDataSavingUpdate { |
263 public: | 262 public: |
264 DailyDataSavingUpdate( | 263 DailyDataSavingUpdate( |
265 const char* pref_original, | 264 const char* pref_original, |
266 const char* pref_received, | 265 const char* pref_received, |
267 PrefService* pref_service) | 266 DataReductionProxyStatisticsPrefs* prefs) |
268 : original_(pref_original, pref_service), | 267 : original_(pref_original, prefs), |
269 received_(pref_received, pref_service) { | 268 received_(pref_received, prefs) { |
270 } | 269 } |
271 | 270 |
272 void UpdateForDataChange(int days_since_last_update) { | 271 void UpdateForDataChange(int days_since_last_update) { |
273 original_.UpdateForDataChange(days_since_last_update); | 272 original_.UpdateForDataChange(days_since_last_update); |
274 received_.UpdateForDataChange(days_since_last_update); | 273 received_.UpdateForDataChange(days_since_last_update); |
275 } | 274 } |
276 | 275 |
277 // Update the lengths for the current day. | 276 // Update the lengths for the current day. |
278 void Add(int original_content_length, int received_content_length) { | 277 void Add(int original_content_length, int received_content_length) { |
279 original_.Add(original_content_length); | 278 original_.Add(original_content_length); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 return received_content_length; | 331 return received_content_length; |
333 } | 332 } |
334 return original_content_length; | 333 return original_content_length; |
335 } | 334 } |
336 | 335 |
337 void UpdateContentLengthPrefsForDataReductionProxy( | 336 void UpdateContentLengthPrefsForDataReductionProxy( |
338 int received_content_length, | 337 int received_content_length, |
339 int original_content_length, | 338 int original_content_length, |
340 bool with_data_reduction_proxy_enabled, | 339 bool with_data_reduction_proxy_enabled, |
341 DataReductionProxyRequestType request_type, | 340 DataReductionProxyRequestType request_type, |
342 base::Time now, PrefService* prefs) { | 341 base::Time now, |
| 342 DataReductionProxyStatisticsPrefs* prefs) { |
343 // TODO(bengr): Remove this check once the underlying cause of | 343 // TODO(bengr): Remove this check once the underlying cause of |
344 // http://crbug.com/287821 is fixed. For now, only continue if the current | 344 // http://crbug.com/287821 is fixed. For now, only continue if the current |
345 // year is reported as being between 1972 and 2970. | 345 // year is reported as being between 1972 and 2970. |
346 base::TimeDelta time_since_unix_epoch = now - base::Time::UnixEpoch(); | 346 base::TimeDelta time_since_unix_epoch = now - base::Time::UnixEpoch(); |
347 const int kMinDaysSinceUnixEpoch = 365 * 2; // 2 years. | 347 const int kMinDaysSinceUnixEpoch = 365 * 2; // 2 years. |
348 const int kMaxDaysSinceUnixEpoch = 365 * 1000; // 1000 years. | 348 const int kMaxDaysSinceUnixEpoch = 365 * 1000; // 1000 years. |
349 if (time_since_unix_epoch.InDays() < kMinDaysSinceUnixEpoch || | 349 if (time_since_unix_epoch.InDays() < kMinDaysSinceUnixEpoch || |
350 time_since_unix_epoch.InDays() > kMaxDaysSinceUnixEpoch) { | 350 time_since_unix_epoch.InDays() > kMaxDaysSinceUnixEpoch) { |
351 return; | 351 return; |
352 } | 352 } |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 unknown.GetListPrefValue(kNumDaysInHistory - 2)); | 471 unknown.GetListPrefValue(kNumDaysInHistory - 2)); |
472 } | 472 } |
473 } | 473 } |
474 } | 474 } |
475 | 475 |
476 void UpdateContentLengthPrefs( | 476 void UpdateContentLengthPrefs( |
477 int received_content_length, | 477 int received_content_length, |
478 int original_content_length, | 478 int original_content_length, |
479 bool with_data_reduction_proxy_enabled, | 479 bool with_data_reduction_proxy_enabled, |
480 DataReductionProxyRequestType request_type, | 480 DataReductionProxyRequestType request_type, |
481 PrefService* prefs) { | 481 DataReductionProxyStatisticsPrefs* prefs) { |
482 int64 total_received = prefs->GetInt64( | 482 int64 total_received = prefs->GetInt64( |
483 data_reduction_proxy::prefs::kHttpReceivedContentLength); | 483 data_reduction_proxy::prefs::kHttpReceivedContentLength); |
484 int64 total_original = prefs->GetInt64( | 484 int64 total_original = prefs->GetInt64( |
485 data_reduction_proxy::prefs::kHttpOriginalContentLength); | 485 data_reduction_proxy::prefs::kHttpOriginalContentLength); |
486 total_received += received_content_length; | 486 total_received += received_content_length; |
487 total_original += original_content_length; | 487 total_original += original_content_length; |
488 prefs->SetInt64(data_reduction_proxy::prefs::kHttpReceivedContentLength, | 488 prefs->SetInt64(data_reduction_proxy::prefs::kHttpReceivedContentLength, |
489 total_received); | 489 total_received); |
490 prefs->SetInt64(data_reduction_proxy::prefs::kHttpOriginalContentLength, | 490 prefs->SetInt64(data_reduction_proxy::prefs::kHttpOriginalContentLength, |
491 total_original); | 491 total_original); |
492 | 492 |
493 UpdateContentLengthPrefsForDataReductionProxy( | 493 UpdateContentLengthPrefsForDataReductionProxy( |
494 received_content_length, | 494 received_content_length, |
495 original_content_length, | 495 original_content_length, |
496 with_data_reduction_proxy_enabled, | 496 with_data_reduction_proxy_enabled, |
497 request_type, | 497 request_type, |
498 base::Time::Now(), | 498 base::Time::Now(), |
499 prefs); | 499 prefs); |
500 } | 500 } |
501 | 501 |
502 } // namespace data_reduction_proxy | 502 } // namespace data_reduction_proxy |
OLD | NEW |