OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/metrics/data_use_tracker.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "components/metrics/metrics_pref_names.h" |
| 12 #include "components/prefs/scoped_user_pref_update.h" |
| 13 #include "components/variations/variations_associated_data.h" |
| 14 |
| 15 namespace metrics { |
| 16 |
| 17 namespace { |
| 18 void UpdateMetricsUsagePrefs( |
| 19 UpdateUsagePrefCallbackType update_on_ui_callback, |
| 20 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 21 const std::string& service_name, |
| 22 int message_size) { |
| 23 ui_task_runner->PostTask( |
| 24 FROM_HERE, base::Bind(update_on_ui_callback, service_name, message_size)); |
| 25 } |
| 26 } // namespace |
| 27 |
| 28 // static |
| 29 UpdateUsagePrefCallbackType DataUseTracker::GetForwardingCallback( |
| 30 base::WeakPtr<DataUseTracker> data_use_tracker, |
| 31 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) { |
| 32 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); |
| 33 return base::Bind( |
| 34 &UpdateMetricsUsagePrefs, |
| 35 base::Bind(&DataUseTracker::UpdateMetricsUsagePrefsOnUIThread, |
| 36 data_use_tracker), |
| 37 ui_task_runner); |
| 38 } |
| 39 |
| 40 // static |
| 41 void DataUseTracker::RegisterPrefs(PrefRegistrySimple* registry) { |
| 42 registry->RegisterDictionaryPref(metrics::prefs::kUserCellDataUse); |
| 43 registry->RegisterDictionaryPref(metrics::prefs::kUmaCellDataUse); |
| 44 } |
| 45 |
| 46 bool DataUseTracker::CanUploadUMALog(int log_bytes) { |
| 47 RemoveExpiredEntries(); |
| 48 int uma_total_data_use = ComputeTotalDataUse(prefs::kUmaCellDataUse); |
| 49 int uma_quota; |
| 50 bool is_quota_specified = GetUmaQuota(&uma_quota); |
| 51 if (!is_quota_specified || log_bytes + uma_total_data_use <= uma_quota) |
| 52 return true; |
| 53 |
| 54 double uma_ratio; |
| 55 bool is_ratio_specified = GetUmaRatio(&uma_ratio); |
| 56 if (!is_ratio_specified) |
| 57 return true; |
| 58 |
| 59 int user_total_data_use = ComputeTotalDataUse(prefs::kUserCellDataUse); |
| 60 return (log_bytes + uma_total_data_use) / |
| 61 (double)(log_bytes + user_total_data_use) <= |
| 62 uma_ratio; |
| 63 } |
| 64 |
| 65 base::WeakPtr<DataUseTracker> DataUseTracker::GetWeakPtr() { |
| 66 return weak_ptr_factory_.GetWeakPtr(); |
| 67 } |
| 68 |
| 69 DataUseTracker::DataUseTracker(PrefService* local_state) |
| 70 : local_state_(local_state), |
| 71 uma_quota_for_testing_(0), |
| 72 uma_ratio_for_testing_(0), |
| 73 weak_ptr_factory_(this) {} |
| 74 |
| 75 DataUseTracker::~DataUseTracker() {} |
| 76 |
| 77 void DataUseTracker::UpdateMetricsUsagePrefsOnUIThread( |
| 78 const std::string& service_name, |
| 79 int message_size) { |
| 80 UpdateUsagePref(prefs::kUserCellDataUse, message_size); |
| 81 if (service_name == "UMA") |
| 82 UpdateUsagePref(prefs::kUmaCellDataUse, message_size); |
| 83 } |
| 84 |
| 85 void DataUseTracker::UpdateUsagePref(const std::string& pref_name, |
| 86 int message_size) { |
| 87 DictionaryPrefUpdate pref_updater(local_state_, pref_name); |
| 88 int todays_traffic = 0; |
| 89 std::string todays_key = GetCurrentMeasurementDate(); |
| 90 local_state_->GetDictionary(pref_name)->GetInteger(todays_key, |
| 91 &todays_traffic); |
| 92 pref_updater->SetInteger(todays_key, todays_traffic + message_size); |
| 93 } |
| 94 |
| 95 void DataUseTracker::RemoveExpiredEntries() { |
| 96 RemoveExpiredEntriesForPref(prefs::kUmaCellDataUse); |
| 97 RemoveExpiredEntriesForPref(prefs::kUserCellDataUse); |
| 98 } |
| 99 |
| 100 void DataUseTracker::RemoveExpiredEntriesForPref(const std::string& pref_name) { |
| 101 const base::DictionaryValue* user_pref_dict = |
| 102 local_state_->GetDictionary(pref_name); |
| 103 if (!user_pref_dict) |
| 104 return; |
| 105 |
| 106 base::DictionaryValue user_pref_new_dict; |
| 107 for (base::DictionaryValue::Iterator it(*user_pref_dict); !it.IsAtEnd(); |
| 108 it.Advance()) { |
| 109 base::Time current_date; |
| 110 if (!date_for_testing_.empty()) |
| 111 base::Time::FromUTCString(date_for_testing_.c_str(), ¤t_date); |
| 112 else |
| 113 current_date = base::Time::Now().LocalMidnight(); |
| 114 |
| 115 base::Time week_ago = current_date - base::TimeDelta::FromDays(7); |
| 116 base::Time key_date; |
| 117 base::Time::FromUTCString(it.key().c_str(), &key_date); |
| 118 |
| 119 if (key_date > week_ago) |
| 120 user_pref_new_dict.Set(it.key(), it.value().CreateDeepCopy()); |
| 121 } |
| 122 local_state_->Set(pref_name, user_pref_new_dict); |
| 123 } |
| 124 |
| 125 std::string DataUseTracker::GetCurrentMeasurementDate() { |
| 126 if (!date_for_testing_.empty()) |
| 127 return date_for_testing_; |
| 128 |
| 129 base::Time::Exploded today_exploded; |
| 130 base::Time::Now().LocalMidnight().LocalExplode(&today_exploded); |
| 131 return base::StringPrintf("%04d-%02d-%02d", today_exploded.year, |
| 132 today_exploded.month, today_exploded.day_of_month); |
| 133 } |
| 134 |
| 135 int DataUseTracker::ComputeTotalDataUse(std::string pref_name) { |
| 136 int total_data_use = 0; |
| 137 const base::DictionaryValue* pref_dict = |
| 138 local_state_->GetDictionary(pref_name); |
| 139 if (!pref_dict) |
| 140 return total_data_use; |
| 141 for (base::DictionaryValue::Iterator it(*pref_dict); !it.IsAtEnd(); |
| 142 it.Advance()) { |
| 143 int value = 0; |
| 144 it.value().GetAsInteger(&value); |
| 145 total_data_use += value; |
| 146 } |
| 147 return total_data_use; |
| 148 } |
| 149 |
| 150 bool DataUseTracker::GetUmaQuota(int* quota) { |
| 151 if (uma_quota_for_testing_ > 0) { |
| 152 *quota = uma_quota_for_testing_; |
| 153 return true; |
| 154 } |
| 155 |
| 156 std::string param_value_str = variations::GetVariationParamValue( |
| 157 "UMA_EnableCellularLogUpload", "Uma_Quota"); |
| 158 if (param_value_str.empty()) |
| 159 return false; |
| 160 |
| 161 base::StringToInt(param_value_str, quota); |
| 162 return true; |
| 163 } |
| 164 |
| 165 bool DataUseTracker::GetUmaRatio(double* ratio) { |
| 166 if (uma_ratio_for_testing_ > 0) { |
| 167 *ratio = uma_ratio_for_testing_; |
| 168 return true; |
| 169 } |
| 170 |
| 171 std::string param_value_str = variations::GetVariationParamValue( |
| 172 "UMA_EnableCellularLogUpload", "Uma_Ratio"); |
| 173 if (param_value_str.empty()) |
| 174 return false; |
| 175 base::StringToDouble(param_value_str, ratio); |
| 176 return true; |
| 177 } |
| 178 |
| 179 void DataUseTracker::SetMeasurementDateForTesting(const std::string& date) { |
| 180 date_for_testing_ = date; |
| 181 } |
| 182 |
| 183 void DataUseTracker::SetUmaQuotaForTesting(int uma_quota) { |
| 184 uma_quota_for_testing_ = uma_quota; |
| 185 } |
| 186 |
| 187 void DataUseTracker::SetUmaRatioForTesting(double uma_ratio) { |
| 188 uma_ratio_for_testing_ = uma_ratio; |
| 189 } |
| 190 |
| 191 } // namespace metrics |
OLD | NEW |