Chromium Code Reviews| 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 | |
| 19 // This function is for forwarding metrics usage pref changes to the appropriate | |
| 20 // callback on the appropriate thread. | |
| 21 void UpdateMetricsUsagePrefs( | |
| 22 const UpdateUsagePrefCallbackType& update_on_ui_callback, | |
| 23 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | |
| 24 const std::string& service_name, | |
| 25 int message_size) { | |
| 26 ui_task_runner->PostTask( | |
| 27 FROM_HERE, base::Bind(update_on_ui_callback, service_name, message_size)); | |
| 28 } | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 DataUseTracker::DataUseTracker(PrefService* local_state) | |
| 33 : local_state_(local_state), | |
| 34 uma_quota_for_testing_(0), | |
| 35 uma_ratio_for_testing_(0), | |
| 36 weak_ptr_factory_(this) {} | |
| 37 | |
| 38 DataUseTracker::~DataUseTracker() {} | |
| 39 | |
| 40 // static | |
| 41 void DataUseTracker::RegisterPrefs(PrefRegistrySimple* registry) { | |
| 42 registry->RegisterDictionaryPref(metrics::prefs::kUserCellDataUse); | |
| 43 registry->RegisterDictionaryPref(metrics::prefs::kUmaCellDataUse); | |
| 44 } | |
| 45 | |
| 46 UpdateUsagePrefCallbackType DataUseTracker::GetDataUseForwardingCallback( | |
| 47 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) { | |
| 48 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); | |
| 49 return base::Bind( | |
| 50 &UpdateMetricsUsagePrefs, | |
| 51 base::Bind(&DataUseTracker::UpdateMetricsUsagePrefsOnUIThread, | |
| 52 weak_ptr_factory_.GetWeakPtr()), | |
| 53 ui_task_runner); | |
| 54 } | |
| 55 | |
| 56 bool DataUseTracker::CanUploadUMALog(int log_bytes) { | |
| 57 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 58 RemoveExpiredEntries(); | |
| 59 | |
| 60 int uma_quota; | |
|
Alexei Svitkine (slow)
2016/03/31 22:18:03
Nit: uma_weekly_quota_bytes
gayane -on leave until 09-2017
2016/04/01 19:49:30
Done.
| |
| 61 if (!GetUmaQuota(&uma_quota)) | |
| 62 return true; | |
| 63 | |
| 64 int uma_total_data_use = ComputeTotalDataUse(prefs::kUmaCellDataUse); | |
| 65 if (log_bytes + uma_total_data_use <= uma_quota) | |
|
Alexei Svitkine (slow)
2016/03/31 22:18:03
Nit: Make a var for log_bytes + log_bytes + uma_to
gayane -on leave until 09-2017
2016/04/01 19:49:30
Done.
| |
| 66 return true; | |
| 67 | |
| 68 double uma_ratio; | |
| 69 if (!GetUmaRatio(&uma_ratio)) | |
| 70 return true; | |
| 71 | |
| 72 int user_total_data_use = ComputeTotalDataUse(prefs::kUserCellDataUse); | |
|
Alexei Svitkine (slow)
2016/03/31 22:18:02
Please add a comment above both this and the check
gayane -on leave until 09-2017
2016/04/01 19:49:30
Done.
| |
| 73 return (log_bytes + uma_total_data_use) / | |
| 74 static_cast<double>(log_bytes + user_total_data_use) <= | |
| 75 uma_ratio; | |
| 76 } | |
| 77 | |
| 78 void DataUseTracker::UpdateMetricsUsagePrefsOnUIThread( | |
| 79 const std::string& service_name, | |
| 80 int message_size) { | |
| 81 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 82 UpdateUsagePref(prefs::kUserCellDataUse, message_size); | |
| 83 if (service_name == "UMA") | |
| 84 UpdateUsagePref(prefs::kUmaCellDataUse, message_size); | |
| 85 } | |
| 86 | |
| 87 void DataUseTracker::UpdateUsagePref(const std::string& pref_name, | |
| 88 int message_size) { | |
| 89 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 90 DictionaryPrefUpdate pref_updater(local_state_, pref_name); | |
| 91 int todays_traffic = 0; | |
| 92 std::string todays_key = GetCurrentMeasurementDateAsString(); | |
| 93 | |
| 94 const base::DictionaryValue* user_pref_dict = | |
| 95 local_state_->GetDictionary(pref_name); | |
| 96 if (user_pref_dict) { | |
|
Alexei Svitkine (slow)
2016/03/31 22:18:03
Nit: No {}'s
Add a comment explaining when it wil
gayane -on leave until 09-2017
2016/04/01 19:49:30
Done.
| |
| 97 user_pref_dict->GetInteger(todays_key, &todays_traffic); | |
| 98 } | |
| 99 | |
| 100 pref_updater->SetInteger(todays_key, todays_traffic + message_size); | |
| 101 } | |
| 102 | |
| 103 void DataUseTracker::RemoveExpiredEntries() { | |
| 104 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 105 RemoveExpiredEntriesForPref(prefs::kUmaCellDataUse); | |
| 106 RemoveExpiredEntriesForPref(prefs::kUserCellDataUse); | |
| 107 } | |
| 108 | |
| 109 void DataUseTracker::RemoveExpiredEntriesForPref(const std::string& pref_name) { | |
| 110 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 111 const base::DictionaryValue* user_pref_dict = | |
| 112 local_state_->GetDictionary(pref_name); | |
| 113 if (!user_pref_dict) | |
| 114 return; | |
| 115 | |
| 116 base::DictionaryValue user_pref_new_dict; | |
| 117 for (base::DictionaryValue::Iterator it(*user_pref_dict); !it.IsAtEnd(); | |
| 118 it.Advance()) { | |
| 119 base::Time key_date; | |
| 120 base::Time::FromUTCString(it.key().c_str(), &key_date); | |
| 121 base::Time current_date = GetCurrentMeasurementDate(); | |
| 122 base::Time week_ago = current_date - base::TimeDelta::FromDays(7); | |
|
Alexei Svitkine (slow)
2016/03/31 22:18:03
Move these lines outside of the for loop and make
gayane -on leave until 09-2017
2016/04/01 19:49:30
Done.
| |
| 123 | |
| 124 if (key_date > week_ago) | |
| 125 user_pref_new_dict.Set(it.key(), it.value().CreateDeepCopy()); | |
| 126 } | |
| 127 local_state_->Set(pref_name, user_pref_new_dict); | |
| 128 } | |
| 129 | |
| 130 int DataUseTracker::ComputeTotalDataUse(std::string pref_name) { | |
| 131 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 132 int total_data_use = 0; | |
| 133 const base::DictionaryValue* pref_dict = | |
| 134 local_state_->GetDictionary(pref_name); | |
| 135 if (!pref_dict) | |
| 136 return total_data_use; | |
| 137 for (base::DictionaryValue::Iterator it(*pref_dict); !it.IsAtEnd(); | |
| 138 it.Advance()) { | |
| 139 int value = 0; | |
| 140 it.value().GetAsInteger(&value); | |
| 141 total_data_use += value; | |
| 142 } | |
| 143 return total_data_use; | |
| 144 } | |
| 145 | |
| 146 bool DataUseTracker::GetUmaQuota(int* quota) { | |
| 147 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 148 std::string param_value_str = variations::GetVariationParamValue( | |
| 149 "UMA_EnableCellularLogUpload", "Uma_Quota"); | |
| 150 if (param_value_str.empty()) | |
| 151 return false; | |
| 152 | |
| 153 base::StringToInt(param_value_str, quota); | |
| 154 return true; | |
| 155 } | |
| 156 | |
| 157 bool DataUseTracker::GetUmaRatio(double* ratio) { | |
| 158 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 159 std::string param_value_str = variations::GetVariationParamValue( | |
| 160 "UMA_EnableCellularLogUpload", "Uma_Ratio"); | |
| 161 if (param_value_str.empty()) | |
| 162 return false; | |
| 163 base::StringToDouble(param_value_str, ratio); | |
| 164 return true; | |
| 165 } | |
| 166 | |
| 167 base::Time DataUseTracker::GetCurrentMeasurementDate() { | |
| 168 return base::Time::Now().LocalMidnight(); | |
| 169 } | |
| 170 | |
| 171 std::string DataUseTracker::GetCurrentMeasurementDateAsString() { | |
| 172 DCHECK(thread_checker_.CalledOnValidThread()); | |
|
Alexei Svitkine (slow)
2016/03/31 22:18:03
Nit: Add an empty line after each line like this.
gayane -on leave until 09-2017
2016/04/01 19:49:30
Done.
| |
| 173 base::Time::Exploded today_exploded; | |
| 174 GetCurrentMeasurementDate().LocalExplode(&today_exploded); | |
| 175 return base::StringPrintf("%04d-%02d-%02d", today_exploded.year, | |
| 176 today_exploded.month, today_exploded.day_of_month); | |
| 177 } | |
| 178 | |
| 179 } // namespace metrics | |
| OLD | NEW |