[Profiles] Delete test-only method from the ProfileInfoCache
[chromium-blink-merge.git] / chrome / browser / profiles / profile_info_cache.h
blob1fe893723ef17339b01902ebfc8d5db1a8ade0c5
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.
5 #ifndef CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_
6 #define CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_
8 #include <map>
9 #include <string>
10 #include <vector>
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"
21 namespace gfx {
22 class Image;
25 namespace base {
26 class DictionaryValue;
29 class PrefService;
30 class PrefRegistrySimple;
31 class ProfileAvatarDownloader;
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 ~ProfileInfoCache() override;
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);
54 // ProfileInfoInterface:
55 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 size_t GetIndexOfProfileWithPath(
59 const base::FilePath& profile_path) const override;
60 base::string16 GetNameOfProfileAtIndex(size_t index) const override;
61 base::string16 GetShortcutNameOfProfileAtIndex(size_t index) const override;
62 base::FilePath GetPathOfProfileAtIndex(size_t index) const override;
63 base::Time GetProfileActiveTimeAtIndex(size_t index) const override;
64 base::string16 GetUserNameOfProfileAtIndex(size_t index) const override;
65 const gfx::Image& GetAvatarIconOfProfileAtIndex(size_t index) const override;
66 std::string GetLocalAuthCredentialsOfProfileAtIndex(
67 size_t index) const override;
68 // Note that a return value of false could mean an error in collection or
69 // that there are currently no background apps running. However, the action
70 // which results is the same in both cases (thus far).
71 bool GetBackgroundStatusOfProfileAtIndex(size_t index) const override;
72 base::string16 GetGAIANameOfProfileAtIndex(size_t index) const override;
73 base::string16 GetGAIAGivenNameOfProfileAtIndex(size_t index) const override;
74 // Returns the GAIA picture for the given profile. This may return NULL
75 // if the profile does not have a GAIA picture or if the picture must be
76 // loaded from disk.
77 const gfx::Image* GetGAIAPictureOfProfileAtIndex(size_t index) const override;
78 bool IsUsingGAIAPictureOfProfileAtIndex(size_t index) const override;
79 bool ProfileIsSupervisedAtIndex(size_t index) const override;
80 bool ProfileIsChildAtIndex(size_t index) const override;
81 bool ProfileIsLegacySupervisedAtIndex(size_t index) const override;
82 bool IsOmittedProfileAtIndex(size_t index) const override;
83 bool ProfileIsSigninRequiredAtIndex(size_t index) const override;
84 std::string GetSupervisedUserIdOfProfileAtIndex(size_t index) const override;
85 bool ProfileIsEphemeralAtIndex(size_t index) const override;
86 bool ProfileIsUsingDefaultNameAtIndex(size_t index) const override;
87 bool ProfileIsUsingDefaultAvatarAtIndex(size_t index) const override;
88 bool ProfileIsAuthErrorAtIndex(size_t index) const;
90 size_t GetAvatarIconIndexOfProfileAtIndex(size_t index) const;
92 void SetProfileActiveTimeAtIndex(size_t index);
93 // Warning: This will re-sort profiles and thus may change indices!
94 void SetNameOfProfileAtIndex(size_t index, const base::string16& name);
95 void SetShortcutNameOfProfileAtIndex(size_t index,
96 const base::string16& name);
97 void SetUserNameOfProfileAtIndex(size_t index,
98 const base::string16& user_name);
99 void SetAvatarIconOfProfileAtIndex(size_t index, size_t icon_index);
100 void SetIsOmittedProfileAtIndex(size_t index, bool is_omitted);
101 void SetSupervisedUserIdOfProfileAtIndex(size_t index, const std::string& id);
102 void SetLocalAuthCredentialsOfProfileAtIndex(size_t index,
103 const std::string& auth);
104 void SetBackgroundStatusOfProfileAtIndex(size_t index,
105 bool running_background_apps);
106 // Warning: This will re-sort profiles and thus may change indices!
107 void SetGAIANameOfProfileAtIndex(size_t index, const base::string16& name);
108 // Warning: This will re-sort profiles and thus may change indices!
109 void SetGAIAGivenNameOfProfileAtIndex(size_t index,
110 const base::string16& name);
111 void SetGAIAPictureOfProfileAtIndex(size_t index, const gfx::Image* image);
112 void SetIsUsingGAIAPictureOfProfileAtIndex(size_t index, bool value);
113 void SetProfileSigninRequiredAtIndex(size_t index, bool value);
114 void SetProfileIsEphemeralAtIndex(size_t index, bool value);
115 void SetProfileIsUsingDefaultNameAtIndex(size_t index, bool value);
116 void SetProfileIsUsingDefaultAvatarAtIndex(size_t index, bool value);
117 void SetProfileIsAuthErrorAtIndex(size_t index, bool value);
119 // Determines whether |name| is one of the default assigned names.
120 bool IsDefaultProfileName(const base::string16& name) const;
122 // Returns unique name that can be assigned to a newly created profile.
123 base::string16 ChooseNameForNewProfile(size_t icon_index) const;
125 // Returns an avatar icon index that can be assigned to a newly created
126 // profile. Note that the icon may not be unique since there are a limited
127 // set of default icons.
128 size_t ChooseAvatarIconIndexForNewProfile() const;
130 const base::FilePath& GetUserDataDir() const;
132 // Register cache related preferences in Local State.
133 static void RegisterPrefs(PrefRegistrySimple* registry);
135 // Checks whether the high res avatar at index |icon_index| exists, and
136 // if it does not, calls |DownloadHighResAvatar|.
137 void DownloadHighResAvatarIfNeeded(size_t icon_index,
138 const base::FilePath& profile_path);
140 // Saves the avatar |image| at |image_path|. This is used both for the
141 // GAIA profile pictures and the ProfileAvatarDownloader that is used to
142 // download the high res avatars.
143 void SaveAvatarImageAtPath(const gfx::Image* image,
144 const std::string& key,
145 const base::FilePath& image_path,
146 const base::FilePath& profile_path);
148 void AddObserver(ProfileInfoCacheObserver* obs);
149 void RemoveObserver(ProfileInfoCacheObserver* obs);
151 private:
152 FRIEND_TEST_ALL_PREFIXES(ProfileInfoCacheTest, DownloadHighResAvatarTest);
154 const base::DictionaryValue* GetInfoForProfileAtIndex(size_t index) const;
155 // Saves the profile info to a cache and takes ownership of |info|.
156 // Currently the only information that is cached is the profile's name,
157 // user name, and avatar icon.
158 void SetInfoQuietlyForProfileAtIndex(size_t index,
159 base::DictionaryValue* info);
160 void SetInfoForProfileAtIndex(size_t index, base::DictionaryValue* info);
161 std::string CacheKeyFromProfilePath(const base::FilePath& profile_path) const;
162 std::vector<std::string>::iterator FindPositionForProfile(
163 const std::string& search_key,
164 const base::string16& search_name);
166 // Returns true if the given icon index is not in use by another profie.
167 bool IconIndexIsUnique(size_t icon_index) const;
169 // Tries to find an icon index that satisfies all the given conditions.
170 // Returns true if an icon was found, false otherwise.
171 bool ChooseAvatarIconIndexForNewProfile(bool allow_generic_icon,
172 bool must_be_unique,
173 size_t* out_icon_index) const;
175 // Updates the position of the profile at the given index so that the list
176 // of profiles is still sorted.
177 void UpdateSortForProfileIndex(size_t index);
179 // Loads or uses an already loaded high resolution image of the
180 // generic profile avatar.
181 const gfx::Image* GetHighResAvatarOfProfileAtIndex(size_t index) const;
183 // Starts downloading the high res avatar at index |icon_index| for profile
184 // with path |profile_path|.
185 void DownloadHighResAvatar(size_t icon_index,
186 const base::FilePath& profile_path);
188 // Returns the decoded image at |image_path|. Used both by the GAIA profile
189 // image and the high res avatars.
190 const gfx::Image* LoadAvatarPictureFromPath(
191 const base::FilePath& profile_path,
192 const std::string& key,
193 const base::FilePath& image_path) const;
195 // Called when the picture given by |key| has been loaded from disk and
196 // decoded into |image|.
197 void OnAvatarPictureLoaded(const base::FilePath& profile_path,
198 const std::string& key,
199 gfx::Image** image) const;
200 // Called when the picture given by |file_name| has been saved to disk.
201 // Used both for the GAIA profile picture and the high res avatar files.
202 void OnAvatarPictureSaved(const std::string& file_name,
203 const base::FilePath& profile_path);
205 // Migrate any legacy profile names ("First user", "Default Profile") to
206 // new style default names ("Person 1"), and download and high-res avatars
207 // used by the profiles.
208 void MigrateLegacyProfileNamesAndDownloadAvatars();
210 PrefService* prefs_;
211 std::vector<std::string> sorted_keys_;
212 base::FilePath user_data_dir_;
214 mutable ObserverList<ProfileInfoCacheObserver> observer_list_;
216 // A cache of gaia/high res avatar profile pictures. This cache is updated
217 // lazily so it needs to be mutable.
218 mutable std::map<std::string, gfx::Image*> cached_avatar_images_;
219 // Marks a profile picture as loading from disk. This prevents a picture from
220 // loading multiple times.
221 mutable std::map<std::string, bool> cached_avatar_images_loading_;
223 // Map of profile pictures currently being downloaded from the remote
224 // location and the ProfileAvatarDownloader instances downloading them.
225 // This prevents a picture from being downloaded multiple times. The
226 // ProfileAvatarDownloader instances are deleted when the download completes
227 // or when the ProfileInfoCache is destroyed.
228 std::map<std::string, ProfileAvatarDownloader*>
229 avatar_images_downloads_in_progress_;
231 DISALLOW_COPY_AND_ASSIGN(ProfileInfoCache);
234 #endif // CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_