1 // Copyright 2019 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_DEMOGRAPHICS_USER_DEMOGRAPHICS_H_ 6 #define COMPONENTS_METRICS_DEMOGRAPHICS_USER_DEMOGRAPHICS_H_ 7 8 #include "base/time/time.h" 9 #include "build/build_config.h" 10 #include "third_party/metrics_proto/user_demographics.pb.h" 11 12 class PrefService; 13 class PrefRegistrySimple; 14 15 namespace metrics { 16 17 // Default value for user gender when no value has been set. 18 constexpr int kUserDemographicsGenderDefaultValue = -1; 19 20 // Default value for user gender enum when no value has been set. 21 constexpr UserDemographicsProto_Gender kUserDemographicGenderDefaultEnumValue = 22 UserDemographicsProto_Gender_Gender_MIN; 23 24 // Default value for user birth year when no value has been set. 25 constexpr int kUserDemographicsBirthYearDefaultValue = -1; 26 27 // Default value for birth year offset when no value has been set. Set to a 28 // really high value that |kUserDemographicsBirthYearNoiseOffsetRange| will 29 // never go over. 30 constexpr int kUserDemographicsBirthYearNoiseOffsetDefaultValue = 100; 31 32 // Boundary of the +/- range in years within witch to randomly pick an offset to 33 // add to the user birth year. This adds noise to the birth year to not allow 34 // someone to accurately know a user's age. Possible offsets range from -2 to 2. 35 constexpr int kUserDemographicsBirthYearNoiseOffsetRange = 2; 36 37 // Minimal user age in years to provide demographics for. 38 constexpr int kUserDemographicsMinAgeInYears = 20; 39 40 // Max user age to provide demopgrahics for. 41 constexpr int kUserDemographicsMaxAgeInYears = 85; 42 43 // Root dictionary pref to store the user's birth year and gender that are 44 // provided by the sync server. This is a read-only syncable priority pref on 45 // all platforms except ChromeOS Ash, where it is a syncable OS-level priority 46 // pref. 47 #if !BUILDFLAG(IS_CHROMEOS_ASH) 48 inline constexpr char kSyncDemographicsPrefName[] = "sync.demographics"; 49 #else 50 inline constexpr char kSyncOsDemographicsPrefName[] = "sync.os_demographics"; 51 // TODO(crbug.com/40240008): Make this non-syncable (on Ash only) after full 52 // rollout of the syncable os priority pref; then delete it locally from Ash 53 // devices. 54 inline constexpr char kSyncDemographicsPrefName[] = "sync.demographics"; 55 #endif 56 57 // Stores a "secret" offset that is used to randomize the birth year for metrics 58 // reporting. This value should not be logged to UMA directly; instead, it 59 // should be summed with the kSyncDemographicsBirthYear. This value is generated 60 // locally on the client the first time a user begins to merge birth year data 61 // into their UMA reports. 62 inline constexpr char kUserDemographicsBirthYearOffsetPrefName[] = 63 "demographics_birth_year_offset"; 64 // TODO(crbug.com/40240008): Delete after 2023/09 65 inline constexpr char kDeprecatedDemographicsBirthYearOffsetPrefName[] = 66 "sync.demographics_birth_year_offset"; 67 68 // These are not prefs, they are paths inside of kSyncDemographics. 69 70 // This pref value is subordinate to the kSyncDemographics dictionary pref and 71 // is synced to the client. It stores the self-reported birth year of the 72 // syncing user. as provided by the sync server. This value should not be logged 73 // to UMA directly; instead, it should be summed with the 74 // kSyncDemographicsBirthYearNoiseOffset. 75 inline constexpr char kSyncDemographicsBirthYearPath[] = "birth_year"; 76 77 // This pref value is subordinate to the kSyncDemographics dictionary pref and 78 // is synced to the client. It stores the self-reported gender of the syncing 79 // user, as provided by the sync server. The gender is encoded using the Gender 80 // enum defined in UserDemographicsProto 81 // (see third_party/metrics_proto/user_demographics.proto). 82 inline constexpr char kSyncDemographicsGenderPath[] = "gender"; 83 84 // Container of user demographics. 85 struct UserDemographics { 86 int birth_year = 0; 87 UserDemographicsProto_Gender gender = UserDemographicsProto::Gender_MIN; 88 }; 89 90 // Represents the status of providing user demographics (noised birth year and 91 // gender) that are logged to UMA. Entries of the enum should not be renumbered 92 // and numeric values should never be reused. Please keep in sync with 93 // "UserDemographicsStatus" in src/tools/metrics/histograms/enums.xml. There 94 // should only be one entry representing demographic data that is ineligible to 95 // be provided. A finer grained division of kIneligibleDemographicsData (e.g., 96 // INVALID_BIRTH_YEAR) might help inferring categories of demographics that 97 // should not be exposed to protect privacy. 98 enum class UserDemographicsStatus { 99 // Could get the user's noised birth year and gender. 100 kSuccess = 0, 101 // Sync is not enabled. 102 kSyncNotEnabled = 1, 103 // User's birth year and gender are ineligible to be provided either because 104 // some of them don't exist (missing data) or some of them are not eligible to 105 // be provided. 106 kIneligibleDemographicsData = 2, 107 // Could not get the time needed to compute the user's age. 108 kCannotGetTime = 3, 109 // There is more than one Profile for the Chrome browser. This entry is used 110 // by the DemographicMetricsProvider client. 111 kMoreThanOneProfile = 4, 112 // There is no sync service available for the loaded Profile. This entry is 113 // used by the DemographicMetricsProvider client. 114 kNoSyncService = 5, 115 // Upper boundary of the enum that is needed for the histogram. 116 kMaxValue = kNoSyncService 117 }; 118 119 // Container and handler for data related to the retrieval of user demographics. 120 // Holds either valid demographics data or an error code. 121 class UserDemographicsResult { 122 public: 123 // Builds a UserDemographicsResult that contains user demographics and has a 124 // UserDemographicsStatus::kSuccess status. 125 static UserDemographicsResult ForValue(UserDemographics value); 126 127 // Builds a UserDemographicsResult that does not have user demographics (only 128 // default values) and has a status other than 129 // UserDemographicsStatus::kSuccess. 130 static UserDemographicsResult ForStatus(UserDemographicsStatus status); 131 132 // Determines whether demographics could be successfully retrieved. 133 bool IsSuccess() const; 134 135 // Gets the status of the result. 136 UserDemographicsStatus status() const; 137 138 // Gets the value of the result which will contain data when status() is 139 // UserDemographicsStatus::kSuccess. 140 const UserDemographics& value() const; 141 142 private: 143 UserDemographicsResult(UserDemographics value, UserDemographicsStatus status); 144 145 UserDemographics value_; 146 UserDemographicsStatus status_ = UserDemographicsStatus::kMaxValue; 147 }; 148 149 // Registers the local state preferences that are needed to persist demographics 150 // information exposed via GetUserNoisedBirthYearAndGenderFromPrefs(). 151 void RegisterDemographicsLocalStatePrefs(PrefRegistrySimple* registry); 152 153 // Registers the profile preferences that are needed to persist demographics 154 // information exposed via GetUserNoisedBirthYearAndGenderFromPrefs(). 155 void RegisterDemographicsProfilePrefs(PrefRegistrySimple* registry); 156 157 // Clears the profile's demographics-related preferences containing user data. 158 // This excludes the internal birth year offset. 159 void ClearDemographicsPrefs(PrefService* profile_prefs); 160 161 // Gets the synced user’s birth year and gender from |profile_prefs|, and noise 162 // from |local_state|. See docs for metrics::DemographicMetricsProvider in 163 // components/metrics/demographic_metrics_provider.h for more details. Returns 164 // an error status with an empty value when the user's birth year or gender 165 // cannot be provided. You need to provide an accurate |now| time that 166 // represents the current time. 167 UserDemographicsResult GetUserNoisedBirthYearAndGenderFromPrefs( 168 base::Time now, 169 PrefService* local_state, 170 PrefService* profile_prefs); 171 172 } // namespace metrics 173 174 #endif // COMPONENTS_METRICS_DEMOGRAPHICS_USER_DEMOGRAPHICS_H_ 175