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

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

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

Powered by Google App Engine
This is Rietveld 408576698