blob: 75186d7304f2e2b67bf3ec79074f300ba469506f [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>
13#include <unordered_set>
lwchkg99addc852016-02-10 16:14:5514#include <vector>
avib896c712015-12-26 02:10:4315
lwchkg778965f2016-05-12 13:14:2216#include "base/files/file_path.h"
17#include "base/gtest_prod_util.h"
lwchkg99addc852016-02-10 16:14:5518#include "base/macros.h"
WC Leung31f72732017-10-01 04:59:3219#include "base/memory/weak_ptr.h"
WC Leungc001c31e2017-08-21 16:41:1620#include "base/observer_list.h"
lwchkg99addc852016-02-10 16:14:5521#include "base/strings/string16.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
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
lwchkg778965f2016-05-12 13:14:2242 ProfileAttributesStorage(PrefService* prefs,
43 const base::FilePath& user_data_dir);
44 virtual ~ProfileAttributesStorage();
anthonyvdb50a7cb2015-07-13 15:42:0345
46 // If the |supervised_user_id| is non-empty, the profile will be marked to be
47 // omitted from the avatar-menu list on desktop versions. This is used while a
48 // supervised user is in the process of being registered with the server. Use
49 // ProfileAttributesEntry::SetIsOmitted() to clear the flag when the profile
50 // is ready to be shown in the menu.
51 virtual void AddProfile(const base::FilePath& profile_path,
52 const base::string16& name,
53 const std::string& gaia_id,
54 const base::string16& user_name,
55 size_t icon_index,
56 const std::string& supervised_user_id) = 0;
57 // Removes the profile at |profile_path| from this storage. Does not delete or
58 // affect the actual profile's data.
59 virtual void RemoveProfile(const base::FilePath& profile_path) = 0;
60
61 // Returns a vector containing one attributes entry per known profile. They
62 // are not sorted in any particular order.
WC Leungf416f8e82017-08-19 14:50:0863 std::vector<ProfileAttributesEntry*> GetAllProfilesAttributes();
lwchkg778965f2016-05-12 13:14:2264 std::vector<ProfileAttributesEntry*> GetAllProfilesAttributesSortedByName();
anthonyvdb50a7cb2015-07-13 15:42:0365
66 // Populates |entry| with the data for the profile at |path| and returns true
67 // if the operation is successful and |entry| can be used. Returns false
68 // otherwise.
69 // |entry| should not be cached as it may not reflect subsequent changes to
70 // the profile's metadata.
71 virtual bool GetProfileAttributesWithPath(
72 const base::FilePath& path, ProfileAttributesEntry** entry) = 0;
73
74 // Returns the count of known profiles.
75 virtual size_t GetNumberOfProfiles() const = 0;
76
lwchkgc4d18c82016-02-20 06:12:2577 // Returns a unique name that can be assigned to a newly created profile.
lwchkg778965f2016-05-12 13:14:2278 base::string16 ChooseNameForNewProfile(size_t icon_index) const;
lwchkgc4d18c82016-02-20 06:12:2579
lwchkg498e92492016-04-23 11:04:1280 // Determines whether |name| is one of the default assigned names.
lwchkg778965f2016-05-12 13:14:2281 bool IsDefaultProfileName(const base::string16& name) const;
lwchkg498e92492016-04-23 11:04:1282
lwchkgc4d18c82016-02-20 06:12:2583 // Returns an avatar icon index that can be assigned to a newly created
84 // profile. Note that the icon may not be unique since there are a limited
85 // set of default icons.
lwchkg778965f2016-05-12 13:14:2286 size_t ChooseAvatarIconIndexForNewProfile() const;
lwchkgc4d18c82016-02-20 06:12:2587
WC Leung31f72732017-10-01 04:59:3288 // Returns the decoded image at |image_path|. Used both by the GAIA profile
89 // image and the high res avatars.
90 const gfx::Image* LoadAvatarPictureFromPath(
91 const base::FilePath& profile_path,
92 const std::string& key,
93 const base::FilePath& image_path) const;
94
WC Leungc001c31e2017-08-21 16:41:1695 void AddObserver(Observer* observer);
96 void RemoveObserver(Observer* observer);
lwchkg99addc852016-02-10 16:14:5597
WC Leung31f72732017-10-01 04:59:3298 void set_disable_avatar_download_for_testing(
99 bool disable_avatar_download_for_testing) {
100 disable_avatar_download_for_testing_ = disable_avatar_download_for_testing;
101 }
102
lwchkg778965f2016-05-12 13:14:22103 protected:
104 FRIEND_TEST_ALL_PREFIXES(ProfileInfoCacheTest, EntriesInAttributesStorage);
WC Leung31f72732017-10-01 04:59:32105 FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest,
106 DownloadHighResAvatarTest);
107 FRIEND_TEST_ALL_PREFIXES(ProfileAttributesStorageTest,
108 NothingToDownloadHighResAvatarTest);
109
110 // Checks whether the high res avatar at index |icon_index| exists, and if it
111 // does not, calls |DownloadHighResAvatar|.
112 void DownloadHighResAvatarIfNeeded(size_t icon_index,
113 const base::FilePath& profile_path);
114
115 // Starts downloading the high res avatar at index |icon_index| for profile
116 // with path |profile_path|.
117 void DownloadHighResAvatar(size_t icon_index,
118 const base::FilePath& profile_path);
119
120 // Saves the avatar |image| at |image_path|. This is used both for the GAIA
121 // profile pictures and the ProfileAvatarDownloader that is used to download
122 // the high res avatars.
123 void SaveAvatarImageAtPath(const base::FilePath& profile_path,
124 const gfx::Image* image,
125 const std::string& key,
126 const base::FilePath& image_path);
lwchkg778965f2016-05-12 13:14:22127
128 PrefService* prefs_;
129 base::FilePath user_data_dir_;
130 mutable std::unordered_map<base::FilePath::StringType,
131 std::unique_ptr<ProfileAttributesEntry>>
132 profile_attributes_entries_;
133
WC Leungc001c31e2017-08-21 16:41:16134 mutable base::ObserverList<Observer> observer_list_;
135
WC Leung31f72732017-10-01 04:59:32136 // A cache of gaia/high res avatar profile pictures. This cache is updated
137 // lazily so it needs to be mutable.
138 mutable std::unordered_map<std::string, std::unique_ptr<gfx::Image>>
139 cached_avatar_images_;
140
141 // Marks a profile picture as loading from disk. This prevents a picture from
142 // loading multiple times.
143 mutable std::unordered_map<std::string, bool> cached_avatar_images_loading_;
144
145 // Hash table of profile pictures currently being downloaded from the remote
146 // location and the ProfileAvatarDownloader instances downloading them.
147 // This prevents a picture from being downloaded multiple times. The
148 // ProfileAvatarDownloader instances are deleted when the download completes
149 // or when the ProfileInfoCache is destroyed.
150 std::unordered_map<std::string, std::unique_ptr<ProfileAvatarDownloader>>
151 avatar_images_downloads_in_progress_;
152
153 // Determines of the ProfileAvatarDownloader should be created and executed
154 // or not. Only set to true for tests.
155 bool disable_avatar_download_for_testing_;
156
157 // Task runner used for file operation on avatar images.
158 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
159
lwchkg99addc852016-02-10 16:14:55160 private:
WC Leung31f72732017-10-01 04:59:32161 // Called when the picture given by |key| has been loaded from disk and
162 // decoded into |image|.
163 void OnAvatarPictureLoaded(const base::FilePath& profile_path,
164 const std::string& key,
165 gfx::Image** image) const;
166
167 // Called when the picture given by |file_name| has been saved to disk. Used
168 // both for the GAIA profile picture and the high res avatar files.
169 void OnAvatarPictureSaved(const std::string& file_name,
170 const base::FilePath& profile_path) const;
171
172 // Calls observers.
173 void CallOnProfileHighResAvatarLoaded(base::FilePath profile_path) const;
174
anthonyvdb50a7cb2015-07-13 15:42:03175 DISALLOW_COPY_AND_ASSIGN(ProfileAttributesStorage);
176};
177
178#endif // CHROME_BROWSER_PROFILES_PROFILE_ATTRIBUTES_STORAGE_H_