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

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

Powered by Google App Engine
This is Rietveld 408576698