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

Unified Diff: components/metrics/data_use_tracker.h

Issue 1818613002: Implement UMA log throttling for cellular connections (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix param order 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.h
diff --git a/components/metrics/data_use_tracker.h b/components/metrics/data_use_tracker.h
new file mode 100644
index 0000000000000000000000000000000000000000..2f80b4328fd49e364f1997c59a64c0e3b3177715
--- /dev/null
+++ b/components/metrics/data_use_tracker.h
@@ -0,0 +1,88 @@
+// 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.
+
+#ifndef COMPONENTS_METRICS_DATA_USE_TRACKER_H_
+#define COMPONENTS_METRICS_DATA_USE_TRACKER_H_
+
+#include "base/callback.h"
+#include "base/gtest_prod_util.h"
+#include "base/memory/weak_ptr.h"
+#include "components/prefs/pref_registry_simple.h"
+#include "components/prefs/pref_service.h"
+
+namespace metrics {
+
+typedef base::Callback<void(const std::string&, int)>
+ UpdateUsagePrefCallbackType;
+
+// Records the data use of user traffic and UMA traffic in user prefs. Taking
+// into account those prefs it can verify whether certain UMA log upload is
+// allowed.
+class DataUseTracker {
+ public:
+ explicit DataUseTracker(PrefService* local_state);
+ ~DataUseTracker();
+
+ // Registers data use prefs using provided |registry|.
+ static void RegisterPrefs(PrefRegistrySimple* registry);
+
+ // Returns a callback to data use pref updating function attached to
+ // |data_use_tracker| weak pointer. Should be called on UI thread.
+ UpdateUsagePrefCallbackType GetDataUseForwardingCallback(
+ scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
+
+ // Returns whether a log with provided |log_bytes| can be uploaded according
+ // to data use ratio and UMA quota provided by finch experiment.
+ bool CanUploadUMALog(int log_bytes);
+
+ base::WeakPtr<DataUseTracker> GetWeakPtr();
Alexei Svitkine (slow) 2016/03/31 20:59:31 Is this needed anymore?
gayane -on leave until 09-2017 2016/03/31 21:55:10 Done.
+
+ private:
+ FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckUpdateUsagePref);
+ FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckRemoveExpiredEntries);
+ FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckComputeTotalDataUse);
+ FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckCanUploadUMALog);
+
+ // Updates data usage prefs on UI thread according to what Prefservice
+ // expects.
+ void UpdateMetricsUsagePrefsOnUIThread(const std::string& service_name,
+ int message_size);
+
+ // Updates provided |pref_name| for a current date with the given message
+ // size.
+ void UpdateUsagePref(const std::string& pref_name, int message_size);
+
+ // Removes entries from the all data use prefs.
+ void RemoveExpiredEntries();
+
+ // Removes entries from the given |pref_name| if they are more than 7 days
+ // old.
+ void RemoveExpiredEntriesForPref(const std::string& pref_name);
+
+ // Returns the current date as a string with a proper formatting.
+ std::string GetCurrentMeasurementDate();
+
+ // Computes data usage according to all the entries in the given dictionary
+ // pref.
+ int ComputeTotalDataUse(std::string pref_name);
+
+ bool GetUmaQuota(int* quota);
+ bool GetUmaRatio(double* ratio);
+
+ // Only for testing.
+ void SetMeasurementDateForTesting(const std::string& date);
+ void SetUmaQuotaForTesting(int uma_quota);
+ void SetUmaRatioForTesting(double uma_ratio);
+
+ PrefService* local_state_;
+
+ std::string date_for_testing_;
+ int uma_quota_for_testing_;
+ double uma_ratio_for_testing_;
+
+ base::WeakPtrFactory<DataUseTracker> weak_ptr_factory_;
+};
Alexei Svitkine (slow) 2016/03/31 20:59:31 Add DISALLOW_COPY_AND_ASSIGN()
gayane -on leave until 09-2017 2016/03/31 21:55:09 Done.
+
+} // namespace metrics
+#endif // COMPONENTS_METRICS_DATA_USE_TRACKER_H_

Powered by Google App Engine
This is Rietveld 408576698