Commit 3029056f authored by Monica Basta's avatar Monica Basta Committed by Commit Bot

[ProfileInfoCache]: Refactoring.

Bug: 305720
Change-Id: I283ada9ff6b7d55c1055bfbdfb301854d372b651
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1926473
Commit-Queue: Monica Basta <msalama@chromium.org>
Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#717665}
parent da58a15f
......@@ -18,6 +18,10 @@
#include "components/prefs/scoped_user_pref_update.h"
#include "ui/base/resource/resource_bundle.h"
#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
#include "chrome/browser/supervised_user/supervised_user_constants.h"
#endif
namespace {
const char kGAIAGivenNameKey[] = "gaia_given_name";
......@@ -49,6 +53,9 @@ int NextAvailableMetricsBucketIndex() {
const base::Feature kPersistUPAInProfileInfoCache{
"PersistUPAInProfileInfoCache", base::FEATURE_ENABLED_BY_DEFAULT};
const char ProfileAttributesEntry::kSupervisedUserId[] = "managed_user_id";
const char ProfileAttributesEntry::kIsOmittedFromProfileListKey[] =
"is_omitted_from_profile_list";
const char ProfileAttributesEntry::kAvatarIconKey[] = "avatar_icon";
const char ProfileAttributesEntry::kBackgroundAppsKey[] = "background_apps";
const char ProfileAttributesEntry::kProfileIsEphemeral[] = "is_ephemeral";
......@@ -300,19 +307,23 @@ bool ProfileAttributesEntry::IsGAIAPictureLoaded() const {
}
bool ProfileAttributesEntry::IsSupervised() const {
return profile_info_cache_->ProfileIsSupervisedAtIndex(profile_index());
return !GetSupervisedUserId().empty();
}
bool ProfileAttributesEntry::IsChild() const {
return profile_info_cache_->ProfileIsChildAtIndex(profile_index());
#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
return GetSupervisedUserId() == supervised_users::kChildAccountSUID;
#else
return false;
#endif
}
bool ProfileAttributesEntry::IsLegacySupervised() const {
return profile_info_cache_->ProfileIsLegacySupervisedAtIndex(profile_index());
return IsSupervised() && !IsChild();
}
bool ProfileAttributesEntry::IsOmitted() const {
return profile_info_cache_->IsOmittedProfileAtIndex(profile_index());
return GetBool(kIsOmittedFromProfileListKey);
}
bool ProfileAttributesEntry::IsSigninRequired() const {
......@@ -321,8 +332,7 @@ bool ProfileAttributesEntry::IsSigninRequired() const {
}
std::string ProfileAttributesEntry::GetSupervisedUserId() const {
return profile_info_cache_->GetSupervisedUserIdOfProfileAtIndex(
profile_index());
return GetString(kSupervisedUserId);
}
bool ProfileAttributesEntry::IsEphemeral() const {
......@@ -393,11 +403,13 @@ void ProfileAttributesEntry::SetActiveTimeToNow() {
}
void ProfileAttributesEntry::SetIsOmitted(bool is_omitted) {
profile_info_cache_->SetIsOmittedProfileAtIndex(profile_index(), is_omitted);
if (SetBool(kIsOmittedFromProfileListKey, is_omitted))
profile_info_cache_->NotifyProfileIsOmittedChanged(GetPath());
}
void ProfileAttributesEntry::SetSupervisedUserId(const std::string& id) {
profile_info_cache_->SetSupervisedUserIdOfProfileAtIndex(profile_index(), id);
if (SetString(kSupervisedUserId, id))
profile_info_cache_->NotifyProfileSupervisedUserIdChanged(GetPath());
}
void ProfileAttributesEntry::SetLocalAuthCredentials(const std::string& auth) {
......@@ -443,7 +455,7 @@ void ProfileAttributesEntry::LockForceSigninProfile(bool is_lock) {
if (is_force_signin_profile_locked_ == is_lock)
return;
is_force_signin_profile_locked_ = is_lock;
profile_info_cache_->NotifyIsSigninRequiredChanged(profile_path_);
profile_info_cache_->NotifyIsSigninRequiredChanged(GetPath());
}
void ProfileAttributesEntry::SetIsEphemeral(bool value) {
......
......@@ -151,6 +151,8 @@ class ProfileAttributesEntry {
// Lock/Unlock the profile, should be called only if force-sign-in is enabled.
void LockForceSigninProfile(bool is_lock);
static const char kSupervisedUserId[];
static const char kIsOmittedFromProfileListKey[];
static const char kAvatarIconKey[];
static const char kBackgroundAppsKey[];
static const char kProfileIsEphemeral[];
......
......@@ -43,9 +43,7 @@ namespace {
const char kIsUsingDefaultAvatarKey[] = "is_using_default_avatar";
const char kUseGAIAPictureKey[] = "use_gaia_picture";
const char kGAIAPictureFileNameKey[] = "gaia_picture_file_name";
const char kIsOmittedFromProfileListKey[] = "is_omitted_from_profile_list";
const char kSigninRequiredKey[] = "signin_required";
const char kSupervisedUserId[] = "managed_user_id";
const char kAccountIdKey[] = "account_id_key";
#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
const char kLegacyProfileNameMigrated[] = "legacy.profile.name.migrated";
......@@ -73,7 +71,8 @@ ProfileInfoCache::ProfileInfoCache(PrefService* prefs,
#if BUILDFLAG(ENABLE_SUPERVISED_USERS) && !defined(OS_ANDROID) && \
!defined(OS_CHROMEOS)
std::string supervised_user_id;
info->GetString(kSupervisedUserId, &supervised_user_id);
info->GetString(ProfileAttributesEntry::kSupervisedUserId,
&supervised_user_id);
// Silently ignore legacy supervised user profiles.
if (!supervised_user_id.empty() &&
supervised_user_id != supervised_users::kChildAccountSUID) {
......@@ -160,8 +159,10 @@ void ProfileInfoCache::AddProfileToCache(const base::FilePath& profile_path,
profiles::GetDefaultAvatarIconUrl(icon_index));
// Default value for whether background apps are running is false.
info->SetBoolean(ProfileAttributesEntry::kBackgroundAppsKey, false);
info->SetString(kSupervisedUserId, supervised_user_id);
info->SetBoolean(kIsOmittedFromProfileListKey, !supervised_user_id.empty());
info->SetString(ProfileAttributesEntry::kSupervisedUserId,
supervised_user_id);
info->SetBoolean(ProfileAttributesEntry::kIsOmittedFromProfileListKey,
!supervised_user_id.empty());
info->SetBoolean(ProfileAttributesEntry::kProfileIsEphemeral, false);
// Either the user has provided a name manually on purpose, and in this case
// we should not check for legacy profile names or this a new profile but then
......@@ -197,6 +198,18 @@ void ProfileInfoCache::NotifyIfProfileNamesHaveChanged() {
}
}
void ProfileInfoCache::NotifyProfileSupervisedUserIdChanged(
const base::FilePath& profile_path) {
for (auto& observer : observer_list_)
observer.OnProfileSupervisedUserIdChanged(profile_path);
}
void ProfileInfoCache::NotifyProfileIsOmittedChanged(
const base::FilePath& profile_path) {
for (auto& observer : observer_list_)
observer.OnProfileIsOmittedChanged(profile_path);
}
void ProfileInfoCache::DeleteProfileFromCache(
const base::FilePath& profile_path) {
ProfileAttributesEntry* entry;
......@@ -239,19 +252,6 @@ size_t ProfileInfoCache::GetIndexOfProfileWithPath(
return std::string::npos;
}
base::string16 ProfileInfoCache::GetNameToDisplayOfProfileAtIndex(
size_t index) {
base::FilePath profile_path = GetPathOfProfileAtIndex(index);
ProfileAttributesEntry* entry;
GetProfileAttributesWithPath(profile_path, &entry);
if (!entry) {
DLOG(ERROR) << "No entry found for this profile!";
return base::string16();
}
return entry->GetName();
}
base::FilePath ProfileInfoCache::GetPathOfProfileAtIndex(size_t index) const {
return user_data_dir_.AppendASCII(keys_[index]);
}
......@@ -292,44 +292,12 @@ bool ProfileInfoCache::IsUsingGAIAPictureOfProfileAtIndex(size_t index) const {
return value;
}
bool ProfileInfoCache::ProfileIsSupervisedAtIndex(size_t index) const {
return !GetSupervisedUserIdOfProfileAtIndex(index).empty();
}
bool ProfileInfoCache::ProfileIsChildAtIndex(size_t index) const {
#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
return GetSupervisedUserIdOfProfileAtIndex(index) ==
supervised_users::kChildAccountSUID;
#else
return false;
#endif
}
bool ProfileInfoCache::ProfileIsLegacySupervisedAtIndex(size_t index) const {
return ProfileIsSupervisedAtIndex(index) && !ProfileIsChildAtIndex(index);
}
bool ProfileInfoCache::IsOmittedProfileAtIndex(size_t index) const {
bool value = false;
GetInfoForProfileAtIndex(index)->GetBoolean(kIsOmittedFromProfileListKey,
&value);
return value;
}
bool ProfileInfoCache::ProfileIsSigninRequiredAtIndex(size_t index) const {
bool value = false;
GetInfoForProfileAtIndex(index)->GetBoolean(kSigninRequiredKey, &value);
return value;
}
std::string ProfileInfoCache::GetSupervisedUserIdOfProfileAtIndex(
size_t index) const {
std::string supervised_user_id;
GetInfoForProfileAtIndex(index)->GetString(kSupervisedUserId,
&supervised_user_id);
return supervised_user_id;
}
bool ProfileInfoCache::ProfileIsUsingDefaultAvatarAtIndex(size_t index) const {
bool value = false;
GetInfoForProfileAtIndex(index)->GetBoolean(kIsUsingDefaultAvatarKey, &value);
......@@ -381,35 +349,6 @@ void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index,
observer.OnProfileAvatarChanged(profile_path);
}
void ProfileInfoCache::SetIsOmittedProfileAtIndex(size_t index,
bool is_omitted) {
if (IsOmittedProfileAtIndex(index) == is_omitted)
return;
std::unique_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetBoolean(kIsOmittedFromProfileListKey, is_omitted);
SetInfoForProfileAtIndex(index, std::move(info));
base::FilePath profile_path = GetPathOfProfileAtIndex(index);
for (auto& observer : observer_list_)
observer.OnProfileIsOmittedChanged(profile_path);
}
void ProfileInfoCache::SetSupervisedUserIdOfProfileAtIndex(
size_t index,
const std::string& id) {
if (GetSupervisedUserIdOfProfileAtIndex(index) == id)
return;
std::unique_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetString(kSupervisedUserId, id);
SetInfoForProfileAtIndex(index, std::move(info));
base::FilePath profile_path = GetPathOfProfileAtIndex(index);
for (auto& observer : observer_list_)
observer.OnProfileSupervisedUserIdChanged(profile_path);
}
void ProfileInfoCache::SetGAIAPictureOfProfileAtIndex(size_t index,
gfx::Image image) {
base::FilePath path = GetPathOfProfileAtIndex(index);
......
......@@ -82,12 +82,7 @@ class ProfileInfoCache : public ProfileInfoInterface,
// loaded from disk.
const gfx::Image* GetGAIAPictureOfProfileAtIndex(size_t index) const override;
bool IsUsingGAIAPictureOfProfileAtIndex(size_t index) const override;
bool ProfileIsSupervisedAtIndex(size_t index) const override;
bool ProfileIsChildAtIndex(size_t index) const override;
bool ProfileIsLegacySupervisedAtIndex(size_t index) const override;
bool IsOmittedProfileAtIndex(size_t index) const override;
bool ProfileIsSigninRequiredAtIndex(size_t index) const override;
std::string GetSupervisedUserIdOfProfileAtIndex(size_t index) const override;
bool ProfileIsUsingDefaultAvatarAtIndex(size_t index) const override;
// Returns true if a GAIA picture has been loaded or has failed to load for
......@@ -98,7 +93,6 @@ class ProfileInfoCache : public ProfileInfoInterface,
// Will be removed SOON with ProfileInfoCache tests. Do not use!
void SetAvatarIconOfProfileAtIndex(size_t index, size_t icon_index);
void SetIsOmittedProfileAtIndex(size_t index, bool is_omitted);
void SetSupervisedUserIdOfProfileAtIndex(size_t index, const std::string& id);
void SetGAIAPictureOfProfileAtIndex(size_t index, gfx::Image image);
void SetIsUsingGAIAPictureOfProfileAtIndex(size_t index, bool value);
......@@ -110,9 +104,6 @@ class ProfileInfoCache : public ProfileInfoInterface,
const base::FilePath& GetUserDataDir() const;
// Gets the name of the profile, which is the one displayed in the User Menu.
base::string16 GetNameToDisplayOfProfileAtIndex(size_t index);
// Register cache related preferences in Local State.
static void RegisterPrefs(PrefRegistrySimple* registry);
......@@ -133,8 +124,8 @@ class ProfileInfoCache : public ProfileInfoInterface,
void NotifyProfileAuthInfoChanged(const base::FilePath& profile_path);
void NotifyIfProfileNamesHaveChanged();
static const char kNameKey[];
void NotifyProfileSupervisedUserIdChanged(const base::FilePath& profile_path);
void NotifyProfileIsOmittedChanged(const base::FilePath& profile_path);
private:
FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest,
......
......@@ -37,22 +37,6 @@ class ProfileInfoInterface {
// Checks if the GAIA picture should be used as the profile's avatar icon.
virtual bool IsUsingGAIAPictureOfProfileAtIndex(size_t index) const = 0;
// Returns whether the profile is supervised (either a legacy supervised
// user or a child account; see SupervisedUserService).
virtual bool ProfileIsSupervisedAtIndex(size_t index) const = 0;
// Returns whether the profile is associated with a child account.
virtual bool ProfileIsChildAtIndex(size_t index) const = 0;
// Returns whether the profile is a legacy supervised user profile.
virtual bool ProfileIsLegacySupervisedAtIndex(size_t index) const = 0;
// Returns true if the profile should be omitted from the desktop profile
// list (see ProfileListDesktop), so it won't appear in the avatar menu
// or user manager.
virtual bool IsOmittedProfileAtIndex(size_t index) const = 0;
virtual std::string GetSupervisedUserIdOfProfileAtIndex(
size_t index) const = 0;
// This profile is associated with an account but has been signed-out.
virtual bool ProfileIsSigninRequiredAtIndex(size_t index) const = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment