blob: feb07458cca9879361c6844ecd9f6a93d433fae6 [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"
David Rogerbbd82d72020-10-13 10:50:5321#include "build/build_config.h"
lwchkg778965f2016-05-12 13:14:2222#include "chrome/browser/profiles/profile_attributes_entry.h"
lwchkg99addc852016-02-10 16:14:5523#include "chrome/browser/profiles/profile_info_cache_observer.h"
24
WC Leung31f72732017-10-01 04:59:3225namespace base {
26class SequencedTaskRunner;
27}
28
29namespace gfx {
30class Image;
31}
32
Alexander Alekseev4084f502018-02-20 22:16:4633class AccountId;
lwchkg778965f2016-05-12 13:14:2234class PrefService;
anthonyvdb50a7cb2015-07-13 15:42:0335class ProfileAttributesEntry;
WC Leung31f72732017-10-01 04:59:3236class ProfileAvatarDownloader;
anthonyvdb50a7cb2015-07-13 15:42:0337
WC Leung31f72732017-10-01 04:59:3238class ProfileAttributesStorage
39 : public base::SupportsWeakPtr<ProfileAttributesStorage> {
anthonyvdb50a7cb2015-07-13 15:42:0340 public:
lwchkg99addc852016-02-10 16:14:5541 using Observer = ProfileInfoCacheObserver;
42
Lei Zhang09d7ef52018-07-03 18:38:3743 explicit ProfileAttributesStorage(PrefService* prefs);
David Bienvenu1e9f6a72020-08-20 01:05:4844 ProfileAttributesStorage(const ProfileAttributesStorage&) = delete;
45 ProfileAttributesStorage& operator=(const ProfileAttributesStorage&) = delete;
lwchkg778965f2016-05-12 13:14:2246 virtual ~ProfileAttributesStorage();
anthonyvdb50a7cb2015-07-13 15:42:0347
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 Basta9ca47042019-09-16 17:36:5157 bool is_consented_primary_account,
anthonyvdb50a7cb2015-07-13 15:42:0358 size_t icon_index,
Roman Sorokin8ead4d72018-04-16 15:58:5859 const std::string& supervised_user_id,
60 const AccountId& account_id) = 0;
Alexander Alekseev4084f502018-02-20 22:16:4661
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
anthonyvdb50a7cb2015-07-13 15:42:0366 // 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 Leungf416f8e82017-08-19 14:50:0872 std::vector<ProfileAttributesEntry*> GetAllProfilesAttributes();
lwchkg778965f2016-05-12 13:14:2273 std::vector<ProfileAttributesEntry*> GetAllProfilesAttributesSortedByName();
anthonyvdb50a7cb2015-07-13 15:42:0374
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
lwchkgc4d18c82016-02-20 06:12:2586 // Returns a unique name that can be assigned to a newly created profile.
lwchkg778965f2016-05-12 13:14:2287 base::string16 ChooseNameForNewProfile(size_t icon_index) const;
lwchkgc4d18c82016-02-20 06:12:2588
lwchkg498e92492016-04-23 11:04:1289 // Determines whether |name| is one of the default assigned names.
Monica Basta6655884a2019-10-29 10:38:3990 // 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;
lwchkg498e92492016-04-23 11:04:1299
David Rogerbbd82d72020-10-13 10:50:53100#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 Rogere582c4452020-03-18 20:45:38107 // 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
lwchkgc4d18c82016-02-20 06:12:25111 // 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.
lwchkg778965f2016-05-12 13:14:22114 size_t ChooseAvatarIconIndexForNewProfile() const;
lwchkgc4d18c82016-02-20 06:12:25115
WC Leung31f72732017-10-01 04:59:32116 // 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 Leung484920e2017-10-03 00:24:18123 // 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 Leungc001c31e2017-08-21 16:41:16128 void AddObserver(Observer* observer);
129 void RemoveObserver(Observer* observer);
lwchkg99addc852016-02-10 16:14:55130
WC Leung484920e2017-10-03 00:24:18131 bool GetDisableAvatarDownloadForTesting() {
132 return disable_avatar_download_for_testing_;
133 }
134
WC Leung31f72732017-10-01 04:59:32135 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 Leung484920e2017-10-03 00:24:18140 // Notifies observers. The following methods are accessed by
141 // ProfileAttributesEntry.
142 void NotifyOnProfileAvatarChanged(const base::FilePath& profile_path) const;
143
David Roger14c3f872020-03-12 11:57:11144 // Disables the periodic reporting of profile metrics, as this is causing
145 // tests to time out.
146 virtual void DisableProfileMetricsForTesting() {}
147
lwchkg778965f2016-05-12 13:14:22148 protected:
149 FRIEND_TEST_ALL_PREFIXES(ProfileInfoCacheTest, EntriesInAttributesStorage);
WC Leung31f72732017-10-01 04:59:32150 FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest,
151 DownloadHighResAvatarTest);
152 FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest,
153 NothingToDownloadHighResAvatarTest);
154
WC Leung31f72732017-10-01 04:59:32155 // 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 Friedfa14d5f2019-04-21 20:49:36164 gfx::Image image,
WC Leung31f72732017-10-01 04:59:32165 const std::string& key,
Monica Bastaff6a35972020-02-14 19:16:04166 const base::FilePath& image_path,
167 base::OnceClosure callback);
lwchkg778965f2016-05-12 13:14:22168
Lei Zhang09d7ef52018-07-03 18:38:37169 PrefService* const prefs_;
lwchkg778965f2016-05-12 13:14:22170 mutable std::unordered_map<base::FilePath::StringType,
171 std::unique_ptr<ProfileAttributesEntry>>
172 profile_attributes_entries_;
173
Trent Apteda250ec3ab2018-08-19 08:52:19174 mutable base::ObserverList<Observer>::Unchecked observer_list_;
WC Leungc001c31e2017-08-21 16:41:16175
WC Leung31f72732017-10-01 04:59:32176 // A cache of gaia/high res avatar profile pictures. This cache is updated
177 // lazily so it needs to be mutable.
Dana Friedfa14d5f2019-04-21 20:49:36178 mutable std::unordered_map<std::string, gfx::Image> cached_avatar_images_;
WC Leung31f72732017-10-01 04:59:32179
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 Zhang09d7ef52018-07-03 18:38:37194 bool disable_avatar_download_for_testing_ = false;
WC Leung31f72732017-10-01 04:59:32195
196 // Task runner used for file operation on avatar images.
197 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
198
lwchkg99addc852016-02-10 16:14:55199 private:
WC Leung31f72732017-10-01 04:59:32200 // 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 Treib866ca832019-05-15 10:11:12204 gfx::Image image) const;
WC Leung31f72732017-10-01 04:59:32205
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 Bastaff6a35972020-02-14 19:16:04209 const base::FilePath& profile_path,
210 base::OnceClosure callback,
211 bool success) const;
WC Leung31f72732017-10-01 04:59:32212
David Roger4efdc012020-03-10 12:47:18213 // 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 Leung484920e2017-10-03 00:24:18219 // Notifies observers.
220 void NotifyOnProfileHighResAvatarLoaded(
221 const base::FilePath& profile_path) const;
anthonyvdb50a7cb2015-07-13 15:42:03222};
223
224#endif // CHROME_BROWSER_PROFILES_PROFILE_ATTRIBUTES_STORAGE_H_