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 for Android. | 19 // Default weekly quota and allowed UMA ratio for UMA log uploads for Android. |
| 20 // These defaults will not be used for non-Android as |DataUseTracker| will not | 20 // These defaults will not be used for non-Android as |DataUseTracker| will not |
| 21 // be initialized. Default values can be overridden by variation params. | 21 // be initialized. Default values can be overridden by variation params. |
| 22 const int kDefaultUMAWeeklyQuotaBytes = 204800; | 22 const int kDefaultUMAWeeklyQuotaBytes = 204800; |
| 23 const double kDefaultUMARatio = 0.05; | 23 const double kDefaultUMARatio = 0.05; |
| 24 | 24 |
| 25 struct CellDataUsePrefs { | |
| 26 const MetricsLogUploader::MetricServiceType service_type; | |
| 27 const char* service_name; | |
| 28 const char* pref_name; | |
| 29 }; | |
| 30 | |
| 31 const CellDataUsePrefs data_use_prefs[] = { | |
| 32 {MetricsLogUploader::UMA, "UMA", metrics::prefs::kUmaCellDataUse}, | |
| 33 {MetricsLogUploader::UKM, "UKM", metrics::prefs::kUkmCellDataUse}, | |
| 34 }; | |
| 35 | |
| 36 const char* GetPrefByServiceName(const std::string& service_name) { | |
| 37 for (const auto& pref : data_use_prefs) { | |
| 38 if (service_name == pref.service_name) | |
| 39 return pref.pref_name; | |
| 40 } | |
|
Alexei Svitkine (slow)
2017/03/24 16:26:16
Nit: Add a NOTREACHED()?
Steven Holte
2017/03/24 18:13:29
It is reached, in most cases.
| |
| 41 return NULL; | |
|
Alexei Svitkine (slow)
2017/03/24 16:26:17
nullptr
Steven Holte
2017/03/24 18:13:29
Done.
| |
| 42 } | |
| 43 | |
| 44 const char* GetPrefByServiceType( | |
| 45 const MetricsLogUploader::MetricServiceType service_type) { | |
|
Alexei Svitkine (slow)
2017/03/24 16:26:17
Nit: No need for const on the param since it's an
Steven Holte
2017/03/24 18:13:29
Done.
| |
| 46 for (const auto& pref : data_use_prefs) { | |
| 47 if (service_type == pref.service_type) | |
| 48 return pref.pref_name; | |
| 49 } | |
|
Alexei Svitkine (slow)
2017/03/24 16:26:17
Nit: Add a NOTREACHED()?
Steven Holte
2017/03/24 18:13:29
It is reached, in most cases.
Steven Holte
2017/03/24 18:16:36
Actually, this one probably won't be reached.
Steven Holte
2017/03/24 18:20:13
Added a DCHECK for non-null return to ShouldUpload
| |
| 50 return NULL; | |
|
Alexei Svitkine (slow)
2017/03/24 16:26:16
nullptr
Steven Holte
2017/03/24 18:13:29
Done.
| |
| 51 } | |
| 52 | |
| 25 } // namespace | 53 } // namespace |
| 26 | 54 |
| 27 DataUseTracker::DataUseTracker(PrefService* local_state) | 55 DataUseTracker::DataUseTracker(PrefService* local_state) |
| 28 : local_state_(local_state) {} | 56 : local_state_(local_state) {} |
| 29 | 57 |
| 30 DataUseTracker::~DataUseTracker() {} | 58 DataUseTracker::~DataUseTracker() {} |
| 31 | 59 |
| 32 // static | 60 // static |
| 33 std::unique_ptr<DataUseTracker> DataUseTracker::Create( | 61 std::unique_ptr<DataUseTracker> DataUseTracker::Create( |
| 34 PrefService* local_state) { | 62 PrefService* local_state) { |
| 35 std::unique_ptr<DataUseTracker> data_use_tracker; | 63 std::unique_ptr<DataUseTracker> data_use_tracker; |
| 36 #if defined(OS_ANDROID) | 64 #if defined(OS_ANDROID) |
| 37 data_use_tracker.reset(new DataUseTracker(local_state)); | 65 data_use_tracker.reset(new DataUseTracker(local_state)); |
| 38 #endif | 66 #endif |
| 39 return data_use_tracker; | 67 return data_use_tracker; |
| 40 } | 68 } |
| 41 | 69 |
| 42 // static | 70 // static |
| 43 void DataUseTracker::RegisterPrefs(PrefRegistrySimple* registry) { | 71 void DataUseTracker::RegisterPrefs(PrefRegistrySimple* registry) { |
| 44 registry->RegisterDictionaryPref(metrics::prefs::kUserCellDataUse); | 72 registry->RegisterDictionaryPref(metrics::prefs::kUserCellDataUse); |
| 45 registry->RegisterDictionaryPref(metrics::prefs::kUmaCellDataUse); | 73 for (const auto& pref : data_use_prefs) { |
| 74 registry->RegisterDictionaryPref(pref.pref_name); | |
| 75 } | |
| 46 } | 76 } |
| 47 | 77 |
| 48 void DataUseTracker::UpdateMetricsUsagePrefs(const std::string& service_name, | 78 void DataUseTracker::UpdateMetricsUsagePrefs(const std::string& service_name, |
| 49 int message_size, | 79 int message_size, |
| 50 bool is_cellular) { | 80 bool is_cellular) { |
| 51 DCHECK(thread_checker_.CalledOnValidThread()); | 81 DCHECK(thread_checker_.CalledOnValidThread()); |
| 52 | 82 |
| 53 if (!is_cellular) | 83 if (!is_cellular) |
| 54 return; | 84 return; |
| 55 | 85 |
| 56 UpdateUsagePref(prefs::kUserCellDataUse, message_size); | 86 UpdateUsagePref(prefs::kUserCellDataUse, message_size); |
| 57 if (service_name == "UMA") | 87 const char* pref_name = GetPrefByServiceName(service_name); |
| 58 UpdateUsagePref(prefs::kUmaCellDataUse, message_size); | 88 if (pref_name) |
| 89 UpdateUsagePref(pref_name, message_size); | |
| 59 } | 90 } |
| 60 | 91 |
| 61 bool DataUseTracker::ShouldUploadLogOnCellular(int log_bytes) { | 92 bool DataUseTracker::ShouldUploadLogOnCellular( |
| 93 int log_bytes, | |
| 94 const MetricsLogUploader::MetricServiceType service_type) { | |
|
Alexei Svitkine (slow)
2017/03/24 16:26:17
Nit: Remove const
Steven Holte
2017/03/24 18:13:29
Done.
| |
| 62 DCHECK(thread_checker_.CalledOnValidThread()); | 95 DCHECK(thread_checker_.CalledOnValidThread()); |
| 96 const char* pref_name = GetPrefByServiceType(service_type); | |
| 97 if (!pref_name) | |
| 98 return false; | |
| 63 | 99 |
| 64 RemoveExpiredEntries(); | 100 RemoveExpiredEntries(); |
| 65 | 101 |
| 66 int uma_weekly_quota_bytes; | 102 int uma_weekly_quota_bytes; |
| 67 if (!GetUmaWeeklyQuota(&uma_weekly_quota_bytes)) | 103 if (!GetUmaWeeklyQuota(&uma_weekly_quota_bytes)) |
| 68 return true; | 104 return true; |
| 69 | 105 |
| 70 int uma_total_data_use = ComputeTotalDataUse(prefs::kUmaCellDataUse); | 106 int uma_total_data_use = ComputeTotalDataUse(pref_name); |
| 71 int new_uma_total_data_use = log_bytes + uma_total_data_use; | 107 int new_uma_total_data_use = log_bytes + uma_total_data_use; |
| 72 // If the new log doesn't increase the total UMA traffic to be above the | 108 // If the new log doesn't increase the total UMA traffic to be above the |
| 73 // allowed quota then the log should be uploaded. | 109 // allowed quota then the log should be uploaded. |
| 74 if (new_uma_total_data_use <= uma_weekly_quota_bytes) | 110 if (new_uma_total_data_use <= uma_weekly_quota_bytes) |
| 75 return true; | 111 return true; |
| 76 | 112 |
| 77 double uma_ratio; | 113 double uma_ratio; |
| 78 if (!GetUmaRatio(&uma_ratio)) | 114 if (!GetUmaRatio(&uma_ratio)) |
| 79 return true; | 115 return true; |
| 80 | 116 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 95 std::string todays_key = GetCurrentMeasurementDateAsString(); | 131 std::string todays_key = GetCurrentMeasurementDateAsString(); |
| 96 | 132 |
| 97 const base::DictionaryValue* user_pref_dict = | 133 const base::DictionaryValue* user_pref_dict = |
| 98 local_state_->GetDictionary(pref_name); | 134 local_state_->GetDictionary(pref_name); |
| 99 user_pref_dict->GetInteger(todays_key, &todays_traffic); | 135 user_pref_dict->GetInteger(todays_key, &todays_traffic); |
| 100 pref_updater->SetInteger(todays_key, todays_traffic + message_size); | 136 pref_updater->SetInteger(todays_key, todays_traffic + message_size); |
| 101 } | 137 } |
| 102 | 138 |
| 103 void DataUseTracker::RemoveExpiredEntries() { | 139 void DataUseTracker::RemoveExpiredEntries() { |
| 104 DCHECK(thread_checker_.CalledOnValidThread()); | 140 DCHECK(thread_checker_.CalledOnValidThread()); |
| 105 RemoveExpiredEntriesForPref(prefs::kUmaCellDataUse); | 141 for (const auto& pref : data_use_prefs) { |
| 142 RemoveExpiredEntriesForPref(pref.pref_name); | |
| 143 } | |
| 106 RemoveExpiredEntriesForPref(prefs::kUserCellDataUse); | 144 RemoveExpiredEntriesForPref(prefs::kUserCellDataUse); |
| 107 } | 145 } |
| 108 | 146 |
| 109 void DataUseTracker::RemoveExpiredEntriesForPref(const std::string& pref_name) { | 147 void DataUseTracker::RemoveExpiredEntriesForPref(const std::string& pref_name) { |
| 110 DCHECK(thread_checker_.CalledOnValidThread()); | 148 DCHECK(thread_checker_.CalledOnValidThread()); |
| 111 | 149 |
| 112 const base::DictionaryValue* user_pref_dict = | 150 const base::DictionaryValue* user_pref_dict = |
| 113 local_state_->GetDictionary(pref_name); | 151 local_state_->GetDictionary(pref_name); |
| 114 const base::Time current_date = GetCurrentMeasurementDate(); | 152 const base::Time current_date = GetCurrentMeasurementDate(); |
| 115 const base::Time week_ago = current_date - base::TimeDelta::FromDays(7); | 153 const base::Time week_ago = current_date - base::TimeDelta::FromDays(7); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 std::string DataUseTracker::GetCurrentMeasurementDateAsString() const { | 213 std::string DataUseTracker::GetCurrentMeasurementDateAsString() const { |
| 176 DCHECK(thread_checker_.CalledOnValidThread()); | 214 DCHECK(thread_checker_.CalledOnValidThread()); |
| 177 | 215 |
| 178 base::Time::Exploded today_exploded; | 216 base::Time::Exploded today_exploded; |
| 179 GetCurrentMeasurementDate().LocalExplode(&today_exploded); | 217 GetCurrentMeasurementDate().LocalExplode(&today_exploded); |
| 180 return base::StringPrintf("%04d-%02d-%02d", today_exploded.year, | 218 return base::StringPrintf("%04d-%02d-%02d", today_exploded.year, |
| 181 today_exploded.month, today_exploded.day_of_month); | 219 today_exploded.month, today_exploded.day_of_month); |
| 182 } | 220 } |
| 183 | 221 |
| 184 } // namespace metrics | 222 } // namespace metrics |
| OLD | NEW |