summaryrefslogtreecommitdiffstats
path: root/components/metrics/daily_event.cc
blob: 34c34265a3bba1104018a1ee85e94ab269292f32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// 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.

#include "components/metrics/daily_event.h"

#include "base/i18n/time_formatting.h"
#include "base/metrics/histogram.h"
#include "base/prefs/pref_registry_simple.h"
#include "base/prefs/pref_service.h"

namespace metrics {

namespace {

enum IntervalType {
  FIRST_RUN,
  DAY_ELAPSED,
  CLOCK_CHANGED,
  NUM_INTERVAL_TYPES
};

void RecordIntervalTypeHistogram(const std::string& histogram_name,
                                 IntervalType type) {
  base::Histogram::FactoryGet(
      histogram_name,
      1,
      NUM_INTERVAL_TYPES,
      NUM_INTERVAL_TYPES + 1,
      base::HistogramBase::kUmaTargetedHistogramFlag)->Add(type);
}

}  // namespace

DailyEvent::Observer::Observer() {
}

DailyEvent::Observer::~Observer() {
}

DailyEvent::DailyEvent(PrefService* pref_service,
                       const char* pref_name,
                       const std::string& histogram_name)
    : pref_service_(pref_service),
      pref_name_(pref_name),
      histogram_name_(histogram_name) {
}

DailyEvent::~DailyEvent() {
}

// static
void DailyEvent::RegisterPref(PrefRegistrySimple* registry,
                              const char* pref_name) {
  registry->RegisterInt64Pref(pref_name, base::Time().ToInternalValue());
}

void DailyEvent::AddObserver(scoped_ptr<DailyEvent::Observer> observer) {
  DVLOG(2) << "DailyEvent observer added.";
  DCHECK(last_fired_.is_null());
  observers_.push_back(observer.release());
}

void DailyEvent::CheckInterval() {
  base::Time now = base::Time::Now();
  if (last_fired_.is_null()) {
    // The first time we call CheckInterval, we read the time stored in prefs.
    last_fired_ = base::Time::FromInternalValue(
        pref_service_->GetInt64(pref_name_));
    DVLOG(1) << "DailyEvent time loaded: "
             << base::TimeFormatShortDateAndTime(last_fired_);
    if (last_fired_.is_null()) {
      DVLOG(1) << "DailyEvent first run.";
      RecordIntervalTypeHistogram(pref_name_, FIRST_RUN);
      OnInterval(now);
      return;
    }
  }
  int days_elapsed = (now - last_fired_).InDays();
  if (days_elapsed >= 1) {
    DVLOG(1) << "DailyEvent day elapsed.";
    RecordIntervalTypeHistogram(pref_name_, DAY_ELAPSED);
    OnInterval(now);
  } else if (days_elapsed <= -1) {
    // The "last fired" time is more than a day in the future, so the clock
    // must have been changed.
    DVLOG(1) << "DailyEvent clock change detected.";
    RecordIntervalTypeHistogram(pref_name_, CLOCK_CHANGED);
    OnInterval(now);
  }
}

void DailyEvent::OnInterval(base::Time now) {
  DCHECK(!now.is_null());
  last_fired_ = now;
  pref_service_->SetInt64(pref_name_, last_fired_.ToInternalValue());

  // Notify all observers
  for (ScopedVector<DailyEvent::Observer>::iterator it = observers_.begin();
       it != observers_.end();
       ++it) {
    (*it)->OnDailyEvent();
  }
}

}  // namespace metrics