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

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

Powered by Google App Engine
This is Rietveld 408576698