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 #include "components/metrics/daily_interval.h" | |
| 6 | |
| 7 #include "base/i18n/time_formatting.h" | |
| 8 #include "base/prefs/pref_registry_simple.h" | |
| 9 #include "base/prefs/pref_service.h" | |
| 10 | |
| 11 namespace metrics { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 enum IntervalType { | |
| 16 FIRST_RUN, | |
| 17 DAY_ELAPSED, | |
| 18 CLOCK_CHANGED, | |
| 19 NUM_INTERVAL_TYPES | |
| 20 }; | |
| 21 | |
| 22 void RecordIntervalTypeHistogram(const char* pref_name, IntervalType t) { | |
| 23 std::string metric_name("DailyInterval.IntervalType."); | |
| 24 metric_name += pref_name; | |
| 25 base::Histogram::FactoryGet( | |
| 26 metric_name, | |
| 27 1, | |
| 28 NUM_INTERVAL_TYPES, | |
| 29 NUM_INTERVAL_TYPES + 1, | |
| 30 base::HistogramBase::kUmaTargetedHistogramFlag)->Add(t); | |
| 31 } | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 DailyInterval::DailyInterval(PrefService* pref_service, const char* pref_name) | |
| 36 : pref_service_(pref_service), pref_name_(pref_name) { | |
| 37 } | |
| 38 | |
| 39 DailyInterval::~DailyInterval() { | |
| 40 } | |
| 41 | |
| 42 // static | |
| 43 void DailyInterval::RegisterPref(PrefRegistrySimple* registry, | |
| 44 const char* pref_name) { | |
| 45 registry->RegisterInt64Pref(pref_name, base::Time().ToInternalValue()); | |
| 46 } | |
| 47 | |
| 48 void DailyInterval::AddObserver(DailyObserver* observer) { | |
| 49 DVLOG(2) << "DailyInterval observer added."; | |
| 50 DCHECK(last_fired_.is_null()); | |
| 51 observers_.AddObserver(observer); | |
| 52 } | |
| 53 | |
| 54 void DailyInterval::CheckInterval() { | |
| 55 base::Time now = base::Time::Now(); | |
| 56 if (last_fired_.is_null()) { | |
| 57 // The first time we call CheckInterval, we read the time stored in prefs. | |
| 58 last_fired_ = base::Time::FromInternalValue( | |
| 59 pref_service_->GetInt64(pref_name_)); | |
| 60 DVLOG(1) << "DailyInterval time loaded: " | |
| 61 << base::TimeFormatShortDateAndTime(last_fired_); | |
| 62 if (last_fired_.is_null()) { | |
| 63 DVLOG(1) << "DailyInterval first run."; | |
| 64 RecordIntervalTypeHistogram(pref_name_, FIRST_RUN); | |
| 65 OnInterval(now); | |
| 66 return; | |
| 67 } | |
| 68 } | |
| 69 int days_elapsed = (now - last_fired_).InDays(); | |
| 70 if (days_elapsed > 1) { | |
| 71 DVLOG(1) << "DailyInterval day elapsed."; | |
| 72 RecordIntervalTypeHistogram(pref_name_, DAY_ELAPSED); | |
| 73 OnInterval(now); | |
| 74 } else if (days_elapsed <= -1) { | |
| 75 // The "last fired" time is more than a day in the future, so the clock | |
| 76 // must have been changed. | |
| 77 DVLOG(1) << "DailyInterval clock change detected."; | |
| 78 RecordIntervalTypeHistogram(pref_name_, CLOCK_CHANGED); | |
| 79 OnInterval(now); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 void DailyInterval::OnInterval(base::Time now) { | |
| 84 DCHECK(!now.is_null()); | |
| 85 last_fired_ = now; | |
| 86 pref_service_->SetInt64(pref_name_, last_fired_.ToInternalValue()); | |
| 87 FOR_EACH_OBSERVER( | |
| 88 DailyObserver, observers_, OnDailyInterval()); | |
|
Alexei Svitkine (slow)
2014/09/05 20:00:33
Nit: Does this need to wrap?
Steven Holte
2014/09/05 21:15:07
Done.
| |
| 89 } | |
| 90 | |
| 91 } // namespace metrics | |
| OLD | NEW |