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
;
41 ~WallpaperManager() override
;
43 // Get pointer to singleton WallpaperManager instance, create it if necessary.
44 static WallpaperManager
* Get();
46 // Returns the appropriate wallpaper resolution for all root windows.
47 WallpaperResolution
GetAppropriateResolution() override
;
49 // Indicates imminent shutdown, allowing the WallpaperManager to remove any
50 // observers it has registered.
51 void Shutdown() override
;
53 // Adds PowerManagerClient, TimeZoneSettings and CrosSettings observers.
54 void AddObservers() override
;
56 // Loads wallpaper asynchronously if the current wallpaper is not the
57 // wallpaper of logged in user.
58 void EnsureLoggedInUserWallpaperLoaded() override
;
60 // Initializes wallpaper. If logged in, loads user's wallpaper. If not logged
61 // in, uses a solid color wallpaper. If logged in as a stub user, uses an
63 void InitializeWallpaper() override
;
65 // NotificationObserver overrides:
66 void Observe(int type
,
67 const content::NotificationSource
& source
,
68 const content::NotificationDetails
& details
) override
;
70 // Removes all |user_id| related wallpaper info and saved wallpapers.
71 void RemoveUserWallpaperInfo(const std::string
& user_id
) override
;
73 // Called when the policy-set wallpaper has been fetched. Initiates decoding
74 // of the JPEG |data| with a callback to SetPolicyControlledWallpaper().
75 void OnPolicyFetched(const std::string
& policy
,
76 const std::string
& user_id
,
77 scoped_ptr
<std::string
> data
) override
;
79 // Saves custom wallpaper to file, post task to generate thumbnail and updates
80 // local state preferences. If |update_wallpaper| is false, don't change
81 // wallpaper but only update cache.
82 void SetCustomWallpaper(const std::string
& user_id
,
83 const std::string
& user_id_hash
,
84 const std::string
& file
,
85 wallpaper::WallpaperLayout layout
,
86 user_manager::User::WallpaperType type
,
87 const gfx::ImageSkia
& image
,
88 bool update_wallpaper
) override
;
90 // Sets wallpaper to default wallpaper (asynchronously with zero delay).
91 void SetDefaultWallpaperNow(const std::string
& user_id
) override
;
93 // Sets wallpaper to default wallpaper (asynchronously with default delay).
94 void SetDefaultWallpaperDelayed(const std::string
& user_id
) override
;
96 // Sets wallpaper to default.
97 void DoSetDefaultWallpaper(
98 const std::string
& user_id
,
99 wallpaper::MovableOnDestroyCallbackHolder on_finish
) override
;
101 // Sets selected wallpaper information for |user_id| and saves it to Local
102 // State if |is_persistent| is true.
103 void SetUserWallpaperInfo(const std::string
& user_id
,
104 const wallpaper::WallpaperInfo
& info
,
105 bool is_persistent
) override
;
107 // Creates new PendingWallpaper request (or updates currently pending).
108 void ScheduleSetUserWallpaper(const std::string
& user_id
,
109 bool delayed
) override
;
111 // Sets wallpaper to |image| (asynchronously with zero delay). If
112 // |update_wallpaper| is false, skip change wallpaper but only update cache.
113 void SetWallpaperFromImageSkia(const std::string
& user_id
,
114 const gfx::ImageSkia
& image
,
115 wallpaper::WallpaperLayout layout
,
116 bool update_wallpaper
) override
;
118 // Returns queue size.
119 size_t GetPendingListSizeForTesting() const override
;
121 // Overridden from user_manager::UserManager::UserSessionStateObserver:
122 void UserChangedChildStatus(user_manager::User
* user
) override
;
125 friend class TestApi
;
126 friend class WallpaperManagerBrowserTest
;
127 friend class WallpaperManagerBrowserTestDefaultWallpaper
;
128 friend class WallpaperManagerPolicyTest
;
130 // Returns modifiable PendingWallpaper.
131 // Returns pending_inactive_ or creates new PendingWallpaper if necessary.
132 PendingWallpaper
* GetPendingWallpaper(const std::string
& user_id
,
135 // This is called by PendingWallpaper when load is finished.
136 void RemovePendingWallpaperFromList(PendingWallpaper
* pending
);
138 // WallpaperManagerBase overrides:
139 void ClearObsoleteWallpaperPrefs() override
;
140 void InitializeRegisteredDeviceWallpaper() override
;
141 bool GetUserWallpaperInfo(const std::string
& user_id
,
142 wallpaper::WallpaperInfo
* info
) const override
;
143 void OnWallpaperDecoded(const std::string
& user_id
,
144 wallpaper::WallpaperLayout layout
,
145 bool update_wallpaper
,
146 wallpaper::MovableOnDestroyCallbackHolder on_finish
,
147 const user_manager::UserImage
& user_image
) override
;
148 void StartLoad(const std::string
& user_id
,
149 const wallpaper::WallpaperInfo
& info
,
150 bool update_wallpaper
,
151 const base::FilePath
& wallpaper_path
,
152 wallpaper::MovableOnDestroyCallbackHolder on_finish
) override
;
153 void SetCustomizedDefaultWallpaperAfterCheck(
154 const GURL
& wallpaper_url
,
155 const base::FilePath
& downloaded_file
,
156 scoped_ptr
<CustomizedWallpaperRescaledFiles
> rescaled_files
) override
;
157 void OnCustomizedDefaultWallpaperResized(
158 const GURL
& wallpaper_url
,
159 scoped_ptr
<CustomizedWallpaperRescaledFiles
> rescaled_files
,
160 scoped_ptr
<bool> success
,
161 scoped_ptr
<gfx::ImageSkia
> small_wallpaper_image
,
162 scoped_ptr
<gfx::ImageSkia
> large_wallpaper_image
) override
;
163 void OnDefaultWallpaperDecoded(
164 const base::FilePath
& path
,
165 const wallpaper::WallpaperLayout layout
,
166 scoped_ptr
<user_manager::UserImage
>* result
,
167 wallpaper::MovableOnDestroyCallbackHolder on_finish
,
168 const user_manager::UserImage
& user_image
) override
;
169 void StartLoadAndSetDefaultWallpaper(
170 const base::FilePath
& path
,
171 const wallpaper::WallpaperLayout layout
,
172 wallpaper::MovableOnDestroyCallbackHolder on_finish
,
173 scoped_ptr
<user_manager::UserImage
>* result_out
) override
;
174 void SetDefaultWallpaperPath(
175 const base::FilePath
& customized_default_wallpaper_file_small
,
176 scoped_ptr
<gfx::ImageSkia
> small_wallpaper_image
,
177 const base::FilePath
& customized_default_wallpaper_file_large
,
178 scoped_ptr
<gfx::ImageSkia
> large_wallpaper_image
) override
;
180 // Loads user wallpaper from its file.
181 scoped_refptr
<UserImageLoader
> wallpaper_loader_
;
183 scoped_ptr
<CrosSettings::ObserverSubscription
>
184 show_user_name_on_signin_subscription_
;
186 // Pointer to last inactive (waiting) entry of 'loading_' list.
187 // NULL when there is no inactive request.
188 PendingWallpaper
* pending_inactive_
;
190 // Owns PendingWallpaper.
191 // PendingWallpaper deletes itself from here on load complete.
192 // All pending will be finally deleted on destroy.
193 typedef std::vector
<scoped_refptr
<PendingWallpaper
> > PendingList
;
194 PendingList loading_
;
196 base::WeakPtrFactory
<WallpaperManager
> weak_factory_
;
197 DISALLOW_COPY_AND_ASSIGN(WallpaperManager
);
200 } // namespace chromeos
202 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_USERS_WALLPAPER_WALLPAPER_MANAGER_H_