data_use_tracker.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #ifndef COMPONENTS_METRICS_DATA_USE_TRACKER_H_
  5. #define COMPONENTS_METRICS_DATA_USE_TRACKER_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "base/gtest_prod_util.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/sequence_checker.h"
  11. #include "base/time/time.h"
  12. #include "components/prefs/pref_registry_simple.h"
  13. #include "components/prefs/pref_service.h"
  14. namespace metrics {
  15. // Records the data use of user traffic and UMA traffic in user prefs. Taking
  16. // into account those prefs it can verify whether certain UMA log upload is
  17. // allowed.
  18. class DataUseTracker {
  19. public:
  20. explicit DataUseTracker(PrefService* local_state);
  21. DataUseTracker(const DataUseTracker&) = delete;
  22. DataUseTracker& operator=(const DataUseTracker&) = delete;
  23. virtual ~DataUseTracker();
  24. // Returns an instance of |DataUseTracker| with provided |local_state| if
  25. // users data use should be tracked and null pointer otherwise.
  26. static std::unique_ptr<DataUseTracker> Create(PrefService* local_state);
  27. // Registers data use prefs using provided |registry|.
  28. static void RegisterPrefs(PrefRegistrySimple* registry);
  29. // Updates data usage tracking prefs with the specified values.
  30. static void UpdateMetricsUsagePrefs(int message_size,
  31. bool is_cellular,
  32. bool is_metrics_service_usage,
  33. PrefService* local_state);
  34. // Returns whether a log with provided |log_bytes| can be uploaded according
  35. // to data use ratio and UMA quota provided by variations.
  36. bool ShouldUploadLogOnCellular(int log_bytes);
  37. private:
  38. FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckUpdateUsagePref);
  39. FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckRemoveExpiredEntries);
  40. FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckComputeTotalDataUse);
  41. FRIEND_TEST_ALL_PREFIXES(DataUseTrackerTest, CheckCanUploadUMALog);
  42. // Updates data usage tracking prefs with the specified values.
  43. void UpdateMetricsUsagePrefsInternal(int message_size,
  44. bool is_cellular,
  45. bool is_metrics_service_usage);
  46. // Updates provided |pref_name| for a current date with the given message
  47. // size.
  48. void UpdateUsagePref(const std::string& pref_name, int message_size);
  49. // Removes entries from the all data use prefs.
  50. void RemoveExpiredEntries();
  51. // Removes entries from the given |pref_name| if they are more than 7 days
  52. // old.
  53. void RemoveExpiredEntriesForPref(const std::string& pref_name);
  54. // Computes data usage according to all the entries in the given dictionary
  55. // pref.
  56. int ComputeTotalDataUse(const std::string& pref_name);
  57. // Returns the weekly allowed quota for UMA data use.
  58. virtual bool GetUmaWeeklyQuota(int* uma_weekly_quota_bytes) const;
  59. // Returns the allowed ratio for UMA data use over overall data use.
  60. virtual bool GetUmaRatio(double* ratio) const;
  61. // Returns the current date for measurement.
  62. virtual base::Time GetCurrentMeasurementDate() const;
  63. // Returns the current date as a string with a proper formatting.
  64. virtual std::string GetCurrentMeasurementDateAsString() const;
  65. raw_ptr<PrefService> local_state_;
  66. SEQUENCE_CHECKER(sequence_checker_);
  67. };
  68. } // namespace metrics
  69. #endif // COMPONENTS_METRICS_DATA_USE_TRACKER_H_