| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/profiles/profile_info_cache.h" | 5 #include "chrome/browser/profiles/profile_info_cache.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/i18n/case_conversion.h" | 10 #include "base/i18n/case_conversion.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 #include "ui/gfx/image/image_util.h" | 35 #include "ui/gfx/image/image_util.h" |
| 36 | 36 |
| 37 using content::BrowserThread; | 37 using content::BrowserThread; |
| 38 | 38 |
| 39 namespace { | 39 namespace { |
| 40 | 40 |
| 41 const char kNameKey[] = "name"; | 41 const char kNameKey[] = "name"; |
| 42 const char kShortcutNameKey[] = "shortcut_name"; | 42 const char kShortcutNameKey[] = "shortcut_name"; |
| 43 const char kGAIANameKey[] = "gaia_name"; | 43 const char kGAIANameKey[] = "gaia_name"; |
| 44 const char kGAIAGivenNameKey[] = "gaia_given_name"; | 44 const char kGAIAGivenNameKey[] = "gaia_given_name"; |
| 45 const char kUseGAIANameKey[] = "use_gaia_name"; | |
| 46 const char kUserNameKey[] = "user_name"; | 45 const char kUserNameKey[] = "user_name"; |
| 47 const char kIsUsingDefaultName[] = "is_using_default_name"; | 46 const char kIsUsingDefaultName[] = "is_using_default_name"; |
| 48 const char kAvatarIconKey[] = "avatar_icon"; | 47 const char kAvatarIconKey[] = "avatar_icon"; |
| 49 const char kAuthCredentialsKey[] = "local_auth_credentials"; | 48 const char kAuthCredentialsKey[] = "local_auth_credentials"; |
| 50 const char kUseGAIAPictureKey[] = "use_gaia_picture"; | 49 const char kUseGAIAPictureKey[] = "use_gaia_picture"; |
| 51 const char kBackgroundAppsKey[] = "background_apps"; | 50 const char kBackgroundAppsKey[] = "background_apps"; |
| 52 const char kHasMigratedToGAIAInfoKey[] = "has_migrated_to_gaia_info"; | |
| 53 const char kGAIAPictureFileNameKey[] = "gaia_picture_file_name"; | 51 const char kGAIAPictureFileNameKey[] = "gaia_picture_file_name"; |
| 54 const char kIsManagedKey[] = "is_managed"; | 52 const char kIsManagedKey[] = "is_managed"; |
| 55 const char kIsOmittedFromProfileListKey[] = "is_omitted_from_profile_list"; | 53 const char kIsOmittedFromProfileListKey[] = "is_omitted_from_profile_list"; |
| 56 const char kSigninRequiredKey[] = "signin_required"; | 54 const char kSigninRequiredKey[] = "signin_required"; |
| 57 const char kManagedUserId[] = "managed_user_id"; | 55 const char kManagedUserId[] = "managed_user_id"; |
| 58 const char kProfileIsEphemeral[] = "is_ephemeral"; | 56 const char kProfileIsEphemeral[] = "is_ephemeral"; |
| 59 const char kActiveTimeKey[] = "active_time"; | 57 const char kActiveTimeKey[] = "active_time"; |
| 60 | 58 |
| 61 const char kDefaultUrlPrefix[] = "chrome://theme/IDR_PROFILE_AVATAR_"; | 59 const char kDefaultUrlPrefix[] = "chrome://theme/IDR_PROFILE_AVATAR_"; |
| 62 const char kGAIAPictureFileName[] = "Google Profile Picture.png"; | 60 const char kGAIAPictureFileName[] = "Google Profile Picture.png"; |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 if (sorted_keys_[i] == search_key) | 309 if (sorted_keys_[i] == search_key) |
| 312 return i; | 310 return i; |
| 313 } | 311 } |
| 314 return std::string::npos; | 312 return std::string::npos; |
| 315 } | 313 } |
| 316 | 314 |
| 317 base::string16 ProfileInfoCache::GetNameOfProfileAtIndex(size_t index) const { | 315 base::string16 ProfileInfoCache::GetNameOfProfileAtIndex(size_t index) const { |
| 318 base::string16 name; | 316 base::string16 name; |
| 319 // Unless the user has customized the profile name, we should use the | 317 // Unless the user has customized the profile name, we should use the |
| 320 // profile's Gaia given name, if it's available. | 318 // profile's Gaia given name, if it's available. |
| 321 if (IsUsingGAIANameOfProfileAtIndex(index) && | 319 if (ProfileIsUsingDefaultNameAtIndex(index)) { |
| 322 ProfileIsUsingDefaultNameAtIndex(index)) { | |
| 323 base::string16 given_name = GetGAIAGivenNameOfProfileAtIndex(index); | 320 base::string16 given_name = GetGAIAGivenNameOfProfileAtIndex(index); |
| 324 name = given_name.empty() ? GetGAIANameOfProfileAtIndex(index) : given_name; | 321 name = given_name.empty() ? GetGAIANameOfProfileAtIndex(index) : given_name; |
| 325 } | 322 } |
| 326 if (name.empty()) | 323 if (name.empty()) |
| 327 GetInfoForProfileAtIndex(index)->GetString(kNameKey, &name); | 324 GetInfoForProfileAtIndex(index)->GetString(kNameKey, &name); |
| 328 return name; | 325 return name; |
| 329 } | 326 } |
| 330 | 327 |
| 331 base::string16 ProfileInfoCache::GetShortcutNameOfProfileAtIndex(size_t index) | 328 base::string16 ProfileInfoCache::GetShortcutNameOfProfileAtIndex(size_t index) |
| 332 const { | 329 const { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 return name; | 390 return name; |
| 394 } | 391 } |
| 395 | 392 |
| 396 base::string16 ProfileInfoCache::GetGAIAGivenNameOfProfileAtIndex( | 393 base::string16 ProfileInfoCache::GetGAIAGivenNameOfProfileAtIndex( |
| 397 size_t index) const { | 394 size_t index) const { |
| 398 base::string16 name; | 395 base::string16 name; |
| 399 GetInfoForProfileAtIndex(index)->GetString(kGAIAGivenNameKey, &name); | 396 GetInfoForProfileAtIndex(index)->GetString(kGAIAGivenNameKey, &name); |
| 400 return name; | 397 return name; |
| 401 } | 398 } |
| 402 | 399 |
| 403 bool ProfileInfoCache::IsUsingGAIANameOfProfileAtIndex(size_t index) const { | |
| 404 bool value = false; | |
| 405 GetInfoForProfileAtIndex(index)->GetBoolean(kUseGAIANameKey, &value); | |
| 406 return value; | |
| 407 } | |
| 408 | |
| 409 const gfx::Image* ProfileInfoCache::GetGAIAPictureOfProfileAtIndex( | 400 const gfx::Image* ProfileInfoCache::GetGAIAPictureOfProfileAtIndex( |
| 410 size_t index) const { | 401 size_t index) const { |
| 411 base::FilePath path = GetPathOfProfileAtIndex(index); | 402 base::FilePath path = GetPathOfProfileAtIndex(index); |
| 412 std::string key = CacheKeyFromProfilePath(path); | 403 std::string key = CacheKeyFromProfilePath(path); |
| 413 | 404 |
| 414 // If the picture is already loaded then use it. | 405 // If the picture is already loaded then use it. |
| 415 if (gaia_pictures_.count(key)) { | 406 if (gaia_pictures_.count(key)) { |
| 416 if (gaia_pictures_[key]->IsEmpty()) | 407 if (gaia_pictures_[key]->IsEmpty()) |
| 417 return NULL; | 408 return NULL; |
| 418 return gaia_pictures_[key]; | 409 return gaia_pictures_[key]; |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 670 if (name == GetGAIAGivenNameOfProfileAtIndex(index)) | 661 if (name == GetGAIAGivenNameOfProfileAtIndex(index)) |
| 671 return; | 662 return; |
| 672 | 663 |
| 673 scoped_ptr<base::DictionaryValue> info( | 664 scoped_ptr<base::DictionaryValue> info( |
| 674 GetInfoForProfileAtIndex(index)->DeepCopy()); | 665 GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 675 info->SetString(kGAIAGivenNameKey, name); | 666 info->SetString(kGAIAGivenNameKey, name); |
| 676 // This takes ownership of |info|. | 667 // This takes ownership of |info|. |
| 677 SetInfoForProfileAtIndex(index, info.release()); | 668 SetInfoForProfileAtIndex(index, info.release()); |
| 678 } | 669 } |
| 679 | 670 |
| 680 void ProfileInfoCache::SetIsUsingGAIANameOfProfileAtIndex(size_t index, | |
| 681 bool value) { | |
| 682 if (value == IsUsingGAIANameOfProfileAtIndex(index)) | |
| 683 return; | |
| 684 | |
| 685 base::string16 old_display_name = GetNameOfProfileAtIndex(index); | |
| 686 scoped_ptr<base::DictionaryValue> info( | |
| 687 GetInfoForProfileAtIndex(index)->DeepCopy()); | |
| 688 info->SetBoolean(kUseGAIANameKey, value); | |
| 689 // This takes ownership of |info|. | |
| 690 SetInfoForProfileAtIndex(index, info.release()); | |
| 691 base::string16 new_display_name = GetNameOfProfileAtIndex(index); | |
| 692 base::FilePath profile_path = GetPathOfProfileAtIndex(index); | |
| 693 UpdateSortForProfileIndex(index); | |
| 694 | |
| 695 if (old_display_name != new_display_name) { | |
| 696 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | |
| 697 observer_list_, | |
| 698 OnProfileNameChanged(profile_path, old_display_name)); | |
| 699 } | |
| 700 } | |
| 701 | |
| 702 void ProfileInfoCache::SetGAIAPictureOfProfileAtIndex(size_t index, | 671 void ProfileInfoCache::SetGAIAPictureOfProfileAtIndex(size_t index, |
| 703 const gfx::Image* image) { | 672 const gfx::Image* image) { |
| 704 base::FilePath path = GetPathOfProfileAtIndex(index); | 673 base::FilePath path = GetPathOfProfileAtIndex(index); |
| 705 std::string key = CacheKeyFromProfilePath(path); | 674 std::string key = CacheKeyFromProfilePath(path); |
| 706 | 675 |
| 707 // Delete the old bitmap from cache. | 676 // Delete the old bitmap from cache. |
| 708 std::map<std::string, gfx::Image*>::iterator it = gaia_pictures_.find(key); | 677 std::map<std::string, gfx::Image*>::iterator it = gaia_pictures_.find(key); |
| 709 if (it != gaia_pictures_.end()) { | 678 if (it != gaia_pictures_.end()) { |
| 710 delete it->second; | 679 delete it->second; |
| 711 gaia_pictures_.erase(it); | 680 gaia_pictures_.erase(it); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 832 if (GetNameOfProfileAtIndex(i) == name) { | 801 if (GetNameOfProfileAtIndex(i) == name) { |
| 833 name_found = true; | 802 name_found = true; |
| 834 break; | 803 break; |
| 835 } | 804 } |
| 836 } | 805 } |
| 837 if (!name_found) | 806 if (!name_found) |
| 838 return name; | 807 return name; |
| 839 } | 808 } |
| 840 } | 809 } |
| 841 | 810 |
| 842 bool ProfileInfoCache::GetHasMigratedToGAIAInfoOfProfileAtIndex( | |
| 843 size_t index) const { | |
| 844 bool value = false; | |
| 845 GetInfoForProfileAtIndex(index)->GetBoolean( | |
| 846 kHasMigratedToGAIAInfoKey, &value); | |
| 847 return value; | |
| 848 } | |
| 849 | |
| 850 void ProfileInfoCache::SetHasMigratedToGAIAInfoOfProfileAtIndex( | |
| 851 size_t index, bool value) { | |
| 852 scoped_ptr<base::DictionaryValue> info( | |
| 853 GetInfoForProfileAtIndex(index)->DeepCopy()); | |
| 854 info->SetBoolean(kHasMigratedToGAIAInfoKey, value); | |
| 855 // This takes ownership of |info|. | |
| 856 SetInfoForProfileAtIndex(index, info.release()); | |
| 857 } | |
| 858 | |
| 859 bool ProfileInfoCache::IconIndexIsUnique(size_t icon_index) const { | 811 bool ProfileInfoCache::IconIndexIsUnique(size_t icon_index) const { |
| 860 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) { | 812 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) { |
| 861 if (GetAvatarIconIndexOfProfileAtIndex(i) == icon_index) | 813 if (GetAvatarIconIndexOfProfileAtIndex(i) == icon_index) |
| 862 return false; | 814 return false; |
| 863 } | 815 } |
| 864 return true; | 816 return true; |
| 865 } | 817 } |
| 866 | 818 |
| 867 bool ProfileInfoCache::ChooseAvatarIconIndexForNewProfile( | 819 bool ProfileInfoCache::ChooseAvatarIconIndexForNewProfile( |
| 868 bool allow_generic_icon, | 820 bool allow_generic_icon, |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1036 info->GetString(kNameKey, &name); | 988 info->GetString(kNameKey, &name); |
| 1037 names.push_back(name); | 989 names.push_back(name); |
| 1038 } | 990 } |
| 1039 return names; | 991 return names; |
| 1040 } | 992 } |
| 1041 | 993 |
| 1042 // static | 994 // static |
| 1043 void ProfileInfoCache::RegisterPrefs(PrefRegistrySimple* registry) { | 995 void ProfileInfoCache::RegisterPrefs(PrefRegistrySimple* registry) { |
| 1044 registry->RegisterDictionaryPref(prefs::kProfileInfoCache); | 996 registry->RegisterDictionaryPref(prefs::kProfileInfoCache); |
| 1045 } | 997 } |
| OLD | NEW |