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