Chromium Code Reviews| 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_INTERVAL_H_ | |
| 6 #define COMPONENTS_METRICS_DAILY_INTERVAL_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 // DailyInterval is used for throttling an event to about once per day, even if | |
| 19 // chrome is restarted more frequently. It is based on local machine time, so | |
|
Alexei Svitkine (slow)
2014/09/15 20:29:41
Nit: This is in a component, so don't say "Chrome"
Steven Holte
2014/09/15 21:27:03
Done.
| |
| 20 // it could be fired more often if the clock is changed. | |
|
Alexei Svitkine (slow)
2014/09/15 20:29:41
In the description, mention how this class is inte
Steven Holte
2014/09/15 21:27:03
Done.
| |
| 21 class DailyInterval { | |
| 22 public: | |
| 23 // DailyObserver receives notifications from a DailyInterval. | |
|
Alexei Svitkine (slow)
2014/09/15 20:29:41
Nit: Update comment (no longer named DailyObserver
Steven Holte
2014/09/15 21:27:03
Done.
| |
| 24 // Observers must be added before the DailyInterval begins checking time, | |
| 25 // and will be owned by the DailyInterval. | |
| 26 class Observer { | |
| 27 public: | |
| 28 Observer(); | |
| 29 virtual ~Observer(); | |
| 30 | |
| 31 // Called when daily metrics should be collected. | |
| 32 virtual void OnDailyInterval() = 0; | |
| 33 | |
| 34 private: | |
| 35 DISALLOW_COPY_AND_ASSIGN(Observer); | |
| 36 }; | |
| 37 | |
| 38 // Constructs DailyInterval monitor which stores the time it last fired | |
| 39 // in the preference |pref_name|. | |
| 40 // |pref_name| should be registered by calling RegisterPref before using this | |
| 41 // object. | |
| 42 // Caller is responsible for ensuring |pref_service| outlives the | |
| 43 // DailyInterval. | |
| 44 // |histogram_name| is the name of the UMA metric which record when this | |
| 45 // interval fires, and should be registered in histograms.xml | |
| 46 DailyInterval(PrefService* pref_service, | |
| 47 const char* pref_name, | |
| 48 const std::string& histogram_name); | |
| 49 ~DailyInterval(); | |
| 50 | |
| 51 // Adds a observer to be notified when a day elapses. All observers should | |
| 52 // be registered before the the DailyInterval starts checking time. | |
| 53 void AddObserver(scoped_ptr<Observer> observer); | |
| 54 | |
| 55 // Check if a day has elapsed. If it has, OnDailyInterval will be called on | |
| 56 // all observers. | |
| 57 void CheckInterval(); | |
| 58 | |
| 59 // Registers the preference used by this interval. | |
| 60 static void RegisterPref(PrefRegistrySimple* registry, const char* pref_name); | |
| 61 | |
| 62 private: | |
| 63 // Handle an interval elapsing. | |
| 64 void OnInterval(base::Time now); | |
| 65 | |
| 66 // A weak pointer to the PrefService object to read and write preferences | |
| 67 // from. Calling code should ensure this object continues to exist for the | |
| 68 // lifetime of the DailyInterval object. | |
| 69 PrefService* pref_service_; | |
| 70 | |
| 71 // The name of the preference to store the last fired time in. | |
| 72 const char* pref_name_; | |
| 73 | |
| 74 // The name of the histogram to record intervals. | |
| 75 std::string histogram_name_; | |
| 76 | |
| 77 // List of observers | |
| 78 ScopedVector<Observer> observers_; | |
| 79 | |
| 80 // True if we've started checking for events. | |
|
Alexei Svitkine (slow)
2014/09/15 20:29:41
Comment seems to not match the field (it's a time
Steven Holte
2014/09/15 21:27:03
Done.
| |
| 81 base::Time last_fired_; | |
| 82 | |
| 83 DISALLOW_COPY_AND_ASSIGN(DailyInterval); | |
| 84 }; | |
| 85 | |
| 86 } // namespace metrics | |
| 87 | |
| 88 #endif // COMPONENTS_METRICS_DAILY_INTERVAL_H_ | |
| OLD | NEW |