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

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 // Returns an instance of |DataUseTracker| with provided |local_state|.
Alexei Svitkine (slow) 2016/04/07 15:43:29 Add a sentence about why it may not create one.
gayane -on leave until 09-2017 2016/04/07 18:53:49 Done.
35 static scoped_ptr<DataUseTracker> Create(PrefService* local_state);
36
37 // Registers data use prefs using provided |registry|.
38 static void RegisterPrefs(PrefRegistrySimple* registry);
39
40 // Returns a callback to data use pref updating function. Should be called on
41 // UI thread.
42 UpdateUsagePrefCallbackType GetDataUseForwardingCallback(
43 scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
44
45 // Returns whether a log with provided |log_bytes| can be uploaded according
46 // to data use ratio and UMA quota provided by variations.
47 bool ShouldUploadLogOnCellular(int log_bytes);
48
49 private:
50 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckUpdateUsagePref);
51 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckRemoveExpiredEntries);
52 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckComputeTotalDataUse);
53 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckCanUploadUMALog);
54
55 // Updates data usage prefs on UI thread according to what Prefservice
56 // expects.
57 void UpdateMetricsUsagePrefsOnUIThread(const std::string& service_name,
58 int message_size,
59 bool is_cellular);
60
61 // Updates provided |pref_name| for a current date with the given message
62 // size.
63 void UpdateUsagePref(const std::string& pref_name, int message_size);
64
65 // Removes entries from the all data use prefs.
66 void RemoveExpiredEntries();
67
68 // Removes entries from the given |pref_name| if they are more than 7 days
69 // old.
70 void RemoveExpiredEntriesForPref(const std::string& pref_name);
71
72 // Computes data usage according to all the entries in the given dictionary
73 // pref.
74 int ComputeTotalDataUse(const std::string& pref_name);
75
76 // Returns the weekly allowed quota for UMA data use.
77 virtual bool GetUmaWeeklyQuota(int* uma_weekly_quota_bytes) const;
78
79 // Returns the allowed ratio for UMA data use over overall data use.
80 virtual bool GetUmaRatio(double* ratio) const;
81
82 // Returns the current date for measurement.
83 virtual base::Time GetCurrentMeasurementDate() const;
84
85 // Returns the current date as a string with a proper formatting.
86 virtual std::string GetCurrentMeasurementDateAsString() const;
87
88 PrefService* local_state_;
89
90 base::ThreadChecker thread_checker_;
91
92 base::WeakPtrFactory<DataUseTracker> weak_ptr_factory_;
93
94 DISALLOW_COPY_AND_ASSIGN(DataUseTracker);
95 };
96
97 } // namespace metrics
98 #endif // COMPONENTS_METRICS_DATA_USE_TRACKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698