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 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
186 list->AppendString(base::Int64ToString(0)); | 185 list->AppendString(base::Int64ToString(0)); |
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, |
bengr
2014/08/28 21:38:34
This can be moved up a line:
DailyContentLength
megjablon
2014/08/30 01:10:04
Done.
| |
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, |
bengr
2014/08/28 21:38:34
These can probably move up a line
megjablon
2014/08/30 01:10:04
Done.
| |
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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
333 return received_content_length; | 332 return received_content_length; |
334 } | 333 } |
335 return original_content_length; | 334 return original_content_length; |
336 } | 335 } |
337 | 336 |
338 void UpdateContentLengthPrefsForDataReductionProxy( | 337 void UpdateContentLengthPrefsForDataReductionProxy( |
339 int received_content_length, | 338 int received_content_length, |
340 int original_content_length, | 339 int original_content_length, |
341 bool with_data_reduction_proxy_enabled, | 340 bool with_data_reduction_proxy_enabled, |
342 DataReductionProxyRequestType request_type, | 341 DataReductionProxyRequestType request_type, |
343 base::Time now, PrefService* prefs) { | 342 base::Time now, |
343 DataReductionProxyStatisticsPrefs* prefs) { | |
344 // TODO(bengr): Remove this check once the underlying cause of | 344 // TODO(bengr): Remove this check once the underlying cause of |
345 // http://crbug.com/287821 is fixed. For now, only continue if the current | 345 // http://crbug.com/287821 is fixed. For now, only continue if the current |
346 // year is reported as being between 1972 and 2970. | 346 // year is reported as being between 1972 and 2970. |
347 base::TimeDelta time_since_unix_epoch = now - base::Time::UnixEpoch(); | 347 base::TimeDelta time_since_unix_epoch = now - base::Time::UnixEpoch(); |
348 const int kMinDaysSinceUnixEpoch = 365 * 2; // 2 years. | 348 const int kMinDaysSinceUnixEpoch = 365 * 2; // 2 years. |
349 const int kMaxDaysSinceUnixEpoch = 365 * 1000; // 1000 years. | 349 const int kMaxDaysSinceUnixEpoch = 365 * 1000; // 1000 years. |
350 if (time_since_unix_epoch.InDays() < kMinDaysSinceUnixEpoch || | 350 if (time_since_unix_epoch.InDays() < kMinDaysSinceUnixEpoch || |
351 time_since_unix_epoch.InDays() > kMaxDaysSinceUnixEpoch) { | 351 time_since_unix_epoch.InDays() > kMaxDaysSinceUnixEpoch) { |
352 return; | 352 return; |
353 } | 353 } |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
468 via_proxy.GetReceivedListPrefValue(kNumDaysInHistory - 2), | 468 via_proxy.GetReceivedListPrefValue(kNumDaysInHistory - 2), |
469 https.GetListPrefValue(kNumDaysInHistory - 2), | 469 https.GetListPrefValue(kNumDaysInHistory - 2), |
470 short_bypass.GetListPrefValue(kNumDaysInHistory - 2), | 470 short_bypass.GetListPrefValue(kNumDaysInHistory - 2), |
471 long_bypass.GetListPrefValue(kNumDaysInHistory - 2), | 471 long_bypass.GetListPrefValue(kNumDaysInHistory - 2), |
472 unknown.GetListPrefValue(kNumDaysInHistory - 2)); | 472 unknown.GetListPrefValue(kNumDaysInHistory - 2)); |
473 } | 473 } |
474 } | 474 } |
475 } | 475 } |
476 | 476 |
477 void UpdateContentLengthPrefs( | 477 void UpdateContentLengthPrefs( |
478 int received_content_length, | 478 int received_content_length, |
bengr
2014/08/28 21:38:34
This can probably be moved up a line.
megjablon
2014/08/30 01:10:04
Done.
| |
479 int original_content_length, | 479 int original_content_length, |
480 bool with_data_reduction_proxy_enabled, | 480 bool with_data_reduction_proxy_enabled, |
481 DataReductionProxyRequestType request_type, | 481 DataReductionProxyRequestType request_type, |
482 PrefService* prefs) { | 482 DataReductionProxyStatisticsPrefs* prefs) { |
483 int64 total_received = prefs->GetInt64( | 483 int64 total_received = prefs->GetInt64( |
484 data_reduction_proxy::prefs::kHttpReceivedContentLength); | 484 data_reduction_proxy::prefs::kHttpReceivedContentLength); |
485 int64 total_original = prefs->GetInt64( | 485 int64 total_original = prefs->GetInt64( |
486 data_reduction_proxy::prefs::kHttpOriginalContentLength); | 486 data_reduction_proxy::prefs::kHttpOriginalContentLength); |
487 total_received += received_content_length; | 487 total_received += received_content_length; |
488 total_original += original_content_length; | 488 total_original += original_content_length; |
489 prefs->SetInt64(data_reduction_proxy::prefs::kHttpReceivedContentLength, | 489 prefs->SetInt64(data_reduction_proxy::prefs::kHttpReceivedContentLength, |
490 total_received); | 490 total_received); |
491 prefs->SetInt64(data_reduction_proxy::prefs::kHttpOriginalContentLength, | 491 prefs->SetInt64(data_reduction_proxy::prefs::kHttpOriginalContentLength, |
492 total_original); | 492 total_original); |
493 | 493 |
494 UpdateContentLengthPrefsForDataReductionProxy( | 494 UpdateContentLengthPrefsForDataReductionProxy( |
495 received_content_length, | 495 received_content_length, |
496 original_content_length, | 496 original_content_length, |
497 with_data_reduction_proxy_enabled, | 497 with_data_reduction_proxy_enabled, |
498 request_type, | 498 request_type, |
499 base::Time::Now(), | 499 base::Time::Now(), |
500 prefs); | 500 prefs); |
501 } | 501 } |
502 | 502 |
503 } // namespace data_reduction_proxy | 503 } // namespace data_reduction_proxy |
OLD | NEW |