Chromium Code Reviews| Index: components/metrics/daily_interval.h |
| diff --git a/components/metrics/daily_interval.h b/components/metrics/daily_interval.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e55db86ab3db0797ec60d47f636d1f66348ea3a0 |
| --- /dev/null |
| +++ b/components/metrics/daily_interval.h |
| @@ -0,0 +1,88 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef COMPONENTS_METRICS_DAILY_INTERVAL_H_ |
| +#define COMPONENTS_METRICS_DAILY_INTERVAL_H_ |
| + |
| +#include "base/macros.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/scoped_vector.h" |
| +#include "base/time/time.h" |
| + |
| +class PrefRegistrySimple; |
| +class PrefService; |
| + |
| +namespace metrics { |
| + |
| +// DailyInterval is used for throttling an event to about once per day, even if |
| +// 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.
|
| +// 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.
|
| +class DailyInterval { |
| + public: |
| + // 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.
|
| + // Observers must be added before the DailyInterval begins checking time, |
| + // and will be owned by the DailyInterval. |
| + class Observer { |
| + public: |
| + Observer(); |
| + virtual ~Observer(); |
| + |
| + // Called when daily metrics should be collected. |
| + virtual void OnDailyInterval() = 0; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(Observer); |
| + }; |
| + |
| + // Constructs DailyInterval monitor which stores the time it last fired |
| + // in the preference |pref_name|. |
| + // |pref_name| should be registered by calling RegisterPref before using this |
| + // object. |
| + // Caller is responsible for ensuring |pref_service| outlives the |
| + // DailyInterval. |
| + // |histogram_name| is the name of the UMA metric which record when this |
| + // interval fires, and should be registered in histograms.xml |
| + DailyInterval(PrefService* pref_service, |
| + const char* pref_name, |
| + const std::string& histogram_name); |
| + ~DailyInterval(); |
| + |
| + // Adds a observer to be notified when a day elapses. All observers should |
| + // be registered before the the DailyInterval starts checking time. |
| + void AddObserver(scoped_ptr<Observer> observer); |
| + |
| + // Check if a day has elapsed. If it has, OnDailyInterval will be called on |
| + // all observers. |
| + void CheckInterval(); |
| + |
| + // Registers the preference used by this interval. |
| + static void RegisterPref(PrefRegistrySimple* registry, const char* pref_name); |
| + |
| + private: |
| + // Handle an interval elapsing. |
| + void OnInterval(base::Time now); |
| + |
| + // A weak pointer to the PrefService object to read and write preferences |
| + // from. Calling code should ensure this object continues to exist for the |
| + // lifetime of the DailyInterval object. |
| + PrefService* pref_service_; |
| + |
| + // The name of the preference to store the last fired time in. |
| + const char* pref_name_; |
| + |
| + // The name of the histogram to record intervals. |
| + std::string histogram_name_; |
| + |
| + // List of observers |
| + ScopedVector<Observer> observers_; |
| + |
| + // 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.
|
| + base::Time last_fired_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DailyInterval); |
| +}; |
| + |
| +} // namespace metrics |
| + |
| +#endif // COMPONENTS_METRICS_DAILY_INTERVAL_H_ |