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_WALLPAPER_MANAGER_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_WALLPAPER_MANAGER_H_
10 #include "ash/desktop_background/desktop_background_controller.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/ref_counted_memory.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/threading/sequenced_worker_pool.h"
16 #include "base/time/time.h"
17 #include "chrome/browser/chromeos/login/user.h"
18 #include "chrome/browser/chromeos/login/user_image.h"
19 #include "chrome/browser/chromeos/login/user_image_loader.h"
20 #include "chrome/browser/chromeos/settings/cros_settings.h"
21 #include "content/public/browser/notification_observer.h"
22 #include "content/public/browser/notification_registrar.h"
23 #include "third_party/icu/source/i18n/unicode/timezone.h"
24 #include "ui/gfx/image/image_skia.h"
27 class PrefRegistrySimple;
30 class SequencedTaskRunner;
35 struct WallpaperInfo {
36 // Online wallpaper URL or file name of migrated wallpaper.
38 ash::WallpaperLayout layout;
39 User::WallpaperType type;
41 bool operator==(const WallpaperInfo& other) {
42 return (file == other.file) && (layout == other.layout) &&
47 class WallpaperManagerBrowserTest;
50 // Name of wallpaper sequence token.
51 extern const char kWallpaperSequenceTokenName[];
53 // File path suffices of resized small or large wallpaper.
54 // TODO(bshe): Use the same sub folder system as custom wallpapers use.
56 extern const char kSmallWallpaperSuffix[];
57 extern const char kLargeWallpaperSuffix[];
59 // Directory names of custom wallpapers.
60 extern const char kSmallWallpaperSubDir[];
61 extern const char kLargeWallpaperSubDir[];
62 extern const char kOriginalWallpaperSubDir[];
63 extern const char kThumbnailWallpaperSubDir[];
65 // This class maintains wallpapers for users who have logged into this Chrome
67 class WallpaperManager: public content::NotificationObserver {
72 explicit TestApi(WallpaperManager* wallpaper_manager);
75 base::FilePath current_wallpaper_path();
78 WallpaperManager* wallpaper_manager_; // not owned
80 DISALLOW_COPY_AND_ASSIGN(TestApi);
83 static WallpaperManager* Get();
86 virtual ~WallpaperManager();
88 void set_command_line_for_testing(CommandLine* command_line) {
89 command_line_for_testing_ = command_line;
92 // Indicates imminent shutdown, allowing the WallpaperManager to remove any
93 // observers it has registered.
96 // Registers wallpaper manager preferences.
97 static void RegisterPrefs(PrefRegistrySimple* registry);
99 // Adds PowerManagerClient, TimeZoneSettings and CrosSettings observers.
102 // Loads wallpaper asynchronously if the current wallpaper is not the
103 // wallpaper of logged in user.
104 void EnsureLoggedInUserWallpaperLoaded();
106 // Clears ONLINE and CUSTOM wallpaper cache.
107 void ClearWallpaperCache();
109 // Returns custom wallpaper path. Append |sub_dir|, |user_id_hash| and |file|
110 // to custom wallpaper directory.
111 base::FilePath GetCustomWallpaperPath(const char* sub_dir,
112 const std::string& user_id_hash,
113 const std::string& file);
115 // Gets encoded wallpaper from cache. Returns true if success.
116 bool GetWallpaperFromCache(const std::string& email,
117 gfx::ImageSkia* wallpaper);
119 // Returns filepath to save original custom wallpaper for the given user.
120 base::FilePath GetOriginalWallpaperPathForUser(const std::string& username);
122 // Gets wallpaper information of logged in user.
123 bool GetLoggedInUserWallpaperInfo(WallpaperInfo* info);
125 // Initializes wallpaper. If logged in, loads user's wallpaper. If not logged
126 // in, uses a solid color wallpaper. If logged in as a stub user, uses an
128 void InitializeWallpaper();
130 // NotificationObserver overrides:
131 virtual void Observe(int type,
132 const content::NotificationSource& source,
133 const content::NotificationDetails& details) OVERRIDE;
135 // Removes all |email| related wallpaper info and saved wallpapers.
136 void RemoveUserWallpaperInfo(const std::string& email);
138 // Resizes |wallpaper| to a resolution which is nearest to |preferred_width|
139 // and |preferred_height| while maintaining aspect ratio.
140 bool ResizeWallpaper(const UserImage& wallpaper,
141 ash::WallpaperLayout layout,
143 int preferred_height,
144 scoped_refptr<base::RefCountedBytes>* output);
146 // Resizes |wallpaper| to a resolution which is nearest to |preferred_width|
147 // and |preferred_height| while maintaining aspect ratio. And saves the
148 // resized wallpaper to |path|.
149 void ResizeAndSaveWallpaper(const UserImage& wallpaper,
150 const base::FilePath& path,
151 ash::WallpaperLayout layout,
153 int preferred_height);
155 // Saves custom wallpaper to file, post task to generate thumbnail and updates
156 // local state preferences.
157 void SetCustomWallpaper(const std::string& username,
158 const std::string& user_id_hash,
159 const std::string& file,
160 ash::WallpaperLayout layout,
161 User::WallpaperType type,
162 const UserImage& wallpaper);
164 // Sets wallpaper to default wallpaper.
165 void SetDefaultWallpaper();
167 // Sets one of the default wallpapers for the specified user and saves this
168 // settings in local state.
169 void SetInitialUserWallpaper(const std::string& username, bool is_persistent);
171 // Sets selected wallpaper information for |username| and saves it to Local
172 // State if |is_persistent| is true.
173 void SetUserWallpaperInfo(const std::string& username,
174 const WallpaperInfo& info,
177 // Sets last selected user on user pod row.
178 void SetLastSelectedUser(const std::string& last_selected_user);
180 // Sets |email|'s wallpaper.
181 void SetUserWallpaper(const std::string& email);
183 // Sets wallpaper to |wallpaper|.
184 void SetWallpaperFromImageSkia(const gfx::ImageSkia& wallpaper,
185 ash::WallpaperLayout layout);
187 // Updates current wallpaper. It may switch the size of wallpaper based on the
188 // current display's resolution.
189 void UpdateWallpaper();
192 friend class TestApi;
193 friend class WallpaperManagerBrowserTest;
194 typedef std::map<std::string, gfx::ImageSkia> CustomWallpaperMap;
196 // The number of wallpapers have loaded. For test only.
197 int loaded_wallpapers() const { return loaded_wallpapers_; }
199 // Cache some (or all) logged in users' wallpapers to memory at login
200 // screen. It should not compete with first wallpaper loading when boot
201 // up/initialize login WebUI page.
202 // There are two ways the first wallpaper might be loaded:
203 // 1. Loaded on boot. Login WebUI waits for it.
204 // 2. When flag --disable-boot-animation is passed. Login WebUI is loaded
205 // right away and in 500ms after. Wallpaper started to load.
206 // For case 2, should_cache_wallpaper_ is used to indicate if we need to
207 // cache wallpapers on wallpaper animation finished. The cache operation
208 // should be only executed once.
209 void CacheUsersWallpapers();
211 // Caches |email|'s wallpaper to memory.
212 void CacheUserWallpaper(const std::string& email);
214 // Clears all obsolete wallpaper prefs from old version wallpaper pickers.
215 void ClearObsoleteWallpaperPrefs();
217 // Deletes everything else except |path| in the same directory.
218 void DeleteAllExcept(const base::FilePath& path);
220 // Deletes a list of wallpaper files in |file_list|.
221 void DeleteWallpaperInList(const std::vector<base::FilePath>& file_list);
223 // Deletes all |email| related custom wallpapers and directories.
224 void DeleteUserWallpapers(const std::string& email,
225 const std::string& path_to_file);
227 // Creates all new custom wallpaper directories for |user_id_hash| if not
229 void EnsureCustomWallpaperDirectories(const std::string& user_id_hash);
231 // Gets the CommandLine representing the current process's command line.
232 CommandLine* GetComandLine();
234 // Initialize wallpaper of registered device after device policy is trusted.
235 // Note that before device is enrolled, it proceeds with untrusted setting.
236 void InitializeRegisteredDeviceWallpaper();
238 // Loads |email|'s wallpaper. When |update_wallpaper| is true, sets wallpaper
239 // to the loaded wallpaper.
240 void LoadWallpaper(const std::string& email,
241 const WallpaperInfo& info,
242 bool update_wallpaper);
244 // Moves custom wallpapers from |email| directory to |user_id_hash|
246 void MoveCustomWallpapersOnWorker(const std::string& email,
247 const std::string& user_id_hash);
249 // Called when the original custom wallpaper is moved to the new place.
250 // Updates the corresponding user wallpaper info.
251 void MoveCustomWallpapersSuccess(const std::string& email,
252 const std::string& user_id_hash);
254 // Moves custom wallpaper to a new place. Email address was used as directory
255 // name in the old system, this is not safe. New directory system uses
256 // user_id_hash instead of email. This must be called after user_id_hash is
258 void MoveLoggedInUserCustomWallpaper();
260 // Gets |email|'s custom wallpaper at |wallpaper_path|. Falls back on original
261 // custom wallpaper. When |update_wallpaper| is true, sets wallpaper to the
262 // loaded wallpaper. Must run on wallpaper sequenced worker thread.
263 void GetCustomWallpaperInternal(const std::string& email,
264 const WallpaperInfo& info,
265 const base::FilePath& wallpaper_path,
266 bool update_wallpaper);
268 // Gets wallpaper information of |email| from Local State or memory. Returns
269 // false if wallpaper information is not found.
270 bool GetUserWallpaperInfo(const std::string& email, WallpaperInfo* info);
272 // Sets wallpaper to the decoded wallpaper if |update_wallpaper| is true.
273 // Otherwise, cache wallpaper to memory if not logged in.
274 void OnWallpaperDecoded(const std::string& email,
275 ash::WallpaperLayout layout,
276 bool update_wallpaper,
277 const UserImage& wallpaper);
279 // Generates thumbnail of custom wallpaper on wallpaper sequenced worker
280 // thread. If |persistent| is true, saves original custom image and resized
282 void ProcessCustomWallpaper(const std::string& user_id_hash,
284 const WallpaperInfo& info,
285 scoped_ptr<gfx::ImageSkia> image,
286 const UserImage::RawImage& raw_image);
288 // Record data for User Metrics Analysis.
289 void RecordUma(User::WallpaperType type, int index);
291 // Saves original custom wallpaper to |path| (absolute path) on filesystem
292 // and starts resizing operation of the custom wallpaper if necessary.
293 void SaveCustomWallpaper(const std::string& user_id_hash,
294 const base::FilePath& path,
295 ash::WallpaperLayout layout,
296 const UserImage& wallpaper);
298 // Saves wallpaper image raw |data| to |path| (absolute path) in file system.
299 void SaveWallpaperInternal(const base::FilePath& path, const char* data,
302 // Starts to load wallpaper at |wallpaper_path|. If |wallpaper_path| is the
303 // same as |current_wallpaper_path_|, do nothing. Must be called on UI thread.
304 void StartLoad(const std::string& email,
305 const WallpaperInfo& info,
306 bool update_wallpaper,
307 const base::FilePath& wallpaper_path);
309 // The number of loaded wallpapers.
310 int loaded_wallpapers_;
312 // Sequence token associated with wallpaper operations.
313 base::SequencedWorkerPool::SequenceToken sequence_token_;
315 // Wallpaper sequenced task runner.
316 scoped_refptr<base::SequencedTaskRunner> task_runner_;
318 // The file path of current loaded/loading custom/online wallpaper.
319 base::FilePath current_wallpaper_path_;
321 // Loads user wallpaper from its file.
322 scoped_refptr<UserImageLoader> wallpaper_loader_;
324 // Logged-in user wallpaper information.
325 WallpaperInfo current_user_wallpaper_info_;
327 // If non-NULL, used in place of the real command line.
328 CommandLine* command_line_for_testing_;
330 // Caches wallpapers of users. Accessed only on UI thread.
331 CustomWallpaperMap wallpaper_cache_;
333 // The last selected user on user pod row.
334 std::string last_selected_user_;
336 bool should_cache_wallpaper_;
338 scoped_ptr<CrosSettings::ObserverSubscription>
339 show_user_name_on_signin_subscription_;
341 base::WeakPtrFactory<WallpaperManager> weak_factory_;
343 content::NotificationRegistrar registrar_;
345 DISALLOW_COPY_AND_ASSIGN(WallpaperManager);
348 } // namespace chromeos
350 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_WALLPAPER_MANAGER_H_