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