File: app_platform_metrics_service.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (147 lines) | stat: -rw-r--r-- 5,050 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 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.

#include "chrome/browser/apps/app_service/metrics/app_platform_metrics_service.h"

#include "base/feature_list.h"
#include "base/time/time.h"
#include "chrome/browser/apps/app_service/app_service_proxy_ash.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"

namespace apps {

namespace {

// Interval for reporting noisy AppKM events.
constexpr base::TimeDelta kNoisyAppKMReportInterval = base::Hours(2);

// Check for a new day every 10 minutes.
constexpr base::TimeDelta kTimerInterval = base::Minutes(10);

// Check for app usage time, input event each 5 minutes.
constexpr base::TimeDelta kFiveMinutes = base::Minutes(5);

// Returns the number of days since the origin.
int GetDayId(base::Time time) {
  return time.UTCMidnight().since_origin().InDaysFloored();
}

}  // namespace

constexpr char kAppPlatformMetricsDayId[] = "app_platform_metrics.day_id";

AppPlatformMetricsService::AppPlatformMetricsService(Profile* profile)
    : profile_(profile) {
  DCHECK(profile_);
}

AppPlatformMetricsService::~AppPlatformMetricsService() {
  timer_.Stop();

  // Also notify observers.
  for (auto& observer : observers_) {
    observer.OnAppPlatformMetricsServiceWillBeDestroyed();
  }
}

// static
void AppPlatformMetricsService::RegisterProfilePrefs(
    PrefRegistrySimple* registry) {
  registry->RegisterIntegerPref(kAppPlatformMetricsDayId, 0);
  registry->RegisterDictionaryPref(kAppRunningDuration);
  registry->RegisterDictionaryPref(kAppActivatedCount);
  registry->RegisterDictionaryPref(kAppUsageTime);
  registry->RegisterDictionaryPref(kAppInputEventsKey);
  registry->RegisterDictionaryPref(kWebsiteUsageTime);

  AppDiscoveryMetrics::RegisterProfilePrefs(registry);
}

// static
int AppPlatformMetricsService::GetDayIdForTesting(base::Time time) {
  return GetDayId(time);
}

void AppPlatformMetricsService::Start(
    apps::AppRegistryCache& app_registry_cache,
    InstanceRegistry& instance_registry,
    apps::AppCapabilityAccessCache& app_capability_access_cache) {
  app_platform_app_metrics_ = std::make_unique<apps::AppPlatformMetrics>(
      profile_, app_registry_cache, instance_registry);
  app_platform_input_metrics_ = std::make_unique<apps::AppPlatformInputMetrics>(
      profile_, app_registry_cache, instance_registry);
  website_metrics_ = std::make_unique<apps::WebsiteMetrics>(
      profile_, GetUserTypeByDeviceTypeMetrics());
  app_discovery_metrics_ = std::make_unique<apps::AppDiscoveryMetrics>(
      profile_, app_registry_cache, instance_registry,
      app_platform_app_metrics_.get(), app_capability_access_cache);

  day_id_ = profile_->GetPrefs()->GetInteger(kAppPlatformMetricsDayId);
  CheckForNewDay();

  // Check for a new day every |kTimerInterval| as well.
  timer_.Start(FROM_HERE, kTimerInterval, this,
               &AppPlatformMetricsService::CheckForNewDay);

  // Check every `kFiveMinutes` to record app usage time and input events.
  five_minutes_timer_.Start(FROM_HERE, kFiveMinutes, this,
                            &AppPlatformMetricsService::CheckForFiveMinutes);

  // Check every `kNoisyAppKMReportInterval` to report noisy AppKM events.
  noisy_appkm_reporting_interval_timer_.Start(
      FROM_HERE, kNoisyAppKMReportInterval, this,
      &AppPlatformMetricsService::CheckForNoisyAppKMReportingInterval);

  // Also notify observers.
  for (auto& observer : observers_) {
    observer.OnAppPlatformMetricsInit(app_platform_app_metrics_.get());
    observer.OnWebsiteMetricsInit(website_metrics_.get());
  }
}

void AppPlatformMetricsService::AddObserver(
    AppPlatformMetricsService::Observer* observer) {
  observers_.AddObserver(observer);
}

void AppPlatformMetricsService::RemoveObserver(
    AppPlatformMetricsService::Observer* observer) {
  observers_.RemoveObserver(observer);
}

void AppPlatformMetricsService::SetWebsiteMetricsForTesting(
    std::unique_ptr<apps::WebsiteMetrics> website_metrics) {
  website_metrics_ = std::move(website_metrics);
}

void AppPlatformMetricsService::CheckForNewDay() {
  base::Time now = base::Time::Now();

  DCHECK(app_platform_app_metrics_);
  app_platform_app_metrics_->OnTenMinutes();

  if (day_id_ < GetDayId(now)) {
    day_id_ = GetDayId(now);
    app_platform_app_metrics_->OnNewDay();
    profile_->GetPrefs()->SetInteger(kAppPlatformMetricsDayId, day_id_);
  }
}

void AppPlatformMetricsService::CheckForFiveMinutes() {
  app_platform_app_metrics_->OnFiveMinutes();
  app_platform_input_metrics_->OnFiveMinutes();
  website_metrics_->OnFiveMinutes();
}

void AppPlatformMetricsService::CheckForNoisyAppKMReportingInterval() {
  app_platform_app_metrics_->OnTwoHours();
  app_platform_input_metrics_->OnTwoHours();
  website_metrics_->OnTwoHours();
}

}  // namespace apps