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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_ 6 #define CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_ 7 8 #include <map> 9 #include <string> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "base/compiler_specific.h" 14 #include "base/files/file_path.h" 15 #include "base/memory/weak_ptr.h" 16 #include "base/observer_list.h" 17 #include "base/strings/string16.h" 18 #include "chrome/browser/profiles/profile_info_cache_observer.h" 19 #include "chrome/browser/profiles/profile_info_interface.h" 20 21 namespace gfx { 22 class Image; 23 } 24 25 namespace base { 26 class DictionaryValue; 27 } 28 29 class PrefService; 30 class PrefRegistrySimple; 31 class ProfileAvatarDownloader; 32 33 // This class saves various information about profiles to local preferences. 34 // This cache can be used to display a list of profiles without having to 35 // actually load the profiles from disk. 36 class ProfileInfoCache : public ProfileInfoInterface, 37 public base::SupportsWeakPtr<ProfileInfoCache> { 38 public: 39 ProfileInfoCache(PrefService* prefs, const base::FilePath& user_data_dir); 40 virtual ~ProfileInfoCache(); 41 42 // If the |supervised_user_id| is non-empty, the profile will be marked to be 43 // omitted from the avatar-menu list on desktop versions. This is used while a 44 // supervised user is in the process of being registered with the server. Use 45 // SetIsOmittedProfileAtIndex() to clear the flag when the profile is ready to 46 // be shown in the menu. 47 void AddProfileToCache(const base::FilePath& profile_path, 48 const base::string16& name, 49 const base::string16& username, 50 size_t icon_index, 51 const std::string& supervised_user_id); 52 void DeleteProfileFromCache(const base::FilePath& profile_path); 53 54 // ProfileInfoInterface: 55 virtual size_t GetNumberOfProfiles() const OVERRIDE; 56 // Don't cache this value and reuse, because resorting the menu could cause 57 // the item being referred to to change out from under you. 58 virtual size_t GetIndexOfProfileWithPath( 59 const base::FilePath& profile_path) const OVERRIDE; 60 virtual base::string16 GetNameOfProfileAtIndex(size_t index) const OVERRIDE; 61 virtual base::string16 GetShortcutNameOfProfileAtIndex(size_t index) 62 const OVERRIDE; 63 virtual base::FilePath GetPathOfProfileAtIndex(size_t index) const OVERRIDE; 64 virtual base::Time GetProfileActiveTimeAtIndex(size_t index) const OVERRIDE; 65 virtual base::string16 GetUserNameOfProfileAtIndex( 66 size_t index) const OVERRIDE; 67 virtual const gfx::Image& GetAvatarIconOfProfileAtIndex( 68 size_t index) const OVERRIDE; 69 virtual std::string GetLocalAuthCredentialsOfProfileAtIndex( 70 size_t index) const OVERRIDE; 71 // Note that a return value of false could mean an error in collection or 72 // that there are currently no background apps running. However, the action 73 // which results is the same in both cases (thus far). 74 virtual bool GetBackgroundStatusOfProfileAtIndex( 75 size_t index) const OVERRIDE; 76 virtual base::string16 GetGAIANameOfProfileAtIndex( 77 size_t index) const OVERRIDE; 78 virtual base::string16 GetGAIAGivenNameOfProfileAtIndex( 79 size_t index) const OVERRIDE; 80 // Returns the GAIA picture for the given profile. This may return NULL 81 // if the profile does not have a GAIA picture or if the picture must be 82 // loaded from disk. 83 virtual const gfx::Image* GetGAIAPictureOfProfileAtIndex( 84 size_t index) const OVERRIDE; 85 virtual bool IsUsingGAIAPictureOfProfileAtIndex( 86 size_t index) const OVERRIDE; 87 virtual bool ProfileIsSupervisedAtIndex(size_t index) const OVERRIDE; 88 virtual bool IsOmittedProfileAtIndex(size_t index) const OVERRIDE; 89 virtual bool ProfileIsSigninRequiredAtIndex(size_t index) const OVERRIDE; 90 virtual std::string GetSupervisedUserIdOfProfileAtIndex(size_t index) const 91 OVERRIDE; 92 virtual bool ProfileIsEphemeralAtIndex(size_t index) const OVERRIDE; 93 virtual bool ProfileIsUsingDefaultNameAtIndex(size_t index) const OVERRIDE; 94 virtual bool ProfileIsUsingDefaultAvatarAtIndex(size_t index) const OVERRIDE; 95 96 size_t GetAvatarIconIndexOfProfileAtIndex(size_t index) const; 97 98 void SetProfileActiveTimeAtIndex(size_t index); 99 // Warning: This will re-sort profiles and thus may change indices! 100 void SetNameOfProfileAtIndex(size_t index, const base::string16& name); 101 void SetShortcutNameOfProfileAtIndex(size_t index, 102 const base::string16& name); 103 void SetUserNameOfProfileAtIndex(size_t index, 104 const base::string16& user_name); 105 void SetAvatarIconOfProfileAtIndex(size_t index, size_t icon_index); 106 void SetIsOmittedProfileAtIndex(size_t index, bool is_omitted); 107 void SetSupervisedUserIdOfProfileAtIndex(size_t index, const std::string& id); 108 void SetLocalAuthCredentialsOfProfileAtIndex(size_t index, 109 const std::string& auth); 110 void SetBackgroundStatusOfProfileAtIndex(size_t index, 111 bool running_background_apps); 112 // Warning: This will re-sort profiles and thus may change indices! 113 void SetGAIANameOfProfileAtIndex(size_t index, const base::string16& name); 114 // Warning: This will re-sort profiles and thus may change indices! 115 void SetGAIAGivenNameOfProfileAtIndex(size_t index, 116 const base::string16& name); 117 void SetGAIAPictureOfProfileAtIndex(size_t index, const gfx::Image* image); 118 void SetIsUsingGAIAPictureOfProfileAtIndex(size_t index, bool value); 119 void SetProfileSigninRequiredAtIndex(size_t index, bool value); 120 void SetProfileIsEphemeralAtIndex(size_t index, bool value); 121 void SetProfileIsUsingDefaultNameAtIndex(size_t index, bool value); 122 void SetProfileIsUsingDefaultAvatarAtIndex(size_t index, bool value); 123 124 // Determines whether |name| is one of the default assigned names. 125 bool IsDefaultProfileName(const base::string16& name) const; 126 127 // Returns unique name that can be assigned to a newly created profile. 128 base::string16 ChooseNameForNewProfile(size_t icon_index) const; 129 130 // Returns an avatar icon index that can be assigned to a newly created 131 // profile. Note that the icon may not be unique since there are a limited 132 // set of default icons. 133 size_t ChooseAvatarIconIndexForNewProfile() const; 134 135 const base::FilePath& GetUserDataDir() const; 136 137 // Gets all names of profiles associated with this instance of Chrome. 138 // Because this method will be called during uninstall, before the creation 139 // of the ProfileManager, it reads directly from the local state preferences, 140 // rather than going through the ProfileInfoCache object. 141 static std::vector<base::string16> GetProfileNames(); 142 143 // Register cache related preferences in Local State. 144 static void RegisterPrefs(PrefRegistrySimple* registry); 145 146 // Starts downloading the high res avatar at index |icon_index| for profile 147 // with path |profile_path|. 148 void DownloadHighResAvatar(size_t icon_index, 149 const base::FilePath& profile_path); 150 151 // Saves the avatar |image| at |image_path|. This is used both for the 152 // GAIA profile pictures and the ProfileAvatarDownloader that is used to 153 // download the high res avatars. 154 void SaveAvatarImageAtPath(const gfx::Image* image, 155 const std::string& key, 156 const base::FilePath& image_path, 157 const base::FilePath& profile_path); 158 159 void AddObserver(ProfileInfoCacheObserver* obs); 160 void RemoveObserver(ProfileInfoCacheObserver* obs); 161 162 private: 163 FRIEND_TEST_ALL_PREFIXES(ProfileInfoCacheTest, DownloadHighResAvatarTest); 164 165 const base::DictionaryValue* GetInfoForProfileAtIndex(size_t index) const; 166 // Saves the profile info to a cache and takes ownership of |info|. 167 // Currently the only information that is cached is the profile's name, 168 // user name, and avatar icon. 169 void SetInfoQuietlyForProfileAtIndex(size_t index, 170 base::DictionaryValue* info); 171 void SetInfoForProfileAtIndex(size_t index, base::DictionaryValue* info); 172 std::string CacheKeyFromProfilePath(const base::FilePath& profile_path) const; 173 std::vector<std::string>::iterator FindPositionForProfile( 174 const std::string& search_key, 175 const base::string16& search_name); 176 177 // Returns true if the given icon index is not in use by another profie. 178 bool IconIndexIsUnique(size_t icon_index) const; 179 180 // Tries to find an icon index that satisfies all the given conditions. 181 // Returns true if an icon was found, false otherwise. 182 bool ChooseAvatarIconIndexForNewProfile(bool allow_generic_icon, 183 bool must_be_unique, 184 size_t* out_icon_index) const; 185 186 // Updates the position of the profile at the given index so that the list 187 // of profiles is still sorted. 188 void UpdateSortForProfileIndex(size_t index); 189 190 // Loads or uses an already loaded high resolution image of the 191 // generic profile avatar. 192 const gfx::Image* GetHighResAvatarOfProfileAtIndex(size_t index) const; 193 194 // Returns the decoded image at |image_path|. Used both by the GAIA profile 195 // image and the high res avatars. 196 const gfx::Image* LoadAvatarPictureFromPath( 197 const std::string& key, 198 const base::FilePath& image_path) const; 199 200 // Called when the picture given by |key| has been loaded from disk and 201 // decoded into |image|. 202 void OnAvatarPictureLoaded(const std::string& key, 203 gfx::Image** image) const; 204 // Called when the picture given by |file_name| has been saved to disk. 205 // Used both for the GAIA profile picture and the high res avatar files. 206 void OnAvatarPictureSaved(const std::string& file_name, 207 const base::FilePath& profile_path); 208 209 // Migrate any legacy profile names ("First user", "Default Profile") to 210 // new style default names ("Person 1"), and download and high-res avatars 211 // used by the profiles. 212 void MigrateLegacyProfileNamesAndDownloadAvatars(); 213 214 PrefService* prefs_; 215 std::vector<std::string> sorted_keys_; 216 base::FilePath user_data_dir_; 217 218 ObserverList<ProfileInfoCacheObserver> observer_list_; 219 220 // A cache of gaia/high res avatar profile pictures. This cache is updated 221 // lazily so it needs to be mutable. 222 mutable std::map<std::string, gfx::Image*> cached_avatar_images_; 223 // Marks a profile picture as loading from disk. This prevents a picture from 224 // loading multiple times. 225 mutable std::map<std::string, bool> cached_avatar_images_loading_; 226 227 // Map of profile pictures currently being downloaded from the remote 228 // location and the ProfileAvatarDownloader instances downloading them. 229 // This prevents a picture from being downloaded multiple times. The 230 // ProfileAvatarDownloader instances are deleted when the download completes 231 // or when the ProfileInfoCache is destroyed. 232 mutable std::map<std::string, ProfileAvatarDownloader*> 233 avatar_images_downloads_in_progress_; 234 235 DISALLOW_COPY_AND_ASSIGN(ProfileInfoCache); 236 }; 237 238 #endif // CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_ 239