blob: 63c42928e88121cbdbde6238e6a238ea44c89cc8 [file] [log] [blame]
anthonyvdb50a7cb2015-07-13 15:42:031// 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
avib896c712015-12-26 02:10:438#include <stddef.h>
9
lwchkg778965f2016-05-12 13:14:2210#include <memory>
lwchkg99addc852016-02-10 16:14:5511#include <string>
lwchkg778965f2016-05-12 13:14:2212#include <unordered_map>
lwchkg99addc852016-02-10 16:14:5513#include <vector>
avib896c712015-12-26 02:10:4314
Hans Wennborgf6ad69c2020-06-18 18:02:3215#include "base/callback_forward.h"
lwchkg778965f2016-05-12 13:14:2216#include "base/files/file_path.h"
17#include "base/gtest_prod_util.h"
WC Leung31f72732017-10-01 04:59:3218#include "base/memory/weak_ptr.h"
WC Leungc001c31e2017-08-21 16:41:1619#include "base/observer_list.h"
lwchkg99addc852016-02-10 16:14:5520#include "base/strings/string16.h"
lwchkg778965f2016-05-12 13:14:2221#include "chrome/browser/profiles/profile_attributes_entry.h"
lwchkg99addc852016-02-10 16:14:5522#include "chrome/browser/profiles/profile_info_cache_observer.h"
23
WC Leung31f72732017-10-01 04:59:3224namespace base {
25class SequencedTaskRunner;
26}
27
28namespace gfx {
29class Image;
30}
31
Alexander Alekseev4084f502018-02-20 22:16:4632class AccountId;
lwchkg778965f2016-05-12 13:14:2233class PrefService;
anthonyvdb50a7cb2015-07-13 15:42:0334class ProfileAttributesEntry;
WC Leung31f72732017-10-01 04:59:3235class ProfileAvatarDownloader;
anthonyvdb50a7cb2015-07-13 15:42:0336
WC Leung31f72732017-10-01 04:59:3237class ProfileAttributesStorage
38 : public base::SupportsWeakPtr<ProfileAttributesStorage> {
anthonyvdb50a7cb2015-07-13 15:42:0339 public:
lwchkg99addc852016-02-10 16:14:5540 using Observer = ProfileInfoCacheObserver;
41
Lei Zhang09d7ef52018-07-03 18:38:3742 explicit ProfileAttributesStorage(PrefService* prefs);
David Bienvenu1e9f6a72020-08-20 01:05:4843 ProfileAttributesStorage(const ProfileAttributesStorage&) = delete;
44 ProfileAttributesStorage& operator=(const ProfileAttributesStorage&) = delete;
lwchkg778965f2016-05-12 13:14:2245 virtual ~ProfileAttributesStorage();
anthonyvdb50a7cb2015-07-13 15:42:0346
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 Basta9ca47042019-09-16 17:36:5156 bool is_consented_primary_account,
anthonyvdb50a7cb2015-07-13 15:42:0357 size_t icon_index,
Roman Sorokin8ead4d72018-04-16 15:58:5858 const std::string& supervised_user_id,
59 const AccountId& account_id) = 0;
Alexander Alekseev4084f502018-02-20 22:16:4660
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
anthonyvdb50a7cb2015-07-13 15:42:0365 // 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 Leungf416f8e82017-08-19 14:50:0871 std::vector<ProfileAttributesEntry*> GetAllProfilesAttributes();
lwchkg778965f2016-05-12 13:14:2272 std::vector<ProfileAttributesEntry*> GetAllProfilesAttributesSortedByName();
anthonyvdb50a7cb2015-07-13 15:42:0373
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
lwchkgc4d18c82016-02-20 06:12:2585 // Returns a unique name that can be assigned to a newly created profile.
lwchkg778965f2016-05-12 13:14:2286 base::string16 ChooseNameForNewProfile(size_t icon_index) const;
lwchkgc4d18c82016-02-20 06:12:2587
lwchkg498e92492016-04-23 11:04:1288 // Determines whether |name| is one of the default assigned names.
Monica Basta6655884a2019-10-29 10:38:3989 // 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;
lwchkg498e92492016-04-23 11:04:1298
David Rogere582c4452020-03-18 20:45:3899 // 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
lwchkgc4d18c82016-02-20 06:12:25103 // 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.
lwchkg778965f2016-05-12 13:14:22106 size_t ChooseAvatarIconIndexForNewProfile() const;
lwchkgc4d18c82016-02-20 06:12:25107
WC Leung31f72732017-10-01 04:59:32108 // 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 Leung484920e2017-10-03 00:24:18115 // 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 Leungc001c31e2017-08-21 16:41:16120 void AddObserver(Observer* observer);
121 void RemoveObserver(Observer* observer);
lwchkg99addc852016-02-10 16:14:55122
WC Leung484920e2017-10-03 00:24:18123 bool GetDisableAvatarDownloadForTesting() {
124 return disable_avatar_download_for_testing_;
125 }
126
WC Leung31f72732017-10-01 04:59:32127 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 Leung484920e2017-10-03 00:24:18132 // Notifies observers. The following methods are accessed by
133 // ProfileAttributesEntry.
134 void NotifyOnProfileAvatarChanged(const base::FilePath& profile_path) const;
135
David Roger14c3f872020-03-12 11:57:11136 // Disables the periodic reporting of profile metrics, as this is causing
137 // tests to time out.
138 virtual void DisableProfileMetricsForTesting() {}
139
lwchkg778965f2016-05-12 13:14:22140 protected:
141 FRIEND_TEST_ALL_PREFIXES(ProfileInfoCacheTest, EntriesInAttributesStorage);
WC Leung31f72732017-10-01 04:59:32142 FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest,
143 DownloadHighResAvatarTest);
144 FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest,
145 NothingToDownloadHighResAvatarTest);
146
WC Leung31f72732017-10-01 04:59:32147 // 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 Friedfa14d5f2019-04-21 20:49:36156 gfx::Image image,
WC Leung31f72732017-10-01 04:59:32157 const std::string& key,
Monica Bastaff6a35972020-02-14 19:16:04158 const base::FilePath& image_path,
159 base::OnceClosure callback);
lwchkg778965f2016-05-12 13:14:22160
Lei Zhang09d7ef52018-07-03 18:38:37161 PrefService* const prefs_;
lwchkg778965f2016-05-12 13:14:22162 mutable std::unordered_map<base::FilePath::StringType,
163 std::unique_ptr<ProfileAttributesEntry>>
164 profile_attributes_entries_;
165
Trent Apteda250ec3ab2018-08-19 08:52:19166 mutable base::ObserverList<Observer>::Unchecked observer_list_;
WC Leungc001c31e2017-08-21 16:41:16167
WC Leung31f72732017-10-01 04:59:32168 // A cache of gaia/high res avatar profile pictures. This cache is updated
169 // lazily so it needs to be mutable.
Dana Friedfa14d5f2019-04-21 20:49:36170 mutable std::unordered_map<std::string, gfx::Image> cached_avatar_images_;
WC Leung31f72732017-10-01 04:59:32171
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 Zhang09d7ef52018-07-03 18:38:37186 bool disable_avatar_download_for_testing_ = false;
WC Leung31f72732017-10-01 04:59:32187
188 // Task runner used for file operation on avatar images.
189 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
190
lwchkg99addc852016-02-10 16:14:55191 private:
WC Leung31f72732017-10-01 04:59:32192 // 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 Treib866ca832019-05-15 10:11:12196 gfx::Image image) const;
WC Leung31f72732017-10-01 04:59:32197
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 Bastaff6a35972020-02-14 19:16:04201 const base::FilePath& profile_path,
202 base::OnceClosure callback,
203 bool success) const;
WC Leung31f72732017-10-01 04:59:32204
David Roger4efdc012020-03-10 12:47:18205 // 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 Leung484920e2017-10-03 00:24:18211 // Notifies observers.
212 void NotifyOnProfileHighResAvatarLoaded(
213 const base::FilePath& profile_path) const;
anthonyvdb50a7cb2015-07-13 15:42:03214};
215
216#endif // CHROME_BROWSER_PROFILES_PROFILE_ATTRIBUTES_STORAGE_H_