1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/metrics/android_metrics_helper.h"
9 #include "base/metrics/histogram_functions.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "build/build_config.h"
12 #include "components/prefs/pref_registry_simple.h"
13 #include "components/prefs/pref_service.h"
15 #if BUILDFLAG(IS_ANDROID)
16 #include <sys/system_properties.h>
17 #include "base/android/build_info.h"
18 #endif // BUILDFLAG(IS_ANDROID)
23 AndroidMetricsHelper* AndroidMetricsHelper::GetInstance() {
24 #if BUILDFLAG(IS_ANDROID)
25 char abilist32[PROP_VALUE_MAX];
26 char abilist64[PROP_VALUE_MAX];
27 static AndroidMetricsHelper instance(
28 base::android::BuildInfo::GetInstance()->package_version_code(),
29 __system_property_get("ro.product.cpu.abilist32", abilist32) > 0,
30 __system_property_get("ro.product.cpu.abilist64", abilist64) > 0);
32 static AndroidMetricsHelper instance("", false, false);
37 AndroidMetricsHelper::AndroidMetricsHelper(const std::string& version_code,
40 cpu_abi_bitness_support_ =
41 has_abilist32 ? (has_abilist64 ? CpuAbiBitnessSupport::k32And64bit
42 : CpuAbiBitnessSupport::k32bitOnly)
43 : (has_abilist64 ? CpuAbiBitnessSupport::k64bitOnly
44 : CpuAbiBitnessSupport::kNeither);
46 if (base::StringToInt(version_code, &output)) {
47 version_code_int_ = output;
51 void AndroidMetricsHelper::EmitHistograms(PrefService* local_state,
52 bool on_did_create_metrics_log) {
53 if (on_did_create_metrics_log) {
54 if (version_code_int_) {
55 // The values won't change within the session, so save only once.
56 if (!local_state_saved_) {
57 // version_code_int_ can change across session. Save it so that it can
58 // be restored in case the session dies before logs are flushed.
59 // cpu_abi_bitness_support_ doesn't change across sessions (that'd
60 // require OS reinstall), so no need to save it. It can be reliably
61 // reconstructed in the next session.
62 SaveLocalState(local_state, version_code_int_);
63 local_state_saved_ = true;
66 // This may change across sessions, so log it only for current session.
67 base::UmaHistogramSparse("Android.VersionCode", version_code_int_);
70 // Make sure we didn't overwrite the stored state yet.
71 CHECK(!local_state_saved_);
72 // For previous session, don't log version_code_int_ as version code may
73 // have changed (e.g. version update). Log saved value instead.
74 int restored_version_code_int =
75 local_state->GetInteger(prefs::kVersionCodePref);
76 if (restored_version_code_int) {
77 base::UmaHistogramSparse("Android.VersionCode",
78 restored_version_code_int);
82 // cpu_abi_bitness_support_ doesn't change across sessions (that'd require OS
83 // reinstall), so no need to load it, just use the current value.
84 base::UmaHistogramEnumeration("Android.CpuAbiBitnessSupport",
85 cpu_abi_bitness_support_);
89 void AndroidMetricsHelper::RegisterPrefs(PrefRegistrySimple* registry) {
90 registry->RegisterIntegerPref(prefs::kVersionCodePref, 0);
91 local_state_saved_ = false;
95 void AndroidMetricsHelper::SaveLocalState(PrefService* local_state,
96 int version_code_int) {
97 local_state->SetInteger(prefs::kVersionCodePref, version_code_int);
100 } // namespace metrics