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 <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| if |
| 35 // users data use should be tracked and null pointer otherwise. |
| 36 static scoped_ptr<DataUseTracker> Create(PrefService* local_state); |
| 37 |
| 38 // Registers data use prefs using provided |registry|. |
| 39 static void RegisterPrefs(PrefRegistrySimple* registry); |
| 40 |
| 41 // Returns a callback to data use pref updating function. Should be called on |
| 42 // UI thread. |
| 43 UpdateUsagePrefCallbackType GetDataUseForwardingCallback( |
| 44 scoped_refptr<base::SequencedTaskRunner> ui_task_runner); |
| 45 |
| 46 // Returns whether a log with provided |log_bytes| can be uploaded according |
| 47 // to data use ratio and UMA quota provided by variations. |
| 48 bool ShouldUploadLogOnCellular(int log_bytes); |
| 49 |
| 50 private: |
| 51 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckUpdateUsagePref); |
| 52 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckRemoveExpiredEntries); |
| 53 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckComputeTotalDataUse); |
| 54 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckCanUploadUMALog); |
| 55 |
| 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 |
| 62 // Updates provided |pref_name| for a current date with the given message |
| 63 // size. |
| 64 void UpdateUsagePref(const std::string& pref_name, int message_size); |
| 65 |
| 66 // Removes entries from the all data use prefs. |
| 67 void RemoveExpiredEntries(); |
| 68 |
| 69 // Removes entries from the given |pref_name| if they are more than 7 days |
| 70 // old. |
| 71 void RemoveExpiredEntriesForPref(const std::string& pref_name); |
| 72 |
| 73 // Computes data usage according to all the entries in the given dictionary |
| 74 // pref. |
| 75 int ComputeTotalDataUse(const std::string& pref_name); |
| 76 |
| 77 // Returns the weekly allowed quota for UMA data use. |
| 78 virtual bool GetUmaWeeklyQuota(int* uma_weekly_quota_bytes) const; |
| 79 |
| 80 // Returns the allowed ratio for UMA data use over overall data use. |
| 81 virtual bool GetUmaRatio(double* ratio) const; |
| 82 |
| 83 // Returns the current date for measurement. |
| 84 virtual base::Time GetCurrentMeasurementDate() const; |
| 85 |
| 86 // Returns the current date as a string with a proper formatting. |
| 87 virtual std::string GetCurrentMeasurementDateAsString() const; |
| 88 |
| 89 PrefService* local_state_; |
| 90 |
| 91 base::ThreadChecker thread_checker_; |
| 92 |
| 93 base::WeakPtrFactory<DataUseTracker> weak_ptr_factory_; |
| 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(DataUseTracker); |
| 96 }; |
| 97 |
| 98 } // namespace metrics |
| 99 #endif // COMPONENTS_METRICS_DATA_USE_TRACKER_H_ |
OLD | NEW |