1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 CHROME_BROWSER_PROFILES_PROFILE_ATTRIBUTES_ENTRY_H_ 6 #define CHROME_BROWSER_PROFILES_PROFILE_ATTRIBUTES_ENTRY_H_ 7 8 #include <stddef.h> 9 10 #include <memory> 11 #include <string> 12 13 #include "base/feature_list.h" 14 #include "base/files/file_path.h" 15 #include "base/gtest_prod_util.h" 16 #include "base/optional.h" 17 #include "base/strings/string16.h" 18 #include "base/time/time.h" 19 #include "base/values.h" 20 #include "third_party/skia/include/core/SkColor.h" 21 #include "ui/gfx/image/image.h" 22 23 class PrefRegistrySimple; 24 class PrefService; 25 class ProfileInfoCache; 26 struct ProfileThemeColors; 27 28 enum class SigninState { 29 kNotSignedIn, 30 kSignedInWithUnconsentedPrimaryAccount, 31 kSignedInWithConsentedPrimaryAccount, 32 }; 33 34 enum class NameForm { 35 kGaiaName, 36 kLocalName, 37 kGaiaAndLocalName, 38 }; 39 40 enum class AccountCategory { kConsumer, kEnterprise }; 41 42 class ProfileAttributesEntry { 43 public: 44 static void RegisterLocalStatePrefs(PrefRegistrySimple* registry); 45 46 ProfileAttributesEntry(); 47 ProfileAttributesEntry(const ProfileAttributesEntry&) = delete; 48 ProfileAttributesEntry& operator=(const ProfileAttributesEntry&) = delete; 49 virtual ~ProfileAttributesEntry() = default; 50 51 // Returns whether the profile name is the concatenation of the Gaia name and 52 // of the local profile name. 53 static bool ShouldConcatenateGaiaAndProfileName(); 54 55 // Gets the name of the profile to be displayed in the User Menu. The name can 56 // be the GAIA name, local profile name or a combination of them. 57 base::string16 GetName() const; 58 // Returns |GetGAIAGivenName()| if not empty. Otherwise, returns 59 // |GetGAIAName()|. 60 base::string16 GetGAIANameToDisplay() const; 61 // Returns true if the profile name has changed. 62 bool HasProfileNameChanged(); 63 // Returns how the value of GetName() gets constructed. 64 NameForm GetNameForm() const; 65 66 // Gets the local profile name. 67 base::string16 GetLocalProfileName() const; 68 69 base::string16 GetShortcutName() const; 70 // Gets the path to the profile. Should correspond to the path passed to 71 // ProfileAttributesStorage::GetProfileAttributesWithPath to get this entry. 72 base::FilePath GetPath() const; 73 base::Time GetActiveTime() const; 74 // Gets the user name of the signed in profile. This is typically the email 75 // address used to sign in and the empty string for profiles that aren't 76 // signed in to chrome. 77 base::string16 GetUserName() const; 78 // Gets the icon used as this profile's avatar. High res icon are downloaded 79 // only if `download_high_res` is true, otherwise a low-res fallback is 80 // returned. 81 // TODO(crbug.com/1100835): Rename |size_for_placeholder_avatar| to |size| and 82 // make this function resize all avatars appropriately. Remove the default 83 // value of |size_for_placeholder_avatar| when all callsites pass some value. 84 // Consider adding a |shape| parameter and get rid of 85 // profiles::GetSizedAvatarIcon(). 86 gfx::Image GetAvatarIcon(int size_for_placeholder_avatar = 74, 87 bool use_high_res_file = true) const; 88 std::string GetLocalAuthCredentials() const; 89 std::string GetPasswordChangeDetectionToken() const; 90 // Returns true if the profile is currently running any background apps. Note 91 // that a return value of false could mean an error in collection or that 92 // there are currently no background apps running. However, the action which 93 // results is the same in both cases (thus far). 94 bool GetBackgroundStatus() const; 95 // Gets the GAIA full name associated with this profile if it's signed in. 96 base::string16 GetGAIAName() const; 97 // Gets the GAIA given name associated with this profile if it's signed in. 98 base::string16 GetGAIAGivenName() const; 99 // Gets the opaque string representation of the profile's GAIA ID if it's 100 // signed in. 101 std::string GetGAIAId() const; 102 // Returns the GAIA picture for the given profile. This may return NULL 103 // if the profile does not have a GAIA picture or if the picture must be 104 // loaded from disk. 105 const gfx::Image* GetGAIAPicture() const; 106 // Returns true if the profile displays a GAIA picture instead of one of the 107 // locally bundled icons. 108 bool IsUsingGAIAPicture() const; 109 // Returns true if a GAIA picture has been loaded or has failed to load. 110 bool IsGAIAPictureLoaded() const; 111 // Returns true if the profile is signed in as a supervised user. 112 bool IsSupervised() const; 113 // Returns true if the profile is signed in as a child account. 114 bool IsChild() const; 115 // Returns true if the profile is a supervised user but not a child account. 116 bool IsLegacySupervised() const; 117 bool IsOmitted() const; 118 bool IsSigninRequired() const; 119 // Gets the supervised user ID of the profile's signed in account, if it's a 120 // supervised user. 121 std::string GetSupervisedUserId() const; 122 // Returns true if the profile is an ephemeral profile. 123 bool IsEphemeral() const; 124 // Returns true if the profile is using a default name, typically of the 125 // format "Person %d". 126 bool IsUsingDefaultName() const; 127 // Returns Signin state. 128 SigninState GetSigninState() const; 129 // Returns true if the profile is signed in. 130 bool IsAuthenticated() const; 131 // Returns true if the Profile is using the default avatar, which is one of 132 // the profile icons selectable at profile creation. 133 bool IsUsingDefaultAvatar() const; 134 // Returns true if the profile is signed in but is in an authentication error 135 // state. 136 bool IsAuthError() const; 137 // Indicates that profile was signed in through native OS credential provider. 138 bool IsSignedInWithCredentialProvider() const; 139 // Returns the index of the default icon used by the profile. 140 size_t GetAvatarIconIndex() const; 141 // Returns the colors specified by the profile theme, or default colors if no 142 // theme is specified for the profile. 143 ProfileThemeColors GetProfileThemeColors() const; 144 // Returns the colors specified by the profile theme, or empty if no theme is 145 // set for the profile. 146 base::Optional<ProfileThemeColors> GetProfileThemeColorsIfSet() const; 147 // Returns the metrics bucket this profile should be recorded in. 148 // Note: The bucket index is assigned once and remains the same all time. 0 is 149 // reserved for the guest profile. 150 size_t GetMetricsBucketIndex(); 151 // Returns the hosted domain for the current signed-in account. Returns empty 152 // string if there is no signed-in account and returns |kNoHostedDomainFound| 153 // if the signed-in account has no hosted domain (such as when it is a 154 // standard gmail.com account). Unlike for other string getters, the returned 155 // value is UTF8 encoded. 156 std::string GetHostedDomain() const; 157 158 void SetLocalProfileName(const base::string16& name); 159 void SetShortcutName(const base::string16& name); 160 void SetActiveTimeToNow(); 161 void SetIsOmitted(bool is_omitted); 162 void SetSupervisedUserId(const std::string& id); 163 void SetLocalAuthCredentials(const std::string& auth); 164 void SetPasswordChangeDetectionToken(const std::string& token); 165 void SetBackgroundStatus(bool running_background_apps); 166 void SetGAIAName(const base::string16& name); 167 void SetGAIAGivenName(const base::string16& name); 168 void SetGAIAPicture(const std::string& image_url_with_size, gfx::Image image); 169 void SetIsUsingGAIAPicture(bool value); 170 void SetIsSigninRequired(bool value); 171 void SetSignedInWithCredentialProvider(bool value); 172 void SetIsEphemeral(bool value); 173 void SetIsUsingDefaultName(bool value); 174 void SetIsUsingDefaultAvatar(bool value); 175 void SetIsAuthError(bool value); 176 void SetAvatarIconIndex(size_t icon_index); 177 // base::nullopt resets colors to default. 178 void SetProfileThemeColors(const base::Optional<ProfileThemeColors>& colors); 179 180 // Unlike for other string setters, the argument is expected to be UTF8 181 // encoded. 182 void SetHostedDomain(std::string hosted_domain); 183 184 void SetAuthInfo(const std::string& gaia_id, 185 const base::string16& user_name, 186 bool is_consented_primary_account); 187 188 // Update info about accounts. These functions are idempotent, only the first 189 // call for a given input matters. 190 void AddAccountName(const std::string& name); 191 void AddAccountCategory(AccountCategory category); 192 193 // Clears info about all accounts that have been added in the past via 194 // AddAccountName() and AddAccountCategory(). 195 void ClearAccountNames(); 196 void ClearAccountCategories(); 197 198 // Lock/Unlock the profile, should be called only if force-sign-in is enabled. 199 void LockForceSigninProfile(bool is_lock); 200 201 // Records aggregate metrics about all accounts used in this profile (added 202 // via AddAccount* functions). 203 void RecordAccountMetrics() const; 204 205 // TODO(crbug.com/866790): Check it is not used anymore and remove it. 206 static const char kSupervisedUserId[]; 207 static const char kIsOmittedFromProfileListKey[]; 208 static const char kAvatarIconKey[]; 209 static const char kBackgroundAppsKey[]; 210 static const char kProfileIsEphemeral[]; 211 static const char kUserNameKey[]; 212 static const char kGAIAIdKey[]; 213 static const char kIsConsentedPrimaryAccountKey[]; 214 static const char kNameKey[]; 215 static const char kIsUsingDefaultNameKey[]; 216 217 private: 218 friend class ProfileInfoCache; 219 friend class ProfileThemeUpdateServiceBrowserTest; 220 FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest, 221 EntryInternalAccessors); 222 FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest, ProfileActiveTime); 223 FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest, 224 DownloadHighResAvatarTest); 225 226 void Initialize(ProfileInfoCache* cache, 227 const base::FilePath& path, 228 PrefService* prefs); 229 230 base::string16 GetLastNameToDisplay() const; 231 232 // Returns true if: 233 // - The user has chosen a local profile name on purpose. One exception where 234 // we don't show the local profile name, is when it is equal to the 235 // GAIA name. 236 // - If two profiles have the same GAIA name and we need to show the local 237 // profile name to clear ambiguity. 238 bool ShouldShowProfileLocalName( 239 const base::string16& gaia_name_to_display) const; 240 241 // Loads or uses an already loaded high resolution image of the generic 242 // profile avatar. 243 const gfx::Image* GetHighResAvatar() const; 244 245 // Generates the colored placeholder avatar icon for the given |size|. 246 gfx::Image GetPlaceholderAvatarIcon(int size) const; 247 248 // Returns if this profile has accounts (signed-in or signed-out) with 249 // different account names. This is approximate as only a short hash of an 250 // account name is stored so there can be false negatives. 251 bool HasMultipleAccountNames() const; 252 // Returns if this profile has both consumer and enterprise accounts 253 // (regarding both signed-in and signed-out accounts). 254 bool HasBothAccountCategories() const; 255 256 // Records aggregate metrics about all accounts used in this profile. 257 void RecordAccountCategoriesMetric() const; 258 void RecordAccountNamesMetric() const; 259 260 // Loads and saves the data to the local state. 261 const base::Value* GetEntryData() const; 262 void SetEntryData(base::Value data); 263 264 // Internal getter that returns a base::Value*, or nullptr if the key is not 265 // present. 266 const base::Value* GetValue(const char* key) const; 267 268 // Internal getters that return basic data types. If the key is not present, 269 // or if the data is in a wrong data type, return empty string, 0.0, false or 270 // -1 depending on the target data type. We do not assume that the data type 271 // is correct because the local state file can be modified by a third party. 272 std::string GetString(const char* key) const; 273 base::string16 GetString16(const char* key) const; 274 double GetDouble(const char* key) const; 275 bool GetBool(const char* key) const; 276 int GetInteger(const char* key) const; 277 278 // Internal getter that returns one of the profile theme colors or 279 // base::nullopt if the key is not present. 280 base::Optional<SkColor> GetProfileThemeColor(const char* key) const; 281 282 // Type checking. Only IsDouble is implemented because others do not have 283 // callsites. 284 bool IsDouble(const char* key) const; 285 286 // Internal setters that accept basic data types. Return if the original data 287 // is different from the new data, i.e. whether actual update is done. 288 bool SetString(const char* key, std::string value); 289 bool SetString16(const char* key, base::string16 value); 290 bool SetDouble(const char* key, double value); 291 bool SetBool(const char* key, bool value); 292 bool SetInteger(const char* key, int value); 293 294 // Clears value stored for |key|. Returns if the original data is different 295 // from the new data, i.e. whether actual update is done. 296 bool ClearValue(const char* key); 297 298 // These members are an implementation detail meant to smooth the migration 299 // of the ProfileInfoCache to the ProfileAttributesStorage interface. They can 300 // be safely removed once the ProfileInfoCache stops using indices 301 // internally. 302 // TODO(anthonyvd): Remove ProfileInfoCache related implementation details 303 // when this class holds the members required to fulfill its own contract. 304 size_t profile_index() const; 305 306 ProfileInfoCache* profile_info_cache_ = nullptr; 307 PrefService* prefs_ = nullptr; 308 base::FilePath profile_path_; 309 std::string storage_key_; 310 base::string16 last_name_to_display_; 311 312 // A separate boolean flag indicates whether the signin is required when force 313 // signin is enabled. So that the profile locked status will be stored in 314 // memory only and can be easily reset once the policy is turned off. 315 bool is_force_signin_profile_locked_ = false; 316 bool is_force_signin_enabled_; 317 }; 318 319 #endif // CHROME_BROWSER_PROFILES_PROFILE_ATTRIBUTES_ENTRY_H_ 320