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/shell.h" | 8 #include "ash/shell.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
14 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
16 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
17 #include "base/time.h" | 17 #include "base/time.h" |
18 #include "base/values.h" | 18 #include "base/values.h" |
19 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
20 #include "chrome/browser/chromeos/login/user.h" | |
21 #include "chrome/browser/chromeos/login/user_manager.h" | 20 #include "chrome/browser/chromeos/login/user_manager.h" |
22 #include "chrome/browser/chromeos/login/wizard_controller.h" | 21 #include "chrome/browser/chromeos/login/wizard_controller.h" |
23 #include "chrome/browser/chromeos/settings/cros_settings.h" | 22 #include "chrome/browser/chromeos/settings/cros_settings.h" |
24 #include "chrome/browser/prefs/pref_service.h" | 23 #include "chrome/browser/prefs/pref_service.h" |
25 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 24 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
26 #include "chrome/common/chrome_notification_types.h" | 25 #include "chrome/common/chrome_notification_types.h" |
27 #include "chrome/common/chrome_paths.h" | 26 #include "chrome/common/chrome_paths.h" |
28 #include "chrome/common/chrome_switches.h" | 27 #include "chrome/common/chrome_switches.h" |
29 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
30 #include "chromeos/dbus/dbus_thread_manager.h" | 29 #include "chromeos/dbus/dbus_thread_manager.h" |
(...skipping 10 matching lines...) Expand all Loading... |
41 | 40 |
42 const int kWallpaperUpdateIntervalSec = 24 * 60 * 60; | 41 const int kWallpaperUpdateIntervalSec = 24 * 60 * 60; |
43 | 42 |
44 const char kWallpaperTypeNodeName[] = "type"; | 43 const char kWallpaperTypeNodeName[] = "type"; |
45 const char kWallpaperIndexNodeName[] = "index"; | 44 const char kWallpaperIndexNodeName[] = "index"; |
46 const char kWallpaperDateNodeName[] = "date"; | 45 const char kWallpaperDateNodeName[] = "date"; |
47 | 46 |
48 const int kThumbnailWidth = 128; | 47 const int kThumbnailWidth = 128; |
49 const int kThumbnailHeight = 80; | 48 const int kThumbnailHeight = 80; |
50 | 49 |
51 const int kCacheWallpaperDelayMs = 500; | |
52 | |
53 // Default wallpaper index used in OOBE (first boot). | 50 // Default wallpaper index used in OOBE (first boot). |
54 // Defined here because Chromium default index differs. | 51 // Defined here because Chromium default index differs. |
55 // Also see ash::WallpaperInfo kDefaultWallpapers in | 52 // Also see ash::WallpaperInfo kDefaultWallpapers in |
56 // desktop_background_resources.cc | 53 // desktop_background_resources.cc |
57 #if defined(GOOGLE_CHROME_BUILD) | 54 #if defined(GOOGLE_CHROME_BUILD) |
58 const int kDefaultOOBEWallpaperIndex = 20; // IDR_AURA_WALLPAPERS_2_LANDSCAPE8 | 55 const int kDefaultOOBEWallpaperIndex = 20; // IDR_AURA_WALLPAPERS_2_LANDSCAPE8 |
59 #else | 56 #else |
60 const int kDefaultOOBEWallpaperIndex = 0; // IDR_AURA_WALLPAPERS_ROMAINGUY_0 | 57 const int kDefaultOOBEWallpaperIndex = 0; // IDR_AURA_WALLPAPERS_ROMAINGUY_0 |
61 #endif | 58 #endif |
62 | 59 |
(...skipping 16 matching lines...) Expand all Loading... |
79 if (!g_wallpaper_manager) | 76 if (!g_wallpaper_manager) |
80 g_wallpaper_manager = new WallpaperManager(); | 77 g_wallpaper_manager = new WallpaperManager(); |
81 return g_wallpaper_manager; | 78 return g_wallpaper_manager; |
82 } | 79 } |
83 | 80 |
84 WallpaperManager::WallpaperManager() | 81 WallpaperManager::WallpaperManager() |
85 : ALLOW_THIS_IN_INITIALIZER_LIST(wallpaper_loader_(new UserImageLoader)), | 82 : ALLOW_THIS_IN_INITIALIZER_LIST(wallpaper_loader_(new UserImageLoader)), |
86 current_user_wallpaper_type_(User::UNKNOWN), | 83 current_user_wallpaper_type_(User::UNKNOWN), |
87 ALLOW_THIS_IN_INITIALIZER_LIST(current_user_wallpaper_index_( | 84 ALLOW_THIS_IN_INITIALIZER_LIST(current_user_wallpaper_index_( |
88 ash::GetInvalidWallpaperIndex())), | 85 ash::GetInvalidWallpaperIndex())), |
89 should_cache_wallpaper_(false), | |
90 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 86 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
91 RestartTimer(); | 87 RestartTimer(); |
92 registrar_.Add(this, | 88 registrar_.Add(this, |
93 chrome::NOTIFICATION_LOGIN_USER_CHANGED, | 89 chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
94 content::NotificationService::AllSources()); | 90 content::NotificationService::AllSources()); |
95 registrar_.Add(this, | |
96 chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE, | |
97 content::NotificationService::AllSources()); | |
98 registrar_.Add(this, | |
99 chrome::NOTIFICATION_WALLPAPER_ANIMATION_FINISHED, | |
100 content::NotificationService::AllSources()); | |
101 } | 91 } |
102 | 92 |
103 // static | 93 // static |
104 void WallpaperManager::RegisterPrefs(PrefService* local_state) { | 94 void WallpaperManager::RegisterPrefs(PrefService* local_state) { |
105 local_state->RegisterDictionaryPref(prefs::kUsersWallpaperInfo, | 95 local_state->RegisterDictionaryPref(prefs::kUsersWallpaperInfo, |
106 PrefService::UNSYNCABLE_PREF); | 96 PrefService::UNSYNCABLE_PREF); |
107 } | 97 } |
108 | 98 |
109 void WallpaperManager::AddObservers() { | 99 void WallpaperManager::AddObservers() { |
110 if (!DBusThreadManager::Get()->GetPowerManagerClient()->HasObserver(this)) | 100 if (!DBusThreadManager::Get()->GetPowerManagerClient()->HasObserver(this)) |
111 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this); | 101 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this); |
112 system::TimezoneSettings::GetInstance()->AddObserver(this); | 102 system::TimezoneSettings::GetInstance()->AddObserver(this); |
113 } | 103 } |
114 | 104 |
| 105 void WallpaperManager::CacheIfCustomWallpaper(const std::string& email) { |
| 106 User::WallpaperType type; |
| 107 int index; |
| 108 base::Time date; |
| 109 GetUserWallpaperProperties(email, &type, &index, &date); |
| 110 if (type == User::CUSTOMIZED) { |
| 111 std::string wallpaper_path = GetWallpaperPathForUser(email, false).value(); |
| 112 |
| 113 // Uses WeakPtr here to make the request cancelable. |
| 114 wallpaper_loader_->Start(wallpaper_path, 0, |
| 115 base::Bind(&WallpaperManager::CacheWallpaper, |
| 116 weak_factory_.GetWeakPtr(), email)); |
| 117 } |
| 118 } |
| 119 |
115 void WallpaperManager::EnsureLoggedInUserWallpaperLoaded() { | 120 void WallpaperManager::EnsureLoggedInUserWallpaperLoaded() { |
116 User::WallpaperType type; | 121 User::WallpaperType type; |
117 int index; | 122 int index; |
118 base::Time last_modification_date; | 123 base::Time last_modification_date; |
119 GetLoggedInUserWallpaperProperties(&type, &index, &last_modification_date); | 124 GetLoggedInUserWallpaperProperties(&type, &index, &last_modification_date); |
120 | 125 |
121 if (type != current_user_wallpaper_type_ || | 126 if (type != current_user_wallpaper_type_ || |
122 index != current_user_wallpaper_index_) { | 127 index != current_user_wallpaper_index_) { |
123 SetUserWallpaper(UserManager::Get()->GetLoggedInUser().email()); | 128 SetUserWallpaper(UserManager::Get()->GetLoggedInUser().email()); |
124 } | 129 } |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 } | 238 } |
234 return; | 239 return; |
235 } | 240 } |
236 SetUserWallpaper(user_manager->GetLoggedInUser().email()); | 241 SetUserWallpaper(user_manager->GetLoggedInUser().email()); |
237 } | 242 } |
238 | 243 |
239 void WallpaperManager::Observe(int type, | 244 void WallpaperManager::Observe(int type, |
240 const content::NotificationSource& source, | 245 const content::NotificationSource& source, |
241 const content::NotificationDetails& details) { | 246 const content::NotificationDetails& details) { |
242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
243 switch (type) { | 248 if (type == chrome::NOTIFICATION_LOGIN_USER_CHANGED) { |
244 case chrome::NOTIFICATION_LOGIN_USER_CHANGED: { | 249 // Cancel callback for previous cache requests. |
245 // Cancel callback for previous cache requests. | 250 weak_factory_.InvalidateWeakPtrs(); |
246 weak_factory_.InvalidateWeakPtrs(); | 251 custom_wallpaper_cache_.clear(); |
247 custom_wallpaper_cache_.clear(); | |
248 break; | |
249 } | |
250 case chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE: { | |
251 if (!CommandLine::ForCurrentProcess()-> | |
252 HasSwitch(switches::kDisableBootAnimation)) { | |
253 BrowserThread::PostDelayedTask( | |
254 BrowserThread::UI, FROM_HERE, | |
255 base::Bind(&WallpaperManager::CacheAllUsersWallpapers, | |
256 weak_factory_.GetWeakPtr()), | |
257 base::TimeDelta::FromMilliseconds(kCacheWallpaperDelayMs)); | |
258 } else { | |
259 should_cache_wallpaper_ = true; | |
260 } | |
261 break; | |
262 } | |
263 case chrome::NOTIFICATION_WALLPAPER_ANIMATION_FINISHED: { | |
264 if (should_cache_wallpaper_) { | |
265 BrowserThread::PostDelayedTask( | |
266 BrowserThread::UI, FROM_HERE, | |
267 base::Bind(&WallpaperManager::CacheAllUsersWallpapers, | |
268 weak_factory_.GetWeakPtr()), | |
269 base::TimeDelta::FromMilliseconds(kCacheWallpaperDelayMs)); | |
270 should_cache_wallpaper_ = false; | |
271 } | |
272 break; | |
273 } | |
274 default: | |
275 NOTREACHED() << "Unexpected notification " << type; | |
276 } | 252 } |
277 } | 253 } |
278 | 254 |
279 void WallpaperManager::RestartTimer() { | 255 void WallpaperManager::RestartTimer() { |
280 timer_.Stop(); | 256 timer_.Stop(); |
281 base::Time midnight = base::Time::Now().LocalMidnight(); | 257 base::Time midnight = base::Time::Now().LocalMidnight(); |
282 base::TimeDelta interval = base::Time::Now() - midnight; | 258 base::TimeDelta interval = base::Time::Now() - midnight; |
283 int64 remaining_seconds = kWallpaperUpdateIntervalSec - interval.InSeconds(); | 259 int64 remaining_seconds = kWallpaperUpdateIntervalSec - interval.InSeconds(); |
284 if (remaining_seconds <= 0) { | 260 if (remaining_seconds <= 0) { |
285 BatchUpdateWallpaper(); | 261 BatchUpdateWallpaper(); |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 SetUserWallpaper(email); | 441 SetUserWallpaper(email); |
466 } else if (show_users && | 442 } else if (show_users && |
467 email == last_selected_user_) { | 443 email == last_selected_user_) { |
468 SetUserWallpaper(email); | 444 SetUserWallpaper(email); |
469 } | 445 } |
470 } | 446 } |
471 } | 447 } |
472 RestartTimer(); | 448 RestartTimer(); |
473 } | 449 } |
474 | 450 |
475 void WallpaperManager::CacheAllUsersWallpapers() { | |
476 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
477 UserList users = UserManager::Get()->GetUsers(); | |
478 | |
479 UserList::const_iterator it = users.begin(); | |
480 // Skip the wallpaper of first user in the list. It should have been cached. | |
481 it++; | |
482 for (; it != users.end(); ++it) { | |
483 std::string user_email = (*it)->email(); | |
484 CacheUserWallpaper(user_email); | |
485 } | |
486 } | |
487 | |
488 void WallpaperManager::CacheUserWallpaper(const std::string& email) { | |
489 User::WallpaperType type; | |
490 int index; | |
491 base::Time date; | |
492 GetUserWallpaperProperties(email, &type, &index, &date); | |
493 if (type == User::CUSTOMIZED) { | |
494 std::string wallpaper_path = GetWallpaperPathForUser(email, false).value(); | |
495 | |
496 // Uses WeakPtr here to make the request cancelable. | |
497 wallpaper_loader_->Start(wallpaper_path, 0, | |
498 base::Bind(&WallpaperManager::CacheWallpaper, | |
499 weak_factory_.GetWeakPtr(), email)); | |
500 } else { | |
501 ash::Shell::GetInstance()->desktop_background_controller()-> | |
502 CacheDefaultWallpaper(index); | |
503 } | |
504 } | |
505 | |
506 void WallpaperManager::CacheWallpaper(const std::string& email, | 451 void WallpaperManager::CacheWallpaper(const std::string& email, |
507 const UserImage& wallpaper) { | 452 const UserImage& wallpaper) { |
508 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 453 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
509 DCHECK(custom_wallpaper_cache_.find(email) == custom_wallpaper_cache_.end()); | 454 DCHECK(custom_wallpaper_cache_.find(email) == custom_wallpaper_cache_.end()); |
510 | 455 |
511 BrowserThread::PostTask( | 456 BrowserThread::PostTask( |
512 BrowserThread::FILE, | 457 BrowserThread::FILE, |
513 FROM_HERE, | 458 FROM_HERE, |
514 base::Bind(&WallpaperManager::CacheThumbnail, | 459 base::Bind(&WallpaperManager::CacheThumbnail, |
515 base::Unretained(this), email, wallpaper.image())); | 460 base::Unretained(this), email, wallpaper.image())); |
(...skipping 15 matching lines...) Expand all Loading... |
531 ash::WallpaperLayout layout, | 476 ash::WallpaperLayout layout, |
532 const UserImage& wallpaper) { | 477 const UserImage& wallpaper) { |
533 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 478 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
534 | 479 |
535 BrowserThread::PostTask( | 480 BrowserThread::PostTask( |
536 BrowserThread::FILE, | 481 BrowserThread::FILE, |
537 FROM_HERE, | 482 FROM_HERE, |
538 base::Bind(&WallpaperManager::CacheThumbnail, | 483 base::Bind(&WallpaperManager::CacheThumbnail, |
539 base::Unretained(this), email, wallpaper.image())); | 484 base::Unretained(this), email, wallpaper.image())); |
540 | 485 |
541 custom_wallpaper_cache_.insert(std::make_pair(email, wallpaper.image())); | |
542 ash::Shell::GetInstance()->desktop_background_controller()-> | 486 ash::Shell::GetInstance()->desktop_background_controller()-> |
543 SetCustomWallpaper(wallpaper.image(), layout); | 487 SetCustomWallpaper(wallpaper.image(), layout); |
544 } | 488 } |
545 | 489 |
546 void WallpaperManager::GetUserWallpaperProperties(const std::string& email, | 490 void WallpaperManager::GetUserWallpaperProperties(const std::string& email, |
547 User::WallpaperType* type, | 491 User::WallpaperType* type, |
548 int* index, | 492 int* index, |
549 base::Time* last_modification_date) { | 493 base::Time* last_modification_date) { |
550 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 494 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
551 | 495 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
651 | 595 |
652 void WallpaperManager::SystemResumed() { | 596 void WallpaperManager::SystemResumed() { |
653 BatchUpdateWallpaper(); | 597 BatchUpdateWallpaper(); |
654 } | 598 } |
655 | 599 |
656 void WallpaperManager::TimezoneChanged(const icu::TimeZone& timezone) { | 600 void WallpaperManager::TimezoneChanged(const icu::TimeZone& timezone) { |
657 RestartTimer(); | 601 RestartTimer(); |
658 } | 602 } |
659 | 603 |
660 } // chromeos | 604 } // chromeos |
OLD | NEW |