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 #ifndef CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_ | 5 #ifndef CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_ |
6 #define CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_ | 6 #define CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 10 matching lines...) Expand all Loading... |
21 namespace gfx { | 21 namespace gfx { |
22 class Image; | 22 class Image; |
23 } | 23 } |
24 | 24 |
25 namespace base { | 25 namespace base { |
26 class DictionaryValue; | 26 class DictionaryValue; |
27 } | 27 } |
28 | 28 |
29 class PrefService; | 29 class PrefService; |
30 class PrefRegistrySimple; | 30 class PrefRegistrySimple; |
| 31 class ProfileAvatarDownloader; |
31 | 32 |
32 // This class saves various information about profiles to local preferences. | 33 // This class saves various information about profiles to local preferences. |
33 // This cache can be used to display a list of profiles without having to | 34 // This cache can be used to display a list of profiles without having to |
34 // actually load the profiles from disk. | 35 // actually load the profiles from disk. |
35 class ProfileInfoCache : public ProfileInfoInterface, | 36 class ProfileInfoCache : public ProfileInfoInterface, |
36 public base::SupportsWeakPtr<ProfileInfoCache> { | 37 public base::SupportsWeakPtr<ProfileInfoCache> { |
37 public: | 38 public: |
38 ProfileInfoCache(PrefService* prefs, const base::FilePath& user_data_dir); | 39 ProfileInfoCache(PrefService* prefs, const base::FilePath& user_data_dir); |
39 virtual ~ProfileInfoCache(); | 40 virtual ~ProfileInfoCache(); |
40 | 41 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 | 129 |
129 // Gets all names of profiles associated with this instance of Chrome. | 130 // Gets all names of profiles associated with this instance of Chrome. |
130 // Because this method will be called during uninstall, before the creation | 131 // Because this method will be called during uninstall, before the creation |
131 // of the ProfileManager, it reads directly from the local state preferences, | 132 // of the ProfileManager, it reads directly from the local state preferences, |
132 // rather than going through the ProfileInfoCache object. | 133 // rather than going through the ProfileInfoCache object. |
133 static std::vector<base::string16> GetProfileNames(); | 134 static std::vector<base::string16> GetProfileNames(); |
134 | 135 |
135 // Register cache related preferences in Local State. | 136 // Register cache related preferences in Local State. |
136 static void RegisterPrefs(PrefRegistrySimple* registry); | 137 static void RegisterPrefs(PrefRegistrySimple* registry); |
137 | 138 |
| 139 // Starts downloading the high res avatar at index |icon_index|. |
| 140 // Takes ownership of |avatar_dowloader|, which will be deleted either |
| 141 // when the download completes and OnAvatarPictureSaved is called, or when |
| 142 // the ProfileInfoCache is destroyed. |
| 143 void DownloadHighResAvatar(ProfileAvatarDownloader* avatar_downloader, |
| 144 size_t icon_index); |
| 145 |
| 146 // Saves the avatar |image| at |image_path|. This is used both for the |
| 147 // GAIA profile pictures and the ProfileAvatarDownloader that is used to |
| 148 // download the high res avatars. |
| 149 void SaveAvatarImageAtPath(const gfx::Image* image, |
| 150 const std::string& key, |
| 151 const base::FilePath& image_path); |
| 152 |
138 void AddObserver(ProfileInfoCacheObserver* obs); | 153 void AddObserver(ProfileInfoCacheObserver* obs); |
139 void RemoveObserver(ProfileInfoCacheObserver* obs); | 154 void RemoveObserver(ProfileInfoCacheObserver* obs); |
140 | 155 |
141 private: | 156 private: |
| 157 FRIEND_TEST_ALL_PREFIXES(ProfileInfoCacheTest, DownloadHighResAvatarTest); |
| 158 |
142 const base::DictionaryValue* GetInfoForProfileAtIndex(size_t index) const; | 159 const base::DictionaryValue* GetInfoForProfileAtIndex(size_t index) const; |
143 // Saves the profile info to a cache and takes ownership of |info|. | 160 // Saves the profile info to a cache and takes ownership of |info|. |
144 // Currently the only information that is cached is the profile's name, | 161 // Currently the only information that is cached is the profile's name, |
145 // user name, and avatar icon. | 162 // user name, and avatar icon. |
146 void SetInfoQuietlyForProfileAtIndex(size_t index, | 163 void SetInfoQuietlyForProfileAtIndex(size_t index, |
147 base::DictionaryValue* info); | 164 base::DictionaryValue* info); |
148 void SetInfoForProfileAtIndex(size_t index, base::DictionaryValue* info); | 165 void SetInfoForProfileAtIndex(size_t index, base::DictionaryValue* info); |
149 std::string CacheKeyFromProfilePath(const base::FilePath& profile_path) const; | 166 std::string CacheKeyFromProfilePath(const base::FilePath& profile_path) const; |
150 std::vector<std::string>::iterator FindPositionForProfile( | 167 std::vector<std::string>::iterator FindPositionForProfile( |
151 const std::string& search_key, | 168 const std::string& search_key, |
152 const base::string16& search_name); | 169 const base::string16& search_name); |
153 | 170 |
154 // Returns true if the given icon index is not in use by another profie. | 171 // Returns true if the given icon index is not in use by another profie. |
155 bool IconIndexIsUnique(size_t icon_index) const; | 172 bool IconIndexIsUnique(size_t icon_index) const; |
156 | 173 |
157 // Tries to find an icon index that satisfies all the given conditions. | 174 // Tries to find an icon index that satisfies all the given conditions. |
158 // Returns true if an icon was found, false otherwise. | 175 // Returns true if an icon was found, false otherwise. |
159 bool ChooseAvatarIconIndexForNewProfile(bool allow_generic_icon, | 176 bool ChooseAvatarIconIndexForNewProfile(bool allow_generic_icon, |
160 bool must_be_unique, | 177 bool must_be_unique, |
161 size_t* out_icon_index) const; | 178 size_t* out_icon_index) const; |
162 | 179 |
163 // Updates the position of the profile at the given index so that the list | 180 // Updates the position of the profile at the given index so that the list |
164 // of profiles is still sorted. | 181 // of profiles is still sorted. |
165 void UpdateSortForProfileIndex(size_t index); | 182 void UpdateSortForProfileIndex(size_t index); |
166 | 183 |
167 // Loads or uses an already loaded high resolution image of the | 184 // Loads or uses an already loaded high resolution image of the |
168 // generic profile avatar. | 185 // generic profile avatar. |
169 const gfx::Image* GetHighResAvatarOfProfileAtIndex(size_t index) const; | 186 const gfx::Image* GetHighResAvatarOfProfileAtIndex(size_t index) const; |
170 | 187 |
| 188 // Returns the decoded image at |image_path|. Used both by the GAIA profile |
| 189 // image and the high res avatars. |
171 const gfx::Image* LoadAvatarPictureFromPath( | 190 const gfx::Image* LoadAvatarPictureFromPath( |
172 const std::string& key, | 191 const std::string& key, |
173 const base::FilePath& image_path) const; | 192 const base::FilePath& image_path) const; |
| 193 |
| 194 // Called when the picture given by |key| has been loaded from disk and |
| 195 // decoded into |image|. |
174 void OnAvatarPictureLoaded(const std::string& key, | 196 void OnAvatarPictureLoaded(const std::string& key, |
175 gfx::Image** image) const; | 197 gfx::Image** image) const; |
176 void OnGAIAPictureSaved(const base::FilePath& path, bool* success) const; | 198 // Called when the picture given by |file_name| has been saved to disk. |
| 199 // Used both for the GAIA profile picture and the high res avatar files. |
| 200 void OnAvatarPictureSaved(const std::string& file_name, bool* success); |
177 | 201 |
178 PrefService* prefs_; | 202 PrefService* prefs_; |
179 std::vector<std::string> sorted_keys_; | 203 std::vector<std::string> sorted_keys_; |
180 base::FilePath user_data_dir_; | 204 base::FilePath user_data_dir_; |
181 | 205 |
182 ObserverList<ProfileInfoCacheObserver> observer_list_; | 206 ObserverList<ProfileInfoCacheObserver> observer_list_; |
183 | 207 |
184 // A cache of gaia/high res avatar profile pictures. This cache is updated | 208 // A cache of gaia/high res avatar profile pictures. This cache is updated |
185 // lazily so it needs to be mutable. | 209 // lazily so it needs to be mutable. |
186 mutable std::map<std::string, gfx::Image*> cached_avatar_images_; | 210 mutable std::map<std::string, gfx::Image*> cached_avatar_images_; |
187 // Marks a profile picture as loading. This prevents a picture from | 211 // Marks a profile picture as loading from disk. This prevents a picture from |
188 // loading multiple times. | 212 // loading multiple times. |
189 mutable std::map<std::string, bool> cached_avatar_images_loading_; | 213 mutable std::map<std::string, bool> cached_avatar_images_loading_; |
190 | 214 |
| 215 // Map of profile pictures currently being downloaded from the remote |
| 216 // location and the ProfileAvatarDownloader instances downloading them. |
| 217 // This prevents a picture from being downloaded multiple times. The |
| 218 // ProfileAvatarDownloader pointers are deleted when the download completes or |
| 219 // when the ProfileInfoCache is destroyed. |
| 220 mutable std::map<std::string, ProfileAvatarDownloader*> |
| 221 avatar_images_downloads_in_progress_; |
| 222 |
191 DISALLOW_COPY_AND_ASSIGN(ProfileInfoCache); | 223 DISALLOW_COPY_AND_ASSIGN(ProfileInfoCache); |
192 }; | 224 }; |
193 | 225 |
194 #endif // CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_ | 226 #endif // CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_ |
OLD | NEW |