Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/metrics/data_use_tracker.h" | 5 #include "components/metrics/data_use_tracker.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "components/metrics/metrics_pref_names.h" | 11 #include "components/metrics/metrics_pref_names.h" |
| 12 #include "components/prefs/scoped_user_pref_update.h" | 12 #include "components/prefs/scoped_user_pref_update.h" |
| 13 #include "components/variations/variations_associated_data.h" | 13 #include "components/variations/variations_associated_data.h" |
| 14 | 14 |
| 15 namespace metrics { | 15 namespace metrics { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // Default weekly quota and allowed UMA ratio for UMA log uploads. Can be | |
| 20 // overridden by variation params. | |
| 21 const int kDefaultUMAWeeklyQuotaBytes = 204800; | |
| 22 const double kDefaultUMARatio = 0.05; | |
|
Alexei Svitkine (slow)
2016/05/13 20:46:47
Are these defaults safe for non-Android platforms?
gayane -on leave until 09-2017
2016/05/13 20:57:12
It is not obvious. The functions will be called on
Alexei Svitkine (slow)
2016/05/13 20:59:43
Just having a comment explaining this above these
gayane -on leave until 09-2017
2016/05/16 15:33:04
Done.
| |
| 23 | |
| 19 // This function is for forwarding metrics usage pref changes to the appropriate | 24 // This function is for forwarding metrics usage pref changes to the appropriate |
| 20 // callback on the appropriate thread. | 25 // callback on the appropriate thread. |
| 21 // TODO(gayane): Reduce the frequency of posting tasks from IO to UI thread. | 26 // TODO(gayane): Reduce the frequency of posting tasks from IO to UI thread. |
| 22 void UpdateMetricsUsagePrefs( | 27 void UpdateMetricsUsagePrefs( |
| 23 const UpdateUsagePrefCallbackType& update_on_ui_callback, | 28 const UpdateUsagePrefCallbackType& update_on_ui_callback, |
| 24 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 29 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 25 const std::string& service_name, | 30 const std::string& service_name, |
| 26 int message_size, | 31 int message_size, |
| 27 bool is_cellular) { | 32 bool is_cellular) { |
| 28 ui_task_runner->PostTask( | 33 ui_task_runner->PostTask( |
| 29 FROM_HERE, base::Bind(update_on_ui_callback, service_name, message_size, | 34 FROM_HERE, base::Bind(update_on_ui_callback, service_name, message_size, |
| 30 is_cellular)); | 35 is_cellular)); |
| 31 } | 36 } |
| 32 | 37 |
| 33 } // namespace | 38 } // namespace |
| 34 | 39 |
| 35 DataUseTracker::DataUseTracker(PrefService* local_state) | 40 DataUseTracker::DataUseTracker(PrefService* local_state) |
| 36 : local_state_(local_state), weak_ptr_factory_(this) {} | 41 : local_state_(local_state), weak_ptr_factory_(this) {} |
| 37 | 42 |
| 38 DataUseTracker::~DataUseTracker() {} | 43 DataUseTracker::~DataUseTracker() {} |
| 39 | 44 |
| 40 // static | 45 // static |
| 41 std::unique_ptr<DataUseTracker> DataUseTracker::Create( | 46 std::unique_ptr<DataUseTracker> DataUseTracker::Create( |
| 42 PrefService* local_state) { | 47 PrefService* local_state) { |
| 43 std::unique_ptr<DataUseTracker> data_use_tracker; | 48 std::unique_ptr<DataUseTracker> data_use_tracker; |
| 44 if (variations::GetVariationParamValue("UMA_EnableCellularLogUpload", | 49 if (variations::GetVariationParamValue("UMA_EnableCellularLogUpload", |
| 45 "Uma_Quota") != "" && | 50 "Uma_Quota") != "" && |
| 46 variations::GetVariationParamValue("UMA_EnableCellularLogUpload", | 51 variations::GetVariationParamValue("UMA_EnableCellularLogUpload", |
| 47 "Uma_Ratio") != "") { | 52 "Uma_Ratio") != "") { |
|
Alexei Svitkine (slow)
2016/05/13 20:46:47
We would want this logic to be changed too, since
gayane -on leave until 09-2017
2016/05/13 20:57:12
Done.
Alexei Svitkine (slow)
2016/05/13 20:59:43
(Waiting to see this in your next patchset.)
gayane -on leave until 09-2017
2016/05/16 15:33:04
Acknowledged.
| |
| 48 data_use_tracker.reset(new DataUseTracker(local_state)); | 53 data_use_tracker.reset(new DataUseTracker(local_state)); |
| 49 } | 54 } |
| 50 return data_use_tracker; | 55 return data_use_tracker; |
| 51 } | 56 } |
| 52 | 57 |
| 53 // static | 58 // static |
| 54 void DataUseTracker::RegisterPrefs(PrefRegistrySimple* registry) { | 59 void DataUseTracker::RegisterPrefs(PrefRegistrySimple* registry) { |
| 55 registry->RegisterDictionaryPref(metrics::prefs::kUserCellDataUse); | 60 registry->RegisterDictionaryPref(metrics::prefs::kUserCellDataUse); |
| 56 registry->RegisterDictionaryPref(metrics::prefs::kUmaCellDataUse); | 61 registry->RegisterDictionaryPref(metrics::prefs::kUmaCellDataUse); |
| 57 } | 62 } |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 } | 171 } |
| 167 return total_data_use; | 172 return total_data_use; |
| 168 } | 173 } |
| 169 | 174 |
| 170 bool DataUseTracker::GetUmaWeeklyQuota(int* uma_weekly_quota_bytes) const { | 175 bool DataUseTracker::GetUmaWeeklyQuota(int* uma_weekly_quota_bytes) const { |
| 171 DCHECK(thread_checker_.CalledOnValidThread()); | 176 DCHECK(thread_checker_.CalledOnValidThread()); |
| 172 | 177 |
| 173 std::string param_value_str = variations::GetVariationParamValue( | 178 std::string param_value_str = variations::GetVariationParamValue( |
| 174 "UMA_EnableCellularLogUpload", "Uma_Quota"); | 179 "UMA_EnableCellularLogUpload", "Uma_Quota"); |
| 175 if (param_value_str.empty()) | 180 if (param_value_str.empty()) |
| 176 return false; | 181 *uma_weekly_quota_bytes = kDefaultUMAWeeklyQuotaBytes; |
| 177 | 182 else |
| 178 base::StringToInt(param_value_str, uma_weekly_quota_bytes); | 183 base::StringToInt(param_value_str, uma_weekly_quota_bytes); |
| 179 return true; | 184 return true; |
| 180 } | 185 } |
| 181 | 186 |
| 182 bool DataUseTracker::GetUmaRatio(double* ratio) const { | 187 bool DataUseTracker::GetUmaRatio(double* ratio) const { |
| 183 DCHECK(thread_checker_.CalledOnValidThread()); | 188 DCHECK(thread_checker_.CalledOnValidThread()); |
| 184 | 189 |
| 185 std::string param_value_str = variations::GetVariationParamValue( | 190 std::string param_value_str = variations::GetVariationParamValue( |
| 186 "UMA_EnableCellularLogUpload", "Uma_Ratio"); | 191 "UMA_EnableCellularLogUpload", "Uma_Ratio"); |
| 187 if (param_value_str.empty()) | 192 if (param_value_str.empty()) |
| 188 return false; | 193 *ratio = kDefaultUMARatio; |
| 189 base::StringToDouble(param_value_str, ratio); | 194 else |
| 195 base::StringToDouble(param_value_str, ratio); | |
| 190 return true; | 196 return true; |
| 191 } | 197 } |
| 192 | 198 |
| 193 base::Time DataUseTracker::GetCurrentMeasurementDate() const { | 199 base::Time DataUseTracker::GetCurrentMeasurementDate() const { |
| 194 return base::Time::Now().LocalMidnight(); | 200 return base::Time::Now().LocalMidnight(); |
| 195 } | 201 } |
| 196 | 202 |
| 197 std::string DataUseTracker::GetCurrentMeasurementDateAsString() const { | 203 std::string DataUseTracker::GetCurrentMeasurementDateAsString() const { |
| 198 DCHECK(thread_checker_.CalledOnValidThread()); | 204 DCHECK(thread_checker_.CalledOnValidThread()); |
| 199 | 205 |
| 200 base::Time::Exploded today_exploded; | 206 base::Time::Exploded today_exploded; |
| 201 GetCurrentMeasurementDate().LocalExplode(&today_exploded); | 207 GetCurrentMeasurementDate().LocalExplode(&today_exploded); |
| 202 return base::StringPrintf("%04d-%02d-%02d", today_exploded.year, | 208 return base::StringPrintf("%04d-%02d-%02d", today_exploded.year, |
| 203 today_exploded.month, today_exploded.day_of_month); | 209 today_exploded.month, today_exploded.day_of_month); |
| 204 } | 210 } |
| 205 | 211 |
| 206 } // namespace metrics | 212 } // namespace metrics |
| OLD | NEW |