#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include <algorithm>
#include <unordered_set>
#include <utility>
#include "base/check.h"
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/i18n/number_formatting.h"
#include "base/i18n/string_compare.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted_memory.h"
#include "base/metrics/histogram_functions.h"
#include "base/observer_list.h"
#include "base/ranges/algorithm.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/thread_pool.h"
#include "base/threading/scoped_blocking_call.h"
#include "base/values.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/enterprise/browser_management/management_service_factory.h"
#include "chrome/browser/profiles/profile_avatar_downloader.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "chrome/browser/profiles/profile_metrics.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
#include "components/account_id/account_id.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/profile_metrics/state.h"
#include "components/signin/public/base/persistent_repeating_timer.h"
#include "components/signin/public/base/signin_pref_names.h"
#include "components/signin/public/base/signin_switches.h"
#include "components/signin/public/identity_manager/account_managed_status_finder.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/icu/source/i18n/unicode/coll.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/image/image.h"
#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/ui/browser_list.h"
#endif
namespace {
ImageData;
const int kDefaultNames[] = …;
enum class MultiProfileUserType { … };
const char kProfileCountLastUpdatePref[] = …;
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_CHROMEOS_ASH)
const char kLegacyProfileNameMigrated[] = …;
bool g_migration_enabled_for_testing = …;
#endif
gfx::Image ReadBitmap(const base::FilePath& image_path) { … }
bool SaveBitmap(std::unique_ptr<ImageData> data,
const base::FilePath& image_path) { … }
void DeleteBitmap(const base::FilePath& image_path) { … }
void RunCallbackIfFileMissing(const base::FilePath& file_path,
base::OnceClosure callback) { … }
class ProfileAttributesSortComparator { … };
MultiProfileUserType GetMultiProfileUserType(
const std::vector<ProfileAttributesEntry*>& entries) { … }
profile_metrics::UnconsentedPrimaryAccountType GetUnconsentedPrimaryAccountType(
ProfileAttributesEntry* entry) { … }
void RecordProfileState(ProfileAttributesEntry* entry,
profile_metrics::StateSuffix suffix) { … }
void Rotate(base::Value::List& list, size_t from_index, size_t to_index) { … }
}
ProfileAttributesStorage::ProfileAttributesStorage(
PrefService* prefs,
const base::FilePath& user_data_dir)
: … { … }
ProfileAttributesStorage::~ProfileAttributesStorage() = default;
void ProfileAttributesStorage::RegisterPrefs(PrefRegistrySimple* registry) { … }
base::flat_set<std::string> ProfileAttributesStorage::GetAllProfilesKeys(
PrefService* local_prefs) { … }
void ProfileAttributesStorage::AddProfile(ProfileAttributesInitParams params) { … }
void ProfileAttributesStorage::RemoveProfileByAccountId(
const AccountId& account_id) { … }
void ProfileAttributesStorage::RemoveProfile(
const base::FilePath& profile_path) { … }
std::vector<ProfileAttributesEntry*>
ProfileAttributesStorage::GetAllProfilesAttributes() const { … }
std::vector<ProfileAttributesEntry*>
ProfileAttributesStorage::GetAllProfilesAttributesSorted(
bool use_local_profile_name) const { … }
bool ProfileAttributesStorage::IsProfilesOrderPrefValid() const { … }
void ProfileAttributesStorage::EnsureProfilesOrderPrefIsInitialized() { … }
void ProfileAttributesStorage::UpdateProfilesOrderPref(size_t from_index,
size_t to_index) { … }
base::flat_map<std::string, ProfileAttributesEntry*>
ProfileAttributesStorage::GetStorageKeyEntryMap() const { … }
std::vector<ProfileAttributesEntry*>
ProfileAttributesStorage::GetAllProfilesAttributesSortedForDisplay() const { … }
std::vector<ProfileAttributesEntry*> ProfileAttributesStorage::
GetAllProfilesAttributesSortedByLocalProfileNameWithCheck() const { … }
std::vector<ProfileAttributesEntry*>
ProfileAttributesStorage::GetAllProfilesAttributesSortedByNameWithCheck()
const { … }
std::vector<ProfileAttributesEntry*>
ProfileAttributesStorage::GetAllProfilesAttributesSortedByName() const { … }
std::vector<ProfileAttributesEntry*>
ProfileAttributesStorage::GetAllProfilesAttributesSortedByLocalProfileName()
const { … }
ProfileAttributesEntry* ProfileAttributesStorage::GetProfileAttributesWithPath(
const base::FilePath& path) { … }
size_t ProfileAttributesStorage::GetNumberOfProfiles() const { … }
std::u16string ProfileAttributesStorage::ChooseNameForNewProfile(
size_t icon_index) const { … }
bool ProfileAttributesStorage::IsDefaultProfileName(
const std::u16string& name,
bool include_check_for_legacy_profile_name) const { … }
size_t ProfileAttributesStorage::ChooseAvatarIconIndexForNewProfile() const { … }
const gfx::Image* ProfileAttributesStorage::LoadAvatarPictureFromPath(
const base::FilePath& profile_path,
const std::string& key,
const base::FilePath& image_path) const { … }
bool ProfileAttributesStorage::IsGAIAPictureLoaded(
const std::string& key) const { … }
void ProfileAttributesStorage::SaveGAIAImageAtPath(
const base::FilePath& profile_path,
const std::string& key,
gfx::Image image,
const base::FilePath& image_path,
const std::string& image_url_with_size) { … }
void ProfileAttributesStorage::DeleteGAIAImageAtPath(
const base::FilePath& profile_path,
const std::string& key,
const base::FilePath& image_path) { … }
void ProfileAttributesStorage::AddObserver(Observer* obs) { … }
void ProfileAttributesStorage::RemoveObserver(Observer* obs) { … }
#if !BUILDFLAG(IS_ANDROID)
void ProfileAttributesStorage::RecordDeletedProfileState(
ProfileAttributesEntry* entry) { … }
#endif
void ProfileAttributesStorage::RecordProfilesState() { … }
void ProfileAttributesStorage::NotifyOnProfileAvatarChanged(
const base::FilePath& profile_path) const { … }
void ProfileAttributesStorage::NotifyIsSigninRequiredChanged(
const base::FilePath& profile_path) const { … }
void ProfileAttributesStorage::NotifyProfileAuthInfoChanged(
const base::FilePath& profile_path) const { … }
void ProfileAttributesStorage::NotifyIfProfileNamesHaveChanged() const { … }
void ProfileAttributesStorage::NotifyProfileSupervisedUserIdChanged(
const base::FilePath& profile_path) const { … }
void ProfileAttributesStorage::NotifyProfileIsOmittedChanged(
const base::FilePath& profile_path) const { … }
void ProfileAttributesStorage::NotifyProfileThemeColorsChanged(
const base::FilePath& profile_path) const { … }
void ProfileAttributesStorage::NotifyProfileHostedDomainChanged(
const base::FilePath& profile_path) const { … }
void ProfileAttributesStorage::NotifyOnProfileHighResAvatarLoaded(
const base::FilePath& profile_path) const { … }
void ProfileAttributesStorage::NotifyProfileUserManagementAcceptanceChanged(
const base::FilePath& profile_path) const { … }
void ProfileAttributesStorage::NotifyProfileManagementEnrollmentTokenChanged(
const base::FilePath& profile_path) const { … }
void ProfileAttributesStorage::NotifyProfileManagementIdChanged(
const base::FilePath& profile_path) const { … }
std::string ProfileAttributesStorage::StorageKeyFromProfilePath(
const base::FilePath& profile_path) const { … }
void ProfileAttributesStorage::DisableProfileMetricsForTesting() { … }
void ProfileAttributesStorage::DownloadHighResAvatarIfNeeded(
size_t icon_index,
const base::FilePath& profile_path) { … }
void ProfileAttributesStorage::DownloadHighResAvatar(
size_t icon_index,
const base::FilePath& profile_path) { … }
void ProfileAttributesStorage::SaveAvatarImageAtPath(
const base::FilePath& profile_path,
gfx::Image image,
const std::string& key,
const base::FilePath& image_path,
base::OnceClosure callback) { … }
ProfileAttributesEntry* ProfileAttributesStorage::InitEntryWithKey(
const std::string& key,
bool is_omitted) { … }
void ProfileAttributesStorage::DownloadAvatars() { … }
#if !BUILDFLAG(IS_ANDROID)
void ProfileAttributesStorage::LoadGAIAPictureIfNeeded() { … }
#endif
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_CHROMEOS_ASH)
void ProfileAttributesStorage::MigrateLegacyProfileNamesAndRecomputeIfNeeded() { … }
void ProfileAttributesStorage::SetLegacyProfileMigrationForTesting(bool value) { … }
#endif
void ProfileAttributesStorage::OnAvatarPictureLoaded(
const base::FilePath& profile_path,
const std::string& key,
gfx::Image image) const { … }
void ProfileAttributesStorage::OnAvatarPictureSaved(
const std::string& file_name,
const base::FilePath& profile_path,
base::OnceClosure callback,
bool success) const { … }
void ProfileAttributesStorage::OnGAIAPictureSaved(
const std::string& image_url_with_size,
const base::FilePath& profile_path) { … }
void ProfileAttributesStorage::SaveAvatarImageAtPathNoCallback(
const base::FilePath& profile_path,
gfx::Image image,
const std::string& key,
const base::FilePath& image_path) { … }
void ProfileAttributesStorage::
EnsureProfilesOrderPrefIsInitializedForTesting() { … }