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

Side by Side Diff: chrome/browser/profiles/profile_shortcut_manager_win.cc

Issue 1794353003: Refactor ProfileInfoCache in c/b/profiles (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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
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/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 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <string> 10 #include <string>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/command_line.h" 14 #include "base/command_line.h"
15 #include "base/files/file_enumerator.h" 15 #include "base/files/file_enumerator.h"
16 #include "base/files/file_util.h" 16 #include "base/files/file_util.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/path_service.h" 18 #include "base/path_service.h"
19 #include "base/strings/string16.h" 19 #include "base/strings/string16.h"
20 #include "base/strings/string_util.h" 20 #include "base/strings/string_util.h"
21 #include "base/strings/stringprintf.h" 21 #include "base/strings/stringprintf.h"
22 #include "base/strings/utf_string_conversions.h" 22 #include "base/strings/utf_string_conversions.h"
23 #include "base/win/shortcut.h" 23 #include "base/win/shortcut.h"
24 #include "chrome/browser/app_icon_win.h" 24 #include "chrome/browser/app_icon_win.h"
25 #include "chrome/browser/browser_process.h" 25 #include "chrome/browser/browser_process.h"
26 #include "chrome/browser/chrome_notification_types.h" 26 #include "chrome/browser/chrome_notification_types.h"
27 #include "chrome/browser/profiles/profile_attributes_entry.h"
28 #include "chrome/browser/profiles/profile_attributes_storage.h"
27 #include "chrome/browser/profiles/profile_avatar_icon_util.h" 29 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
28 #include "chrome/browser/profiles/profile_info_cache_observer.h"
29 #include "chrome/browser/profiles/profile_manager.h" 30 #include "chrome/browser/profiles/profile_manager.h"
30 #include "chrome/browser/shell_integration.h" 31 #include "chrome/browser/shell_integration.h"
31 #include "chrome/common/chrome_switches.h" 32 #include "chrome/common/chrome_switches.h"
32 #include "chrome/common/pref_names.h" 33 #include "chrome/common/pref_names.h"
33 #include "chrome/grit/chromium_strings.h" 34 #include "chrome/grit/chromium_strings.h"
34 #include "chrome/installer/util/browser_distribution.h" 35 #include "chrome/installer/util/browser_distribution.h"
35 #include "chrome/installer/util/product.h" 36 #include "chrome/installer/util/product.h"
36 #include "chrome/installer/util/shell_util.h" 37 #include "chrome/installer/util/shell_util.h"
37 #include "components/prefs/pref_service.h" 38 #include "components/prefs/pref_service.h"
38 #include "content/public/browser/browser_thread.h" 39 #include "content/public/browser/browser_thread.h"
(...skipping 632 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 672
672 ProfileShortcutManagerWin::ProfileShortcutManagerWin(ProfileManager* manager) 673 ProfileShortcutManagerWin::ProfileShortcutManagerWin(ProfileManager* manager)
673 : profile_manager_(manager) { 674 : profile_manager_(manager) {
674 DCHECK_EQ( 675 DCHECK_EQ(
675 arraysize(kProfileAvatarIconResources2x), 676 arraysize(kProfileAvatarIconResources2x),
676 profiles::GetDefaultAvatarIconCount()); 677 profiles::GetDefaultAvatarIconCount());
677 678
678 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED, 679 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED,
679 content::NotificationService::AllSources()); 680 content::NotificationService::AllSources());
680 681
681 profile_manager_->GetProfileInfoCache().AddObserver(this); 682 profile_manager_->GetProfileAttributesStorage().AddObserver(this);
682 } 683 }
683 684
684 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() { 685 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() {
685 profile_manager_->GetProfileInfoCache().RemoveObserver(this); 686 profile_manager_->GetProfileAttributesStorage().RemoveObserver(this);
686 } 687 }
687 688
688 void ProfileShortcutManagerWin::CreateOrUpdateProfileIcon( 689 void ProfileShortcutManagerWin::CreateOrUpdateProfileIcon(
689 const base::FilePath& profile_path) { 690 const base::FilePath& profile_path) {
690 CreateOrUpdateShortcutsForProfileAtPath(profile_path, 691 CreateOrUpdateShortcutsForProfileAtPath(profile_path,
691 CREATE_OR_UPDATE_ICON_ONLY, 692 CREATE_OR_UPDATE_ICON_ONLY,
692 IGNORE_NON_PROFILE_SHORTCUTS); 693 IGNORE_NON_PROFILE_SHORTCUTS);
693 } 694 }
694 695
695 void ProfileShortcutManagerWin::CreateProfileShortcut( 696 void ProfileShortcutManagerWin::CreateProfileShortcut(
(...skipping 21 matching lines...) Expand all
717 const base::FilePath& profile_path, 718 const base::FilePath& profile_path,
718 base::CommandLine* command_line, 719 base::CommandLine* command_line,
719 base::string16* name, 720 base::string16* name,
720 base::FilePath* icon_path) { 721 base::FilePath* icon_path) {
721 base::FilePath chrome_exe; 722 base::FilePath chrome_exe;
722 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { 723 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
723 NOTREACHED(); 724 NOTREACHED();
724 return; 725 return;
725 } 726 }
726 727
727 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); 728 ProfileAttributesStorage& storage =
728 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_path); 729 profile_manager_->GetProfileAttributesStorage();
729 DCHECK_LT(profile_index, cache.GetNumberOfProfiles()); 730 ProfileAttributesEntry* entry;
731 bool has_entry = storage.GetProfileAttributesWithPath(profile_path, &entry);
732 DCHECK(has_entry);
730 733
731 // The used profile name should be empty if there is only 1 profile. 734 // The used profile name should be empty if there is only 1 profile.
732 base::string16 shortcut_profile_name; 735 base::string16 shortcut_profile_name;
733 if (cache.GetNumberOfProfiles() > 1) 736 if (storage.GetNumberOfProfiles() > 1u)
734 shortcut_profile_name = cache.GetNameOfProfileAtIndex(profile_index); 737 shortcut_profile_name = entry->GetName();
735 738
736 *name = base::FilePath(profiles::internal::GetShortcutFilenameForProfile( 739 *name = base::FilePath(profiles::internal::GetShortcutFilenameForProfile(
737 shortcut_profile_name, 740 shortcut_profile_name,
738 BrowserDistribution::GetDistribution())).RemoveExtension().value(); 741 BrowserDistribution::GetDistribution())).RemoveExtension().value();
739 742
740 command_line->ParseFromString(L"\"" + chrome_exe.value() + L"\" " + 743 command_line->ParseFromString(L"\"" + chrome_exe.value() + L"\" " +
741 profiles::internal::CreateProfileShortcutFlags(profile_path)); 744 profiles::internal::CreateProfileShortcutFlags(profile_path));
742 745
743 *icon_path = profiles::internal::GetProfileIconPath(profile_path); 746 *icon_path = profiles::internal::GetProfileIconPath(profile_path);
744 } 747 }
745 748
746 void ProfileShortcutManagerWin::OnProfileAdded( 749 void ProfileShortcutManagerWin::OnProfileAdded(
747 const base::FilePath& profile_path) { 750 const base::FilePath& profile_path) {
748 CreateOrUpdateProfileIcon(profile_path); 751 CreateOrUpdateProfileIcon(profile_path);
749 if (profile_manager_->GetProfileInfoCache().GetNumberOfProfiles() == 2) { 752 if (profile_manager_->GetProfileAttributesStorage().GetNumberOfProfiles() ==
753 2u) {
750 // When the second profile is added, make existing non-profile shortcuts 754 // When the second profile is added, make existing non-profile shortcuts
751 // point to the first profile and be badged/named appropriately. 755 // point to the first profile and be badged/named appropriately.
752 CreateOrUpdateShortcutsForProfileAtPath(GetOtherProfilePath(profile_path), 756 CreateOrUpdateShortcutsForProfileAtPath(GetOtherProfilePath(profile_path),
753 UPDATE_EXISTING_ONLY, 757 UPDATE_EXISTING_ONLY,
754 UPDATE_NON_PROFILE_SHORTCUTS); 758 UPDATE_NON_PROFILE_SHORTCUTS);
755 } 759 }
756 } 760 }
757 761
758 void ProfileShortcutManagerWin::OnProfileWasRemoved( 762 void ProfileShortcutManagerWin::OnProfileWasRemoved(
759 const base::FilePath& profile_path, 763 const base::FilePath& profile_path,
760 const base::string16& profile_name) { 764 const base::string16& profile_name) {
761 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); 765 ProfileAttributesStorage& storage =
766 profile_manager_->GetProfileAttributesStorage();
762 // If there is only one profile remaining, remove the badging information 767 // If there is only one profile remaining, remove the badging information
763 // from an existing shortcut. 768 // from an existing shortcut.
764 const bool deleting_down_to_last_profile = (cache.GetNumberOfProfiles() == 1); 769 const bool deleting_down_to_last_profile =
770 (storage.GetNumberOfProfiles() == 1u);
765 if (deleting_down_to_last_profile) { 771 if (deleting_down_to_last_profile) {
766 // This is needed to unbadge the icon. 772 // This is needed to unbadge the icon.
767 CreateOrUpdateShortcutsForProfileAtPath(cache.GetPathOfProfileAtIndex(0), 773 CreateOrUpdateShortcutsForProfileAtPath(
768 UPDATE_EXISTING_ONLY, 774 storage.GetAllProfilesAttributes().front()->GetPath(),
769 IGNORE_NON_PROFILE_SHORTCUTS); 775 UPDATE_EXISTING_ONLY,
776 IGNORE_NON_PROFILE_SHORTCUTS);
770 } 777 }
771 778
772 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 779 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
773 base::Bind(&DeleteDesktopShortcuts, 780 base::Bind(&DeleteDesktopShortcuts,
774 profile_path, 781 profile_path,
775 deleting_down_to_last_profile)); 782 deleting_down_to_last_profile));
776 } 783 }
777 784
778 void ProfileShortcutManagerWin::OnProfileNameChanged( 785 void ProfileShortcutManagerWin::OnProfileNameChanged(
779 const base::FilePath& profile_path, 786 const base::FilePath& profile_path,
780 const base::string16& old_profile_name) { 787 const base::string16& old_profile_name) {
781 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY, 788 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY,
782 IGNORE_NON_PROFILE_SHORTCUTS); 789 IGNORE_NON_PROFILE_SHORTCUTS);
783 } 790 }
784 791
785 void ProfileShortcutManagerWin::OnProfileAvatarChanged( 792 void ProfileShortcutManagerWin::OnProfileAvatarChanged(
786 const base::FilePath& profile_path) { 793 const base::FilePath& profile_path) {
787 CreateOrUpdateProfileIcon(profile_path); 794 CreateOrUpdateProfileIcon(profile_path);
788 } 795 }
789 796
790 base::FilePath ProfileShortcutManagerWin::GetOtherProfilePath( 797 base::FilePath ProfileShortcutManagerWin::GetOtherProfilePath(
791 const base::FilePath& profile_path) { 798 const base::FilePath& profile_path) {
792 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); 799 const ProfileAttributesStorage& storage =
793 DCHECK_EQ(2U, cache.GetNumberOfProfiles()); 800 profile_manager_->GetProfileAttributesStorage();
801 DCHECK_EQ(2u, storage.GetNumberOfProfiles());
794 // Get the index of the current profile, in order to find the index of the 802 // Get the index of the current profile, in order to find the index of the
795 // other profile. 803 // other profile.
796 size_t current_profile_index = cache.GetIndexOfProfileWithPath(profile_path); 804 std::vector<ProfileAttributesEntry*> entries = g_browser_process->
797 size_t other_profile_index = (current_profile_index == 0) ? 1 : 0; 805 profile_manager()->GetProfileAttributesStorage().
798 return cache.GetPathOfProfileAtIndex(other_profile_index); 806 GetAllProfilesAttributes();
807 for (ProfileAttributesEntry* entry : entries) {
808 base::FilePath path = entry->GetPath();
809 if (path != profile_path)
810 return path;
811 }
812 NOTREACHED();
813 return base::FilePath();
799 } 814 }
800 815
801 void ProfileShortcutManagerWin::CreateOrUpdateShortcutsForProfileAtPath( 816 void ProfileShortcutManagerWin::CreateOrUpdateShortcutsForProfileAtPath(
802 const base::FilePath& profile_path, 817 const base::FilePath& profile_path,
803 CreateOrUpdateMode create_mode, 818 CreateOrUpdateMode create_mode,
804 NonProfileShortcutAction action) { 819 NonProfileShortcutAction action) {
805 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || 820 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
806 BrowserThread::CurrentlyOn(BrowserThread::UI)); 821 BrowserThread::CurrentlyOn(BrowserThread::UI));
807 CreateOrUpdateShortcutsParams params(profile_path, create_mode, action); 822 CreateOrUpdateShortcutsParams params(profile_path, create_mode, action);
808 823
809 ProfileInfoCache* cache = &profile_manager_->GetProfileInfoCache(); 824 ProfileAttributesStorage& storage =
810 size_t profile_index = cache->GetIndexOfProfileWithPath(profile_path); 825 profile_manager_->GetProfileAttributesStorage();
811 if (profile_index == std::string::npos) 826 ProfileAttributesEntry* entry;
827 bool has_entry = storage.GetProfileAttributesWithPath(profile_path, &entry);
828 if (!has_entry)
812 return; 829 return;
813 bool remove_badging = cache->GetNumberOfProfiles() == 1; 830 bool remove_badging = (storage.GetNumberOfProfiles() == 1u);
814 831
815 params.old_profile_name = 832 params.old_profile_name =
816 cache->GetShortcutNameOfProfileAtIndex(profile_index); 833 entry->GetShortcutName();
817 834
818 // Exit early if the mode is to update existing profile shortcuts only and 835 // Exit early if the mode is to update existing profile shortcuts only and
819 // none were ever created for this profile, per the shortcut name not being 836 // none were ever created for this profile, per the shortcut name not being
820 // set in the profile info cache. 837 // set in the profile attributes storage.
821 if (params.old_profile_name.empty() && 838 if (params.old_profile_name.empty() &&
822 create_mode == UPDATE_EXISTING_ONLY && 839 create_mode == UPDATE_EXISTING_ONLY &&
823 action == IGNORE_NON_PROFILE_SHORTCUTS) { 840 action == IGNORE_NON_PROFILE_SHORTCUTS) {
824 return; 841 return;
825 } 842 }
826 843
827 if (!remove_badging) { 844 if (!remove_badging) {
828 params.profile_name = cache->GetNameOfProfileAtIndex(profile_index); 845 params.profile_name = entry->GetName();
829 846
830 // The profile might be using the Gaia avatar, which is not in the 847 // The profile might be using the Gaia avatar, which is not in the
831 // resources array. 848 // resources array.
832 bool has_gaia_image = false; 849 bool has_gaia_image = false;
833 if (cache->IsUsingGAIAPictureOfProfileAtIndex(profile_index)) { 850 if (entry->IsUsingGAIAPicture()) {
834 const gfx::Image* image = 851 const gfx::Image* image = entry->GetGAIAPicture();
835 cache->GetGAIAPictureOfProfileAtIndex(profile_index);
836 if (image) { 852 if (image) {
837 params.avatar_image_1x = GetSkBitmapCopy(*image); 853 params.avatar_image_1x = GetSkBitmapCopy(*image);
838 // Gaia images are 256px, which makes them big enough to use in the 854 // Gaia images are 256px, which makes them big enough to use in the
839 // large icon case as well. 855 // large icon case as well.
840 DCHECK_GE(image->Width(), IconUtil::kLargeIconSize); 856 DCHECK_GE(image->Width(), IconUtil::kLargeIconSize);
841 params.avatar_image_2x = params.avatar_image_1x; 857 params.avatar_image_2x = params.avatar_image_1x;
842 has_gaia_image = true; 858 has_gaia_image = true;
843 } 859 }
844 } 860 }
845 861
846 // If the profile isn't using a Gaia image, or if the Gaia image did not 862 // If the profile isn't using a Gaia image, or if the Gaia image did not
847 // exist, revert to the previously used avatar icon. 863 // exist, revert to the previously used avatar icon.
848 if (!has_gaia_image) { 864 if (!has_gaia_image) {
849 const size_t icon_index = 865 const size_t icon_index = entry->GetAvatarIconIndex();
850 cache->GetAvatarIconIndexOfProfileAtIndex(profile_index);
851 const int resource_id_1x = 866 const int resource_id_1x =
852 profiles::GetDefaultAvatarIconResourceIDAtIndex(icon_index); 867 profiles::GetDefaultAvatarIconResourceIDAtIndex(icon_index);
853 const int resource_id_2x = kProfileAvatarIconResources2x[icon_index]; 868 const int resource_id_2x = kProfileAvatarIconResources2x[icon_index];
854 // Make a copy of the SkBitmaps to ensure that we can safely use the image 869 // Make a copy of the SkBitmaps to ensure that we can safely use the image
855 // data on the FILE thread. 870 // data on the FILE thread.
856 params.avatar_image_1x = GetImageResourceSkBitmapCopy(resource_id_1x); 871 params.avatar_image_1x = GetImageResourceSkBitmapCopy(resource_id_1x);
857 params.avatar_image_2x = GetImageResourceSkBitmapCopy(resource_id_2x); 872 params.avatar_image_2x = GetImageResourceSkBitmapCopy(resource_id_2x);
858 } 873 }
859 } 874 }
860 BrowserThread::PostTask( 875 BrowserThread::PostTask(
861 BrowserThread::FILE, FROM_HERE, 876 BrowserThread::FILE, FROM_HERE,
862 base::Bind(&CreateOrUpdateDesktopShortcutsAndIconForProfile, params)); 877 base::Bind(&CreateOrUpdateDesktopShortcutsAndIconForProfile, params));
863 878
864 cache->SetShortcutNameOfProfileAtIndex(profile_index, 879 entry->SetShortcutName(params.profile_name);
865 params.profile_name);
866 } 880 }
867 881
868 void ProfileShortcutManagerWin::Observe( 882 void ProfileShortcutManagerWin::Observe(
869 int type, 883 int type,
870 const content::NotificationSource& source, 884 const content::NotificationSource& source,
871 const content::NotificationDetails& details) { 885 const content::NotificationDetails& details) {
872 switch (type) { 886 switch (type) {
873 // This notification is triggered when a profile is loaded. 887 // This notification is triggered when a profile is loaded.
874 case chrome::NOTIFICATION_PROFILE_CREATED: { 888 case chrome::NOTIFICATION_PROFILE_CREATED: {
875 Profile* profile = 889 Profile* profile =
876 content::Source<Profile>(source).ptr()->GetOriginalProfile(); 890 content::Source<Profile>(source).ptr()->GetOriginalProfile();
877 if (profile->GetPrefs()->GetInteger(prefs::kProfileIconVersion) < 891 if (profile->GetPrefs()->GetInteger(prefs::kProfileIconVersion) <
878 kCurrentProfileIconVersion) { 892 kCurrentProfileIconVersion) {
879 // Ensure the profile's icon file has been created. 893 // Ensure the profile's icon file has been created.
880 CreateOrUpdateProfileIcon(profile->GetPath()); 894 CreateOrUpdateProfileIcon(profile->GetPath());
881 } 895 }
882 break; 896 break;
883 } 897 }
884 default: 898 default:
885 NOTREACHED(); 899 NOTREACHED();
886 break; 900 break;
887 } 901 }
888 } 902 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698