| OLD | NEW |
| 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 tracking prefs with the specified values. |
| 42 // UI thread. | 39 void UpdateMetricsUsagePrefs(const std::string& service_name, |
| 43 UpdateUsagePrefCallbackType GetDataUseForwardingCallback( | 40 int message_size, |
| 44 scoped_refptr<base::SequencedTaskRunner> ui_task_runner); | 41 bool is_cellular); |
| 45 | 42 |
| 46 // Returns whether a log with provided |log_bytes| can be uploaded according | 43 // Returns whether a log with provided |log_bytes| can be uploaded according |
| 47 // to data use ratio and UMA quota provided by variations. | 44 // to data use ratio and UMA quota provided by variations. |
| 48 bool ShouldUploadLogOnCellular(int log_bytes); | 45 bool ShouldUploadLogOnCellular(int log_bytes); |
| 49 | 46 |
| 50 private: | 47 private: |
| 51 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckUpdateUsagePref); | 48 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckUpdateUsagePref); |
| 52 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckRemoveExpiredEntries); | 49 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckRemoveExpiredEntries); |
| 53 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckComputeTotalDataUse); | 50 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckComputeTotalDataUse); |
| 54 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckCanUploadUMALog); | 51 FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckCanUploadUMALog); |
| 55 | 52 |
| 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 | 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 Loading... |
| 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_ |
| OLD | NEW |