profile_info_cache.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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/string16.h"
18#include "chrome/browser/profiles/profile_info_cache_observer.h"
19#include "chrome/browser/profiles/profile_info_interface.h"
20
21namespace gfx {
22class Image;
23}
24
25namespace base {
26class DictionaryValue;
27}
28
29class PrefService;
30class PrefRegistrySimple;
31
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.
35class ProfileInfoCache : public ProfileInfoInterface,
36                         public base::SupportsWeakPtr<ProfileInfoCache> {
37 public:
38  ProfileInfoCache(PrefService* prefs, const base::FilePath& user_data_dir);
39  virtual ~ProfileInfoCache();
40
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 string16& name,
45                         const string16& username,
46                         size_t icon_index,
47                         bool is_managed);
48  void DeleteProfileFromCache(const base::FilePath& profile_path);
49
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 string16 GetNameOfProfileAtIndex(size_t index) const OVERRIDE;
57  virtual string16 GetShortcutNameOfProfileAtIndex(size_t index)
58      const OVERRIDE;
59  virtual base::FilePath GetPathOfProfileAtIndex(size_t index) const OVERRIDE;
60  virtual string16 GetUserNameOfProfileAtIndex(size_t index) const OVERRIDE;
61  virtual const gfx::Image& GetAvatarIconOfProfileAtIndex(
62      size_t index) const OVERRIDE;
63  // Note that a return value of false could mean an error in collection or
64  // that there are currently no background apps running. However, the action
65  // which results is the same in both cases (thus far).
66  virtual bool GetBackgroundStatusOfProfileAtIndex(
67      size_t index) const OVERRIDE;
68  virtual string16 GetGAIANameOfProfileAtIndex(size_t index) const OVERRIDE;
69  virtual bool IsUsingGAIANameOfProfileAtIndex(size_t index) const OVERRIDE;
70  // Returns the GAIA picture for the given profile. This may return NULL
71  // if the profile does not have a GAIA picture or if the picture must be
72  // loaded from disk.
73  virtual const gfx::Image* GetGAIAPictureOfProfileAtIndex(
74      size_t index) const OVERRIDE;
75  virtual bool IsUsingGAIAPictureOfProfileAtIndex(
76      size_t index) const OVERRIDE;
77  virtual bool ProfileIsManagedAtIndex(size_t index) const OVERRIDE;
78  virtual bool ProfileIsSigninRequiredAtIndex(size_t index) const OVERRIDE;
79
80  size_t GetAvatarIconIndexOfProfileAtIndex(size_t index) const;
81
82  void SetNameOfProfileAtIndex(size_t index, const string16& name);
83  void SetShortcutNameOfProfileAtIndex(size_t index, const string16& name);
84  void SetUserNameOfProfileAtIndex(size_t index, const string16& user_name);
85  void SetAvatarIconOfProfileAtIndex(size_t index, size_t icon_index);
86  void SetBackgroundStatusOfProfileAtIndex(size_t index,
87                                           bool running_background_apps);
88  void SetGAIANameOfProfileAtIndex(size_t index, const string16& name);
89  void SetIsUsingGAIANameOfProfileAtIndex(size_t index, bool value);
90  void SetGAIAPictureOfProfileAtIndex(size_t index, const gfx::Image* image);
91  void SetIsUsingGAIAPictureOfProfileAtIndex(size_t index, bool value);
92  void SetProfileSigninRequiredAtIndex(size_t index, bool value);
93
94  // Returns unique name that can be assigned to a newly created profile.
95  string16 ChooseNameForNewProfile(size_t icon_index) const;
96
97  // Checks if the given profile has switched to using GAIA information
98  // for the profile name and picture. This pref is used to switch over
99  // to GAIA info the first time it is available. Afterwards this pref is
100  // checked to prevent clobbering the user's custom settings.
101  bool GetHasMigratedToGAIAInfoOfProfileAtIndex(size_t index) const;
102
103  // Marks the given profile as having switched to using GAIA information
104  // for the profile name and picture.
105  void SetHasMigratedToGAIAInfoOfProfileAtIndex(size_t index, bool value);
106
107  // Returns an avatar icon index that can be assigned to a newly created
108  // profile. Note that the icon may not be unique since there are a limited
109  // set of default icons.
110  size_t ChooseAvatarIconIndexForNewProfile() const;
111
112  const base::FilePath& GetUserDataDir() const;
113
114  // Gets the number of default avatar icons that exist.
115  static size_t GetDefaultAvatarIconCount();
116  // Gets the resource ID of the default avatar icon at |index|.
117  static int GetDefaultAvatarIconResourceIDAtIndex(size_t index);
118  // Returns a URL for the default avatar icon with specified index.
119  static std::string GetDefaultAvatarIconUrl(size_t index);
120  // Checks if |index| is a valid avatar icon index
121  static bool IsDefaultAvatarIconIndex(size_t index);
122  // Checks if the given URL points to one of the default avatar icons. If it
123  // is, returns true and its index through |icon_index|. If not, returns false.
124  static bool IsDefaultAvatarIconUrl(const std::string& icon_url,
125                                     size_t *icon_index);
126
127  // Gets all names of profiles associated with this instance of Chrome.
128  // Because this method will be called during uninstall, before the creation
129  // of the ProfileManager, it reads directly from the local state preferences,
130  // rather than going through the ProfileInfoCache object.
131  static std::vector<string16> GetProfileNames();
132
133  // Register cache related preferences in Local State.
134  static void RegisterPrefs(PrefRegistrySimple* registry);
135
136  void AddObserver(ProfileInfoCacheObserver* obs);
137  void RemoveObserver(ProfileInfoCacheObserver* obs);
138
139 private:
140  const base::DictionaryValue* GetInfoForProfileAtIndex(size_t index) const;
141  // Saves the profile info to a cache and takes ownership of |info|.
142  // Currently the only information that is cached is the profile's name,
143  // user name, and avatar icon.
144  void SetInfoForProfileAtIndex(size_t index, base::DictionaryValue* info);
145  std::string CacheKeyFromProfilePath(const base::FilePath& profile_path) const;
146  std::vector<std::string>::iterator FindPositionForProfile(
147      const std::string& search_key,
148      const string16& search_name);
149
150  // Returns true if the given icon index is not in use by another profie.
151  bool IconIndexIsUnique(size_t icon_index) const;
152
153  // Tries to find an icon index that satisfies all the given conditions.
154  // Returns true if an icon was found, false otherwise.
155  bool ChooseAvatarIconIndexForNewProfile(bool allow_generic_icon,
156                                          bool must_be_unique,
157                                          size_t* out_icon_index) const;
158
159  // Updates the position of the profile at the given index so that the list
160  // of profiles is still sorted.
161  void UpdateSortForProfileIndex(size_t index);
162
163  void OnGAIAPictureLoaded(const base::FilePath& path,
164                           gfx::Image** image) const;
165  void OnGAIAPictureSaved(const base::FilePath& path, bool* success) const;
166
167  PrefService* prefs_;
168  std::vector<std::string> sorted_keys_;
169  base::FilePath user_data_dir_;
170
171  ObserverList<ProfileInfoCacheObserver> observer_list_;
172
173  // A cache of gaia profile pictures. This cache is updated lazily so it needs
174  // to be mutable.
175  mutable std::map<std::string, gfx::Image*> gaia_pictures_;
176  // Marks a gaia profile picture as loading. This prevents a picture from
177  // loading multiple times.
178  mutable std::map<std::string, bool> gaia_pictures_loading_;
179
180  DISALLOW_COPY_AND_ASSIGN(ProfileInfoCache);
181};
182
183#endif  // CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_
184