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