diff options
| author | jeremyim <jeremyim@chromium.org> | 2015-04-08 21:47:28 -0700 | 
|---|---|---|
| committer | Commit bot <commit-bot@chromium.org> | 2015-04-09 04:48:11 +0000 | 
| commit | d0856baaed6f3817a96c657e4eab4b201655bb3a (patch) | |
| tree | 7becc9451d05101abbcb9a5c2cafb0722006106c | |
| parent | 2da6f193662ce92fe80641b2ec595aa822d939e9 (diff) | |
| download | chromium_src-d0856baaed6f3817a96c657e4eab4b201655bb3a.zip chromium_src-d0856baaed6f3817a96c657e4eab4b201655bb3a.tar.gz chromium_src-d0856baaed6f3817a96c657e4eab4b201655bb3a.tar.bz2 | |
Update DataReductionProxyNetworkDelegate/BypassStats to post tasks safely.
- DRP NetworkDelegate/BypassStats are no longer aware of the UI thread.
- Add thread hopping functionality to DRP IOData/Service, using the
  correct WeakPtr to post tasks safely.
- Construct BypassStats in the IOData constructor.
BUG=472290
Review URL: https://codereview.chromium.org/1054363002
Cr-Commit-Position: refs/heads/master@{#324375}
25 files changed, 1280 insertions, 1427 deletions
| diff --git a/chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.cc b/chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.cc index 4b2ccb8..284294b 100644 --- a/chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.cc +++ b/chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.cc @@ -11,7 +11,7 @@  #include "chrome/browser/profiles/profile.h"  #include "chrome/browser/profiles/profile_manager.h"  #include "chrome/common/pref_names.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats.h" +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"  #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"  #include "jni/DataReductionProxySettings_jni.h" @@ -113,7 +113,7 @@ DataReductionProxySettingsAndroid::GetDailyContentLengths(    jlongArray result = env->NewLongArray(        data_reduction_proxy::kNumDaysInHistory); -  DataReductionProxySettings::ContentLengthList lengths  = + data_reduction_proxy::ContentLengthList lengths  =        Settings()->GetDailyContentLengths(pref_name);    if (!lengths.empty()) { diff --git a/components/data_reduction_proxy/core/browser/BUILD.gn b/components/data_reduction_proxy/core/browser/BUILD.gn index 5330d25..0f0489e 100644 --- a/components/data_reduction_proxy/core/browser/BUILD.gn +++ b/components/data_reduction_proxy/core/browser/BUILD.gn @@ -91,10 +91,12 @@ source_set("unit_tests") {    sources = [      "data_reduction_proxy_bypass_protocol_unittest.cc",      "data_reduction_proxy_bypass_stats_unittest.cc", +    "data_reduction_proxy_compression_stats_unittest.cc",      "data_reduction_proxy_config_service_client_unittest.cc",      "data_reduction_proxy_config_unittest.cc",      "data_reduction_proxy_configurator_unittest.cc",      "data_reduction_proxy_interceptor_unittest.cc", +    "data_reduction_proxy_io_data_unittest.cc",      "data_reduction_proxy_metrics_unittest.cc",      "data_reduction_proxy_network_delegate_unittest.cc",      "data_reduction_proxy_prefs_unittest.cc", diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol_unittest.cc index 6e56240..1753b38 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol_unittest.cc @@ -120,8 +120,7 @@ class DataReductionProxyProtocolTest : public testing::Test {      context_->set_http_user_agent_settings(&http_user_agent_settings_);      bypass_stats_.reset(new DataReductionProxyBypassStats(          test_context_->config(), -        test_context_->unreachable_callback(), -        test_context_->task_runner())); +        test_context_->unreachable_callback()));      DataReductionProxyInterceptor* interceptor =          new DataReductionProxyInterceptor(test_context_->config(), diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats.cc index cd4ce09..87e94d2 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats.cc @@ -4,12 +4,10 @@  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats.h" -#include "base/bind.h"  #include "base/callback.h"  #include "base/metrics/histogram.h"  #include "base/metrics/sparse_histogram.h"  #include "base/prefs/pref_member.h" -#include "base/single_thread_task_runner.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_tamper_detection.h"  #include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h" @@ -104,28 +102,24 @@ void DataReductionProxyBypassStats::DetectAndRecordMissingViaHeaderResponseCode(  DataReductionProxyBypassStats::DataReductionProxyBypassStats(      DataReductionProxyConfig* config, -    UnreachableCallback unreachable_callback, -    const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) +    UnreachableCallback unreachable_callback)      : data_reduction_proxy_config_(config),        unreachable_callback_(unreachable_callback),        last_bypass_type_(BYPASS_EVENT_TYPE_MAX),        triggering_request_(true), -      ui_task_runner_(ui_task_runner),        successful_requests_through_proxy_count_(0),        proxy_net_errors_count_(0),        unavailable_(false) {    DCHECK(config);    NetworkChangeNotifier::AddNetworkChangeObserver(this); -}; +}  DataReductionProxyBypassStats::~DataReductionProxyBypassStats() {    NetworkChangeNotifier::RemoveNetworkChangeObserver(this); -}; +}  void DataReductionProxyBypassStats::OnUrlRequestCompleted(      const net::URLRequest* request, bool started) { -  DCHECK(thread_checker_.CalledOnValidThread()); -    DataReductionProxyTypeInfo proxy_info;    // Ignore requests that did not use the data reduction proxy. The check for    // LOAD_BYPASS_PROXY is necessary because the proxy_server() in the |request| @@ -369,12 +363,10 @@ void DataReductionProxyBypassStats::RecordMissingViaHeaderBytes(  void DataReductionProxyBypassStats::OnNetworkChanged(      NetworkChangeNotifier::ConnectionType type) { -  DCHECK(thread_checker_.CalledOnValidThread());    ClearRequestCounts();  }  void DataReductionProxyBypassStats::ClearRequestCounts() { -  DCHECK(thread_checker_.CalledOnValidThread());    successful_requests_through_proxy_count_ = 0;    proxy_net_errors_count_ = 0;  } @@ -385,18 +377,8 @@ void DataReductionProxyBypassStats::NotifyUnavailabilityIfChanged() {        (proxy_net_errors_count_ >= kMinFailedRequestsWhenUnavailable &&         successful_requests_through_proxy_count_ <=             kMaxSuccessfulRequestsWhenUnavailable); -  if (prev_unavailable != unavailable_) { -    ui_task_runner_->PostTask(FROM_HERE, base::Bind( -        &DataReductionProxyBypassStats::NotifyUnavailabilityOnUIThread, -        base::Unretained(this), -        unavailable_)); -  } -} - -void DataReductionProxyBypassStats::NotifyUnavailabilityOnUIThread( -    bool unavailable) { -  DCHECK(ui_task_runner_->BelongsToCurrentThread()); -  unreachable_callback_.Run(unavailable); +  if (prev_unavailable != unavailable_) +    unreachable_callback_.Run(unavailable_);  }  void DataReductionProxyBypassStats::RecordBypassedBytes( diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats.h index 08a7930..efff8d1 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats.h +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats.h @@ -7,8 +7,6 @@  #include "base/callback.h"  #include "base/prefs/pref_member.h" -#include "base/single_thread_task_runner.h" -#include "base/threading/thread_checker.h"  #include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h"  #include "net/base/host_port_pair.h"  #include "net/base/network_change_notifier.h" @@ -45,14 +43,12 @@ class DataReductionProxyBypassStats        bool is_primary,        const net::HttpResponseHeaders* headers); -  // |params| outlives this class instance. |unreachable_callback| provides a -  // hook to inform the user that the Data Reduction Proxy is unreachable, which -  // occurs on the UI thread, hence the |ui_task_runner|. +  // |config| outlives this class instance. |unreachable_callback| provides a +  // hook to inform the user that the Data Reduction Proxy is unreachable.    // |config| must not be null.    DataReductionProxyBypassStats(        DataReductionProxyConfig* config, -      UnreachableCallback unreachable_callback, -      const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner); +      UnreachableCallback unreachable_callback);    ~DataReductionProxyBypassStats() override; @@ -145,7 +141,6 @@ class DataReductionProxyBypassStats    DataReductionProxyBypassType last_bypass_type_;    // True if the last request triggered the current bypass.    bool triggering_request_; -  const scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;    // The following 2 fields are used to determine if data reduction proxy is    // unreachable. We keep a count of requests which should go through @@ -163,8 +158,6 @@ class DataReductionProxyBypassStats    // Whether or not the data reduction proxy is unavailable.    bool unavailable_; -  base::ThreadChecker thread_checker_; -    DISALLOW_COPY_AND_ASSIGN(DataReductionProxyBypassStats);  }; diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats_unittest.cc index c0a4741..cc1930e 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats_unittest.cc @@ -114,8 +114,7 @@ class DataReductionProxyBypassStatsTest : public testing::Test {      return make_scoped_ptr(          new DataReductionProxyBypassStats(              test_context_->config(), -            test_context_->unreachable_callback(), -            test_context_->task_runner())).Pass(); +            test_context_->unreachable_callback()));    }    net::URLRequest* url_request() { diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.cc index c547e44..badf953 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.cc @@ -4,10 +4,14 @@  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h" +#include <vector> + +#include "base/basictypes.h"  #include "base/bind.h"  #include "base/command_line.h"  #include "base/location.h"  #include "base/logging.h" +#include "base/metrics/histogram.h"  #include "base/prefs/pref_change_registrar.h"  #include "base/prefs/pref_service.h"  #include "base/prefs/scoped_user_pref_update.h" @@ -15,10 +19,287 @@  #include "base/strings/string_number_conversions.h"  #include "base/time/time.h"  #include "base/values.h" +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h"  #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switches.h"  namespace data_reduction_proxy { +namespace { + +// Returns the value at |index| of |list_value| as an int64. +int64 GetInt64PrefValue(const base::ListValue& list_value, size_t index) { +  int64 val = 0; +  std::string pref_value; +  bool rv = list_value.GetString(index, &pref_value); +  DCHECK(rv); +  if (rv) { +    rv = base::StringToInt64(pref_value, &val); +    DCHECK(rv); +  } +  return val; +} + +// Ensure list has exactly |length| elements, either by truncating at the +// front, or appending "0"'s to the back. +void MaintainContentLengthPrefsWindow(base::ListValue* list, size_t length) { +  // Remove data for old days from the front. +  while (list->GetSize() > length) +    list->Remove(0, NULL); +  // Newly added lists are empty. Add entries to back to fill the window, +  // each initialized to zero. +  while (list->GetSize() < length) +    list->AppendString(base::Int64ToString(0)); +  DCHECK_EQ(length, list->GetSize()); +} + +// Increments an int64, stored as a string, in a ListPref at the specified +// index.  The value must already exist and be a string representation of a +// number. +void AddInt64ToListPref(size_t index, +                        int64 length, +                        base::ListValue* list_update) { +  int64 value = 0; +  std::string old_string_value; +  bool rv = list_update->GetString(index, &old_string_value); +  DCHECK(rv); +  if (rv) { +    rv = base::StringToInt64(old_string_value, &value); +    DCHECK(rv); +  } +  value += length; +  list_update->Set(index, new base::StringValue(base::Int64ToString(value))); +} + +int64 ListPrefInt64Value(const base::ListValue& list_update, size_t index) { +  std::string string_value; +  if (!list_update.GetString(index, &string_value)) { +    NOTREACHED(); +    return 0; +  } + +  int64 value = 0; +  bool rv = base::StringToInt64(string_value, &value); +  DCHECK(rv); +  return value; +} + +// DailyContentLengthUpdate maintains a data saving pref. The pref is a list +// of |kNumDaysInHistory| elements of daily total content lengths for the past +// |kNumDaysInHistory| days. +void RecordDailyContentLengthHistograms( +    int64 original_length, +    int64 received_length, +    int64 original_length_with_data_reduction_enabled, +    int64 received_length_with_data_reduction_enabled, +    int64 original_length_via_data_reduction_proxy, +    int64 received_length_via_data_reduction_proxy, +    int64 https_length_with_data_reduction_enabled, +    int64 short_bypass_length_with_data_reduction_enabled, +    int64 long_bypass_length_with_data_reduction_enabled, +    int64 unknown_length_with_data_reduction_enabled) { +  // Report daily UMA only for days having received content. +  if (original_length <= 0 || received_length <= 0) +    return; + +  // Record metrics in KB. +  UMA_HISTOGRAM_COUNTS( +      "Net.DailyOriginalContentLength", original_length >> 10); +  UMA_HISTOGRAM_COUNTS( +      "Net.DailyContentLength", received_length >> 10); +  int percent = 0; +  // UMA percentage cannot be negative. +  if (original_length > received_length) { +    percent = (100 * (original_length - received_length)) / original_length; +  } +  UMA_HISTOGRAM_PERCENTAGE("Net.DailyContentSavingPercent", percent); + +  if (original_length_with_data_reduction_enabled <= 0 || +      received_length_with_data_reduction_enabled <= 0) { +    return; +  } + +  UMA_HISTOGRAM_COUNTS( +      "Net.DailyOriginalContentLength_DataReductionProxyEnabled", +      original_length_with_data_reduction_enabled >> 10); +  UMA_HISTOGRAM_COUNTS( +      "Net.DailyContentLength_DataReductionProxyEnabled", +      received_length_with_data_reduction_enabled >> 10); + +  int percent_data_reduction_proxy_enabled = 0; +  // UMA percentage cannot be negative. +  if (original_length_with_data_reduction_enabled > +      received_length_with_data_reduction_enabled) { +    percent_data_reduction_proxy_enabled = +        100 * (original_length_with_data_reduction_enabled - +               received_length_with_data_reduction_enabled) / +        original_length_with_data_reduction_enabled; +  } +  UMA_HISTOGRAM_PERCENTAGE( +      "Net.DailyContentSavingPercent_DataReductionProxyEnabled", +      percent_data_reduction_proxy_enabled); + +  UMA_HISTOGRAM_PERCENTAGE( +      "Net.DailyContentPercent_DataReductionProxyEnabled", +      (100 * received_length_with_data_reduction_enabled) / received_length); + +  DCHECK_GE(https_length_with_data_reduction_enabled, 0); +  UMA_HISTOGRAM_COUNTS( +      "Net.DailyContentLength_DataReductionProxyEnabled_Https", +      https_length_with_data_reduction_enabled >> 10); +  UMA_HISTOGRAM_PERCENTAGE( +      "Net.DailyContentPercent_DataReductionProxyEnabled_Https", +      (100 * https_length_with_data_reduction_enabled) / received_length); + +  DCHECK_GE(short_bypass_length_with_data_reduction_enabled, 0); +  UMA_HISTOGRAM_COUNTS( +      "Net.DailyContentLength_DataReductionProxyEnabled_ShortBypass", +      short_bypass_length_with_data_reduction_enabled >> 10); +  UMA_HISTOGRAM_PERCENTAGE( +      "Net.DailyContentPercent_DataReductionProxyEnabled_ShortBypass", +      ((100 * short_bypass_length_with_data_reduction_enabled) / +       received_length)); + +  DCHECK_GE(long_bypass_length_with_data_reduction_enabled, 0); +  UMA_HISTOGRAM_COUNTS( +      "Net.DailyContentLength_DataReductionProxyEnabled_LongBypass", +      long_bypass_length_with_data_reduction_enabled >> 10); +  UMA_HISTOGRAM_PERCENTAGE( +      "Net.DailyContentPercent_DataReductionProxyEnabled_LongBypass", +      ((100 * long_bypass_length_with_data_reduction_enabled) / +       received_length)); + +  DCHECK_GE(unknown_length_with_data_reduction_enabled, 0); +  UMA_HISTOGRAM_COUNTS( +      "Net.DailyContentLength_DataReductionProxyEnabled_Unknown", +      unknown_length_with_data_reduction_enabled >> 10); +  UMA_HISTOGRAM_PERCENTAGE( +      "Net.DailyContentPercent_DataReductionProxyEnabled_Unknown", +      ((100 * unknown_length_with_data_reduction_enabled) / +       received_length)); + +  DCHECK_GE(original_length_via_data_reduction_proxy, 0); +  UMA_HISTOGRAM_COUNTS( +      "Net.DailyOriginalContentLength_ViaDataReductionProxy", +      original_length_via_data_reduction_proxy >> 10); +  DCHECK_GE(received_length_via_data_reduction_proxy, 0); +  UMA_HISTOGRAM_COUNTS( +      "Net.DailyContentLength_ViaDataReductionProxy", +      received_length_via_data_reduction_proxy >> 10); +  int percent_via_data_reduction_proxy = 0; +  if (original_length_via_data_reduction_proxy > +      received_length_via_data_reduction_proxy) { +    percent_via_data_reduction_proxy = +        100 * (original_length_via_data_reduction_proxy - +               received_length_via_data_reduction_proxy) / +        original_length_via_data_reduction_proxy; +  } +  UMA_HISTOGRAM_PERCENTAGE( +      "Net.DailyContentSavingPercent_ViaDataReductionProxy", +      percent_via_data_reduction_proxy); +  UMA_HISTOGRAM_PERCENTAGE( +      "Net.DailyContentPercent_ViaDataReductionProxy", +      (100 * received_length_via_data_reduction_proxy) / received_length); +} + +class DailyContentLengthUpdate { + public: +  DailyContentLengthUpdate(base::ListValue* update) +      : update_(update) {} + +  void UpdateForDataChange(int days_since_last_update) { +    // New empty lists may have been created. Maintain the invariant that +    // there should be exactly |kNumDaysInHistory| days in the histories. +    MaintainContentLengthPrefsWindow(update_, kNumDaysInHistory); +    if (days_since_last_update) { +      MaintainContentLengthPrefForDateChange(days_since_last_update); +    } +  } + +  // Update the lengths for the current day. +  void Add(int content_length) { +    AddInt64ToListPref(kNumDaysInHistory - 1, content_length, update_); +  } + +  int64 GetListPrefValue(size_t index) { +    return ListPrefInt64Value(*update_, index); +  } + + private: +  // Update the list for date change and ensure the list has exactly |length| +  // elements. The last entry in the list will be for the current day after +  // the update. +  void MaintainContentLengthPrefForDateChange(int days_since_last_update) { +    if (days_since_last_update == -1) { +      // The system may go backwards in time by up to a day for legitimate +      // reasons, such as with changes to the time zone. In such cases, we +      // keep adding to the current day. +      // Note: we accept the fact that some reported data is shifted to +      // the adjacent day if users travel back and forth across time zones. +      days_since_last_update = 0; +    } else if (days_since_last_update < -1) { +      // Erase all entries if the system went backwards in time by more than +      // a day. +      update_->Clear(); + +      days_since_last_update = kNumDaysInHistory; +    } +    DCHECK_GE(days_since_last_update, 0); + +    // Add entries for days since last update event. This will make the +    // lists longer than kNumDaysInHistory. The additional items will be cut off +    // from the head of the lists by |MaintainContentLengthPrefsWindow|, below. +    for (int i = 0; +         i < days_since_last_update && i < static_cast<int>(kNumDaysInHistory); +         ++i) { +      update_->AppendString(base::Int64ToString(0)); +    } + +    // Entries for new days may have been appended. Maintain the invariant that +    // there should be exactly |kNumDaysInHistory| days in the histories. +    MaintainContentLengthPrefsWindow(update_, kNumDaysInHistory); +  } + +  base::ListValue* update_; +}; + +// DailyDataSavingUpdate maintains a pair of data saving prefs, original_update_ +// and received_update_. pref_original is a list of |kNumDaysInHistory| elements +// of daily total original content lengths for the past |kNumDaysInHistory| +// days. pref_received is the corresponding list of the daily total received +// content lengths. +class DailyDataSavingUpdate { + public: +  DailyDataSavingUpdate(base::ListValue* original, +                        base::ListValue* received) +      : original_(original), +        received_(received) {} + +  void UpdateForDataChange(int days_since_last_update) { +    original_.UpdateForDataChange(days_since_last_update); +    received_.UpdateForDataChange(days_since_last_update); +  } + +  // Update the lengths for the current day. +  void Add(int original_content_length, int received_content_length) { +    original_.Add(original_content_length); +    received_.Add(received_content_length); +  } + +  int64 GetOriginalListPrefValue(size_t index) { +    return original_.GetListPrefValue(index); +  } +  int64 GetReceivedListPrefValue(size_t index) { +    return received_.GetListPrefValue(index); +  } + + private: +  DailyContentLengthUpdate original_; +  DailyContentLengthUpdate received_; +}; + +// Report UMA metrics for daily data reductions. +}  // namespace +  DataReductionProxyCompressionStats::DataReductionProxyCompressionStats(      PrefService* prefs,      scoped_refptr<base::SequencedTaskRunner> task_runner, @@ -73,7 +354,7 @@ void DataReductionProxyCompressionStats::Init() {    pref_change_registrar_->Init(pref_service_);    pref_change_registrar_->Add(prefs::kUpdateDailyReceivedContentLengths,        base::Bind(&DataReductionProxyCompressionStats::OnUpdateContentLengths, -                 GetWeakPtr())); +                 weak_factory_.GetWeakPtr()));  }  void DataReductionProxyCompressionStats::OnUpdateContentLengths() { @@ -85,6 +366,31 @@ void DataReductionProxyCompressionStats::OnUpdateContentLengths() {    pref_service_->SetBoolean(prefs::kUpdateDailyReceivedContentLengths, false);  } +void DataReductionProxyCompressionStats::UpdateContentLengths( +    int received_content_length, +    int original_content_length, +    bool data_reduction_proxy_enabled, +    DataReductionProxyRequestType request_type) { +  DCHECK(thread_checker_.CalledOnValidThread()); +  int64 total_received = GetInt64( +      data_reduction_proxy::prefs::kHttpReceivedContentLength); +  int64 total_original = GetInt64( +      data_reduction_proxy::prefs::kHttpOriginalContentLength); +  total_received += received_content_length; +  total_original += original_content_length; +  SetInt64(data_reduction_proxy::prefs::kHttpReceivedContentLength, +           total_received); +  SetInt64(data_reduction_proxy::prefs::kHttpOriginalContentLength, +           total_original); + +  RecordContentLengthPrefs( +      received_content_length, +      original_content_length, +      data_reduction_proxy_enabled, +      request_type, +      base::Time::Now()); +} +  void DataReductionProxyCompressionStats::InitInt64Pref(const char* pref) {    int64 pref_value = pref_service_->GetInt64(pref);    pref_map_[pref] = pref_value; @@ -158,6 +464,70 @@ DataReductionProxyCompressionStats::HistoricNetworkStatsInfoToValue() {    return dict;  } +int64 DataReductionProxyCompressionStats::GetLastUpdateTime() { +  int64 last_update_internal = GetInt64( +      prefs::kDailyHttpContentLengthLastUpdateDate); +  base::Time last_update = base::Time::FromInternalValue(last_update_internal); +  return static_cast<int64>(last_update.ToJsTime()); +} + +void DataReductionProxyCompressionStats::ResetStatistics() { +  base::ListValue* original_update = +      GetList(prefs::kDailyHttpOriginalContentLength); +  base::ListValue* received_update = +      GetList(prefs::kDailyHttpReceivedContentLength); +  original_update->Clear(); +  received_update->Clear(); +  for (size_t i = 0; i < kNumDaysInHistory; ++i) { +    original_update->AppendString(base::Int64ToString(0)); +    received_update->AppendString(base::Int64ToString(0)); +  } +} + +ContentLengthList DataReductionProxyCompressionStats::GetDailyContentLengths( +    const char* pref_name) { +  ContentLengthList content_lengths; +  const base::ListValue* list_value = GetList(pref_name); +  if (list_value->GetSize() == kNumDaysInHistory) { +    for (size_t i = 0; i < kNumDaysInHistory; ++i) +      content_lengths.push_back(GetInt64PrefValue(*list_value, i)); +  } +  return content_lengths; +} + +void DataReductionProxyCompressionStats::GetContentLengths( +    unsigned int days, +    int64* original_content_length, +    int64* received_content_length, +    int64* last_update_time) { +  DCHECK_LE(days, kNumDaysInHistory); + +  const base::ListValue* original_list = +      GetList(prefs::kDailyHttpOriginalContentLength); +  const base::ListValue* received_list = +      GetList(prefs::kDailyHttpReceivedContentLength); + +  if (original_list->GetSize() != kNumDaysInHistory || +      received_list->GetSize() != kNumDaysInHistory) { +    *original_content_length = 0L; +    *received_content_length = 0L; +    *last_update_time = 0L; +    return; +  } + +  int64 orig = 0L; +  int64 recv = 0L; +  // Include days from the end of the list going backwards. +  for (size_t i = kNumDaysInHistory - days; +       i < kNumDaysInHistory; ++i) { +    orig += GetInt64PrefValue(*original_list, i); +    recv += GetInt64PrefValue(*received_list, i); +  } +  *original_content_length = orig; +  *received_content_length = recv; +  *last_update_time = GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate); +} +  void DataReductionProxyCompressionStats::ClearDataSavingStatistics() {    list_pref_map_.get(        prefs::kDailyContentLengthHttpsWithDataReductionProxyEnabled)->Clear(); @@ -191,7 +561,8 @@ void DataReductionProxyCompressionStats::DelayedWritePrefs() {    task_runner_->PostDelayedTask(        FROM_HERE, -      base::Bind(&DataReductionProxyCompressionStats::WritePrefs, GetWeakPtr()), +      base::Bind(&DataReductionProxyCompressionStats::WritePrefs, +                 weak_factory_.GetWeakPtr()),        delay_);    delayed_task_posted_ = true; @@ -222,9 +593,132 @@ int64 DataReductionProxyCompressionStats::GetListPrefInt64Value(    return value;  } -base::WeakPtr<DataReductionProxyCompressionStats> -DataReductionProxyCompressionStats::GetWeakPtr() { -  return weak_factory_.GetWeakPtr(); +void DataReductionProxyCompressionStats::RecordContentLengthPrefs( +      int received_content_length, +      int original_content_length, +      bool with_data_reduction_proxy_enabled, +      DataReductionProxyRequestType request_type, +      base::Time now) { +  // TODO(bengr): Remove this check once the underlying cause of +  // http://crbug.com/287821 is fixed. For now, only continue if the current +  // year is reported as being between 1972 and 2970. +  base::TimeDelta time_since_unix_epoch = now - base::Time::UnixEpoch(); +  const int kMinDaysSinceUnixEpoch = 365 * 2;  // 2 years. +  const int kMaxDaysSinceUnixEpoch = 365 * 1000;  // 1000 years. +  if (time_since_unix_epoch.InDays() < kMinDaysSinceUnixEpoch || +      time_since_unix_epoch.InDays() > kMaxDaysSinceUnixEpoch) { +    return; +  } + +  // Determine how many days it has been since the last update. +  int64 then_internal = GetInt64( +      data_reduction_proxy::prefs::kDailyHttpContentLengthLastUpdateDate); + +  // Local midnight could have been shifted due to time zone change. +  // If time is null then don't care if midnight will be wrong shifted due to +  // time zone change because it's still too much time ago. +  base::Time then_midnight = base::Time::FromInternalValue(then_internal); +  if (!then_midnight.is_null()) { +    then_midnight = then_midnight.LocalMidnight(); +  } +  base::Time midnight = now.LocalMidnight(); + +  int days_since_last_update = (midnight - then_midnight).InDays(); + +  // Each day, we calculate the total number of bytes received and the total +  // size of all corresponding resources before any data-reducing recompression +  // is applied. These values are used to compute the data savings realized +  // by applying our compression techniques. Totals for the last +  // |kNumDaysInHistory| days are maintained. +  DailyDataSavingUpdate total( +      GetList(data_reduction_proxy::prefs::kDailyHttpOriginalContentLength), +      GetList(data_reduction_proxy::prefs::kDailyHttpReceivedContentLength)); +  total.UpdateForDataChange(days_since_last_update); + +  DailyDataSavingUpdate proxy_enabled( +      GetList(data_reduction_proxy::prefs:: +          kDailyOriginalContentLengthWithDataReductionProxyEnabled), +      GetList(data_reduction_proxy::prefs:: +          kDailyContentLengthWithDataReductionProxyEnabled)); +  proxy_enabled.UpdateForDataChange(days_since_last_update); + +  DailyDataSavingUpdate via_proxy( +      GetList(data_reduction_proxy::prefs:: +          kDailyOriginalContentLengthViaDataReductionProxy), +      GetList(data_reduction_proxy::prefs:: +          kDailyContentLengthViaDataReductionProxy)); +  via_proxy.UpdateForDataChange(days_since_last_update); + +  DailyContentLengthUpdate https( +      GetList(data_reduction_proxy::prefs:: +          kDailyContentLengthHttpsWithDataReductionProxyEnabled)); +  https.UpdateForDataChange(days_since_last_update); + +  DailyContentLengthUpdate short_bypass( +      GetList(data_reduction_proxy::prefs:: +          kDailyContentLengthShortBypassWithDataReductionProxyEnabled)); +  short_bypass.UpdateForDataChange(days_since_last_update); + +  DailyContentLengthUpdate long_bypass( +      GetList(data_reduction_proxy::prefs:: +          kDailyContentLengthLongBypassWithDataReductionProxyEnabled)); +  long_bypass.UpdateForDataChange(days_since_last_update); + +  DailyContentLengthUpdate unknown( +      GetList(data_reduction_proxy::prefs:: +          kDailyContentLengthUnknownWithDataReductionProxyEnabled)); +  unknown.UpdateForDataChange(days_since_last_update); + +  total.Add(original_content_length, received_content_length); +  if (with_data_reduction_proxy_enabled) { +    proxy_enabled.Add(original_content_length, received_content_length); +    // Ignore data source cases, if exist, when +    // "with_data_reduction_proxy_enabled == false" +    switch (request_type) { +      case VIA_DATA_REDUCTION_PROXY: +        via_proxy.Add(original_content_length, received_content_length); +        break; +      case HTTPS: +        https.Add(received_content_length); +        break; +      case SHORT_BYPASS: +        short_bypass.Add(received_content_length); +        break; +      case LONG_BYPASS: +        long_bypass.Add(received_content_length); +        break; +      case UNKNOWN_TYPE: +        unknown.Add(received_content_length); +        break; +    } +  } + +  if (days_since_last_update) { +    // Record the last update time in microseconds in UTC. +    SetInt64( +        data_reduction_proxy::prefs::kDailyHttpContentLengthLastUpdateDate, +        midnight.ToInternalValue()); + +    // A new day. Report the previous day's data if exists. We'll lose usage +    // data if the last time Chrome was run was more than a day ago. +    // Here, we prefer collecting less data but the collected data is +    // associated with an accurate date. +    if (days_since_last_update == 1) { +      // The previous day's data point is the second one from the tail. +      // Therefore (kNumDaysInHistory - 2) below. +      RecordDailyContentLengthHistograms( +          total.GetOriginalListPrefValue(kNumDaysInHistory - 2), +          total.GetReceivedListPrefValue(kNumDaysInHistory - 2), +          proxy_enabled.GetOriginalListPrefValue(kNumDaysInHistory - 2), +          proxy_enabled.GetReceivedListPrefValue(kNumDaysInHistory - 2), +          via_proxy.GetOriginalListPrefValue(kNumDaysInHistory - 2), +          via_proxy.GetReceivedListPrefValue(kNumDaysInHistory - 2), +          https.GetListPrefValue(kNumDaysInHistory - 2), +          short_bypass.GetListPrefValue(kNumDaysInHistory - 2), +          long_bypass.GetListPrefValue(kNumDaysInHistory - 2), +          unknown.GetListPrefValue(kNumDaysInHistory - 2)); +    } +  }  }  }  // namespace data_reduction_proxy diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h index 1990295..297f810 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h @@ -13,6 +13,7 @@  #include "base/memory/weak_ptr.h"  #include "base/threading/thread_checker.h"  #include "base/time/time.h" +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h"  #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"  class PrefChangeRegistrar; @@ -44,41 +45,49 @@ class DataReductionProxyCompressionStats {        const base::TimeDelta& delay);    ~DataReductionProxyCompressionStats(); -  // Loads all data_reduction_proxy::prefs into the |pref_map_| and -  // |list_pref_map_|. -  void Init(); - -  void OnUpdateContentLengths(); - -  // Gets the int64 pref at |pref_path| from the |DataReductionProxyPrefMap|. -  int64 GetInt64(const char* pref_path); - -  // Updates the pref value in the |DataReductionProxyPrefMap| map. -  // The pref is later written to |pref service_|. -  void SetInt64(const char* pref_path, int64 pref_value); - -  // Gets the pref list at |pref_path| from the |DataReductionProxyPrefMap|. -  base::ListValue* GetList(const char* pref_path); - -  // Writes the prefs stored in |DataReductionProxyPrefMap| and -  // |DataReductionProxyListPrefMap| to |pref_service|. -  void WritePrefs(); +  // Records daily data savings statistics to prefs and reports data savings +  // UMA. +  void UpdateContentLengths(int received_content_length, +                            int original_content_length, +                            bool data_reduction_proxy_enabled, +                            DataReductionProxyRequestType request_type);    // Creates a |Value| summary of the persistent state of the network session.    // The caller is responsible for deleting the returned value.    // Must be called on the UI thread.    base::Value* HistoricNetworkStatsInfoToValue(); +  // Returns the time in milliseconds since epoch that the last update was made +  // to the daily original and received content lengths. +  int64 GetLastUpdateTime(); + +  // Resets daily content length statistics. +  void ResetStatistics(); +    // Clears all data saving statistics.    void ClearDataSavingStatistics(); -  base::WeakPtr<DataReductionProxyCompressionStats> GetWeakPtr(); +  // Returns a list of all the daily content lengths. +  ContentLengthList GetDailyContentLengths(const char* pref_name); + +  // Returns aggregate received and original content lengths over the specified +  // number of days, as well as the time these stats were last updated. +  void GetContentLengths(unsigned int days, +                         int64* original_content_length, +                         int64* received_content_length, +                         int64* last_update_time);   private: +  friend class DataReductionProxyCompressionStatsTest; +    typedef std::map<const char*, int64> DataReductionProxyPrefMap;    typedef base::ScopedPtrHashMap<const char*, base::ListValue>        DataReductionProxyListPrefMap; +  // Loads all data_reduction_proxy::prefs into the |pref_map_| and +  // |list_pref_map_|. +  void Init(); +    // Gets the value of |pref| from the pref service and adds it to the    // |pref_map|.    void InitInt64Pref(const char* pref); @@ -87,6 +96,22 @@ class DataReductionProxyCompressionStats {    // |list_pref_map|.    void InitListPref(const char* pref); +  void OnUpdateContentLengths(); + +  // Gets the int64 pref at |pref_path| from the |DataReductionProxyPrefMap|. +  int64 GetInt64(const char* pref_path); + +  // Updates the pref value in the |DataReductionProxyPrefMap| map. +  // The pref is later written to |pref service_|. +  void SetInt64(const char* pref_path, int64 pref_value); + +  // Gets the pref list at |pref_path| from the |DataReductionProxyPrefMap|. +  base::ListValue* GetList(const char* pref_path); + +  // Writes the prefs stored in |DataReductionProxyPrefMap| and +  // |DataReductionProxyListPrefMap| to |pref_service|. +  void WritePrefs(); +    // Writes the stored prefs to |pref_service| and then posts another a delayed    // task to write prefs again in |kMinutesBetweenWrites|.    void DelayedWritePrefs(); @@ -100,6 +125,14 @@ class DataReductionProxyCompressionStats {    // index.    int64 GetListPrefInt64Value(const base::ListValue& list_update, size_t index); +  // Records content length updates to prefs. +  void RecordContentLengthPrefs( +      int received_content_length, +      int original_content_length, +      bool with_data_reduction_proxy_enabled, +      DataReductionProxyRequestType request_type, +      base::Time now); +    PrefService* pref_service_;    scoped_refptr<base::SequencedTaskRunner> task_runner_;    const base::TimeDelta delay_; diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats_unittest.cc index 6bdaba8..87e92e6 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats_unittest.cc @@ -3,8 +3,10 @@  // found in the LICENSE file.  #include "base/command_line.h" +#include "base/memory/ref_counted.h"  #include "base/memory/scoped_ptr.h"  #include "base/prefs/pref_registry_simple.h" +#include "base/prefs/pref_service.h"  #include "base/prefs/testing_pref_service.h"  #include "base/strings/string_number_conversions.h"  #include "base/test/test_simple_task_runner.h" @@ -18,8 +20,6 @@  namespace { -// TODO Make kNumDaysInHistory accessible from DataReductionProxySettings. -const size_t kNumDaysInHistory = 60;  const int kWriteDelayMinutes = 60;  int64 GetListPrefInt64Value( @@ -36,13 +36,42 @@ int64 GetListPrefInt64Value(  namespace data_reduction_proxy { +// The initial last update time used in test. There is no leap second a few +// days around this time used in the test. +// Note: No time zone is specified. Local time will be assumed by +// base::Time::FromString below. +const char kLastUpdateTime[] = "Wed, 18 Sep 2013 03:45:26"; +  class DataReductionProxyCompressionStatsTest : public testing::Test {   protected:    DataReductionProxyCompressionStatsTest()        : task_runner_(scoped_refptr<base::TestSimpleTaskRunner>( -            new base::TestSimpleTaskRunner())) {} +          new base::TestSimpleTaskRunner())) { +    base::Time::FromString(kLastUpdateTime, &now_); +  } + +  void SetUp() override { +    compression_stats_.reset(new DataReductionProxyCompressionStats( +        pref_service(), task_runner_, base::TimeDelta())); +    RegisterSimpleProfilePrefs(pref_service()->registry()); +  } + +  void ResetCompressionStatsWithDelay(const base::TimeDelta& delay) { +    compression_stats_.reset(new DataReductionProxyCompressionStats( +        pref_service(), task_runner_, delay)); +  } + +  base::Time FakeNow() const { +    return now_ + now_delta_; +  } -  void SetUp() override { RegisterPrefs(simple_pref_service_.registry()); } +  void SetFakeTimeDeltaInHours(int hours) { +    now_delta_ = base::TimeDelta::FromHours(hours); +  } + +  void AddFakeTimeDeltaInHours(int hours) { +    now_delta_ += base::TimeDelta::FromHours(hours); +  }    void SetUpPrefs() {      CreatePrefList(prefs::kDailyHttpOriginalContentLength); @@ -86,7 +115,7 @@ class DataReductionProxyCompressionStatsTest : public testing::Test {    // |simple_pref_service| for |pref|.    void VerifyPrefListWasWritten(const char* pref) {      const base::ListValue* delayed_list = compression_stats_->GetList(pref); -    const base::ListValue* written_list = simple_pref_service_.GetList(pref); +    const base::ListValue* written_list = pref_service()->GetList(pref);      ASSERT_EQ(delayed_list->GetSize(), written_list->GetSize());      size_t count = delayed_list->GetSize(); @@ -100,7 +129,7 @@ class DataReductionProxyCompressionStatsTest : public testing::Test {    // |simple_pref_service|.    void VerifyPrefWasWritten(const char* pref) {      int64 delayed_pref = compression_stats_->GetInt64(pref); -    int64 written_pref = simple_pref_service_.GetInt64(pref); +    int64 written_pref = pref_service()->GetInt64(pref);      EXPECT_EQ(delayed_pref, written_pref);    } @@ -123,16 +152,154 @@ class DataReductionProxyCompressionStatsTest : public testing::Test {      EXPECT_EQ(service_pref, dict_pref);    } +  // Verify the pref list values are equal to the given values. +  // If the count of values is less than kNumDaysInHistory, zeros are assumed +  // at the beginning. +  void VerifyPrefList(const char* pref, const int64* values, size_t count) { +    ASSERT_GE(kNumDaysInHistory, count); +    base::ListValue* update = compression_stats_->GetList(pref); +    ASSERT_EQ(kNumDaysInHistory, update->GetSize()) << "Pref: " << pref; + +    for (size_t i = 0; i < count; ++i) { +      EXPECT_EQ( +          values[i], +          GetListPrefInt64Value(*update, kNumDaysInHistory - count + i)) +          << "index=" << (kNumDaysInHistory - count + i); +    } +    for (size_t i = 0; i < kNumDaysInHistory - count; ++i) { +      EXPECT_EQ(0, GetListPrefInt64Value(*update, i)) << "index=" << i; +    } +  } + +  // Verify all daily data saving pref list values. +  void VerifyDailyDataSavingContentLengthPrefLists( +      const int64* original_values, size_t original_count, +      const int64* received_values, size_t received_count, +      const int64* original_with_data_reduction_proxy_enabled_values, +      size_t original_with_data_reduction_proxy_enabled_count, +      const int64* received_with_data_reduction_proxy_enabled_values, +      size_t received_with_data_reduction_proxy_count, +      const int64* original_via_data_reduction_proxy_values, +      size_t original_via_data_reduction_proxy_count, +      const int64* received_via_data_reduction_proxy_values, +      size_t received_via_data_reduction_proxy_count) { +    VerifyPrefList(data_reduction_proxy::prefs::kDailyHttpOriginalContentLength, +                   original_values, original_count); +    VerifyPrefList(data_reduction_proxy::prefs::kDailyHttpReceivedContentLength, +                   received_values, received_count); +    VerifyPrefList( +        data_reduction_proxy::prefs:: +            kDailyOriginalContentLengthWithDataReductionProxyEnabled, +        original_with_data_reduction_proxy_enabled_values, +        original_with_data_reduction_proxy_enabled_count); +    VerifyPrefList( +        data_reduction_proxy::prefs:: +            kDailyContentLengthWithDataReductionProxyEnabled, +        received_with_data_reduction_proxy_enabled_values, +        received_with_data_reduction_proxy_count); +    VerifyPrefList( +        data_reduction_proxy::prefs:: +            kDailyOriginalContentLengthViaDataReductionProxy, +        original_via_data_reduction_proxy_values, +        original_via_data_reduction_proxy_count); +    VerifyPrefList( +        data_reduction_proxy::prefs:: +            kDailyContentLengthViaDataReductionProxy, +        received_via_data_reduction_proxy_values, +        received_via_data_reduction_proxy_count); +  } + +  // Verify daily data saving pref for request types. +  void VerifyDailyRequestTypeContentLengthPrefLists( +      const int64* original_values, size_t original_count, +      const int64* received_values, size_t received_count, +      const int64* original_with_data_reduction_proxy_enabled_values, +      size_t original_with_data_reduction_proxy_enabled_count, +      const int64* received_with_data_reduction_proxy_enabled_values, +      size_t received_with_data_reduction_proxy_count, +      const int64* https_with_data_reduction_proxy_enabled_values, +      size_t https_with_data_reduction_proxy_enabled_count, +      const int64* short_bypass_with_data_reduction_proxy_enabled_values, +      size_t short_bypass_with_data_reduction_proxy_enabled_count, +      const int64* long_bypass_with_data_reduction_proxy_enabled_values, +      size_t long_bypass_with_data_reduction_proxy_enabled_count, +      const int64* unknown_with_data_reduction_proxy_enabled_values, +      size_t unknown_with_data_reduction_proxy_enabled_count) { +    VerifyPrefList(data_reduction_proxy::prefs::kDailyHttpOriginalContentLength, +                   original_values, original_count); +    VerifyPrefList(data_reduction_proxy::prefs::kDailyHttpReceivedContentLength, +                   received_values, received_count); +    VerifyPrefList( +        data_reduction_proxy::prefs:: +            kDailyOriginalContentLengthWithDataReductionProxyEnabled, +        original_with_data_reduction_proxy_enabled_values, +        original_with_data_reduction_proxy_enabled_count); +    VerifyPrefList( +        data_reduction_proxy::prefs:: +            kDailyContentLengthWithDataReductionProxyEnabled, +        received_with_data_reduction_proxy_enabled_values, +        received_with_data_reduction_proxy_count); +    VerifyPrefList( +        data_reduction_proxy::prefs:: +            kDailyContentLengthHttpsWithDataReductionProxyEnabled, +        https_with_data_reduction_proxy_enabled_values, +        https_with_data_reduction_proxy_enabled_count); +    VerifyPrefList( +        data_reduction_proxy::prefs:: +            kDailyContentLengthShortBypassWithDataReductionProxyEnabled, +        short_bypass_with_data_reduction_proxy_enabled_values, +        short_bypass_with_data_reduction_proxy_enabled_count); +    VerifyPrefList( +        data_reduction_proxy::prefs:: +            kDailyContentLengthLongBypassWithDataReductionProxyEnabled, +        long_bypass_with_data_reduction_proxy_enabled_values, +        long_bypass_with_data_reduction_proxy_enabled_count); +    VerifyPrefList( +        data_reduction_proxy::prefs:: +            kDailyContentLengthUnknownWithDataReductionProxyEnabled, +        unknown_with_data_reduction_proxy_enabled_values, +        unknown_with_data_reduction_proxy_enabled_count); +  } + +  int64 GetInt64(const char* pref_path) { +    return compression_stats_->GetInt64(pref_path); +  } + +  void SetInt64(const char* pref_path, int64 pref_value) { +    compression_stats_->SetInt64(pref_path, pref_value); +  } + +  void RecordContentLengthPrefs(int received_content_length, +                                int original_content_length, +                                bool with_data_reduction_proxy_enabled, +                                DataReductionProxyRequestType request_type, +                                base::Time now) { +    compression_stats_->RecordContentLengthPrefs( +        received_content_length, original_content_length, +        with_data_reduction_proxy_enabled, request_type, now); +  } + +  DataReductionProxyCompressionStats* compression_stats() { +    return compression_stats_.get(); +  } + +  void RunPendingTasks() { +    task_runner_->RunPendingTasks(); +  } + +  TestingPrefServiceSimple* pref_service() { +    return &simple_pref_service_; +  } + + private:    scoped_refptr<base::TestSimpleTaskRunner> task_runner_;    TestingPrefServiceSimple simple_pref_service_;    scoped_ptr<DataReductionProxyCompressionStats> compression_stats_; +  base::Time now_; +  base::TimeDelta now_delta_;  };  TEST_F(DataReductionProxyCompressionStatsTest, WritePrefsDirect) { -  compression_stats_.reset(new DataReductionProxyCompressionStats( -      &simple_pref_service_, -      task_runner_, -      base::TimeDelta()));    SetUpPrefs();    VerifyPrefWasWritten(prefs::kHttpOriginalContentLength); @@ -142,13 +309,11 @@ TEST_F(DataReductionProxyCompressionStatsTest, WritePrefsDirect) {  }  TEST_F(DataReductionProxyCompressionStatsTest, WritePrefsDelayed) { -  compression_stats_.reset(new DataReductionProxyCompressionStats( -      &simple_pref_service_, -      task_runner_, -      base::TimeDelta::FromMinutes(kWriteDelayMinutes))); +  ResetCompressionStatsWithDelay( +      base::TimeDelta::FromMinutes(kWriteDelayMinutes));    SetUpPrefs(); -  task_runner_->RunPendingTasks(); +  RunPendingTasks();    VerifyPrefWasWritten(prefs::kHttpOriginalContentLength);    VerifyPrefWasWritten(prefs::kHttpReceivedContentLength); @@ -158,13 +323,11 @@ TEST_F(DataReductionProxyCompressionStatsTest, WritePrefsDelayed) {  TEST_F(DataReductionProxyCompressionStatsTest,         WritePrefsOnUpdateDailyReceivedContentLengths) { -  compression_stats_.reset(new DataReductionProxyCompressionStats( -      &simple_pref_service_, -      task_runner_, -      base::TimeDelta::FromMinutes(kWriteDelayMinutes))); +  ResetCompressionStatsWithDelay( +      base::TimeDelta::FromMinutes(kWriteDelayMinutes));    SetUpPrefs(); -  simple_pref_service_.SetBoolean( +  pref_service()->SetBoolean(        prefs::kUpdateDailyReceivedContentLengths, true);    VerifyPrefWasWritten(prefs::kHttpOriginalContentLength); @@ -177,23 +340,19 @@ TEST_F(DataReductionProxyCompressionStatsTest,         HistoricNetworkStatsInfoToValue) {    const int64 kOriginalLength = 150;    const int64 kReceivedLength = 100; -  compression_stats_.reset(new DataReductionProxyCompressionStats( -      &simple_pref_service_, -      task_runner_, -      base::TimeDelta::FromMinutes(kWriteDelayMinutes))); +  ResetCompressionStatsWithDelay( +      base::TimeDelta::FromMinutes(kWriteDelayMinutes));    base::DictionaryValue* dict = nullptr;    scoped_ptr<base::Value> stats_value( -      compression_stats_->HistoricNetworkStatsInfoToValue()); +      compression_stats()->HistoricNetworkStatsInfoToValue());    EXPECT_TRUE(stats_value->GetAsDictionary(&dict));    VerifyPrefs(dict); -  compression_stats_->SetInt64(prefs::kHttpOriginalContentLength, -                               kOriginalLength); -  compression_stats_->SetInt64(prefs::kHttpReceivedContentLength, -                               kReceivedLength); +  SetInt64(prefs::kHttpOriginalContentLength, kOriginalLength); +  SetInt64(prefs::kHttpReceivedContentLength, kReceivedLength); -  stats_value.reset(compression_stats_->HistoricNetworkStatsInfoToValue()); +  stats_value.reset(compression_stats()->HistoricNetworkStatsInfoToValue());    EXPECT_TRUE(stats_value->GetAsDictionary(&dict));    VerifyPrefs(dict);  } @@ -202,23 +361,17 @@ TEST_F(DataReductionProxyCompressionStatsTest,         HistoricNetworkStatsInfoToValueDirect) {    const int64 kOriginalLength = 150;    const int64 kReceivedLength = 100; -  compression_stats_.reset(new DataReductionProxyCompressionStats( -      &simple_pref_service_, -      task_runner_, -      base::TimeDelta()));    base::DictionaryValue* dict = nullptr;    scoped_ptr<base::Value> stats_value( -      compression_stats_->HistoricNetworkStatsInfoToValue()); +      compression_stats()->HistoricNetworkStatsInfoToValue());    EXPECT_TRUE(stats_value->GetAsDictionary(&dict));    VerifyPrefs(dict); -  compression_stats_->SetInt64(prefs::kHttpOriginalContentLength, -                               kOriginalLength); -  compression_stats_->SetInt64(prefs::kHttpReceivedContentLength, -                               kReceivedLength); +  SetInt64(prefs::kHttpOriginalContentLength, kOriginalLength); +  SetInt64(prefs::kHttpReceivedContentLength, kReceivedLength); -  stats_value.reset(compression_stats_->HistoricNetworkStatsInfoToValue()); +  stats_value.reset(compression_stats()->HistoricNetworkStatsInfoToValue());    EXPECT_TRUE(stats_value->GetAsDictionary(&dict));    VerifyPrefs(dict);  } @@ -231,14 +384,12 @@ TEST_F(DataReductionProxyCompressionStatsTest,    base::ListValue list_value;    list_value.Insert(0, new base::StringValue(base::Int64ToString(1234))); -  simple_pref_service_.Set(prefs::kDailyHttpOriginalContentLength, list_value); +  pref_service()->Set(prefs::kDailyHttpOriginalContentLength, list_value); -  compression_stats_.reset(new DataReductionProxyCompressionStats( -      &simple_pref_service_, -      task_runner_, -      base::TimeDelta::FromMinutes(kWriteDelayMinutes))); +  ResetCompressionStatsWithDelay( +      base::TimeDelta::FromMinutes(kWriteDelayMinutes)); -  const base::ListValue* value = simple_pref_service_.GetList( +  const base::ListValue* value = pref_service()->GetList(        prefs::kDailyHttpOriginalContentLength);    EXPECT_EQ(0u, value->GetSize());  } @@ -247,18 +398,421 @@ TEST_F(DataReductionProxyCompressionStatsTest,         ClearPrefsOnRestartDisabled) {    base::ListValue list_value;    list_value.Insert(0, new base::StringValue(base::Int64ToString(1234))); -  simple_pref_service_.Set(prefs::kDailyHttpOriginalContentLength, list_value); +  pref_service()->Set(prefs::kDailyHttpOriginalContentLength, list_value); -  compression_stats_.reset(new DataReductionProxyCompressionStats( -      &simple_pref_service_, -      task_runner_, -      base::TimeDelta::FromMinutes(kWriteDelayMinutes))); +  ResetCompressionStatsWithDelay( +      base::TimeDelta::FromMinutes(kWriteDelayMinutes)); -  const base::ListValue* value = simple_pref_service_.GetList( +  const base::ListValue* value = pref_service()->GetList(        prefs::kDailyHttpOriginalContentLength);    std::string string_value;    value->GetString(0, &string_value);    EXPECT_EQ("1234", string_value);  } +TEST_F(DataReductionProxyCompressionStatsTest, TotalLengths) { +  const int64 kOriginalLength = 200; +  const int64 kReceivedLength = 100; + +  compression_stats()->UpdateContentLengths( +      kReceivedLength, kOriginalLength, +      pref_service()->GetBoolean( +          data_reduction_proxy::prefs::kDataReductionProxyEnabled), +      UNKNOWN_TYPE); + +  EXPECT_EQ(kReceivedLength, +            GetInt64(data_reduction_proxy::prefs::kHttpReceivedContentLength)); +  EXPECT_FALSE(pref_service()->GetBoolean( +      data_reduction_proxy::prefs::kDataReductionProxyEnabled)); +  EXPECT_EQ(kOriginalLength, +            GetInt64(data_reduction_proxy::prefs::kHttpOriginalContentLength)); + +  // Record the same numbers again, and total lengths should be doubled. +  compression_stats()->UpdateContentLengths( +      kReceivedLength, kOriginalLength, +      pref_service()->GetBoolean( +          data_reduction_proxy::prefs::kDataReductionProxyEnabled), +      UNKNOWN_TYPE); + +  EXPECT_EQ(kReceivedLength * 2, +            GetInt64(data_reduction_proxy::prefs::kHttpReceivedContentLength)); +  EXPECT_FALSE(pref_service()->GetBoolean( +      data_reduction_proxy::prefs::kDataReductionProxyEnabled)); +  EXPECT_EQ(kOriginalLength * 2, +            GetInt64(data_reduction_proxy::prefs::kHttpOriginalContentLength)); +} + +TEST_F(DataReductionProxyCompressionStatsTest, OneResponse) { +  const int64 kOriginalLength = 200; +  const int64 kReceivedLength = 100; +  int64 original[] = {kOriginalLength}; +  int64 received[] = {kReceivedLength}; + +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, VIA_DATA_REDUCTION_PROXY, +      FakeNow()); +  VerifyDailyDataSavingContentLengthPrefLists( +      original, 1, received, 1, +      original, 1, received, 1, +      original, 1, received, 1); +} + +TEST_F(DataReductionProxyCompressionStatsTest, MultipleResponses) { +  const int64 kOriginalLength = 150; +  const int64 kReceivedLength = 100; +  int64 original[] = {kOriginalLength}; +  int64 received[] = {kReceivedLength}; +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, false, UNKNOWN_TYPE, FakeNow()); +  VerifyDailyDataSavingContentLengthPrefLists( +      original, 1, received, 1, +      NULL, 0, NULL, 0, NULL, 0, NULL, 0); + +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, UNKNOWN_TYPE, FakeNow()); +  original[0] += kOriginalLength; +  received[0] += kReceivedLength; +  int64 original_proxy_enabled[] = {kOriginalLength}; +  int64 received_proxy_enabled[] = {kReceivedLength}; +  VerifyDailyDataSavingContentLengthPrefLists( +      original, 1, received, 1, +      original_proxy_enabled, 1, received_proxy_enabled, 1, +      NULL, 0, NULL, 0); + +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, VIA_DATA_REDUCTION_PROXY, +      FakeNow()); +  original[0] += kOriginalLength; +  received[0] += kReceivedLength; +  original_proxy_enabled[0] += kOriginalLength; +  received_proxy_enabled[0] += kReceivedLength; +  int64 original_via_proxy[] = {kOriginalLength}; +  int64 received_via_proxy[] = {kReceivedLength}; +  VerifyDailyDataSavingContentLengthPrefLists( +      original, 1, received, 1, +      original_proxy_enabled, 1, received_proxy_enabled, 1, +      original_via_proxy, 1, received_via_proxy, 1); + +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, UNKNOWN_TYPE, FakeNow()); +  original[0] += kOriginalLength; +  received[0] += kReceivedLength; +  original_proxy_enabled[0] += kOriginalLength; +  received_proxy_enabled[0] += kReceivedLength; +  VerifyDailyDataSavingContentLengthPrefLists( +      original, 1, received, 1, +      original_proxy_enabled, 1, received_proxy_enabled, 1, +      original_via_proxy, 1, received_via_proxy, 1); + +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, false, UNKNOWN_TYPE, FakeNow()); +  original[0] += kOriginalLength; +  received[0] += kReceivedLength; +  VerifyDailyDataSavingContentLengthPrefLists( +      original, 1, received, 1, +      original_proxy_enabled, 1, received_proxy_enabled, 1, +      original_via_proxy, 1, received_via_proxy, 1); +} + +TEST_F(DataReductionProxyCompressionStatsTest, RequestType) { +  const int64 kContentLength = 200; +  int64 received[] = {0}; +  int64 https_received[] = {0}; +  int64 total_received[] = {0}; +  int64 proxy_enabled_received[] = {0}; + +  RecordContentLengthPrefs( +      kContentLength, kContentLength, true, HTTPS, FakeNow()); +  total_received[0] += kContentLength; +  proxy_enabled_received[0] += kContentLength; +  https_received[0] += kContentLength; +  VerifyDailyRequestTypeContentLengthPrefLists( +      total_received, 1, total_received, 1, +      proxy_enabled_received, 1, proxy_enabled_received, 1, +      https_received, 1, +      received, 0,  // short bypass +      received, 0,  // long bypass +      received, 0);  // unknown + +  // Data reduction proxy is not enabled. +  RecordContentLengthPrefs( +      kContentLength, kContentLength, false, HTTPS, FakeNow()); +  total_received[0] += kContentLength; +  VerifyDailyRequestTypeContentLengthPrefLists( +      total_received, 1, total_received, 1, +      proxy_enabled_received, 1, proxy_enabled_received, 1, +      https_received, 1, +      received, 0,  // short bypass +      received, 0,  // long bypass +      received, 0);  // unknown + +  RecordContentLengthPrefs( +      kContentLength, kContentLength, true, HTTPS, FakeNow()); +  total_received[0] += kContentLength; +  proxy_enabled_received[0] += kContentLength; +  https_received[0] += kContentLength; +  VerifyDailyRequestTypeContentLengthPrefLists( +      total_received, 1, total_received, 1, +      proxy_enabled_received, 1, proxy_enabled_received, 1, +      https_received, 1, +      received, 0,  // short bypass +      received, 0,  // long bypass +      received, 0);  // unknown + +  RecordContentLengthPrefs( +      kContentLength, kContentLength, true, SHORT_BYPASS, FakeNow()); +  total_received[0] += kContentLength; +  proxy_enabled_received[0] += kContentLength; +  received[0] += kContentLength; +  VerifyDailyRequestTypeContentLengthPrefLists( +      total_received, 1, total_received, 1, +      proxy_enabled_received, 1, proxy_enabled_received, 1, +      https_received, 1, +      received, 1,  // short bypass +      received, 0,  // long bypass +      received, 0);  // unknown + +  RecordContentLengthPrefs( +      kContentLength, kContentLength, true, LONG_BYPASS, FakeNow()); +  total_received[0] += kContentLength; +  proxy_enabled_received[0] += kContentLength; +  VerifyDailyRequestTypeContentLengthPrefLists( +      total_received, 1, total_received, 1,  // total +      proxy_enabled_received, 1, proxy_enabled_received, 1, +      https_received, 1, +      received, 1,  // short bypass +      received, 1,  // long bypass +      received, 0);  // unknown + +  RecordContentLengthPrefs( +      kContentLength, kContentLength, true, UNKNOWN_TYPE, FakeNow()); +  total_received[0] += kContentLength; +  proxy_enabled_received[0] += kContentLength; +  VerifyDailyRequestTypeContentLengthPrefLists( +      total_received, 1, total_received, 1, +      proxy_enabled_received, 1, proxy_enabled_received, 1, +      https_received, 1, +      received, 1,  // short bypass +      received, 1,  // long bypass +      received, 1);  // unknown +} + +TEST_F(DataReductionProxyCompressionStatsTest, ForwardOneDay) { +  const int64 kOriginalLength = 200; +  const int64 kReceivedLength = 100; + +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, VIA_DATA_REDUCTION_PROXY, +      FakeNow()); + +  // Forward one day. +  SetFakeTimeDeltaInHours(24); + +  // Proxy not enabled. Not via proxy. +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, false, UNKNOWN_TYPE, FakeNow()); + +  int64 original[] = {kOriginalLength, kOriginalLength}; +  int64 received[] = {kReceivedLength, kReceivedLength}; +  int64 original_with_data_reduction_proxy_enabled[] = {kOriginalLength, 0}; +  int64 received_with_data_reduction_proxy_enabled[] = {kReceivedLength, 0}; +  int64 original_via_data_reduction_proxy[] = {kOriginalLength, 0}; +  int64 received_via_data_reduction_proxy[] = {kReceivedLength, 0}; +  VerifyDailyDataSavingContentLengthPrefLists( +      original, 2, +      received, 2, +      original_with_data_reduction_proxy_enabled, 2, +      received_with_data_reduction_proxy_enabled, 2, +      original_via_data_reduction_proxy, 2, +      received_via_data_reduction_proxy, 2); + +  // Proxy enabled. Not via proxy. +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, UNKNOWN_TYPE, FakeNow()); +  original[1] += kOriginalLength; +  received[1] += kReceivedLength; +  original_with_data_reduction_proxy_enabled[1] += kOriginalLength; +  received_with_data_reduction_proxy_enabled[1] += kReceivedLength; +  VerifyDailyDataSavingContentLengthPrefLists( +      original, 2, +      received, 2, +      original_with_data_reduction_proxy_enabled, 2, +      received_with_data_reduction_proxy_enabled, 2, +      original_via_data_reduction_proxy, 2, +      received_via_data_reduction_proxy, 2); + +  // Proxy enabled and via proxy. +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, VIA_DATA_REDUCTION_PROXY, +      FakeNow()); +  original[1] += kOriginalLength; +  received[1] += kReceivedLength; +  original_with_data_reduction_proxy_enabled[1] += kOriginalLength; +  received_with_data_reduction_proxy_enabled[1] += kReceivedLength; +  original_via_data_reduction_proxy[1] += kOriginalLength; +  received_via_data_reduction_proxy[1] += kReceivedLength; +  VerifyDailyDataSavingContentLengthPrefLists( +      original, 2, +      received, 2, +      original_with_data_reduction_proxy_enabled, 2, +      received_with_data_reduction_proxy_enabled, 2, +      original_via_data_reduction_proxy, 2, +      received_via_data_reduction_proxy, 2); +} + +TEST_F(DataReductionProxyCompressionStatsTest, PartialDayTimeChange) { +  const int64 kOriginalLength = 200; +  const int64 kReceivedLength = 100; +  int64 original[] = {0, kOriginalLength}; +  int64 received[] = {0, kReceivedLength}; + +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, VIA_DATA_REDUCTION_PROXY, +      FakeNow()); +  VerifyDailyDataSavingContentLengthPrefLists( +      original, 2, received, 2, +      original, 2, received, 2, +      original, 2, received, 2); + +  // Forward 10 hours, stay in the same day. +  // See kLastUpdateTime: "Now" in test is 03:45am. +  SetFakeTimeDeltaInHours(10); +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, VIA_DATA_REDUCTION_PROXY, +      FakeNow()); +  original[1] += kOriginalLength; +  received[1] += kReceivedLength; +  VerifyDailyDataSavingContentLengthPrefLists( +      original, 2, received, 2, +      original, 2, received, 2, +      original, 2, received, 2); + +  // Forward 11 more hours, comes to tomorrow. +  AddFakeTimeDeltaInHours(11); +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, VIA_DATA_REDUCTION_PROXY, +      FakeNow()); +  int64 original2[] = {kOriginalLength * 2, kOriginalLength}; +  int64 received2[] = {kReceivedLength * 2, kReceivedLength}; +  VerifyDailyDataSavingContentLengthPrefLists( +      original2, 2, received2, 2, +      original2, 2, received2, 2, +      original2, 2, received2, 2); +} + +TEST_F(DataReductionProxyCompressionStatsTest, ForwardMultipleDays) { +  const int64 kOriginalLength = 200; +  const int64 kReceivedLength = 100; +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, VIA_DATA_REDUCTION_PROXY, +      FakeNow()); + +  // Forward three days. +  SetFakeTimeDeltaInHours(3 * 24); + +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, VIA_DATA_REDUCTION_PROXY, +      FakeNow()); + +  int64 original[] = {kOriginalLength, 0, 0, kOriginalLength}; +  int64 received[] = {kReceivedLength, 0, 0, kReceivedLength}; +  VerifyDailyDataSavingContentLengthPrefLists( +      original, 4, received, 4, +      original, 4, received, 4, +      original, 4, received, 4); + +  // Forward four more days. +  AddFakeTimeDeltaInHours(4 * 24); +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, VIA_DATA_REDUCTION_PROXY, +      FakeNow()); +  int64 original2[] = { +    kOriginalLength, 0, 0, kOriginalLength, 0, 0, 0, kOriginalLength, +  }; +  int64 received2[] = { +    kReceivedLength, 0, 0, kReceivedLength, 0, 0, 0, kReceivedLength, +  }; +  VerifyDailyDataSavingContentLengthPrefLists( +      original2, 8, received2, 8, +      original2, 8, received2, 8, +      original2, 8, received2, 8); + +  // Forward |kNumDaysInHistory| more days. +  AddFakeTimeDeltaInHours(kNumDaysInHistory * 24); +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, VIA_DATA_REDUCTION_PROXY, +      FakeNow()); +  int64 original3[] = {kOriginalLength}; +  int64 received3[] = {kReceivedLength}; +  VerifyDailyDataSavingContentLengthPrefLists( +      original3, 1, received3, 1, +      original3, 1, received3, 1, +      original3, 1, received3, 1); + +  // Forward |kNumDaysInHistory| + 1 more days. +  AddFakeTimeDeltaInHours((kNumDaysInHistory + 1)* 24); +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, VIA_DATA_REDUCTION_PROXY, +      FakeNow()); +  VerifyDailyDataSavingContentLengthPrefLists( +      original3, 1, received3, 1, +      original3, 1, received3, 1, +      original3, 1, received3, 1); +} + +TEST_F(DataReductionProxyCompressionStatsTest, BackwardAndForwardOneDay) { +  const int64 kOriginalLength = 200; +  const int64 kReceivedLength = 100; +  int64 original[] = {kOriginalLength}; +  int64 received[] = {kReceivedLength}; + +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, VIA_DATA_REDUCTION_PROXY, +      FakeNow()); + +  // Backward one day. +  SetFakeTimeDeltaInHours(-24); +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, VIA_DATA_REDUCTION_PROXY, +      FakeNow()); +  original[0] += kOriginalLength; +  received[0] += kReceivedLength; +  VerifyDailyDataSavingContentLengthPrefLists( +      original, 1, received, 1, +      original, 1, received, 1, +      original, 1, received, 1); + +  // Then, Forward one day +  AddFakeTimeDeltaInHours(24); +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, VIA_DATA_REDUCTION_PROXY, +      FakeNow()); +  int64 original2[] = {kOriginalLength * 2, kOriginalLength}; +  int64 received2[] = {kReceivedLength * 2, kReceivedLength}; +  VerifyDailyDataSavingContentLengthPrefLists( +      original2, 2, received2, 2, +      original2, 2, received2, 2, +      original2, 2, received2, 2); +} + +TEST_F(DataReductionProxyCompressionStatsTest, BackwardTwoDays) { +  const int64 kOriginalLength = 200; +  const int64 kReceivedLength = 100; +  int64 original[] = {kOriginalLength}; +  int64 received[] = {kReceivedLength}; + +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, VIA_DATA_REDUCTION_PROXY, +      FakeNow()); +  // Backward two days. +  SetFakeTimeDeltaInHours(-2 * 24); +  RecordContentLengthPrefs( +      kReceivedLength, kOriginalLength, true, VIA_DATA_REDUCTION_PROXY, +      FakeNow()); +  VerifyDailyDataSavingContentLengthPrefLists( +      original, 1, received, 1, +      original, 1, received, 1, +      original, 1, received, 1); +} +  }  // namespace data_reduction_proxy diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.cc index bfbe85e..3c9ebef 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.cc @@ -64,10 +64,15 @@ DataReductionProxyIOData::DataReductionProxyIOData(          configurator_.get(), event_store_.get()));    } +  // It is safe to use base::Unretained here, since it gets executed +  // synchronously on the IO thread, and |this| outlives the caller (since the +  // caller is owned by |this|. +  bypass_stats_.reset(new DataReductionProxyBypassStats( +      config_.get(), base::Bind(&DataReductionProxyIOData::SetUnreachable, +                                base::Unretained(this))));    request_options_.reset(new DataReductionProxyRequestOptions(        client_, config_.get(), io_task_runner_));    request_options_->Init(); -    if (use_config_client) {      config_client_.reset(new DataReductionProxyConfigServiceClient(          params.Pass(), GetBackoffPolicy(), request_options_.get(), @@ -127,20 +132,30 @@ DataReductionProxyIOData::CreateNetworkDelegate(        new DataReductionProxyNetworkDelegate(            wrapped_network_delegate.Pass(), config_.get(),            request_options_.get(), configurator_.get())); -  if (track_proxy_bypass_statistics && !bypass_stats_) { -    bypass_stats_.reset(new DataReductionProxyBypassStats( -        config_.get(), base::Bind(&DataReductionProxyIOData::SetUnreachable, -                                  base::Unretained(this)), ui_task_runner_)); -    network_delegate->InitIODataAndUMA(ui_task_runner_, this, &enabled_, -                                       bypass_stats_.get()); -  } +  if (track_proxy_bypass_statistics) +    network_delegate->InitIODataAndUMA(this, &enabled_, bypass_stats_.get());    return network_delegate.Pass();  } +void DataReductionProxyIOData::UpdateContentLengths( +    int received_content_length, +    int original_content_length, +    bool data_reduction_proxy_enabled, +    DataReductionProxyRequestType request_type) { +  DCHECK(io_task_runner_->BelongsToCurrentThread()); +  ui_task_runner_->PostTask( +      FROM_HERE, +      base::Bind(&DataReductionProxyService::UpdateContentLengths, +                 service_, received_content_length, original_content_length, +                 data_reduction_proxy_enabled, request_type)); +} +  void DataReductionProxyIOData::SetUnreachable(bool unreachable) { -  DCHECK(ui_task_runner_->BelongsToCurrentThread()); -  if (service_) -    service_->settings()->SetUnreachable(unreachable); +  DCHECK(io_task_runner_->BelongsToCurrentThread()); +  ui_task_runner_->PostTask( +      FROM_HERE, +      base::Bind(&DataReductionProxyService::SetUnreachable, +                 service_, unreachable));  }  }  // namespace data_reduction_proxy diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h index 80eab78..e258ec4 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h @@ -10,6 +10,7 @@  #include "base/prefs/pref_member.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_debug_ui_service.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_delegate.h" +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.h" @@ -20,12 +21,12 @@ class URLRequestInterceptor;  namespace data_reduction_proxy { +class DataReductionProxyBypassStats;  class DataReductionProxyConfig;  class DataReductionProxyConfigServiceClient;  class DataReductionProxyConfigurator;  class DataReductionProxyEventStore;  class DataReductionProxyService; -class DataReductionProxyBypassStats;  // Contains and initializes all Data Reduction Proxy objects that operate on  // the IO thread. @@ -67,9 +68,16 @@ class DataReductionProxyIOData {        scoped_ptr<net::NetworkDelegate> wrapped_network_delegate,        bool track_proxy_bypass_statistics); +  // Bridge methods to safely call to the UI thread objects. +  void UpdateContentLengths(int received_content_length, +                            int original_content_length, +                            bool data_reduction_proxy_enabled, +                            DataReductionProxyRequestType request_type); +    // Returns true if the Data Reduction Proxy is enabled and false otherwise.    bool IsEnabled() const; +  // Various accessor methods.    DataReductionProxyConfigurator* configurator() const {      return configurator_.get();    } @@ -172,4 +180,3 @@ class DataReductionProxyIOData {  }  // namespace data_reduction_proxy  #endif  // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_IO_DATA_H_ - diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data_unittest.cc index f0438b0..c9c92e1 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data_unittest.cc @@ -114,7 +114,7 @@ TEST_F(DataReductionProxyIODataTest, TestConstruction) {        base::Bind(&DataReductionProxyIODataTest::RequestCallback,                   base::Unretained(this)), nullptr);    EXPECT_EQ(1, wrapped_network_delegate->created_requests()); -  EXPECT_EQ(nullptr, io_data->bypass_stats()); +  EXPECT_NE(nullptr, io_data->bypass_stats());    // Creating a second delegate with bypass statistics tracking should result    // in usage stats being created. diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.cc index 0408a7b..39efaa9 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.cc @@ -4,296 +4,19 @@  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h" -#include "base/metrics/histogram.h" -#include "base/prefs/pref_service.h" -#include "base/strings/string_number_conversions.h" -#include "base/strings/string_util.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h" +#include "base/basictypes.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"  #include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h" -#include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h" -#include "net/base/host_port_pair.h"  #include "net/base/load_flags.h" -#include "net/http/http_response_headers.h" +#include "net/http/http_response_info.h"  #include "net/proxy/proxy_config.h" -#include "net/proxy/proxy_retry_info.h"  #include "net/proxy/proxy_server.h" -#include "net/proxy/proxy_service.h"  #include "net/url_request/url_request.h"  #include "net/url_request/url_request_context.h"  #include "url/url_constants.h"  namespace data_reduction_proxy { -namespace { - -// A bypass delay more than this is treated as a long delay. -const int kLongBypassDelayInSeconds = 30 * 60; - -// Increments an int64, stored as a string, in a ListPref at the specified -// index.  The value must already exist and be a string representation of a -// number. -void AddInt64ToListPref(size_t index, -                        int64 length, -                        base::ListValue* list_update) { -  int64 value = 0; -  std::string old_string_value; -  bool rv = list_update->GetString(index, &old_string_value); -  DCHECK(rv); -  if (rv) { -    rv = base::StringToInt64(old_string_value, &value); -    DCHECK(rv); -  } -  value += length; -  list_update->Set(index, new base::StringValue(base::Int64ToString(value))); -} - -int64 ListPrefInt64Value(const base::ListValue& list_update, size_t index) { -  std::string string_value; -  if (!list_update.GetString(index, &string_value)) { -    NOTREACHED(); -    return 0; -  } - -  int64 value = 0; -  bool rv = base::StringToInt64(string_value, &value); -  DCHECK(rv); -  return value; -} - -// Report UMA metrics for daily data reductions. -void RecordDailyContentLengthHistograms( -    int64 original_length, -    int64 received_length, -    int64 original_length_with_data_reduction_enabled, -    int64 received_length_with_data_reduction_enabled, -    int64 original_length_via_data_reduction_proxy, -    int64 received_length_via_data_reduction_proxy, -    int64 https_length_with_data_reduction_enabled, -    int64 short_bypass_length_with_data_reduction_enabled, -    int64 long_bypass_length_with_data_reduction_enabled, -    int64 unknown_length_with_data_reduction_enabled) { -  // Report daily UMA only for days having received content. -  if (original_length <= 0 || received_length <= 0) -    return; - -  // Record metrics in KB. -  UMA_HISTOGRAM_COUNTS( -      "Net.DailyOriginalContentLength", original_length >> 10); -  UMA_HISTOGRAM_COUNTS( -      "Net.DailyContentLength", received_length >> 10); -  int percent = 0; -  // UMA percentage cannot be negative. -  if (original_length > received_length) { -    percent = (100 * (original_length - received_length)) / original_length; -  } -  UMA_HISTOGRAM_PERCENTAGE("Net.DailyContentSavingPercent", percent); - -  if (original_length_with_data_reduction_enabled <= 0 || -      received_length_with_data_reduction_enabled <= 0) { -    return; -  } - -  UMA_HISTOGRAM_COUNTS( -      "Net.DailyOriginalContentLength_DataReductionProxyEnabled", -      original_length_with_data_reduction_enabled >> 10); -  UMA_HISTOGRAM_COUNTS( -      "Net.DailyContentLength_DataReductionProxyEnabled", -      received_length_with_data_reduction_enabled >> 10); - -  int percent_data_reduction_proxy_enabled = 0; -  // UMA percentage cannot be negative. -  if (original_length_with_data_reduction_enabled > -      received_length_with_data_reduction_enabled) { -    percent_data_reduction_proxy_enabled = -        100 * (original_length_with_data_reduction_enabled - -               received_length_with_data_reduction_enabled) / -        original_length_with_data_reduction_enabled; -  } -  UMA_HISTOGRAM_PERCENTAGE( -      "Net.DailyContentSavingPercent_DataReductionProxyEnabled", -      percent_data_reduction_proxy_enabled); - -  UMA_HISTOGRAM_PERCENTAGE( -      "Net.DailyContentPercent_DataReductionProxyEnabled", -      (100 * received_length_with_data_reduction_enabled) / received_length); - -  DCHECK_GE(https_length_with_data_reduction_enabled, 0); -  UMA_HISTOGRAM_COUNTS( -      "Net.DailyContentLength_DataReductionProxyEnabled_Https", -      https_length_with_data_reduction_enabled >> 10); -  UMA_HISTOGRAM_PERCENTAGE( -      "Net.DailyContentPercent_DataReductionProxyEnabled_Https", -      (100 * https_length_with_data_reduction_enabled) / received_length); - -  DCHECK_GE(short_bypass_length_with_data_reduction_enabled, 0); -  UMA_HISTOGRAM_COUNTS( -      "Net.DailyContentLength_DataReductionProxyEnabled_ShortBypass", -      short_bypass_length_with_data_reduction_enabled >> 10); -  UMA_HISTOGRAM_PERCENTAGE( -      "Net.DailyContentPercent_DataReductionProxyEnabled_ShortBypass", -      ((100 * short_bypass_length_with_data_reduction_enabled) / -       received_length)); - -  DCHECK_GE(long_bypass_length_with_data_reduction_enabled, 0); -  UMA_HISTOGRAM_COUNTS( -      "Net.DailyContentLength_DataReductionProxyEnabled_LongBypass", -      long_bypass_length_with_data_reduction_enabled >> 10); -  UMA_HISTOGRAM_PERCENTAGE( -      "Net.DailyContentPercent_DataReductionProxyEnabled_LongBypass", -      ((100 * long_bypass_length_with_data_reduction_enabled) / -       received_length)); - -  DCHECK_GE(unknown_length_with_data_reduction_enabled, 0); -  UMA_HISTOGRAM_COUNTS( -      "Net.DailyContentLength_DataReductionProxyEnabled_Unknown", -      unknown_length_with_data_reduction_enabled >> 10); -  UMA_HISTOGRAM_PERCENTAGE( -      "Net.DailyContentPercent_DataReductionProxyEnabled_Unknown", -      ((100 * unknown_length_with_data_reduction_enabled) / -       received_length)); - -  DCHECK_GE(original_length_via_data_reduction_proxy, 0); -  UMA_HISTOGRAM_COUNTS( -      "Net.DailyOriginalContentLength_ViaDataReductionProxy", -      original_length_via_data_reduction_proxy >> 10); -  DCHECK_GE(received_length_via_data_reduction_proxy, 0); -  UMA_HISTOGRAM_COUNTS( -      "Net.DailyContentLength_ViaDataReductionProxy", -      received_length_via_data_reduction_proxy >> 10); -  int percent_via_data_reduction_proxy = 0; -  if (original_length_via_data_reduction_proxy > -      received_length_via_data_reduction_proxy) { -    percent_via_data_reduction_proxy = -        100 * (original_length_via_data_reduction_proxy - -               received_length_via_data_reduction_proxy) / -        original_length_via_data_reduction_proxy; -  } -  UMA_HISTOGRAM_PERCENTAGE( -      "Net.DailyContentSavingPercent_ViaDataReductionProxy", -      percent_via_data_reduction_proxy); -  UMA_HISTOGRAM_PERCENTAGE( -      "Net.DailyContentPercent_ViaDataReductionProxy", -      (100 * received_length_via_data_reduction_proxy) / received_length); -} - -// Ensure list has exactly |length| elements, either by truncating at the -// front, or appending "0"'s to the back. -void MaintainContentLengthPrefsWindow(base::ListValue* list, size_t length) { -  // Remove data for old days from the front. -  while (list->GetSize() > length) -    list->Remove(0, NULL); -  // Newly added lists are empty. Add entries to back to fill the window, -  // each initialized to zero. -  while (list->GetSize() < length) -    list->AppendString(base::Int64ToString(0)); -  DCHECK_EQ(length, list->GetSize()); -} - -// DailyContentLengthUpdate maintains a data saving pref. The pref is a list -// of |kNumDaysInHistory| elements of daily total content lengths for the past -// |kNumDaysInHistory| days. -class DailyContentLengthUpdate { - public: -  DailyContentLengthUpdate(const char* pref, -                           DataReductionProxyCompressionStats* pref_service) -      : update_(pref_service->GetList(pref)) {} - -  void UpdateForDataChange(int days_since_last_update) { -    // New empty lists may have been created. Maintain the invariant that -    // there should be exactly |kNumDaysInHistory| days in the histories. -    MaintainContentLengthPrefsWindow(update_, kNumDaysInHistory); -    if (days_since_last_update) { -      MaintainContentLengthPrefForDateChange(days_since_last_update); -    } -  } - -  // Update the lengths for the current day. -  void Add(int content_length) { -    AddInt64ToListPref(kNumDaysInHistory - 1, content_length, update_); -  } - -  int64 GetListPrefValue(size_t index) { -    return ListPrefInt64Value(*update_, index); -  } - - private: -  // Update the list for date change and ensure the list has exactly |length| -  // elements. The last entry in the list will be for the current day after -  // the update. -  void MaintainContentLengthPrefForDateChange(int days_since_last_update) { -    if (days_since_last_update == -1) { -      // The system may go backwards in time by up to a day for legitimate -      // reasons, such as with changes to the time zone. In such cases, we -      // keep adding to the current day. -      // Note: we accept the fact that some reported data is shifted to -      // the adjacent day if users travel back and forth across time zones. -      days_since_last_update = 0; -    } else if (days_since_last_update < -1) { -      // Erase all entries if the system went backwards in time by more than -      // a day. -      update_->Clear(); - -      days_since_last_update = kNumDaysInHistory; -    } -    DCHECK_GE(days_since_last_update, 0); - -    // Add entries for days since last update event. This will make the -    // lists longer than kNumDaysInHistory. The additional items will be cut off -    // from the head of the lists by |MaintainContentLengthPrefsWindow|, below. -    for (int i = 0; -         i < days_since_last_update && i < static_cast<int>(kNumDaysInHistory); -         ++i) { -      update_->AppendString(base::Int64ToString(0)); -    } - -    // Entries for new days may have been appended. Maintain the invariant that -    // there should be exactly |kNumDaysInHistory| days in the histories. -    MaintainContentLengthPrefsWindow(update_, kNumDaysInHistory); -  } - -  base::ListValue* update_; -}; - -// DailyDataSavingUpdate maintains a pair of data saving prefs, original_update_ -// and received_update_. pref_original is a list of |kNumDaysInHistory| elements -// of daily total original content lengths for the past |kNumDaysInHistory| -// days. pref_received is the corresponding list of the daily total received -// content lengths. -class DailyDataSavingUpdate { - public: -  DailyDataSavingUpdate(const char* pref_original, -                        const char* pref_received, -                        DataReductionProxyCompressionStats* compression_stats) -      : original_(pref_original, compression_stats), -        received_(pref_received, compression_stats) {} - -  void UpdateForDataChange(int days_since_last_update) { -    original_.UpdateForDataChange(days_since_last_update); -    received_.UpdateForDataChange(days_since_last_update); -  } - -  // Update the lengths for the current day. -  void Add(int original_content_length, int received_content_length) { -    original_.Add(original_content_length); -    received_.Add(received_content_length); -  } - -  int64 GetOriginalListPrefValue(size_t index) { -    return original_.GetListPrefValue(index); -  } -  int64 GetReceivedListPrefValue(size_t index) { -    return received_.GetListPrefValue(index); -  } - - private: -  DailyContentLengthUpdate original_; -  DailyContentLengthUpdate received_; -}; - -}  // namespace -  DataReductionProxyRequestType GetDataReductionProxyRequestType(      const net::URLRequest& request,      const net::ProxyConfig& data_reduction_proxy_config, @@ -350,140 +73,4 @@ int64 GetAdjustedOriginalContentLength(    return original_content_length;  } -void UpdateContentLengthPrefsForDataReductionProxy( -    int received_content_length, -    int original_content_length, -    bool with_data_reduction_proxy_enabled, -    DataReductionProxyRequestType request_type, -    base::Time now, -    DataReductionProxyCompressionStats* compression_stats) { -  // TODO(bengr): Remove this check once the underlying cause of -  // http://crbug.com/287821 is fixed. For now, only continue if the current -  // year is reported as being between 1972 and 2970. -  base::TimeDelta time_since_unix_epoch = now - base::Time::UnixEpoch(); -  const int kMinDaysSinceUnixEpoch = 365 * 2;  // 2 years. -  const int kMaxDaysSinceUnixEpoch = 365 * 1000;  // 1000 years. -  if (time_since_unix_epoch.InDays() < kMinDaysSinceUnixEpoch || -      time_since_unix_epoch.InDays() > kMaxDaysSinceUnixEpoch) { -    return; -  } - -  // Determine how many days it has been since the last update. -  int64 then_internal = compression_stats->GetInt64( -      data_reduction_proxy::prefs::kDailyHttpContentLengthLastUpdateDate); - -  // Local midnight could have been shifted due to time zone change. -  // If time is null then don't care if midnight will be wrong shifted due to -  // time zone change because it's still too much time ago. -  base::Time then_midnight = base::Time::FromInternalValue(then_internal); -  if (!then_midnight.is_null()) { -    then_midnight = then_midnight.LocalMidnight(); -  } -  base::Time midnight = now.LocalMidnight(); - -  int days_since_last_update = (midnight - then_midnight).InDays(); - -  // Each day, we calculate the total number of bytes received and the total -  // size of all corresponding resources before any data-reducing recompression -  // is applied. These values are used to compute the data savings realized -  // by applying our compression techniques. Totals for the last -  // |kNumDaysInHistory| days are maintained. -  DailyDataSavingUpdate total( -      data_reduction_proxy::prefs::kDailyHttpOriginalContentLength, -      data_reduction_proxy::prefs::kDailyHttpReceivedContentLength, -      compression_stats); -  total.UpdateForDataChange(days_since_last_update); - -  DailyDataSavingUpdate proxy_enabled( -      data_reduction_proxy::prefs:: -          kDailyOriginalContentLengthWithDataReductionProxyEnabled, -      data_reduction_proxy::prefs:: -          kDailyContentLengthWithDataReductionProxyEnabled, -      compression_stats); -  proxy_enabled.UpdateForDataChange(days_since_last_update); - -  DailyDataSavingUpdate via_proxy( -      data_reduction_proxy::prefs:: -          kDailyOriginalContentLengthViaDataReductionProxy, -      data_reduction_proxy::prefs:: -          kDailyContentLengthViaDataReductionProxy, -      compression_stats); -  via_proxy.UpdateForDataChange(days_since_last_update); - -  DailyContentLengthUpdate https( -      data_reduction_proxy::prefs:: -          kDailyContentLengthHttpsWithDataReductionProxyEnabled, -      compression_stats); -  https.UpdateForDataChange(days_since_last_update); - -  DailyContentLengthUpdate short_bypass( -      data_reduction_proxy::prefs:: -          kDailyContentLengthShortBypassWithDataReductionProxyEnabled, -      compression_stats); -  short_bypass.UpdateForDataChange(days_since_last_update); - -  DailyContentLengthUpdate long_bypass( -      data_reduction_proxy::prefs:: -          kDailyContentLengthLongBypassWithDataReductionProxyEnabled, -      compression_stats); -  long_bypass.UpdateForDataChange(days_since_last_update); - -  DailyContentLengthUpdate unknown( -      data_reduction_proxy::prefs:: -          kDailyContentLengthUnknownWithDataReductionProxyEnabled, -      compression_stats); -  unknown.UpdateForDataChange(days_since_last_update); - -  total.Add(original_content_length, received_content_length); -  if (with_data_reduction_proxy_enabled) { -    proxy_enabled.Add(original_content_length, received_content_length); -    // Ignore data source cases, if exist, when -    // "with_data_reduction_proxy_enabled == false" -    switch (request_type) { -      case VIA_DATA_REDUCTION_PROXY: -        via_proxy.Add(original_content_length, received_content_length); -        break; -      case HTTPS: -        https.Add(received_content_length); -        break; -      case SHORT_BYPASS: -        short_bypass.Add(received_content_length); -        break; -      case LONG_BYPASS: -        long_bypass.Add(received_content_length); -        break; -      case UNKNOWN_TYPE: -        unknown.Add(received_content_length); -        break; -    } -  } - -  if (days_since_last_update) { -    // Record the last update time in microseconds in UTC. -    compression_stats->SetInt64( -        data_reduction_proxy::prefs::kDailyHttpContentLengthLastUpdateDate, -        midnight.ToInternalValue()); - -    // A new day. Report the previous day's data if exists. We'll lose usage -    // data if the last time Chrome was run was more than a day ago. -    // Here, we prefer collecting less data but the collected data is -    // associated with an accurate date. -    if (days_since_last_update == 1) { -      // The previous day's data point is the second one from the tail. -      // Therefore (kNumDaysInHistory - 2) below. -      RecordDailyContentLengthHistograms( -          total.GetOriginalListPrefValue(kNumDaysInHistory - 2), -          total.GetReceivedListPrefValue(kNumDaysInHistory - 2), -          proxy_enabled.GetOriginalListPrefValue(kNumDaysInHistory - 2), -          proxy_enabled.GetReceivedListPrefValue(kNumDaysInHistory - 2), -          via_proxy.GetOriginalListPrefValue(kNumDaysInHistory - 2), -          via_proxy.GetReceivedListPrefValue(kNumDaysInHistory - 2), -          https.GetListPrefValue(kNumDaysInHistory - 2), -          short_bypass.GetListPrefValue(kNumDaysInHistory - 2), -          long_bypass.GetListPrefValue(kNumDaysInHistory - 2), -          unknown.GetListPrefValue(kNumDaysInHistory - 2)); -    } -  } -} -  }  // namespace data_reduction_proxy diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h index 22d47ed..85dd909 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h @@ -5,7 +5,9 @@  #ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_METRICS_H_  #define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_METRICS_H_ -#include "base/time/time.h" +#include <vector> + +#include "base/basictypes.h"  namespace net {  class ProxyConfig; @@ -16,8 +18,22 @@ class PrefService;  namespace data_reduction_proxy { +typedef std::vector<long long> ContentLengthList; +  class DataReductionProxyConfig; -class DataReductionProxyCompressionStats; + +// A bypass delay more than this is treated as a long delay. +const int kLongBypassDelayInSeconds = 30 * 60; + +// The number of days of bandwidth usage statistics that are tracked. +const unsigned int kNumDaysInHistory = 60; + +// The number of days of bandwidth usage statistics that are presented. +const unsigned int kNumDaysInHistorySummary = 30; + +static_assert(kNumDaysInHistorySummary <= kNumDaysInHistory, +              "kNumDaysInHistorySummary should be no larger than " +              "kNumDaysInHistory");  enum DataReductionProxyRequestType {    VIA_DATA_REDUCTION_PROXY,  // A request served by the data reduction proxy. @@ -45,16 +61,6 @@ int64 GetAdjustedOriginalContentLength(      int64 original_content_length,      int64 received_content_length); -// This is only exposed for testing. It is normally called by -// UpdateContentLengthPrefs. -void UpdateContentLengthPrefsForDataReductionProxy( -    int received_content_length, -    int original_content_length, -    bool with_data_reduction_proxy_enabled, -    DataReductionProxyRequestType request_type, -    base::Time now, -    DataReductionProxyCompressionStats* compression_stats); -  }  // namespace data_reduction_proxy  #endif  // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_METRICS_H_ diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics_unittest.cc index 6fe2b60..be48a2a 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics_unittest.cc @@ -6,19 +6,11 @@  #include "base/basictypes.h"  #include "base/compiler_specific.h" -#include "base/memory/ref_counted.h"  #include "base/memory/scoped_ptr.h" -#include "base/prefs/pref_registry_simple.h" -#include "base/prefs/pref_service.h" -#include "base/prefs/testing_pref_service.h" -#include "base/strings/string_number_conversions.h" -#include "base/test/test_simple_task_runner.h"  #include "base/time/time.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config_test_utils.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h"  #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params_test_utils.h" -#include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"  #include "net/base/load_flags.h"  #include "net/log/net_log.h"  #include "net/proxy/proxy_server.h" @@ -30,648 +22,10 @@  using net::MockRead; -namespace { - -int64 GetListPrefInt64Value( -    const base::ListValue& list_update, size_t index) { -  std::string string_value; -  EXPECT_TRUE(list_update.GetString(index, &string_value)); - -  int64 value = 0; -  EXPECT_TRUE(base::StringToInt64(string_value, &value)); -  return value; -} - -}  // namespace -  namespace data_reduction_proxy { -// Test UpdateContentLengthPrefs. -class ChromeNetworkDataSavingMetricsTest : public testing::Test { - protected: -  ChromeNetworkDataSavingMetricsTest() {} - -  void SetUp() override { -    compression_stats_.reset(new DataReductionProxyCompressionStats( -        &pref_service_, -        scoped_refptr<base::TestSimpleTaskRunner>( -            new base::TestSimpleTaskRunner()), -        base::TimeDelta())); -    PrefRegistrySimple* registry = pref_service_.registry(); -    registry->RegisterInt64Pref( -        data_reduction_proxy::prefs::kHttpReceivedContentLength, 0); -    registry->RegisterInt64Pref( -        data_reduction_proxy::prefs::kHttpOriginalContentLength, 0); - -    registry->RegisterListPref(data_reduction_proxy::prefs:: -                                   kDailyHttpOriginalContentLength); -    registry->RegisterListPref(data_reduction_proxy::prefs:: -                                   kDailyHttpReceivedContentLength); -    registry->RegisterListPref( -        data_reduction_proxy::prefs:: -            kDailyOriginalContentLengthWithDataReductionProxyEnabled); -    registry->RegisterListPref( -        data_reduction_proxy::prefs:: -            kDailyContentLengthWithDataReductionProxyEnabled); -    registry->RegisterListPref( -        data_reduction_proxy::prefs:: -            kDailyContentLengthHttpsWithDataReductionProxyEnabled); -    registry->RegisterListPref( -        data_reduction_proxy::prefs:: -            kDailyContentLengthShortBypassWithDataReductionProxyEnabled); -    registry->RegisterListPref( -        data_reduction_proxy::prefs:: -            kDailyContentLengthLongBypassWithDataReductionProxyEnabled); -    registry->RegisterListPref( -        data_reduction_proxy::prefs:: -            kDailyContentLengthUnknownWithDataReductionProxyEnabled); -    registry->RegisterListPref( -        data_reduction_proxy::prefs:: -            kDailyOriginalContentLengthViaDataReductionProxy); -    registry->RegisterListPref( -        data_reduction_proxy::prefs:: -            kDailyContentLengthViaDataReductionProxy); -    registry->RegisterInt64Pref( -        data_reduction_proxy::prefs:: -            kDailyHttpContentLengthLastUpdateDate, 0L); -    registry->RegisterBooleanPref( -        data_reduction_proxy::prefs::kDataReductionProxyEnabled, false); -  } - -  TestingPrefServiceSimple pref_service_; -  scoped_ptr<DataReductionProxyCompressionStats> compression_stats_; -}; - -// The initial last update time used in test. There is no leap second a few -// days around this time used in the test. -// Note: No time zone is specified. Local time will be assumed by -// base::Time::FromString below. -const char kLastUpdateTime[] = "Wed, 18 Sep 2013 03:45:26"; - -class ChromeNetworkDailyDataSavingMetricsTest -    : public ChromeNetworkDataSavingMetricsTest { - protected: -  ChromeNetworkDailyDataSavingMetricsTest() { -    base::Time::FromString(kLastUpdateTime, &now_); -  } - -  void SetUp() override { -    ChromeNetworkDataSavingMetricsTest::SetUp(); - -    // Only create two lists in Setup to test that adding new lists is fine. -    CreatePrefList( -        data_reduction_proxy::prefs::kDailyHttpOriginalContentLength); -    CreatePrefList( -        data_reduction_proxy::prefs::kDailyHttpReceivedContentLength); -  } - -  base::Time FakeNow() const { -    return now_ + now_delta_; -  } - -  void SetFakeTimeDeltaInHours(int hours) { -    now_delta_ = base::TimeDelta::FromHours(hours); -  } - -  void AddFakeTimeDeltaInHours(int hours) { -    now_delta_ += base::TimeDelta::FromHours(hours); -  } - -  // Create daily pref list of |kNumDaysInHistory| zero values. -  void CreatePrefList(const char* pref) { -    base::ListValue* update = compression_stats_->GetList(pref); -    update->Clear(); -    for (size_t i = 0; i < kNumDaysInHistory; ++i) { -      update->Insert(0, new base::StringValue(base::Int64ToString(0))); -    } -  } - -  // Verify the pref list values are equal to the given values. -  // If the count of values is less than kNumDaysInHistory, zeros are assumed -  // at the beginning. -  void VerifyPrefList(const char* pref, const int64* values, size_t count) { -    ASSERT_GE(kNumDaysInHistory, count); -    base::ListValue* update = compression_stats_->GetList(pref); -    ASSERT_EQ(kNumDaysInHistory, update->GetSize()) << "Pref: " << pref; - -    for (size_t i = 0; i < count; ++i) { -      EXPECT_EQ( -          values[i], -          GetListPrefInt64Value(*update, kNumDaysInHistory - count + i)) -          << "index=" << (kNumDaysInHistory - count + i); -    } -    for (size_t i = 0; i < kNumDaysInHistory - count; ++i) { -      EXPECT_EQ(0, GetListPrefInt64Value(*update, i)) << "index=" << i; -    } -  } - -  // Verify all daily data saving pref list values. -  void VerifyDailyDataSavingContentLengthPrefLists( -      const int64* original_values, size_t original_count, -      const int64* received_values, size_t received_count, -      const int64* original_with_data_reduction_proxy_enabled_values, -      size_t original_with_data_reduction_proxy_enabled_count, -      const int64* received_with_data_reduction_proxy_enabled_values, -      size_t received_with_data_reduction_proxy_count, -      const int64* original_via_data_reduction_proxy_values, -      size_t original_via_data_reduction_proxy_count, -      const int64* received_via_data_reduction_proxy_values, -      size_t received_via_data_reduction_proxy_count) { -    VerifyPrefList(data_reduction_proxy::prefs::kDailyHttpOriginalContentLength, -                   original_values, original_count); -    VerifyPrefList(data_reduction_proxy::prefs::kDailyHttpReceivedContentLength, -                   received_values, received_count); -    VerifyPrefList( -        data_reduction_proxy::prefs:: -            kDailyOriginalContentLengthWithDataReductionProxyEnabled, -        original_with_data_reduction_proxy_enabled_values, -        original_with_data_reduction_proxy_enabled_count); -    VerifyPrefList( -        data_reduction_proxy::prefs:: -            kDailyContentLengthWithDataReductionProxyEnabled, -        received_with_data_reduction_proxy_enabled_values, -        received_with_data_reduction_proxy_count); -    VerifyPrefList( -        data_reduction_proxy::prefs:: -            kDailyOriginalContentLengthViaDataReductionProxy, -        original_via_data_reduction_proxy_values, -        original_via_data_reduction_proxy_count); -    VerifyPrefList( -        data_reduction_proxy::prefs:: -            kDailyContentLengthViaDataReductionProxy, -        received_via_data_reduction_proxy_values, -        received_via_data_reduction_proxy_count); -  } - -  // Verify daily data saving pref for request types. -  void VerifyDailyRequestTypeContentLengthPrefLists( -      const int64* original_values, size_t original_count, -      const int64* received_values, size_t received_count, -      const int64* original_with_data_reduction_proxy_enabled_values, -      size_t original_with_data_reduction_proxy_enabled_count, -      const int64* received_with_data_reduction_proxy_enabled_values, -      size_t received_with_data_reduction_proxy_count, -      const int64* https_with_data_reduction_proxy_enabled_values, -      size_t https_with_data_reduction_proxy_enabled_count, -      const int64* short_bypass_with_data_reduction_proxy_enabled_values, -      size_t short_bypass_with_data_reduction_proxy_enabled_count, -      const int64* long_bypass_with_data_reduction_proxy_enabled_values, -      size_t long_bypass_with_data_reduction_proxy_enabled_count, -      const int64* unknown_with_data_reduction_proxy_enabled_values, -      size_t unknown_with_data_reduction_proxy_enabled_count) { -    VerifyPrefList(data_reduction_proxy::prefs::kDailyHttpOriginalContentLength, -                   original_values, original_count); -    VerifyPrefList(data_reduction_proxy::prefs::kDailyHttpReceivedContentLength, -                   received_values, received_count); -    VerifyPrefList( -        data_reduction_proxy::prefs:: -            kDailyOriginalContentLengthWithDataReductionProxyEnabled, -        original_with_data_reduction_proxy_enabled_values, -        original_with_data_reduction_proxy_enabled_count); -    VerifyPrefList( -        data_reduction_proxy::prefs:: -            kDailyContentLengthWithDataReductionProxyEnabled, -        received_with_data_reduction_proxy_enabled_values, -        received_with_data_reduction_proxy_count); -    VerifyPrefList( -        data_reduction_proxy::prefs:: -            kDailyContentLengthHttpsWithDataReductionProxyEnabled, -        https_with_data_reduction_proxy_enabled_values, -        https_with_data_reduction_proxy_enabled_count); -    VerifyPrefList( -        data_reduction_proxy::prefs:: -            kDailyContentLengthShortBypassWithDataReductionProxyEnabled, -        short_bypass_with_data_reduction_proxy_enabled_values, -        short_bypass_with_data_reduction_proxy_enabled_count); -    VerifyPrefList( -        data_reduction_proxy::prefs:: -            kDailyContentLengthLongBypassWithDataReductionProxyEnabled, -        long_bypass_with_data_reduction_proxy_enabled_values, -        long_bypass_with_data_reduction_proxy_enabled_count); -    VerifyPrefList( -        data_reduction_proxy::prefs:: -            kDailyContentLengthUnknownWithDataReductionProxyEnabled, -        unknown_with_data_reduction_proxy_enabled_values, -        unknown_with_data_reduction_proxy_enabled_count); -  } - - private: -  base::Time now_; -  base::TimeDelta now_delta_; -}; - -TEST_F(ChromeNetworkDailyDataSavingMetricsTest, OneResponse) { -  const int64 kOriginalLength = 200; -  const int64 kReceivedLength = 100; -  int64 original[] = {kOriginalLength}; -  int64 received[] = {kReceivedLength}; - -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, VIA_DATA_REDUCTION_PROXY, -      FakeNow(), compression_stats_.get()); -  VerifyDailyDataSavingContentLengthPrefLists( -      original, 1, received, 1, -      original, 1, received, 1, -      original, 1, received, 1); -} - -TEST_F(ChromeNetworkDailyDataSavingMetricsTest, MultipleResponses) { -  const int64 kOriginalLength = 150; -  const int64 kReceivedLength = 100; -  int64 original[] = {kOriginalLength}; -  int64 received[] = {kReceivedLength}; -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      false, UNKNOWN_TYPE, -      FakeNow(), compression_stats_.get()); -  VerifyDailyDataSavingContentLengthPrefLists( -      original, 1, received, 1, -      NULL, 0, NULL, 0, NULL, 0, NULL, 0); - -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, UNKNOWN_TYPE, -      FakeNow(), compression_stats_.get()); -  original[0] += kOriginalLength; -  received[0] += kReceivedLength; -  int64 original_proxy_enabled[] = {kOriginalLength}; -  int64 received_proxy_enabled[] = {kReceivedLength}; -  VerifyDailyDataSavingContentLengthPrefLists( -      original, 1, received, 1, -      original_proxy_enabled, 1, received_proxy_enabled, 1, -      NULL, 0, NULL, 0); - -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, VIA_DATA_REDUCTION_PROXY, -      FakeNow(), compression_stats_.get()); -  original[0] += kOriginalLength; -  received[0] += kReceivedLength; -  original_proxy_enabled[0] += kOriginalLength; -  received_proxy_enabled[0] += kReceivedLength; -  int64 original_via_proxy[] = {kOriginalLength}; -  int64 received_via_proxy[] = {kReceivedLength}; -  VerifyDailyDataSavingContentLengthPrefLists( -      original, 1, received, 1, -      original_proxy_enabled, 1, received_proxy_enabled, 1, -      original_via_proxy, 1, received_via_proxy, 1); - -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, UNKNOWN_TYPE, FakeNow(), compression_stats_.get()); -  original[0] += kOriginalLength; -  received[0] += kReceivedLength; -  original_proxy_enabled[0] += kOriginalLength; -  received_proxy_enabled[0] += kReceivedLength; -  VerifyDailyDataSavingContentLengthPrefLists( -      original, 1, received, 1, -      original_proxy_enabled, 1, received_proxy_enabled, 1, -      original_via_proxy, 1, received_via_proxy, 1); - -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      false, UNKNOWN_TYPE, FakeNow(), compression_stats_.get()); -  original[0] += kOriginalLength; -  received[0] += kReceivedLength; -  VerifyDailyDataSavingContentLengthPrefLists( -      original, 1, received, 1, -      original_proxy_enabled, 1, received_proxy_enabled, 1, -      original_via_proxy, 1, received_via_proxy, 1); -} - -TEST_F(ChromeNetworkDailyDataSavingMetricsTest, RequestType) { -  const int64 kContentLength = 200; -  int64 received[] = {0}; -  int64 https_received[] = {0}; -  int64 total_received[] = {0}; -  int64 proxy_enabled_received[] = {0}; - -  UpdateContentLengthPrefsForDataReductionProxy( -      kContentLength, kContentLength, -      true, HTTPS, -      FakeNow(), compression_stats_.get()); -  total_received[0] += kContentLength; -  proxy_enabled_received[0] += kContentLength; -  https_received[0] += kContentLength; -  VerifyDailyRequestTypeContentLengthPrefLists( -      total_received, 1, total_received, 1, -      proxy_enabled_received, 1, proxy_enabled_received, 1, -      https_received, 1, -      received, 0,  // short bypass -      received, 0,  // long bypass -      received, 0);  // unknown - -  // Data reduction proxy is not enabled. -  UpdateContentLengthPrefsForDataReductionProxy( -      kContentLength, kContentLength, -      false, HTTPS, -      FakeNow(), compression_stats_.get()); -  total_received[0] += kContentLength; -  VerifyDailyRequestTypeContentLengthPrefLists( -      total_received, 1, total_received, 1, -      proxy_enabled_received, 1, proxy_enabled_received, 1, -      https_received, 1, -      received, 0,  // short bypass -      received, 0,  // long bypass -      received, 0);  // unknown - -  UpdateContentLengthPrefsForDataReductionProxy( -      kContentLength, kContentLength, -      true, HTTPS, -      FakeNow(), compression_stats_.get()); -  total_received[0] += kContentLength; -  proxy_enabled_received[0] += kContentLength; -  https_received[0] += kContentLength; -  VerifyDailyRequestTypeContentLengthPrefLists( -      total_received, 1, total_received, 1, -      proxy_enabled_received, 1, proxy_enabled_received, 1, -      https_received, 1, -      received, 0,  // short bypass -      received, 0,  // long bypass -      received, 0);  // unknown - -  UpdateContentLengthPrefsForDataReductionProxy( -      kContentLength, kContentLength, -      true, SHORT_BYPASS, -      FakeNow(), compression_stats_.get()); -  total_received[0] += kContentLength; -  proxy_enabled_received[0] += kContentLength; -  received[0] += kContentLength; -  VerifyDailyRequestTypeContentLengthPrefLists( -      total_received, 1, total_received, 1, -      proxy_enabled_received, 1, proxy_enabled_received, 1, -      https_received, 1, -      received, 1,  // short bypass -      received, 0,  // long bypass -      received, 0);  // unknown - -  UpdateContentLengthPrefsForDataReductionProxy( -      kContentLength, kContentLength, -      true, LONG_BYPASS, -      FakeNow(), compression_stats_.get()); -  total_received[0] += kContentLength; -  proxy_enabled_received[0] += kContentLength; -  VerifyDailyRequestTypeContentLengthPrefLists( -      total_received, 1, total_received, 1,  // total -      proxy_enabled_received, 1, proxy_enabled_received, 1, -      https_received, 1, -      received, 1,  // short bypass -      received, 1,  // long bypass -      received, 0);  // unknown - -  UpdateContentLengthPrefsForDataReductionProxy( -      kContentLength, kContentLength, -      true, UNKNOWN_TYPE, -      FakeNow(), compression_stats_.get()); -  total_received[0] += kContentLength; -  proxy_enabled_received[0] += kContentLength; -  VerifyDailyRequestTypeContentLengthPrefLists( -      total_received, 1, total_received, 1, -      proxy_enabled_received, 1, proxy_enabled_received, 1, -      https_received, 1, -      received, 1,  // short bypass -      received, 1,  // long bypass -      received, 1);  // unknown -} - -TEST_F(ChromeNetworkDailyDataSavingMetricsTest, ForwardOneDay) { -  const int64 kOriginalLength = 200; -  const int64 kReceivedLength = 100; - -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, VIA_DATA_REDUCTION_PROXY, -      FakeNow(), compression_stats_.get()); - -  // Forward one day. -  SetFakeTimeDeltaInHours(24); - -  // Proxy not enabled. Not via proxy. -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      false, UNKNOWN_TYPE, FakeNow(), compression_stats_.get()); - -  int64 original[] = {kOriginalLength, kOriginalLength}; -  int64 received[] = {kReceivedLength, kReceivedLength}; -  int64 original_with_data_reduction_proxy_enabled[] = {kOriginalLength, 0}; -  int64 received_with_data_reduction_proxy_enabled[] = {kReceivedLength, 0}; -  int64 original_via_data_reduction_proxy[] = {kOriginalLength, 0}; -  int64 received_via_data_reduction_proxy[] = {kReceivedLength, 0}; -  VerifyDailyDataSavingContentLengthPrefLists( -      original, 2, -      received, 2, -      original_with_data_reduction_proxy_enabled, 2, -      received_with_data_reduction_proxy_enabled, 2, -      original_via_data_reduction_proxy, 2, -      received_via_data_reduction_proxy, 2); - -  // Proxy enabled. Not via proxy. -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, UNKNOWN_TYPE, FakeNow(), compression_stats_.get()); -  original[1] += kOriginalLength; -  received[1] += kReceivedLength; -  original_with_data_reduction_proxy_enabled[1] += kOriginalLength; -  received_with_data_reduction_proxy_enabled[1] += kReceivedLength; -  VerifyDailyDataSavingContentLengthPrefLists( -      original, 2, -      received, 2, -      original_with_data_reduction_proxy_enabled, 2, -      received_with_data_reduction_proxy_enabled, 2, -      original_via_data_reduction_proxy, 2, -      received_via_data_reduction_proxy, 2); - -  // Proxy enabled and via proxy. -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, VIA_DATA_REDUCTION_PROXY, -      FakeNow(), compression_stats_.get()); -  original[1] += kOriginalLength; -  received[1] += kReceivedLength; -  original_with_data_reduction_proxy_enabled[1] += kOriginalLength; -  received_with_data_reduction_proxy_enabled[1] += kReceivedLength; -  original_via_data_reduction_proxy[1] += kOriginalLength; -  received_via_data_reduction_proxy[1] += kReceivedLength; -  VerifyDailyDataSavingContentLengthPrefLists( -      original, 2, -      received, 2, -      original_with_data_reduction_proxy_enabled, 2, -      received_with_data_reduction_proxy_enabled, 2, -      original_via_data_reduction_proxy, 2, -      received_via_data_reduction_proxy, 2); -} - -TEST_F(ChromeNetworkDailyDataSavingMetricsTest, PartialDayTimeChange) { -  const int64 kOriginalLength = 200; -  const int64 kReceivedLength = 100; -  int64 original[] = {0, kOriginalLength}; -  int64 received[] = {0, kReceivedLength}; - -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, VIA_DATA_REDUCTION_PROXY, -      FakeNow(), compression_stats_.get()); -  VerifyDailyDataSavingContentLengthPrefLists( -      original, 2, received, 2, -      original, 2, received, 2, -      original, 2, received, 2); - -  // Forward 10 hours, stay in the same day. -  // See kLastUpdateTime: "Now" in test is 03:45am. -  SetFakeTimeDeltaInHours(10); -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, VIA_DATA_REDUCTION_PROXY, -      FakeNow(), compression_stats_.get()); -  original[1] += kOriginalLength; -  received[1] += kReceivedLength; -  VerifyDailyDataSavingContentLengthPrefLists( -      original, 2, received, 2, -      original, 2, received, 2, -      original, 2, received, 2); - -  // Forward 11 more hours, comes to tomorrow. -  AddFakeTimeDeltaInHours(11); -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, VIA_DATA_REDUCTION_PROXY, -      FakeNow(), compression_stats_.get()); -  int64 original2[] = {kOriginalLength * 2, kOriginalLength}; -  int64 received2[] = {kReceivedLength * 2, kReceivedLength}; -  VerifyDailyDataSavingContentLengthPrefLists( -      original2, 2, received2, 2, -      original2, 2, received2, 2, -      original2, 2, received2, 2); -} - -TEST_F(ChromeNetworkDailyDataSavingMetricsTest, ForwardMultipleDays) { -  const int64 kOriginalLength = 200; -  const int64 kReceivedLength = 100; -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, VIA_DATA_REDUCTION_PROXY, -      FakeNow(), compression_stats_.get()); - -  // Forward three days. -  SetFakeTimeDeltaInHours(3 * 24); - -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, VIA_DATA_REDUCTION_PROXY, -      FakeNow(), compression_stats_.get()); - -  int64 original[] = {kOriginalLength, 0, 0, kOriginalLength}; -  int64 received[] = {kReceivedLength, 0, 0, kReceivedLength}; -  VerifyDailyDataSavingContentLengthPrefLists( -      original, 4, received, 4, -      original, 4, received, 4, -      original, 4, received, 4); - -  // Forward four more days. -  AddFakeTimeDeltaInHours(4 * 24); -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, VIA_DATA_REDUCTION_PROXY, -      FakeNow(), compression_stats_.get()); -  int64 original2[] = { -    kOriginalLength, 0, 0, kOriginalLength, 0, 0, 0, kOriginalLength, -  }; -  int64 received2[] = { -    kReceivedLength, 0, 0, kReceivedLength, 0, 0, 0, kReceivedLength, -  }; -  VerifyDailyDataSavingContentLengthPrefLists( -      original2, 8, received2, 8, -      original2, 8, received2, 8, -      original2, 8, received2, 8); - -  // Forward |kNumDaysInHistory| more days. -  AddFakeTimeDeltaInHours(kNumDaysInHistory * 24); -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, VIA_DATA_REDUCTION_PROXY, -      FakeNow(), compression_stats_.get()); -  int64 original3[] = {kOriginalLength}; -  int64 received3[] = {kReceivedLength}; -  VerifyDailyDataSavingContentLengthPrefLists( -      original3, 1, received3, 1, -      original3, 1, received3, 1, -      original3, 1, received3, 1); - -  // Forward |kNumDaysInHistory| + 1 more days. -  AddFakeTimeDeltaInHours((kNumDaysInHistory + 1)* 24); -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, VIA_DATA_REDUCTION_PROXY, -      FakeNow(), compression_stats_.get()); -  VerifyDailyDataSavingContentLengthPrefLists( -      original3, 1, received3, 1, -      original3, 1, received3, 1, -      original3, 1, received3, 1); -} - -TEST_F(ChromeNetworkDailyDataSavingMetricsTest, BackwardAndForwardOneDay) { -  const int64 kOriginalLength = 200; -  const int64 kReceivedLength = 100; -  int64 original[] = {kOriginalLength}; -  int64 received[] = {kReceivedLength}; - -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, VIA_DATA_REDUCTION_PROXY, -      FakeNow(), compression_stats_.get()); - -  // Backward one day. -  SetFakeTimeDeltaInHours(-24); -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, VIA_DATA_REDUCTION_PROXY, -      FakeNow(), compression_stats_.get()); -  original[0] += kOriginalLength; -  received[0] += kReceivedLength; -  VerifyDailyDataSavingContentLengthPrefLists( -      original, 1, received, 1, -      original, 1, received, 1, -      original, 1, received, 1); - -  // Then, Forward one day -  AddFakeTimeDeltaInHours(24); -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, VIA_DATA_REDUCTION_PROXY, -      FakeNow(), compression_stats_.get()); -  int64 original2[] = {kOriginalLength * 2, kOriginalLength}; -  int64 received2[] = {kReceivedLength * 2, kReceivedLength}; -  VerifyDailyDataSavingContentLengthPrefLists( -      original2, 2, received2, 2, -      original2, 2, received2, 2, -      original2, 2, received2, 2); -} - -TEST_F(ChromeNetworkDailyDataSavingMetricsTest, BackwardTwoDays) { -  const int64 kOriginalLength = 200; -  const int64 kReceivedLength = 100; -  int64 original[] = {kOriginalLength}; -  int64 received[] = {kReceivedLength}; - -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, VIA_DATA_REDUCTION_PROXY, -      FakeNow(), compression_stats_.get()); -  // Backward two days. -  SetFakeTimeDeltaInHours(-2 * 24); -  UpdateContentLengthPrefsForDataReductionProxy( -      kReceivedLength, kOriginalLength, -      true, VIA_DATA_REDUCTION_PROXY, -      FakeNow(), compression_stats_.get()); -  VerifyDailyDataSavingContentLengthPrefLists( -      original, 1, received, 1, -      original, 1, received, 1, -      original, 1, received, 1); -} - -TEST_F(ChromeNetworkDailyDataSavingMetricsTest, -       GetDataReductionProxyRequestType) { +TEST(ChromeNetworkDailyDataSavingMetricsTest, +     GetDataReductionProxyRequestType) {    scoped_ptr<DataReductionProxyTestContext> test_context =        DataReductionProxyTestContext::Builder()            .WithParamsFlags(DataReductionProxyParams::kAllowed) diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.cc index fbe71f6..c9b9cb1 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.cc @@ -8,16 +8,13 @@  #include "base/bind_helpers.h"  #include "base/metrics/histogram.h"  #include "base/prefs/pref_service.h" -#include "base/single_thread_task_runner.h"  #include "base/strings/string_number_conversions.h"  #include "base/time/time.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h"  #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"  #include "net/base/load_flags.h"  #include "net/http/http_response_headers.h" @@ -82,7 +79,6 @@ DataReductionProxyNetworkDelegate::DataReductionProxyNetworkDelegate(      DataReductionProxyRequestOptions* request_options,      const DataReductionProxyConfigurator* configurator)      : LayeredNetworkDelegate(network_delegate.Pass()), -      ui_task_runner_(NULL),        received_content_length_(0),        original_content_length_(0),        data_reduction_proxy_enabled_(NULL), @@ -98,13 +94,11 @@ DataReductionProxyNetworkDelegate::~DataReductionProxyNetworkDelegate() {  }  void DataReductionProxyNetworkDelegate::InitIODataAndUMA( -    scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,      DataReductionProxyIOData* io_data,      BooleanPrefMember* data_reduction_proxy_enabled,      DataReductionProxyBypassStats* bypass_stats) {    DCHECK(data_reduction_proxy_enabled);    DCHECK(bypass_stats); -  ui_task_runner_ = ui_task_runner;    data_reduction_proxy_io_data_ = io_data;    data_reduction_proxy_enabled_ = data_reduction_proxy_enabled;    data_reduction_proxy_bypass_stats_ = bypass_stats; @@ -218,51 +212,14 @@ void DataReductionProxyNetworkDelegate::AccumulateContentLength(    DCHECK_GE(received_content_length, 0);    DCHECK_GE(original_content_length, 0);    if (data_reduction_proxy_enabled_) { -    ui_task_runner_->PostTask( -        FROM_HERE, -        base::Bind(&DataReductionProxyNetworkDelegate::UpdateContentLengthPrefs, -                   base::Unretained(this), -                   received_content_length, -                   original_content_length, -                   data_reduction_proxy_enabled_->GetValue(), -                   request_type)); +    data_reduction_proxy_io_data_->UpdateContentLengths( +        received_content_length, original_content_length, +        data_reduction_proxy_enabled_->GetValue(), request_type);    }    received_content_length_ += received_content_length;    original_content_length_ += original_content_length;  } -void DataReductionProxyNetworkDelegate::UpdateContentLengthPrefs( -    int received_content_length, -    int original_content_length, -    bool data_reduction_proxy_enabled, -    DataReductionProxyRequestType request_type) { -  if (data_reduction_proxy_io_data_ && -      data_reduction_proxy_io_data_->service()) { -    DataReductionProxyCompressionStats* data_reduction_proxy_compression_stats = -        data_reduction_proxy_io_data_->service()->compression_stats(); -    int64 total_received = data_reduction_proxy_compression_stats->GetInt64( -        data_reduction_proxy::prefs::kHttpReceivedContentLength); -    int64 total_original = data_reduction_proxy_compression_stats->GetInt64( -        data_reduction_proxy::prefs::kHttpOriginalContentLength); -    total_received += received_content_length; -    total_original += original_content_length; -    data_reduction_proxy_compression_stats->SetInt64( -        data_reduction_proxy::prefs::kHttpReceivedContentLength, -        total_received); -    data_reduction_proxy_compression_stats->SetInt64( -        data_reduction_proxy::prefs::kHttpOriginalContentLength, -        total_original); - -    UpdateContentLengthPrefsForDataReductionProxy( -        received_content_length, -        original_content_length, -        data_reduction_proxy_enabled, -        request_type, -        base::Time::Now(), -        data_reduction_proxy_compression_stats); -  } -} -  void OnResolveProxyHandler(const GURL& url,                             int load_flags,                             const net::ProxyConfig& data_reduction_proxy_config, diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h index 08457ba..8cb016e 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h @@ -7,9 +7,7 @@  #include "base/basictypes.h"  #include "base/gtest_prod_util.h" -#include "base/memory/ref_counted.h"  #include "base/memory/scoped_ptr.h" -#include "base/memory/weak_ptr.h"  #include "base/values.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h"  #include "net/base/layered_network_delegate.h" @@ -22,10 +20,6 @@ typedef PrefMember<bool> BooleanPrefMember;  class GURL;  class PrefService; -namespace base { -class SingleThreadTaskRunner; -} -  namespace net {  class HttpResponseHeaders;  class HttpRequestHeaders; @@ -39,11 +33,11 @@ class URLRequest;  namespace data_reduction_proxy { +class DataReductionProxyBypassStats;  class DataReductionProxyConfig;  class DataReductionProxyConfigurator;  class DataReductionProxyIOData;  class DataReductionProxyRequestOptions; -class DataReductionProxyBypassStats;  // DataReductionProxyNetworkDelegate is a LayeredNetworkDelegate that wraps a  // NetworkDelegate and adds Data Reduction Proxy specific logic. @@ -69,7 +63,6 @@ class DataReductionProxyNetworkDelegate : public net::LayeredNetworkDelegate {    // Initializes member variables to record data reduction proxy prefs and    // report UMA.    void InitIODataAndUMA( -      scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,        DataReductionProxyIOData* io_data,        BooleanPrefMember* data_reduction_proxy_enabled,        DataReductionProxyBypassStats* bypass_stats); @@ -117,15 +110,6 @@ class DataReductionProxyNetworkDelegate : public net::LayeredNetworkDelegate {                                 int64 original_content_length,                                 DataReductionProxyRequestType request_type); -  // Records daily data savings statistics to prefs and reports data savings -  // UMA. -  void UpdateContentLengthPrefs(int received_content_length, -                                int original_content_length, -                                bool data_reduction_proxy_enabled, -                                DataReductionProxyRequestType request_type); - -  scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; -    // Total size of all content (excluding headers) that has been received    // over the network.    int64 received_content_length_; diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc index dcf65cd..bf130c2 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate_unittest.cc @@ -4,15 +4,10 @@  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h" -#include "base/bind.h" -#include "base/bind_helpers.h"  #include "base/metrics/field_trial.h" -#include "base/prefs/pref_registry_simple.h" -#include "base/prefs/testing_pref_service.h"  #include "base/strings/string_number_conversions.h"  #include "base/test/histogram_tester.h"  #include "base/time/time.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config_test_utils.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.h" @@ -143,18 +138,10 @@ class DataReductionProxyNetworkDelegateTest : public testing::Test {      return test_context_->io_data();    } -  TestingPrefServiceSimple* pref_service() const { -    return test_context_->pref_service(); -  } -    TestDataReductionProxyConfig* config() {      return test_context_->config();    } -  DataReductionProxyCompressionStats* compression_stats() const { -    return test_context_->data_reduction_proxy_service()->compression_stats(); -  } -    scoped_ptr<DataReductionProxyNetworkDelegate>        data_reduction_proxy_network_delegate_; @@ -411,43 +398,4 @@ TEST_F(DataReductionProxyNetworkDelegateTest, OnResolveProxyHandler) {    EXPECT_FALSE(other_proxy_info.is_direct());  } -TEST_F(DataReductionProxyNetworkDelegateTest, TotalLengths) { -  const int64 kOriginalLength = 200; -  const int64 kReceivedLength = 100; - -  data_reduction_proxy_network_delegate_->data_reduction_proxy_io_data_ = -      io_data(); - -  data_reduction_proxy_network_delegate_->UpdateContentLengthPrefs( -      kReceivedLength, kOriginalLength, -      pref_service()->GetBoolean( -          data_reduction_proxy::prefs::kDataReductionProxyEnabled), -      UNKNOWN_TYPE); - -  EXPECT_EQ(kReceivedLength, -            compression_stats()->GetInt64( -                data_reduction_proxy::prefs::kHttpReceivedContentLength)); -  EXPECT_FALSE(pref_service()->GetBoolean( -      data_reduction_proxy::prefs::kDataReductionProxyEnabled)); -  EXPECT_EQ(kOriginalLength, -            compression_stats()->GetInt64( -                data_reduction_proxy::prefs::kHttpOriginalContentLength)); - -  // Record the same numbers again, and total lengths should be doubled. -  data_reduction_proxy_network_delegate_->UpdateContentLengthPrefs( -      kReceivedLength, kOriginalLength, -      pref_service()->GetBoolean( -          data_reduction_proxy::prefs::kDataReductionProxyEnabled), -      UNKNOWN_TYPE); - -  EXPECT_EQ(kReceivedLength * 2, -            compression_stats()->GetInt64( -                data_reduction_proxy::prefs::kHttpReceivedContentLength)); -  EXPECT_FALSE(pref_service()->GetBoolean( -      data_reduction_proxy::prefs::kDataReductionProxyEnabled)); -  EXPECT_EQ(kOriginalLength * 2, -            compression_stats()->GetInt64( -                data_reduction_proxy::prefs::kHttpOriginalContentLength)); -} -  }  // namespace data_reduction_proxy diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.cc index b4ec8e3..326d92a 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.cc @@ -6,6 +6,7 @@  #include "base/sequenced_task_runner.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h" +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"  #include "net/base/load_flags.h"  #include "net/url_request/url_fetcher.h"  #include "net/url_request/url_request_status.h" @@ -35,11 +36,30 @@ void DataReductionProxyService::EnableCompressionStatisticsLogging(      PrefService* prefs,      scoped_refptr<base::SequencedTaskRunner> ui_task_runner,      const base::TimeDelta& commit_delay) { +  DCHECK(CalledOnValidThread());    DCHECK(!compression_stats_);    compression_stats_.reset(new DataReductionProxyCompressionStats(        prefs, ui_task_runner, commit_delay));  } +void DataReductionProxyService::UpdateContentLengths( +    int received_content_length, +    int original_content_length, +    bool data_reduction_proxy_enabled, +    DataReductionProxyRequestType request_type) { +  DCHECK(CalledOnValidThread()); +  if (compression_stats_) { +    compression_stats_->UpdateContentLengths( +        received_content_length, original_content_length, +        data_reduction_proxy_enabled, request_type); +  } +} + +void DataReductionProxyService::SetUnreachable(bool unreachable) { +  DCHECK(CalledOnValidThread()); +  settings_->SetUnreachable(unreachable); +} +  base::WeakPtr<DataReductionProxyService>  DataReductionProxyService::GetWeakPtr() {    DCHECK(CalledOnValidThread()); diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h index 1760c10..bf44a45 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h @@ -10,6 +10,7 @@  #include "base/memory/scoped_ptr.h"  #include "base/memory/weak_ptr.h"  #include "base/threading/non_thread_safe.h" +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h"  #include "net/url_request/url_fetcher_delegate.h"  class GURL; @@ -66,6 +67,16 @@ class DataReductionProxyService : public base::NonThreadSafe,        scoped_refptr<base::SequencedTaskRunner> ui_task_runner,        const base::TimeDelta& commit_delay); +  // Records daily data savings statistics in |compression_stats_|. +  void UpdateContentLengths(int received_content_length, +                            int original_content_length, +                            bool data_reduction_proxy_enabled, +                            DataReductionProxyRequestType request_type); + +  // Records whether the Data Reduction Proxy is unreachable or not. +  void SetUnreachable(bool unreachable); + +  // Accessor methods.    DataReductionProxyCompressionStats* compression_stats() const {      return compression_stats_.get();    } diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.cc index 838689e..c9b2555 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.cc @@ -9,13 +9,10 @@  #include "base/metrics/histogram.h"  #include "base/prefs/pref_member.h"  #include "base/prefs/pref_service.h" -#include "base/strings/string_number_conversions.h" -#include "base/values.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h"  #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h" -#include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"  #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"  #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switches.h" @@ -24,18 +21,6 @@ namespace {  const char kUMAProxyStartupStateHistogram[] =      "DataReductionProxy.StartupState"; -int64 GetInt64PrefValue(const base::ListValue& list_value, size_t index) { -  int64 val = 0; -  std::string pref_value; -  bool rv = list_value.GetString(index, &pref_value); -  DCHECK(rv); -  if (rv) { -    rv = base::StringToInt64(pref_value, &val); -    DCHECK(rv); -  } -  return val; -} -  bool IsEnabledOnCommandLine() {    const base::CommandLine& command_line =        *base::CommandLine::ForCurrentProcess(); @@ -167,17 +152,8 @@ void DataReductionProxySettings::SetDataReductionProxyAlternativeEnabled(  int64 DataReductionProxySettings::GetDataReductionLastUpdateTime() {    DCHECK(thread_checker_.CalledOnValidThread());    DCHECK(data_reduction_proxy_service_->compression_stats()); -  int64 last_update_internal = -      data_reduction_proxy_service_->compression_stats()->GetInt64( -          prefs::kDailyHttpContentLengthLastUpdateDate); -  base::Time last_update = base::Time::FromInternalValue(last_update_internal); -  return static_cast<int64>(last_update.ToJsTime()); -} - -DataReductionProxySettings::ContentLengthList -DataReductionProxySettings::GetDailyOriginalContentLengths() { -  DCHECK(thread_checker_.CalledOnValidThread()); -  return GetDailyContentLengths(prefs::kDailyHttpOriginalContentLength); +  return +      data_reduction_proxy_service_->compression_stats()->GetLastUpdateTime();  }  void DataReductionProxySettings::SetUnreachable(bool unreachable) { @@ -189,12 +165,6 @@ bool DataReductionProxySettings::IsDataReductionProxyUnreachable() {    return unreachable_;  } -DataReductionProxySettings::ContentLengthList -DataReductionProxySettings::GetDailyReceivedContentLengths() { -  DCHECK(thread_checker_.CalledOnValidThread()); -  return GetDailyContentLengths(prefs::kDailyHttpReceivedContentLength); -} -  PrefService* DataReductionProxySettings::GetOriginalProfilePrefs() {    DCHECK(thread_checker_.CalledOnValidThread());    return prefs_; @@ -237,18 +207,7 @@ void DataReductionProxySettings::OnProxyAlternativeEnabledPrefChange() {  void DataReductionProxySettings::ResetDataReductionStatistics() {    DCHECK(thread_checker_.CalledOnValidThread());    DCHECK(data_reduction_proxy_service_->compression_stats()); -  base::ListValue* original_update = -      data_reduction_proxy_service_->compression_stats()->GetList( -          prefs::kDailyHttpOriginalContentLength); -  base::ListValue* received_update = -      data_reduction_proxy_service_->compression_stats()->GetList( -          prefs::kDailyHttpReceivedContentLength); -  original_update->Clear(); -  received_update->Clear(); -  for (size_t i = 0; i < kNumDaysInHistory; ++i) { -    original_update->AppendString(base::Int64ToString(0)); -    received_update->AppendString(base::Int64ToString(0)); -  } +  data_reduction_proxy_service_->compression_stats()->ResetStatistics();  }  void DataReductionProxySettings::MaybeActivateDataReductionProxy( @@ -292,19 +251,12 @@ void DataReductionProxySettings::RecordStartupState(ProxyStartupState state) {                              PROXY_STARTUP_STATE_COUNT);  } -DataReductionProxySettings::ContentLengthList +ContentLengthList  DataReductionProxySettings::GetDailyContentLengths(const char* pref_name) {    DCHECK(thread_checker_.CalledOnValidThread()); -  DataReductionProxySettings::ContentLengthList content_lengths;    DCHECK(data_reduction_proxy_service_->compression_stats()); -  const base::ListValue* list_value = -      data_reduction_proxy_service_->compression_stats()->GetList(pref_name); -  if (list_value->GetSize() == kNumDaysInHistory) { -    for (size_t i = 0; i < kNumDaysInHistory; ++i) { -      content_lengths.push_back(GetInt64PrefValue(*list_value, i)); -    } -  } -  return content_lengths; +  return data_reduction_proxy_service_->compression_stats()-> +      GetDailyContentLengths(pref_name);  }  void DataReductionProxySettings::GetContentLengths( @@ -313,37 +265,10 @@ void DataReductionProxySettings::GetContentLengths(      int64* received_content_length,      int64* last_update_time) {    DCHECK(thread_checker_.CalledOnValidThread()); -  DCHECK_LE(days, kNumDaysInHistory);    DCHECK(data_reduction_proxy_service_->compression_stats()); -  const base::ListValue* original_list = -      data_reduction_proxy_service_->compression_stats()->GetList( -          prefs::kDailyHttpOriginalContentLength); -  const base::ListValue* received_list = -      data_reduction_proxy_service_->compression_stats()->GetList( -          prefs::kDailyHttpReceivedContentLength); - -  if (original_list->GetSize() != kNumDaysInHistory || -      received_list->GetSize() != kNumDaysInHistory) { -    *original_content_length = 0L; -    *received_content_length = 0L; -    *last_update_time = 0L; -    return; -  } - -  int64 orig = 0L; -  int64 recv = 0L; -  // Include days from the end of the list going backwards. -  for (size_t i = kNumDaysInHistory - days; -       i < kNumDaysInHistory; ++i) { -    orig += GetInt64PrefValue(*original_list, i); -    recv += GetInt64PrefValue(*received_list, i); -  } -  *original_content_length = orig; -  *received_content_length = recv; -  *last_update_time = -      data_reduction_proxy_service_->compression_stats()->GetInt64( -          prefs::kDailyHttpContentLengthLastUpdateDate); +  data_reduction_proxy_service_->compression_stats()->GetContentLengths( +      days, original_content_length, received_content_length, last_update_time);  }  }  // namespace data_reduction_proxy diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h index ca19611..4b87b3c 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h @@ -15,6 +15,7 @@  #include "base/memory/scoped_ptr.h"  #include "base/prefs/pref_member.h"  #include "base/threading/thread_checker.h" +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h"  #include "url/gurl.h"  class PrefService; @@ -27,21 +28,11 @@ class DataReductionProxyIOData;  class DataReductionProxyService;  class DataReductionProxyCompressionStats; -// The number of days of bandwidth usage statistics that are tracked. -const unsigned int kNumDaysInHistory = 60; -  // The header used to request a data reduction proxy pass through. When a  // request is sent to the data reduction proxy with this header, it will respond  // with the original uncompressed response.  extern const char kDataReductionPassThroughHeader[]; -// The number of days of bandwidth usage statistics that are presented. -const unsigned int kNumDaysInHistorySummary = 30; - -static_assert(kNumDaysInHistorySummary <= kNumDaysInHistory, -              "kNumDaysInHistorySummary should be no larger than " -              "kNumDaysInHistory"); -  // Values of the UMA DataReductionProxy.StartupState histogram.  // This enum must remain synchronized with DataReductionProxyStartupState  // in metrics/histograms/histograms.xml. @@ -59,7 +50,6 @@ enum ProxyStartupState {  // associated factory class, and refactor the Java call sites accordingly.  class DataReductionProxySettings {   public: -  typedef std::vector<long long> ContentLengthList;    typedef base::Callback<bool(const std::string&, const std::string&)>        SyntheticFieldTrialRegistrationCallback; @@ -112,11 +102,6 @@ class DataReductionProxySettings {    // daily original and received content lengths.    int64 GetDataReductionLastUpdateTime(); -  // Returns a vector containing the total size of all HTTP content that was -  // received over the last |kNumDaysInHistory| before any compression by the -  // data reduction proxy. Each element in the vector contains one day of data. -  ContentLengthList GetDailyOriginalContentLengths(); -    // Returns aggregate received and original content lengths over the specified    // number of days, as well as the time these stats were last updated.    void GetContentLengths(unsigned int days, @@ -132,10 +117,6 @@ class DataReductionProxySettings {    // some of them should have.    bool IsDataReductionProxyUnreachable(); -  // Returns an vector containing the aggregate received HTTP content in the -  // last |kNumDaysInHistory| days. -  ContentLengthList GetDailyReceivedContentLengths(); -    ContentLengthList GetDailyContentLengths(const char* pref_name);    // Configures data reduction proxy. |at_startup| is true when this method is diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc index 5fda507..3ff88cd 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc @@ -75,9 +75,8 @@ void DataReductionProxySettingsTestBase::SetUp() {      received_update->Insert(0, new base::StringValue(base::Int64ToString(i)));    }    last_update_time_ = base::Time::Now().LocalMidnight(); -  settings_->data_reduction_proxy_service()->compression_stats()->SetInt64( -      prefs::kDailyHttpContentLengthLastUpdateDate, -      last_update_time_.ToInternalValue()); +  pref_service->SetInt64(prefs::kDailyHttpContentLengthLastUpdateDate, +                         last_update_time_.ToInternalValue());  }  template <class C> diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc index 92bb811..90b873b 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc @@ -357,7 +357,7 @@ TEST_F(DataReductionProxySettingsTest, TestEnableLoFiFromCommandLineProxyOff) {  }  TEST_F(DataReductionProxySettingsTest, TestGetDailyContentLengths) { -  DataReductionProxySettings::ContentLengthList result = +  ContentLengthList result =        settings_->GetDailyContentLengths(prefs::kDailyHttpOriginalContentLength);    ASSERT_FALSE(result.empty()); diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.cc index abdf5cc..7333914 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.cc @@ -185,6 +185,9 @@ TestDataReductionProxyIOData::TestDataReductionProxyIOData(    request_options_ = request_options.Pass();    configurator_ = configurator.Pass();    config_client_ = config_client.Pass(); +  bypass_stats_.reset(new DataReductionProxyBypassStats( +      config_.get(), base::Bind(&DataReductionProxyIOData::SetUnreachable, +                                base::Unretained(this))));    io_task_runner_ = task_runner;    ui_task_runner_ = task_runner;  } | 
