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. 4 5 #ifndef COMPONENTS_METRICS_ANDROID_METRICS_HELPER_H_ 6 #define COMPONENTS_METRICS_ANDROID_METRICS_HELPER_H_ 7 8 #include <string> 9 10 #include "components/prefs/pref_registry_simple.h" 11 #include "components/prefs/pref_service.h" 12 13 namespace metrics { 14 15 namespace prefs { 16 constexpr char kVersionCodePref[] = "android_system_info.last_version_code"; 17 } 18 19 // Whether 64-bit and/or 32-bit apps can be installed on this device/OS. 20 // 21 // These values are persisted to logs. Entries should not be renumbered and 22 // numeric values should never be reused. See CpuAbiBitnessSupport in enums.xml. 23 enum class CpuAbiBitnessSupport { 24 kNeither = 0, 25 k32bitOnly = 1, 26 k64bitOnly = 2, 27 k32And64bit = 3, 28 kMaxValue = k32And64bit, 29 }; 30 31 // AndroidMetricsHelper is responsible for helping to log information related to 32 // system-level information about the Android device as well as the process. 33 class AndroidMetricsHelper { 34 public: 35 AndroidMetricsHelper(const AndroidMetricsHelper&) = delete; 36 AndroidMetricsHelper& operator=(const AndroidMetricsHelper&) = delete; 37 ~AndroidMetricsHelper() = default; 38 39 static AndroidMetricsHelper* GetInstance(); CreateInstanceForTest(const std::string & version_code,bool has_abilist32,bool has_abilist64)40 static AndroidMetricsHelper* CreateInstanceForTest( 41 const std::string& version_code, 42 bool has_abilist32, 43 bool has_abilist64) { 44 return new AndroidMetricsHelper(version_code, has_abilist32, has_abilist64); 45 } 46 version_code_int()47 int version_code_int() const { return version_code_int_; } cpu_abi_bitness_support()48 CpuAbiBitnessSupport cpu_abi_bitness_support() const { 49 return cpu_abi_bitness_support_; 50 } 51 52 // |on_did_create_metrics_log| denotes whether data is emitted in 53 // OnDidCreateMetricsLog, as opposed to in ProvidePreviousSessionData. 54 void EmitHistograms(PrefService* local_state, bool on_did_create_metrics_log); 55 56 static void RegisterPrefs(PrefRegistrySimple* registry); 57 58 // Made public for testing. 59 static void SaveLocalState(PrefService* local_state, int version_code_int); 60 ResetGlobalStateForTesting()61 static void ResetGlobalStateForTesting() { local_state_saved_ = false; } 62 63 private: 64 friend struct AndroidMetricsHelperSingletonTraits; 65 66 AndroidMetricsHelper(const std::string& version_code, 67 bool has_abilist32, 68 bool has_abilist64); 69 70 int version_code_int_ = 0; 71 CpuAbiBitnessSupport cpu_abi_bitness_support_ = 72 CpuAbiBitnessSupport::kNeither; 73 74 static inline bool local_state_saved_ = false; 75 }; 76 77 } // namespace metrics 78 79 #endif // COMPONENTS_METRICS_ANDROID_METRICS_HELPER_H_ 80