• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_
6 #define CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_
7 
8 #include <map>
9 #include <string>
10 #include <vector>
11 
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"
20 
21 namespace gfx {
22 class Image;
23 }
24 
25 namespace base {
26 class DictionaryValue;
27 }
28 
29 class PrefService;
30 class PrefRegistrySimple;
31 class ProfileAvatarDownloader;
32 
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   virtual ~ProfileInfoCache();
41 
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);
53 
54   // ProfileInfoInterface:
55   virtual 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   virtual size_t GetIndexOfProfileWithPath(
59       const base::FilePath& profile_path) const OVERRIDE;
60   virtual base::string16 GetNameOfProfileAtIndex(size_t index) const OVERRIDE;
61   virtual base::string16 GetShortcutNameOfProfileAtIndex(size_t index)
62       const OVERRIDE;
63   virtual base::FilePath GetPathOfProfileAtIndex(size_t index) const OVERRIDE;
64   virtual base::Time GetProfileActiveTimeAtIndex(size_t index) const OVERRIDE;
65   virtual base::string16 GetUserNameOfProfileAtIndex(
66       size_t index) const OVERRIDE;
67   virtual const gfx::Image& GetAvatarIconOfProfileAtIndex(
68       size_t index) const OVERRIDE;
69   virtual std::string GetLocalAuthCredentialsOfProfileAtIndex(
70       size_t index) const OVERRIDE;
71   // Note that a return value of false could mean an error in collection or
72   // that there are currently no background apps running. However, the action
73   // which results is the same in both cases (thus far).
74   virtual bool GetBackgroundStatusOfProfileAtIndex(
75       size_t index) const OVERRIDE;
76   virtual base::string16 GetGAIANameOfProfileAtIndex(
77       size_t index) const OVERRIDE;
78   virtual base::string16 GetGAIAGivenNameOfProfileAtIndex(
79       size_t index) const OVERRIDE;
80   // Returns the GAIA picture for the given profile. This may return NULL
81   // if the profile does not have a GAIA picture or if the picture must be
82   // loaded from disk.
83   virtual const gfx::Image* GetGAIAPictureOfProfileAtIndex(
84       size_t index) const OVERRIDE;
85   virtual bool IsUsingGAIAPictureOfProfileAtIndex(
86       size_t index) const OVERRIDE;
87   virtual bool ProfileIsSupervisedAtIndex(size_t index) const OVERRIDE;
88   virtual bool IsOmittedProfileAtIndex(size_t index) const OVERRIDE;
89   virtual bool ProfileIsSigninRequiredAtIndex(size_t index) const OVERRIDE;
90   virtual std::string GetSupervisedUserIdOfProfileAtIndex(size_t index) const
91       OVERRIDE;
92   virtual bool ProfileIsEphemeralAtIndex(size_t index) const OVERRIDE;
93   virtual bool ProfileIsUsingDefaultNameAtIndex(size_t index) const OVERRIDE;
94 
95   size_t GetAvatarIconIndexOfProfileAtIndex(size_t index) const;
96 
97   void SetProfileActiveTimeAtIndex(size_t index);
98   void SetNameOfProfileAtIndex(size_t index, const base::string16& name);
99   void SetShortcutNameOfProfileAtIndex(size_t index,
100                                        const base::string16& name);
101   void SetUserNameOfProfileAtIndex(size_t index,
102                                    const base::string16& user_name);
103   void SetAvatarIconOfProfileAtIndex(size_t index, size_t icon_index);
104   void SetIsOmittedProfileAtIndex(size_t index, bool is_omitted);
105   void SetSupervisedUserIdOfProfileAtIndex(size_t index, const std::string& id);
106   void SetLocalAuthCredentialsOfProfileAtIndex(size_t index,
107                                                const std::string& auth);
108   void SetBackgroundStatusOfProfileAtIndex(size_t index,
109                                            bool running_background_apps);
110   void SetGAIANameOfProfileAtIndex(size_t index, const base::string16& name);
111   void SetGAIAGivenNameOfProfileAtIndex(size_t index,
112                                         const base::string16& name);
113   void SetGAIAPictureOfProfileAtIndex(size_t index, const gfx::Image* image);
114   void SetIsUsingGAIAPictureOfProfileAtIndex(size_t index, bool value);
115   void SetProfileSigninRequiredAtIndex(size_t index, bool value);
116   void SetProfileIsEphemeralAtIndex(size_t index, bool value);
117   void SetProfileIsUsingDefaultNameAtIndex(size_t index, bool value);
118 
119   // Returns unique name that can be assigned to a newly created profile.
120   base::string16 ChooseNameForNewProfile(size_t icon_index) const;
121 
122   // Returns an avatar icon index that can be assigned to a newly created
123   // profile. Note that the icon may not be unique since there are a limited
124   // set of default icons.
125   size_t ChooseAvatarIconIndexForNewProfile() const;
126 
127   const base::FilePath& GetUserDataDir() const;
128 
129   // Gets all names of profiles associated with this instance of Chrome.
130   // Because this method will be called during uninstall, before the creation
131   // of the ProfileManager, it reads directly from the local state preferences,
132   // rather than going through the ProfileInfoCache object.
133   static std::vector<base::string16> GetProfileNames();
134 
135   // Register cache related preferences in Local State.
136   static void RegisterPrefs(PrefRegistrySimple* registry);
137 
138   // Starts downloading the high res avatar at index |icon_index| for profile
139   // with path |profile_path|.
140   void DownloadHighResAvatar(size_t icon_index,
141                              const base::FilePath& profile_path);
142 
143   // Saves the avatar |image| at |image_path|. This is used both for the
144   // GAIA profile pictures and the ProfileAvatarDownloader that is used to
145   // download the high res avatars.
146   void SaveAvatarImageAtPath(const gfx::Image* image,
147                              const std::string& key,
148                              const base::FilePath& image_path,
149                              const base::FilePath& profile_path);
150 
151   void AddObserver(ProfileInfoCacheObserver* obs);
152   void RemoveObserver(ProfileInfoCacheObserver* obs);
153 
154  private:
155   FRIEND_TEST_ALL_PREFIXES(ProfileInfoCacheTest, DownloadHighResAvatarTest);
156 
157   const base::DictionaryValue* GetInfoForProfileAtIndex(size_t index) const;
158   // Saves the profile info to a cache and takes ownership of |info|.
159   // Currently the only information that is cached is the profile's name,
160   // user name, and avatar icon.
161   void SetInfoQuietlyForProfileAtIndex(size_t index,
162                                        base::DictionaryValue* info);
163   void SetInfoForProfileAtIndex(size_t index, base::DictionaryValue* info);
164   std::string CacheKeyFromProfilePath(const base::FilePath& profile_path) const;
165   std::vector<std::string>::iterator FindPositionForProfile(
166       const std::string& search_key,
167       const base::string16& search_name);
168 
169   // Returns true if the given icon index is not in use by another profie.
170   bool IconIndexIsUnique(size_t icon_index) const;
171 
172   // Tries to find an icon index that satisfies all the given conditions.
173   // Returns true if an icon was found, false otherwise.
174   bool ChooseAvatarIconIndexForNewProfile(bool allow_generic_icon,
175                                           bool must_be_unique,
176                                           size_t* out_icon_index) const;
177 
178   // Updates the position of the profile at the given index so that the list
179   // of profiles is still sorted.
180   void UpdateSortForProfileIndex(size_t index);
181 
182   // Loads or uses an already loaded high resolution image of the
183   // generic profile avatar.
184   const gfx::Image* GetHighResAvatarOfProfileAtIndex(size_t index) const;
185 
186   // Returns the decoded image at |image_path|. Used both by the GAIA profile
187   // image and the high res avatars.
188   const gfx::Image* LoadAvatarPictureFromPath(
189       const std::string& key,
190       const base::FilePath& image_path) const;
191 
192   // Called when the picture given by |key| has been loaded from disk and
193   // decoded into |image|.
194   void OnAvatarPictureLoaded(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);
200 
201   PrefService* prefs_;
202   std::vector<std::string> sorted_keys_;
203   base::FilePath user_data_dir_;
204 
205   ObserverList<ProfileInfoCacheObserver> observer_list_;
206 
207   // A cache of gaia/high res avatar profile pictures. This cache is updated
208   // lazily so it needs to be mutable.
209   mutable std::map<std::string, gfx::Image*> cached_avatar_images_;
210   // Marks a profile picture as loading from disk. This prevents a picture from
211   // loading multiple times.
212   mutable std::map<std::string, bool> cached_avatar_images_loading_;
213 
214   // Map of profile pictures currently being downloaded from the remote
215   // location and the ProfileAvatarDownloader instances downloading them.
216   // This prevents a picture from being downloaded multiple times. The
217   // ProfileAvatarDownloader instances are deleted when the download completes
218   // or when the ProfileInfoCache is destroyed.
219   mutable std::map<std::string, ProfileAvatarDownloader*>
220       avatar_images_downloads_in_progress_;
221 
222   DISALLOW_COPY_AND_ASSIGN(ProfileInfoCache);
223 };
224 
225 #endif  // CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_
226