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