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

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

Issue 341713004: Use weak pointer to where possible (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 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
« no previous file with comments | « chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/users/wallpaper/wallpaper_manager.h" 5 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
6 6
7 #include <numeric> 7 #include <numeric>
8 #include <vector> 8 #include <vector>
9 9
10 #include "ash/ash_constants.h" 10 #include "ash/ash_constants.h"
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 // Some users may still have legacy wallpapers with png extension. We need 166 // Some users may still have legacy wallpapers with png extension. We need
167 // to delete these wallpapers too. 167 // to delete these wallpapers too.
168 if (!base::DeleteFile(path, true) && 168 if (!base::DeleteFile(path, true) &&
169 !base::DeleteFile(path.AddExtension(".png"), false)) { 169 !base::DeleteFile(path.AddExtension(".png"), false)) {
170 LOG(ERROR) << "Failed to remove user wallpaper at " << path.value(); 170 LOG(ERROR) << "Failed to remove user wallpaper at " << path.value();
171 } 171 }
172 } 172 }
173 } 173 }
174 174
175 // Creates all new custom wallpaper directories for |user_id_hash| if not exist. 175 // Creates all new custom wallpaper directories for |user_id_hash| if not exist.
176 // static
176 void EnsureCustomWallpaperDirectories(const std::string& user_id_hash) { 177 void EnsureCustomWallpaperDirectories(const std::string& user_id_hash) {
177 base::FilePath dir; 178 base::FilePath dir;
178 dir = GetCustomWallpaperDir(kSmallWallpaperSubDir); 179 dir = GetCustomWallpaperDir(kSmallWallpaperSubDir);
179 dir = dir.Append(user_id_hash); 180 dir = dir.Append(user_id_hash);
180 if (!base::PathExists(dir)) 181 if (!base::PathExists(dir))
181 base::CreateDirectory(dir); 182 base::CreateDirectory(dir);
182 dir = GetCustomWallpaperDir(kLargeWallpaperSubDir); 183 dir = GetCustomWallpaperDir(kLargeWallpaperSubDir);
183 dir = dir.Append(user_id_hash); 184 dir = dir.Append(user_id_hash);
184 if (!base::PathExists(dir)) 185 if (!base::PathExists(dir))
185 base::CreateDirectory(dir); 186 base::CreateDirectory(dir);
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 if (default_) { 391 if (default_) {
391 manager->DoSetDefaultWallpaper(user_id_, on_finish_.Pass()); 392 manager->DoSetDefaultWallpaper(user_id_, on_finish_.Pass());
392 } else if (!user_wallpaper_.isNull()) { 393 } else if (!user_wallpaper_.isNull()) {
393 ash::Shell::GetInstance() 394 ash::Shell::GetInstance()
394 ->desktop_background_controller() 395 ->desktop_background_controller()
395 ->SetWallpaperImage(user_wallpaper_, info_.layout); 396 ->SetWallpaperImage(user_wallpaper_, info_.layout);
396 } else if (!wallpaper_path_.empty()) { 397 } else if (!wallpaper_path_.empty()) {
397 manager->task_runner_->PostTask( 398 manager->task_runner_->PostTask(
398 FROM_HERE, 399 FROM_HERE,
399 base::Bind(&WallpaperManager::GetCustomWallpaperInternal, 400 base::Bind(&WallpaperManager::GetCustomWallpaperInternal,
400 base::Unretained(manager),
401 user_id_, 401 user_id_,
402 info_, 402 info_,
403 wallpaper_path_, 403 wallpaper_path_,
404 true /* update wallpaper */, 404 true /* update wallpaper */,
405 base::Passed(on_finish_.Pass()))); 405 base::Passed(on_finish_.Pass()),
406 manager->weak_factory_.GetWeakPtr()));
406 } else if (!info_.file.empty()) { 407 } else if (!info_.file.empty()) {
407 manager->LoadWallpaper(user_id_, info_, true, on_finish_.Pass()); 408 manager->LoadWallpaper(user_id_, info_, true, on_finish_.Pass());
408 } else { 409 } else {
409 // PendingWallpaper was created and never initialized? 410 // PendingWallpaper was created and never initialized?
410 NOTREACHED(); 411 NOTREACHED();
411 // Error. Do not record time. 412 // Error. Do not record time.
412 started_load_at_ = base::Time(); 413 started_load_at_ = base::Time();
413 } 414 }
414 on_finish_.reset(); 415 on_finish_.reset();
415 } 416 }
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 registry->RegisterDictionaryPref(prefs::kUsersWallpaperInfo); 520 registry->RegisterDictionaryPref(prefs::kUsersWallpaperInfo);
520 registry->RegisterDictionaryPref(kUserWallpapers); 521 registry->RegisterDictionaryPref(kUserWallpapers);
521 registry->RegisterDictionaryPref(kUserWallpapersProperties); 522 registry->RegisterDictionaryPref(kUserWallpapersProperties);
522 } 523 }
523 524
524 void WallpaperManager::AddObservers() { 525 void WallpaperManager::AddObservers() {
525 show_user_name_on_signin_subscription_ = 526 show_user_name_on_signin_subscription_ =
526 CrosSettings::Get()->AddSettingsObserver( 527 CrosSettings::Get()->AddSettingsObserver(
527 kAccountsPrefShowUserNamesOnSignIn, 528 kAccountsPrefShowUserNamesOnSignIn,
528 base::Bind(&WallpaperManager::InitializeRegisteredDeviceWallpaper, 529 base::Bind(&WallpaperManager::InitializeRegisteredDeviceWallpaper,
529 base::Unretained(this))); 530 weak_factory_.GetWeakPtr()));
530 } 531 }
531 532
532 void WallpaperManager::EnsureLoggedInUserWallpaperLoaded() { 533 void WallpaperManager::EnsureLoggedInUserWallpaperLoaded() {
533 // Some browser tests do not have a shell instance. As no wallpaper is needed 534 // Some browser tests do not have a shell instance. As no wallpaper is needed
534 // in these tests anyway, avoid loading one, preventing crashes and speeding 535 // in these tests anyway, avoid loading one, preventing crashes and speeding
535 // up the tests. 536 // up the tests.
536 if (!ash::Shell::HasInstance()) 537 if (!ash::Shell::HasInstance())
537 return; 538 return;
538 539
539 WallpaperInfo info; 540 WallpaperInfo info;
(...skipping 22 matching lines...) Expand all
562 for (CustomWallpaperMap::iterator it = wallpaper_cache_.begin(); 563 for (CustomWallpaperMap::iterator it = wallpaper_cache_.begin();
563 it != wallpaper_cache_.end(); ++it) { 564 it != wallpaper_cache_.end(); ++it) {
564 if (logged_in_users_names.find(it->first) != 565 if (logged_in_users_names.find(it->first) !=
565 logged_in_users_names.end()) { 566 logged_in_users_names.end()) {
566 logged_in_users_cache.insert(*it); 567 logged_in_users_cache.insert(*it);
567 } 568 }
568 } 569 }
569 wallpaper_cache_ = logged_in_users_cache; 570 wallpaper_cache_ = logged_in_users_cache;
570 } 571 }
571 572
572 base::FilePath WallpaperManager::GetCustomWallpaperPath(
573 const char* sub_dir,
574 const std::string& user_id_hash,
575 const std::string& file) const {
576 base::FilePath custom_wallpaper_path = GetCustomWallpaperDir(sub_dir);
577 return custom_wallpaper_path.Append(user_id_hash).Append(file);
578 }
579
580 bool WallpaperManager::GetLoggedInUserWallpaperInfo(WallpaperInfo* info) { 573 bool WallpaperManager::GetLoggedInUserWallpaperInfo(WallpaperInfo* info) {
581 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 574 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
582 575
583 if (UserManager::Get()->IsLoggedInAsStub()) { 576 if (UserManager::Get()->IsLoggedInAsStub()) {
584 info->file = current_user_wallpaper_info_.file = ""; 577 info->file = current_user_wallpaper_info_.file = "";
585 info->layout = current_user_wallpaper_info_.layout = 578 info->layout = current_user_wallpaper_info_.layout =
586 ash::WALLPAPER_LAYOUT_CENTER_CROPPED; 579 ash::WALLPAPER_LAYOUT_CENTER_CROPPED;
587 info->type = current_user_wallpaper_info_.type = User::DEFAULT; 580 info->type = current_user_wallpaper_info_.type = User::DEFAULT;
588 info->date = current_user_wallpaper_info_.date = 581 info->date = current_user_wallpaper_info_.date =
589 base::Time::Now().LocalMidnight(); 582 base::Time::Now().LocalMidnight();
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
821 WallpaperManager::GetAppropriateResolution() { 814 WallpaperManager::GetAppropriateResolution() {
822 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 815 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
823 gfx::Size size = 816 gfx::Size size =
824 ash::DesktopBackgroundController::GetMaxDisplaySizeInNative(); 817 ash::DesktopBackgroundController::GetMaxDisplaySizeInNative();
825 return (size.width() > kSmallWallpaperMaxWidth || 818 return (size.width() > kSmallWallpaperMaxWidth ||
826 size.height() > kSmallWallpaperMaxHeight) 819 size.height() > kSmallWallpaperMaxHeight)
827 ? WALLPAPER_RESOLUTION_LARGE 820 ? WALLPAPER_RESOLUTION_LARGE
828 : WALLPAPER_RESOLUTION_SMALL; 821 : WALLPAPER_RESOLUTION_SMALL;
829 } 822 }
830 823
824 // static
825 base::FilePath WallpaperManager::GetCustomWallpaperPath(
826 const char* sub_dir,
827 const std::string& user_id_hash,
828 const std::string& file) {
829 base::FilePath custom_wallpaper_path = GetCustomWallpaperDir(sub_dir);
830 return custom_wallpaper_path.Append(user_id_hash).Append(file);
831 }
832
831 void WallpaperManager::SetPolicyControlledWallpaper( 833 void WallpaperManager::SetPolicyControlledWallpaper(
832 const std::string& user_id, 834 const std::string& user_id,
833 const UserImage& user_image) { 835 const UserImage& user_image) {
834 const User *user = chromeos::UserManager::Get()->FindUser(user_id); 836 const User *user = chromeos::UserManager::Get()->FindUser(user_id);
835 if (!user) { 837 if (!user) {
836 NOTREACHED() << "Unknown user."; 838 NOTREACHED() << "Unknown user.";
837 return; 839 return;
838 } 840 }
839 SetCustomWallpaper(user_id, 841 SetCustomWallpaper(user_id,
840 user->username_hash(), 842 user->username_hash(),
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
889 // Block shutdown on this task. Otherwise, we may lose the custom wallpaper 891 // Block shutdown on this task. Otherwise, we may lose the custom wallpaper
890 // that the user selected. 892 // that the user selected.
891 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner = 893 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner =
892 BrowserThread::GetBlockingPool() 894 BrowserThread::GetBlockingPool()
893 ->GetSequencedTaskRunnerWithShutdownBehavior( 895 ->GetSequencedTaskRunnerWithShutdownBehavior(
894 sequence_token_, base::SequencedWorkerPool::BLOCK_SHUTDOWN); 896 sequence_token_, base::SequencedWorkerPool::BLOCK_SHUTDOWN);
895 // TODO(bshe): This may break if RawImage becomes RefCountedMemory. 897 // TODO(bshe): This may break if RawImage becomes RefCountedMemory.
896 blocking_task_runner->PostTask( 898 blocking_task_runner->PostTask(
897 FROM_HERE, 899 FROM_HERE,
898 base::Bind(&WallpaperManager::SaveCustomWallpaper, 900 base::Bind(&WallpaperManager::SaveCustomWallpaper,
899 base::Unretained(this),
900 user_id_hash, 901 user_id_hash,
901 base::FilePath(wallpaper_info.file), 902 base::FilePath(wallpaper_info.file),
902 wallpaper_info.layout, 903 wallpaper_info.layout,
903 base::Passed(deep_copy.Pass()))); 904 base::Passed(deep_copy.Pass())));
904 } 905 }
905 906
906 std::string relative_path = base::FilePath(user_id_hash).Append(file).value(); 907 std::string relative_path = base::FilePath(user_id_hash).Append(file).value();
907 // User's custom wallpaper path is determined by relative path and the 908 // User's custom wallpaper path is determined by relative path and the
908 // appropriate wallpaper resolution in GetCustomWallpaperInternal. 909 // appropriate wallpaper resolution in GetCustomWallpaperInternal.
909 WallpaperInfo info = { 910 WallpaperInfo info = {
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
972 } 973 }
973 // 1x1 wallpaper is actually solid color, so it should be stretched. 974 // 1x1 wallpaper is actually solid color, so it should be stretched.
974 if (default_wallpaper_image_->image().width() == 1 && 975 if (default_wallpaper_image_->image().width() == 1 &&
975 default_wallpaper_image_->image().height() == 1) 976 default_wallpaper_image_->image().height() == 1)
976 layout = ash::WALLPAPER_LAYOUT_STRETCH; 977 layout = ash::WALLPAPER_LAYOUT_STRETCH;
977 978
978 ash::Shell::GetInstance()->desktop_background_controller()->SetWallpaperImage( 979 ash::Shell::GetInstance()->desktop_background_controller()->SetWallpaperImage(
979 default_wallpaper_image_->image(), layout); 980 default_wallpaper_image_->image(), layout);
980 } 981 }
981 982
983 // static
984 void WallpaperManager::RecordUma(User::WallpaperType type, int index) {
985 UMA_HISTOGRAM_ENUMERATION(
986 "Ash.Wallpaper.Type", type, User::WALLPAPER_TYPE_COUNT);
987 }
988
989 // static
990 void WallpaperManager::SaveCustomWallpaper(const std::string& user_id_hash,
991 const base::FilePath& original_path,
992 ash::WallpaperLayout layout,
993 scoped_ptr<gfx::ImageSkia> image) {
994 EnsureCustomWallpaperDirectories(user_id_hash);
995 std::string file_name = original_path.BaseName().value();
996 base::FilePath small_wallpaper_path =
997 GetCustomWallpaperPath(kSmallWallpaperSubDir, user_id_hash, file_name);
998 base::FilePath large_wallpaper_path =
999 GetCustomWallpaperPath(kLargeWallpaperSubDir, user_id_hash, file_name);
1000
1001 // Re-encode orginal file to jpeg format and saves the result in case that
1002 // resized wallpaper is not generated (i.e. chrome shutdown before resized
1003 // wallpaper is saved).
1004 ResizeAndSaveWallpaper(*image,
1005 original_path,
1006 ash::WALLPAPER_LAYOUT_STRETCH,
1007 image->width(),
1008 image->height(),
1009 NULL);
1010 DeleteAllExcept(original_path);
1011
1012 ResizeAndSaveWallpaper(*image,
1013 small_wallpaper_path,
1014 layout,
1015 kSmallWallpaperMaxWidth,
1016 kSmallWallpaperMaxHeight,
1017 NULL);
1018 DeleteAllExcept(small_wallpaper_path);
1019 ResizeAndSaveWallpaper(*image,
1020 large_wallpaper_path,
1021 layout,
1022 kLargeWallpaperMaxWidth,
1023 kLargeWallpaperMaxHeight,
1024 NULL);
1025 DeleteAllExcept(large_wallpaper_path);
1026 }
1027
1028 // static
1029 void WallpaperManager::MoveCustomWallpapersOnWorker(
1030 const std::string& user_id,
1031 const std::string& user_id_hash,
1032 base::WeakPtr<WallpaperManager> weak_ptr) {
1033
1034 if (MoveCustomWallpaperDirectory(
1035 kOriginalWallpaperSubDir, user_id, user_id_hash)) {
1036 // Consider success if the original wallpaper is moved to the new directory.
1037 // Original wallpaper is the fallback if the correct resolution wallpaper
1038 // can not be found.
1039 BrowserThread::PostTask(
1040 BrowserThread::UI,
1041 FROM_HERE,
1042 base::Bind(&WallpaperManager::MoveCustomWallpapersSuccess,
1043 weak_ptr,
Alexander Alekseev 2014/06/18 19:11:18 This seems to be dangerous, as WeakPtr is not thre
oshima 2014/06/18 19:25:25 It's safe to pass WeakPtr between threads. The der
Alexander Alekseev 2014/06/18 19:57:58 Yes, you're right.
1044 user_id,
1045 user_id_hash));
1046 }
1047 MoveCustomWallpaperDirectory(kLargeWallpaperSubDir, user_id, user_id_hash);
1048 MoveCustomWallpaperDirectory(kSmallWallpaperSubDir, user_id, user_id_hash);
1049 MoveCustomWallpaperDirectory(
1050 kThumbnailWallpaperSubDir, user_id, user_id_hash);
1051 }
1052
1053 // static
1054 void WallpaperManager::GetCustomWallpaperInternal(
1055 const std::string& user_id,
1056 const WallpaperInfo& info,
1057 const base::FilePath& wallpaper_path,
1058 bool update_wallpaper,
1059 MovableOnDestroyCallbackHolder on_finish,
1060 base::WeakPtr<WallpaperManager> weak_ptr) {
1061
1062 base::FilePath valid_path = wallpaper_path;
1063 if (!base::PathExists(wallpaper_path)) {
1064 // Falls back on original file if the correct resolution file does not
1065 // exist. This may happen when the original custom wallpaper is small or
1066 // browser shutdown before resized wallpaper saved.
1067 valid_path = GetCustomWallpaperDir(kOriginalWallpaperSubDir);
1068 valid_path = valid_path.Append(info.file);
1069 }
1070
1071 if (!base::PathExists(valid_path)) {
1072 // Falls back to custom wallpaper that uses email as part of its file path.
1073 // Note that email is used instead of user_id_hash here.
1074 valid_path =
1075 GetCustomWallpaperPath(kOriginalWallpaperSubDir, user_id, info.file);
1076 }
1077
1078 if (!base::PathExists(valid_path)) {
1079 LOG(ERROR) << "Failed to load previously selected custom wallpaper. " <<
1080 "Fallback to default wallpaper";
1081 BrowserThread::PostTask(BrowserThread::UI,
1082 FROM_HERE,
1083 base::Bind(&WallpaperManager::DoSetDefaultWallpaper,
1084 weak_ptr,
1085 user_id,
1086 base::Passed(on_finish.Pass())));
1087 } else {
1088 BrowserThread::PostTask(BrowserThread::UI,
1089 FROM_HERE,
1090 base::Bind(&WallpaperManager::StartLoad,
1091 weak_ptr,
1092 user_id,
1093 info,
1094 update_wallpaper,
1095 valid_path,
1096 base::Passed(on_finish.Pass())));
1097 }
1098 }
1099
982 void WallpaperManager::InitInitialUserWallpaper(const std::string& user_id, 1100 void WallpaperManager::InitInitialUserWallpaper(const std::string& user_id,
983 bool is_persistent) { 1101 bool is_persistent) {
984 current_user_wallpaper_info_.file = ""; 1102 current_user_wallpaper_info_.file = "";
985 current_user_wallpaper_info_.layout = ash::WALLPAPER_LAYOUT_CENTER_CROPPED; 1103 current_user_wallpaper_info_.layout = ash::WALLPAPER_LAYOUT_CENTER_CROPPED;
986 current_user_wallpaper_info_.type = User::DEFAULT; 1104 current_user_wallpaper_info_.type = User::DEFAULT;
987 current_user_wallpaper_info_.date = base::Time::Now().LocalMidnight(); 1105 current_user_wallpaper_info_.date = base::Time::Now().LocalMidnight();
988 1106
989 WallpaperInfo info = current_user_wallpaper_info_; 1107 WallpaperInfo info = current_user_wallpaper_info_;
990 SetUserWallpaperInfo(user_id, info, is_persistent); 1108 SetUserWallpaperInfo(user_id, info, is_persistent);
991 } 1109 }
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
1174 1292
1175 base::FilePath wallpaper_dir; 1293 base::FilePath wallpaper_dir;
1176 base::FilePath wallpaper_path; 1294 base::FilePath wallpaper_path;
1177 if (info.type == User::CUSTOMIZED || info.type == User::POLICY) { 1295 if (info.type == User::CUSTOMIZED || info.type == User::POLICY) {
1178 const char* sub_dir = GetCustomWallpaperSubdirForCurrentResolution(); 1296 const char* sub_dir = GetCustomWallpaperSubdirForCurrentResolution();
1179 base::FilePath wallpaper_path = GetCustomWallpaperDir(sub_dir); 1297 base::FilePath wallpaper_path = GetCustomWallpaperDir(sub_dir);
1180 wallpaper_path = wallpaper_path.Append(info.file); 1298 wallpaper_path = wallpaper_path.Append(info.file);
1181 task_runner_->PostTask( 1299 task_runner_->PostTask(
1182 FROM_HERE, 1300 FROM_HERE,
1183 base::Bind(&WallpaperManager::GetCustomWallpaperInternal, 1301 base::Bind(&WallpaperManager::GetCustomWallpaperInternal,
1184 base::Unretained(this),
1185 user_id, 1302 user_id,
1186 info, 1303 info,
1187 wallpaper_path, 1304 wallpaper_path,
1188 false /* do not update wallpaper */, 1305 false /* do not update wallpaper */,
1189 base::Passed(MovableOnDestroyCallbackHolder()))); 1306 base::Passed(MovableOnDestroyCallbackHolder()),
1307 weak_factory_.GetWeakPtr()));
1190 return; 1308 return;
1191 } 1309 }
1192 LoadWallpaper(user_id, 1310 LoadWallpaper(user_id,
1193 info, 1311 info,
1194 false /* do not update wallpaper */, 1312 false /* do not update wallpaper */,
1195 MovableOnDestroyCallbackHolder().Pass()); 1313 MovableOnDestroyCallbackHolder().Pass());
1196 } 1314 }
1197 } 1315 }
1198 1316
1199 void WallpaperManager::ClearObsoleteWallpaperPrefs() { 1317 void WallpaperManager::ClearObsoleteWallpaperPrefs() {
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
1380 if (!base::StringToInt64(date_string, &date_val)) 1498 if (!base::StringToInt64(date_string, &date_val))
1381 return false; 1499 return false;
1382 1500
1383 info->file = file; 1501 info->file = file;
1384 info->layout = static_cast<ash::WallpaperLayout>(layout); 1502 info->layout = static_cast<ash::WallpaperLayout>(layout);
1385 info->type = static_cast<User::WallpaperType>(type); 1503 info->type = static_cast<User::WallpaperType>(type);
1386 info->date = base::Time::FromInternalValue(date_val); 1504 info->date = base::Time::FromInternalValue(date_val);
1387 return true; 1505 return true;
1388 } 1506 }
1389 1507
1390 void WallpaperManager::MoveCustomWallpapersOnWorker(
1391 const std::string& user_id,
1392 const std::string& user_id_hash) {
1393 DCHECK(BrowserThread::GetBlockingPool()->
1394 IsRunningSequenceOnCurrentThread(sequence_token_));
1395 if (MoveCustomWallpaperDirectory(
1396 kOriginalWallpaperSubDir, user_id, user_id_hash)) {
1397 // Consider success if the original wallpaper is moved to the new directory.
1398 // Original wallpaper is the fallback if the correct resolution wallpaper
1399 // can not be found.
1400 BrowserThread::PostTask(
1401 BrowserThread::UI,
1402 FROM_HERE,
1403 base::Bind(&WallpaperManager::MoveCustomWallpapersSuccess,
1404 base::Unretained(this),
1405 user_id,
1406 user_id_hash));
1407 }
1408 MoveCustomWallpaperDirectory(kLargeWallpaperSubDir, user_id, user_id_hash);
1409 MoveCustomWallpaperDirectory(kSmallWallpaperSubDir, user_id, user_id_hash);
1410 MoveCustomWallpaperDirectory(
1411 kThumbnailWallpaperSubDir, user_id, user_id_hash);
1412 }
1413
1414 void WallpaperManager::MoveCustomWallpapersSuccess( 1508 void WallpaperManager::MoveCustomWallpapersSuccess(
1415 const std::string& user_id, 1509 const std::string& user_id,
1416 const std::string& user_id_hash) { 1510 const std::string& user_id_hash) {
1417 WallpaperInfo info; 1511 WallpaperInfo info;
1418 GetUserWallpaperInfo(user_id, &info); 1512 GetUserWallpaperInfo(user_id, &info);
1419 if (info.type == User::CUSTOMIZED) { 1513 if (info.type == User::CUSTOMIZED) {
1420 // New file field should include user id hash in addition to file name. 1514 // New file field should include user id hash in addition to file name.
1421 // This is needed because at login screen, user id hash is not available. 1515 // This is needed because at login screen, user id hash is not available.
1422 std::string relative_path = 1516 std::string relative_path =
1423 base::FilePath(user_id_hash).Append(info.file).value(); 1517 base::FilePath(user_id_hash).Append(info.file).value();
1424 info.file = relative_path; 1518 info.file = relative_path;
1425 bool is_persistent = 1519 bool is_persistent =
1426 !UserManager::Get()->IsUserNonCryptohomeDataEphemeral(user_id); 1520 !UserManager::Get()->IsUserNonCryptohomeDataEphemeral(user_id);
1427 SetUserWallpaperInfo(user_id, info, is_persistent); 1521 SetUserWallpaperInfo(user_id, info, is_persistent);
1428 } 1522 }
1429 } 1523 }
1430 1524
1431 void WallpaperManager::MoveLoggedInUserCustomWallpaper() { 1525 void WallpaperManager::MoveLoggedInUserCustomWallpaper() {
1432 const User* logged_in_user = UserManager::Get()->GetLoggedInUser(); 1526 const User* logged_in_user = UserManager::Get()->GetLoggedInUser();
1433 task_runner_->PostTask( 1527 task_runner_->PostTask(
1434 FROM_HERE, 1528 FROM_HERE,
1435 base::Bind(&WallpaperManager::MoveCustomWallpapersOnWorker, 1529 base::Bind(&WallpaperManager::MoveCustomWallpapersOnWorker,
1436 base::Unretained(this),
1437 logged_in_user->email(), 1530 logged_in_user->email(),
1438 logged_in_user->username_hash())); 1531 logged_in_user->username_hash(),
1439 } 1532 weak_factory_.GetWeakPtr()));
1440
1441 void WallpaperManager::GetCustomWallpaperInternal(
1442 const std::string& user_id,
1443 const WallpaperInfo& info,
1444 const base::FilePath& wallpaper_path,
1445 bool update_wallpaper,
1446 MovableOnDestroyCallbackHolder on_finish) {
1447 DCHECK(BrowserThread::GetBlockingPool()->
1448 IsRunningSequenceOnCurrentThread(sequence_token_));
1449
1450 base::FilePath valid_path = wallpaper_path;
1451 if (!base::PathExists(wallpaper_path)) {
1452 // Falls back on original file if the correct resolution file does not
1453 // exist. This may happen when the original custom wallpaper is small or
1454 // browser shutdown before resized wallpaper saved.
1455 valid_path = GetCustomWallpaperDir(kOriginalWallpaperSubDir);
1456 valid_path = valid_path.Append(info.file);
1457 }
1458
1459 if (!base::PathExists(valid_path)) {
1460 // Falls back to custom wallpaper that uses email as part of its file path.
1461 // Note that email is used instead of user_id_hash here.
1462 valid_path =
1463 GetCustomWallpaperPath(kOriginalWallpaperSubDir, user_id, info.file);
1464 }
1465
1466 if (!base::PathExists(valid_path)) {
1467 LOG(ERROR) << "Failed to load previously selected custom wallpaper. " <<
1468 "Fallback to default wallpaper";
1469 BrowserThread::PostTask(BrowserThread::UI,
1470 FROM_HERE,
1471 base::Bind(&WallpaperManager::DoSetDefaultWallpaper,
1472 base::Unretained(this),
1473 user_id,
1474 base::Passed(on_finish.Pass())));
1475 } else {
1476 BrowserThread::PostTask(BrowserThread::UI,
1477 FROM_HERE,
1478 base::Bind(&WallpaperManager::StartLoad,
1479 base::Unretained(this),
1480 user_id,
1481 info,
1482 update_wallpaper,
1483 valid_path,
1484 base::Passed(on_finish.Pass())));
1485 }
1486 } 1533 }
1487 1534
1488 void WallpaperManager::OnWallpaperDecoded( 1535 void WallpaperManager::OnWallpaperDecoded(
1489 const std::string& user_id, 1536 const std::string& user_id,
1490 ash::WallpaperLayout layout, 1537 ash::WallpaperLayout layout,
1491 bool update_wallpaper, 1538 bool update_wallpaper,
1492 MovableOnDestroyCallbackHolder on_finish, 1539 MovableOnDestroyCallbackHolder on_finish,
1493 const UserImage& user_image) { 1540 const UserImage& user_image) {
1494 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1541 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1495 TRACE_EVENT_ASYNC_END0("ui", "LoadAndDecodeWallpaper", this); 1542 TRACE_EVENT_ASYNC_END0("ui", "LoadAndDecodeWallpaper", this);
(...skipping 17 matching lines...) Expand all
1513 1560
1514 wallpaper_cache_[user_id] = user_image.image(); 1561 wallpaper_cache_[user_id] = user_image.image();
1515 1562
1516 if (update_wallpaper) { 1563 if (update_wallpaper) {
1517 ash::Shell::GetInstance() 1564 ash::Shell::GetInstance()
1518 ->desktop_background_controller() 1565 ->desktop_background_controller()
1519 ->SetWallpaperImage(user_image.image(), layout); 1566 ->SetWallpaperImage(user_image.image(), layout);
1520 } 1567 }
1521 } 1568 }
1522 1569
1523 void WallpaperManager::SaveCustomWallpaper(
1524 const std::string& user_id_hash,
1525 const base::FilePath& original_path,
1526 ash::WallpaperLayout layout,
1527 scoped_ptr<gfx::ImageSkia> image) const {
1528 DCHECK(BrowserThread::GetBlockingPool()->
1529 IsRunningSequenceOnCurrentThread(sequence_token_));
1530 EnsureCustomWallpaperDirectories(user_id_hash);
1531 std::string file_name = original_path.BaseName().value();
1532 base::FilePath small_wallpaper_path =
1533 GetCustomWallpaperPath(kSmallWallpaperSubDir, user_id_hash, file_name);
1534 base::FilePath large_wallpaper_path =
1535 GetCustomWallpaperPath(kLargeWallpaperSubDir, user_id_hash, file_name);
1536
1537 // Re-encode orginal file to jpeg format and saves the result in case that
1538 // resized wallpaper is not generated (i.e. chrome shutdown before resized
1539 // wallpaper is saved).
1540 ResizeAndSaveWallpaper(*image,
1541 original_path,
1542 ash::WALLPAPER_LAYOUT_STRETCH,
1543 image->width(),
1544 image->height(),
1545 NULL);
1546 DeleteAllExcept(original_path);
1547
1548 ResizeAndSaveWallpaper(*image,
1549 small_wallpaper_path,
1550 layout,
1551 kSmallWallpaperMaxWidth,
1552 kSmallWallpaperMaxHeight,
1553 NULL);
1554 DeleteAllExcept(small_wallpaper_path);
1555 ResizeAndSaveWallpaper(*image,
1556 large_wallpaper_path,
1557 layout,
1558 kLargeWallpaperMaxWidth,
1559 kLargeWallpaperMaxHeight,
1560 NULL);
1561 DeleteAllExcept(large_wallpaper_path);
1562 }
1563
1564 void WallpaperManager::RecordUma(User::WallpaperType type, int index) const {
1565 UMA_HISTOGRAM_ENUMERATION("Ash.Wallpaper.Type", type,
1566 User::WALLPAPER_TYPE_COUNT);
1567 }
1568
1569 void WallpaperManager::StartLoad(const std::string& user_id, 1570 void WallpaperManager::StartLoad(const std::string& user_id,
1570 const WallpaperInfo& info, 1571 const WallpaperInfo& info,
1571 bool update_wallpaper, 1572 bool update_wallpaper,
1572 const base::FilePath& wallpaper_path, 1573 const base::FilePath& wallpaper_path,
1573 MovableOnDestroyCallbackHolder on_finish) { 1574 MovableOnDestroyCallbackHolder on_finish) {
1574 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1575 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1575 TRACE_EVENT_ASYNC_BEGIN0("ui", "LoadAndDecodeWallpaper", this); 1576 TRACE_EVENT_ASYNC_BEGIN0("ui", "LoadAndDecodeWallpaper", this);
1576 1577
1577 wallpaper_loader_->Start(wallpaper_path.value(), 1578 wallpaper_loader_->Start(wallpaper_path.value(),
1578 0, // Do not crop. 1579 0, // Do not crop.
1579 base::Bind(&WallpaperManager::OnWallpaperDecoded, 1580 base::Bind(&WallpaperManager::OnWallpaperDecoded,
1580 base::Unretained(this), 1581 weak_factory_.GetWeakPtr(),
1581 user_id, 1582 user_id,
1582 info.layout, 1583 info.layout,
1583 update_wallpaper, 1584 update_wallpaper,
1584 base::Passed(on_finish.Pass()))); 1585 base::Passed(on_finish.Pass())));
1585 } 1586 }
1586 1587
1587 void WallpaperManager::SaveLastLoadTime(const base::TimeDelta elapsed) { 1588 void WallpaperManager::SaveLastLoadTime(const base::TimeDelta elapsed) {
1588 while (last_load_times_.size() >= kLastLoadsStatsMsMaxSize) 1589 while (last_load_times_.size() >= kLastLoadsStatsMsMaxSize)
1589 last_load_times_.pop_front(); 1590 last_load_times_.pop_front();
1590 1591
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1666 wallpaper.image().EnsureRepsForSupportedScales(); 1667 wallpaper.image().EnsureRepsForSupportedScales();
1667 scoped_ptr<gfx::ImageSkia> deep_copy(wallpaper.image().DeepCopy()); 1668 scoped_ptr<gfx::ImageSkia> deep_copy(wallpaper.image().DeepCopy());
1668 1669
1669 scoped_ptr<bool> success(new bool(false)); 1670 scoped_ptr<bool> success(new bool(false));
1670 scoped_ptr<gfx::ImageSkia> small_wallpaper_image(new gfx::ImageSkia); 1671 scoped_ptr<gfx::ImageSkia> small_wallpaper_image(new gfx::ImageSkia);
1671 scoped_ptr<gfx::ImageSkia> large_wallpaper_image(new gfx::ImageSkia); 1672 scoped_ptr<gfx::ImageSkia> large_wallpaper_image(new gfx::ImageSkia);
1672 1673
1673 // TODO(bshe): This may break if RawImage becomes RefCountedMemory. 1674 // TODO(bshe): This may break if RawImage becomes RefCountedMemory.
1674 base::Closure resize_closure = 1675 base::Closure resize_closure =
1675 base::Bind(&WallpaperManager::ResizeCustomizedDefaultWallpaper, 1676 base::Bind(&WallpaperManager::ResizeCustomizedDefaultWallpaper,
1676 base::Unretained(this),
1677 base::Passed(&deep_copy), 1677 base::Passed(&deep_copy),
1678 wallpaper.raw_image(), 1678 wallpaper.raw_image(),
1679 base::Unretained(rescaled_files.get()), 1679 base::Unretained(rescaled_files.get()),
1680 base::Unretained(success.get()), 1680 base::Unretained(success.get()),
1681 base::Unretained(small_wallpaper_image.get()), 1681 base::Unretained(small_wallpaper_image.get()),
1682 base::Unretained(large_wallpaper_image.get())); 1682 base::Unretained(large_wallpaper_image.get()));
1683 base::Closure on_resized_closure = 1683 base::Closure on_resized_closure =
1684 base::Bind(&WallpaperManager::OnCustomizedDefaultWallpaperResized, 1684 base::Bind(&WallpaperManager::OnCustomizedDefaultWallpaperResized,
1685 weak_factory_.GetWeakPtr(), 1685 weak_factory_.GetWeakPtr(),
1686 wallpaper_url, 1686 wallpaper_url,
1687 base::Passed(rescaled_files.Pass()), 1687 base::Passed(rescaled_files.Pass()),
1688 base::Passed(success.Pass()), 1688 base::Passed(success.Pass()),
1689 base::Passed(small_wallpaper_image.Pass()), 1689 base::Passed(small_wallpaper_image.Pass()),
1690 base::Passed(large_wallpaper_image.Pass())); 1690 base::Passed(large_wallpaper_image.Pass()));
1691 1691
1692 if (!task_runner_->PostTaskAndReply( 1692 if (!task_runner_->PostTaskAndReply(
1693 FROM_HERE, resize_closure, on_resized_closure)) { 1693 FROM_HERE, resize_closure, on_resized_closure)) {
1694 LOG(WARNING) << "Failed to start Customized Wallpaper resize."; 1694 LOG(WARNING) << "Failed to start Customized Wallpaper resize.";
1695 } 1695 }
1696 } 1696 }
1697 1697
1698 void WallpaperManager::ResizeCustomizedDefaultWallpaper( 1698 void WallpaperManager::ResizeCustomizedDefaultWallpaper(
1699 scoped_ptr<gfx::ImageSkia> image, 1699 scoped_ptr<gfx::ImageSkia> image,
1700 const UserImage::RawImage& raw_image, 1700 const UserImage::RawImage& raw_image,
1701 const CustomizedWallpaperRescaledFiles* rescaled_files, 1701 const CustomizedWallpaperRescaledFiles* rescaled_files,
1702 bool* success, 1702 bool* success,
1703 gfx::ImageSkia* small_wallpaper_image, 1703 gfx::ImageSkia* small_wallpaper_image,
1704 gfx::ImageSkia* large_wallpaper_image) { 1704 gfx::ImageSkia* large_wallpaper_image) {
1705 DCHECK(BrowserThread::GetBlockingPool()->IsRunningSequenceOnCurrentThread(
1706 sequence_token_));
1707
1708 *success = true; 1705 *success = true;
1709 1706
1710 *success &= ResizeAndSaveWallpaper(*image, 1707 *success &= ResizeAndSaveWallpaper(*image,
1711 rescaled_files->path_rescaled_small(), 1708 rescaled_files->path_rescaled_small(),
1712 ash::WALLPAPER_LAYOUT_STRETCH, 1709 ash::WALLPAPER_LAYOUT_STRETCH,
1713 kSmallWallpaperMaxWidth, 1710 kSmallWallpaperMaxWidth,
1714 kSmallWallpaperMaxHeight, 1711 kSmallWallpaperMaxHeight,
1715 small_wallpaper_image); 1712 small_wallpaper_image);
1716 1713
1717 *success &= ResizeAndSaveWallpaper(*image, 1714 *success &= ResizeAndSaveWallpaper(*image,
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
1906 loaded_wallpapers_++; 1903 loaded_wallpapers_++;
1907 SkBitmap bitmap; 1904 SkBitmap bitmap;
1908 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1, 0); 1905 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1, 0);
1909 bitmap.allocPixels(); 1906 bitmap.allocPixels();
1910 bitmap.eraseColor(kDefaultWallpaperColor); 1907 bitmap.eraseColor(kDefaultWallpaperColor);
1911 const gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap); 1908 const gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
1912 default_wallpaper_image_.reset(new UserImage(image)); 1909 default_wallpaper_image_.reset(new UserImage(image));
1913 } 1910 }
1914 1911
1915 } // namespace chromeos 1912 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698