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