Commit a4352d0e authored by noms@chromium.org's avatar noms@chromium.org

Use custom profile names (if available) for signed in profiles

The problem was that if the user was set to use the Gaia profile info, then we ignored any custom profile names that were synced. We shouldn't do that :) 

BUG=343730,348187,349000

Review URL: https://codereview.chromium.org/186803003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@255273 0039d316-1c4b-4281-b951-d872f2087c98
parent 96390516
...@@ -57,8 +57,14 @@ class GAIAInfoUpdateServiceTest : public ProfileInfoCacheTest { ...@@ -57,8 +57,14 @@ class GAIAInfoUpdateServiceTest : public ProfileInfoCacheTest {
} }
Profile* profile() { Profile* profile() {
if (!profile_) if (!profile_) {
profile_ = testing_profile_manager_.CreateTestingProfile("profile_1"); profile_ = testing_profile_manager_.CreateTestingProfile("Person 1");
// The testing manager sets the profile name manually, which counts as
// a user-customized profile name. Reset this to match the default name
// we are actually using.
size_t index = GetCache()->GetIndexOfProfileWithPath(profile_->GetPath());
GetCache()->SetProfileIsUsingDefaultNameAtIndex(index, true);
}
return profile_; return profile_;
} }
...@@ -90,6 +96,7 @@ TEST_F(GAIAInfoUpdateServiceTest, DownloadSuccess) { ...@@ -90,6 +96,7 @@ TEST_F(GAIAInfoUpdateServiceTest, DownloadSuccess) {
// On success both the profile info and GAIA info should be updated. // On success both the profile info and GAIA info should be updated.
size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath());
EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(index)); EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(index));
EXPECT_TRUE(GetCache()->IsUsingGAIANameOfProfileAtIndex(index));
EXPECT_EQ(name, GetCache()->GetNameOfProfileAtIndex(index)); EXPECT_EQ(name, GetCache()->GetNameOfProfileAtIndex(index));
EXPECT_EQ(name, GetCache()->GetGAIANameOfProfileAtIndex(index)); EXPECT_EQ(name, GetCache()->GetGAIANameOfProfileAtIndex(index));
EXPECT_TRUE(gfx::test::IsEqual( EXPECT_TRUE(gfx::test::IsEqual(
......
...@@ -44,6 +44,7 @@ const char kGAIANameKey[] = "gaia_name"; ...@@ -44,6 +44,7 @@ const char kGAIANameKey[] = "gaia_name";
const char kGAIAGivenNameKey[] = "gaia_given_name"; const char kGAIAGivenNameKey[] = "gaia_given_name";
const char kUseGAIANameKey[] = "use_gaia_name"; const char kUseGAIANameKey[] = "use_gaia_name";
const char kUserNameKey[] = "user_name"; const char kUserNameKey[] = "user_name";
const char kIsUsingDefaultName[] = "is_using_default_name";
const char kAvatarIconKey[] = "avatar_icon"; const char kAvatarIconKey[] = "avatar_icon";
const char kAuthCredentialsKey[] = "local_auth_credentials"; const char kAuthCredentialsKey[] = "local_auth_credentials";
const char kUseGAIAPictureKey[] = "use_gaia_picture"; const char kUseGAIAPictureKey[] = "use_gaia_picture";
...@@ -173,6 +174,29 @@ void DeleteBitmap(const base::FilePath& image_path) { ...@@ -173,6 +174,29 @@ void DeleteBitmap(const base::FilePath& image_path) {
base::DeleteFile(image_path, false); base::DeleteFile(image_path, false);
} }
bool IsDefaultName(const base::string16& name) {
// Check if it's a "First user" old-style name.
if (name == l10n_util::GetStringUTF16(IDS_DEFAULT_PROFILE_NAME))
return true;
// Check if it's one of the old-style profile names.
for (size_t i = 0; i < arraysize(kDefaultNames); ++i) {
if (name == l10n_util::GetStringUTF16(kDefaultNames[i]))
return true;
}
// Check whether it's one of the "Person %d" style names.
std::string default_name_format = l10n_util::GetStringFUTF8(
IDS_NEW_NUMBERED_PROFILE_NAME, base::string16()) + "%d";
int generic_profile_number; // Unused. Just a placeholder for sscanf.
int assignments = sscanf(base::UTF16ToUTF8(name).c_str(),
default_name_format.c_str(),
&generic_profile_number);
// Unless it matched the format, this is a custom name.
return assignments == 1;
}
} // namespace } // namespace
ProfileInfoCache::ProfileInfoCache(PrefService* prefs, ProfileInfoCache::ProfileInfoCache(PrefService* prefs,
...@@ -196,6 +220,7 @@ ProfileInfoCache::ProfileInfoCache(PrefService* prefs, ...@@ -196,6 +220,7 @@ ProfileInfoCache::ProfileInfoCache(PrefService* prefs,
info->Remove(kIsManagedKey, NULL); info->Remove(kIsManagedKey, NULL);
info->SetString(kManagedUserId, is_managed ? "DUMMY_ID" : std::string()); info->SetString(kManagedUserId, is_managed ? "DUMMY_ID" : std::string());
} }
info->SetBoolean(kIsUsingDefaultName, IsDefaultName(name));
} }
} }
...@@ -222,6 +247,7 @@ void ProfileInfoCache::AddProfileToCache(const base::FilePath& profile_path, ...@@ -222,6 +247,7 @@ void ProfileInfoCache::AddProfileToCache(const base::FilePath& profile_path,
info->SetString(kManagedUserId, managed_user_id); info->SetString(kManagedUserId, managed_user_id);
info->SetBoolean(kIsOmittedFromProfileListKey, !managed_user_id.empty()); info->SetBoolean(kIsOmittedFromProfileListKey, !managed_user_id.empty());
info->SetBoolean(kProfileIsEphemeral, false); info->SetBoolean(kProfileIsEphemeral, false);
info->SetBoolean(kIsUsingDefaultName, IsDefaultName(name));
cache->SetWithoutPathExpansion(key, info.release()); cache->SetWithoutPathExpansion(key, info.release());
sorted_keys_.insert(FindPositionForProfile(key, name), key); sorted_keys_.insert(FindPositionForProfile(key, name), key);
...@@ -291,11 +317,13 @@ size_t ProfileInfoCache::GetIndexOfProfileWithPath( ...@@ -291,11 +317,13 @@ size_t ProfileInfoCache::GetIndexOfProfileWithPath(
base::string16 ProfileInfoCache::GetNameOfProfileAtIndex(size_t index) const { base::string16 ProfileInfoCache::GetNameOfProfileAtIndex(size_t index) const {
base::string16 name; base::string16 name;
if (IsUsingGAIANameOfProfileAtIndex(index)) { // Unless the user has customized the profile name, we should use the
// profile's Gaia given name, if it's available.
if (IsUsingGAIANameOfProfileAtIndex(index) &&
ProfileIsUsingDefaultNameAtIndex(index)) {
base::string16 given_name = GetGAIAGivenNameOfProfileAtIndex(index); base::string16 given_name = GetGAIAGivenNameOfProfileAtIndex(index);
name = given_name.empty() ? GetGAIANameOfProfileAtIndex(index) : given_name; name = given_name.empty() ? GetGAIANameOfProfileAtIndex(index) : given_name;
} }
if (name.empty()) if (name.empty())
GetInfoForProfileAtIndex(index)->GetString(kNameKey, &name); GetInfoForProfileAtIndex(index)->GetString(kNameKey, &name);
return name; return name;
...@@ -441,6 +469,12 @@ bool ProfileInfoCache::ProfileIsEphemeralAtIndex(size_t index) const { ...@@ -441,6 +469,12 @@ bool ProfileInfoCache::ProfileIsEphemeralAtIndex(size_t index) const {
return value; return value;
} }
bool ProfileInfoCache::ProfileIsUsingDefaultNameAtIndex(size_t index) const {
bool value = false;
GetInfoForProfileAtIndex(index)->GetBoolean(kIsUsingDefaultName, &value);
return value;
}
void ProfileInfoCache::OnGAIAPictureLoaded(const base::FilePath& path, void ProfileInfoCache::OnGAIAPictureLoaded(const base::FilePath& path,
gfx::Image** image) const { gfx::Image** image) const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
...@@ -513,6 +547,8 @@ void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index, ...@@ -513,6 +547,8 @@ void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index,
base::string16 old_display_name = GetNameOfProfileAtIndex(index); base::string16 old_display_name = GetNameOfProfileAtIndex(index);
info->SetString(kNameKey, name); info->SetString(kNameKey, name);
info->SetBoolean(kIsUsingDefaultName, false);
// This takes ownership of |info|. // This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release()); SetInfoForProfileAtIndex(index, info.release());
base::string16 new_display_name = GetNameOfProfileAtIndex(index); base::string16 new_display_name = GetNameOfProfileAtIndex(index);
...@@ -762,6 +798,18 @@ void ProfileInfoCache::SetProfileIsEphemeralAtIndex(size_t index, bool value) { ...@@ -762,6 +798,18 @@ void ProfileInfoCache::SetProfileIsEphemeralAtIndex(size_t index, bool value) {
SetInfoForProfileAtIndex(index, info.release()); SetInfoForProfileAtIndex(index, info.release());
} }
void ProfileInfoCache::SetProfileIsUsingDefaultNameAtIndex(
size_t index, bool value) {
if (value == ProfileIsUsingDefaultNameAtIndex(index))
return;
scoped_ptr<base::DictionaryValue> info(
GetInfoForProfileAtIndex(index)->DeepCopy());
info->SetBoolean(kIsUsingDefaultName, value);
// This takes ownership of |info|.
SetInfoForProfileAtIndex(index, info.release());
}
base::string16 ProfileInfoCache::ChooseNameForNewProfile( base::string16 ProfileInfoCache::ChooseNameForNewProfile(
size_t icon_index) const { size_t icon_index) const {
base::string16 name; base::string16 name;
......
...@@ -91,6 +91,7 @@ class ProfileInfoCache : public ProfileInfoInterface, ...@@ -91,6 +91,7 @@ class ProfileInfoCache : public ProfileInfoInterface,
virtual std::string GetManagedUserIdOfProfileAtIndex(size_t index) const virtual std::string GetManagedUserIdOfProfileAtIndex(size_t index) const
OVERRIDE; OVERRIDE;
virtual bool ProfileIsEphemeralAtIndex(size_t index) const OVERRIDE; virtual bool ProfileIsEphemeralAtIndex(size_t index) const OVERRIDE;
virtual bool ProfileIsUsingDefaultNameAtIndex(size_t index) const OVERRIDE;
size_t GetAvatarIconIndexOfProfileAtIndex(size_t index) const; size_t GetAvatarIconIndexOfProfileAtIndex(size_t index) const;
...@@ -115,6 +116,7 @@ class ProfileInfoCache : public ProfileInfoInterface, ...@@ -115,6 +116,7 @@ class ProfileInfoCache : public ProfileInfoInterface,
void SetIsUsingGAIAPictureOfProfileAtIndex(size_t index, bool value); void SetIsUsingGAIAPictureOfProfileAtIndex(size_t index, bool value);
void SetProfileSigninRequiredAtIndex(size_t index, bool value); void SetProfileSigninRequiredAtIndex(size_t index, bool value);
void SetProfileIsEphemeralAtIndex(size_t index, bool value); void SetProfileIsEphemeralAtIndex(size_t index, bool value);
void SetProfileIsUsingDefaultNameAtIndex(size_t index, bool value);
// Returns unique name that can be assigned to a newly created profile. // Returns unique name that can be assigned to a newly created profile.
base::string16 ChooseNameForNewProfile(size_t icon_index) const; base::string16 ChooseNameForNewProfile(size_t icon_index) const;
......
...@@ -329,36 +329,45 @@ TEST_F(ProfileInfoCacheTest, ProfileActiveTime) { ...@@ -329,36 +329,45 @@ TEST_F(ProfileInfoCacheTest, ProfileActiveTime) {
TEST_F(ProfileInfoCacheTest, GAIAName) { TEST_F(ProfileInfoCacheTest, GAIAName) {
GetCache()->AddProfileToCache( GetCache()->AddProfileToCache(
GetProfilePath("path_1"), ASCIIToUTF16("name_1"), GetProfilePath("path_1"), ASCIIToUTF16("Person 1"),
base::string16(), 0, std::string()); base::string16(), 0, std::string());
base::string16 profile_name(ASCIIToUTF16("profile name 2")); base::string16 profile_name(ASCIIToUTF16("Person 2"));
GetCache()->AddProfileToCache( GetCache()->AddProfileToCache(
GetProfilePath("path_2"), profile_name, base::string16(), 0, GetProfilePath("path_2"), profile_name, base::string16(), 0,
std::string()); std::string());
int index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
int index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
// Sanity check. // Sanity check.
EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty()); EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(1).empty()); EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index2).empty());
EXPECT_FALSE(GetCache()->IsUsingGAIANameOfProfileAtIndex(0)); EXPECT_FALSE(GetCache()->IsUsingGAIANameOfProfileAtIndex(index1));
EXPECT_FALSE(GetCache()->IsUsingGAIANameOfProfileAtIndex(1)); EXPECT_FALSE(GetCache()->IsUsingGAIANameOfProfileAtIndex(index2));
// Set GAIA name. // Set GAIA name.
base::string16 gaia_name(ASCIIToUTF16("Pat Smith")); base::string16 gaia_name(ASCIIToUTF16("Pat Smith"));
GetCache()->SetGAIANameOfProfileAtIndex(1, gaia_name); GetCache()->SetGAIANameOfProfileAtIndex(index2, gaia_name);
EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty()); EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1)); EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(1)); EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(index2));
// Use GAIA name as profile name. // Use GAIA name as profile name. This re-sorts the cache.
GetCache()->SetIsUsingGAIANameOfProfileAtIndex(1, true); GetCache()->SetIsUsingGAIANameOfProfileAtIndex(index2, true);
index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(1)); index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1));
EXPECT_EQ(GetCache()->IsUsingGAIANameOfProfileAtIndex(index2), true);
// Don't use GAIA name as profile name. EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(index2));
GetCache()->SetIsUsingGAIANameOfProfileAtIndex(1, false); EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(1));
EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1)); // Don't use GAIA name as profile name. This re-sorts the cache.
GetCache()->SetIsUsingGAIANameOfProfileAtIndex(index2, false);
index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(index2));
EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
} }
TEST_F(ProfileInfoCacheTest, GAIAPicture) { TEST_F(ProfileInfoCacheTest, GAIAPicture) {
......
...@@ -76,6 +76,11 @@ class ProfileInfoInterface { ...@@ -76,6 +76,11 @@ class ProfileInfoInterface {
// Profile is known to be ephemeral and should be deleted when closed. // Profile is known to be ephemeral and should be deleted when closed.
virtual bool ProfileIsEphemeralAtIndex(size_t index) const = 0; virtual bool ProfileIsEphemeralAtIndex(size_t index) const = 0;
// Returns true if the profile is using the name it was assigned by default
// at creation (either the old-style "Lemonade" name, or the new "Profile %d"
// style name).
virtual bool ProfileIsUsingDefaultNameAtIndex(size_t index) const = 0;
protected: protected:
virtual ~ProfileInfoInterface() {} virtual ~ProfileInfoInterface() {}
}; };
......
...@@ -4,8 +4,6 @@ ...@@ -4,8 +4,6 @@
#include "chrome/browser/profiles/profiles_state.h" #include "chrome/browser/profiles/profiles_state.h"
#include <stdio.h>
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/prefs/pref_registry_simple.h" #include "base/prefs/pref_registry_simple.h"
#include "base/prefs/pref_service.h" #include "base/prefs/pref_service.h"
...@@ -72,13 +70,7 @@ base::string16 GetAvatarNameForProfile(Profile* profile) { ...@@ -72,13 +70,7 @@ base::string16 GetAvatarNameForProfile(Profile* profile) {
// or the user has edited the profile name, or there are multiple profiles, // or the user has edited the profile name, or there are multiple profiles,
// it will return the actual name of the profile. // it will return the actual name of the profile.
base::string16 profile_name = cache.GetNameOfProfileAtIndex(index); base::string16 profile_name = cache.GetNameOfProfileAtIndex(index);
std::string default_name_format = l10n_util::GetStringFUTF8( bool has_default_name = cache.ProfileIsUsingDefaultNameAtIndex(index);
IDS_NEW_NUMBERED_PROFILE_NAME, base::string16()) + "%d";
int generic_profile_number; // Unused. Just a placeholder for sscanf.
int assignments = sscanf(base::UTF16ToUTF8(profile_name).c_str(),
default_name_format.c_str(),
&generic_profile_number);
bool has_default_name = (assignments == 1);
if (cache.GetNumberOfProfiles() == 1 && has_default_name && if (cache.GetNumberOfProfiles() == 1 && has_default_name &&
cache.GetUserNameOfProfileAtIndex(index).empty()) { cache.GetUserNameOfProfileAtIndex(index).empty()) {
......
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