| Index: components/metrics/data_use_tracker.cc
|
| diff --git a/components/metrics/data_use_tracker.cc b/components/metrics/data_use_tracker.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..718fb9445fcdab671d201f8ab780c11a9f3e4680
|
| --- /dev/null
|
| +++ b/components/metrics/data_use_tracker.cc
|
| @@ -0,0 +1,191 @@
|
| +// Copyright 2016 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/metrics/data_use_tracker.h"
|
| +
|
| +#include <string>
|
| +
|
| +#include "base/strings/string_number_conversions.h"
|
| +#include "base/strings/stringprintf.h"
|
| +#include "components/metrics/metrics_pref_names.h"
|
| +#include "components/prefs/scoped_user_pref_update.h"
|
| +#include "components/variations/variations_associated_data.h"
|
| +
|
| +namespace metrics {
|
| +
|
| +namespace {
|
| +void UpdateMetricsUsagePrefs(
|
| + UpdateUsagePrefCallbackType update_on_ui_callback,
|
| + scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
|
| + const std::string& service_name,
|
| + int message_size) {
|
| + ui_task_runner->PostTask(
|
| + FROM_HERE, base::Bind(update_on_ui_callback, service_name, message_size));
|
| +}
|
| +} // namespace
|
| +
|
| +// static
|
| +UpdateUsagePrefCallbackType DataUseTracker::GetForwardingCallback(
|
| + base::WeakPtr<DataUseTracker> data_use_tracker,
|
| + scoped_refptr<base::SequencedTaskRunner> ui_task_runner) {
|
| + DCHECK(ui_task_runner->RunsTasksOnCurrentThread());
|
| + return base::Bind(
|
| + &UpdateMetricsUsagePrefs,
|
| + base::Bind(&DataUseTracker::UpdateMetricsUsagePrefsOnUIThread,
|
| + data_use_tracker),
|
| + ui_task_runner);
|
| +}
|
| +
|
| +// static
|
| +void DataUseTracker::RegisterPrefs(PrefRegistrySimple* registry) {
|
| + registry->RegisterDictionaryPref(metrics::prefs::kUserCellDataUse);
|
| + registry->RegisterDictionaryPref(metrics::prefs::kUmaCellDataUse);
|
| +}
|
| +
|
| +bool DataUseTracker::CanUploadUMALog(int log_bytes) {
|
| + RemoveExpiredEntries();
|
| + int uma_total_data_use = ComputeTotalDataUse(prefs::kUmaCellDataUse);
|
| + int uma_quota;
|
| + bool is_quota_specified = GetUmaQuota(&uma_quota);
|
| + if (!is_quota_specified || log_bytes + uma_total_data_use <= uma_quota)
|
| + return true;
|
| +
|
| + double uma_ratio;
|
| + bool is_ratio_specified = GetUmaRatio(&uma_ratio);
|
| + if (!is_ratio_specified)
|
| + return true;
|
| +
|
| + int user_total_data_use = ComputeTotalDataUse(prefs::kUserCellDataUse);
|
| + return (log_bytes + uma_total_data_use) /
|
| + (double)(log_bytes + user_total_data_use) <=
|
| + uma_ratio;
|
| +}
|
| +
|
| +base::WeakPtr<DataUseTracker> DataUseTracker::GetWeakPtr() {
|
| + return weak_ptr_factory_.GetWeakPtr();
|
| +}
|
| +
|
| +DataUseTracker::DataUseTracker(PrefService* local_state)
|
| + : local_state_(local_state),
|
| + uma_quota_for_testing_(0),
|
| + uma_ratio_for_testing_(0),
|
| + weak_ptr_factory_(this) {}
|
| +
|
| +DataUseTracker::~DataUseTracker() {}
|
| +
|
| +void DataUseTracker::UpdateMetricsUsagePrefsOnUIThread(
|
| + const std::string& service_name,
|
| + int message_size) {
|
| + UpdateUsagePref(prefs::kUserCellDataUse, message_size);
|
| + if (service_name == "UMA")
|
| + UpdateUsagePref(prefs::kUmaCellDataUse, message_size);
|
| +}
|
| +
|
| +void DataUseTracker::UpdateUsagePref(const std::string& pref_name,
|
| + int message_size) {
|
| + DictionaryPrefUpdate pref_updater(local_state_, pref_name);
|
| + int todays_traffic = 0;
|
| + std::string todays_key = GetCurrentMeasurementDate();
|
| + local_state_->GetDictionary(pref_name)->GetInteger(todays_key,
|
| + &todays_traffic);
|
| + pref_updater->SetInteger(todays_key, todays_traffic + message_size);
|
| +}
|
| +
|
| +void DataUseTracker::RemoveExpiredEntries() {
|
| + RemoveExpiredEntriesForPref(prefs::kUmaCellDataUse);
|
| + RemoveExpiredEntriesForPref(prefs::kUserCellDataUse);
|
| +}
|
| +
|
| +void DataUseTracker::RemoveExpiredEntriesForPref(const std::string& pref_name) {
|
| + const base::DictionaryValue* user_pref_dict =
|
| + local_state_->GetDictionary(pref_name);
|
| + if (!user_pref_dict)
|
| + return;
|
| +
|
| + base::DictionaryValue user_pref_new_dict;
|
| + for (base::DictionaryValue::Iterator it(*user_pref_dict); !it.IsAtEnd();
|
| + it.Advance()) {
|
| + base::Time current_date;
|
| + if (!date_for_testing_.empty())
|
| + base::Time::FromUTCString(date_for_testing_.c_str(), ¤t_date);
|
| + else
|
| + current_date = base::Time::Now().LocalMidnight();
|
| +
|
| + base::Time week_ago = current_date - base::TimeDelta::FromDays(7);
|
| + base::Time key_date;
|
| + base::Time::FromUTCString(it.key().c_str(), &key_date);
|
| +
|
| + if (key_date > week_ago)
|
| + user_pref_new_dict.Set(it.key(), it.value().CreateDeepCopy());
|
| + }
|
| + local_state_->Set(pref_name, user_pref_new_dict);
|
| +}
|
| +
|
| +std::string DataUseTracker::GetCurrentMeasurementDate() {
|
| + if (!date_for_testing_.empty())
|
| + return date_for_testing_;
|
| +
|
| + base::Time::Exploded today_exploded;
|
| + base::Time::Now().LocalMidnight().LocalExplode(&today_exploded);
|
| + return base::StringPrintf("%04d-%02d-%02d", today_exploded.year,
|
| + today_exploded.month, today_exploded.day_of_month);
|
| +}
|
| +
|
| +int DataUseTracker::ComputeTotalDataUse(std::string pref_name) {
|
| + int total_data_use = 0;
|
| + const base::DictionaryValue* pref_dict =
|
| + local_state_->GetDictionary(pref_name);
|
| + if (!pref_dict)
|
| + return total_data_use;
|
| + for (base::DictionaryValue::Iterator it(*pref_dict); !it.IsAtEnd();
|
| + it.Advance()) {
|
| + int value = 0;
|
| + it.value().GetAsInteger(&value);
|
| + total_data_use += value;
|
| + }
|
| + return total_data_use;
|
| +}
|
| +
|
| +bool DataUseTracker::GetUmaQuota(int* quota) {
|
| + if (uma_quota_for_testing_ > 0) {
|
| + *quota = uma_quota_for_testing_;
|
| + return true;
|
| + }
|
| +
|
| + std::string param_value_str = variations::GetVariationParamValue(
|
| + "UMA_EnableCellularLogUpload", "Uma_Quota");
|
| + if (param_value_str.empty())
|
| + return false;
|
| +
|
| + base::StringToInt(param_value_str, quota);
|
| + return true;
|
| +}
|
| +
|
| +bool DataUseTracker::GetUmaRatio(double* ratio) {
|
| + if (uma_ratio_for_testing_ > 0) {
|
| + *ratio = uma_ratio_for_testing_;
|
| + return true;
|
| + }
|
| +
|
| + std::string param_value_str = variations::GetVariationParamValue(
|
| + "UMA_EnableCellularLogUpload", "Uma_Ratio");
|
| + if (param_value_str.empty())
|
| + return false;
|
| + base::StringToDouble(param_value_str, ratio);
|
| + return true;
|
| +}
|
| +
|
| +void DataUseTracker::SetMeasurementDateForTesting(const std::string& date) {
|
| + date_for_testing_ = date;
|
| +}
|
| +
|
| +void DataUseTracker::SetUmaQuotaForTesting(int uma_quota) {
|
| + uma_quota_for_testing_ = uma_quota;
|
| +}
|
| +
|
| +void DataUseTracker::SetUmaRatioForTesting(double uma_ratio) {
|
| + uma_ratio_for_testing_ = uma_ratio;
|
| +}
|
| +
|
| +} // namespace metrics
|
|
|