File: chromeos_metrics_provider.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 (317 lines) | stat: -rw-r--r-- 12,244 bytes parent folder | download | duplicates (5)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright 2014 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/chromeos_metrics_provider.h"

#include <stddef.h>

#include <string>
#include <vector>

#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "base/barrier_closure.h"
#include "base/feature_list.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/hash/md5.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/ash/accessibility/accessibility_manager.h"
#include "chrome/browser/ash/arc/arc_optin_uma.h"
#include "chrome/browser/ash/login/demo_mode/demo_session.h"
#include "chrome/browser/ash/login/users/chrome_user_manager_util.h"
#include "chrome/browser/ash/multidevice_setup/multidevice_setup_client_factory.h"
#include "chrome/browser/ash/policy/core/browser_policy_connector_ash.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chrome/browser/metrics/cached_metrics_profile.h"
#include "chrome/browser/metrics/chromeos_system_profile_provider.h"
#include "chrome/browser/metrics/enrollment_status.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h"
#include "chromeos/ash/components/system/statistics_provider.h"
#include "chromeos/ash/experiences/arc/arc_features_parser.h"
#include "chromeos/ash/experiences/arc/metrics/stability_metrics_manager.h"
#include "chromeos/ash/services/multidevice_setup/public/cpp/multidevice_setup_client.h"
#include "chromeos/constants/chromeos_features.h"
#include "chromeos/dbus/tpm_manager/tpm_manager_client.h"
#include "components/metrics/metrics_service.h"
#include "components/metrics/structured/recorder.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/user_manager/user.h"
#include "components/user_manager/user_manager.h"
#include "components/user_manager/user_type.h"
#include "components/variations/hashing.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"
#include "ui/display/display.h"
#include "ui/events/event_utils.h"

using metrics::ChromeUserMetricsExtension;
using metrics::SampledProfile;
using metrics::SystemProfileProto;

namespace {

inline constexpr char kFeatureManagementLevelFlag[] =
    "feature-management-level";
inline constexpr char kFeatureManagementMaxLevelFlag[] =
    "feature-management-max-level";
inline constexpr char kFeatureManagementScopeFlag[] =
    "feature-management-scope";

void IncrementPrefValue(const char* path, int num_samples) {
  PrefService* pref = g_browser_process->local_state();
  DCHECK(pref);
  int value = pref->GetInteger(path);
  pref->SetInteger(path, value + num_samples);
}

}  // namespace

ChromeOSMetricsProvider::ChromeOSMetricsProvider(
    metrics::MetricsLogUploader::MetricServiceType service_type,
    ChromeOSSystemProfileProvider* cros_system_profile_provider)
    : cros_system_profile_provider_(cros_system_profile_provider) {
  DCHECK(cros_system_profile_provider_);
  if (service_type == metrics::MetricsLogUploader::UMA)
    profile_provider_ = std::make_unique<metrics::ProfileProvider>();
}

ChromeOSMetricsProvider::~ChromeOSMetricsProvider() = default;

// static
void ChromeOSMetricsProvider::RegisterPrefs(PrefRegistrySimple* registry) {
  registry->RegisterIntegerPref(prefs::kStabilityOtherUserCrashCount, 0);
  registry->RegisterIntegerPref(prefs::kStabilityKernelCrashCount, 0);
  registry->RegisterIntegerPref(prefs::kStabilitySystemUncleanShutdownCount, 0);
}

// static
void ChromeOSMetricsProvider::LogCrash(const std::string& crash_type,
                                       int num_samples) {
  if (crash_type == "user") {
    IncrementPrefValue(prefs::kStabilityOtherUserCrashCount, num_samples);
  } else if (crash_type == "kernel") {
    IncrementPrefValue(prefs::kStabilityKernelCrashCount, num_samples);
  } else if (crash_type == "uncleanshutdown") {
    IncrementPrefValue(prefs::kStabilitySystemUncleanShutdownCount,
                       num_samples);
  } else {
    NOTREACHED() << "Unexpected Chrome OS crash type " << crash_type;
  }

  // Wake up metrics logs sending if necessary now that new
  // log data is available.
  g_browser_process->metrics_service()->OnApplicationNotIdle();
}

