1 // Copyright 2020 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 #ifndef CHROME_BROWSER_CHROMEOS_CHILD_ACCOUNTS_FAMILY_USER_METRICS_SERVICE_H_
6 #define CHROME_BROWSER_CHROMEOS_CHILD_ACCOUNTS_FAMILY_USER_METRICS_SERVICE_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "base/observer_list.h"
12 #include "base/observer_list_types.h"
13 #include "base/timer/timer.h"
14 #include "components/keyed_service/core/keyed_service.h"
15 
16 class PrefRegistrySimple;
17 class PrefService;
18 
19 namespace base {
20 class Time;
21 }
22 
23 namespace content {
24 class BrowserContext;
25 }
26 
27 namespace chromeos {
28 
29 // Service to initialize and control metric recorders of family users on Chrome
30 // OS.
31 class FamilyUserMetricsService : public KeyedService {
32  public:
33   // Interface for observing events on the FamilyUserMetricsService.
34   class Observer : public base::CheckedObserver {
35    public:
36     // Called when we detect a new day. This event can fire sooner or later than
37     // 24 hours due to clock or time zone changes.
OnNewDay()38     virtual void OnNewDay() {}
39   };
40 
41   static void RegisterProfilePrefs(PrefRegistrySimple* registry);
42   // Returns the day id for a given time for testing.
43   static int GetDayIdForTesting(base::Time time);
44 
45   explicit FamilyUserMetricsService(content::BrowserContext* context);
46   FamilyUserMetricsService(const FamilyUserMetricsService&) = delete;
47   FamilyUserMetricsService& operator=(const FamilyUserMetricsService&) = delete;
48   ~FamilyUserMetricsService() override;
49 
50   // KeyedService:
51   void Shutdown() override;
52 
53   void AddObserver(Observer* observer);
54   void RemoveObserver(Observer* observer);
55 
56  private:
57   // Helper function to check if a new day has arrived.
58   void CheckForNewDay();
59 
60   PrefService* const pref_service_;
61 
62   // A periodic timer that checks if a new day has arrived.
63   base::RepeatingTimer timer_;
64 
65   // The |observers_| list is a superset of the |family_user_metrics_|.
66   base::ObserverList<Observer> observers_;
67   std::vector<std::unique_ptr<Observer>> family_user_metrics_;
68 };
69 
70 }  // namespace chromeos
71 
72 #endif  // CHROME_BROWSER_CHROMEOS_CHILD_ACCOUNTS_FAMILY_USER_METRICS_SERVICE_H_
73