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

Side by Side 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, 8 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 unified diff | Download patch
OLDNEW
(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 #ifndef COMPONENTS_METRICS_DATA_USE_TRACKER_H_
6 #define COMPONENTS_METRICS_DATA_USE_TRACKER_H_
7
8 #include "base/callback.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/memory/weak_ptr.h"
11 #include "components/prefs/pref_registry_simple.h"
12 #include "components/prefs/pref_service.h"
13
14 namespace metrics {
15
16 typedef base::Callback<void(const std::string&, int)>
17 UpdateUsagePrefCallbackType;
18
19 // Records the data use of user traffic and UMA traffic in user prefs. Taking
20 // into account those prefs it can verify whether certain UMA log upload is
21 // allowed.
22 class DataUseTracker {
23 public:
24 explicit DataUseTracker(PrefService* local_state);
25 ~DataUseTracker();
26
27 // Registers data use prefs using provided |registry|.
28 static void RegisterPrefs(PrefRegistrySimple* registry);
29
30 // Returns a callback to data use pref updating function attached to
31 // |data_use_tracker| weak pointer. Should be called on UI thread.
32 UpdateUsagePrefCallbackType GetDataUseForwardingCallback(
33 scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
34
35 // Returns whether a log with provided |log_bytes| can be uploaded according
36 // to data use ratio and UMA quota provided by finch experiment.
37 bool CanUploadUMALog(int log_bytes);
38
39 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.
40
41 private:
42 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckUpdateUsagePref);
43 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckRemoveExpiredEntries);
44 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckComputeTotalDataUse);
45 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckCanUploadUMALog);
46
47 // Updates data usage prefs on UI thread according to what Prefservice
48 // expects.
49 void UpdateMetricsUsagePrefsOnUIThread(const std::string& service_name,
50 int message_size);
51
52 // Updates provided |pref_name| for a current date with the given message
53 // size.
54 void UpdateUsagePref(const std::string& pref_name, int message_size);
55
56 // Removes entries from the all data use prefs.
57 void RemoveExpiredEntries();
58
59 // Removes entries from the given |pref_name| if they are more than 7 days
60 // old.
61 void RemoveExpiredEntriesForPref(const std::string& pref_name);
62
63 // Returns the current date as a string with a proper formatting.
64 std::string GetCurrentMeasurementDate();
65
66 // Computes data usage according to all the entries in the given dictionary
67 // pref.
68 int ComputeTotalDataUse(std::string pref_name);
69
70 bool GetUmaQuota(int* quota);
71 bool GetUmaRatio(double* ratio);
72
73 // Only for testing.
74 void SetMeasurementDateForTesting(const std::string& date);
75 void SetUmaQuotaForTesting(int uma_quota);
76 void SetUmaRatioForTesting(double uma_ratio);
77
78 PrefService* local_state_;
79
80 std::string date_for_testing_;
81 int uma_quota_for_testing_;
82 double uma_ratio_for_testing_;
83
84 base::WeakPtrFactory<DataUseTracker> weak_ptr_factory_;
85 };
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.
86
87 } // namespace metrics
88 #endif // COMPONENTS_METRICS_DATA_USE_TRACKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698