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_
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"
26 class DictionaryValue
;
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
> {
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
,
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
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
);
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 void SetInfoForProfileAtIndex(size_t index
, base::DictionaryValue
* info
);
157 std::string
CacheKeyFromProfilePath(const base::FilePath
& profile_path
) const;
158 std::vector
<std::string
>::iterator
FindPositionForProfile(
159 const std::string
& search_key
,
160 const base::string16
& search_name
);
162 // Returns true if the given icon index is not in use by another profie.
163 bool IconIndexIsUnique(size_t icon_index
) const;
165 // Tries to find an icon index that satisfies all the given conditions.
166 // Returns true if an icon was found, false otherwise.
167 bool ChooseAvatarIconIndexForNewProfile(bool allow_generic_icon
,
169 size_t* out_icon_index
) const;
171 // Updates the position of the profile at the given index so that the list
172 // of profiles is still sorted.
173 void UpdateSortForProfileIndex(size_t index
);
175 // Loads or uses an already loaded high resolution image of the
176 // generic profile avatar.
177 const gfx::Image
* GetHighResAvatarOfProfileAtIndex(size_t index
) const;
179 // Starts downloading the high res avatar at index |icon_index| for profile
180 // with path |profile_path|.
181 void DownloadHighResAvatar(size_t icon_index
,
182 const base::FilePath
& profile_path
);
184 // Returns the decoded image at |image_path|. Used both by the GAIA profile
185 // image and the high res avatars.
186 const gfx::Image
* LoadAvatarPictureFromPath(
187 const base::FilePath
& profile_path
,
188 const std::string
& key
,
189 const base::FilePath
& image_path
) const;
191 // Called when the picture given by |key| has been loaded from disk and
192 // decoded into |image|.
193 void OnAvatarPictureLoaded(const base::FilePath
& profile_path
,
194 const std::string
& key
,
195 gfx::Image
** image
) const;
196 // Called when the picture given by |file_name| has been saved to disk.
197 // Used both for the GAIA profile picture and the high res avatar files.
198 void OnAvatarPictureSaved(const std::string
& file_name
,
199 const base::FilePath
& profile_path
);
201 // Migrate any legacy profile names ("First user", "Default Profile") to
202 // new style default names ("Person 1"), and download and high-res avatars
203 // used by the profiles.
204 void MigrateLegacyProfileNamesAndDownloadAvatars();
207 std::vector
<std::string
> sorted_keys_
;
208 base::FilePath user_data_dir_
;
210 mutable ObserverList
<ProfileInfoCacheObserver
> observer_list_
;
212 // A cache of gaia/high res avatar profile pictures. This cache is updated
213 // lazily so it needs to be mutable.
214 mutable std::map
<std::string
, gfx::Image
*> cached_avatar_images_
;
215 // Marks a profile picture as loading from disk. This prevents a picture from
216 // loading multiple times.
217 mutable std::map
<std::string
, bool> cached_avatar_images_loading_
;
219 // Map of profile pictures currently being downloaded from the remote
220 // location and the ProfileAvatarDownloader instances downloading them.
221 // This prevents a picture from being downloaded multiple times. The
222 // ProfileAvatarDownloader instances are deleted when the download completes
223 // or when the ProfileInfoCache is destroyed.
224 std::map
<std::string
, ProfileAvatarDownloader
*>
225 avatar_images_downloads_in_progress_
;
227 DISALLOW_COPY_AND_ASSIGN(ProfileInfoCache
);
230 #endif // CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_