[M120 Migration][VD] Enable direct rendering for TVPlus
[platform/framework/web/chromium-efl.git] / components / metrics / date_changed_helper.cc
1 // Copyright 2020 The Chromium Authors
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/date_changed_helper.h"
6
7 #include "base/time/time.h"
8 #include "components/prefs/pref_registry_simple.h"
9 #include "components/prefs/pref_service.h"
10
11 namespace metrics {
12
13 namespace {
14
15 base::Time GetCurrentDate(base::Time now) {
16   // Use local midnight to ensure reporting is for calendar days, not UTC, and
17   // avoid storing extra information about when this was called.
18   return now.LocalMidnight();
19 }
20
21 void UpdateStoredDate(PrefService* prefs,
22                       const char* pref_name,
23                       base::Time now) {
24   prefs->SetTime(pref_name, GetCurrentDate(now));
25 }
26
27 bool IsStoredDateToday(PrefService* prefs,
28                        const char* pref_name,
29                        base::Time now) {
30   base::Time stored_date = prefs->GetTime(pref_name);
31   if (stored_date.is_null()) {
32     // Consider date unchanged if pref has never been set.
33     UpdateStoredDate(prefs, pref_name, GetCurrentDate(now));
34     return true;
35   }
36   // Ignore small changes in midnight such as time zone changes.
37   return std::abs((stored_date - GetCurrentDate(now)).InHours()) < 12;
38 }
39
40 }  // namespace
41
42 namespace date_changed_helper {
43
44 bool HasDateChangedSinceLastCall(PrefService* prefs, const char* pref_name) {
45   DCHECK(prefs);
46   DCHECK(pref_name);
47   base::Time now = base::Time::Now();
48   if (IsStoredDateToday(prefs, pref_name, now))
49     return false;
50   UpdateStoredDate(prefs, pref_name, now);
51   return true;
52 }
53
54 void RegisterPref(PrefRegistrySimple* registry, const char* pref_name) {
55   registry->RegisterTimePref(pref_name, base::Time());
56 }
57
58 }  // namespace date_changed_helper
59
60 }  // namespace metrics