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.
5 #include "components/metrics/daily_event.h"
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"
23 void RecordIntervalTypeHistogram(const std::string& histogram_name,
25 base::Histogram::FactoryGet(
29 NUM_INTERVAL_TYPES + 1,
30 base::HistogramBase::kUmaTargetedHistogramFlag)->Add(type);
35 DailyEvent::Observer::Observer() {
38 DailyEvent::Observer::~Observer() {
41 DailyEvent::DailyEvent(PrefService* pref_service,
42 const char* pref_name,
43 const std::string& histogram_name)
44 : pref_service_(pref_service),
45 pref_name_(pref_name),
46 histogram_name_(histogram_name) {
49 DailyEvent::~DailyEvent() {
53 void DailyEvent::RegisterPref(PrefRegistrySimple* registry,
54 const char* pref_name) {
55 registry->RegisterInt64Pref(pref_name, base::Time().ToInternalValue());
58 void DailyEvent::AddObserver(scoped_ptr<DailyEvent::Observer> observer) {
59 DVLOG(2) << "DailyEvent observer added.";
60 DCHECK(last_fired_.is_null());
61 observers_.push_back(observer.release());
64 void DailyEvent::CheckInterval() {
65 base::Time now = base::Time::Now();
66 if (last_fired_.is_null()) {
67 // The first time we call CheckInterval, we read the time stored in prefs.
68 last_fired_ = base::Time::FromInternalValue(
69 pref_service_->GetInt64(pref_name_));
70 DVLOG(1) << "DailyEvent time loaded: "
71 << base::TimeFormatShortDateAndTime(last_fired_);
72 if (last_fired_.is_null()) {
73 DVLOG(1) << "DailyEvent first run.";
74 RecordIntervalTypeHistogram(pref_name_, FIRST_RUN);
79 int days_elapsed = (now - last_fired_).InDays();
80 if (days_elapsed >= 1) {
81 DVLOG(1) << "DailyEvent day elapsed.";
82 RecordIntervalTypeHistogram(pref_name_, DAY_ELAPSED);
84 } else if (days_elapsed <= -1) {
85 // The "last fired" time is more than a day in the future, so the clock
86 // must have been changed.
87 DVLOG(1) << "DailyEvent clock change detected.";
88 RecordIntervalTypeHistogram(pref_name_, CLOCK_CHANGED);
93 void DailyEvent::OnInterval(base::Time now) {
94 DCHECK(!now.is_null());
96 pref_service_->SetInt64(pref_name_, last_fired_.ToInternalValue());
98 // Notify all observers
99 for (ScopedVector<DailyEvent::Observer>::iterator it = observers_.begin();
100 it != observers_.end();
102 (*it)->OnDailyEvent();
106 } // namespace metrics