| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <numeric> | 7 #include <numeric> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "ash/ash_switches.h" |
| 11 #include "ash/desktop_background/desktop_background_controller.h" |
| 10 #include "ash/shell.h" | 12 #include "ash/shell.h" |
| 11 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 12 #include "base/debug/trace_event.h" | 14 #include "base/debug/trace_event.h" |
| 13 #include "base/file_util.h" | 15 #include "base/file_util.h" |
| 14 #include "base/files/file_enumerator.h" | 16 #include "base/files/file_enumerator.h" |
| 15 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
| 16 #include "base/logging.h" | 18 #include "base/logging.h" |
| 17 #include "base/metrics/histogram.h" | 19 #include "base/metrics/histogram.h" |
| 18 #include "base/path_service.h" | 20 #include "base/path_service.h" |
| 19 #include "base/prefs/pref_registry_simple.h" | 21 #include "base/prefs/pref_registry_simple.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 39 #include "chromeos/dbus/dbus_thread_manager.h" | 41 #include "chromeos/dbus/dbus_thread_manager.h" |
| 40 #include "content/public/browser/browser_thread.h" | 42 #include "content/public/browser/browser_thread.h" |
| 41 #include "content/public/browser/notification_service.h" | 43 #include "content/public/browser/notification_service.h" |
| 42 #include "ui/base/resource/resource_bundle.h" | 44 #include "ui/base/resource/resource_bundle.h" |
| 43 #include "ui/gfx/codec/jpeg_codec.h" | 45 #include "ui/gfx/codec/jpeg_codec.h" |
| 44 #include "ui/gfx/image/image_skia_operations.h" | 46 #include "ui/gfx/image/image_skia_operations.h" |
| 45 #include "ui/gfx/skia_util.h" | 47 #include "ui/gfx/skia_util.h" |
| 46 | 48 |
| 47 using content::BrowserThread; | 49 using content::BrowserThread; |
| 48 | 50 |
| 51 namespace chromeos { |
| 52 |
| 49 namespace { | 53 namespace { |
| 50 | 54 |
| 51 // The amount of delay before starts to move custom wallpapers to the new place. | 55 // The amount of delay before starts to move custom wallpapers to the new place. |
| 52 const int kMoveCustomWallpaperDelaySeconds = 30; | 56 const int kMoveCustomWallpaperDelaySeconds = 30; |
| 53 | 57 |
| 54 // Default quality for encoding wallpaper. | 58 // Default quality for encoding wallpaper. |
| 55 const int kDefaultEncodingQuality = 90; | 59 const int kDefaultEncodingQuality = 90; |
| 56 | 60 |
| 57 // A dictionary pref that maps usernames to file paths to their wallpapers. | 61 // A dictionary pref that maps usernames to file paths to their wallpapers. |
| 58 // Deprecated. Will remove this const char after done migration. | 62 // Deprecated. Will remove this const char after done migration. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 const std::string& user_id, | 109 const std::string& user_id, |
| 106 const std::string& user_id_hash) { | 110 const std::string& user_id_hash) { |
| 107 base::FilePath base_path = GetCustomWallpaperDir(sub_dir); | 111 base::FilePath base_path = GetCustomWallpaperDir(sub_dir); |
| 108 base::FilePath to_path = base_path.Append(user_id_hash); | 112 base::FilePath to_path = base_path.Append(user_id_hash); |
| 109 base::FilePath from_path = base_path.Append(user_id); | 113 base::FilePath from_path = base_path.Append(user_id); |
| 110 if (base::PathExists(from_path)) | 114 if (base::PathExists(from_path)) |
| 111 return base::Move(from_path, to_path); | 115 return base::Move(from_path, to_path); |
| 112 return false; | 116 return false; |
| 113 } | 117 } |
| 114 | 118 |
| 119 void CheckCustomizedWallpaperFilesExist( |
| 120 const base::FilePath& downloaded_file, |
| 121 const WallpaperManager::CustomizedWallpaperRescaledFiles* rescaled_files, |
| 122 WallpaperManager::CustomizedWallpaperFilesExist* exist) { |
| 123 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 124 DCHECK(rescaled_files); |
| 125 DCHECK(exist); |
| 126 exist->downloaded = base::PathExists(downloaded_file); |
| 127 exist->rescaled_small = base::PathExists(rescaled_files->path_rescaled_small); |
| 128 exist->rescaled_large = base::PathExists(rescaled_files->path_rescaled_large); |
| 129 } |
| 130 |
| 115 } // namespace | 131 } // namespace |
| 116 | 132 |
| 117 namespace chromeos { | |
| 118 | |
| 119 const char kWallpaperSequenceTokenName[] = "wallpaper-sequence"; | 133 const char kWallpaperSequenceTokenName[] = "wallpaper-sequence"; |
| 120 | 134 |
| 121 const char kSmallWallpaperSuffix[] = "_small"; | 135 const char kSmallWallpaperSuffix[] = "_small"; |
| 122 const char kLargeWallpaperSuffix[] = "_large"; | 136 const char kLargeWallpaperSuffix[] = "_large"; |
| 123 | 137 |
| 124 const char kSmallWallpaperSubDir[] = "small"; | 138 const char kSmallWallpaperSubDir[] = "small"; |
| 125 const char kLargeWallpaperSubDir[] = "large"; | 139 const char kLargeWallpaperSubDir[] = "large"; |
| 126 const char kOriginalWallpaperSubDir[] = "original"; | 140 const char kOriginalWallpaperSubDir[] = "original"; |
| 127 const char kThumbnailWallpaperSubDir[] = "thumb"; | 141 const char kThumbnailWallpaperSubDir[] = "thumb"; |
| 128 | 142 |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 i != manager->loading_.end(); | 273 i != manager->loading_.end(); |
| 260 ++i) | 274 ++i) |
| 261 if (i->get() == this) { | 275 if (i->get() == this) { |
| 262 manager->loading_.erase(i); | 276 manager->loading_.erase(i); |
| 263 break; | 277 break; |
| 264 } | 278 } |
| 265 } | 279 } |
| 266 | 280 |
| 267 // WallpaperManager, public: --------------------------------------------------- | 281 // WallpaperManager, public: --------------------------------------------------- |
| 268 | 282 |
| 283 WallpaperManager::CustomizedWallpaperRescaledFiles:: |
| 284 CustomizedWallpaperRescaledFiles(const base::FilePath& path_rescaled_small, |
| 285 const base::FilePath& path_rescaled_large) |
| 286 : path_rescaled_small(path_rescaled_small), |
| 287 path_rescaled_large(path_rescaled_large) { |
| 288 } |
| 289 |
| 290 WallpaperManager::CustomizedWallpaperFilesExist::CustomizedWallpaperFilesExist() |
| 291 : downloaded(false), rescaled_small(false), rescaled_large(false) { |
| 292 } |
| 293 |
| 294 bool WallpaperManager::CustomizedWallpaperFilesExist::AllRescaledExist() const { |
| 295 return rescaled_small && rescaled_large; |
| 296 } |
| 297 |
| 269 // TestApi. For testing purpose | 298 // TestApi. For testing purpose |
| 270 WallpaperManager::TestApi::TestApi(WallpaperManager* wallpaper_manager) | 299 WallpaperManager::TestApi::TestApi(WallpaperManager* wallpaper_manager) |
| 271 : wallpaper_manager_(wallpaper_manager) { | 300 : wallpaper_manager_(wallpaper_manager) { |
| 272 } | 301 } |
| 273 | 302 |
| 274 WallpaperManager::TestApi::~TestApi() { | 303 WallpaperManager::TestApi::~TestApi() { |
| 275 } | 304 } |
| 276 | 305 |
| 277 base::FilePath WallpaperManager::TestApi::current_wallpaper_path() { | 306 base::FilePath WallpaperManager::TestApi::current_wallpaper_path() { |
| 278 return wallpaper_manager_->current_wallpaper_path_; | 307 return wallpaper_manager_->current_wallpaper_path_; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 299 g_wallpaper_manager = new WallpaperManager(); | 328 g_wallpaper_manager = new WallpaperManager(); |
| 300 return g_wallpaper_manager; | 329 return g_wallpaper_manager; |
| 301 } | 330 } |
| 302 | 331 |
| 303 WallpaperManager::WallpaperManager() | 332 WallpaperManager::WallpaperManager() |
| 304 : loaded_wallpapers_(0), | 333 : loaded_wallpapers_(0), |
| 305 command_line_for_testing_(NULL), | 334 command_line_for_testing_(NULL), |
| 306 should_cache_wallpaper_(false), | 335 should_cache_wallpaper_(false), |
| 307 weak_factory_(this), | 336 weak_factory_(this), |
| 308 pending_inactive_(NULL) { | 337 pending_inactive_(NULL) { |
| 338 SetDefaultWallpaperPathFromCommandLine( |
| 339 base::CommandLine::ForCurrentProcess()); |
| 309 registrar_.Add(this, | 340 registrar_.Add(this, |
| 310 chrome::NOTIFICATION_LOGIN_USER_CHANGED, | 341 chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
| 311 content::NotificationService::AllSources()); | 342 content::NotificationService::AllSources()); |
| 312 registrar_.Add(this, | 343 registrar_.Add(this, |
| 313 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, | 344 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, |
| 314 content::NotificationService::AllSources()); | 345 content::NotificationService::AllSources()); |
| 315 registrar_.Add(this, | 346 registrar_.Add(this, |
| 316 chrome::NOTIFICATION_WALLPAPER_ANIMATION_FINISHED, | 347 chrome::NOTIFICATION_WALLPAPER_ANIMATION_FINISHED, |
| 317 content::NotificationService::AllSources()); | 348 content::NotificationService::AllSources()); |
| 318 sequence_token_ = BrowserThread::GetBlockingPool()-> | 349 sequence_token_ = BrowserThread::GetBlockingPool()-> |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 } | 456 } |
| 426 | 457 |
| 427 return GetUserWallpaperInfo(UserManager::Get()->GetLoggedInUser()->email(), | 458 return GetUserWallpaperInfo(UserManager::Get()->GetLoggedInUser()->email(), |
| 428 info); | 459 info); |
| 429 } | 460 } |
| 430 | 461 |
| 431 void WallpaperManager::InitializeWallpaper() { | 462 void WallpaperManager::InitializeWallpaper() { |
| 432 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 463 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 433 UserManager* user_manager = UserManager::Get(); | 464 UserManager* user_manager = UserManager::Get(); |
| 434 | 465 |
| 466 // Apply device customization. |
| 467 if (ShouldUseCustomizedDefaultWallpaper()) { |
| 468 SetDefaultWallpaperPath( |
| 469 GetCustomizedWallpaperDefaultRescaledFileName(kSmallWallpaperSuffix), |
| 470 scoped_ptr<gfx::ImageSkia>().Pass(), |
| 471 GetCustomizedWallpaperDefaultRescaledFileName(kLargeWallpaperSuffix), |
| 472 scoped_ptr<gfx::ImageSkia>().Pass()); |
| 473 } |
| 474 |
| 435 CommandLine* command_line = GetComandLine(); | 475 CommandLine* command_line = GetComandLine(); |
| 436 if (command_line->HasSwitch(chromeos::switches::kGuestSession)) { | 476 if (command_line->HasSwitch(chromeos::switches::kGuestSession)) { |
| 437 // Guest wallpaper should be initialized when guest login. | 477 // Guest wallpaper should be initialized when guest login. |
| 438 // Note: This maybe called before login. So IsLoggedInAsGuest can not be | 478 // Note: This maybe called before login. So IsLoggedInAsGuest can not be |
| 439 // used here to determine if current user is guest. | 479 // used here to determine if current user is guest. |
| 440 return; | 480 return; |
| 441 } | 481 } |
| 442 | 482 |
| 443 if (command_line->HasSwitch(::switches::kTestType)) | 483 if (command_line->HasSwitch(::switches::kTestType)) |
| 444 WizardController::SetZeroDelays(); | 484 WizardController::SetZeroDelays(); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 prefs::kUsersWallpaperInfo); | 553 prefs::kUsersWallpaperInfo); |
| 514 prefs_wallpapers_info_update->RemoveWithoutPathExpansion(user_id, NULL); | 554 prefs_wallpapers_info_update->RemoveWithoutPathExpansion(user_id, NULL); |
| 515 DeleteUserWallpapers(user_id, info.file); | 555 DeleteUserWallpapers(user_id, info.file); |
| 516 } | 556 } |
| 517 | 557 |
| 518 bool WallpaperManager::ResizeWallpaper( | 558 bool WallpaperManager::ResizeWallpaper( |
| 519 const UserImage& wallpaper, | 559 const UserImage& wallpaper, |
| 520 ash::WallpaperLayout layout, | 560 ash::WallpaperLayout layout, |
| 521 int preferred_width, | 561 int preferred_width, |
| 522 int preferred_height, | 562 int preferred_height, |
| 523 scoped_refptr<base::RefCountedBytes>* output) const { | 563 scoped_refptr<base::RefCountedBytes>* output, |
| 564 gfx::ImageSkia* output_skia) const { |
| 524 DCHECK(BrowserThread::GetBlockingPool()-> | 565 DCHECK(BrowserThread::GetBlockingPool()-> |
| 525 IsRunningSequenceOnCurrentThread(sequence_token_)); | 566 IsRunningSequenceOnCurrentThread(sequence_token_)); |
| 526 int width = wallpaper.image().width(); | 567 int width = wallpaper.image().width(); |
| 527 int height = wallpaper.image().height(); | 568 int height = wallpaper.image().height(); |
| 528 int resized_width; | 569 int resized_width; |
| 529 int resized_height; | 570 int resized_height; |
| 530 *output = new base::RefCountedBytes(); | 571 *output = new base::RefCountedBytes(); |
| 531 | 572 |
| 532 if (layout == ash::WALLPAPER_LAYOUT_CENTER_CROPPED) { | 573 if (layout == ash::WALLPAPER_LAYOUT_CENTER_CROPPED) { |
| 533 // Do not resize custom wallpaper if it is smaller than preferred size. | 574 // Do not resize custom wallpaper if it is smaller than preferred size. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 560 | 601 |
| 561 SkBitmap image = *(resized_image.bitmap()); | 602 SkBitmap image = *(resized_image.bitmap()); |
| 562 SkAutoLockPixels lock_input(image); | 603 SkAutoLockPixels lock_input(image); |
| 563 gfx::JPEGCodec::Encode( | 604 gfx::JPEGCodec::Encode( |
| 564 reinterpret_cast<unsigned char*>(image.getAddr32(0, 0)), | 605 reinterpret_cast<unsigned char*>(image.getAddr32(0, 0)), |
| 565 gfx::JPEGCodec::FORMAT_SkBitmap, | 606 gfx::JPEGCodec::FORMAT_SkBitmap, |
| 566 image.width(), | 607 image.width(), |
| 567 image.height(), | 608 image.height(), |
| 568 image.width() * image.bytesPerPixel(), | 609 image.width() * image.bytesPerPixel(), |
| 569 kDefaultEncodingQuality, &(*output)->data()); | 610 kDefaultEncodingQuality, &(*output)->data()); |
| 611 |
| 612 if (output_skia) { |
| 613 resized_image.MakeThreadSafe(); |
| 614 *output_skia = resized_image; |
| 615 } |
| 616 |
| 570 return true; | 617 return true; |
| 571 } | 618 } |
| 572 | 619 |
| 573 void WallpaperManager::ResizeAndSaveWallpaper(const UserImage& wallpaper, | 620 bool WallpaperManager::ResizeAndSaveWallpaper(const UserImage& wallpaper, |
| 574 const base::FilePath& path, | 621 const base::FilePath& path, |
| 575 ash::WallpaperLayout layout, | 622 ash::WallpaperLayout layout, |
| 576 int preferred_width, | 623 int preferred_width, |
| 577 int preferred_height) const { | 624 int preferred_height, |
| 625 gfx::ImageSkia* result) const { |
| 578 if (layout == ash::WALLPAPER_LAYOUT_CENTER) { | 626 if (layout == ash::WALLPAPER_LAYOUT_CENTER) { |
| 579 // TODO(bshe): Generates cropped custom wallpaper for CENTER layout. | 627 // TODO(bshe): Generates cropped custom wallpaper for CENTER layout. |
| 580 if (base::PathExists(path)) | 628 if (base::PathExists(path)) |
| 581 base::DeleteFile(path, false); | 629 base::DeleteFile(path, false); |
| 582 return; | 630 return false; |
| 583 } | 631 } |
| 584 scoped_refptr<base::RefCountedBytes> data; | 632 scoped_refptr<base::RefCountedBytes> data; |
| 585 if (ResizeWallpaper(wallpaper, layout, preferred_width, preferred_height, | 633 if (ResizeWallpaper(wallpaper, |
| 586 &data)) { | 634 layout, |
| 587 SaveWallpaperInternal(path, | 635 preferred_width, |
| 588 reinterpret_cast<const char*>(data->front()), | 636 preferred_height, |
| 589 data->size()); | 637 &data, |
| 638 result)) { |
| 639 return SaveWallpaperInternal( |
| 640 path, reinterpret_cast<const char*>(data->front()), data->size()); |
| 590 } | 641 } |
| 642 return false; |
| 591 } | 643 } |
| 592 | 644 |
| 593 bool WallpaperManager::IsPolicyControlled(const std::string& user_id) const { | 645 bool WallpaperManager::IsPolicyControlled(const std::string& user_id) const { |
| 594 chromeos::WallpaperInfo info; | 646 chromeos::WallpaperInfo info; |
| 595 if (!GetUserWallpaperInfo(user_id, &info)) | 647 if (!GetUserWallpaperInfo(user_id, &info)) |
| 596 return false; | 648 return false; |
| 597 return info.type == chromeos::User::POLICY; | 649 return info.type == chromeos::User::POLICY; |
| 598 } | 650 } |
| 599 | 651 |
| 600 void WallpaperManager::OnPolicySet(const std::string& policy, | 652 void WallpaperManager::OnPolicySet(const std::string& policy, |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 722 } | 774 } |
| 723 | 775 |
| 724 void WallpaperManager::SetDefaultWallpaperNow(const std::string& user_id) { | 776 void WallpaperManager::SetDefaultWallpaperNow(const std::string& user_id) { |
| 725 GetPendingWallpaper(user_id, false)->ResetSetDefaultWallpaper(); | 777 GetPendingWallpaper(user_id, false)->ResetSetDefaultWallpaper(); |
| 726 } | 778 } |
| 727 | 779 |
| 728 void WallpaperManager::SetDefaultWallpaperDelayed(const std::string& user_id) { | 780 void WallpaperManager::SetDefaultWallpaperDelayed(const std::string& user_id) { |
| 729 GetPendingWallpaper(user_id, true)->ResetSetDefaultWallpaper(); | 781 GetPendingWallpaper(user_id, true)->ResetSetDefaultWallpaper(); |
| 730 } | 782 } |
| 731 | 783 |
| 784 void WallpaperManager::OnDefaultWallpaperDecoded( |
| 785 const base::FilePath& path, |
| 786 scoped_ptr<gfx::ImageSkia>* result, |
| 787 MovableOnDestroyCallbackHolder on_finish, |
| 788 const UserImage& wallpaper) { |
| 789 result->reset(new gfx::ImageSkia); |
| 790 (**result) = wallpaper.image(); |
| 791 ash::Shell::GetInstance() |
| 792 ->desktop_background_controller() |
| 793 ->SetCustomWallpaper(wallpaper.image(), ash::WALLPAPER_LAYOUT_STRETCH); |
| 794 } |
| 795 |
| 796 void WallpaperManager::StartLoadAndSetDefaultWallpaper( |
| 797 const base::FilePath& path, |
| 798 scoped_ptr<gfx::ImageSkia>* result, |
| 799 MovableOnDestroyCallbackHolder on_finish) { |
| 800 wallpaper_loader_->Start( |
| 801 path.value(), |
| 802 0, // Do not crop. |
| 803 base::Bind(&WallpaperManager::OnDefaultWallpaperDecoded, |
| 804 base::Unretained(this), |
| 805 path, |
| 806 base::Unretained(result), |
| 807 base::Passed(on_finish.Pass()))); |
| 808 } |
| 809 |
| 732 void WallpaperManager::DoSetDefaultWallpaper( | 810 void WallpaperManager::DoSetDefaultWallpaper( |
| 733 const std::string& user_id, | 811 const std::string& user_id, |
| 734 MovableOnDestroyCallbackHolder on_finish) { | 812 MovableOnDestroyCallbackHolder on_finish) { |
| 735 // There is no visible background in kiosk mode. | 813 // There is no visible background in kiosk mode. |
| 736 if (UserManager::Get()->IsLoggedInAsKioskApp()) | 814 if (UserManager::Get()->IsLoggedInAsKioskApp()) |
| 737 return; | 815 return; |
| 738 current_wallpaper_path_.clear(); | 816 current_wallpaper_path_.clear(); |
| 739 wallpaper_cache_.erase(user_id); | 817 wallpaper_cache_.erase(user_id); |
| 740 // Some browser tests do not have a shell instance. As no wallpaper is needed | 818 // Some browser tests do not have a shell instance. As no wallpaper is needed |
| 741 // in these tests anyway, avoid loading one, preventing crashes and speeding | 819 // in these tests anyway, avoid loading one, preventing crashes and speeding |
| 742 // up the tests. | 820 // up the tests. |
| 743 if (!ash::Shell::HasInstance()) | 821 if (!ash::Shell::HasInstance()) |
| 744 return; | 822 return; |
| 745 if (ash::Shell::GetInstance()->desktop_background_controller()-> | 823 |
| 746 SetDefaultWallpaper(UserManager::Get()->IsLoggedInAsGuest())) | 824 const bool is_guest = UserManager::Get()->IsLoggedInAsGuest(); |
| 747 loaded_wallpapers_++; | 825 |
| 826 gfx::ImageSkia image; |
| 827 |
| 828 ash::WallpaperResolution resolution = ash::Shell::GetInstance() |
| 829 ->desktop_background_controller() |
| 830 ->GetAppropriateResolution(); |
| 831 const bool use_small = (resolution == ash::WALLPAPER_RESOLUTION_SMALL); |
| 832 |
| 833 if (is_guest) { |
| 834 if (use_small) { |
| 835 if (guest_default_small_wallpaper_image_.get() == NULL) { |
| 836 StartLoadAndSetDefaultWallpaper(guest_default_small_wallpaper_file_, |
| 837 &guest_default_small_wallpaper_image_, |
| 838 on_finish.Pass()); |
| 839 return; |
| 840 } |
| 841 image = *guest_default_small_wallpaper_image_; |
| 842 } else { |
| 843 if (guest_default_large_wallpaper_image_.get() == NULL) { |
| 844 StartLoadAndSetDefaultWallpaper(guest_default_large_wallpaper_file_, |
| 845 &guest_default_large_wallpaper_image_, |
| 846 on_finish.Pass()); |
| 847 return; |
| 848 } |
| 849 image = *guest_default_large_wallpaper_image_; |
| 850 } |
| 851 } else { |
| 852 // not guest |
| 853 if (use_small) { |
| 854 if (default_small_wallpaper_image_.get() == NULL) { |
| 855 StartLoadAndSetDefaultWallpaper(default_small_wallpaper_file_, |
| 856 &default_small_wallpaper_image_, |
| 857 on_finish.Pass()); |
| 858 return; |
| 859 } |
| 860 image = *default_small_wallpaper_image_; |
| 861 } else { |
| 862 if (default_large_wallpaper_image_.get() == NULL) { |
| 863 StartLoadAndSetDefaultWallpaper(default_large_wallpaper_file_, |
| 864 &default_large_wallpaper_image_, |
| 865 on_finish.Pass()); |
| 866 return; |
| 867 } |
| 868 image = *default_large_wallpaper_image_; |
| 869 } |
| 870 } |
| 871 ash::Shell::GetInstance() |
| 872 ->desktop_background_controller() |
| 873 ->SetCustomWallpaper(image, ash::WALLPAPER_LAYOUT_STRETCH); |
| 748 } | 874 } |
| 749 | 875 |
| 750 void WallpaperManager::InitInitialUserWallpaper(const std::string& user_id, | 876 void WallpaperManager::InitInitialUserWallpaper(const std::string& user_id, |
| 751 bool is_persistent) { | 877 bool is_persistent) { |
| 752 current_user_wallpaper_info_.file = ""; | 878 current_user_wallpaper_info_.file = ""; |
| 753 current_user_wallpaper_info_.layout = ash::WALLPAPER_LAYOUT_CENTER_CROPPED; | 879 current_user_wallpaper_info_.layout = ash::WALLPAPER_LAYOUT_CENTER_CROPPED; |
| 754 current_user_wallpaper_info_.type = User::DEFAULT; | 880 current_user_wallpaper_info_.type = User::DEFAULT; |
| 755 current_user_wallpaper_info_.date = base::Time::Now().LocalMidnight(); | 881 current_user_wallpaper_info_.date = base::Time::Now().LocalMidnight(); |
| 756 | 882 |
| 757 WallpaperInfo info = current_user_wallpaper_info_; | 883 WallpaperInfo info = current_user_wallpaper_info_; |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1063 dir = GetCustomWallpaperDir(kOriginalWallpaperSubDir); | 1189 dir = GetCustomWallpaperDir(kOriginalWallpaperSubDir); |
| 1064 dir = dir.Append(user_id_hash); | 1190 dir = dir.Append(user_id_hash); |
| 1065 if (!base::PathExists(dir)) | 1191 if (!base::PathExists(dir)) |
| 1066 base::CreateDirectory(dir); | 1192 base::CreateDirectory(dir); |
| 1067 dir = GetCustomWallpaperDir(kThumbnailWallpaperSubDir); | 1193 dir = GetCustomWallpaperDir(kThumbnailWallpaperSubDir); |
| 1068 dir = dir.Append(user_id_hash); | 1194 dir = dir.Append(user_id_hash); |
| 1069 if (!base::PathExists(dir)) | 1195 if (!base::PathExists(dir)) |
| 1070 base::CreateDirectory(dir); | 1196 base::CreateDirectory(dir); |
| 1071 } | 1197 } |
| 1072 | 1198 |
| 1199 void WallpaperManager::set_command_line_for_testing( |
| 1200 base::CommandLine* command_line) { |
| 1201 command_line_for_testing_ = command_line; |
| 1202 SetDefaultWallpaperPathFromCommandLine(command_line); |
| 1203 } |
| 1204 |
| 1073 CommandLine* WallpaperManager::GetComandLine() { | 1205 CommandLine* WallpaperManager::GetComandLine() { |
| 1074 CommandLine* command_line = command_line_for_testing_ ? | 1206 CommandLine* command_line = command_line_for_testing_ ? |
| 1075 command_line_for_testing_ : CommandLine::ForCurrentProcess(); | 1207 command_line_for_testing_ : CommandLine::ForCurrentProcess(); |
| 1076 return command_line; | 1208 return command_line; |
| 1077 } | 1209 } |
| 1078 | 1210 |
| 1079 void WallpaperManager::InitializeRegisteredDeviceWallpaper() { | 1211 void WallpaperManager::InitializeRegisteredDeviceWallpaper() { |
| 1080 if (UserManager::Get()->IsUserLoggedIn()) | 1212 if (UserManager::Get()->IsUserLoggedIn()) |
| 1081 return; | 1213 return; |
| 1082 | 1214 |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1350 EnsureCustomWallpaperDirectories(user_id_hash); | 1482 EnsureCustomWallpaperDirectories(user_id_hash); |
| 1351 std::string file_name = original_path.BaseName().value(); | 1483 std::string file_name = original_path.BaseName().value(); |
| 1352 base::FilePath small_wallpaper_path = | 1484 base::FilePath small_wallpaper_path = |
| 1353 GetCustomWallpaperPath(kSmallWallpaperSubDir, user_id_hash, file_name); | 1485 GetCustomWallpaperPath(kSmallWallpaperSubDir, user_id_hash, file_name); |
| 1354 base::FilePath large_wallpaper_path = | 1486 base::FilePath large_wallpaper_path = |
| 1355 GetCustomWallpaperPath(kLargeWallpaperSubDir, user_id_hash, file_name); | 1487 GetCustomWallpaperPath(kLargeWallpaperSubDir, user_id_hash, file_name); |
| 1356 | 1488 |
| 1357 // Re-encode orginal file to jpeg format and saves the result in case that | 1489 // Re-encode orginal file to jpeg format and saves the result in case that |
| 1358 // resized wallpaper is not generated (i.e. chrome shutdown before resized | 1490 // resized wallpaper is not generated (i.e. chrome shutdown before resized |
| 1359 // wallpaper is saved). | 1491 // wallpaper is saved). |
| 1360 ResizeAndSaveWallpaper(wallpaper, original_path, | 1492 ResizeAndSaveWallpaper(wallpaper, |
| 1493 original_path, |
| 1361 ash::WALLPAPER_LAYOUT_STRETCH, | 1494 ash::WALLPAPER_LAYOUT_STRETCH, |
| 1362 wallpaper.image().width(), | 1495 wallpaper.image().width(), |
| 1363 wallpaper.image().height()); | 1496 wallpaper.image().height(), |
| 1497 NULL); |
| 1364 DeleteAllExcept(original_path); | 1498 DeleteAllExcept(original_path); |
| 1365 | 1499 |
| 1366 ResizeAndSaveWallpaper(wallpaper, small_wallpaper_path, layout, | 1500 ResizeAndSaveWallpaper(wallpaper, |
| 1501 small_wallpaper_path, |
| 1502 layout, |
| 1367 ash::kSmallWallpaperMaxWidth, | 1503 ash::kSmallWallpaperMaxWidth, |
| 1368 ash::kSmallWallpaperMaxHeight); | 1504 ash::kSmallWallpaperMaxHeight, |
| 1505 NULL); |
| 1369 DeleteAllExcept(small_wallpaper_path); | 1506 DeleteAllExcept(small_wallpaper_path); |
| 1370 ResizeAndSaveWallpaper(wallpaper, large_wallpaper_path, layout, | 1507 ResizeAndSaveWallpaper(wallpaper, |
| 1508 large_wallpaper_path, |
| 1509 layout, |
| 1371 ash::kLargeWallpaperMaxWidth, | 1510 ash::kLargeWallpaperMaxWidth, |
| 1372 ash::kLargeWallpaperMaxHeight); | 1511 ash::kLargeWallpaperMaxHeight, |
| 1512 NULL); |
| 1373 DeleteAllExcept(large_wallpaper_path); | 1513 DeleteAllExcept(large_wallpaper_path); |
| 1374 } | 1514 } |
| 1375 | 1515 |
| 1376 void WallpaperManager::RecordUma(User::WallpaperType type, int index) const { | 1516 void WallpaperManager::RecordUma(User::WallpaperType type, int index) const { |
| 1377 UMA_HISTOGRAM_ENUMERATION("Ash.Wallpaper.Type", type, | 1517 UMA_HISTOGRAM_ENUMERATION("Ash.Wallpaper.Type", type, |
| 1378 User::WALLPAPER_TYPE_COUNT); | 1518 User::WALLPAPER_TYPE_COUNT); |
| 1379 } | 1519 } |
| 1380 | 1520 |
| 1381 void WallpaperManager::SaveWallpaperInternal(const base::FilePath& path, | 1521 bool WallpaperManager::SaveWallpaperInternal(const base::FilePath& path, |
| 1382 const char* data, | 1522 const char* data, |
| 1383 int size) const { | 1523 int size) const { |
| 1384 int written_bytes = base::WriteFile(path, data, size); | 1524 int written_bytes = base::WriteFile(path, data, size); |
| 1385 DCHECK(written_bytes == size); | 1525 return written_bytes == size; |
| 1386 } | 1526 } |
| 1387 | 1527 |
| 1388 void WallpaperManager::StartLoad(const std::string& user_id, | 1528 void WallpaperManager::StartLoad(const std::string& user_id, |
| 1389 const WallpaperInfo& info, | 1529 const WallpaperInfo& info, |
| 1390 bool update_wallpaper, | 1530 bool update_wallpaper, |
| 1391 const base::FilePath& wallpaper_path, | 1531 const base::FilePath& wallpaper_path, |
| 1392 MovableOnDestroyCallbackHolder on_finish) { | 1532 MovableOnDestroyCallbackHolder on_finish) { |
| 1393 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1533 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1394 TRACE_EVENT_ASYNC_BEGIN0("ui", "LoadAndDecodeWallpaper", this); | 1534 TRACE_EVENT_ASYNC_BEGIN0("ui", "LoadAndDecodeWallpaper", this); |
| 1395 | 1535 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1448 if (!pending_inactive_) { | 1588 if (!pending_inactive_) { |
| 1449 loading_.push_back(new WallpaperManager::PendingWallpaper( | 1589 loading_.push_back(new WallpaperManager::PendingWallpaper( |
| 1450 (delayed ? GetWallpaperLoadDelay() | 1590 (delayed ? GetWallpaperLoadDelay() |
| 1451 : base::TimeDelta::FromMilliseconds(0)), | 1591 : base::TimeDelta::FromMilliseconds(0)), |
| 1452 user_id)); | 1592 user_id)); |
| 1453 pending_inactive_ = loading_.back(); | 1593 pending_inactive_ = loading_.back(); |
| 1454 } | 1594 } |
| 1455 return pending_inactive_; | 1595 return pending_inactive_; |
| 1456 } | 1596 } |
| 1457 | 1597 |
| 1598 // static |
| 1599 base::FilePath WallpaperManager::GetCustomizedWallpaperDefaultRescaledFileName( |
| 1600 const char* suffix) { |
| 1601 const base::FilePath default_downloaded_file_name = |
| 1602 ServicesCustomizationDocument::GetCustomizedWallpaperDownloadedFileName(); |
| 1603 const base::FilePath default_cache_dir = |
| 1604 ServicesCustomizationDocument::GetCustomizedWallpaperCacheDir(); |
| 1605 if (default_downloaded_file_name.empty() || default_cache_dir.empty()) |
| 1606 return base::FilePath(); |
| 1607 return default_cache_dir.Append( |
| 1608 default_downloaded_file_name.BaseName().value() + suffix); |
| 1609 } |
| 1610 |
| 1611 void WallpaperManager::SetCustomizedDefaultWallpaper( |
| 1612 const GURL& wallpaper_url, |
| 1613 const base::FilePath& downloaded_file, |
| 1614 const base::FilePath& resized_directory) { |
| 1615 // Should fail if this ever happens in tests. |
| 1616 DCHECK(wallpaper_url.is_valid() || wallpaper_url.is_empty()); |
| 1617 if (!wallpaper_url.is_valid()) { |
| 1618 if (!wallpaper_url.is_empty()) { |
| 1619 LOG(WARNING) << "Invalid Customized Wallpaper URL."; |
| 1620 } |
| 1621 return; |
| 1622 } |
| 1623 std::string downloaded_file_name = downloaded_file.BaseName().value(); |
| 1624 scoped_ptr<CustomizedWallpaperRescaledFiles> rescaled_files( |
| 1625 new CustomizedWallpaperRescaledFiles( |
| 1626 resized_directory.Append(downloaded_file_name + |
| 1627 kSmallWallpaperSuffix), |
| 1628 resized_directory.Append(downloaded_file_name + |
| 1629 kLargeWallpaperSuffix))); |
| 1630 scoped_ptr<CustomizedWallpaperFilesExist> exist( |
| 1631 new CustomizedWallpaperFilesExist); |
| 1632 |
| 1633 base::Closure check_file_exists = |
| 1634 base::Bind(&CheckCustomizedWallpaperFilesExist, |
| 1635 downloaded_file, |
| 1636 base::Unretained(rescaled_files.get()), |
| 1637 base::Unretained(exist.get())); |
| 1638 base::Closure on_checked_closure = |
| 1639 base::Bind(&WallpaperManager::SetCustomizedDefaultWallpaperAfterCheck, |
| 1640 weak_factory_.GetWeakPtr(), |
| 1641 wallpaper_url, |
| 1642 downloaded_file, |
| 1643 base::Passed(rescaled_files.Pass()), |
| 1644 base::Passed(exist.Pass())); |
| 1645 if (!BrowserThread::PostBlockingPoolTaskAndReply( |
| 1646 FROM_HERE, check_file_exists, on_checked_closure)) { |
| 1647 LOG(WARNING) << "Failed to start check CheckCustomizedWallpaperFilesExist."; |
| 1648 } |
| 1649 } |
| 1650 |
| 1651 void WallpaperManager::ResizeCustomizedDefaultWallpaper( |
| 1652 scoped_ptr<gfx::ImageSkia> image, |
| 1653 const UserImage::RawImage& raw_image, |
| 1654 const CustomizedWallpaperRescaledFiles* rescaled_files, |
| 1655 bool* success, |
| 1656 gfx::ImageSkia* small_wallpaper_image, |
| 1657 gfx::ImageSkia* large_wallpaper_image) { |
| 1658 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread( |
| 1659 sequence_token_)); |
| 1660 UserImage wallpaper(*image.get(), raw_image); |
| 1661 |
| 1662 *success = true; |
| 1663 |
| 1664 *success &= ResizeAndSaveWallpaper(wallpaper, |
| 1665 rescaled_files->path_rescaled_small, |
| 1666 ash::WALLPAPER_LAYOUT_STRETCH, |
| 1667 ash::kSmallWallpaperMaxWidth, |
| 1668 ash::kSmallWallpaperMaxHeight, |
| 1669 small_wallpaper_image); |
| 1670 |
| 1671 *success &= ResizeAndSaveWallpaper(wallpaper, |
| 1672 rescaled_files->path_rescaled_large, |
| 1673 ash::WALLPAPER_LAYOUT_STRETCH, |
| 1674 ash::kLargeWallpaperMaxWidth, |
| 1675 ash::kLargeWallpaperMaxHeight, |
| 1676 large_wallpaper_image); |
| 1677 } |
| 1678 |
| 1679 void WallpaperManager::OnCustomizedDefaultWallpaperResized( |
| 1680 const GURL& wallpaper_url, |
| 1681 scoped_ptr<CustomizedWallpaperRescaledFiles> rescaled_files, |
| 1682 scoped_ptr<bool> success, |
| 1683 scoped_ptr<gfx::ImageSkia> small_wallpaper_image, |
| 1684 scoped_ptr<gfx::ImageSkia> large_wallpaper_image) { |
| 1685 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1686 DCHECK(rescaled_files.get()); |
| 1687 DCHECK(success.get()); |
| 1688 if (!*success) { |
| 1689 LOG(WARNING) << "Failed to save Resized Customized Default Wallpaper"; |
| 1690 return; |
| 1691 } |
| 1692 PrefService* pref_service = g_browser_process->local_state(); |
| 1693 pref_service->SetString(prefs::kCustomizationDefaultWallpaperURL, |
| 1694 wallpaper_url.spec()); |
| 1695 SetDefaultWallpaperPath(rescaled_files->path_rescaled_small, |
| 1696 small_wallpaper_image.Pass(), |
| 1697 rescaled_files->path_rescaled_large, |
| 1698 large_wallpaper_image.Pass()); |
| 1699 VLOG(1) << "Customized Default Wallpaper applied."; |
| 1700 } |
| 1701 |
| 1702 void WallpaperManager::OnCustomizedDefaultWallpaperDecoded( |
| 1703 const GURL& wallpaper_url, |
| 1704 scoped_ptr<CustomizedWallpaperRescaledFiles> rescaled_files, |
| 1705 const UserImage& wallpaper) { |
| 1706 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1707 |
| 1708 // If decoded wallpaper is empty, we have probably failed to decode the file. |
| 1709 if (wallpaper.image().isNull()) { |
| 1710 LOG(WARNING) << "Failed to decode customized wallpaper."; |
| 1711 return; |
| 1712 } |
| 1713 |
| 1714 wallpaper.image().EnsureRepsForSupportedScales(); |
| 1715 scoped_ptr<gfx::ImageSkia> deep_copy(wallpaper.image().DeepCopy()); |
| 1716 |
| 1717 scoped_ptr<bool> success(new bool(false)); |
| 1718 scoped_ptr<gfx::ImageSkia> small_wallpaper_image(new gfx::ImageSkia); |
| 1719 scoped_ptr<gfx::ImageSkia> large_wallpaper_image(new gfx::ImageSkia); |
| 1720 |
| 1721 // TODO(bshe): This may break if RawImage becomes RefCountedMemory. |
| 1722 base::Closure resize_closure = |
| 1723 base::Bind(&WallpaperManager::ResizeCustomizedDefaultWallpaper, |
| 1724 base::Unretained(this), |
| 1725 base::Passed(&deep_copy), |
| 1726 wallpaper.raw_image(), |
| 1727 base::Unretained(rescaled_files.get()), |
| 1728 base::Unretained(success.get()), |
| 1729 base::Unretained(small_wallpaper_image.get()), |
| 1730 base::Unretained(large_wallpaper_image.get())); |
| 1731 base::Closure on_resized_closure = |
| 1732 base::Bind(&WallpaperManager::OnCustomizedDefaultWallpaperResized, |
| 1733 weak_factory_.GetWeakPtr(), |
| 1734 wallpaper_url, |
| 1735 base::Passed(rescaled_files.Pass()), |
| 1736 base::Passed(success.Pass()), |
| 1737 base::Passed(small_wallpaper_image.Pass()), |
| 1738 base::Passed(large_wallpaper_image.Pass())); |
| 1739 |
| 1740 if (!task_runner_->PostTaskAndReply( |
| 1741 FROM_HERE, resize_closure, on_resized_closure)) { |
| 1742 LOG(WARNING) << "Failed to start Customized Wallpaper resize."; |
| 1743 } |
| 1744 } |
| 1745 |
| 1746 void WallpaperManager::SetCustomizedDefaultWallpaperAfterCheck( |
| 1747 const GURL& wallpaper_url, |
| 1748 const base::FilePath& downloaded_file, |
| 1749 scoped_ptr<CustomizedWallpaperRescaledFiles> rescaled_files, |
| 1750 scoped_ptr<CustomizedWallpaperFilesExist> exist) { |
| 1751 PrefService* pref_service = g_browser_process->local_state(); |
| 1752 |
| 1753 std::string current_url = |
| 1754 pref_service->GetString(prefs::kCustomizationDefaultWallpaperURL); |
| 1755 if (current_url != wallpaper_url.spec() || !exist->AllRescaledExist()) { |
| 1756 DCHECK(exist->downloaded); |
| 1757 // Need rescale |
| 1758 wallpaper_loader_->Start( |
| 1759 downloaded_file.value(), |
| 1760 0, // Do not crop. |
| 1761 base::Bind(&WallpaperManager::OnCustomizedDefaultWallpaperDecoded, |
| 1762 weak_factory_.GetWeakPtr(), |
| 1763 wallpaper_url, |
| 1764 base::Passed(rescaled_files.Pass()))); |
| 1765 } else { |
| 1766 SetDefaultWallpaperPath(rescaled_files->path_rescaled_small, |
| 1767 scoped_ptr<gfx::ImageSkia>().Pass(), |
| 1768 rescaled_files->path_rescaled_large, |
| 1769 scoped_ptr<gfx::ImageSkia>().Pass()); |
| 1770 } |
| 1771 } |
| 1772 |
| 1773 // static |
| 1774 bool WallpaperManager::ShouldUseCustomizedDefaultWallpaper() { |
| 1775 PrefService* pref_service = g_browser_process->local_state(); |
| 1776 |
| 1777 return !(pref_service->FindPreference( |
| 1778 prefs::kCustomizationDefaultWallpaperURL) |
| 1779 ->IsDefaultValue()); |
| 1780 } |
| 1781 |
| 1782 void WallpaperManager::SetDefaultWallpaperPathFromCommandLine( |
| 1783 base::CommandLine* command_line) { |
| 1784 default_small_wallpaper_file_ = command_line->GetSwitchValuePath( |
| 1785 ash::switches::kAshDefaultWallpaperSmall); |
| 1786 default_large_wallpaper_file_ = command_line->GetSwitchValuePath( |
| 1787 ash::switches::kAshDefaultWallpaperLarge); |
| 1788 guest_default_small_wallpaper_file_ = |
| 1789 command_line->GetSwitchValuePath(ash::switches::kAshGuestWallpaperSmall); |
| 1790 guest_default_large_wallpaper_file_ = |
| 1791 command_line->GetSwitchValuePath(ash::switches::kAshGuestWallpaperLarge); |
| 1792 } |
| 1793 |
| 1794 void WallpaperManager::SetDefaultWallpaperPath( |
| 1795 const base::FilePath& default_small_wallpaper_file, |
| 1796 scoped_ptr<gfx::ImageSkia> small_wallpaper_image, |
| 1797 const base::FilePath& default_large_wallpaper_file, |
| 1798 scoped_ptr<gfx::ImageSkia> large_wallpaper_image) { |
| 1799 default_small_wallpaper_file_ = default_small_wallpaper_file; |
| 1800 default_large_wallpaper_file_ = default_large_wallpaper_file; |
| 1801 default_small_wallpaper_image_.swap(small_wallpaper_image); |
| 1802 default_large_wallpaper_image_.swap(large_wallpaper_image); |
| 1803 |
| 1804 ash::DesktopBackgroundController* dbc = |
| 1805 ash::Shell::GetInstance()->desktop_background_controller(); |
| 1806 if ((small_wallpaper_image.get() && |
| 1807 dbc->CustomWallpaperIsAlreadyLoaded(*small_wallpaper_image)) || |
| 1808 (large_wallpaper_image.get() && |
| 1809 dbc->CustomWallpaperIsAlreadyLoaded(*large_wallpaper_image))) { |
| 1810 DoSetDefaultWallpaper(std::string(), |
| 1811 MovableOnDestroyCallbackHolder().Pass()); |
| 1812 } |
| 1813 } |
| 1814 |
| 1458 } // namespace chromeos | 1815 } // namespace chromeos |
| OLD | NEW |