Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / login / users / avatar / user_image_manager_impl.h
blob8a0875f93f6ec2e06ab9fa181f3a8ce8715f0094
1 // Copyright 2014 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.
5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_USERS_AVATAR_USER_IMAGE_MANAGER_IMPL_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_USERS_AVATAR_USER_IMAGE_MANAGER_IMPL_H_
8 #include <map>
9 #include <set>
10 #include <string>
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/time/time.h"
18 #include "base/timer/timer.h"
19 #include "base/values.h"
20 #include "chrome/browser/chromeos/login/users/avatar/user_image_loader.h"
21 #include "chrome/browser/chromeos/login/users/avatar/user_image_manager.h"
22 #include "chrome/browser/profiles/profile_downloader_delegate.h"
23 #include "components/user_manager/user.h"
24 #include "ui/gfx/image/image_skia.h"
26 class ProfileDownloader;
28 namespace base {
29 class FilePath;
30 class SequencedTaskRunner;
33 namespace user_manager {
34 class UserImage;
35 class UserManager;
38 namespace chromeos {
40 class UserImageSyncObserver;
42 class UserImageManagerImpl
43 : public UserImageManager,
44 public ProfileDownloaderDelegate {
45 public:
46 // UserImageManager:
47 UserImageManagerImpl(const std::string& user_id,
48 user_manager::UserManager* user_manager);
49 ~UserImageManagerImpl() override;
51 void LoadUserImage() override;
52 void UserLoggedIn(bool user_is_new, bool user_is_local) override;
53 void UserProfileCreated() override;
54 void SaveUserDefaultImageIndex(int default_image_index) override;
55 void SaveUserImage(const user_manager::UserImage& user_image) override;
56 void SaveUserImageFromFile(const base::FilePath& path) override;
57 void SaveUserImageFromProfileImage() override;
58 void DeleteUserImage() override;
59 void DownloadProfileImage(const std::string& reason) override;
60 const gfx::ImageSkia& DownloadedProfileImage() const override;
61 UserImageSyncObserver* GetSyncObserver() const override;
62 void Shutdown() override;
64 void OnExternalDataSet(const std::string& policy) override;
65 void OnExternalDataCleared(const std::string& policy) override;
66 void OnExternalDataFetched(const std::string& policy,
67 scoped_ptr<std::string> data) override;
69 static void IgnoreProfileDataDownloadDelayForTesting();
71 private:
72 friend class UserImageManagerTest;
74 // Every image load or update is encapsulated by a Job. Whenever an image load
75 // or update is requested for a user, the Job currently running for that user
76 // (if any) is canceled. This ensures that at most one Job is running per user
77 // at any given time. There are two further guarantees:
79 // * Changes to User objects and local state are performed on the thread that
80 // |this| runs on.
81 // * File writes and deletions are performed via |background_task_runner_|.
83 // With the above, it is guaranteed that any changes made by a canceled Job
84 // cannot race against against changes made by the superseding Job.
85 class Job;
87 // ProfileDownloaderDelegate:
88 bool NeedsProfilePicture() const override;
89 int GetDesiredImageSideLength() const override;
90 Profile* GetBrowserProfile() override;
91 std::string GetCachedPictureURL() const override;
92 bool IsPreSignin() const override;
93 void OnProfileDownloadSuccess(ProfileDownloader* downloader) override;
94 void OnProfileDownloadFailure(
95 ProfileDownloader* downloader,
96 ProfileDownloaderDelegate::FailureReason reason) override;
98 // Returns true if the user image for the user is managed by
99 // policy and the user is not allowed to change it.
100 bool IsUserImageManaged() const;
102 // Randomly chooses one of the default images for the specified user, sends a
103 // LOGIN_USER_IMAGE_CHANGED notification and updates local state.
104 void SetInitialUserImage();
106 // Initializes the |downloaded_profile_image_| for the currently logged-in
107 // user to a profile image that had been downloaded and saved before if such
108 // a saved image is available and no updated image has been downloaded yet.
109 void TryToInitDownloadedProfileImage();
111 // Returns true if the profile image needs to be downloaded. This is the case
112 // when a GAIA user is logged in and at least one of the following applies:
113 // * The profile image has explicitly been requested by a call to
114 // DownloadProfileImage() and has not been successfully downloaded since.
115 // * The user's user image is the profile image.
116 bool NeedProfileImage() const;
118 // Downloads the profile data for the currently logged-in user. The user's
119 // full name and, if NeedProfileImage() is true, the profile image are
120 // downloaded. |reason| is an arbitrary string (used to report UMA histograms
121 // with download times).
122 void DownloadProfileData(const std::string& reason);
124 // Removes ther user from the dictionary |prefs_dict_root| in
125 // local state and deletes the image file that the dictionary
126 // referenced for that user.
127 void DeleteUserImageAndLocalStateEntry(const char* prefs_dict_root);
129 // Called when a Job updates the copy of the user image held in
130 // memory. Allows |this| to update |downloaded_profile_image_| and
131 // send a NOTIFICATION_LOGIN_USER_IMAGE_CHANGED notification.
132 void OnJobChangedUserImage();
134 // Called when a Job for the user finishes. If a migration was
135 // required for the user, the migration is now complete and the old
136 // image file for that user, if any, is deleted.
137 void OnJobDone();
139 // Completes migration by removing the user from the old prefs
140 // dictionary.
141 void UpdateLocalStateAfterMigration();
143 // Create a sync observer if a user is logged in, the user's user image is
144 // allowed to be synced and no sync observer exists yet.
145 void TryToCreateImageSyncObserver();
147 // Returns immutable version of user with |user_id_|.
148 const user_manager::User* GetUser() const;
150 // Returns mutable version of user with |user_id_|.
151 user_manager::User* GetUserAndModify() const;
153 // Returns true if user with |user_id_| is logged in and has gaia account.
154 bool IsUserLoggedInAndHasGaiaAccount() const;
156 // The user manager.
157 user_manager::UserManager* user_manager_;
159 // Loader for JPEG user images.
160 scoped_refptr<UserImageLoader> image_loader_;
162 // Unsafe loader instance for all user images formats.
163 scoped_refptr<UserImageLoader> unsafe_image_loader_;
165 // Whether the |profile_downloader_| is downloading the profile image for the
166 // currently logged-in user (and not just the full name). Only valid when a
167 // download is currently in progress.
168 bool downloading_profile_image_;
170 // Download reason given to DownloadProfileImage(), used for UMA histograms.
171 // Only valid when a download is currently in progress and
172 // |downloading_profile_image_| is true.
173 std::string profile_image_download_reason_;
175 // Time when the profile image download started. Only valid when a download is
176 // currently in progress and |downloading_profile_image_| is true.
177 base::TimeTicks profile_image_load_start_time_;
179 // Downloader for the user's profile data. NULL when no download is
180 // currently in progress.
181 scoped_ptr<ProfileDownloader> profile_downloader_;
183 // The currently logged-in user's downloaded profile image, if successfully
184 // downloaded or initialized from a previously downloaded and saved image.
185 gfx::ImageSkia downloaded_profile_image_;
187 // Data URL corresponding to |downloaded_profile_image_|. Empty if no
188 // |downloaded_profile_image_| is currently available.
189 std::string downloaded_profile_image_data_url_;
191 // URL from which |downloaded_profile_image_| was downloaded. Empty if no
192 // |downloaded_profile_image_| is currently available.
193 GURL profile_image_url_;
195 // Whether a download of the currently logged-in user's profile image has been
196 // explicitly requested by a call to DownloadProfileImage() and has not been
197 // satisfied by a successful download yet.
198 bool profile_image_requested_;
200 // Timer used to start a profile data download shortly after login and to
201 // restart the download after network errors.
202 base::OneShotTimer<UserImageManagerImpl> profile_download_one_shot_timer_;
204 // Timer used to periodically start a profile data, ensuring the profile data
205 // stays up to date.
206 base::RepeatingTimer<UserImageManagerImpl> profile_download_periodic_timer_;
208 // Sync observer for the currently logged-in user.
209 scoped_ptr<UserImageSyncObserver> user_image_sync_observer_;
211 // Background task runner on which Jobs perform file I/O and the image
212 // decoders run.
213 scoped_refptr<base::SequencedTaskRunner> background_task_runner_;
215 // The currently running job.
216 scoped_ptr<Job> job_;
218 bool has_managed_image_;
219 bool user_needs_migration_;
221 base::WeakPtrFactory<UserImageManagerImpl> weak_factory_;
223 DISALLOW_COPY_AND_ASSIGN(UserImageManagerImpl);
226 } // namespace chromeos
228 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_USERS_AVATAR_USER_IMAGE_MANAGER_IMPL_H_