Chromium Code Reviews| 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..ab3346341b5ce67473f0a41f9fbd2c378435d422 |
| --- /dev/null |
| +++ b/components/metrics/data_use_tracker.cc |
| @@ -0,0 +1,199 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
|
Alexei Svitkine (slow)
2016/03/29 16:29:10
2016
gayane -on leave until 09-2017
2016/03/31 01:38:24
Done.
|
| +// 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" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace metrics { |
| + |
| +namespace { |
| +static scoped_ptr<metrics::DataUseTracker> instance_; |
|
Alexei Svitkine (slow)
2016/03/29 16:29:10
This is not needed anymore, right?
gayane -on leave until 09-2017
2016/03/31 01:38:24
Done.
|
| +} |
| + |
| +// static |
| +void DataUseTracker::GetForwardingCallback( |
| + base::WeakPtr<DataUseTracker> data_use_tracker, |
| + base::Callback<void(base::Callback<void(const std::string&, int)>)> |
| + reply_callback) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + content::BrowserThread::PostTaskAndReplyWithResult( |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(&DataUseTracker::GetForwardingCallbackOnUIThread, |
| + data_use_tracker), |
| + reply_callback); |
| +} |
| + |
| +// static |
| +base::Callback<void(const std::string&, int)> |
| +DataUseTracker::GetForwardingCallbackOnUIThread( |
| + base::WeakPtr<DataUseTracker> data_use_tracker) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + return base::Bind(&DataUseTracker::UpdateMetricsUsagePrefsOnUIThread, |
| + data_use_tracker); |
| +} |
| + |
| +void DataUseTracker::RunOnUI( |
| + base::Callback<void(const std::string&, int)> callback, |
| + const std::string& service_name, |
| + int message_size) { |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(callback, service_name, message_size)); |
| +} |
| + |
| +// static |
| +void DataUseTracker::RegisterPrefs(PrefRegistrySimple* registry) { |
| + registry->RegisterDictionaryPref(metrics::prefs::kUserCellDataUse); |
| + registry->RegisterDictionaryPref(metrics::prefs::kUmaCellDataUse); |
| +} |
| + |
| +DataUseTracker::DataUseTracker(PrefService* local_state) |
| + : local_state_(local_state), |
| + uma_qouta_for_testing_(0), |
| + uma_ratio_for_testing_(0), |
| + weak_ptr_factory_(this) {} |
| + |
| +DataUseTracker::~DataUseTracker() {} |
| + |
| +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); |
| +} |
| + |
| +void DataUseTracker::UpdateMetricsUsagePrefsOnUIThread( |
| + const std::string& service_name, |
| + int message_size) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + UpdateUsagePref(prefs::kUserCellDataUse, message_size); |
| + if (service_name == "UMA") |
| + UpdateUsagePref(prefs::kUmaCellDataUse, 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 start_of_week = |
| + base::Time::Now().LocalMidnight() - base::TimeDelta::FromDays(7); |
| + base::Time key_date; |
| + base::Time::FromString(it.key().c_str(), &key_date); |
| + if (key_date >= start_of_week) { |
| + user_pref_new_dict.Set(it.key(), it.value().CreateDeepCopy()); |
| + } |
| + } |
| + local_state_->Set(pref_name, user_pref_new_dict); |
| +} |
| + |
| +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); |
| +} |
| + |
| +bool DataUseTracker::CanUploadUMALog(int log_bytes) { |
| + RemoveExpiredEntries(); |
| + int uma_total_data_use = ComputeTotalDataUse(prefs::kUmaCellDataUse); |
| + int uma_qouta; |
| + bool is_qouta_specified = GetUmaQouta(&uma_qouta); |
| + if (!is_qouta_specified || log_bytes + uma_total_data_use <= uma_qouta) |
| + 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; |
| +} |
| + |
| +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::GetUmaQouta(int* qouta) { |
| + if (uma_qouta_for_testing_ > 0) { |
| + *qouta = uma_qouta_for_testing_; |
| + return true; |
| + } |
| + |
| + std::string param_value_str = variations::GetVariationParamValue( |
| + "UMA_EnableCellularLogUpload", "Uma_Qouta"); |
| + if (param_value_str.empty()) |
| + return false; |
| + |
| + base::StringToInt(param_value_str, qouta); |
| + 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::SetUmaQoutaForTesting(int uma_qouta) { |
| + uma_qouta_for_testing_ = uma_qouta; |
| +} |
| + |
| +void DataUseTracker::SetUmaRatioForTesting(double uma_ratio) { |
| + uma_ratio_for_testing_ = uma_ratio; |
| +} |
| + |
| +base::WeakPtr<DataUseTracker> DataUseTracker::GetWeakPtr() { |
| + return weak_ptr_factory_.GetWeakPtr(); |
| +} |
| + |
| +} // namespace metrics |