- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / wallpaper_manager.h
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.
4
5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_WALLPAPER_MANAGER_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_WALLPAPER_MANAGER_H_
7
8 #include <string>
9
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"
25
26 class CommandLine;
27 class PrefRegistrySimple;
28
29 namespace base {
30 class SequencedTaskRunner;
31 }
32
33 namespace chromeos {
34
35 struct WallpaperInfo {
36   // Online wallpaper URL or file name of migrated wallpaper.
37   std::string file;
38   ash::WallpaperLayout layout;
39   User::WallpaperType type;
40   base::Time date;
41   bool operator==(const WallpaperInfo& other) {
42     return (file == other.file) && (layout == other.layout) &&
43         (type == other.type);
44   }
45 };
46
47 class WallpaperManagerBrowserTest;
48 class UserImage;
49
50 // Name of wallpaper sequence token.
51 extern const char kWallpaperSequenceTokenName[];
52
53 // File path suffices of resized small or large wallpaper.
54 // TODO(bshe): Use the same sub folder system as custom wallpapers use.
55 // crbug.com/174928
56 extern const char kSmallWallpaperSuffix[];
57 extern const char kLargeWallpaperSuffix[];
58
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[];
64
65 // This class maintains wallpapers for users who have logged into this Chrome
66 // OS device.
67 class WallpaperManager: public content::NotificationObserver {
68  public:
69   // For testing.
70   class TestApi {
71    public:
72     explicit TestApi(WallpaperManager* wallpaper_manager);
73     virtual ~TestApi();
74
75     base::FilePath current_wallpaper_path();
76
77    private:
78     WallpaperManager* wallpaper_manager_;  // not owned
79
80     DISALLOW_COPY_AND_ASSIGN(TestApi);
81   };
82
83   static WallpaperManager* Get();
84
85   WallpaperManager();
86   virtual ~WallpaperManager();
87
88   void set_command_line_for_testing(CommandLine* command_line) {
89     command_line_for_testing_ = command_line;
90   }
91
92   // Indicates imminent shutdown, allowing the WallpaperManager to remove any
93   // observers it has registered.
94   void Shutdown();
95
96   // Registers wallpaper manager preferences.
97   static void RegisterPrefs(PrefRegistrySimple* registry);
98
99   // Adds PowerManagerClient, TimeZoneSettings and CrosSettings observers.
100   void AddObservers();
101
102   // Loads wallpaper asynchronously if the current wallpaper is not the
103   // wallpaper of logged in user.
104   void EnsureLoggedInUserWallpaperLoaded();
105
106   // Clears ONLINE and CUSTOM wallpaper cache.
107   void ClearWallpaperCache();
108
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);
114
115   // Gets encoded wallpaper from cache. Returns true if success.
116   bool GetWallpaperFromCache(const std::string& email,
117                              gfx::ImageSkia* wallpaper);
118
119   // Returns filepath to save original custom wallpaper for the given user.
120   base::FilePath GetOriginalWallpaperPathForUser(const std::string& username);
121
122   // Gets wallpaper information of logged in user.
123   bool GetLoggedInUserWallpaperInfo(WallpaperInfo* info);
124
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
127   // empty wallpaper.
128   void InitializeWallpaper();
129
130   // NotificationObserver overrides:
131   virtual void Observe(int type,
132                        const content::NotificationSource& source,
133                        const content::NotificationDetails& details) OVERRIDE;
134
135   // Removes all |email| related wallpaper info and saved wallpapers.
136   void RemoveUserWallpaperInfo(const std::string& email);
137
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,
142                        int preferred_width,
143                        int preferred_height,
144                        scoped_refptr<base::RefCountedBytes>* output);
145
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,
152                               int preferred_width,
153                               int preferred_height);
154
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);
163
164   // Sets wallpaper to default wallpaper.
165   void SetDefaultWallpaper();
166
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);
170
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,
175                             bool is_persistent);
176
177   // Sets last selected user on user pod row.
178   void SetLastSelectedUser(const std::string& last_selected_user);
179
180   // Sets |email|'s wallpaper.
181   void SetUserWallpaper(const std::string& email);
182
183   // Sets wallpaper to |wallpaper|.
184   void SetWallpaperFromImageSkia(const gfx::ImageSkia& wallpaper,
185                                  ash::WallpaperLayout layout);
186
187   // Updates current wallpaper. It may switch the size of wallpaper based on the
188   // current display's resolution.
189   void UpdateWallpaper();
190
191  private:
192   friend class TestApi;
193   friend class WallpaperManagerBrowserTest;
194   typedef std::map<std::string, gfx::ImageSkia> CustomWallpaperMap;
195
196   // The number of wallpapers have loaded. For test only.
197   int loaded_wallpapers() const { return loaded_wallpapers_; }
198
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();
210
211   // Caches |email|'s wallpaper to memory.
212   void CacheUserWallpaper(const std::string& email);
213
214   // Clears all obsolete wallpaper prefs from old version wallpaper pickers.
215   void ClearObsoleteWallpaperPrefs();
216
217   // Deletes everything else except |path| in the same directory.
218   void DeleteAllExcept(const base::FilePath& path);
219
220   // Deletes a list of wallpaper files in |file_list|.
221   void DeleteWallpaperInList(const std::vector<base::FilePath>& file_list);
222
223   // Deletes all |email| related custom wallpapers and directories.
224   void DeleteUserWallpapers(const std::string& email,
225                             const std::string& path_to_file);
226
227   // Creates all new custom wallpaper directories for |user_id_hash| if not
228   // exist.
229   void EnsureCustomWallpaperDirectories(const std::string& user_id_hash);
230
231   // Gets the CommandLine representing the current process's command line.
232   CommandLine* GetComandLine();
233
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();
237
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);
243
244   // Moves custom wallpapers from |email| directory to |user_id_hash|
245   // directory.
246   void MoveCustomWallpapersOnWorker(const std::string& email,
247                                     const std::string& user_id_hash);
248
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);
253
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
257   // ready.
258   void MoveLoggedInUserCustomWallpaper();
259
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);
267
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);
271
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);
278
279   // Generates thumbnail of custom wallpaper on wallpaper sequenced worker
280   // thread. If |persistent| is true, saves original custom image and resized
281   // images to disk.
282   void ProcessCustomWallpaper(const std::string& user_id_hash,
283                               bool persistent,
284                               const WallpaperInfo& info,
285                               scoped_ptr<gfx::ImageSkia> image,
286                               const UserImage::RawImage& raw_image);
287
288   // Record data for User Metrics Analysis.
289   void RecordUma(User::WallpaperType type, int index);
290
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);
297
298   // Saves wallpaper image raw |data| to |path| (absolute path) in file system.
299   void SaveWallpaperInternal(const base::FilePath& path, const char* data,
300                              int size);
301
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);
308
309   // The number of loaded wallpapers.
310   int loaded_wallpapers_;
311
312   // Sequence token associated with wallpaper operations.
313   base::SequencedWorkerPool::SequenceToken sequence_token_;
314
315   // Wallpaper sequenced task runner.
316   scoped_refptr<base::SequencedTaskRunner> task_runner_;
317
318   // The file path of current loaded/loading custom/online wallpaper.
319   base::FilePath current_wallpaper_path_;
320
321   // Loads user wallpaper from its file.
322   scoped_refptr<UserImageLoader> wallpaper_loader_;
323
324   // Logged-in user wallpaper information.
325   WallpaperInfo current_user_wallpaper_info_;
326
327   // If non-NULL, used in place of the real command line.
328   CommandLine* command_line_for_testing_;
329
330   // Caches wallpapers of users. Accessed only on UI thread.
331   CustomWallpaperMap wallpaper_cache_;
332
333   // The last selected user on user pod row.
334   std::string last_selected_user_;
335
336   bool should_cache_wallpaper_;
337
338   scoped_ptr<CrosSettings::ObserverSubscription>
339       show_user_name_on_signin_subscription_;
340
341   base::WeakPtrFactory<WallpaperManager> weak_factory_;
342
343   content::NotificationRegistrar registrar_;
344
345   DISALLOW_COPY_AND_ASSIGN(WallpaperManager);
346 };
347
348 }  // namespace chromeos
349
350 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_WALLPAPER_MANAGER_H_