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

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: Fix a bug that causes unit tests without debug code to fail. Created 4 years, 8 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 <algorithm> 10 #include <algorithm>
11 #include <set> 11 #include <set>
12 #include <string> 12 #include <string>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/bind.h" 15 #include "base/bind.h"
16 #include "base/command_line.h" 16 #include "base/command_line.h"
17 #include "base/files/file_enumerator.h" 17 #include "base/files/file_enumerator.h"
18 #include "base/files/file_util.h" 18 #include "base/files/file_util.h"
19 #include "base/macros.h" 19 #include "base/macros.h"
20 #include "base/path_service.h" 20 #include "base/path_service.h"
21 #include "base/strings/string16.h" 21 #include "base/strings/string16.h"
22 #include "base/strings/string_util.h" 22 #include "base/strings/string_util.h"
23 #include "base/strings/stringprintf.h" 23 #include "base/strings/stringprintf.h"
24 #include "base/strings/utf_string_conversions.h" 24 #include "base/strings/utf_string_conversions.h"
25 #include "base/win/shortcut.h" 25 #include "base/win/shortcut.h"
26 #include "chrome/browser/app_icon_win.h" 26 #include "chrome/browser/app_icon_win.h"
27 #include "chrome/browser/browser_process.h" 27 #include "chrome/browser/browser_process.h"
28 #include "chrome/browser/chrome_notification_types.h" 28 #include "chrome/browser/chrome_notification_types.h"
29 #include "chrome/browser/profiles/profile_attributes_entry.h"
30 #include "chrome/browser/profiles/profile_attributes_storage.h"
29 #include "chrome/browser/profiles/profile_avatar_icon_util.h" 31 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
30 #include "chrome/browser/profiles/profile_info_cache_observer.h"
31 #include "chrome/browser/profiles/profile_manager.h" 32 #include "chrome/browser/profiles/profile_manager.h"
32 #include "chrome/browser/shell_integration_win.h" 33 #include "chrome/browser/shell_integration_win.h"
33 #include "chrome/common/chrome_switches.h" 34 #include "chrome/common/chrome_switches.h"
34 #include "chrome/common/pref_names.h" 35 #include "chrome/common/pref_names.h"
35 #include "chrome/grit/chromium_strings.h" 36 #include "chrome/grit/chromium_strings.h"
36 #include "chrome/installer/util/browser_distribution.h" 37 #include "chrome/installer/util/browser_distribution.h"
37 #include "chrome/installer/util/product.h" 38 #include "chrome/installer/util/product.h"
38 #include "chrome/installer/util/shell_util.h" 39 #include "chrome/installer/util/shell_util.h"
39 #include "components/prefs/pref_service.h" 40 #include "components/prefs/pref_service.h"
40 #include "content/public/browser/browser_thread.h" 41 #include "content/public/browser/browser_thread.h"
(...skipping 756 matching lines...) Expand 10 before | Expand all | Expand 10 after
797 798
798 ProfileShortcutManagerWin::ProfileShortcutManagerWin(ProfileManager* manager) 799 ProfileShortcutManagerWin::ProfileShortcutManagerWin(ProfileManager* manager)
799 : profile_manager_(manager) { 800 : profile_manager_(manager) {
800 DCHECK_EQ( 801 DCHECK_EQ(
801 arraysize(kProfileAvatarIconResources2x), 802 arraysize(kProfileAvatarIconResources2x),
802 profiles::GetDefaultAvatarIconCount()); 803 profiles::GetDefaultAvatarIconCount());
803 804
804 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED, 805 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED,
805 content::NotificationService::AllSources()); 806 content::NotificationService::AllSources());
806 807
807 profile_manager_->GetProfileInfoCache().AddObserver(this); 808 profile_manager_->GetProfileAttributesStorage().AddObserver(this);
808 } 809 }
809 810
810 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() { 811 ProfileShortcutManagerWin::~ProfileShortcutManagerWin() {
811 profile_manager_->GetProfileInfoCache().RemoveObserver(this); 812 profile_manager_->GetProfileAttributesStorage().RemoveObserver(this);
812 } 813 }
813 814
814 void ProfileShortcutManagerWin::CreateOrUpdateProfileIcon( 815 void ProfileShortcutManagerWin::CreateOrUpdateProfileIcon(
815 const base::FilePath& profile_path) { 816 const base::FilePath& profile_path) {
816 CreateOrUpdateShortcutsForProfileAtPath(profile_path, 817 CreateOrUpdateShortcutsForProfileAtPath(profile_path,
817 CREATE_OR_UPDATE_ICON_ONLY, 818 CREATE_OR_UPDATE_ICON_ONLY,
818 IGNORE_NON_PROFILE_SHORTCUTS); 819 IGNORE_NON_PROFILE_SHORTCUTS);
819 } 820 }
820 821
821 void ProfileShortcutManagerWin::CreateProfileShortcut( 822 void ProfileShortcutManagerWin::CreateProfileShortcut(
(...skipping 21 matching lines...) Expand all
843 const base::FilePath& profile_path, 844 const base::FilePath& profile_path,
844 base::CommandLine* command_line, 845 base::CommandLine* command_line,
845 base::string16* name, 846 base::string16* name,
846 base::FilePath* icon_path) { 847 base::FilePath* icon_path) {
847 base::FilePath chrome_exe; 848 base::FilePath chrome_exe;
848 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { 849 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
849 NOTREACHED(); 850 NOTREACHED();
850 return; 851 return;
851 } 852 }
852 853
853 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); 854 ProfileAttributesStorage& storage =
854 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_path); 855 profile_manager_->GetProfileAttributesStorage();
855 DCHECK_LT(profile_index, cache.GetNumberOfProfiles()); 856 ProfileAttributesEntry* entry;
857 bool has_entry = storage.GetProfileAttributesWithPath(profile_path, &entry);
858 DCHECK(has_entry);
856 859
857 // The used profile name should be empty if there is only 1 profile. 860 // The used profile name should be empty if there is only 1 profile.
858 base::string16 shortcut_profile_name; 861 base::string16 shortcut_profile_name;
859 if (cache.GetNumberOfProfiles() > 1) 862 if (storage.GetNumberOfProfiles() > 1u)
860 shortcut_profile_name = cache.GetNameOfProfileAtIndex(profile_index); 863 shortcut_profile_name = entry->GetName();
861 864
862 *name = base::FilePath(profiles::internal::GetShortcutFilenameForProfile( 865 *name = base::FilePath(profiles::internal::GetShortcutFilenameForProfile(
863 shortcut_profile_name, 866 shortcut_profile_name,
864 BrowserDistribution::GetDistribution())).RemoveExtension().value(); 867 BrowserDistribution::GetDistribution())).RemoveExtension().value();
865 868
866 command_line->ParseFromString(L"\"" + chrome_exe.value() + L"\" " + 869 command_line->ParseFromString(L"\"" + chrome_exe.value() + L"\" " +
867 profiles::internal::CreateProfileShortcutFlags(profile_path)); 870 profiles::internal::CreateProfileShortcutFlags(profile_path));
868 871
869 *icon_path = profiles::internal::GetProfileIconPath(profile_path); 872 *icon_path = profiles::internal::GetProfileIconPath(profile_path);
870 } 873 }
871 874
872 void ProfileShortcutManagerWin::OnProfileAdded( 875 void ProfileShortcutManagerWin::OnProfileAdded(
873 const base::FilePath& profile_path) { 876 const base::FilePath& profile_path) {
874 CreateOrUpdateProfileIcon(profile_path); 877 CreateOrUpdateProfileIcon(profile_path);
875 if (profile_manager_->GetProfileInfoCache().GetNumberOfProfiles() == 2) { 878 if (profile_manager_->GetProfileAttributesStorage().GetNumberOfProfiles() ==
879 2u) {
876 // When the second profile is added, make existing non-profile shortcuts 880 // When the second profile is added, make existing non-profile shortcuts
877 // point to the first profile and be badged/named appropriately. 881 // point to the first profile and be badged/named appropriately.
878 CreateOrUpdateShortcutsForProfileAtPath(GetOtherProfilePath(profile_path), 882 CreateOrUpdateShortcutsForProfileAtPath(GetOtherProfilePath(profile_path),
879 UPDATE_EXISTING_ONLY, 883 UPDATE_EXISTING_ONLY,
880 UPDATE_NON_PROFILE_SHORTCUTS); 884 UPDATE_NON_PROFILE_SHORTCUTS);
881 } 885 }
882 } 886 }
883 887
884 void ProfileShortcutManagerWin::OnProfileWasRemoved( 888 void ProfileShortcutManagerWin::OnProfileWasRemoved(
885 const base::FilePath& profile_path, 889 const base::FilePath& profile_path,
886 const base::string16& profile_name) { 890 const base::string16& profile_name) {
887 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); 891 ProfileAttributesStorage& storage =
892 profile_manager_->GetProfileAttributesStorage();
888 // If there is only one profile remaining, remove the badging information 893 // If there is only one profile remaining, remove the badging information
889 // from an existing shortcut. 894 // from an existing shortcut.
890 const bool deleting_down_to_last_profile = (cache.GetNumberOfProfiles() == 1); 895 const bool deleting_down_to_last_profile =
896 (storage.GetNumberOfProfiles() == 1u);
891 if (deleting_down_to_last_profile) { 897 if (deleting_down_to_last_profile) {
892 // This is needed to unbadge the icon. 898 // This is needed to unbadge the icon.
893 CreateOrUpdateShortcutsForProfileAtPath(cache.GetPathOfProfileAtIndex(0), 899 CreateOrUpdateShortcutsForProfileAtPath(
894 UPDATE_EXISTING_ONLY, 900 storage.GetAllProfilesAttributes().front()->GetPath(),
895 IGNORE_NON_PROFILE_SHORTCUTS); 901 UPDATE_EXISTING_ONLY,
902 IGNORE_NON_PROFILE_SHORTCUTS);
896 } 903 }
897 904
898 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 905 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
899 base::Bind(&DeleteDesktopShortcuts, 906 base::Bind(&DeleteDesktopShortcuts,
900 profile_path, 907 profile_path,
901 deleting_down_to_last_profile)); 908 deleting_down_to_last_profile));
902 } 909 }
903 910
904 void ProfileShortcutManagerWin::OnProfileNameChanged( 911 void ProfileShortcutManagerWin::OnProfileNameChanged(
905 const base::FilePath& profile_path, 912 const base::FilePath& profile_path,
906 const base::string16& old_profile_name) { 913 const base::string16& old_profile_name) {
907 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY, 914 CreateOrUpdateShortcutsForProfileAtPath(profile_path, UPDATE_EXISTING_ONLY,
908 IGNORE_NON_PROFILE_SHORTCUTS); 915 IGNORE_NON_PROFILE_SHORTCUTS);
909 } 916 }
910 917
911 void ProfileShortcutManagerWin::OnProfileAvatarChanged( 918 void ProfileShortcutManagerWin::OnProfileAvatarChanged(
912 const base::FilePath& profile_path) { 919 const base::FilePath& profile_path) {
913 CreateOrUpdateProfileIcon(profile_path); 920 CreateOrUpdateProfileIcon(profile_path);
914 } 921 }
915 922
916 base::FilePath ProfileShortcutManagerWin::GetOtherProfilePath( 923 base::FilePath ProfileShortcutManagerWin::GetOtherProfilePath(
917 const base::FilePath& profile_path) { 924 const base::FilePath& profile_path) {
918 const ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); 925 const ProfileAttributesStorage& storage =
919 DCHECK_EQ(2U, cache.GetNumberOfProfiles()); 926 profile_manager_->GetProfileAttributesStorage();
927 DCHECK_EQ(2u, storage.GetNumberOfProfiles());
920 // Get the index of the current profile, in order to find the index of the 928 // Get the index of the current profile, in order to find the index of the
921 // other profile. 929 // other profile.
922 size_t current_profile_index = cache.GetIndexOfProfileWithPath(profile_path); 930 std::vector<ProfileAttributesEntry*> entries = g_browser_process->
923 size_t other_profile_index = (current_profile_index == 0) ? 1 : 0; 931 profile_manager()->GetProfileAttributesStorage().
924 return cache.GetPathOfProfileAtIndex(other_profile_index); 932 GetAllProfilesAttributes();
933 for (ProfileAttributesEntry* entry : entries) {
934 base::FilePath path = entry->GetPath();
935 if (path != profile_path)
936 return path;
937 }
938 NOTREACHED();
939 return base::FilePath();
925 } 940 }
926 941
927 void ProfileShortcutManagerWin::CreateOrUpdateShortcutsForProfileAtPath( 942 void ProfileShortcutManagerWin::CreateOrUpdateShortcutsForProfileAtPath(
928 const base::FilePath& profile_path, 943 const base::FilePath& profile_path,
929 CreateOrUpdateMode create_mode, 944 CreateOrUpdateMode create_mode,
930 NonProfileShortcutAction action) { 945 NonProfileShortcutAction action) {
931 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || 946 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
932 BrowserThread::CurrentlyOn(BrowserThread::UI)); 947 BrowserThread::CurrentlyOn(BrowserThread::UI));
933 CreateOrUpdateShortcutsParams params(profile_path, create_mode, action); 948 CreateOrUpdateShortcutsParams params(profile_path, create_mode, action);
934 949
935 ProfileInfoCache* cache = &profile_manager_->GetProfileInfoCache(); 950 ProfileAttributesStorage& storage =
936 size_t profile_index = cache->GetIndexOfProfileWithPath(profile_path); 951 profile_manager_->GetProfileAttributesStorage();
937 if (profile_index == std::string::npos) 952 ProfileAttributesEntry* entry;
953 bool has_entry = storage.GetProfileAttributesWithPath(profile_path, &entry);
954 if (!has_entry)
938 return; 955 return;
939 bool remove_badging = cache->GetNumberOfProfiles() == 1; 956 bool remove_badging = (storage.GetNumberOfProfiles() == 1u);
940 957
941 params.old_profile_name = 958 params.old_profile_name =
942 cache->GetShortcutNameOfProfileAtIndex(profile_index); 959 entry->GetShortcutName();
943 960
944 // Exit early if the mode is to update existing profile shortcuts only and 961 // Exit early if the mode is to update existing profile shortcuts only and
945 // none were ever created for this profile, per the shortcut name not being 962 // none were ever created for this profile, per the shortcut name not being
946 // set in the profile info cache. 963 // set in the profile attributes storage.
947 if (params.old_profile_name.empty() && 964 if (params.old_profile_name.empty() &&
948 create_mode == UPDATE_EXISTING_ONLY && 965 create_mode == UPDATE_EXISTING_ONLY &&
949 action == IGNORE_NON_PROFILE_SHORTCUTS) { 966 action == IGNORE_NON_PROFILE_SHORTCUTS) {
950 return; 967 return;
951 } 968 }
952 969
953 if (!remove_badging) { 970 if (!remove_badging) {
954 params.profile_name = cache->GetNameOfProfileAtIndex(profile_index); 971 params.profile_name = entry->GetName();
955 972
956 // The profile might be using the Gaia avatar, which is not in the 973 // The profile might be using the Gaia avatar, which is not in the
957 // resources array. 974 // resources array.
958 bool has_gaia_image = false; 975 bool has_gaia_image = false;
959 if (cache->IsUsingGAIAPictureOfProfileAtIndex(profile_index)) { 976 if (entry->IsUsingGAIAPicture()) {
960 const gfx::Image* image = 977 const gfx::Image* image = entry->GetGAIAPicture();
961 cache->GetGAIAPictureOfProfileAtIndex(profile_index);
962 if (image) { 978 if (image) {
963 params.avatar_image_1x = GetSkBitmapCopy(*image); 979 params.avatar_image_1x = GetSkBitmapCopy(*image);
964 // Gaia images are 256px, which makes them big enough to use in the 980 // Gaia images are 256px, which makes them big enough to use in the
965 // large icon case as well. 981 // large icon case as well.
966 DCHECK_GE(image->Width(), IconUtil::kLargeIconSize); 982 DCHECK_GE(image->Width(), IconUtil::kLargeIconSize);
967 params.avatar_image_2x = params.avatar_image_1x; 983 params.avatar_image_2x = params.avatar_image_1x;
968 has_gaia_image = true; 984 has_gaia_image = true;
969 } 985 }
970 } 986 }
971 987
972 // If the profile isn't using a Gaia image, or if the Gaia image did not 988 // If the profile isn't using a Gaia image, or if the Gaia image did not
973 // exist, revert to the previously used avatar icon. 989 // exist, revert to the previously used avatar icon.
974 if (!has_gaia_image) { 990 if (!has_gaia_image) {
975 const size_t icon_index = 991 const size_t icon_index = entry->GetAvatarIconIndex();
976 cache->GetAvatarIconIndexOfProfileAtIndex(profile_index);
977 const int resource_id_1x = 992 const int resource_id_1x =
978 profiles::GetDefaultAvatarIconResourceIDAtIndex(icon_index); 993 profiles::GetDefaultAvatarIconResourceIDAtIndex(icon_index);
979 const int resource_id_2x = kProfileAvatarIconResources2x[icon_index]; 994 const int resource_id_2x = kProfileAvatarIconResources2x[icon_index];
980 // Make a copy of the SkBitmaps to ensure that we can safely use the image 995 // Make a copy of the SkBitmaps to ensure that we can safely use the image
981 // data on the FILE thread. 996 // data on the FILE thread.
982 params.avatar_image_1x = GetImageResourceSkBitmapCopy(resource_id_1x); 997 params.avatar_image_1x = GetImageResourceSkBitmapCopy(resource_id_1x);
983 params.avatar_image_2x = GetImageResourceSkBitmapCopy(resource_id_2x); 998 params.avatar_image_2x = GetImageResourceSkBitmapCopy(resource_id_2x);
984 } 999 }
985 } 1000 }
986 BrowserThread::PostTask( 1001 BrowserThread::PostTask(
987 BrowserThread::FILE, FROM_HERE, 1002 BrowserThread::FILE, FROM_HERE,
988 base::Bind(&CreateOrUpdateDesktopShortcutsAndIconForProfile, params)); 1003 base::Bind(&CreateOrUpdateDesktopShortcutsAndIconForProfile, params));
989 1004
990 cache->SetShortcutNameOfProfileAtIndex(profile_index, 1005 entry->SetShortcutName(params.profile_name);
991 params.profile_name);
992 } 1006 }
993 1007
994 void ProfileShortcutManagerWin::Observe( 1008 void ProfileShortcutManagerWin::Observe(
995 int type, 1009 int type,
996 const content::NotificationSource& source, 1010 const content::NotificationSource& source,
997 const content::NotificationDetails& details) { 1011 const content::NotificationDetails& details) {
998 switch (type) { 1012 switch (type) {
999 // This notification is triggered when a profile is loaded. 1013 // This notification is triggered when a profile is loaded.
1000 case chrome::NOTIFICATION_PROFILE_CREATED: { 1014 case chrome::NOTIFICATION_PROFILE_CREATED: {
1001 Profile* profile = 1015 Profile* profile =
1002 content::Source<Profile>(source).ptr()->GetOriginalProfile(); 1016 content::Source<Profile>(source).ptr()->GetOriginalProfile();
1003 if (profile->GetPrefs()->GetInteger(prefs::kProfileIconVersion) < 1017 if (profile->GetPrefs()->GetInteger(prefs::kProfileIconVersion) <
1004 kCurrentProfileIconVersion) { 1018 kCurrentProfileIconVersion) {
1005 // Ensure the profile's icon file has been created. 1019 // Ensure the profile's icon file has been created.
1006 CreateOrUpdateProfileIcon(profile->GetPath()); 1020 CreateOrUpdateProfileIcon(profile->GetPath());
1007 } 1021 }
1008 break; 1022 break;
1009 } 1023 }
1010 default: 1024 default:
1011 NOTREACHED(); 1025 NOTREACHED();
1012 break; 1026 break;
1013 } 1027 }
1014 } 1028 }
OLDNEW
« no previous file with comments | « chrome/browser/profiles/profile_shortcut_manager_win.h ('k') | chrome/browser/profiles/profile_window.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698