Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / login / user_image_manager_impl.h
blobc75555ac8688f7388ed0be88e4aab6265a44cbd3
1 // Copyright (c) 2013 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_USER_IMAGE_MANAGER_IMPL_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_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/user.h"
21 #include "chrome/browser/chromeos/login/user_image_loader.h"
22 #include "chrome/browser/chromeos/login/user_image_manager.h"
23 #include "chrome/browser/profiles/profile_downloader_delegate.h"
24 #include "ui/gfx/image/image_skia.h"
26 class ProfileDownloader;
27 class UserImage;
29 namespace base {
30 class FilePath;
31 class SequencedTaskRunner;
34 namespace chromeos {
36 class CrosSettings;
37 class UserImageSyncObserver;
38 class UserManager;
40 class UserImageManagerImpl
41 : public UserImageManager,
42 public ProfileDownloaderDelegate {
43 public:
44 // UserImageManager:
45 UserImageManagerImpl(const std::string& user_id,
46 CrosSettings* cros_settings,
47 UserManager* user_manager);
48 virtual ~UserImageManagerImpl();
50 virtual void LoadUserImage() OVERRIDE;
51 virtual void UserLoggedIn(bool user_is_new, bool user_is_local) OVERRIDE;
52 virtual void SaveUserDefaultImageIndex(int default_image_index) OVERRIDE;
53 virtual void SaveUserImage(const UserImage& user_image) OVERRIDE;
54 virtual void SaveUserImageFromFile(const base::FilePath& path) OVERRIDE;
55 virtual void SaveUserImageFromProfileImage() OVERRIDE;
56 virtual void DeleteUserImage() OVERRIDE;
57 virtual void DownloadProfileImage(const std::string& reason) OVERRIDE;
58 virtual const gfx::ImageSkia& DownloadedProfileImage() const OVERRIDE;
59 virtual UserImageSyncObserver* GetSyncObserver() const OVERRIDE;
60 virtual void Shutdown() OVERRIDE;
62 virtual void OnExternalDataSet(const std::string& policy) OVERRIDE;
63 virtual void OnExternalDataCleared(const std::string& policy) OVERRIDE;
64 virtual void OnExternalDataFetched(const std::string& policy,
65 scoped_ptr<std::string> data) OVERRIDE;
67 static void IgnoreProfileDataDownloadDelayForTesting();
69 private:
70 friend class UserImageManagerTest;
72 // Every image load or update is encapsulated by a Job. Whenever an image load
73 // or update is requested for a user, the Job currently running for that user
74 // (if any) is canceled. This ensures that at most one Job is running per user
75 // at any given time. There are two further guarantees:
77 // * Changes to User objects and local state are performed on the thread that
78 // |this| runs on.
79 // * File writes and deletions are performed via |background_task_runner_|.
81 // With the above, it is guaranteed that any changes made by a canceled Job
82 // cannot race against against changes made by the superseding Job.
83 class Job;
85 // ProfileDownloaderDelegate:
86 virtual bool NeedsProfilePicture() const OVERRIDE;
87 virtual int GetDesiredImageSideLength() const OVERRIDE;
88 virtual Profile* GetBrowserProfile() OVERRIDE;
89 virtual std::string GetCachedPictureURL() const OVERRIDE;
90 virtual void OnProfileDownloadSuccess(ProfileDownloader* downloader) OVERRIDE;
91 virtual void OnProfileDownloadFailure(
92 ProfileDownloader* downloader,
93 ProfileDownloaderDelegate::FailureReason reason) OVERRIDE;
95 // Returns true if the user image for the user is managed by
96 // policy and the user is not allowed to change it.
97 bool IsUserImageManaged() const;
99 // Randomly chooses one of the default images for the specified user, sends a
100 // LOGIN_USER_IMAGE_CHANGED notification and updates local state.
101 void SetInitialUserImage();
103 // Initializes the |downloaded_profile_image_| for the currently logged-in
104 // user to a profile image that had been downloaded and saved before if such
105 // a saved image is available and no updated image has been downloaded yet.
106 void TryToInitDownloadedProfileImage();
108 // Returns true if the profile image needs to be downloaded. This is the case
109 // when a GAIA user is logged in and at least one of the following applies:
110 // * The profile image has explicitly been requested by a call to
111 // DownloadProfileImage() and has not been successfully downloaded since.
112 // * The user's user image is the profile image.
113 bool NeedProfileImage() const;
115 // Downloads the profile data for the currently logged-in user. The user's
116 // full name and, if NeedProfileImage() is true, the profile image are
117 // downloaded. |reason| is an arbitrary string (used to report UMA histograms
118 // with download times).
119 void DownloadProfileData(const std::string& reason);
121 // Removes ther user from the dictionary |prefs_dict_root| in
122 // local state and deletes the image file that the dictionary
123 // referenced for that user.
124 void DeleteUserImageAndLocalStateEntry(const char* prefs_dict_root);
126 // Called when a Job updates the copy of the user image held in
127 // memory. Allows |this| to update |downloaded_profile_image_| and
128 // send a NOTIFICATION_LOGIN_USER_IMAGE_CHANGED notification.
129 void OnJobChangedUserImage();
131 // Called when a Job for the user finishes. If a migration was
132 // required for the user, the migration is now complete and the old
133 // image file for that user, if any, is deleted.
134 void OnJobDone();
136 // Completes migration by removing the user from the old prefs
137 // dictionary.
138 void UpdateLocalStateAfterMigration();
140 // Create a sync observer if a user is logged in, the user's user image is
141 // allowed to be synced and no sync observer exists yet.
142 void TryToCreateImageSyncObserver();
144 // Returns immutable version of user with |user_id_|.
145 const User* GetUser() const;
147 // Returns mutable version of user with |user_id_|.
148 User* GetUserAndModify() const;
150 // Returns true if user with |user_id_| is logged in and a regular user.
151 bool IsUserLoggedInAndRegular() const;
153 // The user manager.
154 UserManager* user_manager_;
156 // Loader for JPEG user images.
157 scoped_refptr<UserImageLoader> image_loader_;
159 // Unsafe loader instance for all user images formats.
160 scoped_refptr<UserImageLoader> unsafe_image_loader_;
162 // Whether the |profile_downloader_| is downloading the profile image for the
163 // currently logged-in user (and not just the full name). Only valid when a
164 // download is currently in progress.
165 bool downloading_profile_image_;
167 // Download reason given to DownloadProfileImage(), used for UMA histograms.
168 // Only valid when a download is currently in progress and
169 // |downloading_profile_image_| is true.
170 std::string profile_image_download_reason_;
172 // Time when the profile image download started. Only valid when a download is
173 // currently in progress and |downloading_profile_image_| is true.
174 base::TimeTicks profile_image_load_start_time_;
176 // Downloader for the user's profile data. NULL when no download is
177 // currently in progress.
178 scoped_ptr<ProfileDownloader> profile_downloader_;
180 // The currently logged-in user's downloaded profile image, if successfully
181 // downloaded or initialized from a previously downloaded and saved image.
182 gfx::ImageSkia downloaded_profile_image_;
184 // Data URL corresponding to |downloaded_profile_image_|. Empty if no
185 // |downloaded_profile_image_| is currently available.
186 std::string downloaded_profile_image_data_url_;
188 // URL from which |downloaded_profile_image_| was downloaded. Empty if no
189 // |downloaded_profile_image_| is currently available.
190 GURL profile_image_url_;
192 // Whether a download of the currently logged-in user's profile image has been
193 // explicitly requested by a call to DownloadProfileImage() and has not been
194 // satisfied by a successful download yet.
195 bool profile_image_requested_;
197 // Timer used to start a profile data download shortly after login and to
198 // restart the download after network errors.
199 base::OneShotTimer<UserImageManagerImpl> profile_download_one_shot_timer_;
201 // Timer used to periodically start a profile data, ensuring the profile data
202 // stays up to date.
203 base::RepeatingTimer<UserImageManagerImpl> profile_download_periodic_timer_;
205 // Sync observer for the currently logged-in user.
206 scoped_ptr<UserImageSyncObserver> user_image_sync_observer_;
208 // Background task runner on which Jobs perform file I/O and the image
209 // decoders run.
210 scoped_refptr<base::SequencedTaskRunner> background_task_runner_;
212 // The currently running job.
213 scoped_ptr<Job> job_;
215 bool has_managed_image_;
216 bool user_needs_migration_;
218 base::WeakPtrFactory<UserImageManagerImpl> weak_factory_;
220 DISALLOW_COPY_AND_ASSIGN(UserImageManagerImpl);
223 } // namespace chromeos
225 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_USER_IMAGE_MANAGER_IMPL_H_