diff options
Diffstat (limited to 'chromium/components/data_reduction_proxy/core/browser')
25 files changed, 7 insertions, 5945 deletions
diff --git a/chromium/components/data_reduction_proxy/core/browser/BUILD.gn b/chromium/components/data_reduction_proxy/core/browser/BUILD.gn index 86120f4b4f6..8b2208f6440 100644 --- a/chromium/components/data_reduction_proxy/core/browser/BUILD.gn +++ b/chromium/components/data_reduction_proxy/core/browser/BUILD.gn @@ -7,21 +7,9 @@ if (is_android) { } browser_sources = [ - "data_reduction_proxy_compression_stats.cc", - "data_reduction_proxy_compression_stats.h", "data_reduction_proxy_metrics.h", - "data_reduction_proxy_prefs.cc", - "data_reduction_proxy_prefs.h", - "data_reduction_proxy_service.cc", - "data_reduction_proxy_service.h", "data_reduction_proxy_settings.cc", "data_reduction_proxy_settings.h", - "data_store.cc", - "data_store.h", - "data_usage_store.cc", - "data_usage_store.h", - "db_data_owner.cc", - "db_data_owner.h", ] if (is_android) { @@ -31,9 +19,6 @@ if (is_android) { deps = [ "//base", "//components/data_reduction_proxy/core/common", - "//components/data_reduction_proxy/proto:data_reduction_proxy_proto", - "//components/data_use_measurement/core", - "//components/data_use_measurement/core:ascriber", "//components/pref_registry", "//components/prefs", "//crypto", @@ -42,27 +27,14 @@ if (is_android) { "//url:url", ] } - - java_cpp_enum("data_reduction_proxy_savings_cleared_enum_java") { - sources = [ "data_reduction_proxy_compression_stats.h" ] - } } static_library("browser") { sources = browser_sources - sources += [ - "data_store_impl.cc", - "data_store_impl.h", - ] - public_deps = [ - "//components/data_reduction_proxy/core/common", - "//components/data_reduction_proxy/proto:data_reduction_proxy_proto", - ] + public_deps = [ "//components/data_reduction_proxy/core/common" ] deps = [ "//base", - "//components/data_use_measurement/core", - "//components/data_use_measurement/core:ascriber", "//components/pref_registry", "//components/prefs", "//crypto", @@ -78,71 +50,3 @@ static_library("browser") { defines += [ "USE_GOOGLE_API_KEYS_FOR_AUTH_KEY" ] } } - -static_library("test_support") { - testonly = true - sources = [ - "data_reduction_proxy_settings_test_utils.cc", - "data_reduction_proxy_settings_test_utils.h", - "data_reduction_proxy_test_utils.cc", - "data_reduction_proxy_test_utils.h", - ] - - public_deps = [ ":browser" ] - deps = [ - "//base", - "//components/data_reduction_proxy/core/common", - "//components/data_use_measurement/core", - "//components/data_use_measurement/core:ascriber", - "//components/prefs:test_support", - "//net", - "//net:test_support", - "//services/network:test_support", - "//testing/gmock", - "//testing/gtest", - ] -} - -bundle_data("unit_tests_bundle_data") { - visibility = [ ":unit_tests" ] - testonly = true - sources = [ - "//components/test/data/data_reduction_proxy/direct/block10.html", - "//components/test/data/data_reduction_proxy/direct/block10.html.mock-http-headers", - "//components/test/data/data_reduction_proxy/direct/noblock.html", - "//components/test/data/data_reduction_proxy/direct/noblock.html.mock-http-headers", - "//components/test/data/data_reduction_proxy/proxy/block10.html", - "//components/test/data/data_reduction_proxy/proxy/block10.html.mock-http-headers", - "//components/test/data/data_reduction_proxy/proxy/noblock.html", - "//components/test/data/data_reduction_proxy/proxy/noblock.html.mock-http-headers", - ] - outputs = [ "{{bundle_resources_dir}}/" + - "{{source_root_relative_dir}}/{{source_file_part}}" ] -} - -source_set("unit_tests") { - testonly = true - sources = [ - "data_reduction_proxy_compression_stats_unittest.cc", - "data_reduction_proxy_prefs_unittest.cc", - "data_reduction_proxy_settings_unittest.cc", - "data_usage_store_unittest.cc", - ] - - deps = [ - ":browser", - ":test_support", - ":unit_tests_bundle_data", - "//base", - "//base/test:test_support", - "//build:chromeos_buildflags", - "//components/data_reduction_proxy/proto:data_reduction_proxy_proto", - "//components/data_use_measurement/core:ascriber", - "//components/prefs:test_support", - "//components/variations:test_support", - "//net:test_support", - "//services/network:test_support", - "//testing/gmock", - "//testing/gtest", - ] -} diff --git a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.cc b/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.cc deleted file mode 100644 index 9d8538be04e..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.cc +++ /dev/null @@ -1,890 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h" - -#include <algorithm> -#include <cmath> -#include <utility> -#include <vector> - -#include "base/bind.h" -#include "base/check_op.h" -#include "base/command_line.h" -#include "base/feature_list.h" -#include "base/location.h" -#include "base/memory/raw_ptr.h" -#include "base/metrics/histogram_macros.h" -#include "base/metrics/sparse_histogram.h" -#include "base/strings/string_number_conversions.h" -#include "base/trace_event/trace_event.h" -#include "base/values.h" -#include "build/build_config.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h" -#include "components/data_reduction_proxy/core/browser/data_usage_store.h" -#include "components/data_reduction_proxy/core/common/data_reduction_proxy_features.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" -#include "components/data_reduction_proxy/proto/data_store.pb.h" -#include "components/prefs/pref_service.h" -#include "components/prefs/scoped_user_pref_update.h" -#include "net/base/mime_util.h" - -namespace data_reduction_proxy { - -namespace { - -#define CONCAT(a, b) a##b -// CONCAT1 provides extra level of indirection so that __LINE__ macro expands. -#define CONCAT1(a, b) CONCAT(a, b) -#define UNIQUE_VARNAME CONCAT1(var_, __LINE__) -// We need to use a macro instead of a method because UMA_HISTOGRAM_COUNTS_1M -// requires its first argument to be an inline string and not a variable. -#define RECORD_INT64PREF_TO_HISTOGRAM(pref, uma) \ - int64_t UNIQUE_VARNAME = GetInt64(pref); \ - if (UNIQUE_VARNAME > 0) { \ - UMA_HISTOGRAM_COUNTS_1M(uma, UNIQUE_VARNAME >> 10); \ - } - -// Returns the value at |index| of |list_value| as an int64_t. -int64_t GetInt64PrefValue(const base::ListValue& list_value, size_t index) { - int64_t val = 0; - base::Value::ConstListView list_value_view = list_value.GetList(); - if (index < list_value_view.size() && list_value_view[index].is_string()) { - std::string pref_value = list_value_view[index].GetString(); - bool 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. - base::Value::ListView list_view = list->GetList(); - while (list_view.size() > length) - list->EraseListIter(list_view.begin()); - // Newly added lists are empty. Add entries to back to fill the window, - // each initialized to zero. - while (list_view.size() < length) - list->Append(base::NumberToString(0)); - DCHECK_EQ(length, list_view.size()); -} - -// Increments an int64_t, 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_t length, - base::ListValue* list_update) { - int64_t value = GetInt64PrefValue(*list_update, index) + length; - list_update->Set(index, - std::make_unique<base::Value>(base::NumberToString(value))); -} - -void RecordSavingsClearedMetric(DataReductionProxySavingsClearedReason reason) { - DCHECK_GT(DataReductionProxySavingsClearedReason::REASON_COUNT, reason); - UMA_HISTOGRAM_ENUMERATION( - "DataReductionProxy.SavingsCleared.Reason", reason, - DataReductionProxySavingsClearedReason::REASON_COUNT); -} - -// TODO(rajendrant): Enable aggregate metrics recording in x86 Android. -// http://crbug.com/865373 -#if !defined(OS_ANDROID) || !defined(ARCH_CPU_X86) -const double kSecondsPerWeek = - base::Time::kMicrosecondsPerWeek / base::Time::kMicrosecondsPerSecond; - -// Returns the week number for the current time. The epoch time is treated as -// week=0. -int32_t GetCurrentWeekNumber(const base::Time& now) { - double now_in_seconds = now.ToDoubleT(); - return now_in_seconds / kSecondsPerWeek; -} - -// Adds |value| to the item at |key| in the preference dictionary found at -// |pref|. If |key| is not found it will be inserted. -void AddToDictionaryPref(PrefService* pref_service, - const std::string& pref, - int key, - int value) { - DictionaryPrefUpdate pref_update(pref_service, pref); - base::DictionaryValue* pref_dict = pref_update.Get(); - const std::string key_str = base::NumberToString(key); - base::Value* dict_value = pref_dict->FindKey(key_str); - if (dict_value) - value += dict_value->GetInt(); - pref_dict->SetKey(key_str, base::Value(value)); -} - -// Moves the dictionary stored in preference |pref_src| to |pref_dst|, and -// clears the preference |pref_src|. -void MoveAndClearDictionaryPrefs(PrefService* pref_service, - const std::string& pref_dst, - const std::string& pref_src) { - DictionaryPrefUpdate pref_update_dst(pref_service, pref_dst); - base::DictionaryValue* pref_dict_dst = pref_update_dst.Get(); - DictionaryPrefUpdate pref_update_src(pref_service, pref_src); - base::DictionaryValue* pref_dict_src = pref_update_src.Get(); - pref_dict_dst->DictClear(); - pref_dict_dst->Swap(pref_dict_src); - DCHECK(pref_dict_src->DictEmpty()); -} - -void MaybeInitWeeklyAggregateDataUsePrefs(const base::Time& now, - PrefService* pref_service) { - int saved_week = pref_service->GetInteger(prefs::kThisWeekNumber); - int current_week = GetCurrentWeekNumber(now); - - if (saved_week == current_week) - return; - - pref_service->SetInteger(prefs::kThisWeekNumber, current_week); - if (current_week == saved_week + 1) { - // The next week has just started. Move the data use aggregate prefs for - // this week to last week, and clear the prefs for this week. - MoveAndClearDictionaryPrefs(pref_service, - prefs::kLastWeekServicesDownstreamBackgroundKB, - prefs::kThisWeekServicesDownstreamBackgroundKB); - MoveAndClearDictionaryPrefs(pref_service, - prefs::kLastWeekServicesDownstreamForegroundKB, - prefs::kThisWeekServicesDownstreamForegroundKB); - MoveAndClearDictionaryPrefs( - pref_service, prefs::kLastWeekUserTrafficContentTypeDownstreamKB, - prefs::kThisWeekUserTrafficContentTypeDownstreamKB); - } else { - // Current week is too different than the last time data use aggregate prefs - // were updated. This may happen if Chrome was opened after a long time, or - // due to system clock being changed backward or forward. Clear all prefs in - // this case. - pref_service->ClearPref(prefs::kLastWeekServicesDownstreamBackgroundKB); - pref_service->ClearPref(prefs::kLastWeekServicesDownstreamForegroundKB); - pref_service->ClearPref(prefs::kLastWeekUserTrafficContentTypeDownstreamKB); - pref_service->ClearPref(prefs::kThisWeekServicesDownstreamBackgroundKB); - pref_service->ClearPref(prefs::kThisWeekServicesDownstreamForegroundKB); - pref_service->ClearPref(prefs::kThisWeekUserTrafficContentTypeDownstreamKB); - } -} - -// Records the key-value pairs in the dictionary in a sparse histogram. -void RecordDictionaryToHistogram(const std::string& histogram_name, - const base::DictionaryValue* dictionary) { - base::HistogramBase* histogram = base::SparseHistogram::FactoryGet( - histogram_name, base::HistogramBase::kUmaTargetedHistogramFlag); - for (auto entry : dictionary->DictItems()) { - int key; - int value = entry.second.GetInt(); - if (value > 0 && base::StringToInt(entry.first, &key)) { - histogram->AddCount(key, value); - } - } -} -#endif - -} // namespace - -class DataReductionProxyCompressionStats::DailyContentLengthUpdate { - public: - DailyContentLengthUpdate( - DataReductionProxyCompressionStats* compression_stats, - const char* pref_path) - : update_(nullptr), - compression_stats_(compression_stats), - pref_path_(pref_path) {} - - DailyContentLengthUpdate(const DailyContentLengthUpdate&) = delete; - DailyContentLengthUpdate& operator=(const DailyContentLengthUpdate&) = delete; - - void UpdateForDateChange(int days_since_last_update) { - if (days_since_last_update) { - MaybeInitialize(); - MaintainContentLengthPrefForDateChange(days_since_last_update); - } - } - - // Update the lengths for the current day. - void Add(int64_t content_length) { - if (content_length != 0) { - MaybeInitialize(); - AddInt64ToListPref(kNumDaysInHistory - 1, content_length, update_); - } - } - - int64_t GetListPrefValue(size_t index) { - MaybeInitialize(); - return std::max(GetInt64PrefValue(*update_, index), - static_cast<int64_t>(0)); - } - - private: - void MaybeInitialize() { - if (update_) - return; - - update_ = compression_stats_->GetList(pref_path_); - // New empty lists may have been created. Maintain the invariant that - // there should be exactly |kNumDaysInHistory| days in the histories. - MaintainContentLengthPrefsWindow(update_, kNumDaysInHistory); - } - - // 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_->ClearList(); - - 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_->Append(base::NumberToString(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); - } - - // Non-owned. Lazily initialized, set to nullptr until initialized. - raw_ptr<base::ListValue> update_; - // Non-owned pointer. - raw_ptr<DataReductionProxyCompressionStats> compression_stats_; - // The path of the content length pref for |this|. - const char* pref_path_; -}; - -// 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 DataReductionProxyCompressionStats::DailyDataSavingUpdate { - public: - DailyDataSavingUpdate(DataReductionProxyCompressionStats* compression_stats, - const char* original_pref_path, - const char* received_pref_path) - : original_(compression_stats, original_pref_path), - received_(compression_stats, received_pref_path) {} - - DailyDataSavingUpdate(const DailyDataSavingUpdate&) = delete; - DailyDataSavingUpdate& operator=(const DailyDataSavingUpdate&) = delete; - - void UpdateForDateChange(int days_since_last_update) { - original_.UpdateForDateChange(days_since_last_update); - received_.UpdateForDateChange(days_since_last_update); - } - - // Update the lengths for the current day. - void Add(int64_t original_content_length, int64_t received_content_length) { - original_.Add(original_content_length); - received_.Add(received_content_length); - } - - int64_t GetOriginalListPrefValue(size_t index) { - return original_.GetListPrefValue(index); - } - int64_t GetReceivedListPrefValue(size_t index) { - return received_.GetListPrefValue(index); - } - - private: - DailyContentLengthUpdate original_; - DailyContentLengthUpdate received_; -}; - -DataReductionProxyCompressionStats::DataReductionProxyCompressionStats( - DataReductionProxyService* service, - PrefService* prefs, - const base::TimeDelta& delay) - : service_(service), - pref_service_(prefs), - delay_(delay), - data_usage_map_is_dirty_(false), - current_data_usage_load_status_(NOT_LOADED) { - DCHECK(service); - DCHECK(prefs); - DCHECK_GE(delay.InMilliseconds(), 0); - Init(); -} - -DataReductionProxyCompressionStats::~DataReductionProxyCompressionStats() { - DCHECK(thread_checker_.CalledOnValidThread()); - - if (current_data_usage_load_status_ == LOADED) - PersistDataUsage(); - - WritePrefs(); -} - -void DataReductionProxyCompressionStats::Init() { - DCHECK(thread_checker_.CalledOnValidThread()); - - data_usage_reporting_enabled_.Init( - prefs::kDataUsageReportingEnabled, pref_service_, - base::BindRepeating( - &DataReductionProxyCompressionStats::OnDataUsageReportingPrefChanged, - weak_factory_.GetWeakPtr())); - - if (data_usage_reporting_enabled_.GetValue()) { - current_data_usage_load_status_ = LOADING; - service_->LoadCurrentDataUsageBucket(base::BindRepeating( - &DataReductionProxyCompressionStats::OnCurrentDataUsageLoaded, - weak_factory_.GetWeakPtr())); - } - - InitializeWeeklyAggregateDataUse(base::Time::Now()); - - if (delay_.is_zero()) - return; - - // Init all int64_t prefs. - InitInt64Pref(prefs::kDailyHttpContentLengthLastUpdateDate); - InitInt64Pref(prefs::kHttpReceivedContentLength); - InitInt64Pref(prefs::kHttpOriginalContentLength); - - // Init all list prefs. - InitListPref(prefs::kDailyHttpOriginalContentLength); - InitListPref(prefs::kDailyHttpReceivedContentLength); -} - -void DataReductionProxyCompressionStats::RecordDataUseWithMimeType( - int64_t data_used, - int64_t original_size, - bool data_saver_enabled, - const std::string& mime_type, - bool is_user_traffic, - data_use_measurement::DataUseUserData::DataUseContentType content_type, - int32_t service_hash_code) { - DCHECK(thread_checker_.CalledOnValidThread()); - TRACE_EVENT0("loading", - "DataReductionProxyCompressionStats::RecordDataUseWithMimeType"); - - IncreaseInt64Pref(data_reduction_proxy::prefs::kHttpReceivedContentLength, - data_used); - IncreaseInt64Pref(data_reduction_proxy::prefs::kHttpOriginalContentLength, - original_size); - - RecordRequestSizePrefs(data_used, original_size, data_saver_enabled, - mime_type, base::Time::Now()); - RecordWeeklyAggregateDataUse( - base::Time::Now(), std::round(static_cast<double>(data_used) / 1024), - is_user_traffic, content_type, service_hash_code); -} - -void DataReductionProxyCompressionStats::InitInt64Pref(const char* pref) { - int64_t pref_value = pref_service_->GetInt64(pref); - pref_map_[pref] = pref_value; -} - -void DataReductionProxyCompressionStats::InitListPref(const char* pref) { - std::unique_ptr<base::ListValue> pref_value = - pref_service_->GetList(pref)->CreateDeepCopy(); - list_pref_map_[pref] = std::move(pref_value); -} - -int64_t DataReductionProxyCompressionStats::GetInt64(const char* pref_path) { - if (delay_.is_zero()) - return pref_service_->GetInt64(pref_path); - - auto iter = pref_map_.find(pref_path); - return iter->second; -} - -void DataReductionProxyCompressionStats::SetInt64(const char* pref_path, - int64_t pref_value) { - if (delay_.is_zero()) { - pref_service_->SetInt64(pref_path, pref_value); - return; - } - - DelayedWritePrefs(); - pref_map_[pref_path] = pref_value; -} - -void DataReductionProxyCompressionStats::IncreaseInt64Pref( - const char* pref_path, - int64_t delta) { - SetInt64(pref_path, GetInt64(pref_path) + delta); -} - -base::ListValue* DataReductionProxyCompressionStats::GetList( - const char* pref_path) { - if (delay_.is_zero()) - return ListPrefUpdate(pref_service_, pref_path).Get(); - - DelayedWritePrefs(); - auto it = list_pref_map_.find(pref_path); - if (it == list_pref_map_.end()) - return nullptr; - return it->second.get(); -} - -void DataReductionProxyCompressionStats::WritePrefs() { - DCHECK(thread_checker_.CalledOnValidThread()); - if (delay_.is_zero()) - return; - - for (auto iter = pref_map_.begin(); iter != pref_map_.end(); ++iter) { - pref_service_->SetInt64(iter->first, iter->second); - } - - for (auto iter = list_pref_map_.begin(); iter != list_pref_map_.end(); - ++iter) { - TransferList(*(iter->second.get()), - ListPrefUpdate(pref_service_, iter->first).Get()); - } -} - -int64_t DataReductionProxyCompressionStats::GetLastUpdateTime() { - int64_t last_update_internal = - GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate); - base::Time last_update = base::Time::FromInternalValue(last_update_internal); - return static_cast<int64_t>(last_update.ToJsTime()); -} - -void DataReductionProxyCompressionStats::ResetStatistics() { - base::ListValue* original_update = - GetList(prefs::kDailyHttpOriginalContentLength); - base::ListValue* received_update = - GetList(prefs::kDailyHttpReceivedContentLength); - original_update->ClearList(); - received_update->ClearList(); - for (size_t i = 0; i < kNumDaysInHistory; ++i) { - original_update->Append(base::NumberToString(0)); - received_update->Append(base::NumberToString(0)); - } -} - -int64_t DataReductionProxyCompressionStats::GetHttpReceivedContentLength() { - return GetInt64(prefs::kHttpReceivedContentLength); -} - -int64_t DataReductionProxyCompressionStats::GetHttpOriginalContentLength() { - return GetInt64(prefs::kHttpOriginalContentLength); -} - -ContentLengthList DataReductionProxyCompressionStats::GetDailyContentLengths( - const char* pref_name) { - ContentLengthList content_lengths; - const base::ListValue* list_value = GetList(pref_name); - if (list_value->GetList().size() == 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_t* original_content_length, - int64_t* received_content_length, - int64_t* 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->GetList().size() != kNumDaysInHistory || - received_list->GetList().size() != kNumDaysInHistory) { - *original_content_length = 0L; - *received_content_length = 0L; - *last_update_time = 0L; - return; - } - - int64_t orig = 0L; - int64_t 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::GetHistoricalDataUsage( - HistoricalDataUsageCallback get_data_usage_callback) { - GetHistoricalDataUsageImpl(std::move(get_data_usage_callback), - base::Time::Now()); -} - -void DataReductionProxyCompressionStats::DeleteBrowsingHistory( - const base::Time& start, - const base::Time& end) { - DCHECK_NE(LOADING, current_data_usage_load_status_); - - if (!data_usage_map_last_updated_.is_null() && - DataUsageStore::BucketOverlapsInterval(data_usage_map_last_updated_, - start, end)) { - data_usage_map_.clear(); - data_usage_map_last_updated_ = base::Time(); - data_usage_map_is_dirty_ = false; - } - - service_->DeleteBrowsingHistory(start, end); - - RecordSavingsClearedMetric(DataReductionProxySavingsClearedReason:: - USER_ACTION_DELETE_BROWSING_HISTORY); -} - -void DataReductionProxyCompressionStats::OnCurrentDataUsageLoaded( - std::unique_ptr<DataUsageBucket> data_usage) { - // Exit early if the pref was turned off before loading from storage - // completed. - if (!data_usage_reporting_enabled_.GetValue()) { - DCHECK_EQ(NOT_LOADED, current_data_usage_load_status_); - DCHECK(data_usage_map_.empty()); - current_data_usage_load_status_ = NOT_LOADED; - return; - } else { - DCHECK_EQ(LOADING, current_data_usage_load_status_); - } - - DCHECK(data_usage_map_last_updated_.is_null()); - DCHECK(data_usage_map_.empty()); - - // We currently do not break down by connection type. However, we use a schema - // that makes it easy to transition to a connection based breakdown without - // requiring a data migration. - DCHECK(data_usage->connection_usage_size() == 0 || - data_usage->connection_usage_size() == 1); - for (const auto& connection_usage : data_usage->connection_usage()) { - for (const auto& site_usage : connection_usage.site_usage()) { - data_usage_map_[site_usage.hostname()] = - std::make_unique<PerSiteDataUsage>(site_usage); - } - } - - data_usage_map_last_updated_ = - base::Time::FromInternalValue(data_usage->last_updated_timestamp()); - // Record if there was a read error. - if (data_usage->had_read_error()) { - RecordSavingsClearedMetric( - DataReductionProxySavingsClearedReason::PREFS_PARSE_ERROR); - } - - current_data_usage_load_status_ = LOADED; -} - -void DataReductionProxyCompressionStats::SetDataUsageReportingEnabled( - bool enabled) { - DCHECK(thread_checker_.CalledOnValidThread()); - if (data_usage_reporting_enabled_.GetValue() != enabled) { - data_usage_reporting_enabled_.SetValue(enabled); - OnDataUsageReportingPrefChanged(); - } -} - -void DataReductionProxyCompressionStats::ClearDataSavingStatistics( - DataReductionProxySavingsClearedReason reason) { - DeleteHistoricalDataUsage(); - - pref_service_->ClearPref(prefs::kDailyHttpContentLengthLastUpdateDate); - pref_service_->ClearPref(prefs::kHttpReceivedContentLength); - pref_service_->ClearPref(prefs::kHttpOriginalContentLength); - - pref_service_->ClearPref(prefs::kDailyHttpOriginalContentLength); - pref_service_->ClearPref(prefs::kDailyHttpReceivedContentLength); - - for (auto iter = list_pref_map_.begin(); iter != list_pref_map_.end(); - ++iter) { - iter->second->ClearList(); - } - - RecordSavingsClearedMetric(reason); -} - -void DataReductionProxyCompressionStats::DelayedWritePrefs() { - if (pref_writer_timer_.IsRunning()) - return; - - pref_writer_timer_.Start(FROM_HERE, delay_, this, - &DataReductionProxyCompressionStats::WritePrefs); -} - -void DataReductionProxyCompressionStats::TransferList( - const base::Value& from_list, - base::Value* to_list) { - *to_list = from_list.Clone(); -} - -void DataReductionProxyCompressionStats::RecordRequestSizePrefs( - int64_t data_used, - int64_t original_size, - bool with_data_saver_enabled, - const std::string& mime_type, - const 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_t 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(); - - DailyDataSavingUpdate total( - this, data_reduction_proxy::prefs::kDailyHttpOriginalContentLength, - data_reduction_proxy::prefs::kDailyHttpReceivedContentLength); - - int days_since_last_update = (midnight - then_midnight).InDays(); - if (days_since_last_update) { - // Record the last update time in microseconds in UTC. - SetInt64(data_reduction_proxy::prefs::kDailyHttpContentLengthLastUpdateDate, - midnight.ToInternalValue()); - - // 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. - // (Actually resetting the numbers when we're more than a day off - // happens elsewhere.) - if (days_since_last_update < -1) { - RecordSavingsClearedMetric( - DataReductionProxySavingsClearedReason::SYSTEM_CLOCK_MOVED_BACK); - } - } - - total.UpdateForDateChange(days_since_last_update); - total.Add(original_size, data_used); -} - -void DataReductionProxyCompressionStats::RecordDataUseByHost( - const std::string& data_usage_host, - int64_t data_used, - int64_t original_size, - const base::Time time) { - DCHECK(thread_checker_.CalledOnValidThread()); - if (current_data_usage_load_status_ != LOADED) - return; - - DCHECK(data_usage_reporting_enabled_.GetValue()); - - if (!DataUsageStore::AreInSameInterval(data_usage_map_last_updated_, time)) { - PersistDataUsage(); - data_usage_map_.clear(); - data_usage_map_last_updated_ = base::Time(); - } - - std::string normalized_host = NormalizeHostname(data_usage_host); - auto j = data_usage_map_.insert( - std::make_pair(normalized_host, std::make_unique<PerSiteDataUsage>())); - PerSiteDataUsage* per_site_usage = j.first->second.get(); - per_site_usage->set_hostname(normalized_host); - per_site_usage->set_original_size(per_site_usage->original_size() + - original_size); - per_site_usage->set_data_used(per_site_usage->data_used() + data_used); - - data_usage_map_last_updated_ = time; - data_usage_map_is_dirty_ = true; -} - -void DataReductionProxyCompressionStats::PersistDataUsage() { - DCHECK(current_data_usage_load_status_ == LOADED); - - if (data_usage_map_is_dirty_) { - std::unique_ptr<DataUsageBucket> data_usage_bucket(new DataUsageBucket()); - data_usage_bucket->set_last_updated_timestamp( - data_usage_map_last_updated_.ToInternalValue()); - PerConnectionDataUsage* connection_usage = - data_usage_bucket->add_connection_usage(); - for (auto i = data_usage_map_.begin(); i != data_usage_map_.end(); ++i) { - PerSiteDataUsage* per_site_usage = connection_usage->add_site_usage(); - per_site_usage->CopyFrom(*(i->second.get())); - } - service_->StoreCurrentDataUsageBucket(std::move(data_usage_bucket)); - } - - data_usage_map_is_dirty_ = false; -} - -void DataReductionProxyCompressionStats::DeleteHistoricalDataUsage() { - // This method does not support being called in |LOADING| status since this - // means that the in-memory data usage will get populated when data usage - // loads, which will undo the clear below. This method is called when users - // click on the "Clear Data" button, or when user deletes the extension. In - // both cases, enough time has passed since startup to load current data - // usage. Technically, this could occur, and will have the effect of not - // clearing data from the current bucket. - // TODO(kundaji): Use cancellable tasks and remove this DCHECK. - DCHECK(current_data_usage_load_status_ != LOADING); - - data_usage_map_.clear(); - data_usage_map_last_updated_ = base::Time(); - data_usage_map_is_dirty_ = false; - - service_->DeleteHistoricalDataUsage(); -} - -void DataReductionProxyCompressionStats::GetHistoricalDataUsageImpl( - HistoricalDataUsageCallback get_data_usage_callback, - const base::Time& now) { -#if !defined(OS_ANDROID) - if (current_data_usage_load_status_ != LOADED) { - // If current data usage has not yet loaded, we return an empty array. The - // extension can retry after a slight delay. - // This use case is unlikely to occur in practice since current data usage - // should have sufficient time to load before user tries to view data usage. - std::move(get_data_usage_callback) - .Run(std::make_unique<std::vector<DataUsageBucket>>()); - return; - } -#endif - - if (current_data_usage_load_status_ == LOADED) - PersistDataUsage(); - - if (!data_usage_map_last_updated_.is_null() && - !DataUsageStore::AreInSameInterval(data_usage_map_last_updated_, now)) { - data_usage_map_.clear(); - data_usage_map_last_updated_ = base::Time(); - - // Force the last bucket to be for the current interval. - std::unique_ptr<DataUsageBucket> data_usage_bucket(new DataUsageBucket()); - data_usage_bucket->set_last_updated_timestamp(now.ToInternalValue()); - service_->StoreCurrentDataUsageBucket(std::move(data_usage_bucket)); - } - - service_->LoadHistoricalDataUsage(std::move(get_data_usage_callback)); -} - -void DataReductionProxyCompressionStats::OnDataUsageReportingPrefChanged() { - if (data_usage_reporting_enabled_.GetValue()) { - if (current_data_usage_load_status_ == NOT_LOADED) { - current_data_usage_load_status_ = LOADING; - service_->LoadCurrentDataUsageBucket(base::BindOnce( - &DataReductionProxyCompressionStats::OnCurrentDataUsageLoaded, - weak_factory_.GetWeakPtr())); - } - } else { -// Don't delete the historical data on Android, but clear the map. -#if defined(OS_ANDROID) - if (current_data_usage_load_status_ == LOADED) - PersistDataUsage(); - - data_usage_map_.clear(); - data_usage_map_last_updated_ = base::Time(); - data_usage_map_is_dirty_ = false; -#else - DeleteHistoricalDataUsage(); -#endif - current_data_usage_load_status_ = NOT_LOADED; - } -} - -void DataReductionProxyCompressionStats::InitializeWeeklyAggregateDataUse( - const base::Time& now) { - // TODO(rajendrant): Enable aggregate metrics recording in x86 Android. - // http://crbug.com/865373 -#if !defined(OS_ANDROID) || !defined(ARCH_CPU_X86) - MaybeInitWeeklyAggregateDataUsePrefs(now, pref_service_); - // Record the histograms that will show up in the user feedback. - RecordDictionaryToHistogram( - "DataReductionProxy.ThisWeekAggregateKB.Services.Downstream.Background", - pref_service_->GetDictionary( - prefs::kThisWeekServicesDownstreamBackgroundKB)); - RecordDictionaryToHistogram( - "DataReductionProxy.ThisWeekAggregateKB.Services.Downstream.Foreground", - pref_service_->GetDictionary( - prefs::kThisWeekServicesDownstreamForegroundKB)); - RecordDictionaryToHistogram( - "DataReductionProxy.ThisWeekAggregateKB.UserTraffic.Downstream." - "ContentType", - pref_service_->GetDictionary( - prefs::kThisWeekUserTrafficContentTypeDownstreamKB)); - RecordDictionaryToHistogram( - "DataReductionProxy.LastWeekAggregateKB.Services.Downstream.Background", - pref_service_->GetDictionary( - prefs::kLastWeekServicesDownstreamBackgroundKB)); - RecordDictionaryToHistogram( - "DataReductionProxy.LastWeekAggregateKB.Services.Downstream.Foreground", - pref_service_->GetDictionary( - prefs::kLastWeekServicesDownstreamForegroundKB)); - RecordDictionaryToHistogram( - "DataReductionProxy.LastWeekAggregateKB.UserTraffic.Downstream." - "ContentType", - pref_service_->GetDictionary( - prefs::kLastWeekUserTrafficContentTypeDownstreamKB)); -#endif -} - -void DataReductionProxyCompressionStats::RecordWeeklyAggregateDataUse( - const base::Time& now, - int32_t data_used_kb, - bool is_user_request, - data_use_measurement::DataUseUserData::DataUseContentType content_type, - int32_t service_hash_code) { - // TODO(rajendrant): Enable aggregate metrics recording in x86 Android. - // http://crbug.com/865373 -#if !defined(OS_ANDROID) || !defined(ARCH_CPU_X86) - // Update the prefs if this is a new week. This can happen when chrome is open - // for weeks without being closed. - MaybeInitWeeklyAggregateDataUsePrefs(now, pref_service_); - if (is_user_request) { - AddToDictionaryPref(pref_service_, - prefs::kThisWeekUserTrafficContentTypeDownstreamKB, - content_type, data_used_kb); - } else { - bool is_app_foreground = true; - if (is_app_foreground) { - AddToDictionaryPref(pref_service_, - prefs::kThisWeekServicesDownstreamForegroundKB, - service_hash_code, data_used_kb); - } else { - AddToDictionaryPref(pref_service_, - prefs::kThisWeekServicesDownstreamBackgroundKB, - service_hash_code, data_used_kb); - } - } -#endif -} - -// static -std::string DataReductionProxyCompressionStats::NormalizeHostname( - const std::string& host) { - size_t pos = host.find("://"); - if (pos != std::string::npos) - return host.substr(pos + 3); - - return host; -} - -} // namespace data_reduction_proxy diff --git a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h b/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h deleted file mode 100644 index cc86976e7fb..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h +++ /dev/null @@ -1,306 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_COMPRESSION_STATS_H_ -#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_COMPRESSION_STATS_H_ - -#include <stddef.h> -#include <stdint.h> - -#include <map> -#include <memory> -#include <string> -#include <unordered_map> - -#include "base/memory/raw_ptr.h" -#include "base/memory/ref_counted.h" -#include "base/memory/weak_ptr.h" -#include "base/threading/thread_checker.h" -#include "base/time/time.h" -#include "base/timer/timer.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h" -#include "components/data_reduction_proxy/core/browser/db_data_owner.h" -#include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h" -#include "components/data_use_measurement/core/data_use_user_data.h" -#include "components/prefs/pref_member.h" - -class PrefService; - -namespace base { -class ListValue; -class Value; -} - -namespace data_reduction_proxy { -class DataReductionProxyService; -class DataUsageBucket; -class PerSiteDataUsage; - -// These values are persisted to logs. Entries should not be renumbered and -// numeric values should never be reused. -// -// A Java counterpart will be generated for this enum. -// GENERATED_JAVA_ENUM_PACKAGE: ( -// org.chromium.chrome.browser.settings.datareduction) -enum class DataReductionProxySavingsClearedReason { - SYSTEM_CLOCK_MOVED_BACK, - PREFS_PARSE_ERROR, - USER_ACTION_EXTENSION, - USER_ACTION_SETTINGS_MENU, - USER_ACTION_DELETE_BROWSING_HISTORY, - // NOTE: always keep this entry at the end. Add new result types only - // immediately above this line. Make sure to update the corresponding - // histogram enum accordingly. - REASON_COUNT, -}; - -// Data reduction proxy delayed pref service reduces the number calls to pref -// service by storing prefs in memory and writing to the given PrefService after -// |delay| amount of time. If |delay| is zero, the delayed pref service writes -// directly to the PrefService and does not store the prefs in memory. All -// prefs must be stored and read on the UI thread. -class DataReductionProxyCompressionStats { - public: - typedef std::unordered_map<std::string, std::unique_ptr<PerSiteDataUsage>> - SiteUsageMap; - - // Collects and store data usage and compression statistics. Basic data usage - // stats are stored in browser preferences. More detailed stats broken down - // by site and internet type are stored in |DataReductionProxyStore|. - // - // To store basic stats, it constructs a data reduction proxy delayed pref - // service object using |pref_service|. Writes prefs to |pref_service| after - // |delay| and stores them in |pref_map_| and |list_pref_map| between writes. - // If |delay| is zero, writes directly to the PrefService and does not store - // in the maps. - DataReductionProxyCompressionStats(DataReductionProxyService* service, - PrefService* pref_service, - const base::TimeDelta& delay); - - DataReductionProxyCompressionStats( - const DataReductionProxyCompressionStats&) = delete; - DataReductionProxyCompressionStats& operator=( - const DataReductionProxyCompressionStats&) = delete; - - ~DataReductionProxyCompressionStats(); - - // Records detailed data usage broken down by |mime_type|. Also records daily - // data savings statistics to prefs and reports data savings UMA. |data_used| - // and |original_size| are measured in bytes. - void RecordDataUseWithMimeType( - int64_t compressed_size, - int64_t original_size, - bool data_reduction_proxy_enabled, - const std::string& mime_type, - bool is_user_traffic, - data_use_measurement::DataUseUserData::DataUseContentType content_type, - int32_t service_hash_code); - - // Record data usage and original size of request broken down by host. - // |original_request_size| and |data_used| are in bytes. |time| is the time at - // which the data usage occurred. This method should be called in real time, - // so |time| is expected to be |Time::Now()|. - void RecordDataUseByHost(const std::string& data_usage_host, - int64_t original_request_size, - int64_t data_used, - const base::Time time); - - // Returns the time in milliseconds since epoch that the last update was made - // to the daily original and received content lengths. - int64_t GetLastUpdateTime(); - - // Resets daily content length statistics. - void ResetStatistics(); - - // Clears all data saving statistics for the given |reason|. - void ClearDataSavingStatistics(DataReductionProxySavingsClearedReason reason); - - // Returns the total size of all HTTP content received from the network. - int64_t GetHttpReceivedContentLength(); - - // Returns the value the total original size of all HTTP content received from - // the network. - int64_t GetHttpOriginalContentLength(); - - // 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_t* original_content_length, - int64_t* received_content_length, - int64_t* last_update_time); - - // Calls |get_data_usage_callback| with full data usage history. In-memory - // data usage stats are flushed to storage before querying for full history. - // An empty vector will be returned if "data_usage_reporting.enabled" pref is - // not enabled or if called immediately after enabling the pref before - // in-memory stats could be initialized from storage. Data usage is sorted - // chronologically with the last entry corresponding to |base::Time::Now()|. - void GetHistoricalDataUsage( - HistoricalDataUsageCallback get_data_usage_callback); - - // Deletes browsing history from storage and memory for the given time - // range. Currently, this method deletes all data usage for the given range. - void DeleteBrowsingHistory(const base::Time& start, const base::Time& end); - - // Callback from loading detailed data usage. Initializes in memory data - // structures used to collect data usage. |data_usage| contains the data usage - // for the last stored interval. - void OnCurrentDataUsageLoaded(std::unique_ptr<DataUsageBucket> data_usage); - - // Sets the value of |prefs::kDataUsageReportingEnabled| to |enabled|. - // Initializes data usage statistics in memory when pref is enabled and - // persists data usage to memory when pref is disabled. - void SetDataUsageReportingEnabled(bool enabled); - - // Returns |data_usage_map_|. - const DataReductionProxyCompressionStats::SiteUsageMap& - DataUsageMapForTesting() const { - return data_usage_map_; - } - - private: - // Enum to track the state of loading data usage from storage. - enum CurrentDataUsageLoadStatus { NOT_LOADED = 0, LOADING = 1, LOADED = 2 }; - - friend class DataReductionProxyCompressionStatsTest; - - typedef std::map<const char*, int64_t> DataReductionProxyPrefMap; - typedef std::unordered_map<const char*, std::unique_ptr<base::ListValue>> - DataReductionProxyListPrefMap; - - class DailyContentLengthUpdate; - class DailyDataSavingUpdate; - - // 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); - - // Gets the value of |pref| from the pref service and adds it to the - // |list_pref_map|. - void InitListPref(const char* pref); - - void OnUpdateContentLengths(); - - // Gets the int64_t pref at |pref_path| from the |DataReductionProxyPrefMap|. - int64_t 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_t pref_value); - - // Increases the pref value in the |DataReductionProxyPrefMap| map. - // The pref is later written to |pref service_|. - void IncreaseInt64Pref(const char* pref_path, int64_t delta); - - // 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(); - - // Starts a timer (if necessary) to write prefs in |kMinutesBetweenWrites| to - // the |pref_service|. - void DelayedWritePrefs(); - - // Copies the values at each index of |from_list| to the same index in - // |to_list|. - void TransferList(const base::Value& from_list, base::Value* to_list); - - // Records content length updates to prefs. - void RecordRequestSizePrefs(int64_t compressed_size, - int64_t original_size, - bool with_data_reduction_proxy_enabled, - const std::string& mime_type, - const base::Time& now); - - void IncrementDailyUmaPrefs(int64_t original_size, - int64_t received_size, - const char* original_size_pref, - const char* received_size_pref, - bool data_reduction_proxy_enabled, - const char* original_size_with_proxy_enabled_pref, - const char* recevied_size_with_proxy_enabled_pref, - bool via_data_reduction_proxy, - const char* original_size_via_proxy_pref, - const char* received_size_via_proxy_pref); - - // Persists the in memory data usage information to storage and clears all - // in-memory data usage. Do not call this method unless |data_usage_loaded_| - // is |LOADED|. - void PersistDataUsage(); - - // Deletes all historical data usage from storage and memory. This method - // should not be called when |current_data_usage_load_status_| is |LOADING|. - void DeleteHistoricalDataUsage(); - - // Actual implementation of |GetHistoricalDataUsage|. This helper method - // explicitly passes |base::Time::Now()| to make testing easier. - void GetHistoricalDataUsageImpl( - HistoricalDataUsageCallback get_data_usage_callback, - const base::Time& now); - - // Called when |prefs::kDataUsageReportingEnabled| pref values changes. - // Initializes data usage statistics in memory when pref is enabled and - // persists data usage to memory when pref is disabled. - void OnDataUsageReportingPrefChanged(); - - // Initialize the weekly data use prefs for the current week, and records the - // weekly aggregate data use histograms. - void InitializeWeeklyAggregateDataUse(const base::Time& now); - - // Records |data_used_kb| to the current week data use pref. |is_user_request| - // indicates if this is user-initiated traffic or chrome services traffic, and - // |service_hash_code| uniquely identifies the corresponding chrome service. - void RecordWeeklyAggregateDataUse( - const base::Time& now, - int32_t data_used_kb, - bool is_user_request, - data_use_measurement::DataUseUserData::DataUseContentType content_type, - int32_t service_hash_code); - - // Normalizes the hostname for data usage attribution. Returns a substring - // without the protocol. - // Example: "http://www.finance.google.com" -> "www.finance.google.com" - static std::string NormalizeHostname(const std::string& host); - - raw_ptr<DataReductionProxyService> service_; - raw_ptr<PrefService> pref_service_; - const base::TimeDelta delay_; - DataReductionProxyPrefMap pref_map_; - DataReductionProxyListPrefMap list_pref_map_; - BooleanPrefMember data_usage_reporting_enabled_; - - // Maintains detailed data usage for current interval. - SiteUsageMap data_usage_map_; - - // Time when |data_usage_map_| was last updated. Contains NULL time if - // |data_usage_map_| does not have any data. This could happen either because - // current data usage has not yet been loaded from storage, or because - // no data usage has ever been recorded. - base::Time data_usage_map_last_updated_; - - // Tracks whether |data_usage_map_| has changes that have not yet been - // persisted to storage. - bool data_usage_map_is_dirty_; - - // Tracks state of loading data usage from storage. - CurrentDataUsageLoadStatus current_data_usage_load_status_; - - base::OneShotTimer pref_writer_timer_; - base::ThreadChecker thread_checker_; - - base::WeakPtrFactory<DataReductionProxyCompressionStats> weak_factory_{this}; -}; - -} // namespace data_reduction_proxy - -#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_COMPRESSION_STATS_H_ diff --git a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats_unittest.cc b/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats_unittest.cc deleted file mode 100644 index 254e98c97f0..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats_unittest.cc +++ /dev/null @@ -1,1080 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h" - -#include <stddef.h> -#include <stdint.h> - -#include <memory> -#include <string> -#include <utility> - -#include "base/bind.h" -#include "base/memory/ref_counted.h" -#include "base/run_loop.h" -#include "base/strings/string_number_conversions.h" -#include "base/test/metrics/histogram_tester.h" -#include "base/test/task_environment.h" -#include "base/time/time.h" -#include "base/values.h" -#include "build/build_config.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.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" -#include "components/data_reduction_proxy/proto/data_store.pb.h" -#include "components/prefs/pref_registry_simple.h" -#include "components/prefs/pref_service.h" -#include "components/prefs/testing_pref_service.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace { - -const int kWriteDelayMinutes = 60; - -// Each bucket holds data usage for a 15 minute interval. History is maintained -// for 60 days. -const int kNumExpectedBuckets = 60 * 24 * 60 / 15; - -int64_t GetListPrefInt64Value(const base::ListValue& list_update, - size_t index) { - std::string string_value; - base::Value::ConstListView list_view = list_update.GetList(); - if (index < list_view.size() && list_view[index].is_string()) { - string_value = list_view[index].GetString(); - } else { - ADD_FAILURE() << "invalid index or [index] not a string"; - } - - int64_t value = 0; - EXPECT_TRUE(base::StringToInt64(string_value, &value)); - return value; -} - -class DataUsageLoadVerifier { - public: - DataUsageLoadVerifier( - std::unique_ptr<std::vector<data_reduction_proxy::DataUsageBucket>> - expected) { - expected_ = std::move(expected); - } - - void OnLoadDataUsage( - std::unique_ptr<std::vector<data_reduction_proxy::DataUsageBucket>> - actual) { - EXPECT_EQ(expected_->size(), actual->size()); - - // We are iterating through 2 vectors, |actual| and |expected|, so using an - // index rather than an iterator. - for (size_t i = 0; i < expected_->size(); ++i) { - data_reduction_proxy::DataUsageBucket* actual_bucket = &(actual->at(i)); - data_reduction_proxy::DataUsageBucket* expected_bucket = - &(expected_->at(i)); - EXPECT_EQ(expected_bucket->connection_usage_size(), - actual_bucket->connection_usage_size()); - - for (int j = 0; j < expected_bucket->connection_usage_size(); ++j) { - data_reduction_proxy::PerConnectionDataUsage actual_connection_usage = - actual_bucket->connection_usage(j); - data_reduction_proxy::PerConnectionDataUsage expected_connection_usage = - expected_bucket->connection_usage(j); - - EXPECT_EQ(expected_connection_usage.site_usage_size(), - actual_connection_usage.site_usage_size()); - - for (auto expected_site_usage : - expected_connection_usage.site_usage()) { - data_reduction_proxy::PerSiteDataUsage actual_site_usage; - for (auto it = actual_connection_usage.site_usage().begin(); - it != actual_connection_usage.site_usage().end(); ++it) { - if (it->hostname() == expected_site_usage.hostname()) { - actual_site_usage = *it; - } - } - - EXPECT_EQ(expected_site_usage.data_used(), - actual_site_usage.data_used()); - EXPECT_EQ(expected_site_usage.original_size(), - actual_site_usage.original_size()); - } - } - } - } - - private: - std::unique_ptr<std::vector<data_reduction_proxy::DataUsageBucket>> expected_; -}; - -} // namespace - -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_environment_( - base::test::SingleThreadTaskEnvironment::MainThreadType::UI) { - EXPECT_TRUE(base::Time::FromString(kLastUpdateTime, &now_)); - } - - void SetUp() override { - drp_test_context_ = DataReductionProxyTestContext::Builder().Build(); - - compression_stats_ = std::make_unique<DataReductionProxyCompressionStats>( - data_reduction_proxy_service(), pref_service(), base::TimeDelta()); - } - - void ResetCompressionStatsWithDelay(const base::TimeDelta& delay) { - compression_stats_ = std::make_unique<DataReductionProxyCompressionStats>( - data_reduction_proxy_service(), pref_service(), delay); - } - - base::Time FakeNow() const { - return now_ + now_delta_; - } - - void SetFakeTimeDeltaInHours(int hours) { now_delta_ = base::Hours(hours); } - - void AddFakeTimeDeltaInHours(int hours) { now_delta_ += base::Hours(hours); } - - void SetUpPrefs() { - CreatePrefList(prefs::kDailyHttpOriginalContentLength); - CreatePrefList(prefs::kDailyHttpReceivedContentLength); - - const int64_t kOriginalLength = 150; - const int64_t kReceivedLength = 100; - - compression_stats_->SetInt64( - prefs::kHttpOriginalContentLength, kOriginalLength); - compression_stats_->SetInt64( - prefs::kHttpReceivedContentLength, kReceivedLength); - - base::ListValue* original_daily_content_length_list = - compression_stats_->GetList(prefs::kDailyHttpOriginalContentLength); - base::ListValue* received_daily_content_length_list = - compression_stats_->GetList(prefs::kDailyHttpReceivedContentLength); - - for (size_t i = 0; i < kNumDaysInHistory; ++i) { - original_daily_content_length_list->Set( - i, std::make_unique<base::Value>(base::NumberToString(i))); - } - - received_daily_content_length_list->ClearList(); - for (size_t i = 0; i < kNumDaysInHistory / 2; ++i) { - received_daily_content_length_list->Append(base::NumberToString(i)); - } - } - - // Create daily pref list of |kNumDaysInHistory| zero values. - void CreatePrefList(const char* pref) { - base::Value* update = compression_stats_->GetList(pref); - update->ClearList(); - for (size_t i = 0; i < kNumDaysInHistory; ++i) { - update->Append(base::Value(base::NumberToString(0))); - } - } - - // Verify the pref list values in |pref_service_| are equal to those in - // |simple_pref_service| for |pref|. - void VerifyPrefListWasWritten(const char* pref) { - const base::ListValue* delayed_list = compression_stats_->GetList(pref); - const base::ListValue* written_list = pref_service()->GetList(pref); - ASSERT_EQ(delayed_list->GetList().size(), written_list->GetList().size()); - size_t count = delayed_list->GetList().size(); - - for (size_t i = 0; i < count; ++i) { - EXPECT_EQ(GetListPrefInt64Value(*delayed_list, i), - GetListPrefInt64Value(*written_list, i)); - } - } - - // Verify the pref value in |pref_service_| are equal to that in - // |simple_pref_service|. - void VerifyPrefWasWritten(const char* pref) { - int64_t delayed_pref = compression_stats_->GetInt64(pref); - int64_t written_pref = pref_service()->GetInt64(pref); - EXPECT_EQ(delayed_pref, written_pref); - } - - // Verify the pref values in |dict| are equal to that in |compression_stats_|. - void VerifyPrefs(base::DictionaryValue* dict) { - std::u16string dict_pref_string; - int64_t dict_pref; - int64_t service_pref; - - dict->GetString("historic_original_content_length", &dict_pref_string); - base::StringToInt64(dict_pref_string, &dict_pref); - service_pref = - compression_stats_->GetInt64(prefs::kHttpOriginalContentLength); - EXPECT_EQ(service_pref, dict_pref); - - dict->GetString("historic_received_content_length", &dict_pref_string); - base::StringToInt64(dict_pref_string, &dict_pref); - service_pref = - compression_stats_->GetInt64(prefs::kHttpReceivedContentLength); - 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_t* values, - size_t count, - size_t num_days_in_history) { - ASSERT_GE(num_days_in_history, count); - base::ListValue* update = compression_stats_->GetList(pref); - ASSERT_EQ(num_days_in_history, update->GetList().size()) - << "Pref: " << pref; - - for (size_t i = 0; i < count; ++i) { - EXPECT_EQ(values[i], - GetListPrefInt64Value(*update, num_days_in_history - count + i)) - << pref << "; index=" << (num_days_in_history - count + i); - } - for (size_t i = 0; i < num_days_in_history - count; ++i) { - EXPECT_EQ(0, GetListPrefInt64Value(*update, i)) << "index=" << i; - } - } - - // Verify that the pref value is equal to given value. - void VerifyPrefInt64(const char* pref, const int64_t value) { - EXPECT_EQ(value, compression_stats_->GetInt64(pref)); - } - - // Verify all daily data saving pref list values. - void VerifyDailyDataSavingContentLengthPrefLists( - const int64_t* original_values, - size_t original_count, - const int64_t* received_values, - size_t received_count, - size_t num_days_in_history) { - VerifyPrefList(data_reduction_proxy::prefs::kDailyHttpOriginalContentLength, - original_values, original_count, num_days_in_history); - VerifyPrefList(data_reduction_proxy::prefs::kDailyHttpReceivedContentLength, - received_values, received_count, num_days_in_history); - } - - int64_t GetInt64(const char* pref_path) { - return compression_stats_->GetInt64(pref_path); - } - - void SetInt64(const char* pref_path, int64_t pref_value) { - compression_stats_->SetInt64(pref_path, pref_value); - } - - std::string NormalizeHostname(const std::string& hostname) { - return DataReductionProxyCompressionStats::NormalizeHostname(hostname); - } - - void RecordContentLengthPrefs(int64_t received_content_length, - int64_t original_content_length, - bool with_data_reduction_proxy_enabled, - const std::string& mime_type, - base::Time now) { - compression_stats_->RecordRequestSizePrefs( - received_content_length, original_content_length, - with_data_reduction_proxy_enabled, mime_type, now); - } - - void RecordContentLengthPrefs(int64_t received_content_length, - int64_t original_content_length, - bool with_data_reduction_proxy_enabled, - base::Time now) { - RecordContentLengthPrefs(received_content_length, original_content_length, - with_data_reduction_proxy_enabled, - "application/octet-stream", now); - } - - void RecordDataUsage(const std::string& data_usage_host, - int64_t data_used, - int64_t original_size, - const base::Time& time) { - compression_stats_->RecordDataUseByHost(data_usage_host, data_used, - original_size, time); - } - - void GetHistoricalDataUsage(HistoricalDataUsageCallback on_load_data_usage, - const base::Time& now) { - compression_stats_->GetHistoricalDataUsageImpl( - std::move(on_load_data_usage), now); - } - - void LoadHistoricalDataUsage(HistoricalDataUsageCallback on_load_data_usage) { - compression_stats_->service_->LoadHistoricalDataUsage( - std::move(on_load_data_usage)); - } - - void DeleteHistoricalDataUsage() { - compression_stats_->DeleteHistoricalDataUsage(); - } - - void ClearDataSavingStatistics() { - compression_stats_->ClearDataSavingStatistics( - DataReductionProxySavingsClearedReason:: - USER_ACTION_DELETE_BROWSING_HISTORY); - } - - void DeleteBrowsingHistory(const base::Time& start, const base::Time& end) { - compression_stats_->DeleteBrowsingHistory(start, end); - } - - void EnableDataUsageReporting() { - pref_service()->SetBoolean(prefs::kDataUsageReportingEnabled, true); - } - - void DisableDataUsageReporting() { - pref_service()->SetBoolean(prefs::kDataUsageReportingEnabled, false); - } - - DataReductionProxyCompressionStats* compression_stats() { - return compression_stats_.get(); - } - - void ForceWritePrefs() { compression_stats_->WritePrefs(); } - - bool IsDelayedWriteTimerRunning() const { - return compression_stats_->pref_writer_timer_.IsRunning(); - } - - TestingPrefServiceSimple* pref_service() { - return drp_test_context_->pref_service(); - } - - DataReductionProxyService* data_reduction_proxy_service() { - return drp_test_context_->data_reduction_proxy_service(); - } - - bool IsDataReductionProxyEnabled() { - return drp_test_context_->IsDataReductionProxyEnabled(); - } - - void InitializeWeeklyAggregateDataUse(const base::Time& now) { - compression_stats_->InitializeWeeklyAggregateDataUse(now); - } - - void RecordWeeklyAggregateDataUse( - const base::Time& now, - int32_t received_kb, - bool is_user_request, - data_use_measurement::DataUseUserData::DataUseContentType content_type, - int32_t service_hash_code) { - compression_stats_->RecordWeeklyAggregateDataUse( - now, received_kb, is_user_request, content_type, service_hash_code); - } - - void VerifyDictionaryPref(const std::string& pref, - int key, - int expected_value) const { - const base::DictionaryValue* dict = - compression_stats_->pref_service_->GetDictionary(pref); - EXPECT_EQ(expected_value != 0, dict->HasKey(base::NumberToString(key))); - if (expected_value) { - EXPECT_EQ(expected_value, - dict->FindKey(base::NumberToString(key))->GetInt()); - } - } - - private: - base::test::SingleThreadTaskEnvironment task_environment_; - std::unique_ptr<DataReductionProxyTestContext> drp_test_context_; - std::unique_ptr<DataReductionProxyCompressionStats> compression_stats_; - base::Time now_; - base::TimeDelta now_delta_; -}; - -TEST_F(DataReductionProxyCompressionStatsTest, WritePrefsDirect) { - SetUpPrefs(); - - VerifyPrefWasWritten(prefs::kHttpOriginalContentLength); - VerifyPrefWasWritten(prefs::kHttpReceivedContentLength); - VerifyPrefListWasWritten(prefs::kDailyHttpOriginalContentLength); - VerifyPrefListWasWritten(prefs::kDailyHttpReceivedContentLength); -} - -TEST_F(DataReductionProxyCompressionStatsTest, WritePrefsDelayed) { - ResetCompressionStatsWithDelay(base::Minutes(kWriteDelayMinutes)); - - EXPECT_EQ(0, pref_service()->GetInt64(prefs::kHttpOriginalContentLength)); - EXPECT_EQ(0, pref_service()->GetInt64(prefs::kHttpReceivedContentLength)); - EXPECT_FALSE(IsDelayedWriteTimerRunning()); - - SetUpPrefs(); - EXPECT_TRUE(IsDelayedWriteTimerRunning()); - ForceWritePrefs(); - - VerifyPrefWasWritten(prefs::kHttpOriginalContentLength); - VerifyPrefWasWritten(prefs::kHttpReceivedContentLength); - VerifyPrefListWasWritten(prefs::kDailyHttpOriginalContentLength); - VerifyPrefListWasWritten(prefs::kDailyHttpReceivedContentLength); -} - -TEST_F(DataReductionProxyCompressionStatsTest, StatsRestoredOnOnRestart) { - base::Value list_value(base::Value::Type::LIST); - list_value.Append(base::Value(base::NumberToString(1234))); - pref_service()->Set(prefs::kDailyHttpOriginalContentLength, list_value); - - ResetCompressionStatsWithDelay(base::Minutes(kWriteDelayMinutes)); - - const base::Value* value = - pref_service()->GetList(prefs::kDailyHttpOriginalContentLength); - const std::string* string_value = value->GetList()[0].GetIfString(); - EXPECT_EQ("1234", *string_value); -} - -TEST_F(DataReductionProxyCompressionStatsTest, TotalLengths) { - const int64_t kOriginalLength = 200; - const int64_t kReceivedLength = 100; - - compression_stats()->RecordDataUseWithMimeType( - kReceivedLength, kOriginalLength, IsDataReductionProxyEnabled(), - std::string(), true, data_use_measurement::DataUseUserData::OTHER, 0); - - EXPECT_EQ(kReceivedLength, - GetInt64(data_reduction_proxy::prefs::kHttpReceivedContentLength)); - EXPECT_FALSE(IsDataReductionProxyEnabled()); - EXPECT_EQ(kOriginalLength, - GetInt64(data_reduction_proxy::prefs::kHttpOriginalContentLength)); - - // Record the same numbers again, and total lengths should be doubled. - compression_stats()->RecordDataUseWithMimeType( - kReceivedLength, kOriginalLength, IsDataReductionProxyEnabled(), - std::string(), true, data_use_measurement::DataUseUserData::OTHER, 0); - - EXPECT_EQ(kReceivedLength * 2, - GetInt64(data_reduction_proxy::prefs::kHttpReceivedContentLength)); - EXPECT_FALSE(IsDataReductionProxyEnabled()); - EXPECT_EQ(kOriginalLength * 2, - GetInt64(data_reduction_proxy::prefs::kHttpOriginalContentLength)); -} - -TEST_F(DataReductionProxyCompressionStatsTest, OneResponse) { - const int64_t kOriginalLength = 200; - const int64_t kReceivedLength = 100; - int64_t original[] = {kOriginalLength}; - int64_t received[] = {kReceivedLength}; - - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, true, FakeNow()); - - VerifyDailyDataSavingContentLengthPrefLists(original, 1, received, 1, - kNumDaysInHistory); -} - -TEST_F(DataReductionProxyCompressionStatsTest, MultipleResponses) { - const int64_t kOriginalLength = 150; - const int64_t kReceivedLength = 100; - int64_t original[] = {kOriginalLength}; - int64_t received[] = {kReceivedLength}; - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, false, FakeNow()); - VerifyDailyDataSavingContentLengthPrefLists(original, 1, received, 1, - kNumDaysInHistory); - - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, true, FakeNow()); - original[0] += kOriginalLength; - received[0] += kReceivedLength; - VerifyDailyDataSavingContentLengthPrefLists(original, 1, received, 1, - kNumDaysInHistory); - - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, true, FakeNow()); - original[0] += kOriginalLength; - received[0] += kReceivedLength; - VerifyDailyDataSavingContentLengthPrefLists(original, 1, received, 1, - kNumDaysInHistory); - - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, true, FakeNow()); - original[0] += kOriginalLength; - received[0] += kReceivedLength; - VerifyDailyDataSavingContentLengthPrefLists(original, 1, received, 1, - kNumDaysInHistory); - - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, false, FakeNow()); - original[0] += kOriginalLength; - received[0] += kReceivedLength; - VerifyDailyDataSavingContentLengthPrefLists(original, 1, received, 1, - kNumDaysInHistory); -} - -TEST_F(DataReductionProxyCompressionStatsTest, ForwardOneDay) { - const int64_t kOriginalLength = 200; - const int64_t kReceivedLength = 100; - - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, true, FakeNow()); - - // Forward one day. - SetFakeTimeDeltaInHours(24); - - // Proxy not enabled. Not via proxy. - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, false, FakeNow()); - - int64_t original[] = {kOriginalLength, kOriginalLength}; - int64_t received[] = {kReceivedLength, kReceivedLength}; - VerifyDailyDataSavingContentLengthPrefLists(original, 2, received, 2, - kNumDaysInHistory); - - // Proxy enabled. Not via proxy. - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, true, FakeNow()); - original[1] += kOriginalLength; - received[1] += kReceivedLength; - VerifyDailyDataSavingContentLengthPrefLists(original, 2, received, 2, - kNumDaysInHistory); - - // Proxy enabled and via proxy. - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, true, FakeNow()); - original[1] += kOriginalLength; - received[1] += kReceivedLength; - VerifyDailyDataSavingContentLengthPrefLists(original, 2, received, 2, - kNumDaysInHistory); - - // Proxy enabled and via proxy, with content length greater than max int32_t. - const int64_t kBigOriginalLength = 0x300000000LL; // 12G. - const int64_t kBigReceivedLength = 0x200000000LL; // 8G. - RecordContentLengthPrefs(kBigReceivedLength, kBigOriginalLength, true, - FakeNow()); - original[1] += kBigOriginalLength; - received[1] += kBigReceivedLength; - VerifyDailyDataSavingContentLengthPrefLists(original, 2, received, 2, - kNumDaysInHistory); -} - -TEST_F(DataReductionProxyCompressionStatsTest, PartialDayTimeChange) { - const int64_t kOriginalLength = 200; - const int64_t kReceivedLength = 100; - int64_t original[] = {0, kOriginalLength}; - int64_t received[] = {0, kReceivedLength}; - - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, true, FakeNow()); - VerifyDailyDataSavingContentLengthPrefLists(original, 2, received, 2, - kNumDaysInHistory); - - // Forward 10 hours, stay in the same day. - // See kLastUpdateTime: "Now" in test is 03:45am. - SetFakeTimeDeltaInHours(10); - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, true, FakeNow()); - original[1] += kOriginalLength; - received[1] += kReceivedLength; - VerifyDailyDataSavingContentLengthPrefLists(original, 2, received, 2, - kNumDaysInHistory); - - // Forward 11 more hours, comes to tomorrow. - AddFakeTimeDeltaInHours(11); - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, true, FakeNow()); - int64_t original2[] = {kOriginalLength * 2, kOriginalLength}; - int64_t received2[] = {kReceivedLength * 2, kReceivedLength}; - VerifyDailyDataSavingContentLengthPrefLists(original2, 2, received2, 2, - kNumDaysInHistory); -} - -TEST_F(DataReductionProxyCompressionStatsTest, BackwardAndForwardOneDay) { - base::HistogramTester histogram_tester; - const int64_t kOriginalLength = 200; - const int64_t kReceivedLength = 100; - int64_t original[] = {kOriginalLength}; - int64_t received[] = {kReceivedLength}; - - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, true, FakeNow()); - - // Backward one day, expect no count. - SetFakeTimeDeltaInHours(-24); - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, true, FakeNow()); - original[0] += kOriginalLength; - received[0] += kReceivedLength; - VerifyDailyDataSavingContentLengthPrefLists(original, 1, received, 1, - kNumDaysInHistory); - histogram_tester.ExpectTotalCount("DataReductionProxy.SavingsCleared.Reason", - 0); - - // Then forward one day, expect no count. - AddFakeTimeDeltaInHours(24); - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, true, FakeNow()); - int64_t original2[] = {kOriginalLength * 2, kOriginalLength}; - int64_t received2[] = {kReceivedLength * 2, kReceivedLength}; - VerifyDailyDataSavingContentLengthPrefLists(original2, 2, received2, 2, - kNumDaysInHistory); - histogram_tester.ExpectTotalCount("DataReductionProxy.SavingsCleared.Reason", - 0); -} - -TEST_F(DataReductionProxyCompressionStatsTest, BackwardTwoDays) { - base::HistogramTester histogram_tester; - const int64_t kOriginalLength = 200; - const int64_t kReceivedLength = 100; - int64_t original[] = {kOriginalLength}; - int64_t received[] = {kReceivedLength}; - - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, true, FakeNow()); - - // Backward two days, expect SYSTEM_CLOCK_MOVED_BACK. - SetFakeTimeDeltaInHours(-2 * 24); - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, true, FakeNow()); - VerifyDailyDataSavingContentLengthPrefLists(original, 1, received, 1, - kNumDaysInHistory); - histogram_tester.ExpectUniqueSample( - "DataReductionProxy.SavingsCleared.Reason", - DataReductionProxySavingsClearedReason::SYSTEM_CLOCK_MOVED_BACK, 1); - - // Backward another two days, expect SYSTEM_CLOCK_MOVED_BACK. - SetFakeTimeDeltaInHours(-4 * 24); - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, true, FakeNow()); - histogram_tester.ExpectUniqueSample( - "DataReductionProxy.SavingsCleared.Reason", - DataReductionProxySavingsClearedReason::SYSTEM_CLOCK_MOVED_BACK, 2); - - // Forward 2 days, expect no change. - AddFakeTimeDeltaInHours(2 * 24); - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, true, FakeNow()); - histogram_tester.ExpectUniqueSample( - "DataReductionProxy.SavingsCleared.Reason", - DataReductionProxySavingsClearedReason::SYSTEM_CLOCK_MOVED_BACK, 2); -} - -TEST_F(DataReductionProxyCompressionStatsTest, NormalizeHostname) { - EXPECT_EQ("www.foo.com", NormalizeHostname("http://www.foo.com")); - EXPECT_EQ("foo.com", NormalizeHostname("https://foo.com")); - EXPECT_EQ("bar.co.uk", NormalizeHostname("http://bar.co.uk")); - EXPECT_EQ("http.www.co.in", NormalizeHostname("http://http.www.co.in")); -} - -TEST_F(DataReductionProxyCompressionStatsTest, RecordDataUsageSingleSite) { - EnableDataUsageReporting(); - base::RunLoop().RunUntilIdle(); - - base::Time now = base::Time::Now(); - RecordDataUsage("https://www.foo.com", 1000, 1250, now); - - auto expected_data_usage = - std::make_unique<std::vector<data_reduction_proxy::DataUsageBucket>>( - kNumExpectedBuckets); - data_reduction_proxy::PerConnectionDataUsage* connection_usage = - expected_data_usage->at(kNumExpectedBuckets - 1).add_connection_usage(); - data_reduction_proxy::PerSiteDataUsage* site_usage = - connection_usage->add_site_usage(); - site_usage->set_hostname("www.foo.com"); - site_usage->set_data_used(1000); - site_usage->set_original_size(1250); - - DataUsageLoadVerifier verifier(std::move(expected_data_usage)); - - GetHistoricalDataUsage(base::BindOnce(&DataUsageLoadVerifier::OnLoadDataUsage, - base::Unretained(&verifier)), - now); - base::RunLoop().RunUntilIdle(); -} - -TEST_F(DataReductionProxyCompressionStatsTest, DisableDataUsageRecording) { - EnableDataUsageReporting(); - base::RunLoop().RunUntilIdle(); - - base::Time now = base::Time::Now(); - RecordDataUsage("https://www.foo.com", 1000, 1250, now); - - DisableDataUsageReporting(); - base::RunLoop().RunUntilIdle(); - -#if !defined(OS_ANDROID) - // Data usage on disk must be deleted. - auto expected_data_usage1 = - std::make_unique<std::vector<data_reduction_proxy::DataUsageBucket>>( - kNumExpectedBuckets); - DataUsageLoadVerifier verifier1(std::move(expected_data_usage1)); - LoadHistoricalDataUsage(base::BindOnce( - &DataUsageLoadVerifier::OnLoadDataUsage, base::Unretained(&verifier1))); - - // Public API must return an empty array. - auto expected_data_usage2 = - std::make_unique<std::vector<data_reduction_proxy::DataUsageBucket>>(); - DataUsageLoadVerifier verifier2(std::move(expected_data_usage2)); - GetHistoricalDataUsage(base::BindOnce(&DataUsageLoadVerifier::OnLoadDataUsage, - base::Unretained(&verifier2)), - now); -#else - // For Android don't delete data usage. - auto expected_data_usage = - std::make_unique<std::vector<data_reduction_proxy::DataUsageBucket>>( - kNumExpectedBuckets); - data_reduction_proxy::PerConnectionDataUsage* connection_usage = - expected_data_usage->at(kNumExpectedBuckets - 1).add_connection_usage(); - data_reduction_proxy::PerSiteDataUsage* site_usage = - connection_usage->add_site_usage(); - site_usage->set_hostname("www.foo.com"); - site_usage->set_data_used(1000); - site_usage->set_original_size(1250); - - DataUsageLoadVerifier verifier(std::move(expected_data_usage)); - - GetHistoricalDataUsage(base::BindOnce(&DataUsageLoadVerifier::OnLoadDataUsage, - base::Unretained(&verifier)), - now); -#endif - - base::RunLoop().RunUntilIdle(); -} - -TEST_F(DataReductionProxyCompressionStatsTest, RecordDataUsageMultipleSites) { - EnableDataUsageReporting(); - base::RunLoop().RunUntilIdle(); - - base::Time now = base::Time::Now(); - RecordDataUsage("https://www.foo.com", 1000, 1250, now); - RecordDataUsage("https://bar.com", 1001, 1251, now); - RecordDataUsage("http://foobar.com", 1002, 1252, now); - - auto expected_data_usage = - std::make_unique<std::vector<data_reduction_proxy::DataUsageBucket>>( - kNumExpectedBuckets); - data_reduction_proxy::PerConnectionDataUsage* connection_usage = - expected_data_usage->at(kNumExpectedBuckets - 1).add_connection_usage(); - data_reduction_proxy::PerSiteDataUsage* site_usage = - connection_usage->add_site_usage(); - site_usage->set_hostname("www.foo.com"); - site_usage->set_data_used(1000); - site_usage->set_original_size(1250); - - site_usage = connection_usage->add_site_usage(); - site_usage->set_hostname("bar.com"); - site_usage->set_data_used(1001); - site_usage->set_original_size(1251); - - site_usage = connection_usage->add_site_usage(); - site_usage->set_hostname("foobar.com"); - site_usage->set_data_used(1002); - site_usage->set_original_size(1252); - - DataUsageLoadVerifier verifier(std::move(expected_data_usage)); - - GetHistoricalDataUsage(base::BindOnce(&DataUsageLoadVerifier::OnLoadDataUsage, - base::Unretained(&verifier)), - now); - base::RunLoop().RunUntilIdle(); -} - -TEST_F(DataReductionProxyCompressionStatsTest, - RecordDataUsageConsecutiveBuckets) { - EnableDataUsageReporting(); - base::RunLoop().RunUntilIdle(); - - base::Time now = base::Time::Now(); - base::Time fifteen_mins_ago = now - base::Minutes(15); - - RecordDataUsage("https://www.foo.com", 1000, 1250, fifteen_mins_ago); - - RecordDataUsage("https://bar.com", 1001, 1251, now); - - auto expected_data_usage = - std::make_unique<std::vector<data_reduction_proxy::DataUsageBucket>>( - kNumExpectedBuckets); - data_reduction_proxy::PerConnectionDataUsage* connection_usage = - expected_data_usage->at(kNumExpectedBuckets - 2).add_connection_usage(); - data_reduction_proxy::PerSiteDataUsage* site_usage = - connection_usage->add_site_usage(); - site_usage->set_hostname("www.foo.com"); - site_usage->set_data_used(1000); - site_usage->set_original_size(1250); - - connection_usage = - expected_data_usage->at(kNumExpectedBuckets - 1).add_connection_usage(); - site_usage = connection_usage->add_site_usage(); - site_usage->set_hostname("bar.com"); - site_usage->set_data_used(1001); - site_usage->set_original_size(1251); - - DataUsageLoadVerifier verifier(std::move(expected_data_usage)); - - GetHistoricalDataUsage(base::BindOnce(&DataUsageLoadVerifier::OnLoadDataUsage, - base::Unretained(&verifier)), - now); - base::RunLoop().RunUntilIdle(); -} - -// Test that the last entry in data usage bucket vector is for the current -// interval even when current interval does not have any data usage. -TEST_F(DataReductionProxyCompressionStatsTest, - RecordDataUsageEmptyCurrentInterval) { - EnableDataUsageReporting(); - base::RunLoop().RunUntilIdle(); - - base::Time now = base::Time::Now(); - base::Time fifteen_mins_ago = now - base::Minutes(15); - - RecordDataUsage("https://www.foo.com", 1000, 1250, fifteen_mins_ago); - - auto expected_data_usage = - std::make_unique<std::vector<data_reduction_proxy::DataUsageBucket>>( - kNumExpectedBuckets); - data_reduction_proxy::PerConnectionDataUsage* connection_usage = - expected_data_usage->at(kNumExpectedBuckets - 2).add_connection_usage(); - data_reduction_proxy::PerSiteDataUsage* site_usage = - connection_usage->add_site_usage(); - site_usage->set_hostname("www.foo.com"); - site_usage->set_data_used(1000); - site_usage->set_original_size(1250); - - DataUsageLoadVerifier verifier(std::move(expected_data_usage)); - - GetHistoricalDataUsage(base::BindOnce(&DataUsageLoadVerifier::OnLoadDataUsage, - base::Unretained(&verifier)), - now); - base::RunLoop().RunUntilIdle(); -} - -TEST_F(DataReductionProxyCompressionStatsTest, DeleteHistoricalDataUsage) { - EnableDataUsageReporting(); - base::RunLoop().RunUntilIdle(); - - base::Time now = base::Time::Now(); - base::Time fifteen_mins_ago = now - base::Minutes(15); - // Fake record to be from 15 minutes ago so that it is flushed to storage. - RecordDataUsage("https://www.bar.com", 900, 1100, fifteen_mins_ago); - - RecordDataUsage("https://www.foo.com", 1000, 1250, now); - - DeleteHistoricalDataUsage(); - base::RunLoop().RunUntilIdle(); - - auto expected_data_usage = - std::make_unique<std::vector<data_reduction_proxy::DataUsageBucket>>( - kNumExpectedBuckets); - DataUsageLoadVerifier verifier(std::move(expected_data_usage)); - - GetHistoricalDataUsage(base::BindOnce(&DataUsageLoadVerifier::OnLoadDataUsage, - base::Unretained(&verifier)), - now); - base::RunLoop().RunUntilIdle(); -} - -TEST_F(DataReductionProxyCompressionStatsTest, DeleteBrowsingHistory) { - EnableDataUsageReporting(); - base::RunLoop().RunUntilIdle(); - - base::Time now = base::Time::Now(); - base::Time fifteen_mins_ago = now - base::Minutes(15); - - // Fake record to be from 15 minutes ago so that it is flushed to storage. - RecordDataUsage("https://www.bar.com", 900, 1100, fifteen_mins_ago); - - // This data usage will be in kept in memory. - RecordDataUsage("https://www.foo.com", 1000, 1250, now); - - // This should only delete in-memory usage. - DeleteBrowsingHistory(now, now); - base::RunLoop().RunUntilIdle(); - - ASSERT_TRUE(compression_stats()->DataUsageMapForTesting().empty()); - - auto expected_data_usage = - std::make_unique<std::vector<data_reduction_proxy::DataUsageBucket>>( - kNumExpectedBuckets); - data_reduction_proxy::PerConnectionDataUsage* connection_usage = - expected_data_usage->at(kNumExpectedBuckets - 1).add_connection_usage(); - data_reduction_proxy::PerSiteDataUsage* site_usage = - connection_usage->add_site_usage(); - site_usage->set_hostname("www.bar.com"); - site_usage->set_data_used(900); - site_usage->set_original_size(1100); - DataUsageLoadVerifier verifier1(std::move(expected_data_usage)); - - LoadHistoricalDataUsage(base::BindOnce( - &DataUsageLoadVerifier::OnLoadDataUsage, base::Unretained(&verifier1))); - base::RunLoop().RunUntilIdle(); - - // This should delete in-storage usage as well. - DeleteBrowsingHistory(fifteen_mins_ago, now); - base::RunLoop().RunUntilIdle(); - - expected_data_usage = - std::make_unique<std::vector<data_reduction_proxy::DataUsageBucket>>( - kNumExpectedBuckets); - DataUsageLoadVerifier verifier2(std::move(expected_data_usage)); - LoadHistoricalDataUsage(base::BindOnce( - &DataUsageLoadVerifier::OnLoadDataUsage, base::Unretained(&verifier2))); - base::RunLoop().RunUntilIdle(); -} - -TEST_F(DataReductionProxyCompressionStatsTest, ClearDataSavingStatistics) { - EnableDataUsageReporting(); - base::RunLoop().RunUntilIdle(); - - base::Time now = base::Time::Now(); - base::Time fifteen_mins_ago = now - base::Minutes(15); - // Fake record to be from 15 minutes ago so that it is flushed to storage. - RecordDataUsage("https://www.bar.com", 900, 1100, fifteen_mins_ago); - - RecordDataUsage("https://www.foo.com", 1000, 1250, now); - - const int64_t kOriginalLength = 200; - const int64_t kReceivedLength = 100; - int64_t original[] = {kOriginalLength}; - int64_t received[] = {kReceivedLength}; - - RecordContentLengthPrefs(kReceivedLength, kOriginalLength, true, FakeNow()); - - VerifyDailyDataSavingContentLengthPrefLists(original, 1, received, 1, - kNumDaysInHistory); - - ClearDataSavingStatistics(); - base::RunLoop().RunUntilIdle(); - - auto expected_data_usage = - std::make_unique<std::vector<data_reduction_proxy::DataUsageBucket>>( - kNumExpectedBuckets); - DataUsageLoadVerifier verifier(std::move(expected_data_usage)); - - GetHistoricalDataUsage(base::BindOnce(&DataUsageLoadVerifier::OnLoadDataUsage, - base::Unretained(&verifier)), - now); - base::RunLoop().RunUntilIdle(); - - VerifyDailyDataSavingContentLengthPrefLists(nullptr, 0, nullptr, 0, 0); -} - -// Aggregate metrics recording was disabled on Android x86 in crbug.com/865373. -#if defined(OS_ANDROID) && defined(ARCH_CPU_X86) -#define MAYBE_WeeklyAggregateDataUse DISABLED_WeeklyAggregateDataUse -#else -#define MAYBE_WeeklyAggregateDataUse WeeklyAggregateDataUse -#endif -TEST_F(DataReductionProxyCompressionStatsTest, MAYBE_WeeklyAggregateDataUse) { - const int32_t kDataUseKB = 100; - base::HistogramTester histogram_tester; - - InitializeWeeklyAggregateDataUse(base::Time::Now()); - histogram_tester.ExpectTotalCount( - "DataReductionProxy.ThisWeekAggregateKB.Services.Downstream.Background", - 0); - histogram_tester.ExpectTotalCount( - "DataReductionProxy.ThisWeekAggregateKB.Services.Downstream.Foreground", - 0); - histogram_tester.ExpectTotalCount( - "DataReductionProxy.LastWeekAggregateKB.Services.Downstream.Background", - 0); - histogram_tester.ExpectTotalCount( - "DataReductionProxy.LastWeekAggregateKB.Services.Downstream.Foreground", - 0); - histogram_tester.ExpectTotalCount( - "DataReductionProxy.ThisWeekAggregateKB.UserTraffic.Downstream." - "ContentType", - 0); - histogram_tester.ExpectTotalCount( - "DataReductionProxy.LastWeekAggregateKB.UserTraffic.Downstream." - "ContentType", - 0); - - RecordWeeklyAggregateDataUse( - base::Time::Now(), kDataUseKB, true, - data_use_measurement::DataUseUserData::MAIN_FRAME_HTML, 0); - VerifyDictionaryPref(prefs::kThisWeekUserTrafficContentTypeDownstreamKB, - data_use_measurement::DataUseUserData::MAIN_FRAME_HTML, - kDataUseKB); - - histogram_tester.ExpectTotalCount( - "DataReductionProxy.ThisWeekAggregateKB.UserTraffic.Downstream." - "ContentType", - 0); - - InitializeWeeklyAggregateDataUse(base::Time::Now()); - histogram_tester.ExpectBucketCount( - "DataReductionProxy.ThisWeekAggregateKB.UserTraffic.Downstream." - "ContentType", - data_use_measurement::DataUseUserData::MAIN_FRAME_HTML, kDataUseKB); - histogram_tester.ExpectBucketCount( - "DataReductionProxy.LastWeekAggregateKB.UserTraffic.Downstream." - "ContentType", - data_use_measurement::DataUseUserData::MAIN_FRAME_HTML, 0); -} - -// Aggregate metrics recording was disabled on Android x86 in crbug.com/865373. -#if defined(OS_ANDROID) && defined(ARCH_CPU_X86) -#define MAYBE_AggregateDataUseForwardWeeks DISABLED_AggregateDataUseForwardWeeks -#else -#define MAYBE_AggregateDataUseForwardWeeks AggregateDataUseForwardWeeks -#endif -TEST_F(DataReductionProxyCompressionStatsTest, - MAYBE_AggregateDataUseForwardWeeks) { - const int32_t kMainFrameKB = 100; - const int32_t kNonMainFrameKB = 101; - base::HistogramTester histogram_tester; - - base::Time fake_time_now = base::Time::Now(); - - InitializeWeeklyAggregateDataUse(fake_time_now); - histogram_tester.ExpectTotalCount( - "DataReductionProxy.ThisWeekAggregateKB.UserTraffic.Downstream." - "ContentType", - 0); - histogram_tester.ExpectTotalCount( - "DataReductionProxy.LastWeekAggregateKB.UserTraffic.Downstream." - "ContentType", - 0); - - RecordWeeklyAggregateDataUse( - fake_time_now, kMainFrameKB, true, - data_use_measurement::DataUseUserData::MAIN_FRAME_HTML, 0); - VerifyDictionaryPref(prefs::kThisWeekUserTrafficContentTypeDownstreamKB, - data_use_measurement::DataUseUserData::MAIN_FRAME_HTML, - kMainFrameKB); - RecordWeeklyAggregateDataUse( - fake_time_now, kNonMainFrameKB, true, - data_use_measurement::DataUseUserData::NON_MAIN_FRAME_HTML, 0); - VerifyDictionaryPref( - prefs::kThisWeekUserTrafficContentTypeDownstreamKB, - data_use_measurement::DataUseUserData::NON_MAIN_FRAME_HTML, - kNonMainFrameKB); - - // Fast forward 7 days, and verify that the last week histograms are recorded. - fake_time_now += base::Days(7); - InitializeWeeklyAggregateDataUse(fake_time_now); - VerifyDictionaryPref(prefs::kLastWeekUserTrafficContentTypeDownstreamKB, - data_use_measurement::DataUseUserData::MAIN_FRAME_HTML, - kMainFrameKB); - VerifyDictionaryPref( - prefs::kLastWeekUserTrafficContentTypeDownstreamKB, - data_use_measurement::DataUseUserData::NON_MAIN_FRAME_HTML, - kNonMainFrameKB); - - histogram_tester.ExpectBucketCount( - "DataReductionProxy.LastWeekAggregateKB.UserTraffic.Downstream." - "ContentType", - data_use_measurement::DataUseUserData::MAIN_FRAME_HTML, kMainFrameKB); - histogram_tester.ExpectBucketCount( - "DataReductionProxy.LastWeekAggregateKB.UserTraffic.Downstream." - "ContentType", - data_use_measurement::DataUseUserData::NON_MAIN_FRAME_HTML, - kNonMainFrameKB); - histogram_tester.ExpectTotalCount( - "DataReductionProxy.ThisWeekAggregateKB.UserTraffic.Downstream." - "ContentType", - 0); - - // Subsequent data use should be recorded to the current week prefs. - RecordWeeklyAggregateDataUse( - fake_time_now, kMainFrameKB, true, - data_use_measurement::DataUseUserData::MAIN_FRAME_HTML, 0); - VerifyDictionaryPref(prefs::kThisWeekUserTrafficContentTypeDownstreamKB, - data_use_measurement::DataUseUserData::MAIN_FRAME_HTML, - kMainFrameKB); - - // Fast forward by more than two weeks, and the prefs will be cleared. - fake_time_now += base::Days(15); - InitializeWeeklyAggregateDataUse(fake_time_now); - VerifyDictionaryPref(prefs::kLastWeekUserTrafficContentTypeDownstreamKB, - data_use_measurement::DataUseUserData::MAIN_FRAME_HTML, - 0); - VerifyDictionaryPref( - prefs::kLastWeekUserTrafficContentTypeDownstreamKB, - data_use_measurement::DataUseUserData::NON_MAIN_FRAME_HTML, 0); - VerifyDictionaryPref(prefs::kThisWeekUserTrafficContentTypeDownstreamKB, - data_use_measurement::DataUseUserData::MAIN_FRAME_HTML, - 0); - VerifyDictionaryPref( - prefs::kThisWeekUserTrafficContentTypeDownstreamKB, - data_use_measurement::DataUseUserData::NON_MAIN_FRAME_HTML, 0); -} - -} // namespace data_reduction_proxy diff --git a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.cc b/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.cc deleted file mode 100644 index 8c495b703e0..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.cc +++ /dev/null @@ -1,92 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h" - -#include <memory> - -#include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h" -#include "components/pref_registry/pref_registry_syncable.h" -#include "components/prefs/pref_registry_simple.h" -#include "components/prefs/pref_service.h" -#include "components/prefs/scoped_user_pref_update.h" - -namespace data_reduction_proxy { - -// Make sure any changes here that have the potential to impact android_webview -// are reflected in RegisterSimpleProfilePrefs. -void RegisterSyncableProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { - registry->RegisterBooleanPref(prefs::kDataSaverEnabled, false); - registry->RegisterBooleanPref(prefs::kDataReductionProxyWasEnabledBefore, - false); - - registry->RegisterInt64Pref(prefs::kDataReductionProxyLastEnabledTime, 0L); - - registry->RegisterBooleanPref(prefs::kDataUsageReportingEnabled, false); - - registry->RegisterInt64Pref(prefs::kHttpReceivedContentLength, 0); - registry->RegisterInt64Pref(prefs::kHttpOriginalContentLength, 0); - - registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength); - - registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength); - - registry->RegisterInt64Pref(prefs::kDailyHttpContentLengthLastUpdateDate, 0L); - - registry->RegisterIntegerPref(prefs::kThisWeekNumber, 0); - registry->RegisterDictionaryPref( - prefs::kThisWeekServicesDownstreamBackgroundKB, PrefRegistry::LOSSY_PREF); - registry->RegisterDictionaryPref( - prefs::kThisWeekServicesDownstreamForegroundKB, PrefRegistry::LOSSY_PREF); - registry->RegisterDictionaryPref( - prefs::kLastWeekServicesDownstreamBackgroundKB, PrefRegistry::LOSSY_PREF); - registry->RegisterDictionaryPref( - prefs::kLastWeekServicesDownstreamForegroundKB, PrefRegistry::LOSSY_PREF); - registry->RegisterDictionaryPref( - prefs::kThisWeekUserTrafficContentTypeDownstreamKB, - PrefRegistry::LOSSY_PREF); - registry->RegisterDictionaryPref( - prefs::kLastWeekUserTrafficContentTypeDownstreamKB, - PrefRegistry::LOSSY_PREF); -} - -void RegisterSimpleProfilePrefs(PrefRegistrySimple* registry) { - registry->RegisterBooleanPref( - prefs::kDataReductionProxyWasEnabledBefore, false); - - registry->RegisterBooleanPref(prefs::kDataUsageReportingEnabled, false); - RegisterPrefs(registry); -} - -// Add any new data reduction proxy prefs to the |pref_map_| or the -// |list_pref_map_| in Init() of DataReductionProxyCompressionStats. -void RegisterPrefs(PrefRegistrySimple* registry) { - registry->RegisterInt64Pref(prefs::kDataReductionProxyLastEnabledTime, 0L); - registry->RegisterInt64Pref(prefs::kHttpReceivedContentLength, 0); - registry->RegisterInt64Pref( - prefs::kHttpOriginalContentLength, 0); - registry->RegisterListPref( - prefs::kDailyHttpOriginalContentLength); - registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength); - registry->RegisterInt64Pref( - prefs::kDailyHttpContentLengthLastUpdateDate, 0L); - - registry->RegisterIntegerPref(prefs::kThisWeekNumber, 0); - registry->RegisterDictionaryPref( - prefs::kThisWeekServicesDownstreamBackgroundKB, PrefRegistry::LOSSY_PREF); - registry->RegisterDictionaryPref( - prefs::kThisWeekServicesDownstreamForegroundKB, PrefRegistry::LOSSY_PREF); - registry->RegisterDictionaryPref( - prefs::kLastWeekServicesDownstreamBackgroundKB, PrefRegistry::LOSSY_PREF); - registry->RegisterDictionaryPref( - prefs::kLastWeekServicesDownstreamForegroundKB, PrefRegistry::LOSSY_PREF); - registry->RegisterDictionaryPref( - prefs::kThisWeekUserTrafficContentTypeDownstreamKB, - PrefRegistry::LOSSY_PREF); - registry->RegisterDictionaryPref( - prefs::kLastWeekUserTrafficContentTypeDownstreamKB, - PrefRegistry::LOSSY_PREF); -} - -} // namespace data_reduction_proxy diff --git a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h b/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h deleted file mode 100644 index 0801cb37431..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_PREFS_H_ -#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_PREFS_H_ - -namespace user_prefs { -class PrefRegistrySyncable; -} - -class PrefRegistrySimple; - -namespace data_reduction_proxy { - -// Registers the data reduction proxy's profile prefs on platforms that use -// syncable prefs. -void RegisterSyncableProfilePrefs( - user_prefs::PrefRegistrySyncable* registry); - -// Registers the data reduction proxy's profile prefs on platforms that do not -// use syncable prefs. -void RegisterSimpleProfilePrefs(PrefRegistrySimple* registry); - -// Registers local state, i.e., profile-agnostic prefs for the data -// reduction proxy. -void RegisterPrefs(PrefRegistrySimple* registry); - -} // namespace data_reduction_proxy - -#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_PREFS_H_ diff --git a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs_unittest.cc b/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs_unittest.cc deleted file mode 100644 index 455de14b1d8..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs_unittest.cc +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h" - -#include <stdint.h> - -#include "base/strings/string_number_conversions.h" -#include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h" -#include "components/prefs/pref_registry_simple.h" -#include "components/prefs/pref_service.h" -#include "components/prefs/scoped_user_pref_update.h" -#include "components/prefs/testing_pref_service.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace data_reduction_proxy { - -class DataReductionProxyPrefsTest : public testing::Test { - public: - void SetUp() override { - RegisterPrefs(local_state_prefs_.registry()); - PrefRegistrySimple* profile_registry = profile_prefs_.registry(); - RegisterPrefs(profile_registry); - } - - PrefService* local_state_prefs() { - return &local_state_prefs_; - } - - PrefService* profile_prefs() { - return &profile_prefs_; - } - - // Initializes a list with ten string representations of successive int64_t - // values, starting with |starting_value|. - void InitializeList(const char* pref_name, - int64_t starting_value, - PrefService* pref_service) { - ListPrefUpdate list(local_state_prefs(), pref_name); - for (int64_t i = 0; i < 10L; ++i) { - list->Append(base::NumberToString(i + starting_value)); - } - } - - // Verifies that ten string repreentations of successive int64_t values - // starting with |starting_value| are found in the |ListValue| with the - // associated |pref_name|. - void VerifyList(const char* pref_name, - int64_t starting_value, - PrefService* pref_service) { - const base::ListValue* list_value = pref_service->GetList(pref_name); - base::Value::ConstListView list_view = list_value->GetList(); - for (int64_t i = 0; i < 10L; ++i) { - std::string string_value; - int64_t value; - if (static_cast<size_t>(i) < list_view.size() && - list_view[i].is_string()) { - string_value = list_view[i].GetString(); - } - base::StringToInt64(string_value, &value); - EXPECT_EQ(i + starting_value, value); - } - } - - private: - void RegisterPrefs(PrefRegistrySimple* registry) { - registry->RegisterInt64Pref(prefs::kHttpReceivedContentLength, 0); - registry->RegisterInt64Pref(prefs::kHttpOriginalContentLength, 0); - - registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength); - registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength); - registry->RegisterInt64Pref( - prefs::kDailyHttpContentLengthLastUpdateDate, 0L); - } - - TestingPrefServiceSimple local_state_prefs_; - TestingPrefServiceSimple profile_prefs_; -}; - -} // namespace data_reduction_proxy diff --git a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.cc b/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.cc deleted file mode 100644 index 81f23c62fa0..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.cc +++ /dev/null @@ -1,236 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h" - -#include <memory> -#include <utility> - -#include "base/bind.h" -#include "base/files/file_path.h" -#include "base/json/json_reader.h" -#include "base/location.h" -#include "base/metrics/field_trial_params.h" -#include "base/metrics/histogram_macros.h" -#include "base/task/sequenced_task_runner.h" -#include "base/task/single_thread_task_runner.h" -#include "base/task/task_runner_util.h" -#include "base/time/default_clock.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_settings.h" -#include "components/data_reduction_proxy/core/browser/data_store.h" -#include "components/data_reduction_proxy/core/common/data_reduction_proxy_features.h" -#include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers.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/proto/data_store.pb.h" -#include "components/data_use_measurement/core/data_use_measurement.h" -#include "components/prefs/pref_service.h" - -namespace data_reduction_proxy { - -namespace { - -absl::optional<base::Value> GetSaveDataSavingsPercentEstimateFromFieldTrial() { - if (!base::FeatureList::IsEnabled(features::kReportSaveDataSavings)) - return absl::nullopt; - const auto origin_savings_estimate_json = - base::GetFieldTrialParamValueByFeature(features::kReportSaveDataSavings, - "origin_savings_estimate"); - if (origin_savings_estimate_json.empty()) - return absl::nullopt; - - auto origin_savings_estimates = - base::JSONReader::Read(origin_savings_estimate_json); - - UMA_HISTOGRAM_BOOLEAN( - "DataReductionProxy.ReportSaveDataSavings.ParseResult", - origin_savings_estimates && origin_savings_estimates->is_dict()); - - return origin_savings_estimates; -} - -// Hostname used for the other bucket which consists of chrome-services traffic. -// This should be in sync with the same in DataReductionSiteBreakdownView.java -const char kOtherHostName[] = "Other"; - -} // namespace - -DataReductionProxyService::DataReductionProxyService( - DataReductionProxySettings* settings, - PrefService* prefs, - std::unique_ptr<DataStore> store, - data_use_measurement::DataUseMeasurement* data_use_measurement, - const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, - const base::TimeDelta& commit_delay) - : settings_(settings), - prefs_(prefs), - db_data_owner_(new DBDataOwner(std::move(store))), - db_task_runner_(db_task_runner), - data_use_measurement_(data_use_measurement), - save_data_savings_estimate_dict_( - GetSaveDataSavingsPercentEstimateFromFieldTrial()) { - DCHECK(data_use_measurement_); - DCHECK(settings); - - db_task_runner_->PostTask(FROM_HERE, - base::BindOnce(&DBDataOwner::InitializeOnDBThread, - db_data_owner_->GetWeakPtr())); - if (prefs_) { - compression_stats_ = std::make_unique<DataReductionProxyCompressionStats>( - this, prefs_, commit_delay); - } - if (data_use_measurement_) { // null in unit tests. - data_use_measurement_->AddServicesDataUseObserver(this); - } -} - -DataReductionProxyService::~DataReductionProxyService() { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - compression_stats_.reset(); - db_task_runner_->DeleteSoon(FROM_HERE, db_data_owner_.release()); - if (data_use_measurement_) { // null in unit tests. - data_use_measurement_->RemoveServicesDataUseObserver(this); - } -} - - -void DataReductionProxyService::Shutdown() { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - weak_factory_.InvalidateWeakPtrs(); -} - -void DataReductionProxyService::UpdateDataUseForHost(int64_t network_bytes, - int64_t original_bytes, - const std::string& host) { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - if (compression_stats_) { - compression_stats_->RecordDataUseByHost(host, network_bytes, original_bytes, - base::Time::Now()); - } -} - -void DataReductionProxyService::UpdateContentLengths( - int64_t data_used, - int64_t original_size, - bool data_reduction_proxy_enabled, - const std::string& mime_type, - bool is_user_traffic, - data_use_measurement::DataUseUserData::DataUseContentType content_type, - int32_t service_hash_code) { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - if (compression_stats_) { - compression_stats_->RecordDataUseWithMimeType( - data_used, original_size, data_reduction_proxy_enabled, mime_type, - is_user_traffic, content_type, service_hash_code); - } -} - -void DataReductionProxyService::SetUnreachable(bool unreachable) { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - settings_->SetUnreachable(unreachable); -} - -void DataReductionProxyService::SetInt64Pref(const std::string& pref_path, - int64_t value) { - if (prefs_) - prefs_->SetInt64(pref_path, value); -} - -void DataReductionProxyService::SetStringPref(const std::string& pref_path, - const std::string& value) { - if (prefs_) - prefs_->SetString(pref_path, value); -} - - - -void DataReductionProxyService::LoadHistoricalDataUsage( - HistoricalDataUsageCallback load_data_usage_callback) { - std::unique_ptr<std::vector<DataUsageBucket>> data_usage( - new std::vector<DataUsageBucket>()); - std::vector<DataUsageBucket>* data_usage_ptr = data_usage.get(); - db_task_runner_->PostTaskAndReply( - FROM_HERE, - base::BindOnce(&DBDataOwner::LoadHistoricalDataUsage, - db_data_owner_->GetWeakPtr(), - base::Unretained(data_usage_ptr)), - base::BindOnce(std::move(load_data_usage_callback), - std::move(data_usage))); -} - -void DataReductionProxyService::LoadCurrentDataUsageBucket( - LoadCurrentDataUsageCallback load_current_data_usage_callback) { - std::unique_ptr<DataUsageBucket> bucket(new DataUsageBucket()); - DataUsageBucket* bucket_ptr = bucket.get(); - db_task_runner_->PostTaskAndReply( - FROM_HERE, - base::BindOnce(&DBDataOwner::LoadCurrentDataUsageBucket, - db_data_owner_->GetWeakPtr(), - base::Unretained(bucket_ptr)), - base::BindOnce(std::move(load_current_data_usage_callback), - std::move(bucket))); -} - -void DataReductionProxyService::StoreCurrentDataUsageBucket( - std::unique_ptr<DataUsageBucket> current) { - db_task_runner_->PostTask( - FROM_HERE, - base::BindOnce(&DBDataOwner::StoreCurrentDataUsageBucket, - db_data_owner_->GetWeakPtr(), std::move(current))); -} - -void DataReductionProxyService::DeleteHistoricalDataUsage() { - db_task_runner_->PostTask( - FROM_HERE, base::BindOnce(&DBDataOwner::DeleteHistoricalDataUsage, - db_data_owner_->GetWeakPtr())); -} - -void DataReductionProxyService::DeleteBrowsingHistory(const base::Time& start, - const base::Time& end) { - DCHECK_LE(start, end); - db_task_runner_->PostTask( - FROM_HERE, base::BindOnce(&DBDataOwner::DeleteBrowsingHistory, - db_data_owner_->GetWeakPtr(), start, end)); -} - -base::WeakPtr<DataReductionProxyService> -DataReductionProxyService::GetWeakPtr() { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - return weak_factory_.GetWeakPtr(); -} - -void DataReductionProxyService::OnServicesDataUse(int32_t service_hash_code, - int64_t recv_bytes, - int64_t sent_bytes) { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - if (compression_stats_) { - // Record non-content initiated traffic to the Other bucket for data saver - // site-breakdown. - compression_stats_->RecordDataUseByHost(kOtherHostName, sent_bytes, - sent_bytes, base::Time::Now()); - compression_stats_->RecordDataUseByHost(kOtherHostName, recv_bytes, - recv_bytes, base::Time::Now()); - compression_stats_->RecordDataUseWithMimeType( - recv_bytes, recv_bytes, settings_->IsDataReductionProxyEnabled(), - std::string(), false, data_use_measurement::DataUseUserData::OTHER, - service_hash_code); - } -} - -double DataReductionProxyService::GetSaveDataSavingsPercentEstimate( - const std::string& origin) const { - if (origin.empty() || !save_data_savings_estimate_dict_ || - !save_data_savings_estimate_dict_->is_dict()) { - return 0; - } - const auto savings_percent = - save_data_savings_estimate_dict_->FindDoubleKey(origin); - if (!savings_percent) - return 0; - return *savings_percent; -} - -} // namespace data_reduction_proxy diff --git a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h b/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h deleted file mode 100644 index 503728b6967..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h +++ /dev/null @@ -1,154 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_SERVICE_H_ -#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_SERVICE_H_ - -#include <stdint.h> - -#include <memory> -#include <string> - -#include "base/callback.h" -#include "base/gtest_prod_util.h" -#include "base/memory/raw_ptr.h" -#include "base/memory/ref_counted.h" -#include "base/memory/weak_ptr.h" -#include "base/sequence_checker.h" -#include "base/values.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h" -#include "components/data_reduction_proxy/core/browser/db_data_owner.h" -#include "components/data_use_measurement/core/data_use_measurement.h" -#include "net/nqe/effective_connection_type.h" - -class PrefService; - -namespace base { -class SequencedTaskRunner; -class TimeDelta; -} // namespace base - -namespace data_reduction_proxy { - -class DataReductionProxyCompressionStats; -class DataReductionProxySettings; - -// Contains and initializes all Data Reduction Proxy objects that have a -// lifetime based on the UI thread. -class DataReductionProxyService - : public data_use_measurement::DataUseMeasurement::ServicesDataUseObserver { - public: - // The caller must ensure that |settings|, |prefs|, |request_context|, and - // |io_task_runner| remain alive for the lifetime of the - // |DataReductionProxyService| instance. |prefs| may be null. This instance - // will take ownership of |compression_stats|. - // TODO(jeremyim): DataReductionProxyService should own - // DataReductionProxySettings and not vice versa. - DataReductionProxyService( - DataReductionProxySettings* settings, - PrefService* prefs, - std::unique_ptr<DataStore> store, - data_use_measurement::DataUseMeasurement* data_use_measurement, - const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, - const base::TimeDelta& commit_delay); - - DataReductionProxyService(const DataReductionProxyService&) = delete; - DataReductionProxyService& operator=(const DataReductionProxyService&) = - delete; - - virtual ~DataReductionProxyService(); - - void Shutdown(); - - // Records data usage per host. - // Virtual for testing. - virtual void UpdateDataUseForHost(int64_t network_bytes, - int64_t original_bytes, - const std::string& host); - - // Records daily data savings statistics in |compression_stats_|. - // Virtual for testing. - virtual void UpdateContentLengths( - int64_t data_used, - int64_t original_size, - bool data_reduction_proxy_enabled, - const std::string& mime_type, - bool is_user_traffic, - data_use_measurement::DataUseUserData::DataUseContentType content_type, - int32_t service_hash_code); - - // Records whether the Data Reduction Proxy is unreachable or not. - void SetUnreachable(bool unreachable); - - // Stores an int64_t value in |prefs_|. - void SetInt64Pref(const std::string& pref_path, int64_t value); - - // Stores a string value in |prefs_|. - void SetStringPref(const std::string& pref_path, const std::string& value); - - void LoadHistoricalDataUsage( - HistoricalDataUsageCallback load_data_usage_callback); - void LoadCurrentDataUsageBucket( - LoadCurrentDataUsageCallback load_current_data_usage_callback); - void StoreCurrentDataUsageBucket(std::unique_ptr<DataUsageBucket> current); - void DeleteHistoricalDataUsage(); - void DeleteBrowsingHistory(const base::Time& start, const base::Time& end); - - void SetSettingsForTesting(DataReductionProxySettings* settings) { - settings_ = settings; - } - - // Returns the percentage of data savings estimate provided by save-data for - // an origin. - double GetSaveDataSavingsPercentEstimate(const std::string& origin) const; - - // Accessor methods. - DataReductionProxyCompressionStats* compression_stats() const { - return compression_stats_.get(); - } - - base::WeakPtr<DataReductionProxyService> GetWeakPtr(); - - base::SequencedTaskRunner* GetDBTaskRunnerForTesting() const { - return db_task_runner_.get(); - } - - private: - FRIEND_TEST_ALL_PREFIXES(DataReductionProxyConfigServiceClientTest, - MultipleAuthFailures); - FRIEND_TEST_ALL_PREFIXES(DataReductionProxyConfigServiceClientTest, - ValidatePersistedClientConfig); - - void OnServicesDataUse(int32_t service_hash_code, - int64_t recv_bytes, - int64_t sent_bytes) override; - - // Tracks compression statistics to be displayed to the user. - std::unique_ptr<DataReductionProxyCompressionStats> compression_stats_; - - raw_ptr<DataReductionProxySettings> settings_; - - // A prefs service for storing data. - raw_ptr<PrefService> prefs_; - - std::unique_ptr<DBDataOwner> db_data_owner_; - - // Used to post tasks to |db_data_owner_|. - scoped_refptr<base::SequencedTaskRunner> db_task_runner_; - - // Must be accessed on UI thread. Guaranteed to be non-null during the - // lifetime of |this|. - raw_ptr<data_use_measurement::DataUseMeasurement> data_use_measurement_; - - // Dictionary of save-data savings estimates by origin. - const absl::optional<base::Value> save_data_savings_estimate_dict_; - - SEQUENCE_CHECKER(sequence_checker_); - - base::WeakPtrFactory<DataReductionProxyService> weak_factory_{this}; -}; - -} // namespace data_reduction_proxy - -#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_SERVICE_H_ diff --git a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.cc b/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.cc index ee8861ad00d..099bd766b29 100644 --- a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.cc +++ b/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.cc @@ -13,15 +13,7 @@ #include "base/time/clock.h" #include "base/time/default_clock.h" #include "build/build_config.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h" -#include "components/data_reduction_proxy/core/common/data_reduction_proxy_features.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" -#include "components/prefs/pref_member.h" -#include "components/prefs/pref_service.h" -#include "net/base/network_change_notifier.h" -#include "net/http/http_request_headers.h" namespace { @@ -32,244 +24,28 @@ bool ShouldForceEnableDataReductionProxy() { data_reduction_proxy::switches::kEnableDataReductionProxy); } -// Key of the UMA DataReductionProxy.StartupState histogram. -const char kUMAProxyStartupStateHistogram[] = "DataReductionProxy.StartupState"; - -void RecordSettingsEnabledState( - data_reduction_proxy::DataReductionSettingsEnabledAction action) { - UMA_HISTOGRAM_ENUMERATION( - "DataReductionProxy.EnabledState", action, - data_reduction_proxy::DATA_REDUCTION_SETTINGS_ACTION_BOUNDARY); -} - -// Record the number of days since data reduction proxy was enabled by the -// user. -void RecordDaysSinceEnabledMetric(int days_since_enabled) { - UMA_HISTOGRAM_CUSTOM_COUNTS("DataReductionProxy.DaysSinceEnabled", - days_since_enabled, 0, 365 * 10, 100); -} - } // namespace namespace data_reduction_proxy { DataReductionProxySettings::DataReductionProxySettings( bool is_off_the_record_profile) - : unreachable_(false), - prefs_(nullptr), - clock_(base::DefaultClock::GetInstance()), - is_off_the_record_profile_(is_off_the_record_profile) { + : is_off_the_record_profile_(is_off_the_record_profile) { DCHECK(!is_off_the_record_profile_); } DataReductionProxySettings::~DataReductionProxySettings() = default; -void DataReductionProxySettings::InitDataReductionProxySettings( - PrefService* prefs, - std::unique_ptr<DataReductionProxyService> data_reduction_proxy_service) { - DCHECK(thread_checker_.CalledOnValidThread()); - DCHECK(prefs); - DCHECK(data_reduction_proxy_service); - prefs_ = prefs; - data_reduction_proxy_service_ = std::move(data_reduction_proxy_service); - RecordDataReductionInit(); - - registrar_.Init(prefs_); - registrar_.Add( - prefs::kDataSaverEnabled, - base::BindRepeating(&DataReductionProxySettings::OnProxyEnabledPrefChange, - base::Unretained(this))); - -#if defined(OS_ANDROID) - if (IsDataSaverEnabledByUser(is_off_the_record_profile_, prefs_)) { - data_reduction_proxy_service_->compression_stats() - ->SetDataUsageReportingEnabled(true); - } -#endif // defined(OS_ANDROID) -} - -void DataReductionProxySettings::SetCallbackToRegisterSyntheticFieldTrial( - const SyntheticFieldTrialRegistrationCallback& - on_data_reduction_proxy_enabled) { - register_synthetic_field_trial_ = on_data_reduction_proxy_enabled; - RegisterDataReductionProxyFieldTrial(); -} +void DataReductionProxySettings::InitDataReductionProxySettings() {} // static bool DataReductionProxySettings::IsDataSaverEnabledByUser( - bool is_off_the_record_profile, - PrefService* prefs) { + bool is_off_the_record_profile) { if (is_off_the_record_profile) return false; if (ShouldForceEnableDataReductionProxy()) return true; - -#if defined(OS_ANDROID) - return prefs && prefs->GetBoolean(prefs::kDataSaverEnabled); -#else return false; -#endif -} - -// static -void DataReductionProxySettings::SetDataSaverEnabledForTesting( - PrefService* prefs, - bool enabled) { - // Set the command line so that |IsDataSaverEnabledByUser| returns as expected - // on all platforms. - base::CommandLine* cmd = base::CommandLine::ForCurrentProcess(); - if (enabled) { - cmd->AppendSwitch(switches::kEnableDataReductionProxy); - } else { - cmd->RemoveSwitch(switches::kEnableDataReductionProxy); - } - - // Set the pref so that all the pref change callbacks run. - prefs->SetBoolean(prefs::kDataSaverEnabled, enabled); -} - -bool DataReductionProxySettings::IsDataReductionProxyEnabled() const { - return IsDataSaverEnabledByUser(is_off_the_record_profile_, - GetOriginalProfilePrefs()); -} - -bool DataReductionProxySettings::CanUseDataReductionProxy( - const GURL& url) const { - return url.is_valid() && url.scheme() == url::kHttpScheme && - IsDataReductionProxyEnabled(); -} - -bool DataReductionProxySettings::IsDataReductionProxyManaged() { - const PrefService::Preference* pref = - GetOriginalProfilePrefs()->FindPreference(prefs::kDataSaverEnabled); - return pref && pref->IsManaged(); -} - -void DataReductionProxySettings::SetDataReductionProxyEnabled(bool enabled) { - DCHECK(thread_checker_.CalledOnValidThread()); - DCHECK(data_reduction_proxy_service_->compression_stats()); - if (GetOriginalProfilePrefs()->GetBoolean(prefs::kDataSaverEnabled) != - enabled) { - GetOriginalProfilePrefs()->SetBoolean(prefs::kDataSaverEnabled, enabled); - OnProxyEnabledPrefChange(); -#if defined(OS_ANDROID) - data_reduction_proxy_service_->compression_stats() - ->SetDataUsageReportingEnabled(enabled); -#endif // defined(OS_ANDROID) - } -} - -int64_t DataReductionProxySettings::GetDataReductionLastUpdateTime() { - DCHECK(thread_checker_.CalledOnValidThread()); - DCHECK(data_reduction_proxy_service_->compression_stats()); - return data_reduction_proxy_service_->compression_stats() - ->GetLastUpdateTime(); -} - -void DataReductionProxySettings::ClearDataSavingStatistics( - DataReductionProxySavingsClearedReason reason) { - DCHECK(thread_checker_.CalledOnValidThread()); - DCHECK(data_reduction_proxy_service_->compression_stats()); - data_reduction_proxy_service_->compression_stats()->ClearDataSavingStatistics( - reason); -} - -int64_t DataReductionProxySettings::GetTotalHttpContentLengthSaved() { - DCHECK(thread_checker_.CalledOnValidThread()); - return data_reduction_proxy_service_->compression_stats() - ->GetHttpOriginalContentLength() - - data_reduction_proxy_service_->compression_stats() - ->GetHttpReceivedContentLength(); -} - -void DataReductionProxySettings::SetUnreachable(bool unreachable) { - unreachable_ = unreachable; -} - -bool DataReductionProxySettings::IsDataReductionProxyUnreachable() { - DCHECK(thread_checker_.CalledOnValidThread()); - return unreachable_; -} - -PrefService* DataReductionProxySettings::GetOriginalProfilePrefs() const { - DCHECK(thread_checker_.CalledOnValidThread()); - return prefs_; -} - -base::Time DataReductionProxySettings::GetLastEnabledTime() const { - PrefService* prefs = GetOriginalProfilePrefs(); - int64_t last_enabled_time = - prefs->GetInt64(prefs::kDataReductionProxyLastEnabledTime); - if (last_enabled_time <= 0) - return base::Time(); - return base::Time::FromInternalValue(last_enabled_time); -} - -void DataReductionProxySettings::RegisterDataReductionProxyFieldTrial() { - register_synthetic_field_trial_.Run( - "SyntheticDataReductionProxySetting", - IsDataReductionProxyEnabled() ? "Enabled" : "Disabled"); -} - -void DataReductionProxySettings::OnProxyEnabledPrefChange() { - DCHECK(thread_checker_.CalledOnValidThread()); - if (!register_synthetic_field_trial_.is_null()) { - RegisterDataReductionProxyFieldTrial(); - } - MaybeActivateDataReductionProxy(false); - - bool enabled = IsDataReductionProxyEnabled(); - for (auto& observer : observers_) - observer.OnDataSaverEnabledChanged(enabled); -} - -void DataReductionProxySettings::ResetDataReductionStatistics() { - DCHECK(thread_checker_.CalledOnValidThread()); - DCHECK(data_reduction_proxy_service_->compression_stats()); - data_reduction_proxy_service_->compression_stats()->ResetStatistics(); -} - -void DataReductionProxySettings::MaybeActivateDataReductionProxy( - bool at_startup) { - DCHECK(thread_checker_.CalledOnValidThread()); - PrefService* prefs = GetOriginalProfilePrefs(); - // Do nothing if prefs have not been initialized. This allows unit testing - // of profile related code without having to initialize data reduction proxy - // related prefs. - if (!prefs) - return; - - bool enabled = IsDataSaverEnabledByUser(is_off_the_record_profile_, prefs); - - if (enabled && at_startup) { - const auto last_enabled_time = GetLastEnabledTime(); - if (!last_enabled_time.is_null()) { - // Record the metric only if the time when data reduction proxy was - // enabled is available. - RecordDaysSinceEnabledMetric( - (clock_->Now() - last_enabled_time).InDays()); - } - } - - if (enabled && - !prefs->GetBoolean(prefs::kDataReductionProxyWasEnabledBefore)) { - prefs->SetBoolean(prefs::kDataReductionProxyWasEnabledBefore, true); - ResetDataReductionStatistics(); - } - if (!at_startup) { - if (IsDataReductionProxyEnabled()) { - RecordSettingsEnabledState(DATA_REDUCTION_SETTINGS_ACTION_OFF_TO_ON); - - // Data reduction proxy has been enabled by the user. Record the number of - // days since the data reduction proxy has been enabled as zero, and - // store the current time in the pref. - prefs->SetInt64(prefs::kDataReductionProxyLastEnabledTime, - clock_->Now().ToInternalValue()); - RecordDaysSinceEnabledMetric(0); - } else { - RecordSettingsEnabledState(DATA_REDUCTION_SETTINGS_ACTION_ON_TO_OFF); - } - } } void DataReductionProxySettings::AddDataReductionProxySettingsObserver( @@ -284,70 +60,4 @@ void DataReductionProxySettings::RemoveDataReductionProxySettingsObserver( observers_.RemoveObserver(observer); } -// Metrics methods -void DataReductionProxySettings::RecordDataReductionInit() const { - DCHECK(thread_checker_.CalledOnValidThread()); - RecordStartupState(IsDataReductionProxyEnabled() ? PROXY_ENABLED - : PROXY_DISABLED); - RecordStartupSavings(); -} - -void DataReductionProxySettings::RecordStartupState( - ProxyStartupState state) const { - UMA_HISTOGRAM_ENUMERATION(kUMAProxyStartupStateHistogram, state, - PROXY_STARTUP_STATE_COUNT); -} - -void DataReductionProxySettings::RecordStartupSavings() const { - // Minimum bytes the user should have browsed, for the data savings percent - // UMA to be recorded at startup. - const unsigned int kMinOriginalContentLengthBytes = - 10 * 1024 * 1024; // 10 MB. - - if (!IsDataReductionProxyEnabled()) - return; - - DCHECK(data_reduction_proxy_service_->compression_stats()); - int64_t original_content_length = - data_reduction_proxy_service_->compression_stats() - ->GetHttpOriginalContentLength(); - int64_t received_content_length = - data_reduction_proxy_service_->compression_stats() - ->GetHttpReceivedContentLength(); - if (original_content_length < kMinOriginalContentLengthBytes) - return; - int savings_percent = - static_cast<int>(((original_content_length - received_content_length) / - (float)original_content_length) * - 100.0); - if (savings_percent >= 0) { - UMA_HISTOGRAM_PERCENTAGE("DataReductionProxy.StartupSavingsPercent", - savings_percent > 0 ? savings_percent : 0); - } - if (savings_percent < 0) { - UMA_HISTOGRAM_PERCENTAGE("DataReductionProxy.StartupNegativeSavingsPercent", - -savings_percent); - } -} - -ContentLengthList DataReductionProxySettings::GetDailyContentLengths( - const char* pref_name) { - DCHECK(thread_checker_.CalledOnValidThread()); - DCHECK(data_reduction_proxy_service_->compression_stats()); - return data_reduction_proxy_service_->compression_stats() - ->GetDailyContentLengths(pref_name); -} - -void DataReductionProxySettings::GetContentLengths( - unsigned int days, - int64_t* original_content_length, - int64_t* received_content_length, - int64_t* last_update_time) { - DCHECK(thread_checker_.CalledOnValidThread()); - DCHECK(data_reduction_proxy_service_->compression_stats()); - - data_reduction_proxy_service_->compression_stats()->GetContentLengths( - days, original_content_length, received_content_length, last_update_time); -} - } // namespace data_reduction_proxy diff --git a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h b/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h index 982e668ad00..c1729d5fd6f 100644 --- a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h +++ b/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h @@ -5,53 +5,17 @@ #ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_H_ #define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_H_ -#include <stdint.h> - -#include <memory> - #include "base/callback.h" #include "base/compiler_specific.h" #include "base/gtest_prod_util.h" #include "base/memory/raw_ptr.h" #include "base/observer_list.h" #include "base/threading/thread_checker.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h" -#include "components/prefs/pref_change_registrar.h" -#include "components/prefs/pref_member.h" -#include "net/http/http_request_headers.h" #include "url/gurl.h" -class PrefService; - -namespace base { -class Clock; -} - namespace data_reduction_proxy { -class DataReductionProxyService; -class DataReductionProxyCompressionStats; - -// Values of the UMA DataReductionProxy.StartupState histogram. -// This enum must remain synchronized with DataReductionProxyStartupState -// in metrics/histograms/histograms.xml. -enum ProxyStartupState { - PROXY_NOT_AVAILABLE = 0, - PROXY_DISABLED, - PROXY_ENABLED, - PROXY_STARTUP_STATE_COUNT, -}; - -// Values of the UMA DataReductionProxy.EnabledState histogram. -// This enum must remain synchronized with DataReductionProxyEnabledState -// in metrics/histograms/histograms.xml. -enum DataReductionSettingsEnabledAction { - DATA_REDUCTION_SETTINGS_ACTION_OFF_TO_ON = 0, - DATA_REDUCTION_SETTINGS_ACTION_ON_TO_OFF, - DATA_REDUCTION_SETTINGS_ACTION_BOUNDARY, -}; - // Classes may derive from |DataReductionProxySettingsObserver| and register as // an observer of |DataReductionProxySettings| to get notified when the proxy // request headers change or when the DRPSettings class is initialized. @@ -66,9 +30,6 @@ class DataReductionProxySettingsObserver { // be called from there. class DataReductionProxySettings { public: - using SyntheticFieldTrialRegistrationCallback = - base::RepeatingCallback<bool(base::StringPiece, base::StringPiece)>; - explicit DataReductionProxySettings(bool is_off_the_record_profile); DataReductionProxySettings(const DataReductionProxySettings&) = delete; @@ -80,82 +41,16 @@ class DataReductionProxySettings { // Initializes the Data Reduction Proxy with the profile prefs. The caller // must ensure that all parameters remain alive for the lifetime of the // |DataReductionProxySettings| instance. - void InitDataReductionProxySettings( - PrefService* prefs, - std::unique_ptr<DataReductionProxyService> data_reduction_proxy_service); - - // Sets the |register_synthetic_field_trial_| callback and runs to register - // the DataReductionProxyEnabled synthetic field trial. - void SetCallbackToRegisterSyntheticFieldTrial( - const SyntheticFieldTrialRegistrationCallback& - on_data_reduction_proxy_enabled); + void InitDataReductionProxySettings(); // Returns true if the Data Saver feature is enabled by the user on Android. // This checks only the Data Saver prefs on Android or forcing flag on any // platform. Does not check any holdback experiments. Note that this may be // different from the value of |IsDataReductionProxyEnabled|. - static bool IsDataSaverEnabledByUser(bool is_off_the_record_profile, - PrefService* prefs); + static bool IsDataSaverEnabledByUser(bool is_off_the_record_profile); // Enables or disables Data Saver, regardless of platform. - static void SetDataSaverEnabledForTesting(PrefService* prefs, bool enabled); - - // Returns true if the Data Reduction HTTP Proxy is enabled. Note that this - // may be different from the value of |IsDataSaverEnabledByUser|. - bool IsDataReductionProxyEnabled() const; - - // Returns true if the proxy can be used for the given url. This method does - // not take into account the proxy config or proxy retry list, so it can - // return true even when the proxy will not be used. Specifically, if - // another proxy configuration overrides use of data reduction proxy, or - // if data reduction proxy is in proxy retry list, then data reduction proxy - // will not be used, but this method will still return true. If this method - // returns false, then we are guaranteed that data reduction proxy will not be - // used. - bool CanUseDataReductionProxy(const GURL& url) const; - - // Returns true if the proxy is managed by an adminstrator's policy. - bool IsDataReductionProxyManaged(); - - // Enables or disables the data reduction proxy. - void SetDataReductionProxyEnabled(bool enabled); - - // Returns the time in microseconds that the last update was made to the - // daily original and received content lengths. - int64_t GetDataReductionLastUpdateTime(); - - // Clears all data saving statistics for the given |reason|. - void ClearDataSavingStatistics(DataReductionProxySavingsClearedReason reason); - - // Returns the difference between the total original size of all HTTP content - // received from the network and the actual size of the HTTP content received. - int64_t GetTotalHttpContentLengthSaved(); - - // 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_t* original_content_length, - int64_t* received_content_length, - int64_t* last_update_time); - - // Records that the data reduction proxy is unreachable or not. - void SetUnreachable(bool unreachable); - - // Returns whether the data reduction proxy is unreachable. Returns true - // if no request has successfully completed through proxy, even though atleast - // some of them should have. - bool IsDataReductionProxyUnreachable(); - - ContentLengthList GetDailyContentLengths(const char* pref_name); - - // Configures data reduction proxy. |at_startup| is true when this method is - // called in response to creating or loading a new profile. - void MaybeActivateDataReductionProxy(bool at_startup); - - // Returns the time LiteMode was last enabled. This is reset whenever LiteMode - // is disabled and re-enabled from settings. Null time is returned when - // LiteMode has never been enabled. - base::Time GetLastEnabledTime() const; + static void SetDataSaverEnabledForTesting(bool enabled); // Adds an observer that is notified every time the proxy request headers // change. @@ -167,94 +62,7 @@ class DataReductionProxySettings { void RemoveDataReductionProxySettingsObserver( DataReductionProxySettingsObserver* observer); - DataReductionProxyService* data_reduction_proxy_service() { - return data_reduction_proxy_service_.get(); - } - - protected: - void InitPrefMembers(); - - // Virtualized for unit test support. - virtual PrefService* GetOriginalProfilePrefs() const; - - // Metrics method. Subclasses should override if they wish to provide - // alternatives. - virtual void RecordDataReductionInit() const; - - // Virtualized for mocking. Records UMA specifying whether the proxy was - // enabled or disabled at startup. - virtual void RecordStartupState( - data_reduction_proxy::ProxyStartupState state) const; - private: - friend class DataReductionProxySettingsTestBase; - friend class DataReductionProxySettingsTest; - friend class DataReductionProxyTestContext; - FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, - TestResetDataReductionStatistics); - FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, - TestIsProxyEnabledOrManaged); - FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, - TestCanUseDataReductionProxy); - FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, TestContentLengths); - FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, - TestGetDailyContentLengths); - FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, - TestMaybeActivateDataReductionProxy); - FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, - TestOnProxyEnabledPrefChange); - FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, - TestInitDataReductionProxyOn); - FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, - TestInitDataReductionProxyOff); - FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, - CheckInitMetricsWhenNotAllowed); - FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, - TestSettingsEnabledStateHistograms); - FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, - TestDaysSinceEnabled); - FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, - TestDaysSinceEnabledWithTestClock); - FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, - TestDaysSinceEnabledExistingUser); - FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, - TestDaysSinceSavingsCleared); - - // Registers the trial "SyntheticDataReductionProxySetting" with the group - // "Enabled" or "Disabled". Indicates whether the proxy is turned on or not. - void RegisterDataReductionProxyFieldTrial(); - - void OnProxyEnabledPrefChange(); - - // Records data savings percentage histogram at chrome startup, for users who - // have browsed a reasonable amount. Positive and negative savings are - // recorded in a separate histogram. - void RecordStartupSavings() const; - - void ResetDataReductionStatistics(); - - bool unreachable_; - - // The number of requests to reload the page with images from the Lo-Fi - // UI until Lo-Fi is disabled for the remainder of the session. - int lo_fi_user_requests_for_images_per_session_; - - // The number of consecutive sessions where Lo-Fi was disabled for - // Lo-Fi to be disabled until the next implicit opt out epoch, which may be in - // a later session, or never. - int lo_fi_consecutive_session_disables_; - - std::unique_ptr<DataReductionProxyService> data_reduction_proxy_service_; - - raw_ptr<PrefService> prefs_; - - PrefChangeRegistrar registrar_; - - SyntheticFieldTrialRegistrationCallback register_synthetic_field_trial_; - - // Should not be null. - raw_ptr<base::Clock> clock_; - // Observers to notify when the proxy request headers change or |this| is // initialized. base::ObserverList<DataReductionProxySettingsObserver>::Unchecked observers_; diff --git a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc b/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc deleted file mode 100644 index 4180ef9c1f8..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc +++ /dev/null @@ -1,136 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h" - -#include <stdint.h> - -#include "base/bind.h" -#include "base/command_line.h" -#include "base/strings/string_number_conversions.h" -#include "base/strings/string_piece.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.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.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" -#include "components/prefs/pref_registry_simple.h" -#include "components/prefs/scoped_user_pref_update.h" -#include "components/prefs/testing_pref_service.h" - -using testing::_; -using testing::AnyNumber; -using testing::Return; - -namespace { - -const char kProxy[] = "proxy"; - -} // namespace - -namespace data_reduction_proxy { - -DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase() {} - -DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {} - -// testing::Test implementation: -void DataReductionProxySettingsTestBase::SetUp() { - test_context_ = - DataReductionProxyTestContext::Builder() - .WithMockConfig() - .WithMockDataReductionProxyService() - .SkipSettingsInitialization() - .Build(); - - test_context_->SetDataReductionProxyEnabled(false); - TestingPrefServiceSimple* pref_service = test_context_->pref_service(); - pref_service->SetInt64(prefs::kDailyHttpContentLengthLastUpdateDate, 0L); - pref_service->registry()->RegisterDictionaryPref(kProxy); - pref_service->SetBoolean(prefs::kDataReductionProxyWasEnabledBefore, false); - - ResetSettings(nullptr); - - ListPrefUpdate original_update(test_context_->pref_service(), - prefs::kDailyHttpOriginalContentLength); - ListPrefUpdate received_update(test_context_->pref_service(), - prefs::kDailyHttpReceivedContentLength); - for (int64_t i = 0; i < kNumDaysInHistory; i++) { - original_update->Insert(original_update->GetList().begin(), - base::Value(base::NumberToString(2 * i))); - received_update->Insert(received_update->GetList().begin(), - base::Value(base::NumberToString(i))); - } - last_update_time_ = base::Time::Now().LocalMidnight(); - pref_service->SetInt64(prefs::kDailyHttpContentLengthLastUpdateDate, - last_update_time_.ToInternalValue()); -} - -template <class C> -void DataReductionProxySettingsTestBase::ResetSettings(base::Clock* clock) { - MockDataReductionProxySettings<C>* settings = - new MockDataReductionProxySettings<C>(); - if (settings_) { - settings->data_reduction_proxy_service_ = - std::move(settings_->data_reduction_proxy_service_); - } else { - settings->data_reduction_proxy_service_ = test_context_->TakeService(); - } - settings->data_reduction_proxy_service_->SetSettingsForTesting(settings); - settings->prefs_ = test_context_->pref_service(); - if (clock) - settings->clock_ = clock; - EXPECT_CALL(*settings, GetOriginalProfilePrefs()) - .Times(AnyNumber()) - .WillRepeatedly(Return(test_context_->pref_service())); - EXPECT_CALL(*settings, GetLocalStatePrefs()) - .Times(AnyNumber()) - .WillRepeatedly(Return(test_context_->pref_service())); - settings_.reset(settings); -} - -// Explicitly generate required instantiations. -template void DataReductionProxySettingsTestBase::ResetSettings< - DataReductionProxySettings>(base::Clock* clock); - -void DataReductionProxySettingsTestBase::CheckOnPrefChange( - bool enabled, - bool expected_enabled, - bool managed) { - if (managed) { - test_context_->pref_service()->SetManagedPref( - prefs::kDataSaverEnabled, std::make_unique<base::Value>(enabled)); - } else { - test_context_->SetDataReductionProxyEnabled(enabled); - } - test_context_->RunUntilIdle(); - // Never expect the proxy to be restricted for pref change tests. -} - -void DataReductionProxySettingsTestBase::InitDataReductionProxy( - bool enabled_at_startup) { - settings_->InitDataReductionProxySettings( - test_context_->pref_service(), - std::move(settings_->data_reduction_proxy_service_)); - settings_->SetCallbackToRegisterSyntheticFieldTrial(base::BindRepeating( - &DataReductionProxySettingsTestBase::OnSyntheticFieldTrialRegistration, - base::Unretained(this))); - - test_context_->RunUntilIdle(); -} - -void DataReductionProxySettingsTestBase::CheckDataReductionProxySyntheticTrial( - bool enabled) { - EXPECT_EQ(enabled ? "Enabled" : "Disabled", - synthetic_field_trials_["SyntheticDataReductionProxySetting"]); -} - -bool DataReductionProxySettingsTestBase::OnSyntheticFieldTrialRegistration( - base::StringPiece trial_name, - base::StringPiece group_name) { - synthetic_field_trials_[std::string(trial_name)] = std::string(group_name); - return true; -} - -} // namespace data_reduction_proxy diff --git a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h b/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h deleted file mode 100644 index 913e8209b9d..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_TEST_UTILS_H_ -#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_TEST_UTILS_H_ - -#include <map> -#include <memory> -#include <string> -#include <utility> - -#include "base/message_loop/message_pump_type.h" -#include "base/strings/string_piece.h" -#include "base/task/single_thread_task_executor.h" -#include "base/time/clock.h" -#include "base/time/time.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h" -#include "components/prefs/testing_pref_service.h" -#include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" - -class PrefService; - -namespace data_reduction_proxy { - -class DataReductionProxyTestContext; - -template <class C> -class MockDataReductionProxySettings : public C { - public: - MockDataReductionProxySettings<C>() : C(false) {} - MOCK_CONST_METHOD0(GetOriginalProfilePrefs, PrefService*()); - MOCK_METHOD0(GetLocalStatePrefs, PrefService*()); - MOCK_CONST_METHOD1(RecordStartupState, void(ProxyStartupState state)); -}; - -class DataReductionProxySettingsTestBase : public testing::Test { - public: - static void AddTestProxyToCommandLine(); - - DataReductionProxySettingsTestBase(); - ~DataReductionProxySettingsTestBase() override; - - void AddProxyToCommandLine(); - - void SetUp() override; - - template <class C> - void ResetSettings(base::Clock* clock); - virtual void ResetSettings(base::Clock* clock) = 0; - - void CheckMaybeActivateDataReductionProxy(bool initially_enabled, - bool request_succeeded, - bool expected_enabled, - bool expected_restricted, - bool expected_fallback_restricted); - void CheckOnPrefChange(bool enabled, bool expected_enabled, bool managed); - void InitWithStatisticsPrefs(); - void InitDataReductionProxy(bool enabled_at_startup); - void CheckDataReductionProxySyntheticTrial(bool enabled); - bool OnSyntheticFieldTrialRegistration(base::StringPiece trial_name, - base::StringPiece group_name); - - protected: - base::SingleThreadTaskExecutor io_task_executor_{base::MessagePumpType::IO}; - std::unique_ptr<DataReductionProxyTestContext> test_context_; - std::unique_ptr<DataReductionProxySettings> settings_; - base::Time last_update_time_; - std::map<std::string, std::string> synthetic_field_trials_; -}; - -// Test implementations should be subclasses of an instantiation of this -// class parameterized for whatever DataReductionProxySettings class -// is being tested. -template <class C> -class ConcreteDataReductionProxySettingsTest - : public DataReductionProxySettingsTestBase { - public: - typedef MockDataReductionProxySettings<C> MockSettings; - void ResetSettings(base::Clock* clock) override { - return DataReductionProxySettingsTestBase::ResetSettings<C>(clock); - } -}; - -} // namespace data_reduction_proxy - -#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_TEST_UTILS_H_ diff --git a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc b/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc deleted file mode 100644 index cf0d51796ac..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc +++ /dev/null @@ -1,347 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h" - -#include <stddef.h> -#include <stdint.h> - -#include "base/command_line.h" -#include "base/metrics/field_trial.h" -#include "base/metrics/histogram_samples.h" -#include "base/test/metrics/histogram_tester.h" -#include "base/test/mock_entropy_provider.h" -#include "base/test/simple_test_clock.h" -#include "base/test/task_environment.h" -#include "base/time/clock.h" -#include "base/time/default_clock.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_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_pref_names.h" -#include "components/data_reduction_proxy/core/common/data_reduction_proxy_switches.h" -#include "components/prefs/pref_registry_simple.h" -#include "net/base/proxy_server.h" -#include "net/http/http_util.h" -#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h" -#include "services/network/test/test_url_loader_factory.h" -#include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace data_reduction_proxy { - -class DataReductionProxySettingsTest - : public ConcreteDataReductionProxySettingsTest< - DataReductionProxySettings> { - public: - void CheckMaybeActivateDataReductionProxy(bool initially_enabled, - bool request_succeeded, - bool expected_enabled, - bool expected_restricted, - bool expected_fallback_restricted) { - test_context_->SetDataReductionProxyEnabled(initially_enabled); - settings_->MaybeActivateDataReductionProxy(false); - test_context_->RunUntilIdle(); - } -}; - -TEST_F(DataReductionProxySettingsTest, TestResetDataReductionStatistics) { - int64_t original_content_length; - int64_t received_content_length; - int64_t last_update_time; - settings_->ResetDataReductionStatistics(); - settings_->GetContentLengths(kNumDaysInHistory, - &original_content_length, - &received_content_length, - &last_update_time); - EXPECT_EQ(0L, original_content_length); - EXPECT_EQ(0L, received_content_length); - EXPECT_EQ(last_update_time_.ToInternalValue(), last_update_time); -} - -TEST_F(DataReductionProxySettingsTest, TestContentLengths) { - int64_t original_content_length; - int64_t received_content_length; - int64_t last_update_time; - - // Request |kNumDaysInHistory| days. - settings_->GetContentLengths(kNumDaysInHistory, - &original_content_length, - &received_content_length, - &last_update_time); - const unsigned int days = kNumDaysInHistory; - // Received content length history values are 0 to |kNumDaysInHistory - 1|. - int64_t expected_total_received_content_length = (days - 1L) * days / 2; - // Original content length history values are 0 to - // |2 * (kNumDaysInHistory - 1)|. - long expected_total_original_content_length = (days - 1L) * days; - EXPECT_EQ(expected_total_original_content_length, original_content_length); - EXPECT_EQ(expected_total_received_content_length, received_content_length); - EXPECT_EQ(last_update_time_.ToInternalValue(), last_update_time); - - // Request |kNumDaysInHistory - 1| days. - settings_->GetContentLengths(kNumDaysInHistory - 1, - &original_content_length, - &received_content_length, - &last_update_time); - expected_total_received_content_length -= (days - 1); - expected_total_original_content_length -= 2 * (days - 1); - EXPECT_EQ(expected_total_original_content_length, original_content_length); - EXPECT_EQ(expected_total_received_content_length, received_content_length); - - // Request 0 days. - settings_->GetContentLengths(0, - &original_content_length, - &received_content_length, - &last_update_time); - expected_total_received_content_length = 0; - expected_total_original_content_length = 0; - EXPECT_EQ(expected_total_original_content_length, original_content_length); - EXPECT_EQ(expected_total_received_content_length, received_content_length); - - // Request 1 day. First day had 0 bytes so should be same as 0 days. - settings_->GetContentLengths(1, - &original_content_length, - &received_content_length, - &last_update_time); - EXPECT_EQ(expected_total_original_content_length, original_content_length); - EXPECT_EQ(expected_total_received_content_length, received_content_length); -} - - -TEST(DataReductionProxySettingsStandaloneTest, TestIsProxyEnabledOrManaged) { - base::test::SingleThreadTaskEnvironment task_environment{ - base::test::SingleThreadTaskEnvironment::MainThreadType::IO}; - std::unique_ptr<DataReductionProxyTestContext> drp_test_context = - DataReductionProxyTestContext::Builder() - .WithMockConfig() - .WithMockDataReductionProxyService() - .SkipSettingsInitialization() - .Build(); - - drp_test_context->InitSettings(); - - DataReductionProxySettings* settings = drp_test_context->settings(); - - drp_test_context->SetDataReductionProxyEnabled(true); - EXPECT_TRUE(settings->IsDataReductionProxyEnabled()); - EXPECT_FALSE(settings->IsDataReductionProxyManaged()); - - drp_test_context->SetDataReductionProxyEnabled(false); - EXPECT_FALSE(settings->IsDataReductionProxyEnabled()); - EXPECT_FALSE(settings->IsDataReductionProxyManaged()); - - drp_test_context->SetDataReductionProxyEnabled(false); - drp_test_context->pref_service()->SetManagedPref( - prefs::kDataSaverEnabled, std::make_unique<base::Value>(false)); - EXPECT_FALSE(settings->IsDataReductionProxyEnabled()); - EXPECT_TRUE(settings->IsDataReductionProxyManaged()); - - drp_test_context->SetDataReductionProxyEnabled(true); - drp_test_context->pref_service()->SetManagedPref( - prefs::kDataSaverEnabled, std::make_unique<base::Value>(true)); - EXPECT_TRUE(settings->IsDataReductionProxyEnabled()); - EXPECT_TRUE(settings->IsDataReductionProxyManaged()); - - drp_test_context->RunUntilIdle(); -} - - -TEST_F(DataReductionProxySettingsTest, TestMaybeActivateDataReductionProxy) { - // Initialize the pref member in |settings_| without the usual callback - // so it won't trigger MaybeActivateDataReductionProxy when the pref value - // is set. - - // TODO(bengr): Test enabling/disabling while a secure proxy check is - // outstanding. - // The proxy is enabled and unrestricted initially. - // Request succeeded but with bad response, expect proxy to be restricted. - CheckMaybeActivateDataReductionProxy(true, true, true, true, false); - // Request succeeded with valid response, expect proxy to be unrestricted. - CheckMaybeActivateDataReductionProxy(true, true, true, false, false); - // Request failed, expect proxy to be enabled but restricted. - CheckMaybeActivateDataReductionProxy(true, false, true, true, false); - // The proxy is disabled initially. No secure proxy checks should take place, - // and so the state should not change. - CheckMaybeActivateDataReductionProxy(false, true, false, false, false); -} - -TEST_F(DataReductionProxySettingsTest, TestInitDataReductionProxyOn) { - MockSettings* settings = static_cast<MockSettings*>(settings_.get()); - EXPECT_CALL(*settings, RecordStartupState(PROXY_ENABLED)); - - test_context_->SetDataReductionProxyEnabled(true); - InitDataReductionProxy(true); - CheckDataReductionProxySyntheticTrial(true); -} - -TEST_F(DataReductionProxySettingsTest, TestInitDataReductionProxyOff) { - // InitDataReductionProxySettings with the preference off will directly call - // LogProxyState. - MockSettings* settings = static_cast<MockSettings*>(settings_.get()); - EXPECT_CALL(*settings, RecordStartupState(PROXY_DISABLED)); - - test_context_->SetDataReductionProxyEnabled(false); - InitDataReductionProxy(false); - CheckDataReductionProxySyntheticTrial(false); -} - -TEST_F(DataReductionProxySettingsTest, TestEnableProxyFromCommandLine) { - MockSettings* settings = static_cast<MockSettings*>(settings_.get()); - EXPECT_CALL(*settings, RecordStartupState(PROXY_ENABLED)); - - base::CommandLine::ForCurrentProcess()->AppendSwitch( - switches::kEnableDataReductionProxy); - InitDataReductionProxy(true); - CheckDataReductionProxySyntheticTrial(true); -} - -TEST_F(DataReductionProxySettingsTest, TestSetDataReductionProxyEnabled) { - MockSettings* settings = static_cast<MockSettings*>(settings_.get()); - EXPECT_CALL(*settings, RecordStartupState(PROXY_ENABLED)); - test_context_->SetDataReductionProxyEnabled(true); - InitDataReductionProxy(true); - - test_context_->SetDataReductionProxyEnabled(false); - test_context_->RunUntilIdle(); - CheckDataReductionProxySyntheticTrial(false); - - test_context_->SetDataReductionProxyEnabled(true); - test_context_->RunUntilIdle(); - CheckDataReductionProxySyntheticTrial(true); -} - -TEST_F(DataReductionProxySettingsTest, TestSettingsEnabledStateHistograms) { - const char kUMAEnabledState[] = "DataReductionProxy.EnabledState"; - base::HistogramTester histogram_tester; - - // No settings state histograms should be recorded during startup. - test_context_->RunUntilIdle(); - histogram_tester.ExpectTotalCount(kUMAEnabledState, 0); - - test_context_->SetDataReductionProxyEnabled(true); - settings_->MaybeActivateDataReductionProxy(false); - test_context_->RunUntilIdle(); - histogram_tester.ExpectBucketCount( - kUMAEnabledState, DATA_REDUCTION_SETTINGS_ACTION_OFF_TO_ON, 1); - histogram_tester.ExpectBucketCount( - kUMAEnabledState, DATA_REDUCTION_SETTINGS_ACTION_ON_TO_OFF, 0); - - test_context_->SetDataReductionProxyEnabled(false); - settings_->MaybeActivateDataReductionProxy(false); - test_context_->RunUntilIdle(); - histogram_tester.ExpectBucketCount( - kUMAEnabledState, DATA_REDUCTION_SETTINGS_ACTION_OFF_TO_ON, 1); - histogram_tester.ExpectBucketCount( - kUMAEnabledState, DATA_REDUCTION_SETTINGS_ACTION_ON_TO_OFF, 1); -} - -// Verify that the UMA metric and the pref is recorded correctly when the user -// enables the data reduction proxy. -TEST_F(DataReductionProxySettingsTest, TestDaysSinceEnabledWithTestClock) { - const char kUMAEnabledState[] = "DataReductionProxy.DaysSinceEnabled"; - base::SimpleTestClock clock; - clock.Advance(base::Days(1)); - ResetSettings(&clock); - - base::Time last_enabled_time = clock.Now(); - - { - base::HistogramTester histogram_tester; - - test_context_->RunUntilIdle(); - histogram_tester.ExpectTotalCount(kUMAEnabledState, 0); - - // Enable data reduction proxy. The metric should be recorded. - test_context_->SetDataReductionProxyEnabled(true); - settings_->MaybeActivateDataReductionProxy(false); - test_context_->RunUntilIdle(); - - last_enabled_time = clock.Now(); - - EXPECT_EQ( - last_enabled_time, - base::Time::FromInternalValue(test_context_->pref_service()->GetInt64( - prefs::kDataReductionProxyLastEnabledTime))); - histogram_tester.ExpectUniqueSample(kUMAEnabledState, 0, 1); - } - - { - // Simulate turning off and on of data reduction proxy while Chromium is - // running. - test_context_->SetDataReductionProxyEnabled(false); - settings_->MaybeActivateDataReductionProxy(false); - clock.Advance(base::Days(1)); - last_enabled_time = clock.Now(); - - test_context_->SetDataReductionProxyEnabled(true); - base::HistogramTester histogram_tester; - settings_->MaybeActivateDataReductionProxy(false /* at_startup */); - test_context_->RunUntilIdle(); - histogram_tester.ExpectUniqueSample(kUMAEnabledState, 0, 1); - EXPECT_EQ( - last_enabled_time, - base::Time::FromInternalValue(test_context_->pref_service()->GetInt64( - prefs::kDataReductionProxyLastEnabledTime))); - } - - { - // Advance clock by a random number of days. - int advance_clock_days = 42; - clock.Advance(base::Days(advance_clock_days)); - base::HistogramTester histogram_tester; - // Simulate Chromium start up. Data reduction proxy was enabled - // |advance_clock_days| ago. - settings_->MaybeActivateDataReductionProxy(true /* at_startup */); - test_context_->RunUntilIdle(); - histogram_tester.ExpectUniqueSample(kUMAEnabledState, advance_clock_days, - 1); - EXPECT_EQ( - last_enabled_time, - base::Time::FromInternalValue(test_context_->pref_service()->GetInt64( - prefs::kDataReductionProxyLastEnabledTime))); - } -} - -// Verify that the pref and the UMA metric are not recorded for existing users -// that already have data reduction proxy on. -TEST(DataReductionProxySettingsStandaloneTest, - TestDaysSinceEnabledExistingUser) { - base::test::SingleThreadTaskEnvironment task_environment{ - base::test::SingleThreadTaskEnvironment::MainThreadType::IO}; - std::unique_ptr<DataReductionProxyTestContext> drp_test_context = - DataReductionProxyTestContext::Builder() - .WithMockConfig() - .WithMockDataReductionProxyService() - .SkipSettingsInitialization() - .Build(); - - drp_test_context->InitSettings(); - - base::HistogramTester histogram_tester; - - // Simulate Chromium startup with data reduction proxy already enabled. - drp_test_context->settings()->MaybeActivateDataReductionProxy( - true /* at_startup */); - drp_test_context->RunUntilIdle(); - histogram_tester.ExpectTotalCount("DataReductionProxy.DaysSinceEnabled", 0); - EXPECT_EQ(0, drp_test_context->pref_service()->GetInt64( - prefs::kDataReductionProxyLastEnabledTime)); -} - -TEST_F(DataReductionProxySettingsTest, TestGetDailyContentLengths) { - ContentLengthList result = - settings_->GetDailyContentLengths(prefs::kDailyHttpOriginalContentLength); - - ASSERT_FALSE(result.empty()); - ASSERT_EQ(kNumDaysInHistory, result.size()); - - for (size_t i = 0; i < kNumDaysInHistory; ++i) { - long expected_length = - static_cast<long>((kNumDaysInHistory - 1 - i) * 2); - ASSERT_EQ(expected_length, result[i]); - } -} - -} // namespace data_reduction_proxy diff --git a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.cc b/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.cc deleted file mode 100644 index 078e0fe8990..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.cc +++ /dev/null @@ -1,311 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h" - -#include <map> -#include <memory> -#include <utility> - -#include "base/bind.h" -#include "base/command_line.h" -#include "base/memory/ptr_util.h" -#include "base/run_loop.h" -#include "base/strings/string_piece.h" -#include "base/threading/thread_task_runner_handle.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h" -#include "components/data_reduction_proxy/core/browser/data_store.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" -#include "components/prefs/pref_registry_simple.h" -#include "components/prefs/testing_pref_service.h" -#include "net/base/network_delegate_impl.h" -#include "net/proxy_resolution/proxy_config.h" -#include "net/proxy_resolution/proxy_info.h" -#include "net/proxy_resolution/proxy_list.h" -#include "services/network/test/test_network_connection_tracker.h" -#include "services/network/test/test_network_quality_tracker.h" -#include "url/gurl.h" - -namespace { - -enum TestContextOptions { - // Permits mocking of the underlying |DataReductionProxyConfig|. - USE_MOCK_CONFIG = 0x1, - // Construct, but do not initialize the |DataReductionProxySettings| object. - // Primarily used for testing of the |DataReductionProxySettings| object - // itself. - SKIP_SETTINGS_INITIALIZATION = 0x2, - // Permits mocking of the underlying |DataReductionProxyService|. - USE_MOCK_SERVICE = 0x4, -}; - -} // namespace - -namespace data_reduction_proxy { - -MockDataReductionProxyService::MockDataReductionProxyService( - data_use_measurement::DataUseMeasurement* data_use_measurement, - DataReductionProxySettings* settings, - PrefService* prefs, - const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) - : DataReductionProxyService(settings, - prefs, - std::make_unique<TestDataStore>(), - data_use_measurement, - task_runner, - base::TimeDelta()) {} - -MockDataReductionProxyService::~MockDataReductionProxyService() {} - -TestDataReductionProxyService::TestDataReductionProxyService( - data_use_measurement::DataUseMeasurement* data_use_measurement, - DataReductionProxySettings* settings, - PrefService* prefs, - const scoped_refptr<base::SequencedTaskRunner>& db_task_runner) - : DataReductionProxyService(settings, - prefs, - std::make_unique<TestDataStore>(), - data_use_measurement, - db_task_runner, - base::TimeDelta()) {} - -TestDataReductionProxyService::~TestDataReductionProxyService() {} - -TestDataStore::TestDataStore() {} - -TestDataStore::~TestDataStore() {} - -DataStore::Status TestDataStore::Get(base::StringPiece key, - std::string* value) { - auto value_iter = map_.find(std::string(key)); - if (value_iter == map_.end()) - return NOT_FOUND; - - value->assign(value_iter->second); - return OK; -} - -DataStore::Status TestDataStore::Put( - const std::map<std::string, std::string>& map) { - for (auto iter = map.begin(); iter != map.end(); ++iter) - map_[iter->first] = iter->second; - - return OK; -} - -DataStore::Status TestDataStore::Delete(base::StringPiece key) { - map_.erase(std::string(key)); - - return OK; -} - -DataStore::Status TestDataStore::RecreateDB() { - map_.clear(); - - return OK; -} - -DataReductionProxyTestContext::Builder::Builder() - : use_mock_config_(false), - use_mock_service_(false), - skip_settings_initialization_(false), - data_use_measurement_( - std::make_unique<data_use_measurement::DataUseMeasurement>( - nullptr, - network::TestNetworkConnectionTracker::GetInstance())) {} - -DataReductionProxyTestContext::Builder::~Builder() {} - -DataReductionProxyTestContext::Builder& -DataReductionProxyTestContext::Builder::WithMockConfig() { - use_mock_config_ = true; - return *this; -} - -DataReductionProxyTestContext::Builder& -DataReductionProxyTestContext::Builder::WithMockDataReductionProxyService() { - use_mock_service_ = true; - return *this; -} - -DataReductionProxyTestContext::Builder& -DataReductionProxyTestContext::Builder::SkipSettingsInitialization() { - skip_settings_initialization_ = true; - return *this; -} - -DataReductionProxyTestContext::Builder& -DataReductionProxyTestContext::Builder::WithSettings( - std::unique_ptr<DataReductionProxySettings> settings) { - settings_ = std::move(settings); - return *this; -} - -std::unique_ptr<DataReductionProxyTestContext> -DataReductionProxyTestContext::Builder::Build() { - unsigned int test_context_flags = 0; - scoped_refptr<base::SingleThreadTaskRunner> task_runner = - base::ThreadTaskRunnerHandle::Get(); - std::unique_ptr<TestingPrefServiceSimple> pref_service( - new TestingPrefServiceSimple()); - - if (use_mock_config_) { - test_context_flags |= USE_MOCK_CONFIG; - } else { - test_context_flags ^= USE_MOCK_CONFIG; - } - - - if (!settings_) - settings_ = std::make_unique<DataReductionProxySettings>(false); - if (skip_settings_initialization_) { - test_context_flags |= SKIP_SETTINGS_INITIALIZATION; - } - - pref_service->registry()->RegisterBooleanPref(prefs::kDataSaverEnabled, - false); - RegisterSimpleProfilePrefs(pref_service->registry()); - - std::unique_ptr<DataReductionProxyService> service; - if (use_mock_service_) { - test_context_flags |= USE_MOCK_SERVICE; - service = std::make_unique<MockDataReductionProxyService>( - data_use_measurement_.get(), settings_.get(), pref_service.get(), - task_runner); - } else { - service = std::make_unique<TestDataReductionProxyService>( - data_use_measurement_.get(), settings_.get(), pref_service.get(), - task_runner); - } - - std::unique_ptr<DataReductionProxyTestContext> test_context( - new DataReductionProxyTestContext( - std::move(data_use_measurement_), task_runner, - std::move(pref_service), std::move(settings_), std::move(service), - test_context_flags)); - - if (!skip_settings_initialization_) - test_context->InitSettingsWithoutCheck(); - - return test_context; -} - -DataReductionProxyTestContext::DataReductionProxyTestContext( - std::unique_ptr<data_use_measurement::DataUseMeasurement> - data_use_measurement, - const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, - std::unique_ptr<TestingPrefServiceSimple> simple_pref_service, - std::unique_ptr<DataReductionProxySettings> settings, - std::unique_ptr<DataReductionProxyService> service, - unsigned int test_context_flags) - : data_use_measurement_(std::move(data_use_measurement)), - test_context_flags_(test_context_flags), - task_runner_(task_runner), - simple_pref_service_(std::move(simple_pref_service)), - settings_(std::move(settings)), - service_(std::move(service)) { - DCHECK(data_use_measurement_); - - if (service_) - data_reduction_proxy_service_ = service_.get(); - else - data_reduction_proxy_service_ = settings_->data_reduction_proxy_service(); -} - -DataReductionProxyTestContext::~DataReductionProxyTestContext() { - DestroySettings(); -} - -void DataReductionProxyTestContext::RegisterDataReductionProxyEnabledPref() { - simple_pref_service_->registry()->RegisterBooleanPref( - prefs::kDataSaverEnabled, false); -} - -void DataReductionProxyTestContext::SetDataReductionProxyEnabled(bool enabled) { - // Set the command line so that |IsDataSaverEnabledByUser| returns as expected - // on all platforms. - base::CommandLine* cmd = base::CommandLine::ForCurrentProcess(); - if (enabled) { - cmd->AppendSwitch(switches::kEnableDataReductionProxy); - } else { - cmd->RemoveSwitch(switches::kEnableDataReductionProxy); - } - - simple_pref_service_->SetBoolean(prefs::kDataSaverEnabled, enabled); -} - -bool DataReductionProxyTestContext::IsDataReductionProxyEnabled() const { - return simple_pref_service_->GetBoolean(prefs::kDataSaverEnabled); -} - -void DataReductionProxyTestContext::RunUntilIdle() { - base::RunLoop().RunUntilIdle(); -} - -void DataReductionProxyTestContext::InitSettings() { - DCHECK(test_context_flags_ & SKIP_SETTINGS_INITIALIZATION); - InitSettingsWithoutCheck(); -} - -void DataReductionProxyTestContext::DestroySettings() { - // Force destruction of |DBDataOwner|, which lives on DB task runner and is - // indirectly owned by |settings_|. - if (settings_) { - settings_.reset(); - RunUntilIdle(); - } -} - -void DataReductionProxyTestContext::InitSettingsWithoutCheck() { - DCHECK(service_); - settings_->InitDataReductionProxySettings(simple_pref_service_.get(), - std::move(service_)); -} - -std::unique_ptr<DataReductionProxyService> -DataReductionProxyTestContext::TakeService() { - DCHECK(service_); - DCHECK(test_context_flags_ & SKIP_SETTINGS_INITIALIZATION); - return std::move(service_); -} - -void DataReductionProxyTestContext:: - EnableDataReductionProxyWithSecureProxyCheckSuccess() { - // |settings_| needs to have been initialized, since a - // |DataReductionProxyService| is needed in order to issue the secure proxy - // check. - DCHECK(data_reduction_proxy_service()); - - // Set the pref to cause the secure proxy check to be issued. - SetDataReductionProxyEnabled(true); - RunUntilIdle(); -} - - - -DataReductionProxyService* -DataReductionProxyTestContext::data_reduction_proxy_service() const { - return data_reduction_proxy_service_; -} - -TestDataReductionProxyService* -DataReductionProxyTestContext::test_data_reduction_proxy_service() const { - DCHECK(!(test_context_flags_ & USE_MOCK_SERVICE)); - return static_cast<TestDataReductionProxyService*>( - data_reduction_proxy_service()); -} - -MockDataReductionProxyService* -DataReductionProxyTestContext::mock_data_reduction_proxy_service() const { - DCHECK(!(test_context_flags_ & SKIP_SETTINGS_INITIALIZATION)); - DCHECK(test_context_flags_ & USE_MOCK_SERVICE); - return static_cast<MockDataReductionProxyService*>( - data_reduction_proxy_service()); -} - -} // namespace data_reduction_proxy diff --git a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h b/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h deleted file mode 100644 index ac87bacc3e7..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h +++ /dev/null @@ -1,228 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_TEST_UTILS_H_ -#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_TEST_UTILS_H_ - -#include <stddef.h> -#include <stdint.h> - -#include <memory> -#include <string> - -#include "base/memory/raw_ptr.h" -#include "base/memory/ref_counted.h" -#include "base/strings/string_piece.h" -#include "base/task/single_thread_task_runner.h" -#include "base/time/clock.h" -#include "base/time/tick_clock.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h" -#include "components/data_reduction_proxy/core/browser/data_store.h" -#include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h" -#include "components/data_use_measurement/core/data_use_measurement.h" -#include "components/prefs/pref_registry_simple.h" -#include "components/prefs/testing_pref_service.h" -#include "net/base/backoff_entry.h" -#include "net/base/proxy_server.h" -#include "testing/gmock/include/gmock/gmock.h" - -class TestingPrefServiceSimple; - -namespace data_reduction_proxy { - -class DataReductionProxySettings; - - -// Test version of |DataReductionProxyService|, which permits mocking of various -// methods. -class MockDataReductionProxyService : public DataReductionProxyService { - public: - MockDataReductionProxyService( - data_use_measurement::DataUseMeasurement* data_use_measurement, - DataReductionProxySettings* settings, - PrefService* prefs, - const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); - ~MockDataReductionProxyService() override; - - MOCK_METHOD2(SetProxyPrefs, void(bool enabled, bool at_startup)); - MOCK_METHOD7(UpdateContentLengths, - void(int64_t data_used, - int64_t original_size, - bool data_reduction_proxy_enabled, - const std::string& mime_type, - bool is_user_traffic, - data_use_measurement::DataUseUserData::DataUseContentType - content_type, - int32_t service_hash_code)); - MOCK_METHOD3(UpdateDataUseForHost, - void(int64_t network_bytes, - int64_t original_bytes, - const std::string& host)); -}; - -// Test version of |DataReductionProxyService|, which bypasses initialization in -// the constructor in favor of explicitly passing in its owned classes. This -// permits the use of test/mock versions of those classes. -class TestDataReductionProxyService : public DataReductionProxyService { - public: - TestDataReductionProxyService( - data_use_measurement::DataUseMeasurement* data_use_measurement, - DataReductionProxySettings* settings, - PrefService* prefs, - const scoped_refptr<base::SequencedTaskRunner>& db_task_runner); - ~TestDataReductionProxyService() override; - - bool ignore_blocklist() const { return ignore_blocklist_; } - - private: - // Whether the long term blocklist rules should be ignored. - bool ignore_blocklist_ = false; -}; - -// Test version of |DataStore|. Uses an in memory hash map to store data. -class TestDataStore : public data_reduction_proxy::DataStore { - public: - TestDataStore(); - - ~TestDataStore() override; - - void InitializeOnDBThread() override {} - - DataStore::Status Get(base::StringPiece key, std::string* value) override; - - DataStore::Status Put(const std::map<std::string, std::string>& map) override; - - DataStore::Status Delete(base::StringPiece key) override; - - DataStore::Status RecreateDB() override; - - std::map<std::string, std::string>* map() { return &map_; } - - private: - std::map<std::string, std::string> map_; -}; - -// Builds a test version of the Data Reduction Proxy stack for use in tests. -// Takes in various |TestContextOptions| which controls the behavior of the -// underlying objects. -class DataReductionProxyTestContext { - public: - // Allows for a fluent builder interface to configure what kind of objects - // (test vs mock vs real) are used by the |DataReductionProxyTestContext|. - class Builder { - public: - Builder(); - - ~Builder(); - - // Specifies the use of |MockDataReductionProxyConfig| instead of - // |TestDataReductionProxyConfig|. - Builder& WithMockConfig(); - - // Specifies the use of |MockDataReductionProxyService| instead of - // |DataReductionProxyService|. - Builder& WithMockDataReductionProxyService(); - - // Construct, but do not initialize the |DataReductionProxySettings| object. - Builder& SkipSettingsInitialization(); - - // Specifies a settings object to use. - Builder& WithSettings(std::unique_ptr<DataReductionProxySettings> settings); - - // Creates a |DataReductionProxyTestContext|. Owned by the caller. - std::unique_ptr<DataReductionProxyTestContext> Build(); - - private: - bool use_mock_config_; - bool use_mock_service_; - bool skip_settings_initialization_; - std::unique_ptr<DataReductionProxySettings> settings_; - std::unique_ptr<data_use_measurement::DataUseMeasurement> - data_use_measurement_; - }; - - DataReductionProxyTestContext(const DataReductionProxyTestContext&) = delete; - DataReductionProxyTestContext& operator=( - const DataReductionProxyTestContext&) = delete; - - virtual ~DataReductionProxyTestContext(); - - // Registers, sets, and gets the preference used to enable the Data Reduction - // Proxy, respectively. - void RegisterDataReductionProxyEnabledPref(); - void SetDataReductionProxyEnabled(bool enabled); - bool IsDataReductionProxyEnabled() const; - - // Waits while executing all tasks on the current SingleThreadTaskRunner. - void RunUntilIdle(); - - // Initializes the |DataReductionProxySettings| object. Can only be called if - // built with SkipSettingsInitialization. - void InitSettings(); - - // Destroys the |DataReductionProxySettings| object and waits until objects on - // the DB task runner are destroyed. - void DestroySettings(); - - // Takes ownership of the |DataReductionProxyService| object. Can only be - // called if built with SkipSettingsInitialization. - std::unique_ptr<DataReductionProxyService> TakeService(); - - // Enable the Data Reduction Proxy, simulating a successful secure proxy - // check. This can only be called if not built with WithTestConfigurator, - // |settings_| has been initialized, and |this| was built with a - // |net::MockClientSocketFactory| specified. - void EnableDataReductionProxyWithSecureProxyCheckSuccess(); - - - DataReductionProxyService* data_reduction_proxy_service() const; - - // Returns the underlying |TestDataReductionProxyService|. This can only be - // called if not built with WithMockDataReductionProxyService. - TestDataReductionProxyService* test_data_reduction_proxy_service() const; - - // Returns the underlying |MockDataReductionProxyService|. This can only - // be called if built with WithMockDataReductionProxyService. - MockDataReductionProxyService* mock_data_reduction_proxy_service() const; - - scoped_refptr<base::SingleThreadTaskRunner> task_runner() const { - return task_runner_; - } - - TestingPrefServiceSimple* pref_service() { - return simple_pref_service_.get(); - } - - DataReductionProxySettings* settings() const { return settings_.get(); } - - void InitSettingsWithoutCheck(); - - - private: - DataReductionProxyTestContext( - std::unique_ptr<data_use_measurement::DataUseMeasurement> - data_use_measurement, - const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, - std::unique_ptr<TestingPrefServiceSimple> simple_pref_service, - std::unique_ptr<DataReductionProxySettings> settings, - std::unique_ptr<DataReductionProxyService> service, - unsigned int test_context_flags); - - std::unique_ptr<data_use_measurement::DataUseMeasurement> - data_use_measurement_; - - unsigned int test_context_flags_; - - scoped_refptr<base::SingleThreadTaskRunner> task_runner_; - std::unique_ptr<TestingPrefServiceSimple> simple_pref_service_; - - std::unique_ptr<DataReductionProxySettings> settings_; - raw_ptr<DataReductionProxyService> data_reduction_proxy_service_; - std::unique_ptr<DataReductionProxyService> service_; -}; - -} // namespace data_reduction_proxy - -#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_TEST_UTILS_H_ diff --git a/chromium/components/data_reduction_proxy/core/browser/data_store.cc b/chromium/components/data_reduction_proxy/core/browser/data_store.cc deleted file mode 100644 index 782a1944b47..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_store.cc +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/data_reduction_proxy/core/browser/data_store.h" - -namespace data_reduction_proxy { - -DataStore::DataStore() { -} - -DataStore::~DataStore() { -} - -void DataStore::InitializeOnDBThread() { -} - -DataStore::Status DataStore::Get(base::StringPiece key, std::string* value) { - return DataStore::Status::NOT_FOUND; -} - -DataStore::Status DataStore::Put( - const std::map<std::string, std::string>& map) { - return DataStore::Status::OK; -} - -DataStore::Status DataStore::Delete(base::StringPiece key) { - return DataStore::Status::OK; -} - -DataStore::Status DataStore::RecreateDB() { - return DataStore::Status::OK; -} - -} // namespace data_reduction_proxy diff --git a/chromium/components/data_reduction_proxy/core/browser/data_store.h b/chromium/components/data_reduction_proxy/core/browser/data_store.h deleted file mode 100644 index 7b90f1bc7af..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_store.h +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_STORE_H_ -#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_STORE_H_ - -#include <map> -#include <string> - -#include "base/strings/string_piece.h" - -namespace data_reduction_proxy { - -// Interface for a permanent key/value store used by the Data Reduction Proxy -// component. This class has no-op methods for clients that do not want to -// support a permanent store. -class DataStore { - public: - // Values are used in UMA. Do not change existing values; only append to the - // end. STATUS_MAX should always be last element. - enum Status { OK, NOT_FOUND, CORRUPTED, IO_ERROR, MISC_ERROR, STATUS_MAX }; - - DataStore(); - - DataStore(const DataStore&) = delete; - DataStore& operator=(const DataStore&) = delete; - - virtual ~DataStore(); - - // Initializes the store on DB sequenced task runner. - virtual void InitializeOnDBThread(); - - // Gets the value from the store for the provided key. - virtual Status Get(base::StringPiece key, std::string* value); - - // Persists the provided keys and values into the store. - virtual Status Put(const std::map<std::string, std::string>& map); - - virtual Status Delete(base::StringPiece key); - - // Deletes the LevelDB and recreates it. - virtual Status RecreateDB(); -}; - -} // namespace data_reduction_proxy - -#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_STORE_H_ diff --git a/chromium/components/data_reduction_proxy/core/browser/data_store_impl.cc b/chromium/components/data_reduction_proxy/core/browser/data_store_impl.cc deleted file mode 100644 index 043ed02b896..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_store_impl.cc +++ /dev/null @@ -1,159 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/data_reduction_proxy/core/browser/data_store_impl.h" - -#include <stdint.h> - -#include "base/files/file_util.h" -#include "base/logging.h" -#include "base/memory/ref_counted.h" -#include "base/metrics/histogram_macros.h" -#include "components/data_reduction_proxy/proto/data_store.pb.h" -#include "third_party/leveldatabase/env_chromium.h" -#include "third_party/leveldatabase/leveldb_chrome.h" -#include "third_party/leveldatabase/src/include/leveldb/db.h" -#include "third_party/leveldatabase/src/include/leveldb/options.h" -#include "third_party/leveldatabase/src/include/leveldb/status.h" -#include "third_party/leveldatabase/src/include/leveldb/write_batch.h" - -namespace { - -const base::FilePath::CharType kDBName[] = - FILE_PATH_LITERAL("data_reduction_proxy_leveldb"); - -data_reduction_proxy::DataStore::Status LevelDbToDRPStoreStatus( - leveldb::Status leveldb_status) { - if (leveldb_status.ok()) - return data_reduction_proxy::DataStore::Status::OK; - if (leveldb_status.IsNotFound()) - return data_reduction_proxy::DataStore::Status::NOT_FOUND; - if (leveldb_status.IsCorruption()) - return data_reduction_proxy::DataStore::Status::CORRUPTED; - if (leveldb_status.IsIOError()) - return data_reduction_proxy::DataStore::Status::IO_ERROR; - - return data_reduction_proxy::DataStore::Status::MISC_ERROR; -} - -} // namespace - -namespace data_reduction_proxy { - -DataStoreImpl::DataStoreImpl(const base::FilePath& profile_path) - : profile_path_(profile_path) { - sequence_checker_.DetachFromSequence(); -} - -DataStoreImpl::~DataStoreImpl() { - DCHECK(sequence_checker_.CalledOnValidSequence()); -} - -void DataStoreImpl::InitializeOnDBThread() { - DCHECK(sequence_checker_.CalledOnValidSequence()); - DCHECK(!db_); - - DataStore::Status status = OpenDB(); - if (status == CORRUPTED) - RecreateDB(); -} - -DataStore::Status DataStoreImpl::Get(base::StringPiece key, - std::string* value) { - DCHECK(sequence_checker_.CalledOnValidSequence()); - - if (!db_) - return MISC_ERROR; - - leveldb::ReadOptions read_options; - read_options.verify_checksums = true; - leveldb::Slice slice(key.data(), key.size()); - leveldb::Status status = db_->Get(read_options, slice, value); - if (status.IsCorruption()) - RecreateDB(); - - return LevelDbToDRPStoreStatus(status); -} - -DataStore::Status DataStoreImpl::Put( - const std::map<std::string, std::string>& map) { - DCHECK(sequence_checker_.CalledOnValidSequence()); - - if (!db_) - return MISC_ERROR; - - leveldb::WriteBatch batch; - for (const auto& iter : map) { - batch.Put(iter.first, iter.second); - } - - leveldb::WriteOptions write_options; - leveldb::Status status = db_->Write(write_options, &batch); - if (status.IsCorruption()) - RecreateDB(); - - return LevelDbToDRPStoreStatus(status); -} - -DataStore::Status DataStoreImpl::Delete(base::StringPiece key) { - DCHECK(sequence_checker_.CalledOnValidSequence()); - - if (!db_) - return MISC_ERROR; - - leveldb::Slice slice(key.data(), key.size()); - leveldb::WriteOptions write_options; - leveldb::Status status = db_->Delete(write_options, slice); - if (status.IsCorruption()) - RecreateDB(); - - return LevelDbToDRPStoreStatus(status); -} - -DataStore::Status DataStoreImpl::OpenDB() { - DCHECK(sequence_checker_.CalledOnValidSequence()); - - leveldb_env::Options options; - options.create_if_missing = true; - options.paranoid_checks = true; - // Deletes to buckets not found are stored in the log. Use a new log so that - // these log entries are deleted. - options.reuse_logs = false; - std::string db_name = profile_path_.Append(kDBName).AsUTF8Unsafe(); - db_.reset(); - Status status = - LevelDbToDRPStoreStatus(leveldb_env::OpenDB(options, db_name, &db_)); - UMA_HISTOGRAM_ENUMERATION("DataReductionProxy.LevelDBOpenStatus", status, - STATUS_MAX); - - if (status != OK) - LOG(ERROR) << "Failed to open Data Reduction Proxy DB: " << status; - - if (db_) { - leveldb::Range range; - uint64_t size; - // We try to capture the size of the entire DB by using the highest and - // lowest keys. - range.start = ""; - range.limit = "z"; // Keys starting with 'z' will not be included. - db_->GetApproximateSizes(&range, 1, &size); - UMA_HISTOGRAM_MEMORY_KB("DataReductionProxy.LevelDBSize", size / 1024); - } - - return status; -} - -DataStore::Status DataStoreImpl::RecreateDB() { - DCHECK(sequence_checker_.CalledOnValidSequence()); - - db_.reset(); - const base::FilePath db_path = profile_path_.Append(kDBName); - leveldb::Status s = leveldb_chrome::DeleteDB(db_path, leveldb::Options()); - if (!s.ok()) - return LevelDbToDRPStoreStatus(s); - - return OpenDB(); -} - -} // namespace data_reduction_proxy diff --git a/chromium/components/data_reduction_proxy/core/browser/data_store_impl.h b/chromium/components/data_reduction_proxy/core/browser/data_store_impl.h deleted file mode 100644 index fc5f253c311..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_store_impl.h +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_STORE_IMPL_H_ -#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_STORE_IMPL_H_ - -#include <map> -#include <memory> -#include <string> - -#include "base/files/file_path.h" -#include "base/sequence_checker.h" -#include "base/strings/string_piece.h" -#include "components/data_reduction_proxy/core/browser/data_store.h" - -namespace leveldb { -class DB; -} - -namespace data_reduction_proxy { - -// Implementation of |DataStore| using LevelDB. -class DataStoreImpl : public DataStore { - public: - explicit DataStoreImpl(const base::FilePath& profile_path); - - DataStoreImpl(const DataStoreImpl&) = delete; - DataStoreImpl& operator=(const DataStoreImpl&) = delete; - - ~DataStoreImpl() override; - - // Overrides of DataStore. - void InitializeOnDBThread() override; - - Status Get(base::StringPiece key, std::string* value) override; - - Status Put(const std::map<std::string, std::string>& map) override; - - Status Delete(base::StringPiece key) override; - - // Deletes the LevelDB and recreates it. This method is called if any DB call - // returns a |CORRUPTED| status or the database is cleared. - Status RecreateDB() override; - - private: - // Opens the underlying LevelDB for read and write. - Status OpenDB(); - - // The underlying LevelDB used by this implementation. - std::unique_ptr<leveldb::DB> db_; - - // Path to the profile using this store. - const base::FilePath profile_path_; - - base::SequenceChecker sequence_checker_; -}; - -} // namespace data_reduction_proxy - -#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_STORE_IMPL_H_ diff --git a/chromium/components/data_reduction_proxy/core/browser/data_usage_store.cc b/chromium/components/data_reduction_proxy/core/browser/data_usage_store.cc deleted file mode 100644 index e0a2844e01f..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_usage_store.cc +++ /dev/null @@ -1,302 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Each |DataUsageBucket| corresponds to data usage for an interval of -// |kDataUsageBucketLengthMins| minutes. We store data usage for the past -// |kNumDataUsageBuckets| buckets. Buckets are maintained as a circular array -// with indexes from 0 to (kNumDataUsageBuckets - 1). To store the circular -// array in a key-value store, we convert each index to a unique key. The latest -// bucket persisted to DB overwrites the oldest. - -#include "components/data_reduction_proxy/core/browser/data_usage_store.h" - -#include <algorithm> -#include <string> -#include <utility> - -#include "base/logging.h" -#include "base/metrics/histogram_macros.h" -#include "base/strings/string_number_conversions.h" -#include "base/strings/stringprintf.h" -#include "base/time/time.h" -#include "components/data_reduction_proxy/proto/data_store.pb.h" - -namespace data_reduction_proxy { - -namespace { - -const char kCurrentBucketIndexKey[] = "current_bucket_index"; -const char kBucketKeyPrefix[] = "data_usage_bucket:"; - -const int kMinutesInHour = 60; -const int kMinutesInDay = 24 * kMinutesInHour; - -static_assert(data_reduction_proxy::kDataUsageBucketLengthInMinutes > 0, - "Length of time should be positive"); -static_assert(kMinutesInHour % - data_reduction_proxy::kDataUsageBucketLengthInMinutes == - 0, - "kDataUsageBucketLengthMins must be a factor of kMinsInHour"); - -// Total number of buckets persisted to DB. -const int kNumDataUsageBuckets = - kDataUsageHistoryNumDays * kMinutesInDay / kDataUsageBucketLengthInMinutes; - -std::string DbKeyForBucketIndex(int index) { - DCHECK_GE(index, 0); - DCHECK_LT(index, kNumDataUsageBuckets); - - return base::StringPrintf("%s%d", kBucketKeyPrefix, index); -} - -base::Time BucketLowerBoundary(base::Time time) { - base::Time::Exploded exploded; - time.UTCExplode(&exploded); - exploded.minute -= exploded.minute % kDataUsageBucketLengthInMinutes; - exploded.second = 0; - exploded.millisecond = 0; - - base::Time out_time; - bool conversion_success = base::Time::FromUTCExploded(exploded, &out_time); - DCHECK(conversion_success); - return out_time; -} - -} // namespace - -DataUsageStore::DataUsageStore(DataStore* db) - : db_(db), current_bucket_index_(-1) { - sequence_checker_.DetachFromSequence(); -} - -DataUsageStore::~DataUsageStore() { - DCHECK(sequence_checker_.CalledOnValidSequence()); -} - -void DataUsageStore::LoadDataUsage(std::vector<DataUsageBucket>* data_usage) { - SCOPED_UMA_HISTOGRAM_TIMER("DataReductionProxy.HistoricalDataUsageLoadTime"); - - DCHECK(data_usage); - - DataUsageBucket empty_bucket; - data_usage->clear(); - data_usage->resize(kNumDataUsageBuckets, empty_bucket); - - for (int i = 0; i < kNumDataUsageBuckets; ++i) { - int circular_array_index = - (i + current_bucket_index_ + 1) % kNumDataUsageBuckets; - LoadBucketAtIndex(circular_array_index, &data_usage->at(i)); - } -} - -void DataUsageStore::LoadCurrentDataUsageBucket(DataUsageBucket* current) { - DCHECK(sequence_checker_.CalledOnValidSequence()); - DCHECK(current); - - std::string current_index_string; - DataStore::Status index_read_status = - db_->Get(kCurrentBucketIndexKey, ¤t_index_string); - - if (index_read_status != DataStore::Status::OK || - !base::StringToInt(current_index_string, ¤t_bucket_index_)) { - current_bucket_index_ = 0; - } - - DCHECK_GE(current_bucket_index_, 0); - DCHECK_LT(current_bucket_index_, kNumDataUsageBuckets); - - DataStore::Status status = LoadBucketAtIndex(current_bucket_index_, current); - bool bucket_read_ok = status == DataStore::Status::OK; - current->set_had_read_error(!bucket_read_ok); - if (bucket_read_ok) { - current_bucket_last_updated_ = - base::Time::FromInternalValue(current->last_updated_timestamp()); - } -} - -void DataUsageStore::StoreCurrentDataUsageBucket( - const DataUsageBucket& current) { - DCHECK(sequence_checker_.CalledOnValidSequence()); - DCHECK(current_bucket_index_ >= 0 && - current_bucket_index_ < kNumDataUsageBuckets); - - // If current bucket does not have any information, we skip writing to DB. - if (!current.has_last_updated_timestamp() || - (current.has_had_read_error() && current.had_read_error())) - return; - - int prev_current_bucket_index = current_bucket_index_; - base::Time prev_current_bucket_last_updated_ = current_bucket_last_updated_; - // We might have skipped saving buckets because Chrome was not being used. - // Write empty buckets to those slots to overwrite outdated information. - base::Time last_updated = - base::Time::FromInternalValue(current.last_updated_timestamp()); - std::map<std::string, std::string> buckets_to_save; - int num_buckets_since_last_saved = BucketOffsetFromLastSaved(last_updated); - for (int i = 0; i < num_buckets_since_last_saved - 1; ++i) - GenerateKeyAndAddToMap(DataUsageBucket(), &buckets_to_save, true); - - GenerateKeyAndAddToMap(current, &buckets_to_save, - num_buckets_since_last_saved > 0); - - current_bucket_last_updated_ = - base::Time::FromInternalValue(current.last_updated_timestamp()); - - buckets_to_save.insert(std::pair<std::string, std::string>( - kCurrentBucketIndexKey, base::NumberToString(current_bucket_index_))); - - DataStore::Status status = db_->Put(buckets_to_save); - if (status != DataStore::Status::OK) { - current_bucket_index_ = prev_current_bucket_index; - current_bucket_last_updated_ = prev_current_bucket_last_updated_; - LOG(WARNING) << "Failed to write data usage buckets to LevelDB" << status; - } -} - -void DataUsageStore::DeleteHistoricalDataUsage() { - std::string current_index_string; - DataStore::Status index_read_status = - db_->Get(kCurrentBucketIndexKey, ¤t_index_string); - - // If the index doesn't exist, then no buckets have been written and the - // data usage doesn't need to be deleted. - if (index_read_status != DataStore::Status::OK) - return; - - db_->RecreateDB(); -} - -void DataUsageStore::DeleteBrowsingHistory(const base::Time& start, - const base::Time& end) { - DCHECK_LE(start, end); - if (current_bucket_last_updated_.is_null()) - return; - - base::Time begin_current_interval = - BucketLowerBoundary(current_bucket_last_updated_); - // Data usage is stored for the past |kDataUsageHistoryNumDays| days. Compute - // the begin time for data usage. - base::Time begin_history = begin_current_interval - - base::Days(kDataUsageHistoryNumDays) + - base::Minutes(kDataUsageBucketLengthInMinutes); - - // Nothing to do if there is no overlap between given interval and the - // interval for which data usage history is maintained. - if (begin_history > end || start > current_bucket_last_updated_) - return; - - base::Time start_delete = start > begin_history ? start : begin_history; - base::Time end_delete = - end < current_bucket_last_updated_ ? end : current_bucket_last_updated_; - - int first_index_to_delete = ComputeBucketIndex(start_delete); - int num_buckets_to_delete = - 1 + - (BucketLowerBoundary(end_delete) - BucketLowerBoundary(start_delete)) - .InMinutes() / - kDataUsageBucketLengthInMinutes; - for (int i = 0; i < num_buckets_to_delete; ++i) { - int index_to_delete = (first_index_to_delete + i) % kNumDataUsageBuckets; - db_->Delete(DbKeyForBucketIndex(index_to_delete)); - } - UMA_HISTOGRAM_COUNTS_10000( - "DataReductionProxy.DeleteBrowsingHistory.NumBuckets", - num_buckets_to_delete); -} - -int DataUsageStore::ComputeBucketIndex(const base::Time& time) const { - int offset = BucketOffsetFromLastSaved(time); - - int index = current_bucket_index_ + offset; - if (index < 0) { - index += kNumDataUsageBuckets; - } else if (index >= kNumDataUsageBuckets) { - index -= kNumDataUsageBuckets; - } - DCHECK_GE(index, 0); - DCHECK_LT(index, kNumDataUsageBuckets); - return index; -} - -// static -bool DataUsageStore::AreInSameInterval(const base::Time& time1, - const base::Time& time2) { - if (time1.is_null() || time2.is_null()) - return true; - - return BucketLowerBoundary(time1) == BucketLowerBoundary(time2); -} - -// static -bool DataUsageStore::BucketOverlapsInterval( - const base::Time& bucket_last_updated, - const base::Time& start_interval, - const base::Time& end_interval) { - DCHECK(!bucket_last_updated.is_null()); - DCHECK(!end_interval.is_null()); - DCHECK_LE(start_interval, end_interval); - - base::Time bucket_start = BucketLowerBoundary(bucket_last_updated); - base::Time bucket_end = - bucket_start + base::Minutes(kDataUsageBucketLengthInMinutes); - DCHECK_LE(bucket_start, bucket_end); - return bucket_end >= start_interval && end_interval >= bucket_start; -} - -void DataUsageStore::GenerateKeyAndAddToMap( - const DataUsageBucket& bucket, - std::map<std::string, std::string>* map, - bool increment_current_index) { - if (increment_current_index) { - current_bucket_index_++; - DCHECK(current_bucket_index_ <= kNumDataUsageBuckets); - if (current_bucket_index_ == kNumDataUsageBuckets) - current_bucket_index_ = 0; - } - - std::string bucket_key = DbKeyForBucketIndex(current_bucket_index_); - - std::string bucket_value; - bool success = bucket.SerializeToString(&bucket_value); - DCHECK(success); - - map->insert(std::pair<std::string, std::string>(std::move(bucket_key), - std::move(bucket_value))); -} - -int DataUsageStore::BucketOffsetFromLastSaved( - const base::Time& new_last_updated_timestamp) const { - if (current_bucket_last_updated_.is_null()) - return 0; - - base::TimeDelta time_delta = - BucketLowerBoundary(new_last_updated_timestamp) - - BucketLowerBoundary(current_bucket_last_updated_); - int offset_from_last_saved = - (time_delta.InMinutes() / kDataUsageBucketLengthInMinutes); - return offset_from_last_saved > 0 - ? std::min(offset_from_last_saved, kNumDataUsageBuckets) - : std::max(offset_from_last_saved, -kNumDataUsageBuckets); -} - -DataStore::Status DataUsageStore::LoadBucketAtIndex(int index, - DataUsageBucket* bucket) { - DCHECK(index >= 0 && index < kNumDataUsageBuckets); - - std::string bucket_as_string; - DataStore::Status bucket_read_status = - db_->Get(DbKeyForBucketIndex(index), &bucket_as_string); - if ((bucket_read_status != DataStore::Status::OK && - bucket_read_status != DataStore::NOT_FOUND)) { - LOG(WARNING) << "Failed to read data usage bucket from LevelDB: " - << bucket_read_status; - } - if (bucket_read_status == DataStore::Status::OK) { - bool parse_successful = bucket->ParseFromString(bucket_as_string); - DCHECK(parse_successful); - } - return bucket_read_status; -} - -} // namespace data_reduction_proxy diff --git a/chromium/components/data_reduction_proxy/core/browser/data_usage_store.h b/chromium/components/data_reduction_proxy/core/browser/data_usage_store.h deleted file mode 100644 index 19da8b94298..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_usage_store.h +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_USAGE_STORE_H_ -#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_USAGE_STORE_H_ - -#include <map> -#include <string> -#include <vector> - -#include "base/memory/raw_ptr.h" -#include "base/sequence_checker.h" -#include "base/time/time.h" -#include "components/data_reduction_proxy/core/browser/data_store.h" - -namespace data_reduction_proxy { -class DataStore; -class DataUsageBucket; - -// Time interval for each DataUsageBucket. -constexpr int kDataUsageBucketLengthInMinutes = 15; - -// Number of days for which to maintain data usage history. -constexpr int kDataUsageHistoryNumDays = 60; - -// Store for detailed data usage stats. Data usage from every -// |kDataUsageBucketLengthMins| interval is stored in a DataUsageBucket. -class DataUsageStore { - public: - explicit DataUsageStore(DataStore* db); - - DataUsageStore(const DataUsageStore&) = delete; - DataUsageStore& operator=(const DataUsageStore&) = delete; - - ~DataUsageStore(); - - // Loads the historic data usage into |data_usage|. - void LoadDataUsage(std::vector<DataUsageBucket>* data_usage); - - // Loads the data usage bucket for the current interval into |current_bucket|. - // This method must be called at least once before any calls to - // |StoreCurrentDataUsageBucket|. - void LoadCurrentDataUsageBucket(DataUsageBucket* bucket); - - // Stores the data usage bucket for the current interval. This will overwrite - // the current data usage bucket in the |db_| if they are for the same - // interval. It will also backfill any missed intervals with empty data. - // Intervals might be missed because Chrome was not running, or there was no - // network activity during an interval. - void StoreCurrentDataUsageBucket(const DataUsageBucket& current_bucket); - - // Deletes all historical data usage from storage. - void DeleteHistoricalDataUsage(); - - // Deletes historical data usage from storage. - void DeleteBrowsingHistory(const base::Time& start, const base::Time& end); - - // Returns whether |time1| and |time2| are in the same interval. Each hour is - // divided into |kDataUsageBucketLengthMins| minute long intervals. Returns - // true if either |time1| or |time2| has NULL time since an uninitialized - // bucket can be assigned to any interval. - static bool AreInSameInterval(const base::Time& time1, - const base::Time& time2); - - // Returns whether the bucket that was last updated at |bucket_last_updated| - // overlaps in time with the interval [|start_interval|, |end_interval|]. - static bool BucketOverlapsInterval(const base::Time& bucket_last_updated, - const base::Time& start_interval, - const base::Time& end_interval); - - private: - friend class DataUsageStoreTest; - - // Converts the given |bucket| into a string format for persistance to - // |DataReductionProxyStore| and adds it to the map. The key is generated - // based on |current_bucket_index_|. - // |current_bucket_index_| will be incremented before generating the key if - // |increment_current_index| is true. - void GenerateKeyAndAddToMap(const DataUsageBucket& bucket, - std::map<std::string, std::string>* map, - bool increment_current_index); - - // Returns the offset between the bucket for |current| time and the last - // bucket that was persisted to the store. Eg: Returns 0 if |current| is in - // the last persisted bucket. Returns 1 if |current| belongs to the bucket - // immediately after the last persisted bucket. - int BucketOffsetFromLastSaved(const base::Time& current) const; - - // Computes the index of the bucket for the given |time| relative to - // |current_bucket_index_| and |current_bucket_last_updated_|. - // |current_bucket_last_updated_| belongs at |current_bucket_index_| and - // bucket index is computed by going backwards or forwards from current index - // by one position for every |kDataUsageBucketLengthInMinutes| minutes. - int ComputeBucketIndex(const base::Time& time) const; - - // Loads the data usage bucket at the given index. - DataStore::Status LoadBucketAtIndex(int index, DataUsageBucket* current); - - // The store to persist data usage information. - raw_ptr<DataStore> db_; - - // The index of the last bucket persisted in the |db_|. |DataUsageBucket| is - // stored in the |db_| as a circular array. This index points to the array - // position corresponding to the current bucket. - int current_bucket_index_; - - // The time when the current bucket was last written to |db_|. This field is - // used to determine if a DataUsageBucket to be saved belongs to the same - // interval, or a more recent interval. - base::Time current_bucket_last_updated_; - - base::SequenceChecker sequence_checker_; -}; - -} // namespace data_reduction_proxy -#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_USAGE_STORE_H_ diff --git a/chromium/components/data_reduction_proxy/core/browser/data_usage_store_unittest.cc b/chromium/components/data_reduction_proxy/core/browser/data_usage_store_unittest.cc deleted file mode 100644 index 508a57ae214..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/data_usage_store_unittest.cc +++ /dev/null @@ -1,518 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/data_reduction_proxy/core/browser/data_usage_store.h" - -#include <stdint.h> - -#include <map> -#include <memory> -#include <string> - -#include "base/strings/stringprintf.h" -#include "base/test/metrics/histogram_tester.h" -#include "base/time/time.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h" -#include "components/data_reduction_proxy/core/browser/data_store.h" -#include "components/data_reduction_proxy/proto/data_store.pb.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace { -// Each bucket holds data usage for a 15 minute interval. History is maintained -// for 60 days. -const unsigned kNumExpectedBuckets = 60 * 24 * 60 / 15; -const int kBucketsInHour = 60 / 15; -const int kTestCurrentBucketIndex = 2880; - -base::Time::Exploded TestExplodedTime() { - base::Time::Exploded exploded; - exploded.year = 2001; - exploded.month = 12; - exploded.day_of_month = 31; - exploded.day_of_week = 1; - exploded.hour = 12; - exploded.minute = 1; - exploded.second = 0; - exploded.millisecond = 0; - - return exploded; -} -} - -namespace data_reduction_proxy { - -class DataUsageStoreTest : public testing::Test { - protected: - void SetUp() override { - store_ = std::make_unique<TestDataStore>(); - data_usage_store_ = std::make_unique<DataUsageStore>(store_.get()); - } - - int current_bucket_index() const { - return data_usage_store_->current_bucket_index_; - } - - int64_t current_bucket_last_updated() const { - return data_usage_store_->current_bucket_last_updated_.ToInternalValue(); - } - - int ComputeBucketIndex(const base::Time& time) const { - return data_usage_store_->ComputeBucketIndex(time); - } - - TestDataStore* store() const { return store_.get(); } - - DataUsageStore* data_usage_store() const { return data_usage_store_.get(); } - - void PopulateStore() { - base::Time::Exploded exploded = TestExplodedTime(); - base::Time current_time; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, ¤t_time)); - - std::map<std::string, std::string> map; - map.insert( - std::pair<std::string, std::string>("current_bucket_index", "2880")); - for (int i = 0; i < static_cast<int>(kNumExpectedBuckets); ++i) { - base::Time time = current_time - base::Minutes(i * 5); - DataUsageBucket bucket; - bucket.set_last_updated_timestamp(time.ToInternalValue()); - bucket.set_had_read_error(false); - int index = kTestCurrentBucketIndex - i; - if (index < 0) - index += kNumExpectedBuckets; - - std::string bucket_value; - ASSERT_TRUE(bucket.SerializeToString(&bucket_value)); - map.insert(std::pair<std::string, std::string>( - base::StringPrintf("data_usage_bucket:%d", index), bucket_value)); - } - store()->Put(map); - } - - private: - std::unique_ptr<TestDataStore> store_; - std::unique_ptr<DataUsageStore> data_usage_store_; -}; - -TEST_F(DataUsageStoreTest, LoadEmpty) { - ASSERT_NE(0, current_bucket_index()); - - DataUsageBucket bucket; - data_usage_store()->LoadCurrentDataUsageBucket(&bucket); - - ASSERT_EQ(0, current_bucket_index()); - ASSERT_EQ(0, current_bucket_last_updated()); - ASSERT_FALSE(bucket.has_last_updated_timestamp()); - - std::vector<DataUsageBucket> data_usage; - data_usage_store()->LoadDataUsage(&data_usage); - - ASSERT_EQ(kNumExpectedBuckets, data_usage.size()); - ASSERT_FALSE(data_usage[0].has_last_updated_timestamp()); -} - -TEST_F(DataUsageStoreTest, LoadAndStoreToSameBucket) { - DataUsageBucket bucket; - data_usage_store()->LoadCurrentDataUsageBucket(&bucket); - - base::Time now = base::Time::Now(); - bucket.set_last_updated_timestamp(now.ToInternalValue()); - bucket.set_had_read_error(false); - - data_usage_store()->StoreCurrentDataUsageBucket(bucket); - ASSERT_EQ(2u, store()->map()->size()); - - DataUsageBucket stored_bucket; - data_usage_store()->LoadCurrentDataUsageBucket(&stored_bucket); - ASSERT_EQ(now.ToInternalValue(), stored_bucket.last_updated_timestamp()); - ASSERT_FALSE(stored_bucket.had_read_error()); - - std::vector<DataUsageBucket> data_usage; - data_usage_store()->LoadDataUsage(&data_usage); - - ASSERT_EQ(kNumExpectedBuckets, data_usage.size()); - ASSERT_FALSE(data_usage[0].has_last_updated_timestamp()); - ASSERT_FALSE( - data_usage[kNumExpectedBuckets - 2].has_last_updated_timestamp()); - ASSERT_EQ(now.ToInternalValue(), - data_usage[kNumExpectedBuckets - 1].last_updated_timestamp()); - ASSERT_FALSE(data_usage[kNumExpectedBuckets - 1].had_read_error()); -} - -TEST_F(DataUsageStoreTest, StoreSameBucket) { - base::Time::Exploded exploded = TestExplodedTime(); - - exploded.minute = 0; - exploded.second = 0; - exploded.millisecond = 0; - base::Time time1; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &time1)); - - exploded.minute = 14; - exploded.second = 59; - exploded.millisecond = 999; - base::Time time2; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &time2)); - - DataUsageBucket bucket; - data_usage_store()->LoadCurrentDataUsageBucket(&bucket); - - bucket.set_last_updated_timestamp(time1.ToInternalValue()); - bucket.set_had_read_error(false); - - data_usage_store()->StoreCurrentDataUsageBucket(bucket); - ASSERT_EQ(2u, store()->map()->size()); - - bucket.set_last_updated_timestamp(time2.ToInternalValue()); - data_usage_store()->StoreCurrentDataUsageBucket(bucket); - ASSERT_EQ(2u, store()->map()->size()); - - std::vector<DataUsageBucket> data_usage; - data_usage_store()->LoadDataUsage(&data_usage); - - ASSERT_EQ(kNumExpectedBuckets, data_usage.size()); - ASSERT_FALSE(data_usage[0].has_last_updated_timestamp()); - ASSERT_FALSE( - data_usage[kNumExpectedBuckets - 2].has_last_updated_timestamp()); - ASSERT_EQ(time2.ToInternalValue(), - data_usage[kNumExpectedBuckets - 1].last_updated_timestamp()); - ASSERT_FALSE(data_usage[kNumExpectedBuckets - 1].had_read_error()); -} - -TEST_F(DataUsageStoreTest, StoreConsecutiveBuckets) { - base::Time::Exploded exploded = TestExplodedTime(); - - exploded.minute = 0; - exploded.second = 59; - exploded.millisecond = 999; - base::Time time1; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &time1)); - - exploded.minute = 15; - exploded.second = 0; - exploded.millisecond = 0; - base::Time time2; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &time2)); - - DataUsageBucket bucket; - data_usage_store()->LoadCurrentDataUsageBucket(&bucket); - - bucket.set_last_updated_timestamp(time1.ToInternalValue()); - bucket.set_had_read_error(false); - - data_usage_store()->StoreCurrentDataUsageBucket(bucket); - ASSERT_EQ(2u, store()->map()->size()); - - bucket.set_last_updated_timestamp(time2.ToInternalValue()); - data_usage_store()->StoreCurrentDataUsageBucket(bucket); - ASSERT_EQ(3u, store()->map()->size()); - - std::vector<DataUsageBucket> data_usage; - data_usage_store()->LoadDataUsage(&data_usage); - - ASSERT_EQ(kNumExpectedBuckets, data_usage.size()); - ASSERT_FALSE(data_usage[0].has_last_updated_timestamp()); - ASSERT_FALSE( - data_usage[kNumExpectedBuckets - 3].has_last_updated_timestamp()); - ASSERT_EQ(time1.ToInternalValue(), - data_usage[kNumExpectedBuckets - 2].last_updated_timestamp()); - ASSERT_FALSE(data_usage[kNumExpectedBuckets - 2].had_read_error()); - ASSERT_EQ(time2.ToInternalValue(), - data_usage[kNumExpectedBuckets - 1].last_updated_timestamp()); - ASSERT_FALSE(data_usage[kNumExpectedBuckets - 1].had_read_error()); -} - -TEST_F(DataUsageStoreTest, StoreMultipleBuckets) { - DataUsageBucket bucket; - data_usage_store()->LoadCurrentDataUsageBucket(&bucket); - - // Comments indicate time expressed as day.hour.min.sec.millis relative to - // each other beginning at 0.0.0.0.0. - // The first bucket range is 0.0.0.0.0 - 0.0.14.59.999 and - // the second bucket range is 0.0.5.0.0 - 0.0.29.59.999, etc. - base::Time first_bucket_time = base::Time::Now(); // 0.0.0.0.0. - base::Time last_bucket_time = first_bucket_time // 59.23.55.0.0 - + base::Days(60) - base::Minutes(15); - base::Time before_history_time = // 0.0.-5.0.0 - first_bucket_time - base::Minutes(15); - base::Time tenth_bucket_time = // 0.2.15.0.0 - first_bucket_time + base::Hours(2) + base::Minutes(15); - base::Time second_last_bucket_time = // 59.23.45.0.0 - last_bucket_time - base::Minutes(15); - - // This bucket will be discarded when the |last_bucket| is stored. - DataUsageBucket bucket_before_history; - bucket_before_history.set_last_updated_timestamp( - before_history_time.ToInternalValue()); - bucket_before_history.set_had_read_error(false); - data_usage_store()->StoreCurrentDataUsageBucket(bucket_before_history); - // Only one bucket has been stored, so store should have 2 entries, one for - // current index and one for the bucket itself. - ASSERT_EQ(2u, store()->map()->size()); - - // Calling |StoreCurrentDataUsageBucket| with the same last updated timestamp - // should not cause number of stored buckets to increase and current bucket - // should be overwritten. - data_usage_store()->StoreCurrentDataUsageBucket(bucket_before_history); - // Only one bucket has been stored, so store should have 2 entries, one for - // current index and one for the bucket itself. - ASSERT_EQ(2u, store()->map()->size()); - - // This will be the very first bucket once |last_bucket| is stored. - DataUsageBucket first_bucket; - first_bucket.set_last_updated_timestamp(first_bucket_time.ToInternalValue()); - first_bucket.set_had_read_error(false); - data_usage_store()->StoreCurrentDataUsageBucket(first_bucket); - // A new bucket has been stored, so entires in map should increase by one. - ASSERT_EQ(3u, store()->map()->size()); - - // This will be the 10th bucket when |last_bucket| is stored. We skipped - // calling |StoreCurrentDataUsageBucket| on 10 buckets. - DataUsageBucket tenth_bucket; - tenth_bucket.set_last_updated_timestamp(tenth_bucket_time.ToInternalValue()); - tenth_bucket.set_had_read_error(false); - data_usage_store()->StoreCurrentDataUsageBucket(tenth_bucket); - // 9 more (empty) buckets should have been added to the store. - ASSERT_EQ(12u, store()->map()->size()); - - // This will be the last but one bucket when |last_bucket| is stored. - DataUsageBucket second_last_bucket; - second_last_bucket.set_last_updated_timestamp( - second_last_bucket_time.ToInternalValue()); - second_last_bucket.set_had_read_error(false); - data_usage_store()->StoreCurrentDataUsageBucket(second_last_bucket); - // Max number of buckets we store to DB plus one for the current index. - ASSERT_EQ(kNumExpectedBuckets + 1, store()->map()->size()); - - // This bucket should simply overwrite oldest bucket, so number of entries in - // store should be unchanged. - DataUsageBucket last_bucket; - last_bucket.set_last_updated_timestamp(last_bucket_time.ToInternalValue()); - last_bucket.set_had_read_error(false); - data_usage_store()->StoreCurrentDataUsageBucket(last_bucket); - ASSERT_EQ(kNumExpectedBuckets + 1, store()->map()->size()); - - std::vector<DataUsageBucket> data_usage; - data_usage_store()->LoadDataUsage(&data_usage); - - ASSERT_EQ(kNumExpectedBuckets, data_usage.size()); - ASSERT_EQ(first_bucket_time.ToInternalValue(), - data_usage[0].last_updated_timestamp()); - ASSERT_EQ(tenth_bucket_time.ToInternalValue(), - data_usage[9].last_updated_timestamp()); - ASSERT_EQ(second_last_bucket_time.ToInternalValue(), - data_usage[kNumExpectedBuckets - 2].last_updated_timestamp()); - ASSERT_EQ(last_bucket_time.ToInternalValue(), - data_usage[kNumExpectedBuckets - 1].last_updated_timestamp()); - - ASSERT_FALSE(data_usage[0].had_read_error()); - ASSERT_FALSE(data_usage[9].had_read_error()); - ASSERT_FALSE(data_usage[kNumExpectedBuckets - 2].had_read_error()); - ASSERT_FALSE(data_usage[kNumExpectedBuckets - 1].had_read_error()); -} - -TEST_F(DataUsageStoreTest, DeleteHistoricalDataUsage) { - DataUsageBucket bucket; - data_usage_store()->LoadCurrentDataUsageBucket(&bucket); - - bucket.set_last_updated_timestamp(base::Time::Now().ToInternalValue()); - bucket.set_had_read_error(false); - - data_usage_store()->StoreCurrentDataUsageBucket(bucket); - ASSERT_EQ(2u, store()->map()->size()); - - data_usage_store()->DeleteHistoricalDataUsage(); - ASSERT_EQ(0u, store()->map()->size()); -} - -TEST_F(DataUsageStoreTest, BucketOverlapsInterval) { - base::Time::Exploded exploded = TestExplodedTime(); - base::Time time1; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &time1)); - - exploded.minute = 16; - base::Time time16; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &time16)); - - exploded.minute = 17; - base::Time time17; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &time17)); - - exploded.minute = 18; - base::Time time18; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &time18)); - - exploded.minute = 33; - base::Time time33; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &time33)); - - exploded.minute = 34; - base::Time time34; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &time34)); - - exploded.minute = 46; - base::Time time46; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &time46)); - - ASSERT_FALSE(DataUsageStore::BucketOverlapsInterval(time1, time17, time33)); - ASSERT_TRUE(DataUsageStore::BucketOverlapsInterval(time16, time17, time33)); - ASSERT_TRUE(DataUsageStore::BucketOverlapsInterval(time18, time17, time33)); - ASSERT_TRUE(DataUsageStore::BucketOverlapsInterval(time34, time17, time33)); - ASSERT_FALSE(DataUsageStore::BucketOverlapsInterval(time46, time17, time33)); -} - -TEST_F(DataUsageStoreTest, ComputeBucketIndex) { - PopulateStore(); - - base::Time::Exploded exploded = TestExplodedTime(); - - DataUsageBucket current_bucket; - data_usage_store()->LoadCurrentDataUsageBucket(¤t_bucket); - - base::Time out_time; - - exploded.minute = 0; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &out_time)); - ASSERT_EQ(kTestCurrentBucketIndex, ComputeBucketIndex(out_time)); - - exploded.minute = 14; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &out_time)); - ASSERT_EQ(kTestCurrentBucketIndex, ComputeBucketIndex(out_time)); - - exploded.minute = 15; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &out_time)); - ASSERT_EQ(kTestCurrentBucketIndex + 1, ComputeBucketIndex(out_time)); - - exploded.hour = 11; - exploded.minute = 59; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &out_time)); - ASSERT_EQ(kTestCurrentBucketIndex - 1, ComputeBucketIndex(out_time)); - - exploded.minute = 0; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &out_time)); - ASSERT_EQ(kTestCurrentBucketIndex - kBucketsInHour, - ComputeBucketIndex(out_time)); - - exploded.hour = 1; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &out_time)); - ASSERT_EQ(kTestCurrentBucketIndex - kBucketsInHour * 11, - ComputeBucketIndex(out_time)); - - exploded.day_of_month = 1; - exploded.hour = 12; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &out_time)); - ASSERT_EQ(kTestCurrentBucketIndex - kBucketsInHour * 30 * 24, - ComputeBucketIndex(out_time)); - - exploded.hour = 11; - exploded.minute = 45; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &out_time)); - ASSERT_EQ(kTestCurrentBucketIndex - kBucketsInHour * 30 * 24 - 1 + - static_cast<int>(kNumExpectedBuckets), - ComputeBucketIndex(out_time)); - - exploded.minute = 30; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &out_time)); - ASSERT_EQ(kTestCurrentBucketIndex - kBucketsInHour * 30 * 24 - 2 + - static_cast<int>(kNumExpectedBuckets), - ComputeBucketIndex(out_time)); -} - -TEST_F(DataUsageStoreTest, DeleteBrowsingHistory) { - PopulateStore(); - DataUsageBucket current_bucket; - data_usage_store()->LoadCurrentDataUsageBucket(¤t_bucket); - base::HistogramTester histogram_tester; - - ASSERT_EQ(kNumExpectedBuckets + 1, store()->map()->size()); - - base::Time::Exploded exploded = TestExplodedTime(); - exploded.minute = 0; - base::Time now; - EXPECT_TRUE(base::Time::FromUTCExploded(exploded, &now)); - base::Time fifteen_mins_from_now = now + base::Minutes(15); - - // Deleting browsing from the future should be a no-op. - data_usage_store()->DeleteBrowsingHistory(fifteen_mins_from_now, - fifteen_mins_from_now); - ASSERT_EQ(kNumExpectedBuckets + 1, store()->map()->size()); - - ASSERT_TRUE(store()->map()->find(base::StringPrintf( - "data_usage_bucket:%d", kTestCurrentBucketIndex)) != - store()->map()->end()); - - // Delete the current bucket. - data_usage_store()->DeleteBrowsingHistory(now, now); - ASSERT_EQ(kNumExpectedBuckets, store()->map()->size()); - ASSERT_TRUE(store()->map()->find(base::StringPrintf( - "data_usage_bucket:%d", kTestCurrentBucketIndex)) == - store()->map()->end()); - histogram_tester.ExpectUniqueSample( - "DataReductionProxy.DeleteBrowsingHistory.NumBuckets", 1, 1); - - // Delete the current bucket + the last 5 minutes, so two buckets. - data_usage_store()->DeleteBrowsingHistory(now - base::Minutes(5), now); - ASSERT_EQ(kNumExpectedBuckets - 1, store()->map()->size()); - ASSERT_TRUE(store()->map()->find(base::StringPrintf( - "data_usage_bucket:%d", kTestCurrentBucketIndex - 1)) == - store()->map()->end()); - histogram_tester.ExpectBucketCount( - "DataReductionProxy.DeleteBrowsingHistory.NumBuckets", 2, 1); - - // Delete 30 days of browsing history. - data_usage_store()->DeleteBrowsingHistory(now - base::Days(30), now); - ASSERT_EQ(kNumExpectedBuckets - kBucketsInHour * 30 * 24, - store()->map()->size()); - ASSERT_TRUE(store()->map()->find("data_usage_bucket:0") == - store()->map()->end()); - ASSERT_TRUE(store()->map()->find(base::StringPrintf( - "data_usage_bucket:%d", kNumExpectedBuckets - 1)) != - store()->map()->end()); - histogram_tester.ExpectBucketCount( - "DataReductionProxy.DeleteBrowsingHistory.NumBuckets", - kBucketsInHour * 30 * 24, 1); - - // Delete wraps around and removes the last element which is at position - // (|kNumExpectedBuckets| - 1). - data_usage_store()->DeleteBrowsingHistory( - now - base::Days(30) - base::Minutes(5), now); - ASSERT_EQ(kNumExpectedBuckets - kBucketsInHour * 30 * 24 - 1, - store()->map()->size()); - ASSERT_TRUE(store()->map()->find(base::StringPrintf( - "data_usage_bucket:%d", kNumExpectedBuckets - 1)) == - store()->map()->end()); - ASSERT_TRUE(store()->map()->find(base::StringPrintf( - "data_usage_bucket:%d", kNumExpectedBuckets - 2)) != - store()->map()->end()); - - data_usage_store()->DeleteBrowsingHistory(now - base::Days(60), now); - ASSERT_EQ(1u, store()->map()->size()); - histogram_tester.ExpectBucketCount( - "DataReductionProxy.DeleteBrowsingHistory.NumBuckets", - kBucketsInHour * 30 * 24 - 1, 2); -} - -TEST_F(DataUsageStoreTest, DontStoreReadError) { - DataUsageBucket bucket; - data_usage_store()->LoadCurrentDataUsageBucket(&bucket); - - base::Time now = base::Time::Now(); - bucket.set_last_updated_timestamp(now.ToInternalValue()); - bucket.set_had_read_error(true); - - data_usage_store()->StoreCurrentDataUsageBucket(bucket); - ASSERT_EQ(0u, store()->map()->size()); -} - -TEST_F(DataUsageStoreTest, DontStoreNoTimestamp) { - DataUsageBucket bucket; - data_usage_store()->LoadCurrentDataUsageBucket(&bucket); - - bucket.set_had_read_error(false); - - data_usage_store()->StoreCurrentDataUsageBucket(bucket); - ASSERT_EQ(0u, store()->map()->size()); -} - -} // namespace data_reduction_proxy diff --git a/chromium/components/data_reduction_proxy/core/browser/db_data_owner.cc b/chromium/components/data_reduction_proxy/core/browser/db_data_owner.cc deleted file mode 100644 index 49e42fb1253..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/db_data_owner.cc +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/data_reduction_proxy/core/browser/db_data_owner.h" - -#include <utility> - -#include "base/check.h" -#include "components/data_reduction_proxy/core/browser/data_store.h" -#include "components/data_reduction_proxy/core/browser/data_usage_store.h" -#include "components/data_reduction_proxy/proto/data_store.pb.h" - -namespace data_reduction_proxy { - -DBDataOwner::DBDataOwner(std::unique_ptr<DataStore> store) - : store_(std::move(store)), data_usage_(new DataUsageStore(store_.get())) { - sequence_checker_.DetachFromSequence(); -} - -DBDataOwner::~DBDataOwner() { - DCHECK(sequence_checker_.CalledOnValidSequence()); -} - -void DBDataOwner::InitializeOnDBThread() { - DCHECK(sequence_checker_.CalledOnValidSequence()); - - store_->InitializeOnDBThread(); -} - -void DBDataOwner::LoadHistoricalDataUsage( - std::vector<DataUsageBucket>* data_usage) { - DCHECK(sequence_checker_.CalledOnValidSequence()); - - data_usage_->LoadDataUsage(data_usage); -} - -void DBDataOwner::LoadCurrentDataUsageBucket(DataUsageBucket* bucket) { - DCHECK(sequence_checker_.CalledOnValidSequence()); - - data_usage_->LoadCurrentDataUsageBucket(bucket); -} - -void DBDataOwner::StoreCurrentDataUsageBucket( - std::unique_ptr<DataUsageBucket> current) { - DCHECK(sequence_checker_.CalledOnValidSequence()); - - data_usage_->StoreCurrentDataUsageBucket(*current); -} - -void DBDataOwner::DeleteHistoricalDataUsage() { - DCHECK(sequence_checker_.CalledOnValidSequence()); - - data_usage_->DeleteHistoricalDataUsage(); -} - -void DBDataOwner::DeleteBrowsingHistory(const base::Time& start, - const base::Time& end) { - DCHECK(sequence_checker_.CalledOnValidSequence()); - - data_usage_->DeleteBrowsingHistory(start, end); -} - -base::WeakPtr<DBDataOwner> DBDataOwner::GetWeakPtr() { - return weak_factory_.GetWeakPtr(); -} - -} // namespace data_reduction_proxy diff --git a/chromium/components/data_reduction_proxy/core/browser/db_data_owner.h b/chromium/components/data_reduction_proxy/core/browser/db_data_owner.h deleted file mode 100644 index a21a3feebc5..00000000000 --- a/chromium/components/data_reduction_proxy/core/browser/db_data_owner.h +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DB_DATA_OWNER_H_ -#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DB_DATA_OWNER_H_ - -#include <memory> -#include <vector> - -#include "base/callback_forward.h" -#include "base/memory/weak_ptr.h" -#include "base/sequence_checker.h" - -namespace base { -class Time; -} - -namespace data_reduction_proxy { -class DataStore; -class DataUsageBucket; -class DataUsageStore; - -// Callback for loading the historical data usage. -using HistoricalDataUsageCallback = - base::OnceCallback<void(std::unique_ptr<std::vector<DataUsageBucket>>)>; - -// Callback for loading data usage for the current bucket. -using LoadCurrentDataUsageCallback = - base::OnceCallback<void(std::unique_ptr<DataUsageBucket>)>; - -// Contains and initializes all Data Reduction Proxy objects that have a -// lifetime based on the DB task runner. -class DBDataOwner { - public: - explicit DBDataOwner(std::unique_ptr<DataStore> store); - - DBDataOwner(const DBDataOwner&) = delete; - DBDataOwner& operator=(const DBDataOwner&) = delete; - - virtual ~DBDataOwner(); - - // Initializes all the DB objects. Must be called on the DB task runner. - void InitializeOnDBThread(); - - // Loads data usage history stored in |DataStore|. - void LoadHistoricalDataUsage(std::vector<DataUsageBucket>* data_usage); - - // Loads the last stored data usage bucket from |DataStore| into |bucket|. - void LoadCurrentDataUsageBucket(DataUsageBucket* bucket); - - // Stores |current| to |DataStore|. - void StoreCurrentDataUsageBucket(std::unique_ptr<DataUsageBucket> current); - - // Deletes all historical data usage from storage. - void DeleteHistoricalDataUsage(); - - // Deletes browsing history for the given data range from storage. - void DeleteBrowsingHistory(const base::Time& start, const base::Time& end); - - // Returns a weak pointer to self for use on UI thread. - base::WeakPtr<DBDataOwner> GetWeakPtr(); - - private: - std::unique_ptr<DataStore> store_; - std::unique_ptr<DataUsageStore> data_usage_; - base::SequenceChecker sequence_checker_; - base::WeakPtrFactory<DBDataOwner> weak_factory_{this}; -}; - -} // namespace data_reduction_proxy -#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DB_DATA_OWNER_H_ |