OLD | NEW |
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/profiles/profile_shortcut_manager_win.h" | 5 #include "chrome/browser/profiles/profile_shortcut_manager_win.h" |
6 | 6 |
7 #include <shlobj.h> // For SHChangeNotify(). | 7 #include <shlobj.h> // For SHChangeNotify(). |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 711 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
722 | 722 |
723 void ProfileShortcutManagerWin::OnProfileWasRemoved( | 723 void ProfileShortcutManagerWin::OnProfileWasRemoved( |
724 const base::FilePath& profile_path, | 724 const base::FilePath& profile_path, |
725 const string16& profile_name) { | 725 const string16& profile_name) { |
726 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); | 726 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); |
727 // If there is only one profile remaining, remove the badging information | 727 // If there is only one profile remaining, remove the badging information |
728 // from an existing shortcut. | 728 // from an existing shortcut. |
729 const bool deleting_down_to_last_profile = (cache.GetNumberOfProfiles() == 1); | 729 const bool deleting_down_to_last_profile = (cache.GetNumberOfProfiles() == 1); |
730 if (deleting_down_to_last_profile) { | 730 if (deleting_down_to_last_profile) { |
731 // This is needed to unbadge the icon. | 731 // This is needed to unbadge the icon. |
732 CreateOrUpdateShortcutsForProfileAtPath(cache.GetPathOfProfileAtIndex(0), | 732 const std::vector<ProfileInfoEntry> entries(cache.GetProfilesSortedByName())
; |
| 733 CreateOrUpdateShortcutsForProfileAtPath(entries[0].path(), |
733 UPDATE_EXISTING_ONLY, | 734 UPDATE_EXISTING_ONLY, |
734 IGNORE_NON_PROFILE_SHORTCUTS, | 735 IGNORE_NON_PROFILE_SHORTCUTS, |
735 base::Closure()); | 736 base::Closure()); |
736 } | 737 } |
737 | 738 |
738 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 739 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
739 base::Bind(&DeleteDesktopShortcuts, | 740 base::Bind(&DeleteDesktopShortcuts, |
740 profile_path, | 741 profile_path, |
741 deleting_down_to_last_profile)); | 742 deleting_down_to_last_profile)); |
742 } | 743 } |
743 | 744 |
744 void ProfileShortcutManagerWin::OnProfileNameChanged( | 745 void ProfileShortcutManagerWin::OnProfileNameChanged( |
745 const base::FilePath& profile_path, | 746 const base::FilePath& profile_path, |
746 const string16& old_profile_name) { | 747 const string16& old_profile_name) { |
747 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY, | 748 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY, |
748 IGNORE_NON_PROFILE_SHORTCUTS, | 749 IGNORE_NON_PROFILE_SHORTCUTS, |
749 base::Closure()); | 750 base::Closure()); |
750 } | 751 } |
751 | 752 |
752 void ProfileShortcutManagerWin::OnProfileAvatarChanged( | 753 void ProfileShortcutManagerWin::OnProfileAvatarChanged( |
753 const base::FilePath& profile_path) { | 754 const base::FilePath& profile_path) { |
754 CreateOrUpdateProfileIcon(profile_path, base::Closure()); | 755 CreateOrUpdateProfileIcon(profile_path, base::Closure()); |
755 } | 756 } |
756 | 757 |
757 base::FilePath ProfileShortcutManagerWin::GetOtherProfilePath( | 758 base::FilePath ProfileShortcutManagerWin::GetOtherProfilePath( |
758 const base::FilePath& profile_path) { | 759 const base::FilePath& profile_path) { |
759 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); | 760 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); |
760 DCHECK_EQ(2U, cache.GetNumberOfProfiles()); | 761 DCHECK_EQ(2U, cache.GetNumberOfProfiles()); |
| 762 |
761 // Get the index of the current profile, in order to find the index of the | 763 // Get the index of the current profile, in order to find the index of the |
762 // other profile. | 764 // other profile. |
763 size_t current_profile_index = cache.GetIndexOfProfileWithPath(profile_path); | 765 base::FilePath other_path; |
764 size_t other_profile_index = (current_profile_index == 0) ? 1 : 0; | 766 const std::vector<ProfileInfoEntry> entries(cache.GetProfilesSortedByName()); |
765 return cache.GetPathOfProfileAtIndex(other_profile_index); | 767 for (std::vector<ProfileInfoEntry>::const_iterator it = entries.begin(); |
| 768 it != entries.end(); ++it) { |
| 769 if (it->path() != profile_path) |
| 770 return it->path(); |
| 771 } |
| 772 |
| 773 // We should never actually reach this point, but need to return something |
| 774 // to make the compiler happy. |
| 775 return profile_path; |
766 } | 776 } |
767 | 777 |
768 void ProfileShortcutManagerWin::CreateOrUpdateShortcutsForProfileAtPath( | 778 void ProfileShortcutManagerWin::CreateOrUpdateShortcutsForProfileAtPath( |
769 const base::FilePath& profile_path, | 779 const base::FilePath& profile_path, |
770 CreateOrUpdateMode create_mode, | 780 CreateOrUpdateMode create_mode, |
771 NonProfileShortcutAction action, | 781 NonProfileShortcutAction action, |
772 const base::Closure& callback) { | 782 const base::Closure& callback) { |
773 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || | 783 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || |
774 BrowserThread::CurrentlyOn(BrowserThread::UI)); | 784 BrowserThread::CurrentlyOn(BrowserThread::UI)); |
775 CreateOrUpdateShortcutsParams params(profile_path, create_mode, action); | 785 CreateOrUpdateShortcutsParams params(profile_path, create_mode, action); |
776 | 786 |
777 ProfileInfoCache* cache = &profile_manager_->GetProfileInfoCache(); | 787 ProfileInfoCache* cache = &profile_manager_->GetProfileInfoCache(); |
778 size_t profile_index = cache->GetIndexOfProfileWithPath(profile_path); | 788 ProfileInfoEntry entry; |
779 if (profile_index == std::string::npos) | 789 if (!cache->GetInfoForProfile(profile_path, &entry)) |
780 return; | 790 return; |
| 791 |
781 bool remove_badging = cache->GetNumberOfProfiles() == 1; | 792 bool remove_badging = cache->GetNumberOfProfiles() == 1; |
782 | 793 |
783 params.old_profile_name = | 794 params.old_profile_name = entry.shortcut_name(); |
784 cache->GetShortcutNameOfProfileAtIndex(profile_index); | |
785 | 795 |
786 // Exit early if the mode is to update existing profile shortcuts only and | 796 // Exit early if the mode is to update existing profile shortcuts only and |
787 // none were ever created for this profile, per the shortcut name not being | 797 // none were ever created for this profile, per the shortcut name not being |
788 // set in the profile info cache. | 798 // set in the profile info cache. |
789 if (params.old_profile_name.empty() && | 799 if (params.old_profile_name.empty() && |
790 create_mode == UPDATE_EXISTING_ONLY && | 800 create_mode == UPDATE_EXISTING_ONLY && |
791 action == IGNORE_NON_PROFILE_SHORTCUTS) { | 801 action == IGNORE_NON_PROFILE_SHORTCUTS) { |
792 return; | 802 return; |
793 } | 803 } |
794 | 804 |
795 if (!remove_badging) { | 805 if (!remove_badging) { |
796 params.profile_name = cache->GetNameOfProfileAtIndex(profile_index); | 806 params.profile_name = entry.GetDisplayName(); |
797 | 807 |
798 const size_t icon_index = | 808 const size_t icon_index = entry.icon_index(); |
799 cache->GetAvatarIconIndexOfProfileAtIndex(profile_index); | |
800 const int resource_id_1x = | 809 const int resource_id_1x = |
801 cache->GetDefaultAvatarIconResourceIDAtIndex(icon_index); | 810 cache->GetDefaultAvatarIconResourceIDAtIndex(icon_index); |
802 const int resource_id_2x = kProfileAvatarIconResources2x[icon_index]; | 811 const int resource_id_2x = kProfileAvatarIconResources2x[icon_index]; |
803 // Make a copy of the SkBitmaps to ensure that we can safely use the image | 812 // Make a copy of the SkBitmaps to ensure that we can safely use the image |
804 // data on the FILE thread. | 813 // data on the FILE thread. |
805 params.avatar_image_1x = GetImageResourceSkBitmapCopy(resource_id_1x); | 814 params.avatar_image_1x = GetImageResourceSkBitmapCopy(resource_id_1x); |
806 params.avatar_image_2x = GetImageResourceSkBitmapCopy(resource_id_2x); | 815 params.avatar_image_2x = GetImageResourceSkBitmapCopy(resource_id_2x); |
807 } | 816 } |
808 BrowserThread::PostTask( | 817 BrowserThread::PostTask( |
809 BrowserThread::FILE, FROM_HERE, | 818 BrowserThread::FILE, FROM_HERE, |
810 base::Bind(&CreateOrUpdateDesktopShortcutsAndIconForProfile, params, | 819 base::Bind(&CreateOrUpdateDesktopShortcutsAndIconForProfile, params, |
811 callback)); | 820 callback)); |
812 | 821 |
813 cache->SetShortcutNameOfProfileAtIndex(profile_index, | 822 entry.set_shortcut_name(params.profile_name); |
814 params.profile_name); | 823 cache->SetInfoForProfile(entry); |
815 } | 824 } |
816 | 825 |
817 void ProfileShortcutManagerWin::Observe( | 826 void ProfileShortcutManagerWin::Observe( |
818 int type, | 827 int type, |
819 const content::NotificationSource& source, | 828 const content::NotificationSource& source, |
820 const content::NotificationDetails& details) { | 829 const content::NotificationDetails& details) { |
821 switch (type) { | 830 switch (type) { |
822 // This notification is triggered when a profile is loaded. | 831 // This notification is triggered when a profile is loaded. |
823 case chrome::NOTIFICATION_PROFILE_CREATED: { | 832 case chrome::NOTIFICATION_PROFILE_CREATED: { |
824 Profile* profile = | 833 Profile* profile = |
825 content::Source<Profile>(source).ptr()->GetOriginalProfile(); | 834 content::Source<Profile>(source).ptr()->GetOriginalProfile(); |
826 if (profile->GetPrefs()->GetInteger(prefs::kProfileIconVersion) < | 835 if (profile->GetPrefs()->GetInteger(prefs::kProfileIconVersion) < |
827 kCurrentProfileIconVersion) { | 836 kCurrentProfileIconVersion) { |
828 // Ensure the profile's icon file has been created. | 837 // Ensure the profile's icon file has been created. |
829 CreateOrUpdateProfileIcon( | 838 CreateOrUpdateProfileIcon( |
830 profile->GetPath(), | 839 profile->GetPath(), |
831 base::Bind(&OnProfileIconCreateSuccess, profile->GetPath())); | 840 base::Bind(&OnProfileIconCreateSuccess, profile->GetPath())); |
832 } | 841 } |
833 break; | 842 break; |
834 } | 843 } |
835 default: | 844 default: |
836 NOTREACHED(); | 845 NOTREACHED(); |
837 break; | 846 break; |
838 } | 847 } |
839 } | 848 } |
OLD | NEW |