OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_ | 5 #ifndef CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_ |
6 #define CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_ | 6 #define CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
| 10 #include <map> |
10 #include <vector> | 11 #include <vector> |
11 | 12 |
12 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
13 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
14 #include "base/file_path.h" | 15 #include "base/file_path.h" |
15 #include "base/string16.h" | 16 #include "base/string16.h" |
16 #include "chrome/browser/profiles/profile_info_interface.h" | 17 #include "chrome/browser/profiles/profile_info_interface.h" |
17 | 18 #include "chrome/browser/profiles/profile_info_entry.h" |
18 namespace gfx { | |
19 class Image; | |
20 } | |
21 | |
22 namespace base { | |
23 class DictionaryValue; | |
24 } | |
25 | 19 |
26 class PrefService; | 20 class PrefService; |
27 | 21 |
28 | |
29 // This class saves various information about profiles to local preferences. | 22 // This class saves various information about profiles to local preferences. |
30 // This cache can be used to display a list of profiles without having to | 23 // This cache can be used to display a list of profiles without having to |
31 // actually load the profiles from disk. | 24 // actually load the profiles from disk. |
32 class ProfileInfoCache : public ProfileInfoInterface { | 25 class ProfileInfoCache : public ProfileInfoInterface { |
33 public: | 26 public: |
34 ProfileInfoCache(PrefService* prefs, const FilePath& user_data_dir); | 27 ProfileInfoCache(PrefService* prefs, const FilePath& user_data_dir); |
35 virtual ~ProfileInfoCache(); | 28 virtual ~ProfileInfoCache(); |
36 | 29 |
37 void AddProfileToCache(const FilePath& profile_path, | 30 // ProfileInfoInterface: |
38 const string16& name, | 31 virtual size_t GetNumberOfProfiles() const OVERRIDE; |
39 const string16& username, | 32 |
40 size_t icon_index); | 33 virtual std::vector<ProfileInfoEntry> GetProfilesSortedByName() |
| 34 const OVERRIDE; |
| 35 |
| 36 virtual bool GetInfoForProfile(const FilePath& path, |
| 37 ProfileInfoEntry* entry) const OVERRIDE; |
| 38 |
41 void DeleteProfileFromCache(const FilePath& profile_path); | 39 void DeleteProfileFromCache(const FilePath& profile_path); |
42 | 40 |
43 // ProfileInfoInterface: | 41 void SetInfoForProfile(const ProfileInfoEntry& info); |
44 virtual size_t GetNumberOfProfiles() const OVERRIDE; | |
45 // Don't cache this value and reuse, because resorting the menu could cause | |
46 // the item being referred to to change out from under you. | |
47 virtual size_t GetIndexOfProfileWithPath( | |
48 const FilePath& profile_path) const OVERRIDE; | |
49 virtual string16 GetNameOfProfileAtIndex(size_t index) const OVERRIDE; | |
50 virtual FilePath GetPathOfProfileAtIndex(size_t index) const OVERRIDE; | |
51 virtual string16 GetUserNameOfProfileAtIndex(size_t index) const OVERRIDE; | |
52 virtual const gfx::Image& GetAvatarIconOfProfileAtIndex( | |
53 size_t index) const OVERRIDE; | |
54 virtual bool GetBackgroundStatusOfProfileAtIndex( | |
55 size_t index) const OVERRIDE; | |
56 | |
57 size_t GetAvatarIconIndexOfProfileAtIndex(size_t index) const; | |
58 | |
59 void SetNameOfProfileAtIndex(size_t index, const string16& name); | |
60 void SetUserNameOfProfileAtIndex(size_t index, const string16& user_name); | |
61 void SetAvatarIconOfProfileAtIndex(size_t index, size_t icon_index); | |
62 void SetBackgroundStatusOfProfileAtIndex(size_t index, | |
63 bool running_background_apps); | |
64 | |
65 // Returns unique name that can be assigned to a newly created profile. | |
66 string16 ChooseNameForNewProfile(size_t icon_index); | |
67 | |
68 // Returns an avatar icon index that can be assigned to a newly created | |
69 // profile. Note that the icon may not be unique since there are a limited | |
70 // set of default icons. | |
71 size_t ChooseAvatarIconIndexForNewProfile() const; | |
72 | 42 |
73 const FilePath& GetUserDataDir() const; | 43 const FilePath& GetUserDataDir() const; |
74 | 44 |
75 // Gets the number of default avatar icons that exist. | |
76 static size_t GetDefaultAvatarIconCount(); | |
77 // Gets the resource ID of the default avatar icon at |index|. | |
78 static int GetDefaultAvatarIconResourceIDAtIndex(size_t index); | |
79 // Returns a URL for the default avatar icon with specified index. | |
80 static std::string GetDefaultAvatarIconUrl(size_t index); | |
81 // Checks if the given URL points to one of the default avatar icons. If it | |
82 // is, returns true and its index through |icon_index|. If not, returns false. | |
83 static bool IsDefaultAvatarIconUrl(const std::string& icon_url, | |
84 size_t *icon_index); | |
85 | |
86 // Register cache related preferences in Local State. | 45 // Register cache related preferences in Local State. |
87 static void RegisterPrefs(PrefService* prefs); | 46 static void RegisterPrefs(PrefService* prefs); |
88 | 47 |
89 private: | 48 private: |
90 const base::DictionaryValue* GetInfoForProfileAtIndex(size_t index) const; | |
91 // Saves the profile info to a cache and takes ownership of |info|. | |
92 // Currently the only information that is cached is the profiles name, | |
93 // user name, and avatar icon. | |
94 void SetInfoForProfileAtIndex(size_t index, base::DictionaryValue* info); | |
95 std::string CacheKeyFromProfilePath(const FilePath& profile_path) const; | 49 std::string CacheKeyFromProfilePath(const FilePath& profile_path) const; |
96 std::vector<std::string>::iterator FindPositionForProfile( | |
97 std::string search_key, | |
98 const string16& search_name); | |
99 | |
100 // Returns true if the given icon index is not in use by another profie. | |
101 bool IconIndexIsUnique(size_t icon_index) const; | |
102 | |
103 // Tries to find an icon index that satisfies all the given conditions. | |
104 // Returns true if an icon was found, false otherwise. | |
105 bool ChooseAvatarIconIndexForNewProfile(bool allow_generic_icon, | |
106 bool must_be_unique, | |
107 size_t* out_icon_index) const; | |
108 | 50 |
109 PrefService* prefs_; | 51 PrefService* prefs_; |
110 std::vector<std::string> sorted_keys_; | |
111 FilePath user_data_dir_; | 52 FilePath user_data_dir_; |
| 53 std::map<std::string, ProfileInfoEntry> cached_entries_; |
112 | 54 |
113 DISALLOW_COPY_AND_ASSIGN(ProfileInfoCache); | 55 DISALLOW_COPY_AND_ASSIGN(ProfileInfoCache); |
114 }; | 56 }; |
115 | 57 |
116 #endif // CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_ | 58 #endif // CHROME_BROWSER_PROFILES_PROFILE_INFO_CACHE_H_ |
OLD | NEW |