File: metrics_utils.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (133 lines) | stat: -rw-r--r-- 4,656 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
// 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/enterprise/connectors/device_trust/common/metrics_utils.h"

#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "chrome/browser/enterprise/connectors/device_trust/common/common_types.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/ash/components/install_attributes/install_attributes.h"
#endif  // BUILDFLAG(IS_CHROMEOS)

namespace enterprise_connectors {

namespace {

#if BUILDFLAG(IS_CHROMEOS)
// Enrollment status of the device where the Device Trust connector attestation
// is happening. These values are persisted to logs and should not be
// renumbered. Please update the DTEnrollmentStatus enum in enums.xml when
// adding a new step here.
enum class DTEnrollmentStatus {
  kManaged = 0,
  kUnmanaged = 1,
  kMaxValue = kUnmanaged,
};
#endif  // BUILDFLAG(IS_CHROMEOS)

DTHandshakeResult ResponseToResult(const DeviceTrustResponse& response) {
  if (!response.error) {
    return DTHandshakeResult::kSuccess;
  }

  switch (response.error.value()) {
    case DeviceTrustError::kUnknown:
      return DTHandshakeResult::kUnknown;
    case DeviceTrustError::kTimeout:
      return DTHandshakeResult::kTimeout;
    case DeviceTrustError::kFailedToParseChallenge:
      return DTHandshakeResult::kFailedToParseChallenge;
    case DeviceTrustError::kFailedToCreateResponse:
      return DTHandshakeResult::kFailedToCreateResponse;
  }
}

bool ContainsPolicyLevel(const std::set<DTCPolicyLevel>& levels,
                         const DTCPolicyLevel& level) {
  return levels.find(level) != levels.end();
}

DTAttestationPolicyLevel GetAttestationPolicyLevel(
    const std::set<DTCPolicyLevel>& levels) {
  if (levels.empty()) {
    return DTAttestationPolicyLevel::kNone;
  }

  if (ContainsPolicyLevel(levels, DTCPolicyLevel::kBrowser)) {
    if (ContainsPolicyLevel(levels, DTCPolicyLevel::kUser)) {
      return DTAttestationPolicyLevel::kUserAndBrowser;
    }
    return DTAttestationPolicyLevel::kBrowser;
  }

  if (ContainsPolicyLevel(levels, DTCPolicyLevel::kUser)) {
    return DTAttestationPolicyLevel::kUser;
  }

  return DTAttestationPolicyLevel::kUnknown;
}

}  // namespace

void LogAttestationFunnelStep(DTAttestationFunnelStep step) {
  static constexpr char kFunnelStepHistogram[] =
      "Enterprise.DeviceTrust.Attestation.Funnel";
  base::UmaHistogramEnumeration(kFunnelStepHistogram, step);
  VLOG(1) << "Device Trust attestation step: " << static_cast<int>(step);
}

void LogAttestationPolicyLevel(const std::set<DTCPolicyLevel>& levels) {
  static constexpr char kAttestationPolicyLevelHistogram[] =
      "Enterprise.DeviceTrust.Attestation.PolicyLevel";
  base::UmaHistogramEnumeration(kAttestationPolicyLevelHistogram,
                                GetAttestationPolicyLevel(levels));
}

void LogAttestationResult(DTAttestationResult result) {
  static constexpr char kAttestationResultHistogram[] =
      "Enterprise.DeviceTrust.Attestation.Result";
  base::UmaHistogramEnumeration(kAttestationResultHistogram, result);
  if (!IsSuccessAttestationResult(result)) {
    LOG(ERROR) << "Device Trust attestation error: "
               << AttestationErrorToString(result);
  }
}

void LogDeviceTrustResponse(const DeviceTrustResponse& response,
                            base::TimeTicks start_time) {
  static constexpr char kLatencyHistogramFormat[] =
      "Enterprise.DeviceTrust.Attestation.ResponseLatency.%s";
  base::UmaHistogramTimes(
      base::StringPrintf(kLatencyHistogramFormat,
                         response.error ? "Failure" : "Success"),
      base::TimeTicks::Now() - start_time);

  static constexpr char kHandshakeResultHistogram[] =
      "Enterprise.DeviceTrust.Handshake.Result";
  base::UmaHistogramEnumeration(kHandshakeResultHistogram,
                                ResponseToResult(response));
}

#if BUILDFLAG(IS_CHROMEOS)
void LogOrigin(DTOrigin origin) {
  static constexpr char kOriginHistogram[] = "Enterprise.DeviceTrust.Origin";
  base::UmaHistogramEnumeration(kOriginHistogram, origin);
}

void LogEnrollmentStatus() {
  static constexpr char kEnrollmentStatusHistogram[] =
      "Enterprise.DeviceTrust.EnrollmentStatus";
  base::UmaHistogramEnumeration(
      kEnrollmentStatusHistogram,
      ash::InstallAttributes::Get()->IsEnterpriseManaged()
          ? DTEnrollmentStatus::kManaged
          : DTEnrollmentStatus::kUnmanaged);
}
#endif  // BUILDFLAG(IS_CHROMEOS)

}  // namespace enterprise_connectors