EnrollmentStatus ChromeOSMetricsProvider::GetEnrollmentStatus() {
  policy::BrowserPolicyConnectorAsh* connector =
      g_browser_process->platform_part()->browser_policy_connector_ash();
  if (!connector)
    return EnrollmentStatus::kErrorGettingStatus;

  return connector->IsDeviceEnterpriseManaged() ? EnrollmentStatus::kManaged
                                                : EnrollmentStatus::kNonManaged;
}

void ChromeOSMetricsProvider::Init() {
  if (profile_provider_)
    profile_provider_->Init();
}

void ChromeOSMetricsProvider::OnDidCreateMetricsLog() {
  cros_system_profile_provider_->OnDidCreateMetricsLog();
  if (!arc::StabilityMetricsManager::Get()) {
    return;
  }
  // Not guaranteed to result in emitting hisotograms when called early on
  // browser startup.
  arc::StabilityMetricsManager::Get()->RecordMetricsToUMA();
  emitted_ = UpdateUserTypeUMA();
}

void ChromeOSMetricsProvider::OnRecordingEnabled() {
  if (profile_provider_)
    profile_provider_->OnRecordingEnabled();
}

void ChromeOSMetricsProvider::OnRecordingDisabled() {
  if (profile_provider_)
    profile_provider_->OnRecordingDisabled();
}

void ChromeOSMetricsProvider::ProvideSystemProfileMetrics(
    metrics::SystemProfileProto* system_profile_proto) {
  cros_system_profile_provider_->ProvideSystemProfileMetrics(
      system_profile_proto);
}

void ChromeOSMetricsProvider::ProvideSuggestedContentMetrics() {
  UMA_HISTOGRAM_BOOLEAN(
      "Apps.AppList.SuggestedContent.Enabled",
      ProfileManager::GetActiveUserProfile()->GetPrefs()->GetBoolean(
          ash::prefs::kSuggestedContentEnabled));
}

void ChromeOSMetricsProvider::ProvideMetrics(
    metrics::SystemProfileProto* system_profile_proto,
    bool should_include_arc_metrics) {
  metrics::SystemProfileProto::Stability* stability_proto =
      system_profile_proto->mutable_stability();
  PrefService* pref = g_browser_process->local_state();
  int count = pref->GetInteger(prefs::kStabilityOtherUserCrashCount);
  if (count) {
    stability_proto->set_other_user_crash_count(count);
    pref->SetInteger(prefs::kStabilityOtherUserCrashCount, 0);
  }

  count = pref->GetInteger(prefs::kStabilityKernelCrashCount);
  if (count) {
    stability_proto->set_kernel_crash_count(count);
    pref->SetInteger(prefs::kStabilityKernelCrashCount, 0);
  }

  count = pref->GetInteger(prefs::kStabilitySystemUncleanShutdownCount);
  if (count) {
    stability_proto->set_unclean_system_shutdown_count(count);
    pref->SetInteger(prefs::kStabilitySystemUncleanShutdownCount, 0);
  }

  // Use current enrollment status for initial stability logs, since it's not
  // likely to change between browser restarts.
  UMA_STABILITY_HISTOGRAM_ENUMERATION(
      "UMA.EnrollmentStatus", GetEnrollmentStatus(),
      // static_cast because we only have macros for stability histograms.
      static_cast<int>(EnrollmentStatus::kMaxValue) + 1);

  if (should_include_arc_metrics) {
    // Record ARC-related stability metrics that should be included in initial
    // stability logs and all regular UMA logs.
    arc::StabilityMetricsManager::Get()->RecordMetricsToUMA();
  }
}

void ChromeOSMetricsProvider::ProvideStabilityMetrics(
    metrics::SystemProfileProto* system_profile_proto) {
  ProvideMetrics(system_profile_proto, /*should_include_arc_metrics=*/true);
}

void ChromeOSMetricsProvider::ProvideCurrentSessionData(
    metrics::ChromeUserMetricsExtension* uma_proto) {
  ProvideSuggestedContentMetrics();
  ProvideMetrics(uma_proto->mutable_system_profile(),
                 /*should_include_arc_metrics=*/!emitted_);
  std::vector<SampledProfile> sampled_profiles;
  if (profile_provider_->GetSampledProfiles(&sampled_profiles)) {
    for (auto& profile : sampled_profiles) {
      uma_proto->add_sampled_profile()->Swap(&profile);
    }
  }
  arc::UpdateEnabledStateByUserTypeUMA();
  if (!emitted_) {
    UpdateUserTypeUMA();
  }
}

