daily_event.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2014 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_DAILY_EVENT_H_
  5. #define COMPONENTS_METRICS_DAILY_EVENT_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/time/time.h"
  11. class PrefRegistrySimple;
  12. class PrefService;
  13. namespace metrics {
  14. // DailyEvent is used for throttling an event to about once per day, even if
  15. // the program is restarted more frequently. It is based on local machine
  16. // time, so it could be fired more often if the clock is changed.
  17. //
  18. // The service using the DailyEvent should first provide all of the Observers
  19. // for the interval, and then arrange for CheckInterval() to be called
  20. // periodically to test if the event should be fired.
  21. class DailyEvent {
  22. public:
  23. // Different reasons that Observer::OnDailyEvent() is called.
  24. // This enum is used for histograms and must not be renumbered.
  25. enum class IntervalType {
  26. FIRST_RUN,
  27. DAY_ELAPSED,
  28. CLOCK_CHANGED,
  29. kMaxValue = CLOCK_CHANGED,
  30. };
  31. // Observer receives notifications from a DailyEvent.
  32. // Observers must be added before the DailyEvent begins checking time,
  33. // and will be owned by the DailyEvent.
  34. class Observer {
  35. public:
  36. Observer();
  37. Observer(const Observer&) = delete;
  38. Observer& operator=(const Observer&) = delete;
  39. virtual ~Observer();
  40. // Called when the daily event is fired.
  41. virtual void OnDailyEvent(IntervalType type) = 0;
  42. };
  43. // Constructs DailyEvent monitor which stores the time it last fired in the
  44. // preference |pref_name|. |pref_name| should be registered by calling
  45. // RegisterPref before using this object.
  46. // Caller is responsible for ensuring that |pref_service| and |pref_name|
  47. // outlive the DailyEvent.
  48. // |histogram_name| is the name of the UMA metric which record when this
  49. // interval fires, and should be registered in histograms.xml. If
  50. // |histogram_name| is empty - interval fires are not recorded.
  51. DailyEvent(PrefService* pref_service,
  52. const char* pref_name,
  53. const std::string& histogram_name);
  54. DailyEvent(const DailyEvent&) = delete;
  55. DailyEvent& operator=(const DailyEvent&) = delete;
  56. ~DailyEvent();
  57. // Adds a observer to be notified when a day elapses. All observers should
  58. // be registered before the the DailyEvent starts checking time.
  59. void AddObserver(std::unique_ptr<Observer> observer);
  60. // Checks if a day has elapsed. If it has, OnDailyEvent will be called on
  61. // all observers.
  62. void CheckInterval();
  63. // Registers the preference used by this interval.
  64. static void RegisterPref(PrefRegistrySimple* registry,
  65. const std::string& pref_name);
  66. private:
  67. // Handles an interval elapsing because of |type|.
  68. void OnInterval(base::Time now, IntervalType type);
  69. // A weak pointer to the PrefService object to read and write preferences
  70. // from. Calling code should ensure this object continues to exist for the
  71. // lifetime of the DailyEvent object.
  72. raw_ptr<PrefService> pref_service_;
  73. // The name of the preference to store the last fired time in.
  74. // Calling code should ensure this outlives the DailyEvent.
  75. const char* pref_name_;
  76. // The name of the histogram to record intervals.
  77. std::string histogram_name_;
  78. // A list of observers.
  79. std::vector<std::unique_ptr<Observer>> observers_;
  80. // The time that the daily event was last fired.
  81. base::Time last_fired_;
  82. };
  83. } // namespace metrics
  84. #endif // COMPONENTS_METRICS_DAILY_EVENT_H_