1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_METRICS_DAILY_EVENT_H_
6 #define COMPONENTS_METRICS_DAILY_EVENT_H_
12 #include "base/memory/raw_ptr.h"
13 #include "base/time/time.h"
15 class PrefRegistrySimple;
20 // DailyEvent is used for throttling an event to about once per day, even if
21 // the program is restarted more frequently. It is based on local machine
22 // time, so it could be fired more often if the clock is changed.
24 // The service using the DailyEvent should first provide all of the Observers
25 // for the interval, and then arrange for CheckInterval() to be called
26 // periodically to test if the event should be fired.
29 // Different reasons that Observer::OnDailyEvent() is called.
30 // This enum is used for histograms and must not be renumbered.
31 enum class IntervalType {
35 kMaxValue = CLOCK_CHANGED,
38 // Observer receives notifications from a DailyEvent.
39 // Observers must be added before the DailyEvent begins checking time,
40 // and will be owned by the DailyEvent.
45 Observer(const Observer&) = delete;
46 Observer& operator=(const Observer&) = delete;
50 // Called when the daily event is fired.
51 virtual void OnDailyEvent(IntervalType type) = 0;
54 // Constructs DailyEvent monitor which stores the time it last fired in the
55 // preference |pref_name|. |pref_name| should be registered by calling
56 // RegisterPref before using this object.
57 // Caller is responsible for ensuring that |pref_service| and |pref_name|
58 // outlive the DailyEvent.
59 // |histogram_name| is the name of the UMA metric which record when this
60 // interval fires, and should be registered in histograms.xml. If
61 // |histogram_name| is empty - interval fires are not recorded.
62 DailyEvent(PrefService* pref_service,
63 const char* pref_name,
64 const std::string& histogram_name);
66 DailyEvent(const DailyEvent&) = delete;
67 DailyEvent& operator=(const DailyEvent&) = delete;
71 // Adds a observer to be notified when a day elapses. All observers should
72 // be registered before the the DailyEvent starts checking time.
73 void AddObserver(std::unique_ptr<Observer> observer);
75 // Checks if a day has elapsed. If it has, OnDailyEvent will be called on
79 // Registers the preference used by this interval.
80 static void RegisterPref(PrefRegistrySimple* registry,
81 const std::string& pref_name);
84 // Handles an interval elapsing because of |type|.
85 void OnInterval(base::Time now, IntervalType type);
87 // A weak pointer to the PrefService object to read and write preferences
88 // from. Calling code should ensure this object continues to exist for the
89 // lifetime of the DailyEvent object.
90 raw_ptr<PrefService, LeakedDanglingUntriaged> pref_service_;
92 // The name of the preference to store the last fired time in.
93 // Calling code should ensure this outlives the DailyEvent.
94 const char* pref_name_;
96 // The name of the histogram to record intervals.
97 std::string histogram_name_;
99 // A list of observers.
100 std::vector<std::unique_ptr<Observer>> observers_;
102 // The time that the daily event was last fired.
103 base::Time last_fired_;
106 } // namespace metrics
108 #endif // COMPONENTS_METRICS_DAILY_EVENT_H_