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_WALLPAPER_WALLPAPER_MANAGER_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_USERS_WALLPAPER_WALLPAPER_MANAGER_H_
12 #include "ash/desktop_background/desktop_background_controller.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/ref_counted_memory.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "base/threading/sequenced_worker_pool.h"
19 #include "base/time/time.h"
20 #include "chrome/browser/chromeos/login/users/avatar/user_image_loader.h"
21 #include "chrome/browser/chromeos/settings/cros_settings.h"
22 #include "components/user_manager/user.h"
23 #include "components/user_manager/user_image/user_image.h"
24 #include "components/user_manager/user_manager.h"
25 #include "components/wallpaper/wallpaper_layout.h"
26 #include "components/wallpaper/wallpaper_manager_base.h"
27 #include "content/public/browser/notification_observer.h"
28 #include "content/public/browser/notification_registrar.h"
29 #include "third_party/icu/source/i18n/unicode/timezone.h"
30 #include "ui/gfx/image/image_skia.h"
34 class WallpaperManager
:
35 public wallpaper::WallpaperManagerBase
,
36 public user_manager::UserManager::UserSessionStateObserver
{
38 class PendingWallpaper
;
40 ~WallpaperManager() override
;
42 // Creates an instance of Wallpaper Manager. If there is no instance, create
43 // one. Otherwise, returns the existing instance.
44 static void Initialize();
46 // Gets pointer to singleton WallpaperManager instance.
47 static WallpaperManager
* Get();
49 // Deletes the existing instance of WallpaperManager. Allows the
50 // WallpaperManager to remove any observers it has registered.
51 static void Shutdown();
53 // Returns the appropriate wallpaper resolution for all root windows.
54 WallpaperResolution
GetAppropriateResolution() override
;
56 // Adds PowerManagerClient, TimeZoneSettings and CrosSettings observers.
57 void AddObservers() override
;
59 // Loads wallpaper asynchronously if the current wallpaper is not the
60 // wallpaper of logged in user.
61 void EnsureLoggedInUserWallpaperLoaded() override
;
63 // Initializes wallpaper. If logged in, loads user's wallpaper. If not logged
64 // in, uses a solid color wallpaper. If logged in as a stub user, uses an
66 void InitializeWallpaper() override
;
68 // NotificationObserver overrides:
69 void Observe(int type
,
70 const content::NotificationSource
& source
,
71 const content::NotificationDetails
& details
) override
;
73 // Removes all |user_id| related wallpaper info and saved wallpapers.
74 void RemoveUserWallpaperInfo(const std::string
& user_id
) override
;
76 // Called when the policy-set wallpaper has been fetched. Initiates decoding
77 // of the JPEG |data| with a callback to SetPolicyControlledWallpaper().
78 void OnPolicyFetched(const std::string
& policy
,
79 const std::string
& user_id
,
80 scoped_ptr
<std::string
> data
) override
;
82 // Saves custom wallpaper to file, post task to generate thumbnail and updates
83 // local state preferences. If |update_wallpaper| is false, don't change
84 // wallpaper but only update cache.
85 void SetCustomWallpaper(const std::string
& user_id
,
86 const std::string
& user_id_hash
,
87 const std::string
& file
,
88 wallpaper::WallpaperLayout layout
,
89 user_manager::User::WallpaperType type
,
90 const gfx::ImageSkia
& image
,
91 bool update_wallpaper
) override
;
93 // Sets wallpaper to default wallpaper (asynchronously with zero delay).
94 void SetDefaultWallpaperNow(const std::string
& user_id
) override
;
96 // Sets wallpaper to default wallpaper (asynchronously with default delay).
97 void SetDefaultWallpaperDelayed(const std::string
& user_id
) override
;
99 // Sets wallpaper to default.
100 void DoSetDefaultWallpaper(
101 const std::string
& user_id
,
102 wallpaper::MovableOnDestroyCallbackHolder on_finish
) override
;
104 // Sets selected wallpaper information for |user_id| and saves it to Local
105 // State if |is_persistent| is true.
106 void SetUserWallpaperInfo(const std::string
& user_id
,
107 const wallpaper::WallpaperInfo
& info
,
108 bool is_persistent
) override
;
110 // Creates new PendingWallpaper request (or updates currently pending).
111 void ScheduleSetUserWallpaper(const std::string
& user_id
,
112 bool delayed
) override
;
114 // Sets wallpaper to |image| (asynchronously with zero delay). If
115 // |update_wallpaper| is false, skip change wallpaper but only update cache.
116 void SetWallpaperFromImageSkia(const std::string
& user_id
,
117 const gfx::ImageSkia
& image
,
118 wallpaper::WallpaperLayout layout
,
119 bool update_wallpaper
) override
;
121 // Returns queue size.
122 size_t GetPendingListSizeForTesting() const override
;
124 // Overridden from user_manager::UserManager::UserSessionStateObserver:
125 void UserChangedChildStatus(user_manager::User
* user
) override
;
128 friend class TestApi
;
129 friend class WallpaperManagerBrowserTest
;
130 friend class WallpaperManagerBrowserTestDefaultWallpaper
;
131 friend class WallpaperManagerPolicyTest
;
135 // Returns modifiable PendingWallpaper.
136 // Returns pending_inactive_ or creates new PendingWallpaper if necessary.
137 PendingWallpaper
* GetPendingWallpaper(const std::string
& user_id
,
140 // This is called by PendingWallpaper when load is finished.
141 void RemovePendingWallpaperFromList(PendingWallpaper
* pending
);
143 // WallpaperManagerBase overrides:
144 void InitializeRegisteredDeviceWallpaper() override
;
145 bool GetUserWallpaperInfo(const std::string
& user_id
,
146 wallpaper::WallpaperInfo
* info
) const override
;
147 void OnWallpaperDecoded(const std::string
& user_id
,
148 wallpaper::WallpaperLayout layout
,
149 bool update_wallpaper
,
150 wallpaper::MovableOnDestroyCallbackHolder on_finish
,
151 const user_manager::UserImage
& user_image
) override
;
152 void StartLoad(const std::string
& user_id
,
153 const wallpaper::WallpaperInfo
& info
,
154 bool update_wallpaper
,
155 const base::FilePath
& wallpaper_path
,
156 wallpaper::MovableOnDestroyCallbackHolder on_finish
) override
;
157 void SetCustomizedDefaultWallpaperAfterCheck(
158 const GURL
& wallpaper_url
,
159 const base::FilePath
& downloaded_file
,
160 scoped_ptr
<CustomizedWallpaperRescaledFiles
> rescaled_files
) override
;
161 void OnCustomizedDefaultWallpaperResized(
162 const GURL
& wallpaper_url
,
163 scoped_ptr
<CustomizedWallpaperRescaledFiles
> rescaled_files
,
164 scoped_ptr
<bool> success
,
165 scoped_ptr
<gfx::ImageSkia
> small_wallpaper_image
,
166 scoped_ptr
<gfx::ImageSkia
> large_wallpaper_image
) override
;
167 void OnDefaultWallpaperDecoded(
168 const base::FilePath
& path
,
169 const wallpaper::WallpaperLayout layout
,
170 scoped_ptr
<user_manager::UserImage
>* result
,
171 wallpaper::MovableOnDestroyCallbackHolder on_finish
,
172 const user_manager::UserImage
& user_image
) override
;
173 void StartLoadAndSetDefaultWallpaper(
174 const base::FilePath
& path
,
175 const wallpaper::WallpaperLayout layout
,
176 wallpaper::MovableOnDestroyCallbackHolder on_finish
,
177 scoped_ptr
<user_manager::UserImage
>* result_out
) override
;
178 void SetDefaultWallpaperPath(
179 const base::FilePath
& customized_default_wallpaper_file_small
,
180 scoped_ptr
<gfx::ImageSkia
> small_wallpaper_image
,
181 const base::FilePath
& customized_default_wallpaper_file_large
,
182 scoped_ptr
<gfx::ImageSkia
> large_wallpaper_image
) override
;
184 // Loads user wallpaper from its file.
185 scoped_refptr
<UserImageLoader
> wallpaper_loader_
;
187 scoped_ptr
<CrosSettings::ObserverSubscription
>
188 show_user_name_on_signin_subscription_
;
190 // Pointer to last inactive (waiting) entry of 'loading_' list.
191 // NULL when there is no inactive request.
192 PendingWallpaper
* pending_inactive_
;
194 // Owns PendingWallpaper.
195 // PendingWallpaper deletes itself from here on load complete.
196 // All pending will be finally deleted on destroy.
197 typedef std::vector
<scoped_refptr
<PendingWallpaper
> > PendingList
;
198 PendingList loading_
;
200 base::WeakPtrFactory
<WallpaperManager
> weak_factory_
;
201 DISALLOW_COPY_AND_ASSIGN(WallpaperManager
);
204 } // namespace chromeos
206 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_USERS_WALLPAPER_WALLPAPER_MANAGER_H_