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

Side by Side Diff: components/metrics/data_use_tracker.h

Issue 2517363002: Decouple Metrics Initialization from IO Thread Initialization (Closed)
Patch Set: Fix Unit Test Created 4 years 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef COMPONENTS_METRICS_DATA_USE_TRACKER_H_ 5 #ifndef COMPONENTS_METRICS_DATA_USE_TRACKER_H_
6 #define COMPONENTS_METRICS_DATA_USE_TRACKER_H_ 6 #define COMPONENTS_METRICS_DATA_USE_TRACKER_H_
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/gtest_prod_util.h" 11 #include "base/gtest_prod_util.h"
12 #include "base/macros.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" 13 #include "base/threading/thread_checker.h"
17 #include "base/time/time.h" 14 #include "base/time/time.h"
18 #include "components/prefs/pref_registry_simple.h" 15 #include "components/prefs/pref_registry_simple.h"
19 #include "components/prefs/pref_service.h" 16 #include "components/prefs/pref_service.h"
20 17
21 namespace metrics { 18 namespace metrics {
22 19
23 typedef base::Callback<void(const std::string&, int, bool)> 20 typedef base::Callback<void(const std::string&, int, bool)>
24 UpdateUsagePrefCallbackType; 21 UpdateUsagePrefCallbackType;
25 22
26 // Records the data use of user traffic and UMA traffic in user prefs. Taking 23 // 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 24 // into account those prefs it can verify whether certain UMA log upload is
28 // allowed. 25 // allowed.
29 class DataUseTracker { 26 class DataUseTracker {
30 public: 27 public:
31 explicit DataUseTracker(PrefService* local_state); 28 explicit DataUseTracker(PrefService* local_state);
32 ~DataUseTracker(); 29 ~DataUseTracker();
33 30
34 // Returns an instance of |DataUseTracker| with provided |local_state| if 31 // Returns an instance of |DataUseTracker| with provided |local_state| if
35 // users data use should be tracked and null pointer otherwise. 32 // users data use should be tracked and null pointer otherwise.
36 static std::unique_ptr<DataUseTracker> Create(PrefService* local_state); 33 static std::unique_ptr<DataUseTracker> Create(PrefService* local_state);
37 34
38 // Registers data use prefs using provided |registry|. 35 // Registers data use prefs using provided |registry|.
39 static void RegisterPrefs(PrefRegistrySimple* registry); 36 static void RegisterPrefs(PrefRegistrySimple* registry);
40 37
41 // Returns a callback to data use pref updating function. Should be called on 38 // Updates data usage prefs according to what the PrefService expects.
Alexei Svitkine (slow) 2016/11/30 19:16:27 "according to what the PrefService expects" doesn'
robliao 2016/11/30 20:33:13 Done.
42 // UI thread. 39 void UpdateMetricsUsagePrefs(
43 UpdateUsagePrefCallbackType GetDataUseForwardingCallback( 40 const std::string& service_name, int message_size, bool is_cellular);
44 scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
45 41
46 // Returns whether a log with provided |log_bytes| can be uploaded according 42 // Returns whether a log with provided |log_bytes| can be uploaded according
47 // to data use ratio and UMA quota provided by variations. 43 // to data use ratio and UMA quota provided by variations.
48 bool ShouldUploadLogOnCellular(int log_bytes); 44 bool ShouldUploadLogOnCellular(int log_bytes);
49 45
50 private: 46 private:
51 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckUpdateUsagePref); 47 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckUpdateUsagePref);
52 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckRemoveExpiredEntries); 48 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckRemoveExpiredEntries);
53 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckComputeTotalDataUse); 49 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckComputeTotalDataUse);
54 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckCanUploadUMALog); 50 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckCanUploadUMALog);
55 51
56 // Updates data usage prefs on UI thread according to what Prefservice
57 // expects.
58 void UpdateMetricsUsagePrefsOnUIThread(const std::string& service_name,
59 int message_size,
60 bool is_cellular);
61 52
Alexei Svitkine (slow) 2016/11/30 19:16:27 Nit: Remove extra line.
robliao 2016/11/30 20:33:13 Done.
62 // Updates provided |pref_name| for a current date with the given message 53 // Updates provided |pref_name| for a current date with the given message
63 // size. 54 // size.
64 void UpdateUsagePref(const std::string& pref_name, int message_size); 55 void UpdateUsagePref(const std::string& pref_name, int message_size);
65 56
66 // Removes entries from the all data use prefs. 57 // Removes entries from the all data use prefs.
67 void RemoveExpiredEntries(); 58 void RemoveExpiredEntries();
68 59
69 // Removes entries from the given |pref_name| if they are more than 7 days 60 // Removes entries from the given |pref_name| if they are more than 7 days
70 // old. 61 // old.
71 void RemoveExpiredEntriesForPref(const std::string& pref_name); 62 void RemoveExpiredEntriesForPref(const std::string& pref_name);
(...skipping 11 matching lines...) Expand all
83 // Returns the current date for measurement. 74 // Returns the current date for measurement.
84 virtual base::Time GetCurrentMeasurementDate() const; 75 virtual base::Time GetCurrentMeasurementDate() const;
85 76
86 // Returns the current date as a string with a proper formatting. 77 // Returns the current date as a string with a proper formatting.
87 virtual std::string GetCurrentMeasurementDateAsString() const; 78 virtual std::string GetCurrentMeasurementDateAsString() const;
88 79
89 PrefService* local_state_; 80 PrefService* local_state_;
90 81
91 base::ThreadChecker thread_checker_; 82 base::ThreadChecker thread_checker_;
92 83
93 base::WeakPtrFactory<DataUseTracker> weak_ptr_factory_;
94
95 DISALLOW_COPY_AND_ASSIGN(DataUseTracker); 84 DISALLOW_COPY_AND_ASSIGN(DataUseTracker);
96 }; 85 };
97 86
98 } // namespace metrics 87 } // namespace metrics
99 #endif // COMPONENTS_METRICS_DATA_USE_TRACKER_H_ 88 #endif // COMPONENTS_METRICS_DATA_USE_TRACKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698