File: daily_event.cc

package info (click to toggle)
chromium 141.0.7390.107-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,254,812 kB
  • sloc: cpp: 35,264,957; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,636; asm: 950,788; xml: 751,751; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,319; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (112 lines) | stat: -rw-r--r-- 3,552 bytes parent folder | download | duplicates (3)
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
107
108
109
110
111
112
// Copyright 2014 The Chromium Authors
// 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 <memory>
#include <utility>

#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"

namespace metrics {
namespace {

class CallbackObserver : public DailyEvent::Observer {
 public:
  explicit CallbackObserver(base::RepeatingClosure closure)
      : closure_(std::move(closure)) {}
  ~CallbackObserver() override = default;

  CallbackObserver(const CallbackObserver&) = delete;
  CallbackObserver& operator=(const CallbackObserver&) = delete;

  void OnDailyEvent(DailyEvent::IntervalType _) override { closure_.Run(); }

 private:
  base::RepeatingClosure closure_;
};

void RecordIntervalTypeHistogram(const std::string& histogram_name,
                                 DailyEvent::IntervalType type) {
  if (histogram_name.empty()) {
    return;
  }
  base::UmaHistogramEnumeration(histogram_name, type);
}

}  // namespace

DailyEvent::Observer::Observer() = default;
DailyEvent::Observer::~Observer() = default;

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() = default;

// static
void DailyEvent::RegisterPref(PrefRegistrySimple* registry,
                              const std::string& pref_name) {
  registry->RegisterInt64Pref(pref_name, 0);
}

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

void DailyEvent::AddObserverClosure(base::RepeatingClosure closure) {
  AddObserver(std::make_unique<CallbackObserver>(std::move(closure)));
}

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() + base::Microseconds(pref_service_->GetInt64(pref_name_));

    DVLOG(1) << "DailyEvent time loaded: " << last_fired_;
    if (last_fired_.is_null()) {
      DVLOG(1) << "DailyEvent first run.";
      RecordIntervalTypeHistogram(histogram_name_, IntervalType::FIRST_RUN);
      OnInterval(now, IntervalType::FIRST_RUN);
      return;
    }
  }
  int days_elapsed = (now - last_fired_).InDays();
  if (days_elapsed >= 1) {
    DVLOG(1) << "DailyEvent day elapsed.";
    RecordIntervalTypeHistogram(histogram_name_, IntervalType::DAY_ELAPSED);
    OnInterval(now, IntervalType::DAY_ELAPSED);
  } 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(histogram_name_, IntervalType::CLOCK_CHANGED);
    OnInterval(now, IntervalType::CLOCK_CHANGED);
  }
}

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

  for (auto it = observers_.begin(); it != observers_.end(); ++it) {
    (*it)->OnDailyEvent(type);
  }
}

}  // namespace metrics