Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / profiles / profile_info_cache.h
blob6969344dc23d6e5542c223710489c206406a0cbe
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/containers/scoped_ptr_hash_map.h"
15 #include "base/files/file_path.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "base/strings/string16.h"
19 #include "chrome/browser/profiles/profile_attributes_entry.h"
20 #include "chrome/browser/profiles/profile_attributes_storage.h"
21 #include "chrome/browser/profiles/profile_info_cache_observer.h"
22 #include "chrome/browser/profiles/profile_info_interface.h"
24 namespace gfx {
25 class Image;
28 namespace base {
29 class DictionaryValue;
32 class PrefService;
33 class PrefRegistrySimple;
34 class ProfileAvatarDownloader;
36 // This class saves various information about profiles to local preferences.
37 // This cache can be used to display a list of profiles without having to
38 // actually load the profiles from disk.
39 // The ProfileInfoInterface is being deprecated. Prefer using the
40 // ProfileAttributesStorage and avoid using the Get*AtIndex family of functions.
41 class ProfileInfoCache : public ProfileInfoInterface,
42 public ProfileAttributesStorage,
43 public base::SupportsWeakPtr<ProfileInfoCache> {
44 public:
45 ProfileInfoCache(PrefService* prefs, const base::FilePath& user_data_dir);
46 ~ProfileInfoCache() override;
48 // If the |supervised_user_id| is non-empty, the profile will be marked to be
49 // omitted from the avatar-menu list on desktop versions. This is used while a
50 // supervised user is in the process of being registered with the server. Use
51 // SetIsOmittedProfileAtIndex() to clear the flag when the profile is ready to
52 // be shown in the menu.
53 // Deprecated. Use AddProfile instead.
54 void AddProfileToCache(const base::FilePath& profile_path,
55 const base::string16& name,
56 const std::string& gaia_id,
57 const base::string16& user_name,
58 size_t icon_index,
59 const std::string& supervised_user_id);
60 // Deprecated. Use RemoveProfile instead.
61 void DeleteProfileFromCache(const base::FilePath& profile_path);
63 // ProfileInfoInterface:
64 size_t GetNumberOfProfiles() const override;
65 // Don't cache this value and reuse, because resorting the menu could cause
66 // the item being referred to to change out from under you.
67 // Deprecated. Prefer using the ProfileAttributesStorage interface instead of
68 // directly referring to this implementation.
69 size_t GetIndexOfProfileWithPath(
70 const base::FilePath& profile_path) const override;
71 base::string16 GetNameOfProfileAtIndex(size_t index) const override;
72 base::string16 GetShortcutNameOfProfileAtIndex(size_t index) const override;
73 base::FilePath GetPathOfProfileAtIndex(size_t index) const override;
74 base::Time GetProfileActiveTimeAtIndex(size_t index) const override;
75 base::string16 GetUserNameOfProfileAtIndex(size_t index) const override;
76 const gfx::Image& GetAvatarIconOfProfileAtIndex(size_t index) override;
77 std::string GetLocalAuthCredentialsOfProfileAtIndex(
78 size_t index) const override;
79 std::string GetPasswordChangeDetectionTokenAtIndex(
80 size_t index) const override;
81 // Note that a return value of false could mean an error in collection or
82 // that there are currently no background apps running. However, the action
83 // which results is the same in both cases (thus far).
84 bool GetBackgroundStatusOfProfileAtIndex(size_t index) const override;
85 base::string16 GetGAIANameOfProfileAtIndex(size_t index) const override;
86 base::string16 GetGAIAGivenNameOfProfileAtIndex(size_t index) const override;
87 std::string GetGAIAIdOfProfileAtIndex(size_t index) const override;
88 // Returns the GAIA picture for the given profile. This may return NULL
89 // if the profile does not have a GAIA picture or if the picture must be
90 // loaded from disk.
91 const gfx::Image* GetGAIAPictureOfProfileAtIndex(size_t index) const override;
92 bool IsUsingGAIAPictureOfProfileAtIndex(size_t index) const override;
93 bool ProfileIsSupervisedAtIndex(size_t index) const override;
94 bool ProfileIsChildAtIndex(size_t index) const override;
95 bool ProfileIsLegacySupervisedAtIndex(size_t index) const override;
96 bool IsOmittedProfileAtIndex(size_t index) const override;
97 bool ProfileIsSigninRequiredAtIndex(size_t index) const override;
98 std::string GetSupervisedUserIdOfProfileAtIndex(size_t index) const override;
99 bool ProfileIsEphemeralAtIndex(size_t index) const override;
100 bool ProfileIsUsingDefaultNameAtIndex(size_t index) const override;
101 bool ProfileIsAuthenticatedAtIndex(size_t index) const override;
102 bool ProfileIsUsingDefaultAvatarAtIndex(size_t index) const override;
103 bool ProfileIsAuthErrorAtIndex(size_t index) const;
105 size_t GetAvatarIconIndexOfProfileAtIndex(size_t index) const;
107 void SetProfileActiveTimeAtIndex(size_t index);
108 // Warning: This will re-sort profiles and thus may change indices!
109 void SetNameOfProfileAtIndex(size_t index, const base::string16& name);
110 void SetShortcutNameOfProfileAtIndex(size_t index,
111 const base::string16& name);
112 void SetAuthInfoOfProfileAtIndex(size_t index,
113 const std::string& gaia_id,
114 const base::string16& user_name);
115 void SetAvatarIconOfProfileAtIndex(size_t index, size_t icon_index);
116 void SetIsOmittedProfileAtIndex(size_t index, bool is_omitted);
117 void SetSupervisedUserIdOfProfileAtIndex(size_t index, const std::string& id);
118 void SetLocalAuthCredentialsOfProfileAtIndex(size_t index,
119 const std::string& auth);
120 void SetPasswordChangeDetectionTokenAtIndex(size_t index,
121 const std::string& token);
122 void SetBackgroundStatusOfProfileAtIndex(size_t index,
123 bool running_background_apps);
124 // Warning: This will re-sort profiles and thus may change indices!
125 void SetGAIANameOfProfileAtIndex(size_t index, const base::string16& name);
126 // Warning: This will re-sort profiles and thus may change indices!
127 void SetGAIAGivenNameOfProfileAtIndex(size_t index,
128 const base::string16& name);
129 void SetGAIAPictureOfProfileAtIndex(size_t index, const gfx::Image* image);
130 void SetIsUsingGAIAPictureOfProfileAtIndex(size_t index, bool value);
131 void SetProfileSigninRequiredAtIndex(size_t index, bool value);
132 void SetProfileIsEphemeralAtIndex(size_t index, bool value);
133 void SetProfileIsUsingDefaultNameAtIndex(size_t index, bool value);
134 void SetProfileIsUsingDefaultAvatarAtIndex(size_t index, bool value);
135 void SetProfileIsAuthErrorAtIndex(size_t index, bool value);
137 // Determines whether |name| is one of the default assigned names.
138 bool IsDefaultProfileName(const base::string16& name) const;
140 // Returns unique name that can be assigned to a newly created profile.
141 base::string16 ChooseNameForNewProfile(size_t icon_index) const;
143 // Returns an avatar icon index that can be assigned to a newly created
144 // profile. Note that the icon may not be unique since there are a limited
145 // set of default icons.
146 size_t ChooseAvatarIconIndexForNewProfile() const;
148 const base::FilePath& GetUserDataDir() const;
150 // Register cache related preferences in Local State.
151 static void RegisterPrefs(PrefRegistrySimple* registry);
153 // Checks whether the high res avatar at index |icon_index| exists, and
154 // if it does not, calls |DownloadHighResAvatar|.
155 void DownloadHighResAvatarIfNeeded(size_t icon_index,
156 const base::FilePath& profile_path);
158 // Saves the avatar |image| at |image_path|. This is used both for the
159 // GAIA profile pictures and the ProfileAvatarDownloader that is used to
160 // download the high res avatars.
161 void SaveAvatarImageAtPath(const gfx::Image* image,
162 const std::string& key,
163 const base::FilePath& image_path,
164 const base::FilePath& profile_path);
166 void AddObserver(ProfileInfoCacheObserver* obs);
167 void RemoveObserver(ProfileInfoCacheObserver* obs);
169 void set_disable_avatar_download_for_testing(
170 bool disable_avatar_download_for_testing) {
171 disable_avatar_download_for_testing_ = disable_avatar_download_for_testing;
174 // ProfileAttributesStorage:
175 void AddProfile(const base::FilePath& profile_path,
176 const base::string16& name,
177 const std::string& gaia_id,
178 const base::string16& user_name,
179 size_t icon_index,
180 const std::string& supervised_user_id) override;
181 void RemoveProfile(const base::FilePath& profile_path) override;
182 // Returns a vector containing one attributes entry per known profile. They
183 // are not sorted in any particular order.
184 std::vector<ProfileAttributesEntry*> GetAllProfilesAttributes() override;
185 bool GetProfileAttributesWithPath(
186 const base::FilePath& path,
187 ProfileAttributesEntry** entry) override;
189 private:
190 FRIEND_TEST_ALL_PREFIXES(ProfileInfoCacheTest, DownloadHighResAvatarTest);
191 FRIEND_TEST_ALL_PREFIXES(ProfileInfoCacheTest,
192 NothingToDownloadHighResAvatarTest);
194 const base::DictionaryValue* GetInfoForProfileAtIndex(size_t index) const;
195 // Saves the profile info to a cache and takes ownership of |info|.
196 void SetInfoForProfileAtIndex(size_t index, base::DictionaryValue* info);
197 std::string CacheKeyFromProfilePath(const base::FilePath& profile_path) const;
198 std::vector<std::string>::iterator FindPositionForProfile(
199 const std::string& search_key,
200 const base::string16& search_name);
202 // Returns true if the given icon index is not in use by another profie.
203 bool IconIndexIsUnique(size_t icon_index) const;
205 // Tries to find an icon index that satisfies all the given conditions.
206 // Returns true if an icon was found, false otherwise.
207 bool ChooseAvatarIconIndexForNewProfile(bool allow_generic_icon,
208 bool must_be_unique,
209 size_t* out_icon_index) const;
211 // Updates the position of the profile at the given index so that the list
212 // of profiles is still sorted.
213 void UpdateSortForProfileIndex(size_t index);
215 // Loads or uses an already loaded high resolution image of the
216 // generic profile avatar.
217 const gfx::Image* GetHighResAvatarOfProfileAtIndex(size_t index) const;
219 // Starts downloading the high res avatar at index |icon_index| for profile
220 // with path |profile_path|.
221 void DownloadHighResAvatar(size_t icon_index,
222 const base::FilePath& profile_path);
224 // Returns the decoded image at |image_path|. Used both by the GAIA profile
225 // image and the high res avatars.
226 const gfx::Image* LoadAvatarPictureFromPath(
227 const base::FilePath& profile_path,
228 const std::string& key,
229 const base::FilePath& image_path) const;
231 // Called when the picture given by |key| has been loaded from disk and
232 // decoded into |image|.
233 void OnAvatarPictureLoaded(const base::FilePath& profile_path,
234 const std::string& key,
235 gfx::Image** image) const;
236 // Called when the picture given by |file_name| has been saved to disk.
237 // Used both for the GAIA profile picture and the high res avatar files.
238 void OnAvatarPictureSaved(const std::string& file_name,
239 const base::FilePath& profile_path);
241 // Migrate any legacy profile names ("First user", "Default Profile") to
242 // new style default names ("Person 1"), and download and high-res avatars
243 // used by the profiles.
244 void MigrateLegacyProfileNamesAndDownloadAvatars();
246 PrefService* prefs_;
247 std::vector<std::string> sorted_keys_;
248 base::ScopedPtrHashMap<base::FilePath, scoped_ptr<ProfileAttributesEntry> >
249 profile_attributes_entries_;
250 base::FilePath user_data_dir_;
252 mutable base::ObserverList<ProfileInfoCacheObserver> observer_list_;
254 // A cache of gaia/high res avatar profile pictures. This cache is updated
255 // lazily so it needs to be mutable.
256 mutable std::map<std::string, gfx::Image*> cached_avatar_images_;
257 // Marks a profile picture as loading from disk. This prevents a picture from
258 // loading multiple times.
259 mutable std::map<std::string, bool> cached_avatar_images_loading_;
261 // Map of profile pictures currently being downloaded from the remote
262 // location and the ProfileAvatarDownloader instances downloading them.
263 // This prevents a picture from being downloaded multiple times. The
264 // ProfileAvatarDownloader instances are deleted when the download completes
265 // or when the ProfileInfoCache is destroyed.
266 std::map<std::string, ProfileAvatarDownloader*>
267 avatar_images_downloads_in_progress_;
269 // Determines of the ProfileAvatarDownloader should be created and executed
270 // or not. Only set to true for tests.
271 bool disable_avatar_download_for_testing_;
273 DISALLOW_COPY_AND_ASSIGN(ProfileInfoCache);
276 #endif // CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_