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 virtual ~ProfileInfoCache();
42 // This |is_managed| refers to local management (formerly "managed mode"),
43 // not enterprise management. If the |managed_user_id| is non-empty, the
44 // profile will be marked to be omitted from the avatar-menu list on desktop
45 // versions. This is used while a managed user is in the process of being
46 // registered with the server. Use SetIsOmittedProfileAtIndex() to clear the
47 // flag when the profile is ready to be shown in the menu.
48 void AddProfileToCache(const base::FilePath
& profile_path
,
49 const base::string16
& name
,
50 const base::string16
& username
,
52 const std::string
& managed_user_id
);
53 void DeleteProfileFromCache(const base::FilePath
& profile_path
);
55 // ProfileInfoInterface:
56 virtual size_t GetNumberOfProfiles() const OVERRIDE
;
57 // Don't cache this value and reuse, because resorting the menu could cause
58 // the item being referred to to change out from under you.
59 virtual size_t GetIndexOfProfileWithPath(
60 const base::FilePath
& profile_path
) const OVERRIDE
;
61 virtual base::string16
GetNameOfProfileAtIndex(size_t index
) const OVERRIDE
;
62 virtual base::string16
GetShortcutNameOfProfileAtIndex(size_t index
)
64 virtual base::FilePath
GetPathOfProfileAtIndex(size_t index
) const OVERRIDE
;
65 virtual base::Time
GetProfileActiveTimeAtIndex(size_t index
) const OVERRIDE
;
66 virtual base::string16
GetUserNameOfProfileAtIndex(
67 size_t index
) const OVERRIDE
;
68 virtual const gfx::Image
& GetAvatarIconOfProfileAtIndex(
69 size_t index
) const OVERRIDE
;
70 virtual std::string
GetLocalAuthCredentialsOfProfileAtIndex(
71 size_t index
) const OVERRIDE
;
72 // Note that a return value of false could mean an error in collection or
73 // that there are currently no background apps running. However, the action
74 // which results is the same in both cases (thus far).
75 virtual bool GetBackgroundStatusOfProfileAtIndex(
76 size_t index
) const OVERRIDE
;
77 virtual base::string16
GetGAIANameOfProfileAtIndex(
78 size_t index
) const OVERRIDE
;
79 virtual base::string16
GetGAIAGivenNameOfProfileAtIndex(
80 size_t index
) const OVERRIDE
;
81 // Returns the GAIA picture for the given profile. This may return NULL
82 // if the profile does not have a GAIA picture or if the picture must be
84 virtual const gfx::Image
* GetGAIAPictureOfProfileAtIndex(
85 size_t index
) const OVERRIDE
;
86 virtual bool IsUsingGAIAPictureOfProfileAtIndex(
87 size_t index
) const OVERRIDE
;
88 virtual bool ProfileIsManagedAtIndex(size_t index
) const OVERRIDE
;
89 virtual bool IsOmittedProfileAtIndex(size_t index
) const OVERRIDE
;
90 virtual bool ProfileIsSigninRequiredAtIndex(size_t index
) const OVERRIDE
;
91 virtual std::string
GetManagedUserIdOfProfileAtIndex(size_t index
) const
93 virtual bool ProfileIsEphemeralAtIndex(size_t index
) const OVERRIDE
;
94 virtual bool ProfileIsUsingDefaultNameAtIndex(size_t index
) const OVERRIDE
;
96 size_t GetAvatarIconIndexOfProfileAtIndex(size_t index
) const;
98 void SetProfileActiveTimeAtIndex(size_t index
);
99 void SetNameOfProfileAtIndex(size_t index
, const base::string16
& name
);
100 void SetShortcutNameOfProfileAtIndex(size_t index
,
101 const base::string16
& name
);
102 void SetUserNameOfProfileAtIndex(size_t index
,
103 const base::string16
& user_name
);
104 void SetAvatarIconOfProfileAtIndex(size_t index
, size_t icon_index
);
105 void SetIsOmittedProfileAtIndex(size_t index
, bool is_omitted
);
106 void SetManagedUserIdOfProfileAtIndex(size_t index
, const std::string
& id
);
107 void SetLocalAuthCredentialsOfProfileAtIndex(size_t index
,
108 const std::string
& auth
);
109 void SetBackgroundStatusOfProfileAtIndex(size_t index
,
110 bool running_background_apps
);
111 void SetGAIANameOfProfileAtIndex(size_t index
, const base::string16
& name
);
112 void SetGAIAGivenNameOfProfileAtIndex(size_t index
,
113 const base::string16
& name
);
114 void SetGAIAPictureOfProfileAtIndex(size_t index
, const gfx::Image
* image
);
115 void SetIsUsingGAIAPictureOfProfileAtIndex(size_t index
, bool value
);
116 void SetProfileSigninRequiredAtIndex(size_t index
, bool value
);
117 void SetProfileIsEphemeralAtIndex(size_t index
, bool value
);
118 void SetProfileIsUsingDefaultNameAtIndex(size_t index
, bool value
);
120 // Returns unique name that can be assigned to a newly created profile.
121 base::string16
ChooseNameForNewProfile(size_t icon_index
) const;
123 // Returns an avatar icon index that can be assigned to a newly created
124 // profile. Note that the icon may not be unique since there are a limited
125 // set of default icons.
126 size_t ChooseAvatarIconIndexForNewProfile() const;
128 const base::FilePath
& GetUserDataDir() const;
130 // Gets all names of profiles associated with this instance of Chrome.
131 // Because this method will be called during uninstall, before the creation
132 // of the ProfileManager, it reads directly from the local state preferences,
133 // rather than going through the ProfileInfoCache object.
134 static std::vector
<base::string16
> GetProfileNames();
136 // Register cache related preferences in Local State.
137 static void RegisterPrefs(PrefRegistrySimple
* registry
);
139 // Starts downloading the high res avatar at index |icon_index| for profile
140 // with path |profile_path|.
141 void DownloadHighResAvatar(size_t icon_index
,
142 const base::FilePath
& profile_path
);
144 // Saves the avatar |image| at |image_path|. This is used both for the
145 // GAIA profile pictures and the ProfileAvatarDownloader that is used to
146 // download the high res avatars.
147 void SaveAvatarImageAtPath(const gfx::Image
* image
,
148 const std::string
& key
,
149 const base::FilePath
& image_path
,
150 const base::FilePath
& profile_path
);
152 void AddObserver(ProfileInfoCacheObserver
* obs
);
153 void RemoveObserver(ProfileInfoCacheObserver
* obs
);
156 FRIEND_TEST_ALL_PREFIXES(ProfileInfoCacheTest
, DownloadHighResAvatarTest
);
158 const base::DictionaryValue
* GetInfoForProfileAtIndex(size_t index
) const;
159 // Saves the profile info to a cache and takes ownership of |info|.
160 // Currently the only information that is cached is the profile's name,
161 // user name, and avatar icon.
162 void SetInfoQuietlyForProfileAtIndex(size_t index
,
163 base::DictionaryValue
* info
);
164 void SetInfoForProfileAtIndex(size_t index
, base::DictionaryValue
* info
);
165 std::string
CacheKeyFromProfilePath(const base::FilePath
& profile_path
) const;
166 std::vector
<std::string
>::iterator
FindPositionForProfile(
167 const std::string
& search_key
,
168 const base::string16
& search_name
);
170 // Returns true if the given icon index is not in use by another profie.
171 bool IconIndexIsUnique(size_t icon_index
) const;
173 // Tries to find an icon index that satisfies all the given conditions.
174 // Returns true if an icon was found, false otherwise.
175 bool ChooseAvatarIconIndexForNewProfile(bool allow_generic_icon
,
177 size_t* out_icon_index
) const;
179 // Updates the position of the profile at the given index so that the list
180 // of profiles is still sorted.
181 void UpdateSortForProfileIndex(size_t index
);
183 // Loads or uses an already loaded high resolution image of the
184 // generic profile avatar.
185 const gfx::Image
* GetHighResAvatarOfProfileAtIndex(size_t index
) const;
187 // Returns the decoded image at |image_path|. Used both by the GAIA profile
188 // image and the high res avatars.
189 const gfx::Image
* LoadAvatarPictureFromPath(
190 const std::string
& key
,
191 const base::FilePath
& image_path
) const;
193 // Called when the picture given by |key| has been loaded from disk and
194 // decoded into |image|.
195 void OnAvatarPictureLoaded(const std::string
& key
,
196 gfx::Image
** image
) const;
197 // Called when the picture given by |file_name| has been saved to disk.
198 // Used both for the GAIA profile picture and the high res avatar files.
199 void OnAvatarPictureSaved(const std::string
& file_name
,
200 const base::FilePath
& profile_path
);
203 std::vector
<std::string
> sorted_keys_
;
204 base::FilePath user_data_dir_
;
206 ObserverList
<ProfileInfoCacheObserver
> observer_list_
;
208 // A cache of gaia/high res avatar profile pictures. This cache is updated
209 // lazily so it needs to be mutable.
210 mutable std::map
<std::string
, gfx::Image
*> cached_avatar_images_
;
211 // Marks a profile picture as loading from disk. This prevents a picture from
212 // loading multiple times.
213 mutable std::map
<std::string
, bool> cached_avatar_images_loading_
;
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 instances are deleted when the download completes
219 // or when the ProfileInfoCache is destroyed.
220 mutable std::map
<std::string
, ProfileAvatarDownloader
*>
221 avatar_images_downloads_in_progress_
;
223 DISALLOW_COPY_AND_ASSIGN(ProfileInfoCache
);
226 #endif // CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_