Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Unified Diff: components/metrics/data_use_tracker.cc

Issue 1818613002: Implement UMA log throttling for cellular connections (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/metrics/data_use_tracker.cc
diff --git a/components/metrics/data_use_tracker.cc b/components/metrics/data_use_tracker.cc
new file mode 100644
index 0000000000000000000000000000000000000000..47322d13a1007f07cd516a2c51a545d61ba4dc9f
--- /dev/null
+++ b/components/metrics/data_use_tracker.cc
@@ -0,0 +1,179 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/metrics/data_use_tracker.h"
+
+#include <string>
+
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/stringprintf.h"
+#include "components/metrics/metrics_pref_names.h"
+#include "components/prefs/scoped_user_pref_update.h"
+#include "components/variations/variations_associated_data.h"
+
+namespace metrics {
+
+namespace {
+
+// This function is for forwarding metrics usage pref changes to the appropriate
+// callback on the appropriate thread.
+void UpdateMetricsUsagePrefs(
+ const UpdateUsagePrefCallbackType& update_on_ui_callback,
+ scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
+ const std::string& service_name,
+ int message_size) {
+ ui_task_runner->PostTask(
+ FROM_HERE, base::Bind(update_on_ui_callback, service_name, message_size));
+}
+
+} // namespace
+
+DataUseTracker::DataUseTracker(PrefService* local_state)
+ : local_state_(local_state),
+ uma_quota_for_testing_(0),
+ uma_ratio_for_testing_(0),
+ weak_ptr_factory_(this) {}
+
+DataUseTracker::~DataUseTracker() {}
+
+// static
+void DataUseTracker::RegisterPrefs(PrefRegistrySimple* registry) {
+ registry->RegisterDictionaryPref(metrics::prefs::kUserCellDataUse);
+ registry->RegisterDictionaryPref(metrics::prefs::kUmaCellDataUse);
+}
+
+UpdateUsagePrefCallbackType DataUseTracker::GetDataUseForwardingCallback(
+ scoped_refptr<base::SequencedTaskRunner> ui_task_runner) {
+ DCHECK(ui_task_runner->RunsTasksOnCurrentThread());
+ return base::Bind(
+ &UpdateMetricsUsagePrefs,
+ base::Bind(&DataUseTracker::UpdateMetricsUsagePrefsOnUIThread,
+ weak_ptr_factory_.GetWeakPtr()),
+ ui_task_runner);
+}
+
+bool DataUseTracker::CanUploadUMALog(int log_bytes) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ RemoveExpiredEntries();
+
+ 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.
+ if (!GetUmaQuota(&uma_quota))
+ return true;
+
+ int uma_total_data_use = ComputeTotalDataUse(prefs::kUmaCellDataUse);
+ 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.
+ return true;
+
+ double uma_ratio;
+ if (!GetUmaRatio(&uma_ratio))
+ return true;
+
+ 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.
+ return (log_bytes + uma_total_data_use) /
+ static_cast<double>(log_bytes + user_total_data_use) <=
+ uma_ratio;
+}
+
+void DataUseTracker::UpdateMetricsUsagePrefsOnUIThread(
+ const std::string& service_name,
+ int message_size) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ UpdateUsagePref(prefs::kUserCellDataUse, message_size);
+ if (service_name == "UMA")
+ UpdateUsagePref(prefs::kUmaCellDataUse, message_size);
+}
+
+void DataUseTracker::UpdateUsagePref(const std::string& pref_name,
+ int message_size) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DictionaryPrefUpdate pref_updater(local_state_, pref_name);
+ int todays_traffic = 0;
+ std::string todays_key = GetCurrentMeasurementDateAsString();
+
+ const base::DictionaryValue* user_pref_dict =
+ local_state_->GetDictionary(pref_name);
+ 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.
+ user_pref_dict->GetInteger(todays_key, &todays_traffic);
+ }
+
+ pref_updater->SetInteger(todays_key, todays_traffic + message_size);
+}
+
+void DataUseTracker::RemoveExpiredEntries() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ RemoveExpiredEntriesForPref(prefs::kUmaCellDataUse);
+ RemoveExpiredEntriesForPref(prefs::kUserCellDataUse);
+}
+
+void DataUseTracker::RemoveExpiredEntriesForPref(const std::string& pref_name) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ const base::DictionaryValue* user_pref_dict =
+ local_state_->GetDictionary(pref_name);
+ if (!user_pref_dict)
+ return;
+
+ base::DictionaryValue user_pref_new_dict;
+ for (base::DictionaryValue::Iterator it(*user_pref_dict); !it.IsAtEnd();
+ it.Advance()) {
+ base::Time key_date;
+ base::Time::FromUTCString(it.key().c_str(), &key_date);
+ base::Time current_date = GetCurrentMeasurementDate();
+ 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.
+
+ if (key_date > week_ago)
+ user_pref_new_dict.Set(it.key(), it.value().CreateDeepCopy());
+ }
+ local_state_->Set(pref_name, user_pref_new_dict);
+}
+
+int DataUseTracker::ComputeTotalDataUse(std::string pref_name) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ int total_data_use = 0;
+ const base::DictionaryValue* pref_dict =
+ local_state_->GetDictionary(pref_name);
+ if (!pref_dict)
+ return total_data_use;
+ for (base::DictionaryValue::Iterator it(*pref_dict); !it.IsAtEnd();
+ it.Advance()) {
+ int value = 0;
+ it.value().GetAsInteger(&value);
+ total_data_use += value;
+ }
+ return total_data_use;
+}
+
+bool DataUseTracker::GetUmaQuota(int* quota) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ std::string param_value_str = variations::GetVariationParamValue(
+ "UMA_EnableCellularLogUpload", "Uma_Quota");
+ if (param_value_str.empty())
+ return false;
+
+ base::StringToInt(param_value_str, quota);
+ return true;
+}
+
+bool DataUseTracker::GetUmaRatio(double* ratio) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ std::string param_value_str = variations::GetVariationParamValue(
+ "UMA_EnableCellularLogUpload", "Uma_Ratio");
+ if (param_value_str.empty())
+ return false;
+ base::StringToDouble(param_value_str, ratio);
+ return true;
+}
+
+base::Time DataUseTracker::GetCurrentMeasurementDate() {
+ return base::Time::Now().LocalMidnight();
+}
+
+std::string DataUseTracker::GetCurrentMeasurementDateAsString() {
+ 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.
+ base::Time::Exploded today_exploded;
+ GetCurrentMeasurementDate().LocalExplode(&today_exploded);
+ return base::StringPrintf("%04d-%02d-%02d", today_exploded.year,
+ today_exploded.month, today_exploded.day_of_month);
+}
+
+} // namespace metrics

Powered by Google App Engine
This is Rietveld 408576698