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 string16& name,
     45                          const 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 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   virtual std::string GetManagedUserIdOfProfileAtIndex(size_t index) const
     80       OVERRIDE;
     81 
     82   size_t GetAvatarIconIndexOfProfileAtIndex(size_t index) const;
     83 
     84   void SetNameOfProfileAtIndex(size_t index, const string16& name);
     85   void SetShortcutNameOfProfileAtIndex(size_t index, const string16& name);
     86   void SetUserNameOfProfileAtIndex(size_t index, const string16& user_name);
     87   void SetAvatarIconOfProfileAtIndex(size_t index, size_t icon_index);
     88   void SetManagedUserIdOfProfileAtIndex(size_t index, const std::string& id);
     89   void SetBackgroundStatusOfProfileAtIndex(size_t index,
     90                                            bool running_background_apps);
     91   void SetGAIANameOfProfileAtIndex(size_t index, const string16& name);
     92   void SetIsUsingGAIANameOfProfileAtIndex(size_t index, bool value);
     93   void SetGAIAPictureOfProfileAtIndex(size_t index, const gfx::Image* image);
     94   void SetIsUsingGAIAPictureOfProfileAtIndex(size_t index, bool value);
     95   void SetProfileSigninRequiredAtIndex(size_t index, bool value);
     96 
     97   // Returns unique name that can be assigned to a newly created profile.
     98   string16 ChooseNameForNewProfile(size_t icon_index) const;
     99 
    100   // Checks if the given profile has switched to using GAIA information
    101   // for the profile name and picture. This pref is used to switch over
    102   // to GAIA info the first time it is available. Afterwards this pref is
    103   // checked to prevent clobbering the user's custom settings.
    104   bool GetHasMigratedToGAIAInfoOfProfileAtIndex(size_t index) const;
    105 
    106   // Marks the given profile as having switched to using GAIA information
    107   // for the profile name and picture.
    108   void SetHasMigratedToGAIAInfoOfProfileAtIndex(size_t index, bool value);
    109 
    110   // Returns an avatar icon index that can be assigned to a newly created
    111   // profile. Note that the icon may not be unique since there are a limited
    112   // set of default icons.
    113   size_t ChooseAvatarIconIndexForNewProfile() const;
    114 
    115   const base::FilePath& GetUserDataDir() const;
    116 
    117   // Gets the number of default avatar icons that exist.
    118   static size_t GetDefaultAvatarIconCount();
    119   // Gets the resource ID of the default avatar icon at |index|.
    120   static int GetDefaultAvatarIconResourceIDAtIndex(size_t index);
    121   // Returns a URL for the default avatar icon with specified index.
    122   static std::string GetDefaultAvatarIconUrl(size_t index);
    123   // Checks if |index| is a valid avatar icon index
    124   static bool IsDefaultAvatarIconIndex(size_t index);
    125   // Checks if the given URL points to one of the default avatar icons. If it
    126   // is, returns true and its index through |icon_index|. If not, returns false.
    127   static bool IsDefaultAvatarIconUrl(const std::string& icon_url,
    128                                      size_t *icon_index);
    129 
    130   // Gets all names of profiles associated with this instance of Chrome.
    131   // Because this method will be called during uninstall, before the creation
    132   // of the ProfileManager, it reads directly from the local state preferences,
    133   // rather than going through the ProfileInfoCache object.
    134   static std::vector<string16> GetProfileNames();
    135 
    136   // Register cache related preferences in Local State.
    137   static void RegisterPrefs(PrefRegistrySimple* registry);
    138 
    139   void AddObserver(ProfileInfoCacheObserver* obs);
    140   void RemoveObserver(ProfileInfoCacheObserver* obs);
    141 
    142  private:
    143   const base::DictionaryValue* GetInfoForProfileAtIndex(size_t index) const;
    144   // Saves the profile info to a cache and takes ownership of |info|.
    145   // Currently the only information that is cached is the profile's name,
    146   // user name, and avatar icon.
    147   void SetInfoForProfileAtIndex(size_t index, base::DictionaryValue* info);
    148   std::string CacheKeyFromProfilePath(const base::FilePath& profile_path) const;
    149   std::vector<std::string>::iterator FindPositionForProfile(
    150       const std::string& search_key,
    151       const string16& search_name);
    152 
    153   // Returns true if the given icon index is not in use by another profie.
    154   bool IconIndexIsUnique(size_t icon_index) const;
    155 
    156   // Tries to find an icon index that satisfies all the given conditions.
    157   // Returns true if an icon was found, false otherwise.
    158   bool ChooseAvatarIconIndexForNewProfile(bool allow_generic_icon,
    159                                           bool must_be_unique,
    160                                           size_t* out_icon_index) const;
    161 
    162   // Updates the position of the profile at the given index so that the list
    163   // of profiles is still sorted.
    164   void UpdateSortForProfileIndex(size_t index);
    165 
    166   void OnGAIAPictureLoaded(const base::FilePath& path,
    167                            gfx::Image** image) const;
    168   void OnGAIAPictureSaved(const base::FilePath& path, bool* success) const;
    169 
    170   PrefService* prefs_;
    171   std::vector<std::string> sorted_keys_;
    172   base::FilePath user_data_dir_;
    173 
    174   ObserverList<ProfileInfoCacheObserver> observer_list_;
    175 
    176   // A cache of gaia profile pictures. This cache is updated lazily so it needs
    177   // to be mutable.
    178   mutable std::map<std::string, gfx::Image*> gaia_pictures_;
    179   // Marks a gaia profile picture as loading. This prevents a picture from
    180   // loading multiple times.
    181   mutable std::map<std::string, bool> gaia_pictures_loading_;
    182 
    183   DISALLOW_COPY_AND_ASSIGN(ProfileInfoCache);
    184 };
    185 
    186 #endif  // CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_
    187