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 DailyInterval::DailyInterval(PrefService* pref_service, const char* pref_name) | |
14 : pref_service_(pref_service), pref_name_(pref_name) { | |
15 } | |
16 | |
17 DailyInterval::~DailyInterval() { | |
18 } | |
19 | |
20 // static | |
21 void DailyInterval::RegisterPref(PrefRegistrySimple* registry, | |
22 const char* pref_name) { | |
23 registry->RegisterInt64Pref(pref_name, base::Time().ToInternalValue()); | |
24 } | |
25 | |
26 void DailyInterval::AddObserver(DailyObserver* observer) { | |
27 DVLOG(2) << "DailyInterval observer added."; | |
28 DCHECK(last_fired_.is_null()); | |
29 observers_.AddObserver(observer); | |
30 } | |
31 | |
32 void DailyInterval::CheckInterval() { | |
33 base::Time now = base::Time::Now(); | |
34 if (last_fired_.is_null()) { | |
35 // The first time we call CheckInterval, we read the time stored in prefs. | |
36 last_fired_ = base::Time::FromInternalValue( | |
37 pref_service_->GetInt64(pref_name_)); | |
38 DVLOG(1) << "DailyInterval time loaded: " | |
39 << base::TimeFormatShortDateAndTime(last_fired_); | |
40 if (last_fired_.is_null()) { | |
41 DVLOG(1) << "DailyInterval first run."; | |
42 OnInterval(now); | |
43 return; | |
44 } | |
45 } | |
46 int days_elapsed = (now - last_fired_).InDays(); | |
47 if (days_elapsed > 1) { | |
48 DVLOG(1) << "DailyInterval day elapsed."; | |
49 OnInterval(now); | |
50 } else if (days_elapsed <= -1) { | |
51 // The "last fired" time is more than a day in the future, so the clock | |
52 // must have been changed. | |
53 DVLOG(1) << "DailyInterval clock change detected."; | |
jwd
2014/08/29 15:24:15
It might be nice to log an uma metric here, to see
Steven Holte
2014/09/03 19:33:44
Done.
| |
54 OnInterval(now); | |
55 } | |
56 } | |
57 | |
58 void DailyInterval::OnInterval(base::Time now) { | |
59 DCHECK(!now.is_null()); | |
60 last_fired_ = now; | |
61 pref_service_->SetInt64(pref_name_, last_fired_.ToInternalValue()); | |
62 FOR_EACH_OBSERVER( | |
63 DailyObserver, observers_, OnDailyInterval()); | |
64 } | |
65 | |
66 } // namespace metrics | |
OLD | NEW |