OLD | NEW |
---|---|
(Empty) | |
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 | |
5 #ifndef COMPONENTS_METRICS_DAILY_EVENT_H_ | |
6 #define COMPONENTS_METRICS_DAILY_EVENT_H_ | |
7 | |
8 #include "base/macros.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/memory/scoped_vector.h" | |
11 #include "base/time/time.h" | |
12 | |
13 class PrefRegistrySimple; | |
14 class PrefService; | |
15 | |
16 namespace metrics { | |
17 | |
18 // DailyEvent is used for throttling an event to about once per day, even if | |
19 // the program is restarted more frequently. It is based on local machine | |
20 // time, so it could be fired more often if the clock is changed. | |
21 // | |
22 // The service using the DailyEvent should first provide all of the Observers | |
23 // for the interval, and then arrange for CheckInterval() to be called | |
24 // periodically to test if the event should be fired. | |
25 class DailyEvent { | |
26 public: | |
27 // Observer receives notifications from a DailyEvent. | |
28 // Observers must be added before the DailyEvent begins checking time, | |
29 // and will be owned by the DailyEvent. | |
30 class Observer { | |
31 public: | |
32 Observer(); | |
33 virtual ~Observer(); | |
34 | |
35 // Called when the daily event is fired. | |
36 virtual void OnDailyEvent() = 0; | |
37 | |
38 private: | |
39 DISALLOW_COPY_AND_ASSIGN(Observer); | |
40 }; | |
41 | |
42 // Constructs DailyEvent monitor which stores the time it last fired in the | |
43 // preference |pref_name|. | |
44 // |pref_name| should be registered by calling RegisterPref before using this | |
45 // object. | |
46 // Caller is responsible for ensuring |pref_service| outlives the DailyEvent. | |
Alexei Svitkine (slow)
2014/09/15 21:32:32
Nit: Also, pref_name. Update comment and mention t
Steven Holte
2014/09/15 21:43:09
Done.
| |
47 // |histogram_name| is the name of the UMA metric which record when this | |
48 // interval fires, and should be registered in histograms.xml | |
49 DailyEvent(PrefService* pref_service, | |
50 const char* pref_name, | |
51 const std::string& histogram_name); | |
52 ~DailyEvent(); | |
53 | |
54 // Adds a observer to be notified when a day elapses. All observers should | |
55 // be registered before the the DailyEvent starts checking time. | |
56 void AddObserver(scoped_ptr<Observer> observer); | |
57 | |
58 // Check if a day has elapsed. If it has, OnDailyEvent will be called on | |
Alexei Svitkine (slow)
2014/09/15 21:32:32
Nit: Check -> Checks for consistency with verb ten
Steven Holte
2014/09/15 21:43:09
Done.
| |
59 // all observers. | |
60 void CheckInterval(); | |
61 | |
62 // Registers the preference used by this interval. | |
63 static void RegisterPref(PrefRegistrySimple* registry, const char* pref_name); | |
64 | |
65 private: | |
66 // Handle an interval elapsing. | |
Alexei Svitkine (slow)
2014/09/15 21:32:32
Nit: Handle -> Handles
Steven Holte
2014/09/15 21:43:09
Done.
| |
67 void OnInterval(base::Time now); | |
68 | |
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 PrefService* pref_service_; | |
73 | |
74 // The name of the preference to store the last fired time in. | |
75 const char* pref_name_; | |
76 | |
77 // The name of the histogram to record intervals. | |
78 std::string histogram_name_; | |
79 | |
80 // List of observers | |
Alexei Svitkine (slow)
2014/09/15 21:32:32
Nit: Add a .
Steven Holte
2014/09/15 21:43:09
Done.
| |
81 ScopedVector<Observer> observers_; | |
82 | |
83 // The time that the daily event was last fired. | |
84 base::Time last_fired_; | |
85 | |
86 DISALLOW_COPY_AND_ASSIGN(DailyEvent); | |
87 }; | |
88 | |
89 } // namespace metrics | |
90 | |
91 #endif // COMPONENTS_METRICS_DAILY_EVENT_H_ | |
OLD | NEW |