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> |
lwchkg | 99addc85 | 2016-02-10 16:14:55 | [diff] [blame] | 13 | #include <vector> |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 14 | |
Hans Wennborg | f6ad69c | 2020-06-18 18:02:32 | [diff] [blame] | 15 | #include "base/callback_forward.h" |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 16 | #include "base/files/file_path.h" |
| 17 | #include "base/gtest_prod_util.h" |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame] | 18 | #include "base/memory/weak_ptr.h" |
WC Leung | c001c31e | 2017-08-21 16:41:16 | [diff] [blame] | 19 | #include "base/observer_list.h" |
lwchkg | 99addc85 | 2016-02-10 16:14:55 | [diff] [blame] | 20 | #include "base/strings/string16.h" |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 21 | #include "chrome/browser/profiles/profile_attributes_entry.h" |
lwchkg | 99addc85 | 2016-02-10 16:14:55 | [diff] [blame] | 22 | #include "chrome/browser/profiles/profile_info_cache_observer.h" |
| 23 | |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame] | 24 | namespace base { |
| 25 | class SequencedTaskRunner; |
| 26 | } |
| 27 | |
| 28 | namespace gfx { |
| 29 | class Image; |
| 30 | } |
| 31 | |
Alexander Alekseev | 4084f50 | 2018-02-20 22:16:46 | [diff] [blame] | 32 | class AccountId; |
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 | |
Lei Zhang | 09d7ef5 | 2018-07-03 18:38:37 | [diff] [blame] | 42 | explicit ProfileAttributesStorage(PrefService* prefs); |
David Bienvenu | 1e9f6a7 | 2020-08-20 01:05:48 | [diff] [blame] | 43 | ProfileAttributesStorage(const ProfileAttributesStorage&) = delete; |
| 44 | ProfileAttributesStorage& operator=(const ProfileAttributesStorage&) = delete; |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 45 | virtual ~ProfileAttributesStorage(); |
anthonyvd | b50a7cb | 2015-07-13 15:42:03 | [diff] [blame] | 46 | |
| 47 | // If the |supervised_user_id| is non-empty, the profile will be marked to be |
| 48 | // omitted from the avatar-menu list on desktop versions. This is used while a |
| 49 | // supervised user is in the process of being registered with the server. Use |
| 50 | // ProfileAttributesEntry::SetIsOmitted() to clear the flag when the profile |
| 51 | // is ready to be shown in the menu. |
| 52 | virtual void AddProfile(const base::FilePath& profile_path, |
| 53 | const base::string16& name, |
| 54 | const std::string& gaia_id, |
| 55 | const base::string16& user_name, |
Monica Basta | 9ca4704 | 2019-09-16 17:36:51 | [diff] [blame] | 56 | bool is_consented_primary_account, |
anthonyvd | b50a7cb | 2015-07-13 15:42:03 | [diff] [blame] | 57 | size_t icon_index, |
Roman Sorokin | 8ead4d7 | 2018-04-16 15:58:58 | [diff] [blame] | 58 | const std::string& supervised_user_id, |
| 59 | const AccountId& account_id) = 0; |
Alexander Alekseev | 4084f50 | 2018-02-20 22:16:46 | [diff] [blame] | 60 | |
| 61 | // Removes the profile matching given |account_id| from this storage. |
| 62 | // Calculates profile path and calls RemoveProfile() on it. |
| 63 | virtual void RemoveProfileByAccountId(const AccountId& account_id) = 0; |
| 64 | |
anthonyvd | b50a7cb | 2015-07-13 15:42:03 | [diff] [blame] | 65 | // Removes the profile at |profile_path| from this storage. Does not delete or |
| 66 | // affect the actual profile's data. |
| 67 | virtual void RemoveProfile(const base::FilePath& profile_path) = 0; |
| 68 | |
| 69 | // Returns a vector containing one attributes entry per known profile. They |
| 70 | // are not sorted in any particular order. |
WC Leung | f416f8e8 | 2017-08-19 14:50:08 | [diff] [blame] | 71 | std::vector<ProfileAttributesEntry*> GetAllProfilesAttributes(); |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 72 | std::vector<ProfileAttributesEntry*> GetAllProfilesAttributesSortedByName(); |
anthonyvd | b50a7cb | 2015-07-13 15:42:03 | [diff] [blame] | 73 | |
| 74 | // Populates |entry| with the data for the profile at |path| and returns true |
| 75 | // if the operation is successful and |entry| can be used. Returns false |
| 76 | // otherwise. |
| 77 | // |entry| should not be cached as it may not reflect subsequent changes to |
| 78 | // the profile's metadata. |
| 79 | virtual bool GetProfileAttributesWithPath( |
| 80 | const base::FilePath& path, ProfileAttributesEntry** entry) = 0; |
| 81 | |
| 82 | // Returns the count of known profiles. |
| 83 | virtual size_t GetNumberOfProfiles() const = 0; |
| 84 | |
lwchkg | c4d18c8 | 2016-02-20 06:12:25 | [diff] [blame] | 85 | // Returns a unique name that can be assigned to a newly created profile. |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 86 | base::string16 ChooseNameForNewProfile(size_t icon_index) const; |
lwchkg | c4d18c8 | 2016-02-20 06:12:25 | [diff] [blame] | 87 | |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 88 | // Determines whether |name| is one of the default assigned names. |
Monica Basta | 6655884a | 2019-10-29 10:38:39 | [diff] [blame] | 89 | // On Desktop, if |include_check_for_legacy_profile_name| is false, |
| 90 | // |IsDefaultProfileName()| would only return true if the |name| is in the |
| 91 | // form of |Person %n| which is the new default local profile name. If |
| 92 | // |include_check_for_legacy_profile_name| is true, we will also check if name |
| 93 | // is one of the legacy profile names (e.g. Saratoga, Default user, ..). |
| 94 | // For other platforms, so far |include_check_for_legacy_profile_name| |
| 95 | // is not used. |
| 96 | bool IsDefaultProfileName(const base::string16& name, |
| 97 | bool include_check_for_legacy_profile_name) const; |
lwchkg | 498e9249 | 2016-04-23 11:04:12 | [diff] [blame] | 98 | |
David Roger | e582c445 | 2020-03-18 20:45:38 | [diff] [blame] | 99 | // Records statistics about profiles as would be visible in the profile picker |
| 100 | // (if we would display it in this moment). |
| 101 | void RecordProfilesState(); |
| 102 | |
lwchkg | c4d18c8 | 2016-02-20 06:12:25 | [diff] [blame] | 103 | // Returns an avatar icon index that can be assigned to a newly created |
| 104 | // profile. Note that the icon may not be unique since there are a limited |
| 105 | // set of default icons. |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 106 | size_t ChooseAvatarIconIndexForNewProfile() const; |
lwchkg | c4d18c8 | 2016-02-20 06:12:25 | [diff] [blame] | 107 | |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame] | 108 | // Returns the decoded image at |image_path|. Used both by the GAIA profile |
| 109 | // image and the high res avatars. |
| 110 | const gfx::Image* LoadAvatarPictureFromPath( |
| 111 | const base::FilePath& profile_path, |
| 112 | const std::string& key, |
| 113 | const base::FilePath& image_path) const; |
| 114 | |
WC Leung | 484920e | 2017-10-03 00:24:18 | [diff] [blame] | 115 | // Checks whether the high res avatar at index |icon_index| exists, and if it |
| 116 | // does not, calls |DownloadHighResAvatar|. |
| 117 | void DownloadHighResAvatarIfNeeded(size_t icon_index, |
| 118 | const base::FilePath& profile_path); |
| 119 | |
WC Leung | c001c31e | 2017-08-21 16:41:16 | [diff] [blame] | 120 | void AddObserver(Observer* observer); |
| 121 | void RemoveObserver(Observer* observer); |
lwchkg | 99addc85 | 2016-02-10 16:14:55 | [diff] [blame] | 122 | |
WC Leung | 484920e | 2017-10-03 00:24:18 | [diff] [blame] | 123 | bool GetDisableAvatarDownloadForTesting() { |
| 124 | return disable_avatar_download_for_testing_; |
| 125 | } |
| 126 | |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame] | 127 | void set_disable_avatar_download_for_testing( |
| 128 | bool disable_avatar_download_for_testing) { |
| 129 | disable_avatar_download_for_testing_ = disable_avatar_download_for_testing; |
| 130 | } |
| 131 | |
WC Leung | 484920e | 2017-10-03 00:24:18 | [diff] [blame] | 132 | // Notifies observers. The following methods are accessed by |
| 133 | // ProfileAttributesEntry. |
| 134 | void NotifyOnProfileAvatarChanged(const base::FilePath& profile_path) const; |
| 135 | |
David Roger | 14c3f87 | 2020-03-12 11:57:11 | [diff] [blame] | 136 | // Disables the periodic reporting of profile metrics, as this is causing |
| 137 | // tests to time out. |
| 138 | virtual void DisableProfileMetricsForTesting() {} |
| 139 | |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 140 | protected: |
| 141 | FRIEND_TEST_ALL_PREFIXES(ProfileInfoCacheTest, EntriesInAttributesStorage); |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame] | 142 | FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest, |
| 143 | DownloadHighResAvatarTest); |
| 144 | FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest, |
| 145 | NothingToDownloadHighResAvatarTest); |
| 146 | |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame] | 147 | // Starts downloading the high res avatar at index |icon_index| for profile |
| 148 | // with path |profile_path|. |
| 149 | void DownloadHighResAvatar(size_t icon_index, |
| 150 | const base::FilePath& profile_path); |
| 151 | |
| 152 | // Saves the avatar |image| at |image_path|. This is used both for the GAIA |
| 153 | // profile pictures and the ProfileAvatarDownloader that is used to download |
| 154 | // the high res avatars. |
| 155 | void SaveAvatarImageAtPath(const base::FilePath& profile_path, |
Dana Fried | fa14d5f | 2019-04-21 20:49:36 | [diff] [blame] | 156 | gfx::Image image, |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame] | 157 | const std::string& key, |
Monica Basta | ff6a3597 | 2020-02-14 19:16:04 | [diff] [blame] | 158 | const base::FilePath& image_path, |
| 159 | base::OnceClosure callback); |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 160 | |
Lei Zhang | 09d7ef5 | 2018-07-03 18:38:37 | [diff] [blame] | 161 | PrefService* const prefs_; |
lwchkg | 778965f | 2016-05-12 13:14:22 | [diff] [blame] | 162 | mutable std::unordered_map<base::FilePath::StringType, |
| 163 | std::unique_ptr<ProfileAttributesEntry>> |
| 164 | profile_attributes_entries_; |
| 165 | |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 166 | mutable base::ObserverList<Observer>::Unchecked observer_list_; |
WC Leung | c001c31e | 2017-08-21 16:41:16 | [diff] [blame] | 167 | |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame] | 168 | // A cache of gaia/high res avatar profile pictures. This cache is updated |
| 169 | // lazily so it needs to be mutable. |
Dana Fried | fa14d5f | 2019-04-21 20:49:36 | [diff] [blame] | 170 | mutable std::unordered_map<std::string, gfx::Image> cached_avatar_images_; |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame] | 171 | |
| 172 | // Marks a profile picture as loading from disk. This prevents a picture from |
| 173 | // loading multiple times. |
| 174 | mutable std::unordered_map<std::string, bool> cached_avatar_images_loading_; |
| 175 | |
| 176 | // Hash table of profile pictures currently being downloaded from the remote |
| 177 | // location and the ProfileAvatarDownloader instances downloading them. |
| 178 | // This prevents a picture from being downloaded multiple times. The |
| 179 | // ProfileAvatarDownloader instances are deleted when the download completes |
| 180 | // or when the ProfileInfoCache is destroyed. |
| 181 | std::unordered_map<std::string, std::unique_ptr<ProfileAvatarDownloader>> |
| 182 | avatar_images_downloads_in_progress_; |
| 183 | |
| 184 | // Determines of the ProfileAvatarDownloader should be created and executed |
| 185 | // or not. Only set to true for tests. |
Lei Zhang | 09d7ef5 | 2018-07-03 18:38:37 | [diff] [blame] | 186 | bool disable_avatar_download_for_testing_ = false; |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame] | 187 | |
| 188 | // Task runner used for file operation on avatar images. |
| 189 | scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
| 190 | |
lwchkg | 99addc85 | 2016-02-10 16:14:55 | [diff] [blame] | 191 | private: |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame] | 192 | // Called when the picture given by |key| has been loaded from disk and |
| 193 | // decoded into |image|. |
| 194 | void OnAvatarPictureLoaded(const base::FilePath& profile_path, |
| 195 | const std::string& key, |
Marc Treib | 866ca83 | 2019-05-15 10:11:12 | [diff] [blame] | 196 | gfx::Image image) const; |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame] | 197 | |
| 198 | // Called when the picture given by |file_name| has been saved to disk. Used |
| 199 | // both for the GAIA profile picture and the high res avatar files. |
| 200 | void OnAvatarPictureSaved(const std::string& file_name, |
Monica Basta | ff6a3597 | 2020-02-14 19:16:04 | [diff] [blame] | 201 | const base::FilePath& profile_path, |
| 202 | base::OnceClosure callback, |
| 203 | bool success) const; |
WC Leung | 31f7273 | 2017-10-01 04:59:32 | [diff] [blame] | 204 | |
David Roger | 4efdc01 | 2020-03-10 12:47:18 | [diff] [blame] | 205 | // Helper function that calls SaveAvatarImageAtPath without a callback. |
| 206 | void SaveAvatarImageAtPathNoCallback(const base::FilePath& profile_path, |
| 207 | gfx::Image image, |
| 208 | const std::string& key, |
| 209 | const base::FilePath& image_path); |
| 210 | |
WC Leung | 484920e | 2017-10-03 00:24:18 | [diff] [blame] | 211 | // Notifies observers. |
| 212 | void NotifyOnProfileHighResAvatarLoaded( |
| 213 | const base::FilePath& profile_path) const; |
anthonyvd | b50a7cb | 2015-07-13 15:42:03 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | #endif // CHROME_BROWSER_PROFILES_PROFILE_ATTRIBUTES_STORAGE_H_ |