void ChromeOSMetricsProvider::ProvideCurrentSessionUKMData() {
  ukm::SourceId source_id = ukm::NoURLSourceId();
  EnrollmentStatus status = GetEnrollmentStatus();
  ukm::builders::ChromeOS_DeviceManagement(source_id)
      .SetEnrollmentStatus(static_cast<int64_t>(status))
      .Record(ukm::UkmRecorder::Get());
}

bool ChromeOSMetricsProvider::UpdateUserTypeUMA() {
  if (!user_manager::UserManager::IsInitialized()) {
    return false;
  }
  const user_manager::User* primary_user =
      user_manager::UserManager::Get()->GetPrimaryUser();
  if (!primary_user) {
    return false;
  }
  user_manager::UserType user_type = primary_user->GetType();
  base::UmaHistogramEnumeration("UMA.PrimaryUserType", user_type);
  return true;
}

ChromeOSHistogramMetricsProvider::ChromeOSHistogramMetricsProvider() = default;

ChromeOSHistogramMetricsProvider::~ChromeOSHistogramMetricsProvider() = default;

bool ChromeOSHistogramMetricsProvider::ProvideHistograms() {
  // The scope type. Used in a histogram; do not modify existing types.
  // see histograms/enums.xml.
  enum {
    FEATURE_MANAGEMENT_REGULAR = 0,
    FEATURE_MANAGEMENT_SOFT_BRANDED = 1,
    FEATURE_MANAGEMENT_HARD_BRANDED = 2,
    kMaxValue = FEATURE_MANAGEMENT_HARD_BRANDED
  } scope_level;

  if (!base::CommandLine::InitializedForCurrentProcess()) {
    return false;
  }

  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  if (!command_line->HasSwitch(kFeatureManagementLevelFlag) ||
      !command_line->HasSwitch(kFeatureManagementMaxLevelFlag) ||
      !command_line->HasSwitch(kFeatureManagementScopeFlag)) {
    return false;
  }
  int feature_level = -1;
  int feature_max_level = -1;
  int scope_level_raw = -1;
  if (!base::StringToInt(
          command_line->GetSwitchValueASCII(kFeatureManagementLevelFlag),
          &feature_level) ||
      !base::StringToInt(
          command_line->GetSwitchValueASCII(kFeatureManagementMaxLevelFlag),
          &feature_max_level) ||
      !base::StringToInt(
          command_line->GetSwitchValueASCII(kFeatureManagementScopeFlag),
          &scope_level_raw)) {
    return false;
  }
  if (feature_level < 0 || feature_max_level < 0 || scope_level_raw < 0 ||
      feature_max_level < feature_level) {
    LOG(ERROR) << "Invalid FeatureLevel arguments: "
               << kFeatureManagementLevelFlag << " (" << feature_level
               << ") or " << kFeatureManagementMaxLevelFlag << " ("
               << feature_max_level << ") or " << kFeatureManagementScopeFlag
               << " (" << scope_level_raw << ")";
    return false;
  }
  if (feature_level == 0 && scope_level_raw == 0) {
    scope_level = FEATURE_MANAGEMENT_REGULAR;
  } else if (feature_level > 0 && scope_level_raw == 0) {
    scope_level = FEATURE_MANAGEMENT_SOFT_BRANDED;
  } else if (feature_level > 0 && scope_level_raw == 1) {
    scope_level = FEATURE_MANAGEMENT_HARD_BRANDED;
  } else {
    LOG(ERROR) << "Invalid ScopeLevel:" << kFeatureManagementLevelFlag << " ("
               << feature_level << ") or " << kFeatureManagementScopeFlag
               << " (" << scope_level_raw << ")";
    return false;
  }

  base::UmaHistogramExactLinear("Platform.Segmentation.FeatureLevel",
                                feature_level, feature_max_level + 1);
  base::UmaHistogramEnumeration("Platform.Segmentation.ScopeLevel",
                                scope_level);
  return true;
}