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 // Gets all names of profiles associated with this instance of Chrome.
133 // Because this method will be called during uninstall, before the creation
134 // of the ProfileManager, it reads directly from the local state preferences,
135 // rather than going through the ProfileInfoCache object.
136 static std::vector
<base::string16
> GetProfileNames();
138 // Register cache related preferences in Local State.
139 static void RegisterPrefs(PrefRegistrySimple
* registry
);
141 // Starts downloading the high res avatar at index |icon_index| for profile
142 // with path |profile_path|.
143 void DownloadHighResAvatar(size_t icon_index
,
144 const base::FilePath
& profile_path
);
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 const base::FilePath
& profile_path
);
154 void AddObserver(ProfileInfoCacheObserver
* obs
);
155 void RemoveObserver(ProfileInfoCacheObserver
* obs
);
158 FRIEND_TEST_ALL_PREFIXES(ProfileInfoCacheTest
, DownloadHighResAvatarTest
);
160 const base::DictionaryValue
* GetInfoForProfileAtIndex(size_t index
) const;
161 // Saves the profile info to a cache and takes ownership of |info|.
162 // Currently the only information that is cached is the profile's name,
163 // user name, and avatar icon.
164 void SetInfoQuietlyForProfileAtIndex(size_t index
,
165 base::DictionaryValue
* info
);
166 void SetInfoForProfileAtIndex(size_t index
, base::DictionaryValue
* info
);
167 std::string
CacheKeyFromProfilePath(const base::FilePath
& profile_path
) const;
168 std::vector
<std::string
>::iterator
FindPositionForProfile(
169 const std::string
& search_key
,
170 const base::string16
& search_name
);
172 // Returns true if the given icon index is not in use by another profie.
173 bool IconIndexIsUnique(size_t icon_index
) const;
175 // Tries to find an icon index that satisfies all the given conditions.
176 // Returns true if an icon was found, false otherwise.
177 bool ChooseAvatarIconIndexForNewProfile(bool allow_generic_icon
,
179 size_t* out_icon_index
) const;
181 // Updates the position of the profile at the given index so that the list
182 // of profiles is still sorted.
183 void UpdateSortForProfileIndex(size_t index
);
185 // Loads or uses an already loaded high resolution image of the
186 // generic profile avatar.
187 const gfx::Image
* GetHighResAvatarOfProfileAtIndex(size_t index
) const;
189 // Returns the decoded image at |image_path|. Used both by the GAIA profile
190 // image and the high res avatars.
191 const gfx::Image
* LoadAvatarPictureFromPath(
192 const base::FilePath
& profile_path
,
193 const std::string
& key
,
194 const base::FilePath
& image_path
) const;
196 // Called when the picture given by |key| has been loaded from disk and
197 // decoded into |image|.
198 void OnAvatarPictureLoaded(const base::FilePath
& profile_path
,
199 const std::string
& key
,
200 gfx::Image
** image
) const;
201 // Called when the picture given by |file_name| has been saved to disk.
202 // Used both for the GAIA profile picture and the high res avatar files.
203 void OnAvatarPictureSaved(const std::string
& file_name
,
204 const base::FilePath
& profile_path
);
206 // Migrate any legacy profile names ("First user", "Default Profile") to
207 // new style default names ("Person 1"), and download and high-res avatars
208 // used by the profiles.
209 void MigrateLegacyProfileNamesAndDownloadAvatars();
212 std::vector
<std::string
> sorted_keys_
;
213 base::FilePath user_data_dir_
;
215 mutable ObserverList
<ProfileInfoCacheObserver
> observer_list_
;
217 // A cache of gaia/high res avatar profile pictures. This cache is updated
218 // lazily so it needs to be mutable.
219 mutable std::map
<std::string
, gfx::Image
*> cached_avatar_images_
;
220 // Marks a profile picture as loading from disk. This prevents a picture from
221 // loading multiple times.
222 mutable std::map
<std::string
, bool> cached_avatar_images_loading_
;
224 // Map of profile pictures currently being downloaded from the remote
225 // location and the ProfileAvatarDownloader instances downloading them.
226 // This prevents a picture from being downloaded multiple times. The
227 // ProfileAvatarDownloader instances are deleted when the download completes
228 // or when the ProfileInfoCache is destroyed.
229 mutable std::map
<std::string
, ProfileAvatarDownloader
*>
230 avatar_images_downloads_in_progress_
;
232 DISALLOW_COPY_AND_ASSIGN(ProfileInfoCache
);
235 #endif // CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_