blob: 8c6f70ef5254a30e48f00004a8b7eb322d7aa132 [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
lwchkg778965f2016-05-12 13:14:2215#include "base/files/file_path.h"
16#include "base/gtest_prod_util.h"
lwchkg99addc852016-02-10 16:14:5517#include "base/macros.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);
lwchkg778965f2016-05-12 13:14:2243 virtual ~ProfileAttributesStorage();
anthonyvdb50a7cb2015-07-13 15:42:0344
45 // If the |supervised_user_id| is non-empty, the profile will be marked to be
46 // omitted from the avatar-menu list on desktop versions. This is used while a
47 // supervised user is in the process of being registered with the server. Use
48 // ProfileAttributesEntry::SetIsOmitted() to clear the flag when the profile
49 // is ready to be shown in the menu.
50 virtual void AddProfile(const base::FilePath& profile_path,
51 const base::string16& name,
52 const std::string& gaia_id,
53 const base::string16& user_name,
Monica Basta9ca47042019-09-16 17:36:5154 bool is_consented_primary_account,
anthonyvdb50a7cb2015-07-13 15:42:0355 size_t icon_index,
Roman Sorokin8ead4d72018-04-16 15:58:5856 const std::string& supervised_user_id,
57 const AccountId& account_id) = 0;
Alexander Alekseev4084f502018-02-20 22:16:4658
59 // Removes the profile matching given |account_id| from this storage.
60 // Calculates profile path and calls RemoveProfile() on it.
61 virtual void RemoveProfileByAccountId(const AccountId& account_id) = 0;
62
anthonyvdb50a7cb2015-07-13 15:42:0363 // Removes the profile at |profile_path| from this storage. Does not delete or
64 // affect the actual profile's data.
65 virtual void RemoveProfile(const base::FilePath& profile_path) = 0;
66
67 // Returns a vector containing one attributes entry per known profile. They
68 // are not sorted in any particular order.
WC Leungf416f8e82017-08-19 14:50:0869 std::vector<ProfileAttributesEntry*> GetAllProfilesAttributes();
lwchkg778965f2016-05-12 13:14:2270 std::vector<ProfileAttributesEntry*> GetAllProfilesAttributesSortedByName();
anthonyvdb50a7cb2015-07-13 15:42:0371
72 // Populates |entry| with the data for the profile at |path| and returns true
73 // if the operation is successful and |entry| can be used. Returns false
74 // otherwise.
75 // |entry| should not be cached as it may not reflect subsequent changes to
76 // the profile's metadata.
77 virtual bool GetProfileAttributesWithPath(
78 const base::FilePath& path, ProfileAttributesEntry** entry) = 0;
79
80 // Returns the count of known profiles.
81 virtual size_t GetNumberOfProfiles() const = 0;
82
lwchkgc4d18c82016-02-20 06:12:2583 // Returns a unique name that can be assigned to a newly created profile.
lwchkg778965f2016-05-12 13:14:2284 base::string16 ChooseNameForNewProfile(size_t icon_index) const;
lwchkgc4d18c82016-02-20 06:12:2585
lwchkg498e92492016-04-23 11:04:1286 // Determines whether |name| is one of the default assigned names.
Monica Basta6655884a2019-10-29 10:38:3987 // On Desktop, if |include_check_for_legacy_profile_name| is false,
88 // |IsDefaultProfileName()| would only return true if the |name| is in the
89 // form of |Person %n| which is the new default local profile name. If
90 // |include_check_for_legacy_profile_name| is true, we will also check if name
91 // is one of the legacy profile names (e.g. Saratoga, Default user, ..).
92 // For other platforms, so far |include_check_for_legacy_profile_name|
93 // is not used.
94 bool IsDefaultProfileName(const base::string16& name,
95 bool include_check_for_legacy_profile_name) const;
lwchkg498e92492016-04-23 11:04:1296
lwchkgc4d18c82016-02-20 06:12:2597 // Returns an avatar icon index that can be assigned to a newly created
98 // profile. Note that the icon may not be unique since there are a limited
99 // set of default icons.
lwchkg778965f2016-05-12 13:14:22100 size_t ChooseAvatarIconIndexForNewProfile() const;
lwchkgc4d18c82016-02-20 06:12:25101
WC Leung31f72732017-10-01 04:59:32102 // Returns the decoded image at |image_path|. Used both by the GAIA profile
103 // image and the high res avatars.
104 const gfx::Image* LoadAvatarPictureFromPath(
105 const base::FilePath& profile_path,
106 const std::string& key,
107 const base::FilePath& image_path) const;
108
WC Leung484920e2017-10-03 00:24:18109 // Checks whether the high res avatar at index |icon_index| exists, and if it
110 // does not, calls |DownloadHighResAvatar|.
111 void DownloadHighResAvatarIfNeeded(size_t icon_index,
112 const base::FilePath& profile_path);
113
WC Leungc001c31e2017-08-21 16:41:16114 void AddObserver(Observer* observer);
115 void RemoveObserver(Observer* observer);
lwchkg99addc852016-02-10 16:14:55116
WC Leung484920e2017-10-03 00:24:18117 bool GetDisableAvatarDownloadForTesting() {
118 return disable_avatar_download_for_testing_;
119 }
120
WC Leung31f72732017-10-01 04:59:32121 void set_disable_avatar_download_for_testing(
122 bool disable_avatar_download_for_testing) {
123 disable_avatar_download_for_testing_ = disable_avatar_download_for_testing;
124 }
125
WC Leung484920e2017-10-03 00:24:18126 // Notifies observers. The following methods are accessed by
127 // ProfileAttributesEntry.
128 void NotifyOnProfileAvatarChanged(const base::FilePath& profile_path) const;
129
lwchkg778965f2016-05-12 13:14:22130 protected:
131 FRIEND_TEST_ALL_PREFIXES(ProfileInfoCacheTest, EntriesInAttributesStorage);
WC Leung31f72732017-10-01 04:59:32132 FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest,
133 DownloadHighResAvatarTest);
134 FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest,
135 NothingToDownloadHighResAvatarTest);
136
WC Leung31f72732017-10-01 04:59:32137 // Starts downloading the high res avatar at index |icon_index| for profile
138 // with path |profile_path|.
139 void DownloadHighResAvatar(size_t icon_index,
140 const base::FilePath& profile_path);
141
142 // Saves the avatar |image| at |image_path|. This is used both for the GAIA
143 // profile pictures and the ProfileAvatarDownloader that is used to download
144 // the high res avatars.
145 void SaveAvatarImageAtPath(const base::FilePath& profile_path,
Dana Friedfa14d5f2019-04-21 20:49:36146 gfx::Image image,
WC Leung31f72732017-10-01 04:59:32147 const std::string& key,
Monica Bastaff6a35972020-02-14 19:16:04148 const base::FilePath& image_path,
149 base::OnceClosure callback);
lwchkg778965f2016-05-12 13:14:22150
Lei Zhang09d7ef52018-07-03 18:38:37151 PrefService* const prefs_;
lwchkg778965f2016-05-12 13:14:22152 mutable std::unordered_map<base::FilePath::StringType,
153 std::unique_ptr<ProfileAttributesEntry>>
154 profile_attributes_entries_;
155
Trent Apteda250ec3ab2018-08-19 08:52:19156 mutable base::ObserverList<Observer>::Unchecked observer_list_;
WC Leungc001c31e2017-08-21 16:41:16157
WC Leung31f72732017-10-01 04:59:32158 // A cache of gaia/high res avatar profile pictures. This cache is updated
159 // lazily so it needs to be mutable.
Dana Friedfa14d5f2019-04-21 20:49:36160 mutable std::unordered_map<std::string, gfx::Image> cached_avatar_images_;
WC Leung31f72732017-10-01 04:59:32161
162 // Marks a profile picture as loading from disk. This prevents a picture from
163 // loading multiple times.
164 mutable std::unordered_map<std::string, bool> cached_avatar_images_loading_;
165
166 // Hash table of profile pictures currently being downloaded from the remote
167 // location and the ProfileAvatarDownloader instances downloading them.
168 // This prevents a picture from being downloaded multiple times. The
169 // ProfileAvatarDownloader instances are deleted when the download completes
170 // or when the ProfileInfoCache is destroyed.
171 std::unordered_map<std::string, std::unique_ptr<ProfileAvatarDownloader>>
172 avatar_images_downloads_in_progress_;
173
174 // Determines of the ProfileAvatarDownloader should be created and executed
175 // or not. Only set to true for tests.
Lei Zhang09d7ef52018-07-03 18:38:37176 bool disable_avatar_download_for_testing_ = false;
WC Leung31f72732017-10-01 04:59:32177
178 // Task runner used for file operation on avatar images.
179 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
180
lwchkg99addc852016-02-10 16:14:55181 private:
WC Leung31f72732017-10-01 04:59:32182 // Called when the picture given by |key| has been loaded from disk and
183 // decoded into |image|.
184 void OnAvatarPictureLoaded(const base::FilePath& profile_path,
185 const std::string& key,
Marc Treib866ca832019-05-15 10:11:12186 gfx::Image image) const;
WC Leung31f72732017-10-01 04:59:32187
188 // Called when the picture given by |file_name| has been saved to disk. Used
189 // both for the GAIA profile picture and the high res avatar files.
190 void OnAvatarPictureSaved(const std::string& file_name,
Monica Bastaff6a35972020-02-14 19:16:04191 const base::FilePath& profile_path,
192 base::OnceClosure callback,
193 bool success) const;
WC Leung31f72732017-10-01 04:59:32194
David Roger4efdc012020-03-10 12:47:18195 // Helper function that calls SaveAvatarImageAtPath without a callback.
196 void SaveAvatarImageAtPathNoCallback(const base::FilePath& profile_path,
197 gfx::Image image,
198 const std::string& key,
199 const base::FilePath& image_path);
200
WC Leung484920e2017-10-03 00:24:18201 // Notifies observers.
202 void NotifyOnProfileHighResAvatarLoaded(
203 const base::FilePath& profile_path) const;
WC Leung31f72732017-10-01 04:59:32204
anthonyvdb50a7cb2015-07-13 15:42:03205 DISALLOW_COPY_AND_ASSIGN(ProfileAttributesStorage);
206};
207
208#endif // CHROME_BROWSER_PROFILES_PROFILE_ATTRIBUTES_STORAGE_H_