Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(472)

Side by Side Diff: chrome/browser/chromeos/login/wallpaper_manager.cc

Issue 208273005: If customization includes default wallpaper, download and apply it. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update after-review. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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::GetBlockingPool()->RunsTasksOnCurrentThread());
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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 215
202 WallpaperManager* manager = WallpaperManager::Get(); 216 WallpaperManager* manager = WallpaperManager::Get();
203 if (manager->pending_inactive_ == this) 217 if (manager->pending_inactive_ == this)
204 manager->pending_inactive_ = NULL; 218 manager->pending_inactive_ = NULL;
205 219
206 started_load_at_ = base::Time::Now(); 220 started_load_at_ = base::Time::Now();
207 221
208 if (default_) { 222 if (default_) {
209 manager->DoSetDefaultWallpaper(user_id_, on_finish_.Pass()); 223 manager->DoSetDefaultWallpaper(user_id_, on_finish_.Pass());
210 } else if (!user_wallpaper_.isNull()) { 224 } else if (!user_wallpaper_.isNull()) {
211 ash::Shell::GetInstance()-> 225 ash::Shell::GetInstance()->desktop_background_controller()->SetWallpaper(
212 desktop_background_controller()-> 226 user_wallpaper_, info_.layout);
213 SetCustomWallpaper(user_wallpaper_, info_.layout);
214 } else if (!wallpaper_path_.empty()) { 227 } else if (!wallpaper_path_.empty()) {
215 manager->task_runner_->PostTask( 228 manager->task_runner_->PostTask(
216 FROM_HERE, 229 FROM_HERE,
217 base::Bind(&WallpaperManager::GetCustomWallpaperInternal, 230 base::Bind(&WallpaperManager::GetCustomWallpaperInternal,
218 base::Unretained(manager), 231 base::Unretained(manager),
219 user_id_, 232 user_id_,
220 info_, 233 info_,
221 wallpaper_path_, 234 wallpaper_path_,
222 true /* update wallpaper */, 235 true /* update wallpaper */,
223 base::Passed(on_finish_.Pass()))); 236 base::Passed(on_finish_.Pass())));
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 i != manager->loading_.end(); 272 i != manager->loading_.end();
260 ++i) 273 ++i)
261 if (i->get() == this) { 274 if (i->get() == this) {
262 manager->loading_.erase(i); 275 manager->loading_.erase(i);
263 break; 276 break;
264 } 277 }
265 } 278 }
266 279
267 // WallpaperManager, public: --------------------------------------------------- 280 // WallpaperManager, public: ---------------------------------------------------
268 281
282 WallpaperManager::CustomizedWallpaperRescaledFiles::
283 CustomizedWallpaperRescaledFiles(const base::FilePath& path_rescaled_small,
284 const base::FilePath& path_rescaled_large)
285 : path_rescaled_small(path_rescaled_small),
286 path_rescaled_large(path_rescaled_large) {
287 }
288
289 WallpaperManager::CustomizedWallpaperFilesExist::CustomizedWallpaperFilesExist()
290 : downloaded(false), rescaled_small(false), rescaled_large(false) {
291 }
292
293 bool WallpaperManager::CustomizedWallpaperFilesExist::AllRescaledExist() const {
294 return rescaled_small && rescaled_large;
295 }
296
269 // TestApi. For testing purpose 297 // TestApi. For testing purpose
270 WallpaperManager::TestApi::TestApi(WallpaperManager* wallpaper_manager) 298 WallpaperManager::TestApi::TestApi(WallpaperManager* wallpaper_manager)
271 : wallpaper_manager_(wallpaper_manager) { 299 : wallpaper_manager_(wallpaper_manager) {
272 } 300 }
273 301
274 WallpaperManager::TestApi::~TestApi() { 302 WallpaperManager::TestApi::~TestApi() {
275 } 303 }
276 304
277 base::FilePath WallpaperManager::TestApi::current_wallpaper_path() { 305 base::FilePath WallpaperManager::TestApi::current_wallpaper_path() {
278 return wallpaper_manager_->current_wallpaper_path_; 306 return wallpaper_manager_->current_wallpaper_path_;
(...skipping 20 matching lines...) Expand all
299 g_wallpaper_manager = new WallpaperManager(); 327 g_wallpaper_manager = new WallpaperManager();
300 return g_wallpaper_manager; 328 return g_wallpaper_manager;
301 } 329 }
302 330
303 WallpaperManager::WallpaperManager() 331 WallpaperManager::WallpaperManager()
304 : loaded_wallpapers_(0), 332 : loaded_wallpapers_(0),
305 command_line_for_testing_(NULL), 333 command_line_for_testing_(NULL),
306 should_cache_wallpaper_(false), 334 should_cache_wallpaper_(false),
307 weak_factory_(this), 335 weak_factory_(this),
308 pending_inactive_(NULL) { 336 pending_inactive_(NULL) {
337 SetDefaultWallpaperPathFromCommandLine(
338 base::CommandLine::ForCurrentProcess());
309 registrar_.Add(this, 339 registrar_.Add(this,
310 chrome::NOTIFICATION_LOGIN_USER_CHANGED, 340 chrome::NOTIFICATION_LOGIN_USER_CHANGED,
311 content::NotificationService::AllSources()); 341 content::NotificationService::AllSources());
312 registrar_.Add(this, 342 registrar_.Add(this,
313 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, 343 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
314 content::NotificationService::AllSources()); 344 content::NotificationService::AllSources());
315 registrar_.Add(this, 345 registrar_.Add(this,
316 chrome::NOTIFICATION_WALLPAPER_ANIMATION_FINISHED, 346 chrome::NOTIFICATION_WALLPAPER_ANIMATION_FINISHED,
317 content::NotificationService::AllSources()); 347 content::NotificationService::AllSources());
318 sequence_token_ = BrowserThread::GetBlockingPool()-> 348 sequence_token_ = BrowserThread::GetBlockingPool()->
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 } 455 }
426 456
427 return GetUserWallpaperInfo(UserManager::Get()->GetLoggedInUser()->email(), 457 return GetUserWallpaperInfo(UserManager::Get()->GetLoggedInUser()->email(),
428 info); 458 info);
429 } 459 }
430 460
431 void WallpaperManager::InitializeWallpaper() { 461 void WallpaperManager::InitializeWallpaper() {
432 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 462 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
433 UserManager* user_manager = UserManager::Get(); 463 UserManager* user_manager = UserManager::Get();
434 464
465 // Apply device customization.
466 if (ShouldUseCustomizedDefaultWallpaper()) {
467 SetDefaultWallpaperPath(
468 GetCustomizedWallpaperDefaultRescaledFileName(kSmallWallpaperSuffix),
469 scoped_ptr<gfx::ImageSkia>().Pass(),
470 GetCustomizedWallpaperDefaultRescaledFileName(kLargeWallpaperSuffix),
471 scoped_ptr<gfx::ImageSkia>().Pass());
472 }
473
435 CommandLine* command_line = GetComandLine(); 474 CommandLine* command_line = GetComandLine();
436 if (command_line->HasSwitch(chromeos::switches::kGuestSession)) { 475 if (command_line->HasSwitch(chromeos::switches::kGuestSession)) {
437 // Guest wallpaper should be initialized when guest login. 476 // Guest wallpaper should be initialized when guest login.
438 // Note: This maybe called before login. So IsLoggedInAsGuest can not be 477 // Note: This maybe called before login. So IsLoggedInAsGuest can not be
439 // used here to determine if current user is guest. 478 // used here to determine if current user is guest.
440 return; 479 return;
441 } 480 }
442 481
443 if (command_line->HasSwitch(::switches::kTestType)) 482 if (command_line->HasSwitch(::switches::kTestType))
444 WizardController::SetZeroDelays(); 483 WizardController::SetZeroDelays();
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 prefs::kUsersWallpaperInfo); 552 prefs::kUsersWallpaperInfo);
514 prefs_wallpapers_info_update->RemoveWithoutPathExpansion(user_id, NULL); 553 prefs_wallpapers_info_update->RemoveWithoutPathExpansion(user_id, NULL);
515 DeleteUserWallpapers(user_id, info.file); 554 DeleteUserWallpapers(user_id, info.file);
516 } 555 }
517 556
518 bool WallpaperManager::ResizeWallpaper( 557 bool WallpaperManager::ResizeWallpaper(
519 const UserImage& wallpaper, 558 const UserImage& wallpaper,
520 ash::WallpaperLayout layout, 559 ash::WallpaperLayout layout,
521 int preferred_width, 560 int preferred_width,
522 int preferred_height, 561 int preferred_height,
523 scoped_refptr<base::RefCountedBytes>* output) const { 562 scoped_refptr<base::RefCountedBytes>* output,
563 gfx::ImageSkia* output_skia) const {
524 DCHECK(BrowserThread::GetBlockingPool()-> 564 DCHECK(BrowserThread::GetBlockingPool()->
525 IsRunningSequenceOnCurrentThread(sequence_token_)); 565 IsRunningSequenceOnCurrentThread(sequence_token_));
526 int width = wallpaper.image().width(); 566 int width = wallpaper.image().width();
527 int height = wallpaper.image().height(); 567 int height = wallpaper.image().height();
528 int resized_width; 568 int resized_width;
529 int resized_height; 569 int resized_height;
530 *output = new base::RefCountedBytes(); 570 *output = new base::RefCountedBytes();
531 571
532 if (layout == ash::WALLPAPER_LAYOUT_CENTER_CROPPED) { 572 if (layout == ash::WALLPAPER_LAYOUT_CENTER_CROPPED) {
533 // Do not resize custom wallpaper if it is smaller than preferred size. 573 // Do not resize custom wallpaper if it is smaller than preferred size.
(...skipping 26 matching lines...) Expand all
560 600
561 SkBitmap image = *(resized_image.bitmap()); 601 SkBitmap image = *(resized_image.bitmap());
562 SkAutoLockPixels lock_input(image); 602 SkAutoLockPixels lock_input(image);
563 gfx::JPEGCodec::Encode( 603 gfx::JPEGCodec::Encode(
564 reinterpret_cast<unsigned char*>(image.getAddr32(0, 0)), 604 reinterpret_cast<unsigned char*>(image.getAddr32(0, 0)),
565 gfx::JPEGCodec::FORMAT_SkBitmap, 605 gfx::JPEGCodec::FORMAT_SkBitmap,
566 image.width(), 606 image.width(),
567 image.height(), 607 image.height(),
568 image.width() * image.bytesPerPixel(), 608 image.width() * image.bytesPerPixel(),
569 kDefaultEncodingQuality, &(*output)->data()); 609 kDefaultEncodingQuality, &(*output)->data());
610
611 if (output_skia) {
612 resized_image.MakeThreadSafe();
613 *output_skia = resized_image;
614 }
615
570 return true; 616 return true;
571 } 617 }
572 618
573 void WallpaperManager::ResizeAndSaveWallpaper(const UserImage& wallpaper, 619 bool WallpaperManager::ResizeAndSaveWallpaper(const UserImage& wallpaper,
574 const base::FilePath& path, 620 const base::FilePath& path,
575 ash::WallpaperLayout layout, 621 ash::WallpaperLayout layout,
576 int preferred_width, 622 int preferred_width,
577 int preferred_height) const { 623 int preferred_height,
624 gfx::ImageSkia* result) const {
578 if (layout == ash::WALLPAPER_LAYOUT_CENTER) { 625 if (layout == ash::WALLPAPER_LAYOUT_CENTER) {
579 // TODO(bshe): Generates cropped custom wallpaper for CENTER layout. 626 // TODO(bshe): Generates cropped custom wallpaper for CENTER layout.
580 if (base::PathExists(path)) 627 if (base::PathExists(path))
581 base::DeleteFile(path, false); 628 base::DeleteFile(path, false);
582 return; 629 return false;
583 } 630 }
584 scoped_refptr<base::RefCountedBytes> data; 631 scoped_refptr<base::RefCountedBytes> data;
585 if (ResizeWallpaper(wallpaper, layout, preferred_width, preferred_height, 632 if (ResizeWallpaper(wallpaper,
586 &data)) { 633 layout,
587 SaveWallpaperInternal(path, 634 preferred_width,
588 reinterpret_cast<const char*>(data->front()), 635 preferred_height,
589 data->size()); 636 &data,
637 result)) {
638 return SaveWallpaperInternal(
639 path, reinterpret_cast<const char*>(data->front()), data->size());
590 } 640 }
641 return false;
591 } 642 }
592 643
593 bool WallpaperManager::IsPolicyControlled(const std::string& user_id) const { 644 bool WallpaperManager::IsPolicyControlled(const std::string& user_id) const {
594 chromeos::WallpaperInfo info; 645 chromeos::WallpaperInfo info;
595 if (!GetUserWallpaperInfo(user_id, &info)) 646 if (!GetUserWallpaperInfo(user_id, &info))
596 return false; 647 return false;
597 return info.type == chromeos::User::POLICY; 648 return info.type == chromeos::User::POLICY;
598 } 649 }
599 650
600 void WallpaperManager::OnPolicySet(const std::string& policy, 651 void WallpaperManager::OnPolicySet(const std::string& policy,
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
722 } 773 }
723 774
724 void WallpaperManager::SetDefaultWallpaperNow(const std::string& user_id) { 775 void WallpaperManager::SetDefaultWallpaperNow(const std::string& user_id) {
725 GetPendingWallpaper(user_id, false)->ResetSetDefaultWallpaper(); 776 GetPendingWallpaper(user_id, false)->ResetSetDefaultWallpaper();
726 } 777 }
727 778
728 void WallpaperManager::SetDefaultWallpaperDelayed(const std::string& user_id) { 779 void WallpaperManager::SetDefaultWallpaperDelayed(const std::string& user_id) {
729 GetPendingWallpaper(user_id, true)->ResetSetDefaultWallpaper(); 780 GetPendingWallpaper(user_id, true)->ResetSetDefaultWallpaper();
730 } 781 }
731 782
783 void WallpaperManager::OnDefaultWallpaperDecoded(
784 const base::FilePath& path,
785 scoped_ptr<gfx::ImageSkia>* result,
786 MovableOnDestroyCallbackHolder on_finish,
787 const UserImage& wallpaper) {
788 result->reset(new gfx::ImageSkia);
789 (**result) = wallpaper.image();
790 ash::Shell::GetInstance()->desktop_background_controller()->SetWallpaper(
791 wallpaper.image(), ash::WALLPAPER_LAYOUT_STRETCH);
792 }
793
794 void WallpaperManager::StartLoadAndSetDefaultWallpaper(
795 const base::FilePath& path,
796 scoped_ptr<gfx::ImageSkia>* result,
797 MovableOnDestroyCallbackHolder on_finish) {
798 wallpaper_loader_->Start(
799 path.value(),
800 0, // Do not crop.
801 base::Bind(&WallpaperManager::OnDefaultWallpaperDecoded,
802 base::Unretained(this),
803 path,
804 base::Unretained(result),
805 base::Passed(on_finish.Pass())));
806 }
807
732 void WallpaperManager::DoSetDefaultWallpaper( 808 void WallpaperManager::DoSetDefaultWallpaper(
733 const std::string& user_id, 809 const std::string& user_id,
734 MovableOnDestroyCallbackHolder on_finish) { 810 MovableOnDestroyCallbackHolder on_finish) {
735 // There is no visible background in kiosk mode. 811 // There is no visible background in kiosk mode.
736 if (UserManager::Get()->IsLoggedInAsKioskApp()) 812 if (UserManager::Get()->IsLoggedInAsKioskApp())
737 return; 813 return;
738 current_wallpaper_path_.clear(); 814 current_wallpaper_path_.clear();
739 wallpaper_cache_.erase(user_id); 815 wallpaper_cache_.erase(user_id);
740 // Some browser tests do not have a shell instance. As no wallpaper is needed 816 // 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 817 // in these tests anyway, avoid loading one, preventing crashes and speeding
742 // up the tests. 818 // up the tests.
743 if (!ash::Shell::HasInstance()) 819 if (!ash::Shell::HasInstance())
744 return; 820 return;
745 if (ash::Shell::GetInstance()->desktop_background_controller()-> 821
746 SetDefaultWallpaper(UserManager::Get()->IsLoggedInAsGuest())) 822 const bool is_guest = UserManager::Get()->IsLoggedInAsGuest();
747 loaded_wallpapers_++; 823
824 gfx::ImageSkia image;
825
826 ash::WallpaperResolution resolution = ash::Shell::GetInstance()
827 ->desktop_background_controller()
828 ->GetAppropriateResolution();
829 const bool use_small = (resolution == ash::WALLPAPER_RESOLUTION_SMALL);
830
831 if (is_guest) {
832 if (use_small) {
833 if (guest_default_small_wallpaper_image_.get() == NULL) {
834 StartLoadAndSetDefaultWallpaper(guest_default_small_wallpaper_file_,
835 &guest_default_small_wallpaper_image_,
836 on_finish.Pass());
837 return;
838 }
839 image = *guest_default_small_wallpaper_image_;
840 } else {
841 if (guest_default_large_wallpaper_image_.get() == NULL) {
842 StartLoadAndSetDefaultWallpaper(guest_default_large_wallpaper_file_,
843 &guest_default_large_wallpaper_image_,
844 on_finish.Pass());
845 return;
846 }
847 image = *guest_default_large_wallpaper_image_;
848 }
849 } else {
850 // not guest
851 if (use_small) {
852 if (default_small_wallpaper_image_.get() == NULL) {
853 StartLoadAndSetDefaultWallpaper(default_small_wallpaper_file_,
854 &default_small_wallpaper_image_,
855 on_finish.Pass());
856 return;
857 }
858 image = *default_small_wallpaper_image_;
859 } else {
860 if (default_large_wallpaper_image_.get() == NULL) {
861 StartLoadAndSetDefaultWallpaper(default_large_wallpaper_file_,
862 &default_large_wallpaper_image_,
863 on_finish.Pass());
864 return;
865 }
866 image = *default_large_wallpaper_image_;
867 }
868 }
869 ash::Shell::GetInstance()->desktop_background_controller()->SetWallpaper(
870 image, ash::WALLPAPER_LAYOUT_STRETCH);
748 } 871 }
749 872
750 void WallpaperManager::InitInitialUserWallpaper(const std::string& user_id, 873 void WallpaperManager::InitInitialUserWallpaper(const std::string& user_id,
751 bool is_persistent) { 874 bool is_persistent) {
752 current_user_wallpaper_info_.file = ""; 875 current_user_wallpaper_info_.file = "";
753 current_user_wallpaper_info_.layout = ash::WALLPAPER_LAYOUT_CENTER_CROPPED; 876 current_user_wallpaper_info_.layout = ash::WALLPAPER_LAYOUT_CENTER_CROPPED;
754 current_user_wallpaper_info_.type = User::DEFAULT; 877 current_user_wallpaper_info_.type = User::DEFAULT;
755 current_user_wallpaper_info_.date = base::Time::Now().LocalMidnight(); 878 current_user_wallpaper_info_.date = base::Time::Now().LocalMidnight();
756 879
757 WallpaperInfo info = current_user_wallpaper_info_; 880 WallpaperInfo info = current_user_wallpaper_info_;
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
1063 dir = GetCustomWallpaperDir(kOriginalWallpaperSubDir); 1186 dir = GetCustomWallpaperDir(kOriginalWallpaperSubDir);
1064 dir = dir.Append(user_id_hash); 1187 dir = dir.Append(user_id_hash);
1065 if (!base::PathExists(dir)) 1188 if (!base::PathExists(dir))
1066 base::CreateDirectory(dir); 1189 base::CreateDirectory(dir);
1067 dir = GetCustomWallpaperDir(kThumbnailWallpaperSubDir); 1190 dir = GetCustomWallpaperDir(kThumbnailWallpaperSubDir);
1068 dir = dir.Append(user_id_hash); 1191 dir = dir.Append(user_id_hash);
1069 if (!base::PathExists(dir)) 1192 if (!base::PathExists(dir))
1070 base::CreateDirectory(dir); 1193 base::CreateDirectory(dir);
1071 } 1194 }
1072 1195
1196 void WallpaperManager::set_command_line_for_testing(
1197 base::CommandLine* command_line) {
1198 command_line_for_testing_ = command_line;
1199 SetDefaultWallpaperPathFromCommandLine(command_line);
1200 }
1201
1073 CommandLine* WallpaperManager::GetComandLine() { 1202 CommandLine* WallpaperManager::GetComandLine() {
1074 CommandLine* command_line = command_line_for_testing_ ? 1203 CommandLine* command_line = command_line_for_testing_ ?
1075 command_line_for_testing_ : CommandLine::ForCurrentProcess(); 1204 command_line_for_testing_ : CommandLine::ForCurrentProcess();
1076 return command_line; 1205 return command_line;
1077 } 1206 }
1078 1207
1079 void WallpaperManager::InitializeRegisteredDeviceWallpaper() { 1208 void WallpaperManager::InitializeRegisteredDeviceWallpaper() {
1080 if (UserManager::Get()->IsUserLoggedIn()) 1209 if (UserManager::Get()->IsUserLoggedIn())
1081 return; 1210 return;
1082 1211
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
1314 return; 1443 return;
1315 } 1444 }
1316 1445
1317 // Only cache the user wallpaper at login screen and for multi profile users. 1446 // Only cache the user wallpaper at login screen and for multi profile users.
1318 if (!UserManager::Get()->IsUserLoggedIn() || 1447 if (!UserManager::Get()->IsUserLoggedIn() ||
1319 UserManager::IsMultipleProfilesAllowed()) { 1448 UserManager::IsMultipleProfilesAllowed()) {
1320 wallpaper_cache_[user_id] = wallpaper.image(); 1449 wallpaper_cache_[user_id] = wallpaper.image();
1321 } 1450 }
1322 1451
1323 if (update_wallpaper) { 1452 if (update_wallpaper) {
1324 ash::Shell::GetInstance()->desktop_background_controller()-> 1453 ash::Shell::GetInstance()->desktop_background_controller()->SetWallpaper(
1325 SetCustomWallpaper(wallpaper.image(), layout); 1454 wallpaper.image(), layout);
1326 } 1455 }
1327 } 1456 }
1328 1457
1329 void WallpaperManager::ProcessCustomWallpaper( 1458 void WallpaperManager::ProcessCustomWallpaper(
1330 const std::string& user_id_hash, 1459 const std::string& user_id_hash,
1331 bool persistent, 1460 bool persistent,
1332 const WallpaperInfo& info, 1461 const WallpaperInfo& info,
1333 scoped_ptr<gfx::ImageSkia> image, 1462 scoped_ptr<gfx::ImageSkia> image,
1334 const UserImage::RawImage& raw_image) { 1463 const UserImage::RawImage& raw_image) {
1335 DCHECK(BrowserThread::GetBlockingPool()-> 1464 DCHECK(BrowserThread::GetBlockingPool()->
(...skipping 14 matching lines...) Expand all
1350 EnsureCustomWallpaperDirectories(user_id_hash); 1479 EnsureCustomWallpaperDirectories(user_id_hash);
1351 std::string file_name = original_path.BaseName().value(); 1480 std::string file_name = original_path.BaseName().value();
1352 base::FilePath small_wallpaper_path = 1481 base::FilePath small_wallpaper_path =
1353 GetCustomWallpaperPath(kSmallWallpaperSubDir, user_id_hash, file_name); 1482 GetCustomWallpaperPath(kSmallWallpaperSubDir, user_id_hash, file_name);
1354 base::FilePath large_wallpaper_path = 1483 base::FilePath large_wallpaper_path =
1355 GetCustomWallpaperPath(kLargeWallpaperSubDir, user_id_hash, file_name); 1484 GetCustomWallpaperPath(kLargeWallpaperSubDir, user_id_hash, file_name);
1356 1485
1357 // Re-encode orginal file to jpeg format and saves the result in case that 1486 // 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 1487 // resized wallpaper is not generated (i.e. chrome shutdown before resized
1359 // wallpaper is saved). 1488 // wallpaper is saved).
1360 ResizeAndSaveWallpaper(wallpaper, original_path, 1489 ResizeAndSaveWallpaper(wallpaper,
1490 original_path,
1361 ash::WALLPAPER_LAYOUT_STRETCH, 1491 ash::WALLPAPER_LAYOUT_STRETCH,
1362 wallpaper.image().width(), 1492 wallpaper.image().width(),
1363 wallpaper.image().height()); 1493 wallpaper.image().height(),
1494 NULL);
1364 DeleteAllExcept(original_path); 1495 DeleteAllExcept(original_path);
1365 1496
1366 ResizeAndSaveWallpaper(wallpaper, small_wallpaper_path, layout, 1497 ResizeAndSaveWallpaper(wallpaper,
1498 small_wallpaper_path,
1499 layout,
1367 ash::kSmallWallpaperMaxWidth, 1500 ash::kSmallWallpaperMaxWidth,
1368 ash::kSmallWallpaperMaxHeight); 1501 ash::kSmallWallpaperMaxHeight,
1502 NULL);
1369 DeleteAllExcept(small_wallpaper_path); 1503 DeleteAllExcept(small_wallpaper_path);
1370 ResizeAndSaveWallpaper(wallpaper, large_wallpaper_path, layout, 1504 ResizeAndSaveWallpaper(wallpaper,
1505 large_wallpaper_path,
1506 layout,
1371 ash::kLargeWallpaperMaxWidth, 1507 ash::kLargeWallpaperMaxWidth,
1372 ash::kLargeWallpaperMaxHeight); 1508 ash::kLargeWallpaperMaxHeight,
1509 NULL);
1373 DeleteAllExcept(large_wallpaper_path); 1510 DeleteAllExcept(large_wallpaper_path);
1374 } 1511 }
1375 1512
1376 void WallpaperManager::RecordUma(User::WallpaperType type, int index) const { 1513 void WallpaperManager::RecordUma(User::WallpaperType type, int index) const {
1377 UMA_HISTOGRAM_ENUMERATION("Ash.Wallpaper.Type", type, 1514 UMA_HISTOGRAM_ENUMERATION("Ash.Wallpaper.Type", type,
1378 User::WALLPAPER_TYPE_COUNT); 1515 User::WALLPAPER_TYPE_COUNT);
1379 } 1516 }
1380 1517
1381 void WallpaperManager::SaveWallpaperInternal(const base::FilePath& path, 1518 bool WallpaperManager::SaveWallpaperInternal(const base::FilePath& path,
1382 const char* data, 1519 const char* data,
1383 int size) const { 1520 int size) const {
1384 int written_bytes = base::WriteFile(path, data, size); 1521 int written_bytes = base::WriteFile(path, data, size);
1385 DCHECK(written_bytes == size); 1522 return written_bytes == size;
1386 } 1523 }
1387 1524
1388 void WallpaperManager::StartLoad(const std::string& user_id, 1525 void WallpaperManager::StartLoad(const std::string& user_id,
1389 const WallpaperInfo& info, 1526 const WallpaperInfo& info,
1390 bool update_wallpaper, 1527 bool update_wallpaper,
1391 const base::FilePath& wallpaper_path, 1528 const base::FilePath& wallpaper_path,
1392 MovableOnDestroyCallbackHolder on_finish) { 1529 MovableOnDestroyCallbackHolder on_finish) {
1393 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1530 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1394 TRACE_EVENT_ASYNC_BEGIN0("ui", "LoadAndDecodeWallpaper", this); 1531 TRACE_EVENT_ASYNC_BEGIN0("ui", "LoadAndDecodeWallpaper", this);
1395 1532
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1448 if (!pending_inactive_) { 1585 if (!pending_inactive_) {
1449 loading_.push_back(new WallpaperManager::PendingWallpaper( 1586 loading_.push_back(new WallpaperManager::PendingWallpaper(
1450 (delayed ? GetWallpaperLoadDelay() 1587 (delayed ? GetWallpaperLoadDelay()
1451 : base::TimeDelta::FromMilliseconds(0)), 1588 : base::TimeDelta::FromMilliseconds(0)),
1452 user_id)); 1589 user_id));
1453 pending_inactive_ = loading_.back(); 1590 pending_inactive_ = loading_.back();
1454 } 1591 }
1455 return pending_inactive_; 1592 return pending_inactive_;
1456 } 1593 }
1457 1594
1595 // static
1596 base::FilePath WallpaperManager::GetCustomizedWallpaperDefaultRescaledFileName(
1597 const char* suffix) {
1598 const base::FilePath default_downloaded_file_name =
1599 ServicesCustomizationDocument::GetCustomizedWallpaperDownloadedFileName();
1600 const base::FilePath default_cache_dir =
1601 ServicesCustomizationDocument::GetCustomizedWallpaperCacheDir();
1602 if (default_downloaded_file_name.empty() || default_cache_dir.empty())
1603 return base::FilePath();
1604 return default_cache_dir.Append(
1605 default_downloaded_file_name.BaseName().value() + suffix);
1606 }
1607
1608 void WallpaperManager::SetCustomizedDefaultWallpaper(
1609 const GURL& wallpaper_url,
1610 const base::FilePath& downloaded_file,
1611 const base::FilePath& resized_directory) {
1612 // Should fail if this ever happens in tests.
1613 DCHECK(wallpaper_url.is_valid() || wallpaper_url.is_empty());
1614 if (!wallpaper_url.is_valid()) {
1615 if (!wallpaper_url.is_empty()) {
1616 LOG(WARNING) << "Invalid Customized Wallpaper URL.";
1617 }
1618 return;
1619 }
1620 std::string downloaded_file_name = downloaded_file.BaseName().value();
1621 scoped_ptr<CustomizedWallpaperRescaledFiles> rescaled_files(
1622 new CustomizedWallpaperRescaledFiles(
1623 resized_directory.Append(downloaded_file_name +
1624 kSmallWallpaperSuffix),
1625 resized_directory.Append(downloaded_file_name +
1626 kLargeWallpaperSuffix)));
1627 scoped_ptr<CustomizedWallpaperFilesExist> exist(
1628 new CustomizedWallpaperFilesExist);
1629
1630 base::Closure check_file_exists =
1631 base::Bind(&CheckCustomizedWallpaperFilesExist,
1632 downloaded_file,
1633 base::Unretained(rescaled_files.get()),
1634 base::Unretained(exist.get()));
1635 base::Closure on_checked_closure =
1636 base::Bind(&WallpaperManager::SetCustomizedDefaultWallpaperAfterCheck,
1637 weak_factory_.GetWeakPtr(),
1638 wallpaper_url,
1639 downloaded_file,
1640 base::Passed(rescaled_files.Pass()),
1641 base::Passed(exist.Pass()));
1642 if (!BrowserThread::PostBlockingPoolTaskAndReply(
1643 FROM_HERE, check_file_exists, on_checked_closure)) {
1644 LOG(WARNING) << "Failed to start check CheckCustomizedWallpaperFilesExist.";
1645 }
1646 }
1647
1648 void WallpaperManager::ResizeCustomizedDefaultWallpaper(
1649 scoped_ptr<gfx::ImageSkia> image,
1650 const UserImage::RawImage& raw_image,
1651 const CustomizedWallpaperRescaledFiles* rescaled_files,
1652 bool* success,
1653 gfx::ImageSkia* small_wallpaper_image,
1654 gfx::ImageSkia* large_wallpaper_image) {
1655 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
1656 sequence_token_));
1657 UserImage wallpaper(*image.get(), raw_image);
1658
1659 *success = true;
1660
1661 *success &= ResizeAndSaveWallpaper(wallpaper,
1662 rescaled_files->path_rescaled_small,
1663 ash::WALLPAPER_LAYOUT_STRETCH,
1664 ash::kSmallWallpaperMaxWidth,
1665 ash::kSmallWallpaperMaxHeight,
1666 small_wallpaper_image);
1667
1668 *success &= ResizeAndSaveWallpaper(wallpaper,
1669 rescaled_files->path_rescaled_large,
1670 ash::WALLPAPER_LAYOUT_STRETCH,
1671 ash::kLargeWallpaperMaxWidth,
1672 ash::kLargeWallpaperMaxHeight,
1673 large_wallpaper_image);
1674 }
1675
1676 void WallpaperManager::OnCustomizedDefaultWallpaperResized(
1677 const GURL& wallpaper_url,
1678 scoped_ptr<CustomizedWallpaperRescaledFiles> rescaled_files,
1679 scoped_ptr<bool> success,
1680 scoped_ptr<gfx::ImageSkia> small_wallpaper_image,
1681 scoped_ptr<gfx::ImageSkia> large_wallpaper_image) {
1682 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1683 DCHECK(rescaled_files.get());
1684 DCHECK(success.get());
1685 if (!*success) {
1686 LOG(WARNING) << "Failed to save Resized Customized Default Wallpaper";
1687 return;
1688 }
1689 PrefService* pref_service = g_browser_process->local_state();
1690 pref_service->SetString(prefs::kCustomizationDefaultWallpaperURL,
1691 wallpaper_url.spec());
1692 SetDefaultWallpaperPath(rescaled_files->path_rescaled_small,
1693 small_wallpaper_image.Pass(),
1694 rescaled_files->path_rescaled_large,
1695 large_wallpaper_image.Pass());
1696 VLOG(1) << "Customized Default Wallpaper applied.";
1697 }
1698
1699 void WallpaperManager::OnCustomizedDefaultWallpaperDecoded(
1700 const GURL& wallpaper_url,
1701 scoped_ptr<CustomizedWallpaperRescaledFiles> rescaled_files,
1702 const UserImage& wallpaper) {
1703 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1704
1705 // If decoded wallpaper is empty, we have probably failed to decode the file.
1706 if (wallpaper.image().isNull()) {
1707 LOG(WARNING) << "Failed to decode customized wallpaper.";
1708 return;
1709 }
1710
1711 wallpaper.image().EnsureRepsForSupportedScales();
1712 scoped_ptr<gfx::ImageSkia> deep_copy(wallpaper.image().DeepCopy());
1713
1714 scoped_ptr<bool> success(new bool(false));
1715 scoped_ptr<gfx::ImageSkia> small_wallpaper_image(new gfx::ImageSkia);
1716 scoped_ptr<gfx::ImageSkia> large_wallpaper_image(new gfx::ImageSkia);
1717
1718 // TODO(bshe): This may break if RawImage becomes RefCountedMemory.
1719 base::Closure resize_closure =
1720 base::Bind(&WallpaperManager::ResizeCustomizedDefaultWallpaper,
1721 base::Unretained(this),
1722 base::Passed(&deep_copy),
1723 wallpaper.raw_image(),
1724 base::Unretained(rescaled_files.get()),
1725 base::Unretained(success.get()),
1726 base::Unretained(small_wallpaper_image.get()),
1727 base::Unretained(large_wallpaper_image.get()));
1728 base::Closure on_resized_closure =
1729 base::Bind(&WallpaperManager::OnCustomizedDefaultWallpaperResized,
1730 weak_factory_.GetWeakPtr(),
1731 wallpaper_url,
1732 base::Passed(rescaled_files.Pass()),
1733 base::Passed(success.Pass()),
1734 base::Passed(small_wallpaper_image.Pass()),
1735 base::Passed(large_wallpaper_image.Pass()));
1736
1737 if (!task_runner_->PostTaskAndReply(
1738 FROM_HERE, resize_closure, on_resized_closure)) {
1739 LOG(WARNING) << "Failed to start Customized Wallpaper resize.";
1740 }
1741 }
1742
1743 void WallpaperManager::SetCustomizedDefaultWallpaperAfterCheck(
1744 const GURL& wallpaper_url,
1745 const base::FilePath& downloaded_file,
1746 scoped_ptr<CustomizedWallpaperRescaledFiles> rescaled_files,
1747 scoped_ptr<CustomizedWallpaperFilesExist> exist) {
1748 PrefService* pref_service = g_browser_process->local_state();
1749
1750 std::string current_url =
1751 pref_service->GetString(prefs::kCustomizationDefaultWallpaperURL);
1752 if (current_url != wallpaper_url.spec() || !exist->AllRescaledExist()) {
1753 DCHECK(exist->downloaded);
1754 // Need rescale
1755 wallpaper_loader_->Start(
1756 downloaded_file.value(),
1757 0, // Do not crop.
1758 base::Bind(&WallpaperManager::OnCustomizedDefaultWallpaperDecoded,
1759 weak_factory_.GetWeakPtr(),
1760 wallpaper_url,
1761 base::Passed(rescaled_files.Pass())));
1762 } else {
1763 SetDefaultWallpaperPath(rescaled_files->path_rescaled_small,
1764 scoped_ptr<gfx::ImageSkia>().Pass(),
1765 rescaled_files->path_rescaled_large,
1766 scoped_ptr<gfx::ImageSkia>().Pass());
1767 }
1768 }
1769
1770 // static
1771 bool WallpaperManager::ShouldUseCustomizedDefaultWallpaper() {
1772 PrefService* pref_service = g_browser_process->local_state();
1773
1774 return !(pref_service->FindPreference(
1775 prefs::kCustomizationDefaultWallpaperURL)
1776 ->IsDefaultValue());
1777 }
1778
1779 void WallpaperManager::SetDefaultWallpaperPathFromCommandLine(
1780 base::CommandLine* command_line) {
1781 default_small_wallpaper_file_ = command_line->GetSwitchValuePath(
1782 ash::switches::kAshDefaultWallpaperSmall);
1783 default_large_wallpaper_file_ = command_line->GetSwitchValuePath(
1784 ash::switches::kAshDefaultWallpaperLarge);
1785 guest_default_small_wallpaper_file_ =
1786 command_line->GetSwitchValuePath(ash::switches::kAshGuestWallpaperSmall);
1787 guest_default_large_wallpaper_file_ =
1788 command_line->GetSwitchValuePath(ash::switches::kAshGuestWallpaperLarge);
1789 }
1790
1791 void WallpaperManager::SetDefaultWallpaperPath(
1792 const base::FilePath& default_small_wallpaper_file,
1793 scoped_ptr<gfx::ImageSkia> small_wallpaper_image,
1794 const base::FilePath& default_large_wallpaper_file,
1795 scoped_ptr<gfx::ImageSkia> large_wallpaper_image) {
1796 default_small_wallpaper_file_ = default_small_wallpaper_file;
1797 default_large_wallpaper_file_ = default_large_wallpaper_file;
1798 default_small_wallpaper_image_.swap(small_wallpaper_image);
1799 default_large_wallpaper_image_.swap(large_wallpaper_image);
1800
1801 ash::DesktopBackgroundController* dbc =
1802 ash::Shell::GetInstance()->desktop_background_controller();
1803 if ((small_wallpaper_image.get() &&
1804 dbc->WallpaperIsAlreadyLoaded(*small_wallpaper_image)) ||
1805 (large_wallpaper_image.get() &&
1806 dbc->WallpaperIsAlreadyLoaded(*large_wallpaper_image))) {
1807 DoSetDefaultWallpaper(std::string(),
1808 MovableOnDestroyCallbackHolder().Pass());
1809 }
1810 }
1811
1458 } // namespace chromeos 1812 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/login/wallpaper_manager.h ('k') | chrome/browser/chromeos/login/wallpaper_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698