OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/login/wallpaper_manager.h" | 5 #include "chrome/browser/chromeos/login/wallpaper_manager.h" |
6 | 6 |
7 #include <numeric> | 7 #include <numeric> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "ash/shell.h" | 10 #include "ash/shell.h" |
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
398 } | 398 } |
399 | 399 |
400 base::FilePath WallpaperManager::GetCustomWallpaperPath( | 400 base::FilePath WallpaperManager::GetCustomWallpaperPath( |
401 const char* sub_dir, | 401 const char* sub_dir, |
402 const std::string& user_id_hash, | 402 const std::string& user_id_hash, |
403 const std::string& file) const { | 403 const std::string& file) const { |
404 base::FilePath custom_wallpaper_path = GetCustomWallpaperDir(sub_dir); | 404 base::FilePath custom_wallpaper_path = GetCustomWallpaperDir(sub_dir); |
405 return custom_wallpaper_path.Append(user_id_hash).Append(file); | 405 return custom_wallpaper_path.Append(user_id_hash).Append(file); |
406 } | 406 } |
407 | 407 |
408 base::FilePath WallpaperManager::GetOriginalWallpaperPathForUser( | |
409 const std::string& user_id) { | |
410 std::string filename = user_id + kOriginalCustomWallpaperSuffix; | |
411 base::FilePath user_data_dir; | |
412 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | |
413 return user_data_dir.AppendASCII(filename); | |
414 } | |
415 | |
416 bool WallpaperManager::GetLoggedInUserWallpaperInfo(WallpaperInfo* info) { | 408 bool WallpaperManager::GetLoggedInUserWallpaperInfo(WallpaperInfo* info) { |
417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 409 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
418 | 410 |
419 if (UserManager::Get()->IsLoggedInAsStub()) { | 411 if (UserManager::Get()->IsLoggedInAsStub()) { |
420 info->file = current_user_wallpaper_info_.file = ""; | 412 info->file = current_user_wallpaper_info_.file = ""; |
421 info->layout = current_user_wallpaper_info_.layout = | 413 info->layout = current_user_wallpaper_info_.layout = |
422 ash::WALLPAPER_LAYOUT_CENTER_CROPPED; | 414 ash::WALLPAPER_LAYOUT_CENTER_CROPPED; |
423 info->type = current_user_wallpaper_info_.type = User::DEFAULT; | 415 info->type = current_user_wallpaper_info_.type = User::DEFAULT; |
416 info->date = current_user_wallpaper_info_.date = | |
417 base::Time::Now().LocalMidnight(); | |
424 return true; | 418 return true; |
425 } | 419 } |
426 | 420 |
427 return GetUserWallpaperInfo(UserManager::Get()->GetLoggedInUser()->email(), | 421 return GetUserWallpaperInfo(UserManager::Get()->GetLoggedInUser()->email(), |
428 info); | 422 info); |
429 } | 423 } |
430 | 424 |
431 void WallpaperManager::InitializeWallpaper() { | 425 void WallpaperManager::InitializeWallpaper() { |
432 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 426 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
433 UserManager* user_manager = UserManager::Get(); | 427 UserManager* user_manager = UserManager::Get(); |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
669 // If decoded wallpaper is empty, we have probably failed to decode the file. | 663 // If decoded wallpaper is empty, we have probably failed to decode the file. |
670 // Use default wallpaper in this case. | 664 // Use default wallpaper in this case. |
671 if (wallpaper.image().isNull()) { | 665 if (wallpaper.image().isNull()) { |
672 SetDefaultWallpaperDelayed(user_id); | 666 SetDefaultWallpaperDelayed(user_id); |
673 return; | 667 return; |
674 } | 668 } |
675 | 669 |
676 bool is_persistent = | 670 bool is_persistent = |
677 !UserManager::Get()->IsUserNonCryptohomeDataEphemeral(user_id); | 671 !UserManager::Get()->IsUserNonCryptohomeDataEphemeral(user_id); |
678 | 672 |
679 wallpaper.image().EnsureRepsForSupportedScales(); | |
680 scoped_ptr<gfx::ImageSkia> deep_copy(wallpaper.image().DeepCopy()); | |
681 | |
682 WallpaperInfo wallpaper_info = { | 673 WallpaperInfo wallpaper_info = { |
683 wallpaper_path.value(), | 674 wallpaper_path.value(), |
684 layout, | 675 layout, |
685 type, | 676 type, |
686 // Date field is not used. | 677 // Date field is not used. |
687 base::Time::Now().LocalMidnight() | 678 base::Time::Now().LocalMidnight() |
688 }; | 679 }; |
689 // Block shutdown on this task. Otherwise, we may lose the custom wallpaper | 680 if (is_persistent) { |
690 // that the user selected. | 681 wallpaper.image().EnsureRepsForSupportedScales(); |
691 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner = | 682 scoped_ptr<gfx::ImageSkia> deep_copy(wallpaper.image().DeepCopy()); |
692 BrowserThread::GetBlockingPool()-> | 683 // Block shutdown on this task. Otherwise, we may lose the custom wallpaper |
693 GetSequencedTaskRunnerWithShutdownBehavior(sequence_token_, | 684 // that the user selected. |
694 base::SequencedWorkerPool::BLOCK_SHUTDOWN); | 685 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner = |
695 // TODO(bshe): This may break if RawImage becomes RefCountedMemory. | 686 BrowserThread::GetBlockingPool()-> |
696 blocking_task_runner->PostTask(FROM_HERE, | 687 GetSequencedTaskRunnerWithShutdownBehavior( |
697 base::Bind(&WallpaperManager::ProcessCustomWallpaper, | 688 sequence_token_, base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
698 base::Unretained(this), | 689 // TODO(bshe): This may break if RawImage becomes RefCountedMemory. |
699 user_id_hash, | 690 blocking_task_runner->PostTask( |
700 is_persistent, | 691 FROM_HERE, |
701 wallpaper_info, | 692 base::Bind(&WallpaperManager::SaveCustomWallpaper, |
702 base::Passed(&deep_copy), | 693 base::Unretained(this), |
703 wallpaper.raw_image())); | 694 user_id_hash, |
695 base::FilePath(wallpaper_info.file), | |
696 wallpaper_info.layout, | |
697 UserImage(*deep_copy, wallpaper.raw_image()))); | |
bshe
2014/04/02 15:11:10
the ownership of deep_copy is not passed to the |S
Thiemo Nagel
2014/04/02 15:39:56
I don't understand what you're concerned about. C
bshe
2014/04/02 16:32:19
+oshima
I remember it could cause problem when tr
Thiemo Nagel
2014/04/09 16:00:24
Thanks a lot for the explanation! I've fixed this
| |
698 } | |
704 | 699 |
705 std::string relative_path = base::FilePath(user_id_hash).Append(file).value(); | 700 std::string relative_path = base::FilePath(user_id_hash).Append(file).value(); |
706 // User's custom wallpaper path is determined by relative path and the | 701 // User's custom wallpaper path is determined by relative path and the |
707 // appropriate wallpaper resolution in GetCustomWallpaperInternal. | 702 // appropriate wallpaper resolution in GetCustomWallpaperInternal. |
708 WallpaperInfo info = { | 703 WallpaperInfo info = { |
709 relative_path, | 704 relative_path, |
710 layout, | 705 layout, |
711 type, | 706 type, |
712 base::Time::Now().LocalMidnight() | 707 base::Time::Now().LocalMidnight() |
713 }; | 708 }; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
772 | 767 |
773 base::DictionaryValue* wallpaper_info_dict = new base::DictionaryValue(); | 768 base::DictionaryValue* wallpaper_info_dict = new base::DictionaryValue(); |
774 wallpaper_info_dict->SetString(kNewWallpaperDateNodeName, | 769 wallpaper_info_dict->SetString(kNewWallpaperDateNodeName, |
775 base::Int64ToString(info.date.ToInternalValue())); | 770 base::Int64ToString(info.date.ToInternalValue())); |
776 wallpaper_info_dict->SetString(kNewWallpaperFileNodeName, info.file); | 771 wallpaper_info_dict->SetString(kNewWallpaperFileNodeName, info.file); |
777 wallpaper_info_dict->SetInteger(kNewWallpaperLayoutNodeName, info.layout); | 772 wallpaper_info_dict->SetInteger(kNewWallpaperLayoutNodeName, info.layout); |
778 wallpaper_info_dict->SetInteger(kNewWallpaperTypeNodeName, info.type); | 773 wallpaper_info_dict->SetInteger(kNewWallpaperTypeNodeName, info.type); |
779 wallpaper_update->SetWithoutPathExpansion(user_id, wallpaper_info_dict); | 774 wallpaper_update->SetWithoutPathExpansion(user_id, wallpaper_info_dict); |
780 } | 775 } |
781 | 776 |
782 void WallpaperManager::SetLastSelectedUser( | |
783 const std::string& last_selected_user) { | |
784 last_selected_user_ = last_selected_user; | |
785 } | |
786 | |
787 void WallpaperManager::SetUserWallpaperDelayed(const std::string& user_id) { | 777 void WallpaperManager::SetUserWallpaperDelayed(const std::string& user_id) { |
788 ScheduleSetUserWallpaper(user_id, true); | 778 ScheduleSetUserWallpaper(user_id, true); |
789 } | 779 } |
790 | 780 |
791 void WallpaperManager::SetUserWallpaperNow(const std::string& user_id) { | 781 void WallpaperManager::SetUserWallpaperNow(const std::string& user_id) { |
792 ScheduleSetUserWallpaper(user_id, false); | 782 ScheduleSetUserWallpaper(user_id, false); |
793 } | 783 } |
794 | 784 |
795 void WallpaperManager::ScheduleSetUserWallpaper(const std::string& user_id, | 785 void WallpaperManager::ScheduleSetUserWallpaper(const std::string& user_id, |
796 bool delayed) { | 786 bool delayed) { |
797 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 787 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
798 // Some unit tests come here without a UserManager or without a pref system. | 788 // Some unit tests come here without a UserManager or without a pref system. |
799 if (!UserManager::IsInitialized() || !g_browser_process->local_state()) | 789 if (!UserManager::IsInitialized() || !g_browser_process->local_state()) |
800 return; | 790 return; |
801 // There is no visible background in kiosk mode. | 791 // There is no visible background in kiosk mode. |
802 if (UserManager::Get()->IsLoggedInAsKioskApp()) | 792 if (UserManager::Get()->IsLoggedInAsKioskApp()) |
803 return; | 793 return; |
804 // Guest user, regular user in ephemeral mode, or kiosk app. | 794 // Guest user, regular user in ephemeral mode, or kiosk app. |
805 const User* user = UserManager::Get()->FindUser(user_id); | 795 const User* user = UserManager::Get()->FindUser(user_id); |
806 if (UserManager::Get()->IsUserNonCryptohomeDataEphemeral(user_id) || | 796 if (UserManager::Get()->IsUserNonCryptohomeDataEphemeral(user_id) || |
807 (user != NULL && user->GetType() == User::USER_TYPE_KIOSK_APP)) { | 797 (user != NULL && user->GetType() == User::USER_TYPE_KIOSK_APP)) { |
808 InitInitialUserWallpaper(user_id, false); | 798 InitInitialUserWallpaper(user_id, false); |
809 GetPendingWallpaper(user_id, delayed)->ResetSetDefaultWallpaper(); | 799 GetPendingWallpaper(user_id, delayed)->ResetSetDefaultWallpaper(); |
810 return; | 800 return; |
811 } | 801 } |
812 | 802 |
813 if (!UserManager::Get()->IsKnownUser(user_id)) | 803 if (!UserManager::Get()->IsKnownUser(user_id)) |
814 return; | 804 return; |
815 | 805 |
816 SetLastSelectedUser(user_id); | 806 last_selected_user_ = user_id; |
817 | 807 |
818 WallpaperInfo info; | 808 WallpaperInfo info; |
819 | 809 |
820 if (!GetUserWallpaperInfo(user_id, &info)) { | 810 if (!GetUserWallpaperInfo(user_id, &info)) { |
821 InitInitialUserWallpaper(user_id, true); | 811 InitInitialUserWallpaper(user_id, true); |
822 GetUserWallpaperInfo(user_id, &info); | 812 GetUserWallpaperInfo(user_id, &info); |
823 } | 813 } |
824 | 814 |
825 gfx::ImageSkia user_wallpaper; | 815 gfx::ImageSkia user_wallpaper; |
826 current_user_wallpaper_info_ = info; | 816 current_user_wallpaper_info_ = info; |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
979 | 969 |
980 void WallpaperManager::ClearObsoleteWallpaperPrefs() { | 970 void WallpaperManager::ClearObsoleteWallpaperPrefs() { |
981 PrefService* prefs = g_browser_process->local_state(); | 971 PrefService* prefs = g_browser_process->local_state(); |
982 DictionaryPrefUpdate wallpaper_properties_pref(prefs, | 972 DictionaryPrefUpdate wallpaper_properties_pref(prefs, |
983 kUserWallpapersProperties); | 973 kUserWallpapersProperties); |
984 wallpaper_properties_pref->Clear(); | 974 wallpaper_properties_pref->Clear(); |
985 DictionaryPrefUpdate wallpapers_pref(prefs, kUserWallpapers); | 975 DictionaryPrefUpdate wallpapers_pref(prefs, kUserWallpapers); |
986 wallpapers_pref->Clear(); | 976 wallpapers_pref->Clear(); |
987 } | 977 } |
988 | 978 |
979 // static | |
989 void WallpaperManager::DeleteAllExcept(const base::FilePath& path) { | 980 void WallpaperManager::DeleteAllExcept(const base::FilePath& path) { |
990 base::FilePath dir = path.DirName(); | 981 base::FilePath dir = path.DirName(); |
991 if (base::DirectoryExists(dir)) { | 982 if (base::DirectoryExists(dir)) { |
992 base::FileEnumerator files(dir, false, base::FileEnumerator::FILES); | 983 base::FileEnumerator files(dir, false, base::FileEnumerator::FILES); |
993 for (base::FilePath current = files.Next(); !current.empty(); | 984 for (base::FilePath current = files.Next(); !current.empty(); |
994 current = files.Next()) { | 985 current = files.Next()) { |
995 if (current != path) | 986 if (current != path) |
996 base::DeleteFile(current, false); | 987 base::DeleteFile(current, false); |
997 } | 988 } |
998 } | 989 } |
999 } | 990 } |
1000 | 991 |
992 // static | |
1001 void WallpaperManager::DeleteWallpaperInList( | 993 void WallpaperManager::DeleteWallpaperInList( |
1002 const std::vector<base::FilePath>& file_list) { | 994 const std::vector<base::FilePath>& file_list) { |
1003 for (std::vector<base::FilePath>::const_iterator it = file_list.begin(); | 995 for (std::vector<base::FilePath>::const_iterator it = file_list.begin(); |
1004 it != file_list.end(); ++it) { | 996 it != file_list.end(); ++it) { |
1005 base::FilePath path = *it; | 997 base::FilePath path = *it; |
1006 // Some users may still have legacy wallpapers with png extension. We need | 998 // Some users may still have legacy wallpapers with png extension. We need |
1007 // to delete these wallpapers too. | 999 // to delete these wallpapers too. |
1008 if (!base::DeleteFile(path, true) && | 1000 if (!base::DeleteFile(path, true) && |
1009 !base::DeleteFile(path.AddExtension(".png"), false)) { | 1001 !base::DeleteFile(path.AddExtension(".png"), false)) { |
1010 LOG(ERROR) << "Failed to remove user wallpaper at " << path.value(); | 1002 LOG(ERROR) << "Failed to remove user wallpaper at " << path.value(); |
(...skipping 26 matching lines...) Expand all Loading... | |
1037 | 1029 |
1038 // Remove original user wallpaper. | 1030 // Remove original user wallpaper. |
1039 wallpaper_path = GetCustomWallpaperDir(kOriginalWallpaperSubDir); | 1031 wallpaper_path = GetCustomWallpaperDir(kOriginalWallpaperSubDir); |
1040 file_to_remove.push_back(wallpaper_path.Append(user_id)); | 1032 file_to_remove.push_back(wallpaper_path.Append(user_id)); |
1041 wallpaper_path = wallpaper_path.Append(path_to_file); | 1033 wallpaper_path = wallpaper_path.Append(path_to_file); |
1042 file_to_remove.push_back(wallpaper_path); | 1034 file_to_remove.push_back(wallpaper_path); |
1043 | 1035 |
1044 base::WorkerPool::PostTask( | 1036 base::WorkerPool::PostTask( |
1045 FROM_HERE, | 1037 FROM_HERE, |
1046 base::Bind(&WallpaperManager::DeleteWallpaperInList, | 1038 base::Bind(&WallpaperManager::DeleteWallpaperInList, |
1047 base::Unretained(this), | |
1048 file_to_remove), | 1039 file_to_remove), |
1049 false); | 1040 false); |
1050 } | 1041 } |
1051 | 1042 |
1043 // static | |
1052 void WallpaperManager::EnsureCustomWallpaperDirectories( | 1044 void WallpaperManager::EnsureCustomWallpaperDirectories( |
1053 const std::string& user_id_hash) { | 1045 const std::string& user_id_hash) { |
1054 base::FilePath dir; | 1046 base::FilePath dir; |
1055 dir = GetCustomWallpaperDir(kSmallWallpaperSubDir); | 1047 dir = GetCustomWallpaperDir(kSmallWallpaperSubDir); |
1056 dir = dir.Append(user_id_hash); | 1048 dir = dir.Append(user_id_hash); |
1057 if (!base::PathExists(dir)) | 1049 if (!base::PathExists(dir)) |
1058 base::CreateDirectory(dir); | 1050 base::CreateDirectory(dir); |
1059 dir = GetCustomWallpaperDir(kLargeWallpaperSubDir); | 1051 dir = GetCustomWallpaperDir(kLargeWallpaperSubDir); |
1060 dir = dir.Append(user_id_hash); | 1052 dir = dir.Append(user_id_hash); |
1061 if (!base::PathExists(dir)) | 1053 if (!base::PathExists(dir)) |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1319 UserManager::IsMultipleProfilesAllowed()) { | 1311 UserManager::IsMultipleProfilesAllowed()) { |
1320 wallpaper_cache_[user_id] = wallpaper.image(); | 1312 wallpaper_cache_[user_id] = wallpaper.image(); |
1321 } | 1313 } |
1322 | 1314 |
1323 if (update_wallpaper) { | 1315 if (update_wallpaper) { |
1324 ash::Shell::GetInstance()->desktop_background_controller()-> | 1316 ash::Shell::GetInstance()->desktop_background_controller()-> |
1325 SetCustomWallpaper(wallpaper.image(), layout); | 1317 SetCustomWallpaper(wallpaper.image(), layout); |
1326 } | 1318 } |
1327 } | 1319 } |
1328 | 1320 |
1329 void WallpaperManager::ProcessCustomWallpaper( | |
1330 const std::string& user_id_hash, | |
1331 bool persistent, | |
1332 const WallpaperInfo& info, | |
1333 scoped_ptr<gfx::ImageSkia> image, | |
1334 const UserImage::RawImage& raw_image) { | |
1335 DCHECK(BrowserThread::GetBlockingPool()-> | |
1336 IsRunningSequenceOnCurrentThread(sequence_token_)); | |
1337 UserImage wallpaper(*image.get(), raw_image); | |
1338 if (persistent) { | |
1339 SaveCustomWallpaper(user_id_hash, base::FilePath(info.file), info.layout, | |
1340 wallpaper); | |
1341 } | |
1342 } | |
1343 | |
1344 void WallpaperManager::SaveCustomWallpaper(const std::string& user_id_hash, | 1321 void WallpaperManager::SaveCustomWallpaper(const std::string& user_id_hash, |
1345 const base::FilePath& original_path, | 1322 const base::FilePath& original_path, |
1346 ash::WallpaperLayout layout, | 1323 ash::WallpaperLayout layout, |
1347 const UserImage& wallpaper) { | 1324 const UserImage& wallpaper) const { |
1348 DCHECK(BrowserThread::GetBlockingPool()-> | 1325 DCHECK(BrowserThread::GetBlockingPool()-> |
1349 IsRunningSequenceOnCurrentThread(sequence_token_)); | 1326 IsRunningSequenceOnCurrentThread(sequence_token_)); |
1350 EnsureCustomWallpaperDirectories(user_id_hash); | 1327 EnsureCustomWallpaperDirectories(user_id_hash); |
1351 std::string file_name = original_path.BaseName().value(); | 1328 std::string file_name = original_path.BaseName().value(); |
1352 base::FilePath small_wallpaper_path = | 1329 base::FilePath small_wallpaper_path = |
1353 GetCustomWallpaperPath(kSmallWallpaperSubDir, user_id_hash, file_name); | 1330 GetCustomWallpaperPath(kSmallWallpaperSubDir, user_id_hash, file_name); |
1354 base::FilePath large_wallpaper_path = | 1331 base::FilePath large_wallpaper_path = |
1355 GetCustomWallpaperPath(kLargeWallpaperSubDir, user_id_hash, file_name); | 1332 GetCustomWallpaperPath(kLargeWallpaperSubDir, user_id_hash, file_name); |
1356 | 1333 |
1357 // Re-encode orginal file to jpeg format and saves the result in case that | 1334 // Re-encode orginal file to jpeg format and saves the result in case that |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1449 loading_.push_back(new WallpaperManager::PendingWallpaper( | 1426 loading_.push_back(new WallpaperManager::PendingWallpaper( |
1450 (delayed ? GetWallpaperLoadDelay() | 1427 (delayed ? GetWallpaperLoadDelay() |
1451 : base::TimeDelta::FromMilliseconds(0)), | 1428 : base::TimeDelta::FromMilliseconds(0)), |
1452 user_id)); | 1429 user_id)); |
1453 pending_inactive_ = loading_.back(); | 1430 pending_inactive_ = loading_.back(); |
1454 } | 1431 } |
1455 return pending_inactive_; | 1432 return pending_inactive_; |
1456 } | 1433 } |
1457 | 1434 |
1458 } // namespace chromeos | 1435 } // namespace chromeos |
OLD | NEW |