Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / profiles / profile_info_cache.h
blob714427b8e28b40a38cb820d926c64e74f0ef5329
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/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"
21 namespace gfx {
22 class Image;
25 namespace base {
26 class DictionaryValue;
29 class PrefService;
30 class PrefRegistrySimple;
32 // This class saves various information about profiles to local preferences.
33 // This cache can be used to display a list of profiles without having to
34 // actually load the profiles from disk.
35 class ProfileInfoCache : public ProfileInfoInterface,
36 public base::SupportsWeakPtr<ProfileInfoCache> {
37 public:
38 ProfileInfoCache(PrefService* prefs, const base::FilePath& user_data_dir);
39 virtual ~ProfileInfoCache();
41 // This |is_managed| refers to local management (formerly "managed mode"),
42 // not enterprise management.
43 void AddProfileToCache(const base::FilePath& profile_path,
44 const base::string16& name,
45 const base::string16& username,
46 size_t icon_index,
47 const std::string& managed_user_id);
48 void DeleteProfileFromCache(const base::FilePath& profile_path);
50 // ProfileInfoInterface:
51 virtual size_t GetNumberOfProfiles() const OVERRIDE;
52 // Don't cache this value and reuse, because resorting the menu could cause
53 // the item being referred to to change out from under you.
54 virtual size_t GetIndexOfProfileWithPath(
55 const base::FilePath& profile_path) const OVERRIDE;
56 virtual base::string16 GetNameOfProfileAtIndex(size_t index) const OVERRIDE;
57 virtual base::string16 GetShortcutNameOfProfileAtIndex(size_t index)
58 const OVERRIDE;
59 virtual base::FilePath GetPathOfProfileAtIndex(size_t index) const OVERRIDE;
60 virtual base::Time GetProfileActiveTimeAtIndex(size_t index) const OVERRIDE;
61 virtual base::string16 GetUserNameOfProfileAtIndex(
62 size_t index) const OVERRIDE;
63 virtual const gfx::Image& GetAvatarIconOfProfileAtIndex(
64 size_t index) const OVERRIDE;
65 virtual std::string GetLocalAuthCredentialsOfProfileAtIndex(
66 size_t index) const OVERRIDE;
67 // Note that a return value of false could mean an error in collection or
68 // that there are currently no background apps running. However, the action
69 // which results is the same in both cases (thus far).
70 virtual bool GetBackgroundStatusOfProfileAtIndex(
71 size_t index) const OVERRIDE;
72 virtual base::string16 GetGAIANameOfProfileAtIndex(
73 size_t index) const OVERRIDE;
74 virtual base::string16 GetGAIAGivenNameOfProfileAtIndex(
75 size_t index) const OVERRIDE;
76 virtual bool IsUsingGAIANameOfProfileAtIndex(size_t index) const OVERRIDE;
77 // Returns the GAIA picture for the given profile. This may return NULL
78 // if the profile does not have a GAIA picture or if the picture must be
79 // loaded from disk.
80 virtual const gfx::Image* GetGAIAPictureOfProfileAtIndex(
81 size_t index) const OVERRIDE;
82 virtual bool IsUsingGAIAPictureOfProfileAtIndex(
83 size_t index) const OVERRIDE;
84 virtual bool ProfileIsManagedAtIndex(size_t index) const OVERRIDE;
85 virtual bool ProfileIsSigninRequiredAtIndex(size_t index) const OVERRIDE;
86 virtual std::string GetManagedUserIdOfProfileAtIndex(size_t index) const
87 OVERRIDE;
88 virtual bool ProfileIsEphemeralAtIndex(size_t index) const OVERRIDE;
90 size_t GetAvatarIconIndexOfProfileAtIndex(size_t index) const;
92 void SetProfileActiveTimeAtIndex(size_t index);
93 void SetNameOfProfileAtIndex(size_t index, const base::string16& name);
94 void SetShortcutNameOfProfileAtIndex(size_t index,
95 const base::string16& name);
96 void SetUserNameOfProfileAtIndex(size_t index,
97 const base::string16& user_name);
98 void SetAvatarIconOfProfileAtIndex(size_t index, size_t icon_index);
99 void SetManagedUserIdOfProfileAtIndex(size_t index, const std::string& id);
100 void SetLocalAuthCredentialsOfProfileAtIndex(size_t index,
101 const std::string& auth);
102 void SetBackgroundStatusOfProfileAtIndex(size_t index,
103 bool running_background_apps);
104 void SetGAIANameOfProfileAtIndex(size_t index, const base::string16& name);
105 void SetGAIAGivenNameOfProfileAtIndex(size_t index,
106 const base::string16& name);
107 void SetIsUsingGAIANameOfProfileAtIndex(size_t index, bool value);
108 void SetGAIAPictureOfProfileAtIndex(size_t index, const gfx::Image* image);
109 void SetIsUsingGAIAPictureOfProfileAtIndex(size_t index, bool value);
110 void SetProfileSigninRequiredAtIndex(size_t index, bool value);
111 void SetProfileIsEphemeralAtIndex(size_t index, bool value);
113 // Returns unique name that can be assigned to a newly created profile.
114 base::string16 ChooseNameForNewProfile(size_t icon_index) const;
116 // Checks if the given profile has switched to using GAIA information
117 // for the profile name and picture. This pref is used to switch over
118 // to GAIA info the first time it is available. Afterwards this pref is
119 // checked to prevent clobbering the user's custom settings.
120 bool GetHasMigratedToGAIAInfoOfProfileAtIndex(size_t index) const;
122 // Marks the given profile as having switched to using GAIA information
123 // for the profile name and picture.
124 void SetHasMigratedToGAIAInfoOfProfileAtIndex(size_t index, bool value);
126 // Returns an avatar icon index that can be assigned to a newly created
127 // profile. Note that the icon may not be unique since there are a limited
128 // set of default icons.
129 size_t ChooseAvatarIconIndexForNewProfile() const;
131 const base::FilePath& GetUserDataDir() const;
133 // Gets the number of default avatar icons that exist.
134 static size_t GetDefaultAvatarIconCount();
135 // Gets the resource ID of the default avatar icon at |index|.
136 static int GetDefaultAvatarIconResourceIDAtIndex(size_t index);
137 // Returns a URL for the default avatar icon with specified index.
138 static std::string GetDefaultAvatarIconUrl(size_t index);
139 // Checks if |index| is a valid avatar icon index
140 static bool IsDefaultAvatarIconIndex(size_t index);
141 // Checks if the given URL points to one of the default avatar icons. If it
142 // is, returns true and its index through |icon_index|. If not, returns false.
143 static bool IsDefaultAvatarIconUrl(const std::string& icon_url,
144 size_t *icon_index);
146 // Gets all names of profiles associated with this instance of Chrome.
147 // Because this method will be called during uninstall, before the creation
148 // of the ProfileManager, it reads directly from the local state preferences,
149 // rather than going through the ProfileInfoCache object.
150 static std::vector<base::string16> GetProfileNames();
152 // Register cache related preferences in Local State.
153 static void RegisterPrefs(PrefRegistrySimple* registry);
155 void AddObserver(ProfileInfoCacheObserver* obs);
156 void RemoveObserver(ProfileInfoCacheObserver* obs);
158 private:
159 const base::DictionaryValue* GetInfoForProfileAtIndex(size_t index) const;
160 // Saves the profile info to a cache and takes ownership of |info|.
161 // Currently the only information that is cached is the profile's name,
162 // user name, and avatar icon.
163 void SetInfoQuietlyForProfileAtIndex(size_t index,
164 base::DictionaryValue* info);
165 void SetInfoForProfileAtIndex(size_t index, base::DictionaryValue* info);
166 std::string CacheKeyFromProfilePath(const base::FilePath& profile_path) const;
167 std::vector<std::string>::iterator FindPositionForProfile(
168 const std::string& search_key,
169 const base::string16& search_name);
171 // Returns true if the given icon index is not in use by another profie.
172 bool IconIndexIsUnique(size_t icon_index) const;
174 // Tries to find an icon index that satisfies all the given conditions.
175 // Returns true if an icon was found, false otherwise.
176 bool ChooseAvatarIconIndexForNewProfile(bool allow_generic_icon,
177 bool must_be_unique,
178 size_t* out_icon_index) const;
180 // Updates the position of the profile at the given index so that the list
181 // of profiles is still sorted.
182 void UpdateSortForProfileIndex(size_t index);
184 void OnGAIAPictureLoaded(const base::FilePath& path,
185 gfx::Image** image) const;
186 void OnGAIAPictureSaved(const base::FilePath& path, bool* success) const;
188 PrefService* prefs_;
189 std::vector<std::string> sorted_keys_;
190 base::FilePath user_data_dir_;
192 ObserverList<ProfileInfoCacheObserver> observer_list_;
194 // A cache of gaia profile pictures. This cache is updated lazily so it needs
195 // to be mutable.
196 mutable std::map<std::string, gfx::Image*> gaia_pictures_;
197 // Marks a gaia profile picture as loading. This prevents a picture from
198 // loading multiple times.
199 mutable std::map<std::string, bool> gaia_pictures_loading_;
201 DISALLOW_COPY_AND_ASSIGN(ProfileInfoCache);
204 #endif // CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_