| 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_png_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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 gfx::ImageSkia ImageSkiaDeepCopy(const gfx::ImageSkia& image) { | 91 gfx::ImageSkia ImageSkiaDeepCopy(const gfx::ImageSkia& image) { |
| 91 gfx::ImageSkia copy; | 92 gfx::ImageSkia copy; |
| 92 std::vector<gfx::ImageSkiaRep> reps = image.image_reps(); | 93 std::vector<gfx::ImageSkiaRep> reps = image.image_reps(); |
| 93 for (std::vector<gfx::ImageSkiaRep>::iterator iter = reps.begin(); | 94 for (std::vector<gfx::ImageSkiaRep>::iterator iter = reps.begin(); |
| 94 iter != reps.end(); ++iter) { | 95 iter != reps.end(); ++iter) { |
| 95 copy.AddRepresentation(*iter); | 96 copy.AddRepresentation(*iter); |
| 96 } | 97 } |
| 97 return copy; | 98 return copy; |
| 98 } | 99 } |
| 99 | 100 |
| 101 // For our scaling ratios we need to round positive numbers. |
| 102 int RoundPositive(double x) { |
| 103 return static_cast<int>(floor(x + 0.5)); |
| 104 } |
| 105 |
| 100 } // namespace | 106 } // namespace |
| 101 | 107 |
| 102 namespace chromeos { | 108 namespace chromeos { |
| 103 | 109 |
| 104 static WallpaperManager* g_wallpaper_manager = NULL; | 110 static WallpaperManager* g_wallpaper_manager = NULL; |
| 105 | 111 |
| 106 // WallpaperManager, public: --------------------------------------------------- | 112 // WallpaperManager, public: --------------------------------------------------- |
| 107 | 113 |
| 108 // static | 114 // static |
| 109 WallpaperManager* WallpaperManager::Get() { | 115 WallpaperManager* WallpaperManager::Get() { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 GetLoggedInUserWallpaperProperties(&type, &index, &date); | 164 GetLoggedInUserWallpaperProperties(&type, &index, &date); |
| 159 | 165 |
| 160 if (type == current_user_wallpaper_type_ && | 166 if (type == current_user_wallpaper_type_ && |
| 161 index == current_user_wallpaper_index_) { | 167 index == current_user_wallpaper_index_) { |
| 162 return; | 168 return; |
| 163 } | 169 } |
| 164 } | 170 } |
| 165 SetUserWallpaper(UserManager::Get()->GetLoggedInUser().email()); | 171 SetUserWallpaper(UserManager::Get()->GetLoggedInUser().email()); |
| 166 } | 172 } |
| 167 | 173 |
| 168 void WallpaperManager::FetchCustomWallpaper(const std::string& email) { | 174 void WallpaperManager::GetCustomWallpaper(const std::string& email) { |
| 169 User::WallpaperType type; | 175 User::WallpaperType type; |
| 170 int index; | 176 int index; |
| 171 base::Time date; | 177 base::Time date; |
| 178 // Get layout, type and date while on UI thread. |
| 172 GetUserWallpaperProperties(email, &type, &index, &date); | 179 GetUserWallpaperProperties(email, &type, &index, &date); |
| 173 ash::WallpaperLayout layout = static_cast<ash::WallpaperLayout>(index); | 180 ash::WallpaperLayout layout = static_cast<ash::WallpaperLayout>(index); |
| 174 | 181 // Initializes file name to a dummy string here. The real file path will |
| 175 std::string wallpaper_path = GetWallpaperPathForUser(email, false).value(); | 182 // be set in GetCustomWallpaperInternal. |
| 176 | 183 WallpaperInfo info = { |
| 177 wallpaper_loader_->Start(wallpaper_path, 0, | 184 "DUMMY", |
| 178 base::Bind(&WallpaperManager::FetchWallpaper, | 185 layout, |
| 179 base::Unretained(this), email, layout)); | 186 type, |
| 187 date |
| 188 }; |
| 189 BrowserThread::PostTask( |
| 190 BrowserThread::FILE, FROM_HERE, |
| 191 base::Bind(&WallpaperManager::GetCustomWallpaperInternal, |
| 192 base::Unretained(this), email, info)); |
| 180 } | 193 } |
| 181 | 194 |
| 182 bool WallpaperManager::GetCustomWallpaperFromCache(const std::string& email, | 195 bool WallpaperManager::GetCustomWallpaperFromCache(const std::string& email, |
| 183 gfx::ImageSkia* wallpaper) { | 196 gfx::ImageSkia* wallpaper) { |
| 184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 185 CustomWallpaperMap::const_iterator it = wallpaper_cache_.find(email); | 198 CustomWallpaperMap::const_iterator it = wallpaper_cache_.find(email); |
| 186 if (it != wallpaper_cache_.end()) { | 199 if (it != wallpaper_cache_.end()) { |
| 187 *wallpaper = (*it).second; | 200 *wallpaper = (*it).second; |
| 188 return true; | 201 return true; |
| 189 } | 202 } |
| 190 return false; | 203 return false; |
| 191 } | 204 } |
| 192 | 205 |
| 206 FilePath WallpaperManager::GetOriginalWallpaperPathForUser( |
| 207 const std::string& username) { |
| 208 std::string filename = username + "_wallpaper.png"; |
| 209 FilePath user_data_dir; |
| 210 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 211 return user_data_dir.AppendASCII(filename); |
| 212 } |
| 213 |
| 193 FilePath WallpaperManager::GetWallpaperPathForUser(const std::string& username, | 214 FilePath WallpaperManager::GetWallpaperPathForUser(const std::string& username, |
| 194 bool is_thumbnail) { | 215 bool is_small) { |
| 195 const char* suffix = is_thumbnail ? "_thumb" : ""; | 216 const char* suffix = is_small ? "_small" : "_large"; |
| 196 std::string filename = base::StringPrintf("%s_wallpaper%s.png", | 217 std::string filename = base::StringPrintf("%s_wallpaper%s.png", |
| 197 username.c_str(), | 218 username.c_str(), |
| 198 suffix); | 219 suffix); |
| 199 FilePath user_data_dir; | 220 FilePath user_data_dir; |
| 200 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | 221 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 201 return user_data_dir.AppendASCII(filename); | 222 return user_data_dir.AppendASCII(filename); |
| 202 } | 223 } |
| 203 | 224 |
| 204 gfx::ImageSkia WallpaperManager::GetCustomWallpaperThumbnail( | 225 gfx::ImageSkia WallpaperManager::GetCustomWallpaperThumbnail( |
| 205 const std::string& email) { | 226 const std::string& email) { |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 void WallpaperManager::SetUserWallpaperFromFile( | 396 void WallpaperManager::SetUserWallpaperFromFile( |
| 376 const std::string& username, | 397 const std::string& username, |
| 377 const FilePath& path, | 398 const FilePath& path, |
| 378 ash::WallpaperLayout layout, | 399 ash::WallpaperLayout layout, |
| 379 base::WeakPtr<WallpaperDelegate> delegate) { | 400 base::WeakPtr<WallpaperDelegate> delegate) { |
| 380 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 401 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 381 | 402 |
| 382 // For wallpapers, save the image without resizing. | 403 // For wallpapers, save the image without resizing. |
| 383 wallpaper_loader_->Start( | 404 wallpaper_loader_->Start( |
| 384 path.value(), 0, | 405 path.value(), 0, |
| 385 base::Bind(&WallpaperManager::SetWallpaper, | 406 base::Bind(&WallpaperManager::SetCustomWallpaper, |
| 386 base::Unretained(this), username, layout, User::CUSTOMIZED, | 407 base::Unretained(this), username, layout, User::CUSTOMIZED, |
| 387 delegate)); | 408 delegate)); |
| 388 } | 409 } |
| 389 | 410 |
| 390 void WallpaperManager::SetInitialUserWallpaper(const std::string& username, | 411 void WallpaperManager::SetInitialUserWallpaper(const std::string& username, |
| 391 bool is_persistent) { | 412 bool is_persistent) { |
| 392 current_user_wallpaper_type_ = User::DEFAULT; | 413 current_user_wallpaper_type_ = User::DEFAULT; |
| 393 if (username == kGuestUser) | 414 if (username == kGuestUser) |
| 394 current_user_wallpaper_index_ = ash::GetGuestWallpaperIndex(); | 415 current_user_wallpaper_index_ = ash::GetGuestWallpaperIndex(); |
| 395 else | 416 else |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 if (!UserManager::Get()->IsKnownUser(email)) | 478 if (!UserManager::Get()->IsKnownUser(email)) |
| 458 return; | 479 return; |
| 459 | 480 |
| 460 bool new_wallpaper_ui_enabled = CommandLine::ForCurrentProcess()-> | 481 bool new_wallpaper_ui_enabled = CommandLine::ForCurrentProcess()-> |
| 461 HasSwitch(switches::kEnableNewWallpaperUI); | 482 HasSwitch(switches::kEnableNewWallpaperUI); |
| 462 | 483 |
| 463 WallpaperInfo info; | 484 WallpaperInfo info; |
| 464 | 485 |
| 465 if (new_wallpaper_ui_enabled && | 486 if (new_wallpaper_ui_enabled && |
| 466 GetUserWallpaperInfo(email, &info)) { | 487 GetUserWallpaperInfo(email, &info)) { |
| 488 if (info.type == User::CUSTOMIZED) { |
| 489 if (!UserManager::Get()->IsUserLoggedIn()) { |
| 490 ash::Shell::GetInstance()->desktop_background_controller()-> |
| 491 SetDefaultWallpaper(ash::GetDefaultWallpaperIndex(), false); |
| 492 } else { |
| 493 BrowserThread::PostTask( |
| 494 BrowserThread::FILE, FROM_HERE, |
| 495 base::Bind(&WallpaperManager::GetCustomWallpaperInternal, |
| 496 base::Unretained(this), email, info)); |
| 497 } |
| 498 return; |
| 499 } |
| 500 |
| 467 if (info.file_name.empty()) { | 501 if (info.file_name.empty()) { |
| 468 // Uses default built-in wallpaper when file name is empty. Eventually, | 502 // Uses default built-in wallpaper when file name is empty. Eventually, |
| 469 // we will only ship one built-in wallpaper in ChromeOS image. | 503 // we will only ship one built-in wallpaper in ChromeOS image. |
| 470 ash::Shell::GetInstance()->desktop_background_controller()-> | 504 ash::Shell::GetInstance()->desktop_background_controller()-> |
| 471 SetDefaultWallpaper(ash::GetDefaultWallpaperIndex(), false); | 505 SetDefaultWallpaper(ash::GetDefaultWallpaperIndex(), false); |
| 472 return; | 506 return; |
| 473 } | 507 } |
| 508 |
| 474 LoadWallpaper(email, info); | 509 LoadWallpaper(email, info); |
| 475 } else { | 510 } else { |
| 476 User::WallpaperType type; | 511 User::WallpaperType type; |
| 477 int index; | 512 int index; |
| 478 base::Time date; | 513 base::Time date; |
| 479 GetUserWallpaperProperties(email, &type, &index, &date); | 514 GetUserWallpaperProperties(email, &type, &index, &date); |
| 480 if (type == User::DAILY && date != base::Time::Now().LocalMidnight()) { | 515 if (type == User::DAILY && date != base::Time::Now().LocalMidnight()) { |
| 481 index = ash::GetNextWallpaperIndex(index); | 516 index = ash::GetNextWallpaperIndex(index); |
| 482 SetUserWallpaperProperties(email, User::DAILY, index, | 517 SetUserWallpaperProperties(email, User::DAILY, index, |
| 483 ShouldPersistDataForUser(email)); | 518 ShouldPersistDataForUser(email)); |
| 484 } else if (type == User::CUSTOMIZED) { | 519 } else if (type == User::CUSTOMIZED) { |
| 485 // For security reason, use default wallpaper instead of custom wallpaper | 520 // For security reason, use default wallpaper instead of custom wallpaper |
| 486 // at login screen. The security issue is tracked in issue 143198. Once it | 521 // at login screen. The security issue is tracked in issue 143198. Once it |
| 487 // fixed, we should then only use custom wallpaper. | 522 // fixed, we should then only use custom wallpaper. |
| 488 if (!UserManager::Get()->IsUserLoggedIn()) { | 523 if (!UserManager::Get()->IsUserLoggedIn()) { |
| 489 index = ash::GetDefaultWallpaperIndex(); | 524 index = ash::GetDefaultWallpaperIndex(); |
| 490 } else { | 525 } else { |
| 491 FetchCustomWallpaper(email); | 526 GetCustomWallpaper(email); |
| 492 return; | 527 return; |
| 493 } | 528 } |
| 494 } | 529 } |
| 495 ash::Shell::GetInstance()->desktop_background_controller()-> | 530 ash::Shell::GetInstance()->desktop_background_controller()-> |
| 496 SetDefaultWallpaper(index, false); | 531 SetDefaultWallpaper(index, false); |
| 497 if (new_wallpaper_ui_enabled) | 532 if (new_wallpaper_ui_enabled) |
| 498 MigrateBuiltInWallpaper(email); | 533 MigrateBuiltInWallpaper(email); |
| 499 } | 534 } |
| 500 SetLastSelectedUser(email); | 535 SetLastSelectedUser(email); |
| 501 } | 536 } |
| 502 | 537 |
| 503 void WallpaperManager::SetWallpaperFromImageSkia( | 538 void WallpaperManager::SetWallpaperFromImageSkia( |
| 504 const gfx::ImageSkia& wallpaper, | 539 const gfx::ImageSkia& wallpaper, |
| 505 ash::WallpaperLayout layout) { | 540 ash::WallpaperLayout layout) { |
| 506 ash::Shell::GetInstance()->desktop_background_controller()-> | 541 ash::Shell::GetInstance()->desktop_background_controller()-> |
| 507 SetCustomWallpaper(wallpaper, layout); | 542 SetCustomWallpaper(wallpaper, layout); |
| 508 } | 543 } |
| 509 | 544 |
| 510 void WallpaperManager::OnUserSelected(const std::string& email) { | 545 void WallpaperManager::OnUserSelected(const std::string& email) { |
| 511 SetUserWallpaper(email); | 546 SetUserWallpaper(email); |
| 512 } | 547 } |
| 513 | 548 |
| 514 // WallpaperManager, private: -------------------------------------------------- | 549 // WallpaperManager, private: -------------------------------------------------- |
| 515 | 550 |
| 516 WallpaperManager::~WallpaperManager() { | 551 WallpaperManager::~WallpaperManager() { |
| 517 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this); | 552 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this); |
| 518 system::TimezoneSettings::GetInstance()->RemoveObserver(this); | 553 system::TimezoneSettings::GetInstance()->RemoveObserver(this); |
| 554 weak_factory_.InvalidateWeakPtrs(); |
| 519 } | 555 } |
| 520 | 556 |
| 521 void WallpaperManager::BatchUpdateWallpaper() { | 557 void WallpaperManager::BatchUpdateWallpaper() { |
| 522 PrefService* local_state = g_browser_process->local_state(); | 558 PrefService* local_state = g_browser_process->local_state(); |
| 523 UserManager* user_manager = UserManager::Get(); | 559 UserManager* user_manager = UserManager::Get(); |
| 524 bool show_users = true; | 560 bool show_users = true; |
| 525 CrosSettings::Get()->GetBoolean( | 561 CrosSettings::Get()->GetBoolean( |
| 526 kAccountsPrefShowUserNamesOnSignIn, &show_users); | 562 kAccountsPrefShowUserNamesOnSignIn, &show_users); |
| 527 if (local_state) { | 563 if (local_state) { |
| 528 User::WallpaperType type; | 564 User::WallpaperType type; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 FilePath wallpaper_path; | 617 FilePath wallpaper_path; |
| 582 if (info.type == User::CUSTOMIZED) { | 618 if (info.type == User::CUSTOMIZED) { |
| 583 ash::Shell::GetInstance()->desktop_background_controller()-> | 619 ash::Shell::GetInstance()->desktop_background_controller()-> |
| 584 CacheDefaultWallpaper(ash::GetDefaultWallpaperIndex()); | 620 CacheDefaultWallpaper(ash::GetDefaultWallpaperIndex()); |
| 585 return; | 621 return; |
| 586 } | 622 } |
| 587 if (info.type == User::ONLINE) { | 623 if (info.type == User::ONLINE) { |
| 588 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, | 624 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, |
| 589 &wallpaper_dir)); | 625 &wallpaper_dir)); |
| 590 wallpaper_path = wallpaper_dir.Append(info.file_name); | 626 wallpaper_path = wallpaper_dir.Append(info.file_name); |
| 591 } else { | 627 } else if (info.type == User::DEFAULT) { |
| 628 // Current wallpaper is converted from built-in wallpaper. |
| 592 ash::WallpaperResolution resolution = ash::Shell::GetInstance()-> | 629 ash::WallpaperResolution resolution = ash::Shell::GetInstance()-> |
| 593 desktop_background_controller()->GetAppropriateResolution(); | 630 desktop_background_controller()->GetAppropriateResolution(); |
| 594 bool small_resolution = (resolution == ash::SMALL); | 631 bool small_resolution = (resolution == ash::SMALL); |
| 595 wallpaper_path = GetWallpaperPathForUser(email, small_resolution); | 632 wallpaper_path = GetWallpaperPathForUser(email, small_resolution); |
| 633 } else { |
| 634 // Cache default wallpaper when user selected a custom wallpaper. |
| 635 ash::Shell::GetInstance()->desktop_background_controller()-> |
| 636 CacheDefaultWallpaper(ash::GetDefaultWallpaperIndex()); |
| 637 return; |
| 596 } | 638 } |
| 597 wallpaper_loader_->Start(wallpaper_path.value(), 0, | 639 wallpaper_loader_->Start(wallpaper_path.value(), 0, |
| 598 base::Bind(&WallpaperManager::CacheWallpaper, | 640 base::Bind(&WallpaperManager::CacheWallpaper, |
| 599 base::Unretained(this), email)); | 641 base::Unretained(this), email)); |
| 600 } else { | 642 } else { |
| 601 MigrateBuiltInWallpaper(email); | 643 MigrateBuiltInWallpaper(email); |
| 602 return; | 644 return; |
| 603 } | 645 } |
| 604 } else { | 646 } else { |
| 605 User::WallpaperType type; | 647 User::WallpaperType type; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 638 } | 680 } |
| 639 | 681 |
| 640 void WallpaperManager::LoadWallpaper(const std::string& email, | 682 void WallpaperManager::LoadWallpaper(const std::string& email, |
| 641 const WallpaperInfo& info) { | 683 const WallpaperInfo& info) { |
| 642 FilePath wallpaper_dir; | 684 FilePath wallpaper_dir; |
| 643 FilePath wallpaper_path; | 685 FilePath wallpaper_path; |
| 644 if (info.type == User::ONLINE) { | 686 if (info.type == User::ONLINE) { |
| 645 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir)); | 687 CHECK(PathService::Get(chrome::DIR_CHROMEOS_WALLPAPERS, &wallpaper_dir)); |
| 646 wallpaper_path = wallpaper_dir.Append(info.file_name); | 688 wallpaper_path = wallpaper_dir.Append(info.file_name); |
| 647 } else { | 689 } else { |
| 648 ash::WallpaperResolution resolution = ash::Shell::GetInstance()-> | 690 FilePath user_data_dir; |
| 649 desktop_background_controller()->GetAppropriateResolution(); | 691 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 650 bool small_resolution = (resolution == ash::SMALL); | 692 wallpaper_path = user_data_dir.Append(info.file_name); |
| 651 wallpaper_path = GetWallpaperPathForUser(email, small_resolution); | |
| 652 } | 693 } |
| 653 | 694 |
| 654 wallpaper_loader_->Start(wallpaper_path.value(), 0, | 695 wallpaper_loader_->Start(wallpaper_path.value(), 0, |
| 655 base::Bind(&WallpaperManager::FetchWallpaper, | 696 base::Bind(&WallpaperManager::FetchWallpaper, |
| 656 base::Unretained(this), | 697 base::Unretained(this), |
| 657 email, | 698 email, |
| 658 info.layout)); | 699 info.layout)); |
| 659 } | 700 } |
| 660 | 701 |
| 661 void WallpaperManager::FetchWallpaper(const std::string& email, | 702 void WallpaperManager::FetchWallpaper(const std::string& email, |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 760 custom_wallpaper_thumbnail_cache_[email] = GetWallpaperThumbnail(wallpaper); | 801 custom_wallpaper_thumbnail_cache_[email] = GetWallpaperThumbnail(wallpaper); |
| 761 | 802 |
| 762 // Notify thumbnail is ready. | 803 // Notify thumbnail is ready. |
| 763 BrowserThread::PostTask( | 804 BrowserThread::PostTask( |
| 764 BrowserThread::UI, | 805 BrowserThread::UI, |
| 765 FROM_HERE, | 806 FROM_HERE, |
| 766 base::Bind(&WallpaperManager::OnThumbnailUpdated, | 807 base::Bind(&WallpaperManager::OnThumbnailUpdated, |
| 767 base::Unretained(this), delegate)); | 808 base::Unretained(this), delegate)); |
| 768 } | 809 } |
| 769 | 810 |
| 811 void WallpaperManager::GetCustomWallpaperInternal(const std::string& email, |
| 812 const WallpaperInfo& info) { |
| 813 ash::WallpaperResolution resolution = ash::Shell::GetInstance()-> |
| 814 desktop_background_controller()->GetAppropriateResolution(); |
| 815 bool is_small = (resolution == ash::SMALL); |
| 816 FilePath wallpaper_path = GetWallpaperPathForUser(email, is_small); |
| 817 std::string file_name = wallpaper_path.BaseName().value(); |
| 818 // Falls back on original file if the correct resoltuion file does not |
| 819 // exisit. This may happen when the original custom wallpaper is small or |
| 820 // browser shutdown before resized wallpaper saved. |
| 821 if (!file_util::PathExists(wallpaper_path)) |
| 822 file_name = GetOriginalWallpaperPathForUser(email).BaseName().value(); |
| 823 |
| 824 WallpaperInfo new_info = { |
| 825 file_name, |
| 826 info.layout, |
| 827 info.type, |
| 828 info.date |
| 829 }; |
| 830 |
| 831 BrowserThread::PostTask( |
| 832 BrowserThread::UI, FROM_HERE, |
| 833 base::Bind(&WallpaperManager::LoadWallpaper, |
| 834 base::Unretained(this), email, new_info)); |
| 835 } |
| 836 |
| 770 void WallpaperManager::MigrateBuiltInWallpaper(const std::string& email) { | 837 void WallpaperManager::MigrateBuiltInWallpaper(const std::string& email) { |
| 771 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 838 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 772 if (!email.empty() && UserManager::Get()->IsKnownUser(email)) { | 839 if (!email.empty() && UserManager::Get()->IsKnownUser(email)) { |
| 773 PrefService* prefs = g_browser_process->local_state(); | 840 PrefService* prefs = g_browser_process->local_state(); |
| 774 const DictionaryValue* user_wallpapers = prefs-> | 841 const DictionaryValue* user_wallpapers = prefs-> |
| 775 GetDictionary(UserManager::kUserWallpapersProperties); | 842 GetDictionary(UserManager::kUserWallpapersProperties); |
| 776 const base::DictionaryValue* wallpaper_properties; | 843 const base::DictionaryValue* wallpaper_properties; |
| 777 User::WallpaperType type; | 844 User::WallpaperType type; |
| 778 int index; | 845 int index; |
| 779 ash::WallpaperResolution resolution = ash::Shell::GetInstance()-> | 846 ash::WallpaperResolution resolution = ash::Shell::GetInstance()-> |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 815 | 882 |
| 816 // Saves small wallpaper to user custom wallpaper thumbnail path. | 883 // Saves small wallpaper to user custom wallpaper thumbnail path. |
| 817 wallpaper_path = GetWallpaperPathForUser(email, true); | 884 wallpaper_path = GetWallpaperPathForUser(email, true); |
| 818 BrowserThread::PostTask( | 885 BrowserThread::PostTask( |
| 819 BrowserThread::FILE, | 886 BrowserThread::FILE, |
| 820 FROM_HERE, | 887 FROM_HERE, |
| 821 base::Bind(&WallpaperManager::SaveWallpaperInternal, | 888 base::Bind(&WallpaperManager::SaveWallpaperInternal, |
| 822 base::Unretained(this), wallpaper_path, | 889 base::Unretained(this), wallpaper_path, |
| 823 reinterpret_cast<const char*>(wallpaper->front()), | 890 reinterpret_cast<const char*>(wallpaper->front()), |
| 824 wallpaper->size())); | 891 wallpaper->size())); |
| 825 } else { | |
| 826 // Since we only have one resolution for custom wallpaper, always | |
| 827 // use LARGE resolution for custom wallpaper. | |
| 828 resolution = ash::LARGE; | |
| 829 } | 892 } |
| 830 | 893 |
| 831 bool small_resolution = (resolution == ash::SMALL); | 894 bool small_resolution = (resolution == ash::SMALL); |
| 832 std::string file_name = | 895 std::string file_name = |
| 833 GetWallpaperPathForUser(email, small_resolution).BaseName().value(); | 896 GetWallpaperPathForUser(email, small_resolution).BaseName().value(); |
| 834 WallpaperInfo info = { | 897 WallpaperInfo info = { |
| 835 file_name, | 898 file_name, |
| 836 GetWallpaperViewInfo(index, resolution).layout, | 899 GetWallpaperViewInfo(index, resolution).layout, |
| 837 type, | 900 type, |
| 838 base::Time::Now().LocalMidnight() | 901 base::Time::Now().LocalMidnight() |
| 839 }; | 902 }; |
| 840 SetUserWallpaperInfo(email, info, true); | 903 SetUserWallpaperInfo(email, info, true); |
| 841 } | 904 } |
| 842 } | 905 } |
| 843 } | 906 } |
| 844 | 907 |
| 845 void WallpaperManager::OnThumbnailUpdated( | 908 void WallpaperManager::OnThumbnailUpdated( |
| 846 base::WeakPtr<WallpaperDelegate> delegate) { | 909 base::WeakPtr<WallpaperDelegate> delegate) { |
| 847 if (delegate) | 910 if (delegate) |
| 848 delegate->SetCustomWallpaperThumbnail(); | 911 delegate->SetCustomWallpaperThumbnail(); |
| 849 } | 912 } |
| 850 | 913 |
| 851 void WallpaperManager::SaveWallpaper(const FilePath& path, | 914 void WallpaperManager::OnWallpaperEncoded(const FilePath& path, |
| 852 const UserImage& wallpaper) { | 915 scoped_refptr<base::RefCountedBytes> data) { |
| 916 SaveWallpaperInternal(path, |
| 917 reinterpret_cast<const char*>(data->front()), |
| 918 data->size()); |
| 919 } |
| 920 |
| 921 void WallpaperManager::SaveCustomWallpaper(const std::string& email, |
| 922 const FilePath& path, |
| 923 ash::WallpaperLayout layout, |
| 924 const UserImage& wallpaper) { |
| 925 FilePath small_wallpaper_path = GetWallpaperPathForUser(email, true); |
| 926 // Delete previous saved wallpapers. |
| 927 if (file_util::PathExists(small_wallpaper_path)) |
| 928 file_util::Delete(small_wallpaper_path, false); |
| 929 FilePath large_wallpaper_path = GetWallpaperPathForUser(email, false); |
| 930 if (file_util::PathExists(large_wallpaper_path)) |
| 931 file_util::Delete(large_wallpaper_path, false); |
| 932 |
| 853 std::vector<unsigned char> image_data = wallpaper.raw_image(); | 933 std::vector<unsigned char> image_data = wallpaper.raw_image(); |
| 934 // Saves the original file in case that resized wallpaper is not generated |
| 935 // (i.e. chrome shutdown before resized wallpaper is saved). |
| 854 SaveWallpaperInternal(path, reinterpret_cast<char*>(&*image_data.begin()), | 936 SaveWallpaperInternal(path, reinterpret_cast<char*>(&*image_data.begin()), |
| 855 image_data.size()); | 937 image_data.size()); |
| 938 |
| 939 ResizeAndSaveCustomWallpaper(wallpaper, small_wallpaper_path, layout, |
| 940 ash::kSmallWallpaperMaximalWidth, |
| 941 ash::kSmallWallpaperMaximalHeight); |
| 942 ResizeAndSaveCustomWallpaper(wallpaper, large_wallpaper_path, layout, |
| 943 ash::kLargeWallpaperMaximalWidth, |
| 944 ash::kLargeWallpaperMaximalHeight); |
| 945 } |
| 946 |
| 947 void WallpaperManager::ResizeAndSaveCustomWallpaper(const UserImage& wallpaper, |
| 948 const FilePath& path, |
| 949 ash::WallpaperLayout layout, |
| 950 int preferred_width, |
| 951 int preferred_height) { |
| 952 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 953 int width = wallpaper.image().width(); |
| 954 int height = wallpaper.image().height(); |
| 955 int resized_width; |
| 956 int resized_height; |
| 957 |
| 958 if (layout == ash::CENTER_CROPPED) { |
| 959 // Do not resize custom wallpaper if it is smaller than preferred size. |
| 960 if (!(width > preferred_width && height > preferred_height)) |
| 961 return; |
| 962 |
| 963 double horizontal_ratio = static_cast<double>(preferred_width) / |
| 964 static_cast<double>(width); |
| 965 double vertical_ratio = static_cast<double>(preferred_height) / |
| 966 static_cast<double>(height); |
| 967 if (vertical_ratio > horizontal_ratio) { |
| 968 resized_width = |
| 969 RoundPositive(static_cast<double>(width) * vertical_ratio); |
| 970 resized_height = preferred_height; |
| 971 } else { |
| 972 resized_width = preferred_width; |
| 973 resized_height = |
| 974 RoundPositive(static_cast<double>(height) * horizontal_ratio); |
| 975 } |
| 976 } else if (layout == ash::STRETCH) { |
| 977 resized_width = preferred_width; |
| 978 resized_height = preferred_height; |
| 979 } else { |
| 980 // TODO(bshe): Generates cropped custom wallpaper for CENTER layout. |
| 981 if (file_util::PathExists(path)) |
| 982 file_util::Delete(path, false); |
| 983 return; |
| 984 } |
| 985 |
| 986 gfx::ImageSkia resized_image = gfx::ImageSkiaOperations::CreateResizedImage( |
| 987 wallpaper.image(), |
| 988 skia::ImageOperations::RESIZE_LANCZOS3, |
| 989 gfx::Size(resized_width, resized_height)); |
| 990 |
| 991 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes(); |
| 992 // Uses simple png encoder to encode image on worker pool. So we do not block |
| 993 // chrome shutdown on image encoding. |
| 994 SimplePngEncoder* png_encoder = new SimplePngEncoder( |
| 995 data, *(resized_image.bitmap())); |
| 996 png_encoder->Run( |
| 997 base::Bind(&WallpaperManager::OnWallpaperEncoded, |
| 998 weak_factory_.GetWeakPtr(), |
| 999 path)); |
| 856 } | 1000 } |
| 857 | 1001 |
| 858 void WallpaperManager::SaveWallpaperInternal(const FilePath& path, | 1002 void WallpaperManager::SaveWallpaperInternal(const FilePath& path, |
| 859 const char* data, | 1003 const char* data, |
| 860 int size) { | 1004 int size) { |
| 861 int written_bytes = file_util::WriteFile(path, data, size); | 1005 int written_bytes = file_util::WriteFile(path, data, size); |
| 862 DCHECK(written_bytes == size); | 1006 DCHECK(written_bytes == size); |
| 863 } | 1007 } |
| 864 | 1008 |
| 865 void WallpaperManager::SetWallpaper(const std::string& username, | 1009 void WallpaperManager::SetCustomWallpaper(const std::string& username, |
| 866 ash::WallpaperLayout layout, | 1010 ash::WallpaperLayout layout, |
| 867 User::WallpaperType type, | 1011 User::WallpaperType type, |
| 868 base::WeakPtr<WallpaperDelegate> delegate, | 1012 base::WeakPtr<WallpaperDelegate> delegate, |
| 869 const UserImage& wallpaper) { | 1013 const UserImage& wallpaper) { |
| 870 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1014 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 871 | 1015 |
| 872 std::string wallpaper_path = | 1016 std::string wallpaper_path = |
| 873 GetWallpaperPathForUser(username, false).value(); | 1017 GetOriginalWallpaperPathForUser(username).value(); |
| 874 | 1018 |
| 875 bool is_persistent = ShouldPersistDataForUser(username); | 1019 bool is_persistent = ShouldPersistDataForUser(username); |
| 876 | 1020 |
| 877 if (is_persistent) { | |
| 878 BrowserThread::PostTask( | |
| 879 BrowserThread::FILE, | |
| 880 FROM_HERE, | |
| 881 base::Bind(&WallpaperManager::SaveWallpaper, | |
| 882 base::Unretained(this), | |
| 883 FilePath(wallpaper_path), | |
| 884 wallpaper)); | |
| 885 } | |
| 886 | |
| 887 BrowserThread::PostTask( | 1021 BrowserThread::PostTask( |
| 888 BrowserThread::FILE, | 1022 BrowserThread::FILE, |
| 889 FROM_HERE, | 1023 FROM_HERE, |
| 890 base::Bind(&WallpaperManager::GenerateUserWallpaperThumbnail, | 1024 base::Bind(&WallpaperManager::GenerateUserWallpaperThumbnail, |
| 891 base::Unretained(this), username, type, delegate, | 1025 base::Unretained(this), username, type, delegate, |
| 892 wallpaper.image())); | 1026 wallpaper.image())); |
| 893 | 1027 |
| 1028 if (is_persistent) { |
| 1029 BrowserThread::PostTask( |
| 1030 BrowserThread::FILE, |
| 1031 FROM_HERE, |
| 1032 base::Bind(&WallpaperManager::SaveCustomWallpaper, |
| 1033 base::Unretained(this), |
| 1034 username, |
| 1035 FilePath(wallpaper_path), |
| 1036 layout, |
| 1037 wallpaper)); |
| 1038 } |
| 1039 |
| 894 ash::Shell::GetInstance()->desktop_background_controller()-> | 1040 ash::Shell::GetInstance()->desktop_background_controller()-> |
| 895 SetCustomWallpaper(wallpaper.image(), layout); | 1041 SetCustomWallpaper(wallpaper.image(), layout); |
| 896 SetUserWallpaperProperties(username, type, layout, is_persistent); | 1042 SetUserWallpaperProperties(username, type, layout, is_persistent); |
| 897 } | 1043 } |
| 898 | 1044 |
| 899 bool WallpaperManager::ShouldPersistDataForUser(const std::string& email) { | 1045 bool WallpaperManager::ShouldPersistDataForUser(const std::string& email) { |
| 900 UserManager* user_manager = UserManager::Get(); | 1046 UserManager* user_manager = UserManager::Get(); |
| 901 // |email| is from user list in local state. We should persist data in this | 1047 // |email| is from user list in local state. We should persist data in this |
| 902 // case. | 1048 // case. |
| 903 if (!user_manager->IsUserLoggedIn()) | 1049 if (!user_manager->IsUserLoggedIn()) |
| 904 return true; | 1050 return true; |
| 905 return !(email == user_manager->GetLoggedInUser().email() && | 1051 return !(email == user_manager->GetLoggedInUser().email() && |
| 906 user_manager->IsCurrentUserEphemeral()); | 1052 user_manager->IsCurrentUserEphemeral()); |
| 907 } | 1053 } |
| 908 | 1054 |
| 909 void WallpaperManager::OnWallpaperLoaded(ash::WallpaperLayout layout, | 1055 void WallpaperManager::OnWallpaperLoaded(ash::WallpaperLayout layout, |
| 910 const UserImage& user_image) { | 1056 const UserImage& user_image) { |
| 911 SetWallpaperFromImageSkia(user_image.image(), layout); | 1057 SetWallpaperFromImageSkia(user_image.image(), layout); |
| 912 } | 1058 } |
| 913 | 1059 |
| 914 void WallpaperManager::SystemResumed() { | 1060 void WallpaperManager::SystemResumed() { |
| 915 BatchUpdateWallpaper(); | 1061 BatchUpdateWallpaper(); |
| 916 } | 1062 } |
| 917 | 1063 |
| 918 void WallpaperManager::TimezoneChanged(const icu::TimeZone& timezone) { | 1064 void WallpaperManager::TimezoneChanged(const icu::TimeZone& timezone) { |
| 919 RestartTimer(); | 1065 RestartTimer(); |
| 920 } | 1066 } |
| 921 | 1067 |
| 922 } // chromeos | 1068 } // chromeos |
| OLD | NEW |