File: app_platform_metrics_service.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (129 lines) | stat: -rw-r--r-- 4,663 bytes parent folder | download | duplicates (6)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_APPS_APP_SERVICE_METRICS_APP_PLATFORM_METRICS_SERVICE_H_
#define CHROME_BROWSER_APPS_APP_SERVICE_METRICS_APP_PLATFORM_METRICS_SERVICE_H_

#include <utility>

#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/timer/timer.h"
#include "chrome/browser/apps/app_service/metrics/app_discovery_metrics.h"
#include "chrome/browser/apps/app_service/metrics/app_platform_input_metrics.h"
#include "chrome/browser/apps/app_service/metrics/app_platform_metrics.h"
#include "chrome/browser/apps/app_service/metrics/website_metrics.h"
#include "chrome/browser/profiles/profile.h"

class PrefRegistrySimple;

namespace apps {

class AppRegistryCache;
class InstanceRegistry;

extern const char kAppPlatformMetricsDayId[];

// Service to initialize and control app platform metric recorders per day in
// Chrome OS.
class AppPlatformMetricsService {
 public:
  // Observer that can be used to monitor the lifecycle of certain components
  // owned by `AppPlatformMetricsService`.
  class Observer : public base::CheckedObserver {
   public:
    Observer() = default;
    Observer(const Observer&) = delete;
    Observer& operator=(const Observer&) = delete;
    ~Observer() override = default;

    // Triggered once the `AppPlatformMetrics` component is initialized.
    // This enables external components to delay interactions with the
    // component until it is ready.
    virtual void OnAppPlatformMetricsInit(
        AppPlatformMetrics* app_platform_metrics) {}

    // Triggered once the `WebsiteMetrics` component is initialized. This
    // enables external components to delay interactions with the component
    // until it is ready.
    virtual void OnWebsiteMetricsInit(WebsiteMetrics* website_metrics) {}

    // Triggered when the `AppPlatformMetricsService` will be destroyed. This
    // can be used by observer to unregister itself as an observer as well as
    // prevent use-after-free errors.
    virtual void OnAppPlatformMetricsServiceWillBeDestroyed() = 0;
  };

  explicit AppPlatformMetricsService(Profile* profile);
  AppPlatformMetricsService(const AppPlatformMetricsService&) = delete;
  AppPlatformMetricsService& operator=(const AppPlatformMetricsService&) =
      delete;
  ~AppPlatformMetricsService();

  static void RegisterProfilePrefs(PrefRegistrySimple* registry);

  // Returns the day id for a given time for testing.
  static int GetDayIdForTesting(base::Time time);

  // Start the timer and check if a new day has arrived.
  void Start(AppRegistryCache& app_registry_cache,
             InstanceRegistry& instance_registry,
             apps::AppCapabilityAccessCache& app_capability_access_cache);

  apps::AppPlatformMetrics* AppPlatformMetrics() {
    return app_platform_app_metrics_.get();
  }

  apps::WebsiteMetrics* WebsiteMetrics() { return website_metrics_.get(); }

  // Add observer to the observer list.
  void AddObserver(Observer* observer);

  // Remove observer from the observer list.
  void RemoveObserver(Observer* observer);

  void SetWebsiteMetricsForTesting(
      std::unique_ptr<apps::WebsiteMetrics> website_metrics);

 private:
  friend class AppPlatformInputMetricsTest;

  // Helper function to check if a new day has arrived.
  void CheckForNewDay();

  // Helper function to check if 5 mintues have arrived.
  void CheckForFiveMinutes();

  // Helper function to check if the reporting interval for noisy AppKMs has
  // arrived to report noisy AppKMs events.
  void CheckForNoisyAppKMReportingInterval();

  const raw_ptr<Profile> profile_;

  int day_id_;

  // A periodic timer that checks if a new day has arrived.
  base::RepeatingTimer timer_;

  // A periodic timer that checks if five minutes have arrived.
  base::RepeatingTimer five_minutes_timer_;

  // A periodic timer that checks if the reporting interval for noisy AppKMs has
  // arrived to report noisy AppKM events.
  base::RepeatingTimer noisy_appkm_reporting_interval_timer_;

  std::unique_ptr<apps::AppPlatformMetrics> app_platform_app_metrics_;
  std::unique_ptr<apps::AppPlatformInputMetrics> app_platform_input_metrics_;
  std::unique_ptr<apps::WebsiteMetrics> website_metrics_;
  std::unique_ptr<apps::AppDiscoveryMetrics> app_discovery_metrics_;

  // List of observers that will be notified of certain component lifecycle
  // changes.
  base::ObserverList<Observer> observers_;
};

}  // namespace apps

#endif  // CHROME_BROWSER_APPS_APP_SERVICE_METRICS_APP_PLATFORM_METRICS_SERVICE_H_