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 // TODO(gayane): Reduce the frequency of posting tasks from IO to UI thread. |
| 22 void UpdateMetricsUsagePrefs( |
| 23 const UpdateUsagePrefCallbackType& update_on_ui_callback, |
| 24 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 25 const std::string& service_name, |
| 26 int message_size, |
| 27 bool is_cellular) { |
| 28 ui_task_runner->PostTask( |
| 29 FROM_HERE, base::Bind(update_on_ui_callback, service_name, message_size, |
| 30 is_cellular)); |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
| 35 DataUseTracker::DataUseTracker(PrefService* local_state) |
| 36 : local_state_(local_state), weak_ptr_factory_(this) {} |
| 37 |
| 38 DataUseTracker::~DataUseTracker() {} |
| 39 |
| 40 // static |
| 41 scoped_ptr<DataUseTracker> DataUseTracker::Create(PrefService* local_state) { |
| 42 scoped_ptr<DataUseTracker> data_use_tracker; |
| 43 if (variations::GetVariationParamValue("UMA_EnableCellularLogUpload", |
| 44 "Uma_Quota") != "" && |
| 45 variations::GetVariationParamValue("UMA_EnableCellularLogUpload", |
| 46 "Uma_Ratio") != "") { |
| 47 data_use_tracker.reset(new DataUseTracker(local_state)); |
| 48 } |
| 49 return data_use_tracker; |
| 50 } |
| 51 |
| 52 // static |
| 53 void DataUseTracker::RegisterPrefs(PrefRegistrySimple* registry) { |
| 54 registry->RegisterDictionaryPref(metrics::prefs::kUserCellDataUse); |
| 55 registry->RegisterDictionaryPref(metrics::prefs::kUmaCellDataUse); |
| 56 } |
| 57 |
| 58 UpdateUsagePrefCallbackType DataUseTracker::GetDataUseForwardingCallback( |
| 59 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) { |
| 60 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); |
| 61 |
| 62 return base::Bind( |
| 63 &UpdateMetricsUsagePrefs, |
| 64 base::Bind(&DataUseTracker::UpdateMetricsUsagePrefsOnUIThread, |
| 65 weak_ptr_factory_.GetWeakPtr()), |
| 66 ui_task_runner); |
| 67 } |
| 68 |
| 69 bool DataUseTracker::ShouldUploadLogOnCellular(int log_bytes) { |
| 70 DCHECK(thread_checker_.CalledOnValidThread()); |
| 71 |
| 72 RemoveExpiredEntries(); |
| 73 |
| 74 int uma_weekly_quota_bytes; |
| 75 if (!GetUmaWeeklyQuota(&uma_weekly_quota_bytes)) |
| 76 return true; |
| 77 |
| 78 int uma_total_data_use = ComputeTotalDataUse(prefs::kUmaCellDataUse); |
| 79 int new_uma_total_data_use = log_bytes + uma_total_data_use; |
| 80 // If the new log doesn't increase the total UMA traffic to be above the |
| 81 // allowed quota then the log should be uploaded. |
| 82 if (new_uma_total_data_use <= uma_weekly_quota_bytes) |
| 83 return true; |
| 84 |
| 85 double uma_ratio; |
| 86 if (!GetUmaRatio(&uma_ratio)) |
| 87 return true; |
| 88 |
| 89 int user_total_data_use = ComputeTotalDataUse(prefs::kUserCellDataUse); |
| 90 // If after adding the new log the uma ratio is still under the allowed ratio |
| 91 // then the log should be uploaded and vice versa. |
| 92 return new_uma_total_data_use / |
| 93 static_cast<double>(log_bytes + user_total_data_use) <= |
| 94 uma_ratio; |
| 95 } |
| 96 |
| 97 void DataUseTracker::UpdateMetricsUsagePrefsOnUIThread( |
| 98 const std::string& service_name, |
| 99 int message_size, |
| 100 bool is_celllular) { |
| 101 DCHECK(thread_checker_.CalledOnValidThread()); |
| 102 |
| 103 if (!is_celllular) |
| 104 return; |
| 105 |
| 106 UpdateUsagePref(prefs::kUserCellDataUse, message_size); |
| 107 if (service_name == "UMA") |
| 108 UpdateUsagePref(prefs::kUmaCellDataUse, message_size); |
| 109 } |
| 110 |
| 111 void DataUseTracker::UpdateUsagePref(const std::string& pref_name, |
| 112 int message_size) { |
| 113 DCHECK(thread_checker_.CalledOnValidThread()); |
| 114 |
| 115 DictionaryPrefUpdate pref_updater(local_state_, pref_name); |
| 116 int todays_traffic = 0; |
| 117 std::string todays_key = GetCurrentMeasurementDateAsString(); |
| 118 |
| 119 const base::DictionaryValue* user_pref_dict = |
| 120 local_state_->GetDictionary(pref_name); |
| 121 user_pref_dict->GetInteger(todays_key, &todays_traffic); |
| 122 pref_updater->SetInteger(todays_key, todays_traffic + message_size); |
| 123 } |
| 124 |
| 125 void DataUseTracker::RemoveExpiredEntries() { |
| 126 DCHECK(thread_checker_.CalledOnValidThread()); |
| 127 RemoveExpiredEntriesForPref(prefs::kUmaCellDataUse); |
| 128 RemoveExpiredEntriesForPref(prefs::kUserCellDataUse); |
| 129 } |
| 130 |
| 131 void DataUseTracker::RemoveExpiredEntriesForPref(const std::string& pref_name) { |
| 132 DCHECK(thread_checker_.CalledOnValidThread()); |
| 133 |
| 134 const base::DictionaryValue* user_pref_dict = |
| 135 local_state_->GetDictionary(pref_name); |
| 136 const base::Time current_date = GetCurrentMeasurementDate(); |
| 137 const base::Time week_ago = current_date - base::TimeDelta::FromDays(7); |
| 138 |
| 139 base::DictionaryValue user_pref_new_dict; |
| 140 for (base::DictionaryValue::Iterator it(*user_pref_dict); !it.IsAtEnd(); |
| 141 it.Advance()) { |
| 142 base::Time key_date; |
| 143 base::Time::FromUTCString(it.key().c_str(), &key_date); |
| 144 if (key_date > week_ago) |
| 145 user_pref_new_dict.Set(it.key(), it.value().CreateDeepCopy()); |
| 146 } |
| 147 local_state_->Set(pref_name, user_pref_new_dict); |
| 148 } |
| 149 |
| 150 // Note: We compute total data use regardless of what is the current date. In |
| 151 // scenario when user travels back in time zone and current date becomes earlier |
| 152 // than latest registered date in perf, we still count that in total use as user |
| 153 // actually used that data. |
| 154 int DataUseTracker::ComputeTotalDataUse(const std::string& pref_name) { |
| 155 DCHECK(thread_checker_.CalledOnValidThread()); |
| 156 |
| 157 int total_data_use = 0; |
| 158 const base::DictionaryValue* pref_dict = |
| 159 local_state_->GetDictionary(pref_name); |
| 160 for (base::DictionaryValue::Iterator it(*pref_dict); !it.IsAtEnd(); |
| 161 it.Advance()) { |
| 162 int value = 0; |
| 163 it.value().GetAsInteger(&value); |
| 164 total_data_use += value; |
| 165 } |
| 166 return total_data_use; |
| 167 } |
| 168 |
| 169 bool DataUseTracker::GetUmaWeeklyQuota(int* uma_weekly_quota_bytes) const { |
| 170 DCHECK(thread_checker_.CalledOnValidThread()); |
| 171 |
| 172 std::string param_value_str = variations::GetVariationParamValue( |
| 173 "UMA_EnableCellularLogUpload", "Uma_Quota"); |
| 174 if (param_value_str.empty()) |
| 175 return false; |
| 176 |
| 177 base::StringToInt(param_value_str, uma_weekly_quota_bytes); |
| 178 return true; |
| 179 } |
| 180 |
| 181 bool DataUseTracker::GetUmaRatio(double* ratio) const { |
| 182 DCHECK(thread_checker_.CalledOnValidThread()); |
| 183 |
| 184 std::string param_value_str = variations::GetVariationParamValue( |
| 185 "UMA_EnableCellularLogUpload", "Uma_Ratio"); |
| 186 if (param_value_str.empty()) |
| 187 return false; |
| 188 base::StringToDouble(param_value_str, ratio); |
| 189 return true; |
| 190 } |
| 191 |
| 192 base::Time DataUseTracker::GetCurrentMeasurementDate() const { |
| 193 return base::Time::Now().LocalMidnight(); |
| 194 } |
| 195 |
| 196 std::string DataUseTracker::GetCurrentMeasurementDateAsString() const { |
| 197 DCHECK(thread_checker_.CalledOnValidThread()); |
| 198 |
| 199 base::Time::Exploded today_exploded; |
| 200 GetCurrentMeasurementDate().LocalExplode(&today_exploded); |
| 201 return base::StringPrintf("%04d-%02d-%02d", today_exploded.year, |
| 202 today_exploded.month, today_exploded.day_of_month); |
| 203 } |
| 204 |
| 205 } // namespace metrics |
OLD | NEW |