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