blob: c469b38b8ca5797dce523ac8e480e0ff4467c27c [file] [log] [blame]
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_PROFILES_PROFILE_ATTRIBUTES_ENTRY_H_
#define CHROME_BROWSER_PROFILES_PROFILE_ATTRIBUTES_ENTRY_H_
#include <stddef.h>
#include <memory>
#include <string>
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/gtest_prod_util.h"
#include "base/optional.h"
#include "base/time/time.h"
#include "base/values.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/image/image.h"
class PrefRegistrySimple;
class PrefService;
class ProfileInfoCache;
struct ProfileThemeColors;
enum class SigninState {
kNotSignedIn,
kSignedInWithUnconsentedPrimaryAccount,
kSignedInWithConsentedPrimaryAccount,
};
enum class NameForm {
kGaiaName,
kLocalName,
kGaiaAndLocalName,
};
enum class AccountCategory { kConsumer, kEnterprise };
class ProfileAttributesEntry {
public:
static void RegisterLocalStatePrefs(PrefRegistrySimple* registry);
ProfileAttributesEntry();
ProfileAttributesEntry(const ProfileAttributesEntry&) = delete;
ProfileAttributesEntry& operator=(const ProfileAttributesEntry&) = delete;
virtual ~ProfileAttributesEntry() = default;
// Gets the name of the profile to be displayed in the User Menu. The name can
// be the GAIA name, local profile name or a combination of them.
std::u16string GetName() const;
// Returns |GetGAIAGivenName()| if not empty. Otherwise, returns
// |GetGAIAName()|.
std::u16string GetGAIANameToDisplay() const;
// Returns true if the profile name has changed.
bool HasProfileNameChanged();
// Returns how the value of GetName() gets constructed.
NameForm GetNameForm() const;
// Gets the local profile name.
std::u16string GetLocalProfileName() const;
std::u16string GetShortcutName() const;
// Gets the path to the profile. Should correspond to the path passed to
// ProfileAttributesStorage::GetProfileAttributesWithPath to get this entry.
base::FilePath GetPath() const;
base::Time GetActiveTime() const;
// Gets the user name of the signed in profile. This is typically the email
// address used to sign in and the empty string for profiles that aren't
// signed in to chrome.
std::u16string GetUserName() const;
// Gets the icon used as this profile's avatar. High res icon are downloaded
// only if `download_high_res` is true, otherwise a low-res fallback is
// returned.
// TODO(crbug.com/1100835): Rename |size_for_placeholder_avatar| to |size| and
// make this function resize all avatars appropriately. Remove the default
// value of |size_for_placeholder_avatar| when all callsites pass some value.
// Consider adding a |shape| parameter and get rid of
// profiles::GetSizedAvatarIcon().
gfx::Image GetAvatarIcon(int size_for_placeholder_avatar = 74,
bool use_high_res_file = true) const;
// Returns true if the profile is currently running any background apps. Note
// that a return value of false could mean an error in collection or that
// there are currently no background apps running. However, the action which
// results is the same in both cases (thus far).
bool GetBackgroundStatus() const;
// Gets the GAIA full name associated with this profile if it's signed in.
std::u16string GetGAIAName() const;
// Gets the GAIA given name associated with this profile if it's signed in.
std::u16string GetGAIAGivenName() const;
// Gets the opaque string representation of the profile's GAIA ID if it's
// signed in.
std::string GetGAIAId() const;
// Returns the GAIA picture for the given profile. This may return NULL
// if the profile does not have a GAIA picture or if the picture must be
// loaded from disk.
const gfx::Image* GetGAIAPicture() const;
// Returns true if the profile displays a GAIA picture instead of one of the
// locally bundled icons.
bool IsUsingGAIAPicture() const;
// Returns true if a GAIA picture has been loaded or has failed to load.
bool IsGAIAPictureLoaded() const;
// Returns true if the profile is signed in as a supervised user.
bool IsSupervised() const;
// Returns true if the profile is signed in as a child account.
bool IsChild() const;
// Returns true if the profile should not be displayed to the user in the
// list of profiles.
bool IsOmitted() const;
bool IsSigninRequired() const;
// Gets the supervised user ID of the profile's signed in account, if it's a
// supervised user.
std::string GetSupervisedUserId() const;
// Returns true if the profile is an ephemeral profile.
bool IsEphemeral() const;
// Returns true if the profile is a Guest profile.
// Only ephemeral Guest profiles are stored in profile attributes and
// therefore a Guest profile here is always ephemeral as well.
bool IsGuest() const;
// Returns true if the profile is using a default name, typically of the
// format "Person %d".
bool IsUsingDefaultName() const;
// Returns Signin state.
SigninState GetSigninState() const;
// Returns true if the profile is signed in.
bool IsAuthenticated() const;
// Returns true if the Profile is using the default avatar, which is one of
// the profile icons selectable at profile creation.
bool IsUsingDefaultAvatar() const;
// Returns true if the profile is signed in but is in an authentication error
// state.
bool IsAuthError() const;
// Indicates that profile was signed in through native OS credential provider.
bool IsSignedInWithCredentialProvider() const;
// Returns the index of the default icon used by the profile.
size_t GetAvatarIconIndex() const;
// Returns the colors specified by the profile theme, or default colors if no
// theme is specified for the profile.
ProfileThemeColors GetProfileThemeColors() const;
// Returns the colors specified by the profile theme, or empty if no theme is
// set for the profile.
base::Optional<ProfileThemeColors> GetProfileThemeColorsIfSet() const;
// Returns the metrics bucket this profile should be recorded in.
// Note: The bucket index is assigned once and remains the same all time. 0 is
// reserved for the guest profile.
size_t GetMetricsBucketIndex();
// Returns the hosted domain for the current signed-in account. Returns empty
// string if there is no signed-in account and returns |kNoHostedDomainFound|
// if the signed-in account has no hosted domain (such as when it is a
// standard gmail.com account). Unlike for other string getters, the returned
// value is UTF8 encoded.
std::string GetHostedDomain() const;
// |is_using_default| should be set to false for non default profile names.
void SetLocalProfileName(const std::u16string& name, bool is_default_name);
void SetShortcutName(const std::u16string& name);
void SetActiveTimeToNow();
// Only ephemeral profiles can be set as omitted.
void SetIsOmitted(bool is_omitted);
void SetSupervisedUserId(const std::string& id);
void SetBackgroundStatus(bool running_background_apps);
void SetGAIAName(const std::u16string& name);
void SetGAIAGivenName(const std::u16string& name);
void SetGAIAPicture(const std::string& image_url_with_size, gfx::Image image);
void SetIsUsingGAIAPicture(bool value);
void SetIsSigninRequired(bool value);
void SetSignedInWithCredentialProvider(bool value);
// Only non-omitted profiles can be set as non-ephemeral. It's the
// responsibility of the caller to make sure that the entry is set as
// non-ephemeral only if prefs::kForceEphemeralProfiles is false.
void SetIsEphemeral(bool value);
void SetIsGuest(bool value);
// TODO(msalama): Remove this function.
void SetIsUsingDefaultName(bool value);
void SetIsUsingDefaultAvatar(bool value);
void SetIsAuthError(bool value);
void SetAvatarIconIndex(size_t icon_index);
// base::nullopt resets colors to default.
void SetProfileThemeColors(const base::Optional<ProfileThemeColors>& colors);
// Unlike for other string setters, the argument is expected to be UTF8
// encoded.
void SetHostedDomain(std::string hosted_domain);
void SetAuthInfo(const std::string& gaia_id,
const std::u16string& user_name,
bool is_consented_primary_account);
// Update info about accounts. These functions are idempotent, only the first
// call for a given input matters.
void AddAccountName(const std::string& name);
void AddAccountCategory(AccountCategory category);
// Clears info about all accounts that have been added in the past via
// AddAccountName() and AddAccountCategory().
void ClearAccountNames();
void ClearAccountCategories();
// Lock/Unlock the profile, should be called only if force-sign-in is enabled.
void LockForceSigninProfile(bool is_lock);
// Records aggregate metrics about all accounts used in this profile (added
// via AddAccount* functions).
void RecordAccountMetrics() const;
// TODO(crbug/1155729): Check it is not used anymore for deprecated supervised
// users and remove it.
static const char kSupervisedUserId[];
static const char kAvatarIconKey[];
static const char kBackgroundAppsKey[];
static const char kProfileIsEphemeral[];
static const char kProfileIsGuest[];
static const char kUserNameKey[];
static const char kGAIAIdKey[];
static const char kIsConsentedPrimaryAccountKey[];
static const char kNameKey[];
static const char kIsUsingDefaultNameKey[];
private:
friend class ProfileInfoCache;
friend class ProfileThemeUpdateServiceBrowserTest;
FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest,
EntryInternalAccessors);
FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest, ProfileActiveTime);
FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest,
DownloadHighResAvatarTest);
// Initializes the current entry instance. The callers must subsequently call
// InitializeLastNameToDisplay() for this entry.
void Initialize(ProfileInfoCache* cache,
const base::FilePath& path,
PrefService* prefs);
// Sets the initial name of the profile to be displayed. The name might depend
// on other's profiles names so this must be called only after all profiles
// has been initialized.
void InitializeLastNameToDisplay();
std::u16string GetLastNameToDisplay() const;
// Returns true if:
// - The user has chosen a local profile name on purpose. One exception where
// we don't show the local profile name, is when it is equal to the
// GAIA name.
// - If two profiles have the same GAIA name and we need to show the local
// profile name to clear ambiguity.
bool ShouldShowProfileLocalName(
const std::u16string& gaia_name_to_display) const;
// Loads or uses an already loaded high resolution image of the generic
// profile avatar.
const gfx::Image* GetHighResAvatar() const;
// Generates the colored placeholder avatar icon for the given |size|.
gfx::Image GetPlaceholderAvatarIcon(int size) const;
// Returns if this profile has accounts (signed-in or signed-out) with
// different account names. This is approximate as only a short hash of an
// account name is stored so there can be false negatives.
bool HasMultipleAccountNames() const;
// Returns if this profile has both consumer and enterprise accounts
// (regarding both signed-in and signed-out accounts).
bool HasBothAccountCategories() const;
// Records aggregate metrics about all accounts used in this profile.
void RecordAccountCategoriesMetric() const;
void RecordAccountNamesMetric() const;
// Loads and saves the data to the local state.
const base::Value* GetEntryData() const;
void SetEntryData(base::Value data);
// Internal getter that returns a base::Value*, or nullptr if the key is not
// present.
const base::Value* GetValue(const char* key) const;
// Internal getters that return basic data types. If the key is not present,
// or if the data is in a wrong data type, return empty string, 0.0, false or
// -1 depending on the target data type. We do not assume that the data type
// is correct because the local state file can be modified by a third party.
std::string GetString(const char* key) const;
std::u16string GetString16(const char* key) const;
double GetDouble(const char* key) const;
bool GetBool(const char* key) const;
int GetInteger(const char* key) const;
// Internal getter that returns one of the profile theme colors or
// base::nullopt if the key is not present.
base::Optional<SkColor> GetProfileThemeColor(const char* key) const;
// Type checking. Only IsDouble is implemented because others do not have
// callsites.
bool IsDouble(const char* key) const;
// Internal setters that accept basic data types. Return if the original data
// is different from the new data, i.e. whether actual update is done.
bool SetString(const char* key, std::string value);
bool SetString16(const char* key, std::u16string value);
bool SetDouble(const char* key, double value);
bool SetBool(const char* key, bool value);
bool SetInteger(const char* key, int value);
// Clears value stored for |key|. Returns if the original data is different
// from the new data, i.e. whether actual update is done.
bool ClearValue(const char* key);
// Migrate/cleanup deprecated keys in profile attributes. Over time, long
// deprecated keys should be removed as new ones are added, but this call
// should never go away (even if it becomes an empty call for some time) as it
// should remain *the* place to drop deprecated profile attributes keys at.
void MigrateObsoleteProfileAttributes();
// Internal version of `SetIsOmitted()` that doesn't trigger any
// notifications.
void SetIsOmittedInternal(bool is_omitted);
// These members are an implementation detail meant to smooth the migration
// of the ProfileInfoCache to the ProfileAttributesStorage interface. They can
// be safely removed once the ProfileInfoCache stops using indices
// internally.
// TODO(anthonyvd): Remove ProfileInfoCache related implementation details
// when this class holds the members required to fulfill its own contract.
size_t profile_index() const;
ProfileInfoCache* profile_info_cache_ = nullptr;
PrefService* prefs_ = nullptr;
base::FilePath profile_path_;
std::string storage_key_;
std::u16string last_name_to_display_;
// A separate boolean flag indicates whether the signin is required when force
// signin is enabled. So that the profile locked status will be stored in
// memory only and can be easily reset once the policy is turned off.
bool is_force_signin_profile_locked_ = false;
bool is_force_signin_enabled_;
// Indicates whether the profile should not be displayed to the user in the
// list of profiles. This flag is intended to work only with ephemeral
// profiles which get removed after the browser restart. Thus, this flag is
// stored in memory only. Storing in memory also allows to avoid the risk of
// having permanent profiles that the user cannot see or delete, in case the
// ephemeral profile deletion fails.
bool is_omitted_ = false;
};
#endif // CHROME_BROWSER_PROFILES_PROFILE_ATTRIBUTES_ENTRY_H_