Chromium Code Reviews| 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" | 8 #include "ash/desktop_background/desktop_background_resources.h" |
| 9 #include "ash/shell.h" | 9 #include "ash/shell.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/memory/ref_counted_memory.h" | 14 #include "base/memory/ref_counted_memory.h" |
| 15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 16 #include "base/string_number_conversions.h" | 16 #include "base/string_number_conversions.h" |
| 17 #include "base/string_util.h" | 17 #include "base/string_util.h" |
| 18 #include "base/stringprintf.h" | 18 #include "base/stringprintf.h" |
| 19 #include "base/time.h" | 19 #include "base/time.h" |
| 20 #include "base/values.h" | 20 #include "base/values.h" |
| 21 #include "chrome/browser/browser_process.h" | 21 #include "chrome/browser/browser_process.h" |
| 22 #include "chrome/browser/chromeos/login/simple_jpeg_encoder.h" | |
| 22 #include "chrome/browser/chromeos/login/user.h" | 23 #include "chrome/browser/chromeos/login/user.h" |
| 23 #include "chrome/browser/chromeos/login/user_manager.h" | 24 #include "chrome/browser/chromeos/login/user_manager.h" |
| 24 #include "chrome/browser/chromeos/login/wizard_controller.h" | 25 #include "chrome/browser/chromeos/login/wizard_controller.h" |
| 25 #include "chrome/browser/chromeos/settings/cros_settings.h" | 26 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 26 #include "chrome/browser/prefs/pref_service.h" | 27 #include "chrome/browser/prefs/pref_service.h" |
| 27 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 28 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 28 #include "chrome/common/chrome_notification_types.h" | 29 #include "chrome/common/chrome_notification_types.h" |
| 29 #include "chrome/common/chrome_paths.h" | 30 #include "chrome/common/chrome_paths.h" |
| 30 #include "chrome/common/chrome_switches.h" | 31 #include "chrome/common/chrome_switches.h" |
| 31 #include "chrome/common/pref_names.h" | 32 #include "chrome/common/pref_names.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 63 #else | 64 #else |
| 64 const int kDefaultOOBEWallpaperIndex = 0; // IDR_AURA_WALLPAPERS_ROMAINGUY_0 | 65 const int kDefaultOOBEWallpaperIndex = 0; // IDR_AURA_WALLPAPERS_ROMAINGUY_0 |
| 65 #endif | 66 #endif |
| 66 | 67 |
| 67 // Names of nodes with info about wallpaper. | 68 // Names of nodes with info about wallpaper. |
| 68 const char kNewWallpaperDateNodeName[] = "date"; | 69 const char kNewWallpaperDateNodeName[] = "date"; |
| 69 const char kNewWallpaperLayoutNodeName[] = "layout"; | 70 const char kNewWallpaperLayoutNodeName[] = "layout"; |
| 70 const char kNewWallpaperFileNodeName[] = "file"; | 71 const char kNewWallpaperFileNodeName[] = "file"; |
| 71 const char kNewWallpaperTypeNodeName[] = "type"; | 72 const char kNewWallpaperTypeNodeName[] = "type"; |
| 72 | 73 |
| 74 // TODO(bshe): Address the file extension issue. All custom wallpapers should be | |
| 75 // jpeg files. | |
| 76 // File path suffices of resized small or large custom wallpaper. | |
| 77 const char kSmallCustomWallpaperSuffix[] = "_small"; | |
| 78 const char kLargeCustomWallpaperSuffix[] = "_large"; | |
| 79 // File path suffix of the original custom wallpaper. | |
| 80 const char kOriginalCustomWallpaperSuffix[] = "_wallpaper.png"; | |
|
Emmanuel Saint-loubert-Bié
2012/08/30 22:53:26
FWIW I have observed that the extension has no imp
| |
| 81 | |
| 73 gfx::ImageSkia GetWallpaperThumbnail(const gfx::ImageSkia& wallpaper) { | 82 gfx::ImageSkia GetWallpaperThumbnail(const gfx::ImageSkia& wallpaper) { |
| 74 gfx::ImageSkia thumbnail = gfx::ImageSkiaOperations::CreateResizedImage( | 83 gfx::ImageSkia thumbnail = gfx::ImageSkiaOperations::CreateResizedImage( |
| 75 wallpaper, | 84 wallpaper, |
| 76 skia::ImageOperations::RESIZE_LANCZOS3, | 85 skia::ImageOperations::RESIZE_LANCZOS3, |
| 77 gfx::Size(kThumbnailWidth, kThumbnailHeight)); | 86 gfx::Size(kThumbnailWidth, kThumbnailHeight)); |
| 78 | 87 |
| 79 thumbnail.MakeThreadSafe(); | 88 thumbnail.MakeThreadSafe(); |
| 80 return thumbnail; | 89 return thumbnail; |
| 81 } | 90 } |
| 82 | 91 |
| 92 // For our scaling ratios we need to round positive numbers. | |
| 93 int RoundPositive(double x) { | |
| 94 return static_cast<int>(floor(x + 0.5)); | |
| 95 } | |
| 96 | |
| 83 } // namespace | 97 } // namespace |
| 84 | 98 |
| 85 namespace chromeos { | 99 namespace chromeos { |
| 86 | 100 |
| 87 static WallpaperManager* g_wallpaper_manager = NULL; | 101 static WallpaperManager* g_wallpaper_manager = NULL; |
| 88 | 102 |
| 89 // WallpaperManager, public: --------------------------------------------------- | 103 // WallpaperManager, public: --------------------------------------------------- |
| 90 | 104 |
| 91 // static | 105 // static |
| 92 WallpaperManager* WallpaperManager::Get() { | 106 WallpaperManager* WallpaperManager::Get() { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 GetLoggedInUserWallpaperProperties(&type, &index, &date); | 155 GetLoggedInUserWallpaperProperties(&type, &index, &date); |
| 142 | 156 |
| 143 if (type == current_user_wallpaper_type_ && | 157 if (type == current_user_wallpaper_type_ && |
| 144 index == current_user_wallpaper_index_) { | 158 index == current_user_wallpaper_index_) { |
| 145 return; | 159 return; |
| 146 } | 160 } |
| 147 } | 161 } |
| 148 SetUserWallpaper(UserManager::Get()->GetLoggedInUser().email()); | 162 SetUserWallpaper(UserManager::Get()->GetLoggedInUser().email()); |
| 149 } | 163 } |
| 150 | 164 |
| 151 void WallpaperManager::FetchCustomWallpaper(const std::string& email) { | 165 void WallpaperManager::GetCustomWallpaper(const std::string& email) { |
| 152 User::WallpaperType type; | 166 User::WallpaperType type; |
| 153 int index; | 167 int index; |
| 154 base::Time date; | 168 base::Time date; |
| 169 // Get layout, type and date while on UI thread. | |
| 155 GetUserWallpaperProperties(email, &type, &index, &date); | 170 GetUserWallpaperProperties(email, &type, &index, &date); |
| 156 ash::WallpaperLayout layout = static_cast<ash::WallpaperLayout>(index); | 171 ash::WallpaperLayout layout = static_cast<ash::WallpaperLayout>(index); |
| 157 | 172 // Initializes file name to a dummy string here. The real file path will |
| 158 std::string wallpaper_path = GetWallpaperPathForUser(email, false).value(); | 173 // be set in GetCustomWallpaperInternal. |
| 159 | 174 WallpaperInfo info = { |
| 160 wallpaper_loader_->Start(wallpaper_path, 0, | 175 "DUMMY", |
| 161 base::Bind(&WallpaperManager::FetchWallpaper, | 176 layout, |
| 162 base::Unretained(this), email, layout)); | 177 type, |
| 178 date | |
| 179 }; | |
| 180 BrowserThread::PostTask( | |
| 181 BrowserThread::FILE, FROM_HERE, | |
| 182 base::Bind(&WallpaperManager::GetCustomWallpaperInternal, | |
| 183 base::Unretained(this), email, info)); | |
| 163 } | 184 } |
| 164 | 185 |
| 165 bool WallpaperManager::GetCustomWallpaperFromCache(const std::string& email, | 186 bool WallpaperManager::GetCustomWallpaperFromCache(const std::string& email, |
| 166 gfx::ImageSkia* wallpaper) { | 187 gfx::ImageSkia* wallpaper) { |
| 167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 168 CustomWallpaperMap::const_iterator it = wallpaper_cache_.find(email); | 189 CustomWallpaperMap::const_iterator it = wallpaper_cache_.find(email); |
| 169 if (it != wallpaper_cache_.end()) { | 190 if (it != wallpaper_cache_.end()) { |
| 170 *wallpaper = (*it).second; | 191 *wallpaper = (*it).second; |
| 171 return true; | 192 return true; |
| 172 } | 193 } |
| 173 return false; | 194 return false; |
| 174 } | 195 } |
| 175 | 196 |
| 197 FilePath WallpaperManager::GetOriginalWallpaperPathForUser( | |
| 198 const std::string& username) { | |
| 199 std::string filename = username + kOriginalCustomWallpaperSuffix; | |
| 200 FilePath user_data_dir; | |
| 201 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | |
| 202 return user_data_dir.AppendASCII(filename); | |
| 203 } | |
| 204 | |
| 176 FilePath WallpaperManager::GetWallpaperPathForUser(const std::string& username, | 205 FilePath WallpaperManager::GetWallpaperPathForUser(const std::string& username, |
| 177 bool is_thumbnail) { | 206 bool is_small) { |
| 178 const char* suffix = is_thumbnail ? "_thumb" : ""; | 207 const char* suffix = is_small ? |
| 208 kSmallCustomWallpaperSuffix : kLargeCustomWallpaperSuffix; | |
| 179 std::string filename = base::StringPrintf("%s_wallpaper%s.png", | 209 std::string filename = base::StringPrintf("%s_wallpaper%s.png", |
| 180 username.c_str(), | 210 username.c_str(), |
| 181 suffix); | 211 suffix); |
| 182 FilePath user_data_dir; | 212 FilePath user_data_dir; |
| 183 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | 213 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 184 return user_data_dir.AppendASCII(filename); | 214 return user_data_dir.AppendASCII(filename); |
| 185 } | 215 } |
| 186 | 216 |
| 187 gfx::ImageSkia WallpaperManager::GetCustomWallpaperThumbnail( | 217 gfx::ImageSkia WallpaperManager::GetCustomWallpaperThumbnail( |
| 188 const std::string& email) { | 218 const std::string& email) { |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 358 void WallpaperManager::SetUserWallpaperFromFile( | 388 void WallpaperManager::SetUserWallpaperFromFile( |
| 359 const std::string& username, | 389 const std::string& username, |
| 360 const FilePath& path, | 390 const FilePath& path, |
| 361 ash::WallpaperLayout layout, | 391 ash::WallpaperLayout layout, |
| 362 base::WeakPtr<WallpaperDelegate> delegate) { | 392 base::WeakPtr<WallpaperDelegate> delegate) { |
| 363 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 393 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 364 | 394 |
| 365 // For wallpapers, save the image without resizing. | 395 // For wallpapers, save the image without resizing. |
| 366 wallpaper_loader_->Start( | 396 wallpaper_loader_->Start( |
| 367 path.value(), 0, | 397 path.value(), 0, |
| 368 base::Bind(&WallpaperManager::SetWallpaper, | 398 base::Bind(&WallpaperManager::SetCustomWallpaper, |
| 369 base::Unretained(this), username, layout, User::CUSTOMIZED, | 399 base::Unretained(this), username, layout, User::CUSTOMIZED, |
| 370 delegate)); | 400 delegate)); |
| 371 } | 401 } |
| 372 | 402 |
| 373 void WallpaperManager::SetInitialUserWallpaper(const std::string& username, | 403 void WallpaperManager::SetInitialUserWallpaper(const std::string& username, |
| 374 bool is_persistent) { | 404 bool is_persistent) { |
| 375 current_user_wallpaper_type_ = User::DEFAULT; | 405 current_user_wallpaper_type_ = User::DEFAULT; |
| 376 if (username == kGuestUser) | 406 if (username == kGuestUser) |
| 377 current_user_wallpaper_index_ = ash::GetGuestWallpaperIndex(); | 407 current_user_wallpaper_index_ = ash::GetGuestWallpaperIndex(); |
| 378 else | 408 else |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 440 if (!UserManager::Get()->IsKnownUser(email)) | 470 if (!UserManager::Get()->IsKnownUser(email)) |
| 441 return; | 471 return; |
| 442 | 472 |
| 443 bool new_wallpaper_ui_enabled = CommandLine::ForCurrentProcess()-> | 473 bool new_wallpaper_ui_enabled = CommandLine::ForCurrentProcess()-> |
| 444 HasSwitch(switches::kEnableNewWallpaperUI); | 474 HasSwitch(switches::kEnableNewWallpaperUI); |
| 445 | 475 |
| 446 WallpaperInfo info; | 476 WallpaperInfo info; |
| 447 | 477 |
| 448 if (new_wallpaper_ui_enabled && | 478 if (new_wallpaper_ui_enabled && |
| 449 GetUserWallpaperInfo(email, &info)) { | 479 GetUserWallpaperInfo(email, &info)) { |
| 480 if (info.type == User::CUSTOMIZED) { | |
| 481 if (!UserManager::Get()->IsUserLoggedIn()) { | |
| 482 ash::Shell::GetInstance()->desktop_background_controller()-> | |
| 483 SetDefaultWallpaper(ash::GetDefaultWallpaperIndex(), false); | |
| 484 } else { | |
| 485 BrowserThread::PostTask( | |
| 486 BrowserThread::FILE, FROM_HERE, | |
| 487 base::Bind(&WallpaperManager::GetCustomWallpaperInternal, | |
| 488 base::Unretained(this), email, info)); | |
| 489 } | |
| 490 return; | |
| 491 } | |
| 492 | |
| 450 if (info.file_name.empty()) { | 493 if (info.file_name.empty()) { |
| 451 // Uses default built-in wallpaper when file name is empty. Eventually, | 494 // Uses default built-in wallpaper when file name is empty. Eventually, |
| 452 // we will only ship one built-in wallpaper in ChromeOS image. | 495 // we will only ship one built-in wallpaper in ChromeOS image. |
| 453 ash::Shell::GetInstance()->desktop_background_controller()-> | 496 ash::Shell::GetInstance()->desktop_background_controller()-> |
| 454 SetDefaultWallpaper(ash::GetDefaultWallpaperIndex(), false); | 497 SetDefaultWallpaper(ash::GetDefaultWallpaperIndex(), false); |
| 455 return; | 498 return; |
| 456 } | 499 } |
| 500 | |
| 457 LoadWallpaper(email, info); | 501 LoadWallpaper(email, info); |
| 458 } else { | 502 } else { |
| 459 User::WallpaperType type; | 503 User::WallpaperType type; |
| 460 int index; | 504 int index; |
| 461 base::Time date; | 505 base::Time date; |
| 462 GetUserWallpaperProperties(email, &type, &index, &date); | 506 GetUserWallpaperProperties(email, &type, &index, &date); |
| 463 if (type == User::DAILY && date != base::Time::Now().LocalMidnight()) { | 507 if (type == User::DAILY && date != base::Time::Now().LocalMidnight()) { |
| 464 index = ash::GetNextWallpaperIndex(index); | 508 index = ash::GetNextWallpaperIndex(index); |
| 465 SetUserWallpaperProperties(email, User::DAILY, index, | 509 SetUserWallpaperProperties(email, User::DAILY, index, |
| 466 ShouldPersistDataForUser(email)); | 510 ShouldPersistDataForUser(email)); |
| 467 } else if (type == User::CUSTOMIZED) { | 511 } else if (type == User::CUSTOMIZED) { |
| 468 // For security reason, use default wallpaper instead of custom wallpaper | 512 // For security reason, use default wallpaper instead of custom wallpaper |
| 469 // at login screen. The security issue is tracked in issue 143198. Once it | 513 // at login screen. The security issue is tracked in issue 143198. Once it |
| 470 // fixed, we should then only use custom wallpaper. | 514 // fixed, we should then only use custom wallpaper. |
| 471 if (!UserManager::Get()->IsUserLoggedIn()) { | 515 if (!UserManager::Get()->IsUserLoggedIn()) { |
| 472 index = ash::GetDefaultWallpaperIndex(); | 516 index = ash::GetDefaultWallpaperIndex(); |
| 473 } else { | 517 } else { |
| 474 FetchCustomWallpaper(email); | 518 GetCustomWallpaper(email); |
| 475 return; | 519 return; |
| 476 } | 520 } |
| 477 } | 521 } |
| 478 ash::Shell::GetInstance()->desktop_background_controller()-> | 522 ash::Shell::GetInstance()->desktop_background_controller()-> |
| 479 SetDefaultWallpaper(index, false); | 523 SetDefaultWallpaper(index, false); |
| 480 if (new_wallpaper_ui_enabled) | 524 if (new_wallpaper_ui_enabled) |
| 481 MigrateBuiltInWallpaper(email); | 525 MigrateBuiltInWallpaper(email); |
| 482 } | 526 } |
| 483 SetLastSelectedUser(email); | 527 SetLastSelectedUser(email); |
| 484 } | 528 } |
| 485 | 529 |
| 486 void WallpaperManager::SetWallpaperFromImageSkia( | 530 void WallpaperManager::SetWallpaperFromImageSkia( |
| 487 const gfx::ImageSkia& wallpaper, | 531 const gfx::ImageSkia& wallpaper, |
| 488 ash::WallpaperLayout layout) { | 532 ash::WallpaperLayout layout) { |
| 489 ash::Shell::GetInstance()->desktop_background_controller()-> | 533 ash::Shell::GetInstance()->desktop_background_controller()-> |
| 490 SetCustomWallpaper(wallpaper, layout); | 534 SetCustomWallpaper(wallpaper, layout); |
| 491 } | 535 } |
| 492 | 536 |
| 493 void WallpaperManager::OnUserSelected(const std::string& email) { | 537 void WallpaperManager::OnUserSelected(const std::string& email) { |
| 494 SetUserWallpaper(email); | 538 SetUserWallpaper(email); |
| 495 } | 539 } |
| 496 | 540 |
| 497 // WallpaperManager, private: -------------------------------------------------- | 541 // WallpaperManager, private: -------------------------------------------------- |
| 498 | 542 |
| 499 WallpaperManager::~WallpaperManager() { | 543 WallpaperManager::~WallpaperManager() { |
| 500 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this); | 544 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this); |
| 501 system::TimezoneSettings::GetInstance()->RemoveObserver(this); | 545 system::TimezoneSettings::GetInstance()->RemoveObserver(this); |
| 546 weak_factory_.InvalidateWeakPtrs(); | |
| 502 } | 547 } |
| 503 | 548 |
| 504 void WallpaperManager::BatchUpdateWallpaper() { | 549 void WallpaperManager::BatchUpdateWallpaper() { |
| 505 PrefService* local_state = g_browser_process->local_state(); | 550 PrefService* local_state = g_browser_process->local_state(); |
| 506 UserManager* user_manager = UserManager::Get(); | 551 UserManager* user_manager = UserManager::Get(); |
| 507 bool show_users = true; | 552 bool show_users = true; |
| 508 CrosSettings::Get()->GetBoolean( | 553 CrosSettings::Get()->GetBoolean( |
| 509 kAccountsPrefShowUserNamesOnSignIn, &show_users); | 554 kAccountsPrefShowUserNamesOnSignIn, &show_users); |
| 510 if (local_state) { | 555 if (local_state) { |
| 511 User::WallpaperType type; | 556 User::WallpaperType type; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 564 FilePath wallpaper_path; | 609 FilePath wallpaper_path; |
| 565 if (info.type == User::CUSTOMIZED) { | 610 if (info.type == User::CUSTOMIZED) { |
| 566 ash::Shell::GetInstance()->desktop_background_controller()-> | 611 ash::Shell::GetInstance()->desktop_background_controller()-> |
| 567 CacheDefaultWallpaper(ash::GetDefaultWallpaperIndex()); | 612 CacheDefaultWallpaper(ash::GetDefaultWallpaperIndex()); |
| 568 return; | 613 return; |
| 569 } | 614 } |
| 570 if (info.type == User::ONLINE) { | 615 if (info.type == User::ONLINE) { |
| 571 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, | 616 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, |
| 572 &wallpaper_dir)); | 617 &wallpaper_dir)); |
| 573 wallpaper_path = wallpaper_dir.Append(info.file_name); | 618 wallpaper_path = wallpaper_dir.Append(info.file_name); |
| 574 } else { | 619 } else if (info.type == User::DEFAULT) { |
| 620 // Current wallpaper is converted from built-in wallpaper. | |
| 575 ash::WallpaperResolution resolution = ash::Shell::GetInstance()-> | 621 ash::WallpaperResolution resolution = ash::Shell::GetInstance()-> |
| 576 desktop_background_controller()->GetAppropriateResolution(); | 622 desktop_background_controller()->GetAppropriateResolution(); |
| 577 bool small_resolution = (resolution == ash::SMALL); | 623 bool small_resolution = (resolution == ash::SMALL); |
| 578 wallpaper_path = GetWallpaperPathForUser(email, small_resolution); | 624 wallpaper_path = GetWallpaperPathForUser(email, small_resolution); |
| 625 } else { | |
| 626 // Cache default wallpaper when user selected a custom wallpaper. | |
| 627 ash::Shell::GetInstance()->desktop_background_controller()-> | |
| 628 CacheDefaultWallpaper(ash::GetDefaultWallpaperIndex()); | |
| 629 return; | |
| 579 } | 630 } |
| 580 wallpaper_loader_->Start(wallpaper_path.value(), 0, | 631 wallpaper_loader_->Start(wallpaper_path.value(), 0, |
| 581 base::Bind(&WallpaperManager::CacheWallpaper, | 632 base::Bind(&WallpaperManager::CacheWallpaper, |
| 582 base::Unretained(this), email)); | 633 base::Unretained(this), email)); |
| 583 } else { | 634 } else { |
| 584 MigrateBuiltInWallpaper(email); | 635 MigrateBuiltInWallpaper(email); |
| 585 return; | 636 return; |
| 586 } | 637 } |
| 587 } else { | 638 } else { |
| 588 User::WallpaperType type; | 639 User::WallpaperType type; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 621 } | 672 } |
| 622 | 673 |
| 623 void WallpaperManager::LoadWallpaper(const std::string& email, | 674 void WallpaperManager::LoadWallpaper(const std::string& email, |
| 624 const WallpaperInfo& info) { | 675 const WallpaperInfo& info) { |
| 625 FilePath wallpaper_dir; | 676 FilePath wallpaper_dir; |
| 626 FilePath wallpaper_path; | 677 FilePath wallpaper_path; |
| 627 if (info.type == User::ONLINE) { | 678 if (info.type == User::ONLINE) { |
| 628 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir)); | 679 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir)); |
| 629 wallpaper_path = wallpaper_dir.Append(info.file_name); | 680 wallpaper_path = wallpaper_dir.Append(info.file_name); |
| 630 } else { | 681 } else { |
| 631 ash::WallpaperResolution resolution = ash::Shell::GetInstance()-> | 682 FilePath user_data_dir; |
| 632 desktop_background_controller()->GetAppropriateResolution(); | 683 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 633 bool small_resolution = (resolution == ash::SMALL); | 684 wallpaper_path = user_data_dir.Append(info.file_name); |
| 634 wallpaper_path = GetWallpaperPathForUser(email, small_resolution); | |
| 635 } | 685 } |
| 636 | 686 |
| 637 wallpaper_loader_->Start(wallpaper_path.value(), 0, | 687 wallpaper_loader_->Start(wallpaper_path.value(), 0, |
| 638 base::Bind(&WallpaperManager::FetchWallpaper, | 688 base::Bind(&WallpaperManager::FetchWallpaper, |
| 639 base::Unretained(this), | 689 base::Unretained(this), |
| 640 email, | 690 email, |
| 641 info.layout)); | 691 info.layout)); |
| 642 } | 692 } |
| 643 | 693 |
| 644 void WallpaperManager::FetchWallpaper(const std::string& email, | 694 void WallpaperManager::FetchWallpaper(const std::string& email, |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 742 custom_wallpaper_thumbnail_cache_[email] = GetWallpaperThumbnail(wallpaper); | 792 custom_wallpaper_thumbnail_cache_[email] = GetWallpaperThumbnail(wallpaper); |
| 743 | 793 |
| 744 // Notify thumbnail is ready. | 794 // Notify thumbnail is ready. |
| 745 BrowserThread::PostTask( | 795 BrowserThread::PostTask( |
| 746 BrowserThread::UI, | 796 BrowserThread::UI, |
| 747 FROM_HERE, | 797 FROM_HERE, |
| 748 base::Bind(&WallpaperManager::OnThumbnailUpdated, | 798 base::Bind(&WallpaperManager::OnThumbnailUpdated, |
| 749 base::Unretained(this), delegate)); | 799 base::Unretained(this), delegate)); |
| 750 } | 800 } |
| 751 | 801 |
| 802 void WallpaperManager::GetCustomWallpaperInternal(const std::string& email, | |
| 803 const WallpaperInfo& info) { | |
| 804 ash::WallpaperResolution resolution = ash::Shell::GetInstance()-> | |
| 805 desktop_background_controller()->GetAppropriateResolution(); | |
| 806 bool is_small = (resolution == ash::SMALL); | |
| 807 FilePath wallpaper_path = GetWallpaperPathForUser(email, is_small); | |
| 808 std::string file_name = wallpaper_path.BaseName().value(); | |
| 809 // Falls back on original file if the correct resoltuion file does not | |
| 810 // exist. This may happen when the original custom wallpaper is small or | |
| 811 // browser shutdown before resized wallpaper saved. | |
| 812 if (!file_util::PathExists(wallpaper_path)) | |
| 813 file_name = GetOriginalWallpaperPathForUser(email).BaseName().value(); | |
| 814 | |
| 815 WallpaperInfo new_info = { | |
| 816 file_name, | |
| 817 info.layout, | |
| 818 info.type, | |
| 819 info.date | |
| 820 }; | |
| 821 | |
| 822 BrowserThread::PostTask( | |
| 823 BrowserThread::UI, FROM_HERE, | |
| 824 base::Bind(&WallpaperManager::LoadWallpaper, | |
| 825 base::Unretained(this), email, new_info)); | |
| 826 } | |
| 827 | |
| 752 void WallpaperManager::MigrateBuiltInWallpaper(const std::string& email) { | 828 void WallpaperManager::MigrateBuiltInWallpaper(const std::string& email) { |
| 753 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 829 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 754 if (!email.empty() && UserManager::Get()->IsKnownUser(email)) { | 830 if (!email.empty() && UserManager::Get()->IsKnownUser(email)) { |
| 755 PrefService* prefs = g_browser_process->local_state(); | 831 PrefService* prefs = g_browser_process->local_state(); |
| 756 const DictionaryValue* user_wallpapers = prefs-> | 832 const DictionaryValue* user_wallpapers = prefs-> |
| 757 GetDictionary(UserManager::kUserWallpapersProperties); | 833 GetDictionary(UserManager::kUserWallpapersProperties); |
| 758 const base::DictionaryValue* wallpaper_properties; | 834 const base::DictionaryValue* wallpaper_properties; |
| 759 User::WallpaperType type; | 835 User::WallpaperType type; |
| 760 int index; | 836 int index; |
| 761 ash::WallpaperResolution resolution = ash::Shell::GetInstance()-> | 837 ash::WallpaperResolution resolution = ash::Shell::GetInstance()-> |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 797 | 873 |
| 798 // Saves small wallpaper to user custom wallpaper thumbnail path. | 874 // Saves small wallpaper to user custom wallpaper thumbnail path. |
| 799 wallpaper_path = GetWallpaperPathForUser(email, true); | 875 wallpaper_path = GetWallpaperPathForUser(email, true); |
| 800 BrowserThread::PostTask( | 876 BrowserThread::PostTask( |
| 801 BrowserThread::FILE, | 877 BrowserThread::FILE, |
| 802 FROM_HERE, | 878 FROM_HERE, |
| 803 base::Bind(&WallpaperManager::SaveWallpaperInternal, | 879 base::Bind(&WallpaperManager::SaveWallpaperInternal, |
| 804 base::Unretained(this), wallpaper_path, | 880 base::Unretained(this), wallpaper_path, |
| 805 reinterpret_cast<const char*>(wallpaper->front()), | 881 reinterpret_cast<const char*>(wallpaper->front()), |
| 806 wallpaper->size())); | 882 wallpaper->size())); |
| 807 } else { | |
| 808 // Since we only have one resolution for custom wallpaper, always | |
| 809 // use LARGE resolution for custom wallpaper. | |
| 810 resolution = ash::LARGE; | |
| 811 } | 883 } |
| 812 | 884 |
| 813 bool small_resolution = (resolution == ash::SMALL); | 885 bool small_resolution = (resolution == ash::SMALL); |
| 814 std::string file_name = | 886 std::string file_name = |
| 815 GetWallpaperPathForUser(email, small_resolution).BaseName().value(); | 887 GetWallpaperPathForUser(email, small_resolution).BaseName().value(); |
| 816 WallpaperInfo info = { | 888 WallpaperInfo info = { |
| 817 file_name, | 889 file_name, |
| 818 GetWallpaperViewInfo(index, resolution).layout, | 890 GetWallpaperViewInfo(index, resolution).layout, |
| 819 type, | 891 type, |
| 820 base::Time::Now().LocalMidnight() | 892 base::Time::Now().LocalMidnight() |
| 821 }; | 893 }; |
| 822 SetUserWallpaperInfo(email, info, true); | 894 SetUserWallpaperInfo(email, info, true); |
| 823 } | 895 } |
| 824 } | 896 } |
| 825 } | 897 } |
| 826 | 898 |
| 827 void WallpaperManager::OnThumbnailUpdated( | 899 void WallpaperManager::OnThumbnailUpdated( |
| 828 base::WeakPtr<WallpaperDelegate> delegate) { | 900 base::WeakPtr<WallpaperDelegate> delegate) { |
| 829 if (delegate) | 901 if (delegate) |
| 830 delegate->SetCustomWallpaperThumbnail(); | 902 delegate->SetCustomWallpaperThumbnail(); |
| 831 } | 903 } |
| 832 | 904 |
| 833 void WallpaperManager::SaveWallpaper(const FilePath& path, | 905 void WallpaperManager::OnWallpaperEncoded(const FilePath& path, |
| 834 const UserImage& wallpaper) { | 906 scoped_refptr<base::RefCountedBytes> data) { |
| 907 SaveWallpaperInternal(path, | |
| 908 reinterpret_cast<const char*>(data->front()), | |
| 909 data->size()); | |
| 910 } | |
| 911 | |
| 912 void WallpaperManager::SaveCustomWallpaper(const std::string& email, | |
| 913 const FilePath& path, | |
| 914 ash::WallpaperLayout layout, | |
| 915 const UserImage& wallpaper) { | |
| 916 FilePath small_wallpaper_path = GetWallpaperPathForUser(email, true); | |
| 917 // Delete previous saved wallpapers. | |
| 918 if (file_util::PathExists(small_wallpaper_path)) | |
| 919 file_util::Delete(small_wallpaper_path, false); | |
| 920 FilePath large_wallpaper_path = GetWallpaperPathForUser(email, false); | |
| 921 if (file_util::PathExists(large_wallpaper_path)) | |
| 922 file_util::Delete(large_wallpaper_path, false); | |
| 923 | |
| 835 std::vector<unsigned char> image_data = wallpaper.raw_image(); | 924 std::vector<unsigned char> image_data = wallpaper.raw_image(); |
| 925 // Saves the original file in case that resized wallpaper is not generated | |
| 926 // (i.e. chrome shutdown before resized wallpaper is saved). | |
| 836 SaveWallpaperInternal(path, reinterpret_cast<char*>(&*image_data.begin()), | 927 SaveWallpaperInternal(path, reinterpret_cast<char*>(&*image_data.begin()), |
| 837 image_data.size()); | 928 image_data.size()); |
| 929 | |
| 930 ResizeAndSaveCustomWallpaper(wallpaper, small_wallpaper_path, layout, | |
| 931 ash::kSmallWallpaperMaxWidth, | |
| 932 ash::kSmallWallpaperMaxHeight); | |
| 933 ResizeAndSaveCustomWallpaper(wallpaper, large_wallpaper_path, layout, | |
| 934 ash::kLargeWallpaperMaxWidth, | |
| 935 ash::kLargeWallpaperMaxHeight); | |
| 936 } | |
| 937 | |
| 938 void WallpaperManager::ResizeAndSaveCustomWallpaper(const UserImage& wallpaper, | |
| 939 const FilePath& path, | |
| 940 ash::WallpaperLayout layout, | |
| 941 int preferred_width, | |
| 942 int preferred_height) { | |
| 943 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 944 int width = wallpaper.image().width(); | |
| 945 int height = wallpaper.image().height(); | |
| 946 int resized_width; | |
| 947 int resized_height; | |
| 948 | |
| 949 if (layout == ash::CENTER_CROPPED) { | |
| 950 // Do not resize custom wallpaper if it is smaller than preferred size. | |
| 951 if (!(width > preferred_width && height > preferred_height)) | |
| 952 return; | |
| 953 | |
| 954 double horizontal_ratio = static_cast<double>(preferred_width) / width; | |
| 955 double vertical_ratio = static_cast<double>(preferred_height) / height; | |
| 956 if (vertical_ratio > horizontal_ratio) { | |
| 957 resized_width = | |
| 958 RoundPositive(static_cast<double>(width) * vertical_ratio); | |
| 959 resized_height = preferred_height; | |
| 960 } else { | |
| 961 resized_width = preferred_width; | |
| 962 resized_height = | |
| 963 RoundPositive(static_cast<double>(height) * horizontal_ratio); | |
| 964 } | |
| 965 } else if (layout == ash::STRETCH) { | |
| 966 resized_width = preferred_width; | |
| 967 resized_height = preferred_height; | |
| 968 } else { | |
| 969 // TODO(bshe): Generates cropped custom wallpaper for CENTER layout. | |
| 970 if (file_util::PathExists(path)) | |
| 971 file_util::Delete(path, false); | |
| 972 return; | |
| 973 } | |
| 974 | |
| 975 gfx::ImageSkia resized_image = gfx::ImageSkiaOperations::CreateResizedImage( | |
| 976 wallpaper.image(), | |
| 977 skia::ImageOperations::RESIZE_LANCZOS3, | |
| 978 gfx::Size(resized_width, resized_height)); | |
| 979 | |
| 980 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes(); | |
| 981 // Uses simple png encoder to encode image on worker pool. So we do not block | |
| 982 // chrome shutdown on image encoding. | |
| 983 SimpleJpegEncoder* jpeg_encoder = new SimpleJpegEncoder( | |
| 984 data, *(resized_image.bitmap())); | |
| 985 jpeg_encoder->Run( | |
| 986 base::Bind(&WallpaperManager::OnWallpaperEncoded, | |
| 987 weak_factory_.GetWeakPtr(), | |
| 988 path)); | |
| 838 } | 989 } |
| 839 | 990 |
| 840 void WallpaperManager::SaveWallpaperInternal(const FilePath& path, | 991 void WallpaperManager::SaveWallpaperInternal(const FilePath& path, |
| 841 const char* data, | 992 const char* data, |
| 842 int size) { | 993 int size) { |
| 843 int written_bytes = file_util::WriteFile(path, data, size); | 994 int written_bytes = file_util::WriteFile(path, data, size); |
| 844 DCHECK(written_bytes == size); | 995 DCHECK(written_bytes == size); |
| 845 } | 996 } |
| 846 | 997 |
| 847 void WallpaperManager::SetWallpaper(const std::string& username, | 998 void WallpaperManager::SetCustomWallpaper(const std::string& username, |
| 848 ash::WallpaperLayout layout, | 999 ash::WallpaperLayout layout, |
| 849 User::WallpaperType type, | 1000 User::WallpaperType type, |
| 850 base::WeakPtr<WallpaperDelegate> delegate, | 1001 base::WeakPtr<WallpaperDelegate> delegate, |
| 851 const UserImage& wallpaper) { | 1002 const UserImage& wallpaper) { |
| 852 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1003 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 853 | 1004 |
| 854 std::string wallpaper_path = | 1005 std::string wallpaper_path = |
| 855 GetWallpaperPathForUser(username, false).value(); | 1006 GetOriginalWallpaperPathForUser(username).value(); |
| 856 | 1007 |
| 857 bool is_persistent = ShouldPersistDataForUser(username); | 1008 bool is_persistent = ShouldPersistDataForUser(username); |
| 858 | 1009 |
| 859 if (is_persistent) { | |
| 860 BrowserThread::PostTask( | |
| 861 BrowserThread::FILE, | |
| 862 FROM_HERE, | |
| 863 base::Bind(&WallpaperManager::SaveWallpaper, | |
| 864 base::Unretained(this), | |
| 865 FilePath(wallpaper_path), | |
| 866 wallpaper)); | |
| 867 } | |
| 868 | |
| 869 BrowserThread::PostTask( | 1010 BrowserThread::PostTask( |
| 870 BrowserThread::FILE, | 1011 BrowserThread::FILE, |
| 871 FROM_HERE, | 1012 FROM_HERE, |
| 872 base::Bind(&WallpaperManager::GenerateUserWallpaperThumbnail, | 1013 base::Bind(&WallpaperManager::GenerateUserWallpaperThumbnail, |
| 873 base::Unretained(this), username, type, delegate, | 1014 base::Unretained(this), username, type, delegate, |
| 874 wallpaper.image())); | 1015 wallpaper.image())); |
| 875 | 1016 |
| 1017 if (is_persistent) { | |
| 1018 BrowserThread::PostTask( | |
| 1019 BrowserThread::FILE, | |
| 1020 FROM_HERE, | |
| 1021 base::Bind(&WallpaperManager::SaveCustomWallpaper, | |
| 1022 base::Unretained(this), | |
| 1023 username, | |
| 1024 FilePath(wallpaper_path), | |
| 1025 layout, | |
| 1026 wallpaper)); | |
| 1027 } | |
| 1028 | |
| 876 ash::Shell::GetInstance()->desktop_background_controller()-> | 1029 ash::Shell::GetInstance()->desktop_background_controller()-> |
| 877 SetCustomWallpaper(wallpaper.image(), layout); | 1030 SetCustomWallpaper(wallpaper.image(), layout); |
| 878 SetUserWallpaperProperties(username, type, layout, is_persistent); | 1031 SetUserWallpaperProperties(username, type, layout, is_persistent); |
| 879 } | 1032 } |
| 880 | 1033 |
| 881 bool WallpaperManager::ShouldPersistDataForUser(const std::string& email) { | 1034 bool WallpaperManager::ShouldPersistDataForUser(const std::string& email) { |
| 882 UserManager* user_manager = UserManager::Get(); | 1035 UserManager* user_manager = UserManager::Get(); |
| 883 // |email| is from user list in local state. We should persist data in this | 1036 // |email| is from user list in local state. We should persist data in this |
| 884 // case. | 1037 // case. |
| 885 if (!user_manager->IsUserLoggedIn()) | 1038 if (!user_manager->IsUserLoggedIn()) |
| 886 return true; | 1039 return true; |
| 887 return !(email == user_manager->GetLoggedInUser().email() && | 1040 return !(email == user_manager->GetLoggedInUser().email() && |
| 888 user_manager->IsCurrentUserEphemeral()); | 1041 user_manager->IsCurrentUserEphemeral()); |
| 889 } | 1042 } |
| 890 | 1043 |
| 891 void WallpaperManager::OnWallpaperLoaded(ash::WallpaperLayout layout, | 1044 void WallpaperManager::OnWallpaperLoaded(ash::WallpaperLayout layout, |
| 892 const UserImage& user_image) { | 1045 const UserImage& user_image) { |
| 893 SetWallpaperFromImageSkia(user_image.image(), layout); | 1046 SetWallpaperFromImageSkia(user_image.image(), layout); |
| 894 } | 1047 } |
| 895 | 1048 |
| 896 void WallpaperManager::SystemResumed() { | 1049 void WallpaperManager::SystemResumed() { |
| 897 BatchUpdateWallpaper(); | 1050 BatchUpdateWallpaper(); |
| 898 } | 1051 } |
| 899 | 1052 |
| 900 void WallpaperManager::TimezoneChanged(const icu::TimeZone& timezone) { | 1053 void WallpaperManager::TimezoneChanged(const icu::TimeZone& timezone) { |
| 901 RestartTimer(); | 1054 RestartTimer(); |
| 902 } | 1055 } |
| 903 | 1056 |
| 904 } // chromeos | 1057 } // chromeos |
| OLD | NEW |