OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/login/wallpaper_manager.h" | 5 #include "chrome/browser/chromeos/login/wallpaper_manager.h" |
6 | 6 |
7 #include "ash/desktop_background/desktop_background_controller.h" | 7 #include "ash/desktop_background/desktop_background_controller.h" |
8 #include "ash/desktop_background/desktop_background_resources.h" | |
9 #include "ash/shell.h" | 8 #include "ash/shell.h" |
9 #include "base/command_line.h" | |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/file_path.h" | |
12 #include "base/file_util.h" | |
13 #include "base/path_service.h" | |
14 #include "base/string_number_conversions.h" | |
15 #include "base/string_util.h" | |
11 #include "base/time.h" | 16 #include "base/time.h" |
12 #include "base/values.h" | 17 #include "base/values.h" |
13 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/chromeos/cros_settings.h" | 19 #include "chrome/browser/chromeos/cros_settings.h" |
15 #include "chrome/browser/chromeos/login/user.h" | 20 #include "chrome/browser/chromeos/login/user.h" |
16 #include "chrome/browser/chromeos/login/user_manager.h" | 21 #include "chrome/browser/chromeos/login/user_manager.h" |
17 #include "chrome/browser/chromeos/login/user_manager_impl.h" | 22 #include "chrome/browser/chromeos/login/user_manager_impl.h" |
23 #include "chrome/browser/chromeos/login/wizard_controller.h" | |
18 #include "chrome/browser/prefs/pref_service.h" | 24 #include "chrome/browser/prefs/pref_service.h" |
25 #include "chrome/browser/prefs/scoped_user_pref_update.h" | |
26 #include "chrome/common/chrome_notification_types.h" | |
27 #include "chrome/common/chrome_paths.h" | |
28 #include "chrome/common/chrome_switches.h" | |
19 #include "chromeos/dbus/dbus_thread_manager.h" | 29 #include "chromeos/dbus/dbus_thread_manager.h" |
20 #include "chromeos/dbus/power_manager_client.h" | 30 #include "chromeos/dbus/power_manager_client.h" |
31 #include "content/public/browser/browser_thread.h" | |
32 #include "content/public/browser/notification_service.h" | |
33 #include "skia/ext/image_operations.h" | |
34 #include "third_party/zlib/zlib.h" | |
35 #include "ui/gfx/codec/png_codec.h" | |
36 #include "ui/gfx/skia_util.h" | |
37 | |
38 using content::BrowserThread; | |
21 | 39 |
22 namespace { | 40 namespace { |
41 | |
23 const int kWallpaperUpdateIntervalSec = 24 * 60 * 60; | 42 const int kWallpaperUpdateIntervalSec = 24 * 60 * 60; |
43 | |
44 const char kWallpaperTypeNodeName[] = "type"; | |
45 const char kWallpaperIndexNodeName[] = "index"; | |
46 const char kWallpaperDateNodeName[] = "date"; | |
47 | |
48 const int kThumbnailWidth = 128; | |
49 const int kThumbnailHeight = 80; | |
50 | |
51 // Default wallpaper index used in OOBE (first boot). | |
52 // Defined here because Chromium default index differs. | |
53 // Also see ash::WallpaperInfo kDefaultWallpapers in | |
54 // desktop_background_resources.cc | |
55 #if defined(GOOGLE_CHROME_BUILD) | |
56 const int kDefaultOOBEWallpaperIndex = 16; // IDR_AURA_WALLPAPERS_3_URBAN0 | |
57 #else | |
58 const int kDefaultOOBEWallpaperIndex = 0; // IDR_AURA_WALLPAPERS_ROMAINGUY_0 | |
59 #endif | |
60 | |
24 } // namespace | 61 } // namespace |
25 | 62 |
26 namespace chromeos { | 63 namespace chromeos { |
27 | 64 |
28 static WallpaperManager* g_wallpaper_manager = NULL; | 65 static WallpaperManager* g_wallpaper_manager = NULL; |
29 | 66 |
30 // WallpaperManager, public: --------------------------------------------------- | 67 // WallpaperManager, public: --------------------------------------------------- |
31 | 68 |
32 // static | 69 // static |
33 WallpaperManager* WallpaperManager::Get() { | 70 WallpaperManager* WallpaperManager::Get() { |
34 if (!g_wallpaper_manager) | 71 if (!g_wallpaper_manager) |
35 g_wallpaper_manager = new WallpaperManager(); | 72 g_wallpaper_manager = new WallpaperManager(); |
36 return g_wallpaper_manager; | 73 return g_wallpaper_manager; |
37 } | 74 } |
38 | 75 |
39 WallpaperManager::WallpaperManager() : last_selected_user_("") { | 76 WallpaperManager::WallpaperManager() |
77 : ALLOW_THIS_IN_INITIALIZER_LIST(wallpaper_loader_(new WallpaperLoader)), | |
78 current_user_wallpaper_type_(User::UNKNOWN), | |
79 ALLOW_THIS_IN_INITIALIZER_LIST(current_user_wallpaper_index_( | |
80 ash::GetInvalidWallpaperIndex())), | |
81 last_selected_user_(""), | |
82 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
40 system::TimezoneSettings::GetInstance()->AddObserver(this); | 83 system::TimezoneSettings::GetInstance()->AddObserver(this); |
41 RestartTimer(); | 84 RestartTimer(); |
85 registrar_.Add(this, | |
86 chrome::NOTIFICATION_LOGIN_USER_CHANGED, | |
87 content::NotificationService::AllSources()); | |
42 } | 88 } |
43 | 89 |
44 void WallpaperManager::AddPowerManagerClientObserver() { | 90 void WallpaperManager::AddPowerManagerClientObserver() { |
45 if (!DBusThreadManager::Get()->GetPowerManagerClient()->HasObserver(this)) | 91 if (!DBusThreadManager::Get()->GetPowerManagerClient()->HasObserver(this)) |
46 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this); | 92 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this); |
47 } | 93 } |
48 | 94 |
95 void WallpaperManager::CacheIfCustomWallpaper(const std::string& email) { | |
96 User::WallpaperType type; | |
97 int index; | |
98 base::Time date; | |
99 GetUserWallpaperProperties(email, &type, &index, &date); | |
100 if (type == User::CUSTOMIZED) { | |
101 // Uses WeakPtr here to make the request cancelable. | |
102 LoadCustomWallpaperFromFile(email, | |
103 base::Bind(&WallpaperManager::CacheWallpaper, | |
104 weak_factory_.GetWeakPtr(), email)); | |
105 } | |
106 } | |
107 | |
108 void WallpaperManager::EnsureLoggedInUserWallpaperLoaded() { | |
109 User::WallpaperType type; | |
110 int index; | |
111 base::Time last_modification_date; | |
112 GetLoggedInUserWallpaperProperties(&type, &index, &last_modification_date); | |
113 | |
114 if (type != current_user_wallpaper_type_ || | |
115 index != current_user_wallpaper_index_) { | |
116 SetUserWallpaper(UserManager::Get()->GetLoggedInUser().email()); | |
117 } | |
118 } | |
119 | |
120 void WallpaperManager::FetchCustomWallpaper(const std::string& email) { | |
121 User::WallpaperType type; | |
122 int index; | |
123 base::Time date; | |
124 GetUserWallpaperProperties(email, &type, &index, &date); | |
125 ash::WallpaperLayout layout = static_cast<ash::WallpaperLayout>(index); | |
126 LoadCustomWallpaperFromFile(email, | |
127 base::Bind(&WallpaperManager::FetchWallpaper, | |
128 base::Unretained(this), | |
129 email, layout)); | |
130 } | |
131 | |
132 bool WallpaperManager::GetCustomWallpaperFromCache(const std::string& email, | |
133 SkBitmap* wallpaper) { | |
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
135 CustomWallpaperMap::const_iterator it = custom_wallpaper_cache_.find(email); | |
136 if (it != custom_wallpaper_cache_.end()) { | |
137 *wallpaper = (*it).second; | |
138 return true; | |
139 } | |
140 return false; | |
141 } | |
142 | |
143 FilePath WallpaperManager::GetWallpaperPathForUser(const std::string& username, | |
144 bool is_thumbnail) { | |
145 std::string filename = username + | |
146 (is_thumbnail ? "_wallpaper_thumb.png" : "_wallpaper.png"); | |
147 FilePath user_data_dir; | |
148 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | |
149 return user_data_dir.AppendASCII(filename); | |
150 } | |
151 | |
152 SkBitmap WallpaperManager::GetCustomWallpaperThumbnail( | |
153 const std::string& email) { | |
154 CustomWallpaperMap::const_iterator it = | |
155 custom_wallpaper_thumbnail_cache_.find(email); | |
156 if (it != custom_wallpaper_cache_.end()) | |
157 return (*it).second; | |
158 else | |
159 return SkBitmap(); | |
160 } | |
161 | |
162 void WallpaperManager::GetLoggedInUserWallpaperProperties( | |
163 User::WallpaperType* type, | |
Ivan Korotkov
2012/07/31 21:18:16
It looks like there're many functions taking these
bshe
2012/08/01 15:07:35
Will do.
On 2012/07/31 21:18:16, Ivan Korotkov wr
| |
164 int* index, | |
165 base::Time* last_modification_date) { | |
166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
167 | |
168 if (UserManager::Get()->IsLoggedInAsStub()) { | |
169 *type = current_user_wallpaper_type_ = User::DEFAULT; | |
170 *index = current_user_wallpaper_index_ = ash::GetInvalidWallpaperIndex(); | |
171 return; | |
172 } | |
173 | |
174 GetUserWallpaperProperties(UserManager::Get()->GetLoggedInUser().email(), | |
175 type, index, last_modification_date); | |
176 } | |
177 | |
178 void WallpaperManager::InitializeWallpaper() { | |
179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
180 UserManager* user_manager = UserManager::Get(); | |
181 if (!user_manager->IsUserLoggedIn()) { | |
182 if (!CommandLine::ForCurrentProcess()->HasSwitch( | |
183 switches::kDisableNewOobe)) { | |
184 if (!WizardController::IsDeviceRegistered() && | |
185 !WizardController::IsZeroDelayEnabled()) { | |
186 // TODO(nkostylev): Add switch to disable wallpaper transition on OOBE. | |
187 // Should be used on test images so that they are not slowed down. | |
188 ash::Shell::GetInstance()->desktop_background_controller()-> | |
189 SetDefaultWallpaper(kDefaultOOBEWallpaperIndex); | |
190 } else { | |
191 bool show_users = true; | |
192 bool result = CrosSettings::Get()->GetBoolean( | |
193 kAccountsPrefShowUserNamesOnSignIn, &show_users); | |
194 DCHECK(result) << "Unable to fetch setting " | |
195 << kAccountsPrefShowUserNamesOnSignIn; | |
196 if (!show_users) { | |
197 ash::Shell::GetInstance()->desktop_background_controller()-> | |
198 SetDefaultWallpaper(ash::GetSolidColorIndex()); | |
199 } | |
200 } | |
201 } | |
202 return; | |
203 } | |
204 SetUserWallpaper(user_manager->GetLoggedInUser().email()); | |
205 } | |
206 | |
207 void WallpaperManager::Observe(int type, | |
208 const content::NotificationSource& source, | |
209 const content::NotificationDetails& details) { | |
210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
211 if (type == chrome::NOTIFICATION_LOGIN_USER_CHANGED) { | |
212 // Cancel callback for previous cache requests. | |
213 weak_factory_.InvalidateWeakPtrs(); | |
214 custom_wallpaper_cache_.clear(); | |
215 } | |
216 } | |
217 | |
49 void WallpaperManager::RestartTimer() { | 218 void WallpaperManager::RestartTimer() { |
50 timer_.Stop(); | 219 timer_.Stop(); |
51 base::Time midnight = base::Time::Now().LocalMidnight(); | 220 base::Time midnight = base::Time::Now().LocalMidnight(); |
52 base::TimeDelta interval = base::Time::Now() - midnight; | 221 base::TimeDelta interval = base::Time::Now() - midnight; |
53 int64 remaining_seconds = kWallpaperUpdateIntervalSec - interval.InSeconds(); | 222 int64 remaining_seconds = kWallpaperUpdateIntervalSec - interval.InSeconds(); |
54 if (remaining_seconds <= 0) { | 223 if (remaining_seconds <= 0) { |
55 BatchUpdateWallpaper(); | 224 BatchUpdateWallpaper(); |
56 } else { | 225 } else { |
57 // Set up a one shot timer which will batch update wallpaper at midnight. | 226 // Set up a one shot timer which will batch update wallpaper at midnight. |
58 timer_.Start(FROM_HERE, | 227 timer_.Start(FROM_HERE, |
59 base::TimeDelta::FromSeconds(remaining_seconds), | 228 base::TimeDelta::FromSeconds(remaining_seconds), |
60 this, | 229 this, |
61 &WallpaperManager::BatchUpdateWallpaper); | 230 &WallpaperManager::BatchUpdateWallpaper); |
62 } | 231 } |
63 } | 232 } |
64 | 233 |
234 void WallpaperManager::SaveUserWallpaperProperties(const std::string& email, | |
235 User::WallpaperType type, | |
236 int index) { | |
237 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
238 | |
239 current_user_wallpaper_type_ = type; | |
240 current_user_wallpaper_index_ = index; | |
241 // Ephemeral users can not save data to local state. We just cache the index | |
242 // in memory for them. | |
243 if (UserManager::Get()->IsCurrentUserEphemeral()) { | |
244 return; | |
245 } | |
246 | |
247 PrefService* local_state = g_browser_process->local_state(); | |
248 DictionaryPrefUpdate wallpaper_update(local_state, | |
249 UserManager::kUserWallpapersProperties); | |
250 | |
251 base::DictionaryValue* wallpaper_properties = new base::DictionaryValue(); | |
252 wallpaper_properties->Set(kWallpaperTypeNodeName, | |
253 new base::FundamentalValue(type)); | |
254 wallpaper_properties->Set(kWallpaperIndexNodeName, | |
255 new base::FundamentalValue(index)); | |
256 wallpaper_properties->SetString(kWallpaperDateNodeName, | |
257 base::Int64ToString(base::Time::Now().LocalMidnight().ToInternalValue())); | |
258 wallpaper_update->SetWithoutPathExpansion(email, wallpaper_properties); | |
259 } | |
260 | |
261 void WallpaperManager::SetUserWallpaperFromFile( | |
262 const std::string& username, | |
263 const FilePath& path, | |
264 ash::WallpaperLayout layout, | |
265 base::WeakPtr<WallpaperDelegate> delegate) { | |
266 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
267 | |
268 // TODO(bshe): Refactor GetWallpaperPathForUser to this class. | |
269 std::string wallpaper_path = | |
270 GetWallpaperPathForUser(username, false).value(); | |
271 // need to expose ephemeral user. | |
272 bool save_raw_file = true; | |
273 | |
274 // For wallpapers, save the image without resizing. | |
275 wallpaper_loader_->Start( | |
276 path.value(), save_raw_file, wallpaper_path, | |
277 base::Bind(&WallpaperManager::SetCallback, | |
278 base::Unretained(this), username, layout, User::CUSTOMIZED, | |
279 delegate)); | |
280 } | |
281 | |
282 void WallpaperManager::SetInitialUserWallpaper(const std::string& username) { | |
283 current_user_wallpaper_type_ = User::DEFAULT; | |
284 // TODO(bshe): Ideally, wallpaper should start to load as soon as user click | |
285 // "Add user". | |
286 if (username == kGuestUser) | |
287 current_user_wallpaper_index_ = ash::GetGuestWallpaperIndex(); | |
288 else | |
289 current_user_wallpaper_index_ = ash::GetDefaultWallpaperIndex(); | |
290 SaveUserWallpaperProperties(username, | |
291 current_user_wallpaper_type_, | |
292 current_user_wallpaper_index_); | |
293 | |
294 // Some browser tests do not have shell instance. And it is not necessary to | |
295 // create a wallpaper for these tests. Add HasInstance check to prevent tests | |
296 // crash and speed up the tests by avoid loading wallpaper. | |
297 if (ash::Shell::HasInstance()) { | |
298 ash::Shell::GetInstance()->desktop_background_controller()-> | |
299 SetDefaultWallpaper(current_user_wallpaper_index_); | |
300 } | |
301 } | |
302 | |
65 void WallpaperManager::SetLastSelectedUser( | 303 void WallpaperManager::SetLastSelectedUser( |
66 const std::string& last_selected_user) { | 304 const std::string& last_selected_user) { |
67 last_selected_user_ = last_selected_user; | 305 last_selected_user_ = last_selected_user; |
68 } | 306 } |
69 | 307 |
70 void WallpaperManager::SetWallpaperFromFile(std::string email, | 308 void WallpaperManager::SetUserWallpaper(const std::string& email) { |
71 const std::string& path, | 309 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
72 ash::WallpaperLayout layout) { | 310 if (UserManager::Get()->IsKnownUser(email)) { |
73 image_loader_->Start( | 311 User::WallpaperType type; |
74 path, 0, false, | 312 int index; |
75 base::Bind(&WallpaperManager::OnCustomWallpaperLoaded, | 313 base::Time date; |
76 base::Unretained(this), email, layout)); | 314 GetUserWallpaperProperties(email, &type, &index, &date); |
315 if (type == User::DAILY && date != base::Time::Now().LocalMidnight()) { | |
316 index = ash::GetNextWallpaperIndex(index); | |
317 SaveUserWallpaperProperties(email, User::DAILY, index); | |
318 } else if (type == User::CUSTOMIZED) { | |
319 SkBitmap custom_wallpaper; | |
320 if (GetCustomWallpaperFromCache(email, &custom_wallpaper)) { | |
321 // In customized mode, we use index pref to save the user selected | |
322 // wallpaper layout. See function SaveWallpaperToLocalState(). | |
323 ash::WallpaperLayout layout = static_cast<ash::WallpaperLayout>(index); | |
324 ash::Shell::GetInstance()->desktop_background_controller()-> | |
325 SetCustomWallpaper(custom_wallpaper, layout); | |
326 } else { | |
327 FetchCustomWallpaper(email); | |
328 } | |
329 return; | |
330 } | |
331 ash::Shell::GetInstance()->desktop_background_controller()-> | |
332 SetDefaultWallpaper(index); | |
333 SetLastSelectedUser(email); | |
334 } | |
77 } | 335 } |
78 | 336 |
79 void WallpaperManager::UserDeselected() { | 337 void WallpaperManager::OnUserDeselected() { |
80 if (!UserManager::Get()->IsUserLoggedIn()) { | 338 if (!UserManager::Get()->IsUserLoggedIn()) { |
81 // This will set default login wallpaper (#fefefe). | 339 // This will set default login wallpaper (#fefefe). |
82 ash::Shell::GetInstance()->desktop_background_controller()-> | 340 ash::Shell::GetInstance()->desktop_background_controller()-> |
83 SetDefaultWallpaper(ash::GetSolidColorIndex()); | 341 SetDefaultWallpaper(ash::GetSolidColorIndex()); |
84 } | 342 } |
85 } | 343 } |
86 | 344 |
345 void WallpaperManager::OnUserSelected(const std::string& email) { | |
346 SetUserWallpaper(email); | |
347 } | |
348 | |
87 // WallpaperManager, private: -------------------------------------------------- | 349 // WallpaperManager, private: -------------------------------------------------- |
88 | 350 |
89 WallpaperManager::~WallpaperManager() { | 351 WallpaperManager::~WallpaperManager() { |
90 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this); | 352 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this); |
91 system::TimezoneSettings::GetInstance()->RemoveObserver(this); | 353 system::TimezoneSettings::GetInstance()->RemoveObserver(this); |
92 } | 354 } |
93 | 355 |
94 void WallpaperManager::OnCustomWallpaperLoaded(const std::string& email, | 356 void WallpaperManager::OnCustomWallpaperLoaded(const std::string& email, |
95 ash::WallpaperLayout layout, | 357 ash::WallpaperLayout layout, |
96 const UserImage& user_image) { | 358 const UserImage& user_image) { |
97 const SkBitmap& wallpaper = user_image.image(); | 359 const SkBitmap& wallpaper = user_image.image(); |
98 ash::Shell::GetInstance()->desktop_background_controller()-> | 360 ash::Shell::GetInstance()->desktop_background_controller()-> |
99 SetCustomWallpaper(wallpaper, layout); | 361 SetCustomWallpaper(wallpaper, layout); |
100 } | 362 } |
101 | 363 |
102 void WallpaperManager::BatchUpdateWallpaper() { | 364 void WallpaperManager::BatchUpdateWallpaper() { |
103 PrefService* local_state = g_browser_process->local_state(); | 365 PrefService* local_state = g_browser_process->local_state(); |
104 UserManager* user_manager = UserManager::Get(); | 366 UserManager* user_manager = UserManager::Get(); |
105 bool show_users = true; | 367 bool show_users = true; |
106 CrosSettings::Get()->GetBoolean( | 368 CrosSettings::Get()->GetBoolean( |
107 kAccountsPrefShowUserNamesOnSignIn, &show_users); | 369 kAccountsPrefShowUserNamesOnSignIn, &show_users); |
108 if (local_state) { | 370 if (local_state) { |
109 User::WallpaperType type; | 371 User::WallpaperType type; |
110 int index = 0; | 372 int index = 0; |
111 base::Time last_modification_date; | 373 base::Time last_modification_date; |
112 const UserList& users = user_manager->GetUsers(); | 374 const UserList& users = user_manager->GetUsers(); |
113 for (UserList::const_iterator it = users.begin(); | 375 for (UserList::const_iterator it = users.begin(); |
114 it != users.end(); ++it) { | 376 it != users.end(); ++it) { |
115 std::string email = (*it)->email(); | 377 std::string email = (*it)->email(); |
116 // TODO(bshe): Move GetUserWallpaperProperties() to this class. | 378 GetUserWallpaperProperties(email, &type, &index, &last_modification_date); |
117 static_cast<UserManagerImpl*>(user_manager)-> | |
118 GetUserWallpaperProperties(email, &type, &index, | |
119 &last_modification_date); | |
120 base::Time current_date = base::Time::Now().LocalMidnight(); | 379 base::Time current_date = base::Time::Now().LocalMidnight(); |
121 if (type == User::DAILY && current_date != last_modification_date) { | 380 if (type == User::DAILY && current_date != last_modification_date) { |
122 index = ash::GetNextWallpaperIndex(index); | 381 index = ash::GetNextWallpaperIndex(index); |
123 // TODO(bshe): Move SaveUserWallpaperProperties() to this class. | 382 SaveUserWallpaperProperties(email, type, index); |
124 static_cast<UserManagerImpl*>(user_manager)-> | |
125 SaveUserWallpaperProperties(email, type, index); | |
126 } | 383 } |
127 // Force a wallpaper update for logged in / last selected user. | 384 // Force a wallpaper update for logged in / last selected user. |
128 // TODO(bshe): Notify lock screen, wallpaper picker UI to update wallpaper | 385 // TODO(bshe): Notify lock screen, wallpaper picker UI to update wallpaper |
129 // as well. | 386 // as well. |
130 if (user_manager->IsUserLoggedIn() && | 387 if (user_manager->IsUserLoggedIn() && |
131 email == user_manager->GetLoggedInUser().email()) { | 388 email == user_manager->GetLoggedInUser().email()) { |
132 user_manager->UserSelected(email); | 389 SetUserWallpaper(email); |
133 } else if (show_users && | 390 } else if (show_users && |
134 email == last_selected_user_) { | 391 email == last_selected_user_) { |
135 user_manager->UserSelected(email); | 392 SetUserWallpaper(email); |
136 } | 393 } |
137 } | 394 } |
138 } | 395 } |
139 RestartTimer(); | 396 RestartTimer(); |
140 } | 397 } |
141 | 398 |
399 void WallpaperManager::CacheWallpaper(const std::string& email, | |
400 const SkBitmap& wallpaper) { | |
401 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
402 DCHECK(custom_wallpaper_cache_.find(email) == custom_wallpaper_cache_.end()); | |
403 | |
404 BrowserThread::PostTask( | |
405 BrowserThread::FILE, | |
406 FROM_HERE, | |
407 base::Bind(&WallpaperManager::CacheThumbnail, | |
408 base::Unretained(this), email, wallpaper)); | |
409 | |
410 custom_wallpaper_cache_.insert(std::make_pair(email, wallpaper)); | |
411 } | |
412 | |
413 void WallpaperManager::CacheThumbnail(const std::string& email, | |
414 const SkBitmap& wallpaper) { | |
415 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
416 SkBitmap thumbnail = | |
417 skia::ImageOperations::Resize(wallpaper, | |
418 skia::ImageOperations::RESIZE_LANCZOS3, | |
419 kThumbnailWidth, kThumbnailHeight); | |
420 custom_wallpaper_thumbnail_cache_[email] = thumbnail; | |
421 } | |
422 | |
423 void WallpaperManager::FetchWallpaper(const std::string& email, | |
424 ash::WallpaperLayout layout, | |
425 const SkBitmap& wallpaper) { | |
426 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
427 | |
428 BrowserThread::PostTask( | |
429 BrowserThread::FILE, | |
430 FROM_HERE, | |
431 base::Bind(&WallpaperManager::CacheThumbnail, | |
432 base::Unretained(this), email, wallpaper)); | |
433 | |
434 ash::Shell::GetInstance()->desktop_background_controller()-> | |
435 SetCustomWallpaper(wallpaper, layout); | |
436 } | |
437 | |
438 void WallpaperManager::GetUserWallpaperProperties(const std::string& email, | |
439 User::WallpaperType* type, | |
440 int* index, | |
441 base::Time* last_modification_date) { | |
442 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
443 | |
444 // Default to the values cached in memory. | |
445 *type = current_user_wallpaper_type_; | |
446 *index = current_user_wallpaper_index_; | |
447 | |
448 // Override with values found in local store, if any. | |
449 if (!email.empty()) { | |
450 const DictionaryValue* user_wallpapers = g_browser_process->local_state()-> | |
451 GetDictionary(UserManager::kUserWallpapersProperties); | |
452 const base::DictionaryValue* wallpaper_properties; | |
453 if (user_wallpapers->GetDictionaryWithoutPathExpansion( | |
454 email, | |
455 &wallpaper_properties)) { | |
456 *type = User::UNKNOWN; | |
457 *index = ash::GetInvalidWallpaperIndex(); | |
458 wallpaper_properties->GetInteger(kWallpaperTypeNodeName, | |
459 reinterpret_cast<int*>(type)); | |
460 wallpaper_properties->GetInteger(kWallpaperIndexNodeName, index); | |
461 std::string date_string; | |
462 int64 val; | |
463 if (!(wallpaper_properties->GetString(kWallpaperDateNodeName, | |
464 &date_string) && | |
465 base::StringToInt64(date_string, &val))) | |
466 val = 0; | |
467 *last_modification_date = base::Time::FromInternalValue(val); | |
468 } | |
469 } | |
470 } | |
471 | |
472 void WallpaperManager::GenerateUserWallpaperThumbnail( | |
473 const std::string& email, | |
474 User::WallpaperType type, | |
475 base::WeakPtr<WallpaperDelegate> delegate, | |
476 const SkBitmap& wallpaper) { | |
477 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
478 SkBitmap thumbnail = | |
479 skia::ImageOperations::Resize(wallpaper, | |
480 skia::ImageOperations::RESIZE_LANCZOS3, | |
481 kThumbnailWidth, kThumbnailHeight); | |
482 | |
483 custom_wallpaper_thumbnail_cache_[email] = thumbnail; | |
484 | |
485 // Notify thumbnail is ready. | |
486 BrowserThread::PostTask( | |
487 BrowserThread::UI, | |
488 FROM_HERE, | |
489 base::Bind(&WallpaperManager::OnThumbnailUpdated, | |
490 base::Unretained(this), delegate)); | |
491 } | |
492 | |
493 void WallpaperManager::LoadCustomWallpaperFromFile(const std::string& email, | |
494 const LoadedCallback& loaded_cb) { | |
495 // TODO(bshe): Refactor GetWallpaperPathForUser to this class. | |
496 std::string wallpaper_path = | |
497 GetWallpaperPathForUser(email, false).value(); | |
498 // Load user wallpaper asynchronously. | |
499 wallpaper_loader_->Start(wallpaper_path, false, std::string(), loaded_cb); | |
500 } | |
501 | |
502 void WallpaperManager::OnThumbnailUpdated( | |
503 base::WeakPtr<WallpaperDelegate> delegate) { | |
504 if (delegate) | |
505 delegate->SetCustomWallpaperThumbnail(); | |
506 } | |
507 | |
508 void WallpaperManager::SetCallback(const std::string& username, | |
509 ash::WallpaperLayout layout, | |
510 User::WallpaperType type, | |
511 base::WeakPtr<WallpaperDelegate> delegate, | |
512 const SkBitmap& wallpaper) { | |
513 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
514 | |
515 BrowserThread::PostTask( | |
516 BrowserThread::FILE, | |
517 FROM_HERE, | |
518 base::Bind(&WallpaperManager::GenerateUserWallpaperThumbnail, | |
519 base::Unretained(this), username, type, delegate, wallpaper)); | |
520 | |
521 ash::Shell::GetInstance()->desktop_background_controller()-> | |
522 SetCustomWallpaper(wallpaper, layout); | |
523 | |
524 BrowserThread::PostTask( | |
525 BrowserThread::UI, | |
526 FROM_HERE, | |
527 base::Bind(&WallpaperManager::SaveUserWallpaperProperties, | |
528 base::Unretained(this), | |
529 username, type, layout)); | |
530 } | |
531 | |
142 void WallpaperManager::SystemResumed() { | 532 void WallpaperManager::SystemResumed() { |
143 BatchUpdateWallpaper(); | 533 BatchUpdateWallpaper(); |
144 } | 534 } |
145 | 535 |
146 void WallpaperManager::TimezoneChanged(const icu::TimeZone& timezone) { | 536 void WallpaperManager::TimezoneChanged(const icu::TimeZone& timezone) { |
147 RestartTimer(); | 537 RestartTimer(); |
148 } | 538 } |
149 | 539 |
150 } // chromeos | 540 } // chromeos |
OLD | NEW |