anthonyvd | b50a7cb | 2015-07-13 15:42:03 | [diff] [blame] | 1 | // Copyright 2015 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_ATTRIBUTES_STORAGE_H_ |
| 6 | #define CHROME_BROWSER_PROFILES_PROFILE_ATTRIBUTES_STORAGE_H_ |
| 7 | |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 10 | #include <memory> |
lwchkg | 99addc85 | 2016-02-10 16:14:55 | [diff] [blame] | 11 | #include <string> |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 12 | #include <unordered_map> |
| 13 | #include <unordered_set> |
lwchkg | 99addc85 | 2016-02-10 16:14:55 | [diff] [blame] | 14 | #include <vector> |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 15 | |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 16 | #include "base/files/file_path.h" |
| 17 | #include "base/gtest_prod_util.h" |
lwchkg | 99addc85 | 2016-02-10 16:14:55 | [diff] [blame] | 18 | #include "base/macros.h" |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame^] | 19 | #include "base/memory/weak_ptr.h" |
WC Leung | c001c31e | 2017-08-21 16:41:16 | [diff] [blame] | 20 | #include "base/observer_list.h" |
lwchkg | 99addc85 | 2016-02-10 16:14:55 | [diff] [blame] | 21 | #include "base/strings/string16.h" |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 22 | #include "chrome/browser/profiles/profile_attributes_entry.h" |
lwchkg | 99addc85 | 2016-02-10 16:14:55 | [diff] [blame] | 23 | #include "chrome/browser/profiles/profile_info_cache_observer.h" |
| 24 | |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame^] | 25 | namespace base { |
| 26 | class SequencedTaskRunner; |
| 27 | } |
| 28 | |
| 29 | namespace gfx { |
| 30 | class Image; |
| 31 | } |
| 32 | |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 33 | class PrefService; |
anthonyvd | b50a7cb | 2015-07-13 15:42:03 | [diff] [blame] | 34 | class ProfileAttributesEntry; |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame^] | 35 | class ProfileAvatarDownloader; |
anthonyvd | b50a7cb | 2015-07-13 15:42:03 | [diff] [blame] | 36 | |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame^] | 37 | class ProfileAttributesStorage |
| 38 | : public base::SupportsWeakPtr<ProfileAttributesStorage> { |
anthonyvd | b50a7cb | 2015-07-13 15:42:03 | [diff] [blame] | 39 | public: |
lwchkg | 99addc85 | 2016-02-10 16:14:55 | [diff] [blame] | 40 | using Observer = ProfileInfoCacheObserver; |
| 41 | |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 42 | ProfileAttributesStorage(PrefService* prefs, |
| 43 | const base::FilePath& user_data_dir); |
| 44 | virtual ~ProfileAttributesStorage(); |
anthonyvd | b50a7cb | 2015-07-13 15:42:03 | [diff] [blame] | 45 | |
| 46 | // If the |supervised_user_id| is non-empty, the profile will be marked to be |
| 47 | // omitted from the avatar-menu list on desktop versions. This is used while a |
| 48 | // supervised user is in the process of being registered with the server. Use |
| 49 | // ProfileAttributesEntry::SetIsOmitted() to clear the flag when the profile |
| 50 | // is ready to be shown in the menu. |
| 51 | virtual void AddProfile(const base::FilePath& profile_path, |
| 52 | const base::string16& name, |
| 53 | const std::string& gaia_id, |
| 54 | const base::string16& user_name, |
| 55 | size_t icon_index, |
| 56 | const std::string& supervised_user_id) = 0; |
| 57 | // Removes the profile at |profile_path| from this storage. Does not delete or |
| 58 | // affect the actual profile's data. |
| 59 | virtual void RemoveProfile(const base::FilePath& profile_path) = 0; |
| 60 | |
| 61 | // Returns a vector containing one attributes entry per known profile. They |
| 62 | // are not sorted in any particular order. |
WC Leung | f416f8e8 | 2017-08-19 14:50:08 | [diff] [blame] | 63 | std::vector<ProfileAttributesEntry*> GetAllProfilesAttributes(); |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 64 | std::vector<ProfileAttributesEntry*> GetAllProfilesAttributesSortedByName(); |
anthonyvd | b50a7cb | 2015-07-13 15:42:03 | [diff] [blame] | 65 | |
| 66 | // Populates |entry| with the data for the profile at |path| and returns true |
| 67 | // if the operation is successful and |entry| can be used. Returns false |
| 68 | // otherwise. |
| 69 | // |entry| should not be cached as it may not reflect subsequent changes to |
| 70 | // the profile's metadata. |
| 71 | virtual bool GetProfileAttributesWithPath( |
| 72 | const base::FilePath& path, ProfileAttributesEntry** entry) = 0; |
| 73 | |
| 74 | // Returns the count of known profiles. |
| 75 | virtual size_t GetNumberOfProfiles() const = 0; |
| 76 | |
lwchkg | c4d18c8 | 2016-02-20 06:12:25 | [diff] [blame] | 77 | // Returns a unique name that can be assigned to a newly created profile. |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 78 | base::string16 ChooseNameForNewProfile(size_t icon_index) const; |
lwchkg | c4d18c8 | 2016-02-20 06:12:25 | [diff] [blame] | 79 | |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 80 | // Determines whether |name| is one of the default assigned names. |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 81 | bool IsDefaultProfileName(const base::string16& name) const; |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 82 | |
lwchkg | c4d18c8 | 2016-02-20 06:12:25 | [diff] [blame] | 83 | // Returns an avatar icon index that can be assigned to a newly created |
| 84 | // profile. Note that the icon may not be unique since there are a limited |
| 85 | // set of default icons. |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 86 | size_t ChooseAvatarIconIndexForNewProfile() const; |
lwchkg | c4d18c8 | 2016-02-20 06:12:25 | [diff] [blame] | 87 | |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame^] | 88 | // Returns the decoded image at |image_path|. Used both by the GAIA profile |
| 89 | // image and the high res avatars. |
| 90 | const gfx::Image* LoadAvatarPictureFromPath( |
| 91 | const base::FilePath& profile_path, |
| 92 | const std::string& key, |
| 93 | const base::FilePath& image_path) const; |
| 94 | |
WC Leung | c001c31e | 2017-08-21 16:41:16 | [diff] [blame] | 95 | void AddObserver(Observer* observer); |
| 96 | void RemoveObserver(Observer* observer); |
lwchkg | 99addc85 | 2016-02-10 16:14:55 | [diff] [blame] | 97 | |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame^] | 98 | void set_disable_avatar_download_for_testing( |
| 99 | bool disable_avatar_download_for_testing) { |
| 100 | disable_avatar_download_for_testing_ = disable_avatar_download_for_testing; |
| 101 | } |
| 102 | |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 103 | protected: |
| 104 | FRIEND_TEST_ALL_PREFIXES(ProfileInfoCacheTest, EntriesInAttributesStorage); |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame^] | 105 | FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest, |
| 106 | DownloadHighResAvatarTest); |
| 107 | FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest, |
| 108 | NothingToDownloadHighResAvatarTest); |
| 109 | |
| 110 | // Checks whether the high res avatar at index |icon_index| exists, and if it |
| 111 | // does not, calls |DownloadHighResAvatar|. |
| 112 | void DownloadHighResAvatarIfNeeded(size_t icon_index, |
| 113 | const base::FilePath& profile_path); |
| 114 | |
| 115 | // Starts downloading the high res avatar at index |icon_index| for profile |
| 116 | // with path |profile_path|. |
| 117 | void DownloadHighResAvatar(size_t icon_index, |
| 118 | const base::FilePath& profile_path); |
| 119 | |
| 120 | // Saves the avatar |image| at |image_path|. This is used both for the GAIA |
| 121 | // profile pictures and the ProfileAvatarDownloader that is used to download |
| 122 | // the high res avatars. |
| 123 | void SaveAvatarImageAtPath(const base::FilePath& profile_path, |
| 124 | const gfx::Image* image, |
| 125 | const std::string& key, |
| 126 | const base::FilePath& image_path); |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 127 | |
| 128 | PrefService* prefs_; |
| 129 | base::FilePath user_data_dir_; |
| 130 | mutable std::unordered_map<base::FilePath::StringType, |
| 131 | std::unique_ptr<ProfileAttributesEntry>> |
| 132 | profile_attributes_entries_; |
| 133 | |
WC Leung | c001c31e | 2017-08-21 16:41:16 | [diff] [blame] | 134 | mutable base::ObserverList<Observer> observer_list_; |
| 135 | |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame^] | 136 | // A cache of gaia/high res avatar profile pictures. This cache is updated |
| 137 | // lazily so it needs to be mutable. |
| 138 | mutable std::unordered_map<std::string, std::unique_ptr<gfx::Image>> |
| 139 | cached_avatar_images_; |
| 140 | |
| 141 | // Marks a profile picture as loading from disk. This prevents a picture from |
| 142 | // loading multiple times. |
| 143 | mutable std::unordered_map<std::string, bool> cached_avatar_images_loading_; |
| 144 | |
| 145 | // Hash table of profile pictures currently being downloaded from the remote |
| 146 | // location and the ProfileAvatarDownloader instances downloading them. |
| 147 | // This prevents a picture from being downloaded multiple times. The |
| 148 | // ProfileAvatarDownloader instances are deleted when the download completes |
| 149 | // or when the ProfileInfoCache is destroyed. |
| 150 | std::unordered_map<std::string, std::unique_ptr<ProfileAvatarDownloader>> |
| 151 | avatar_images_downloads_in_progress_; |
| 152 | |
| 153 | // Determines of the ProfileAvatarDownloader should be created and executed |
| 154 | // or not. Only set to true for tests. |
| 155 | bool disable_avatar_download_for_testing_; |
| 156 | |
| 157 | // Task runner used for file operation on avatar images. |
| 158 | scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
| 159 | |
lwchkg | 99addc85 | 2016-02-10 16:14:55 | [diff] [blame] | 160 | private: |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame^] | 161 | // Called when the picture given by |key| has been loaded from disk and |
| 162 | // decoded into |image|. |
| 163 | void OnAvatarPictureLoaded(const base::FilePath& profile_path, |
| 164 | const std::string& key, |
| 165 | gfx::Image** image) const; |
| 166 | |
| 167 | // Called when the picture given by |file_name| has been saved to disk. Used |
| 168 | // both for the GAIA profile picture and the high res avatar files. |
| 169 | void OnAvatarPictureSaved(const std::string& file_name, |
| 170 | const base::FilePath& profile_path) const; |
| 171 | |
| 172 | // Calls observers. |
| 173 | void CallOnProfileHighResAvatarLoaded(base::FilePath profile_path) const; |
| 174 | |
anthonyvd | b50a7cb | 2015-07-13 15:42:03 | [diff] [blame] | 175 | DISALLOW_COPY_AND_ASSIGN(ProfileAttributesStorage); |
| 176 | }; |
| 177 | |
| 178 | #endif // CHROME_BROWSER_PROFILES_PROFILE_ATTRIBUTES_STORAGE_H_ |