File: k12_age_classification_metrics_provider.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (110 lines) | stat: -rw-r--r-- 3,936 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
// Copyright 2025 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/metrics/k12_age_classification_metrics_provider.h"

#include "base/check_is_test.h"
#include "base/metrics/histogram_functions.h"
#include "chrome/browser/ash/login/demo_mode/demo_session.h"
#include "chrome/browser/ash/policy/core/browser_policy_connector_ash.h"
#include "chrome/browser/ash/policy/core/user_cloud_policy_manager_ash.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chromeos/components/kiosk/kiosk_utils.h"
#include "chromeos/components/mgs/managed_guest_session_utils.h"
#include "components/session_manager/core/session_manager.h"
#include "components/user_manager/user.h"
#include "components/user_manager/user_manager.h"

namespace {

namespace em = enterprise_management;

K12AgeClassificationMetricsProvider::K12AgeClassificationSegment
GetK12UserAgeClassificationSegment(Profile* profile) {
  const policy::UserCloudPolicyManagerAsh* const user_cloud_policy_manager =
      profile->GetUserCloudPolicyManagerAsh();
  if (!user_cloud_policy_manager) {
    return K12AgeClassificationMetricsProvider::K12AgeClassificationSegment::
        kAgeUnspecified;
  }
  const em::PolicyData* const policy =
      user_cloud_policy_manager->core()->store()->policy();
  if (!policy || !policy->has_k12_age_classification_metrics_log_segment()) {
    return K12AgeClassificationMetricsProvider::K12AgeClassificationSegment::
        kAgeUnspecified;
  }
  switch (policy->k12_age_classification_metrics_log_segment()) {
    case em::PolicyData::AGE_UNDER18:
      return K12AgeClassificationMetricsProvider::K12AgeClassificationSegment::
          kAgeUnder18;
    case em::PolicyData::AGE_EQUAL_OR_OVER18:
      return K12AgeClassificationMetricsProvider::K12AgeClassificationSegment::
          kAgeEqualOrOver18;
    case em::PolicyData::AGE_UNSPECIFIED:
      [[fallthrough]];
    default:
      return K12AgeClassificationMetricsProvider::K12AgeClassificationSegment::
          kAgeUnspecified;
  }
}

}  // namespace

K12AgeClassificationMetricsProvider::K12AgeClassificationMetricsProvider() {
  auto* const session_manager = session_manager::SessionManager::Get();
  // The `session_manager` is nullptr only for unit tests.
  if (session_manager) {
    session_manager->AddObserver(this);
  } else {
    CHECK_IS_TEST();
  }
}

K12AgeClassificationMetricsProvider::~K12AgeClassificationMetricsProvider() {
  auto* const session_manager = session_manager::SessionManager::Get();
  // The `session_manager` is nullptr only for unit tests.
  if (session_manager) {
    session_manager->RemoveObserver(this);
  } else {
    CHECK_IS_TEST();
  }
}

bool K12AgeClassificationMetricsProvider::ProvideHistograms() {
  if (!segment_.has_value()) {
    return false;
  }

  base::UmaHistogramEnumeration(kHistogramName, segment_.value());
  return true;
}

void K12AgeClassificationMetricsProvider::OnUserSessionStarted(
    bool is_primary_user) {
  // Skip non-primary, demo, managed guest, kiosk users.
  if (!is_primary_user || profiles::IsDemoSession() ||
      chromeos::IsManagedGuestSession() || chromeos::IsKioskSession()) {
    return;
  }

  const user_manager::User* const primary_user =
      user_manager::UserManager::Get()->GetPrimaryUser();
  CHECK(primary_user);
  CHECK(primary_user->is_profile_created());
  Profile* const profile =
      ash::ProfileHelper::Get()->GetProfileByUser(primary_user);
  CHECK(profile);

  // Skip unmanaged users.
  if (profile->IsOffTheRecord()) {
    return;
  }

  segment_ = GetK12UserAgeClassificationSegment(profile);
}