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..5ee40cfd7cb220b1c337a1e8e9ed2566d2811311 |
| --- /dev/null |
| +++ b/components/metrics/daily_interval.h |
| @@ -0,0 +1,66 @@ |
| +// 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/observer_list.h" |
| +#include "base/time/time.h" |
| +#include "components/metrics/daily_observer.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 |
| +// it could be fired more often if the clock is changed. |
| +class DailyInterval { |
| +public: |
|
Alexei Svitkine (slow)
2014/09/05 20:00:33
Nit: Indent 1 more, here and private below.
Steven Holte
2014/09/05 21:15:07
Done.
|
| + // 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. |
| + DailyInterval(PrefService* pref_service, const char* pref_name); |
| + ~DailyInterval(); |
| + |
| + // Registers the preference used by this interval. |
| + static void RegisterPref(PrefRegistrySimple* registry, const char* pref_name); |
| + |
| + // Adds a observer to be notified when a day elapses. All observers should |
| + // be registered before the the DailyInterval starts checking time. |
| + void AddObserver(DailyObserver* observer); |
| + |
| + // Check if a day has elapsed. If it has, OnDailyInterval will be called on |
| + // all observers. |
| + void CheckInterval(); |
| + |
| +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_; |
| + |
| + // List of observers |
| + ObserverList<DailyObserver> observers_; |
| + |
| + // True if we've started checking for events. |
| + base::Time last_fired_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DailyInterval); |
| +}; |
| + |
| +} // namespace metrics |
| + |
| +#endif // COMPONENTS_METRICS_DAILY_INTERVAL_H_ |