| 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 <set> | 5 #include <set> |
| 6 | 6 |
| 7 #include "chrome/browser/profiles/profile_manager.h" | 7 #include "chrome/browser/profiles/profile_manager.h" |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 std::vector<Profile*> ProfileManager::GetLastOpenedProfiles() { | 232 std::vector<Profile*> ProfileManager::GetLastOpenedProfiles() { |
| 233 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 233 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 234 return profile_manager->GetLastOpenedProfiles( | 234 return profile_manager->GetLastOpenedProfiles( |
| 235 profile_manager->user_data_dir_); | 235 profile_manager->user_data_dir_); |
| 236 } | 236 } |
| 237 | 237 |
| 238 ProfileManager::ProfileManager(const FilePath& user_data_dir) | 238 ProfileManager::ProfileManager(const FilePath& user_data_dir) |
| 239 : user_data_dir_(user_data_dir), | 239 : user_data_dir_(user_data_dir), |
| 240 logged_in_(false), | 240 logged_in_(false), |
| 241 will_import_(false), | 241 will_import_(false), |
| 242 profile_shortcut_manager_(NULL), |
| 242 #if !defined(OS_ANDROID) | 243 #if !defined(OS_ANDROID) |
| 243 ALLOW_THIS_IN_INITIALIZER_LIST( | 244 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 244 browser_list_observer_(this)), | 245 browser_list_observer_(this)), |
| 245 #endif | 246 #endif |
| 246 closing_all_browsers_(false) { | 247 closing_all_browsers_(false) { |
| 247 #if defined(OS_CHROMEOS) | 248 #if defined(OS_CHROMEOS) |
| 248 registrar_.Add( | 249 registrar_.Add( |
| 249 this, | 250 this, |
| 250 chrome::NOTIFICATION_LOGIN_USER_CHANGED, | 251 chrome::NOTIFICATION_LOGIN_USER_CHANGED, |
| 251 content::NotificationService::AllSources()); | 252 content::NotificationService::AllSources()); |
| 252 #endif | 253 #endif |
| 253 registrar_.Add( | 254 registrar_.Add( |
| 254 this, | 255 this, |
| 255 chrome::NOTIFICATION_BROWSER_OPENED, | 256 chrome::NOTIFICATION_BROWSER_OPENED, |
| 256 content::NotificationService::AllSources()); | 257 content::NotificationService::AllSources()); |
| 257 registrar_.Add( | 258 registrar_.Add( |
| 258 this, | 259 this, |
| 259 chrome::NOTIFICATION_BROWSER_CLOSED, | 260 chrome::NOTIFICATION_BROWSER_CLOSED, |
| 260 content::NotificationService::AllSources()); | 261 content::NotificationService::AllSources()); |
| 261 registrar_.Add( | 262 registrar_.Add( |
| 262 this, | 263 this, |
| 263 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, | 264 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST, |
| 264 content::NotificationService::AllSources()); | 265 content::NotificationService::AllSources()); |
| 265 registrar_.Add( | 266 registrar_.Add( |
| 266 this, | 267 this, |
| 267 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, | 268 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, |
| 268 content::NotificationService::AllSources()); | 269 content::NotificationService::AllSources()); |
| 270 |
| 271 if (ProfileShortcutManager::IsFeatureEnabled()) |
| 272 profile_shortcut_manager_.reset(ProfileShortcutManager::Create()); |
| 269 } | 273 } |
| 270 | 274 |
| 271 ProfileManager::~ProfileManager() { | 275 ProfileManager::~ProfileManager() { |
| 272 #if defined(OS_WIN) | |
| 273 if (profile_shortcut_manager_.get()) | |
| 274 profile_info_cache_->RemoveObserver(profile_shortcut_manager_.get()); | |
| 275 #endif | |
| 276 } | 276 } |
| 277 | 277 |
| 278 FilePath ProfileManager::GetDefaultProfileDir( | 278 FilePath ProfileManager::GetDefaultProfileDir( |
| 279 const FilePath& user_data_dir) { | 279 const FilePath& user_data_dir) { |
| 280 FilePath default_profile_dir(user_data_dir); | 280 FilePath default_profile_dir(user_data_dir); |
| 281 default_profile_dir = | 281 default_profile_dir = |
| 282 default_profile_dir.AppendASCII(chrome::kInitialProfile); | 282 default_profile_dir.AppendASCII(chrome::kInitialProfile); |
| 283 return default_profile_dir; | 283 return default_profile_dir; |
| 284 } | 284 } |
| 285 | 285 |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 RegisterProfile(CreateProfileAsyncHelper(profile_path, this), false); | 447 RegisterProfile(CreateProfileAsyncHelper(profile_path, this), false); |
| 448 ProfileInfoCache& cache = GetProfileInfoCache(); | 448 ProfileInfoCache& cache = GetProfileInfoCache(); |
| 449 // Get the icon index from the user's icon url | 449 // Get the icon index from the user's icon url |
| 450 size_t icon_index; | 450 size_t icon_index; |
| 451 std::string icon_url_std = UTF16ToASCII(icon_url); | 451 std::string icon_url_std = UTF16ToASCII(icon_url); |
| 452 if (cache.IsDefaultAvatarIconUrl(icon_url_std, &icon_index)) { | 452 if (cache.IsDefaultAvatarIconUrl(icon_url_std, &icon_index)) { |
| 453 // add profile to cache with user selected name and avatar | 453 // add profile to cache with user selected name and avatar |
| 454 cache.AddProfileToCache(profile_path, name, string16(), icon_index); | 454 cache.AddProfileToCache(profile_path, name, string16(), icon_index); |
| 455 } | 455 } |
| 456 info->callbacks.push_back(callback); | 456 info->callbacks.push_back(callback); |
| 457 if (profile_shortcut_manager_.get() && !name.empty() && |
| 458 !icon_url.empty()) { |
| 459 BrowserThread::PostTask( |
| 460 BrowserThread::FILE, FROM_HERE, |
| 461 base::Bind(&ProfileShortcutManager::CreateChromeDesktopShortcut, |
| 462 base::Unretained(profile_shortcut_manager_.get()), profile_path, name, |
| 463 ResourceBundle::GetSharedInstance().GetNativeImageNamed( |
| 464 cache.GetDefaultAvatarIconResourceIDAtIndex(icon_index)))); |
| 465 } |
| 457 } | 466 } |
| 458 } | 467 } |
| 459 | 468 |
| 460 // static | 469 // static |
| 461 void ProfileManager::CreateDefaultProfileAsync(const CreateCallback& callback) { | 470 void ProfileManager::CreateDefaultProfileAsync(const CreateCallback& callback) { |
| 462 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 471 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 463 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 472 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 464 | 473 |
| 465 FilePath default_profile_dir = profile_manager->user_data_dir_; | 474 FilePath default_profile_dir = profile_manager->user_data_dir_; |
| 466 // TODO(mirandac): current directory will not always be default in the future | 475 // TODO(mirandac): current directory will not always be default in the future |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 666 last_active->GetPath().BaseName().MaybeAsASCII()); | 675 last_active->GetPath().BaseName().MaybeAsASCII()); |
| 667 } | 676 } |
| 668 } | 677 } |
| 669 #endif // !defined(OS_ANDROID) | 678 #endif // !defined(OS_ANDROID) |
| 670 | 679 |
| 671 void ProfileManager::DoFinalInit(Profile* profile, bool go_off_the_record) { | 680 void ProfileManager::DoFinalInit(Profile* profile, bool go_off_the_record) { |
| 672 DoFinalInitForServices(profile, go_off_the_record); | 681 DoFinalInitForServices(profile, go_off_the_record); |
| 673 InitProfileUserPrefs(profile); | 682 InitProfileUserPrefs(profile); |
| 674 AddProfileToCache(profile); | 683 AddProfileToCache(profile); |
| 675 DoFinalInitLogging(profile); | 684 DoFinalInitLogging(profile); |
| 676 #if defined(OS_WIN) | |
| 677 CreateDesktopShortcut(profile); | |
| 678 #endif | |
| 679 | 685 |
| 680 ProfileMetrics::LogNumberOfProfiles(this, ProfileMetrics::ADD_PROFILE_EVENT); | 686 ProfileMetrics::LogNumberOfProfiles(this, ProfileMetrics::ADD_PROFILE_EVENT); |
| 681 content::NotificationService::current()->Notify( | 687 content::NotificationService::current()->Notify( |
| 682 chrome::NOTIFICATION_PROFILE_ADDED, | 688 chrome::NOTIFICATION_PROFILE_ADDED, |
| 683 content::Source<Profile>(profile), | 689 content::Source<Profile>(profile), |
| 684 content::NotificationService::NoDetails()); | 690 content::NotificationService::NoDetails()); |
| 685 | 691 |
| 686 } | 692 } |
| 687 | 693 |
| 688 void ProfileManager::DoFinalInitForServices(Profile* profile, | 694 void ProfileManager::DoFinalInitForServices(Profile* profile, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 718 return Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS); | 724 return Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS); |
| 719 } | 725 } |
| 720 | 726 |
| 721 Profile* ProfileManager::CreateProfileAsyncHelper(const FilePath& path, | 727 Profile* ProfileManager::CreateProfileAsyncHelper(const FilePath& path, |
| 722 Delegate* delegate) { | 728 Delegate* delegate) { |
| 723 return Profile::CreateProfile(path, | 729 return Profile::CreateProfile(path, |
| 724 delegate, | 730 delegate, |
| 725 Profile::CREATE_MODE_ASYNCHRONOUS); | 731 Profile::CREATE_MODE_ASYNCHRONOUS); |
| 726 } | 732 } |
| 727 | 733 |
| 728 #if defined(OS_WIN) | |
| 729 ProfileShortcutManagerWin* ProfileManager::CreateShortcutManager() { | |
| 730 return new ProfileShortcutManagerWin(); | |
| 731 } | |
| 732 #endif | |
| 733 | |
| 734 void ProfileManager::OnProfileCreated(Profile* profile, | 734 void ProfileManager::OnProfileCreated(Profile* profile, |
| 735 bool success, | 735 bool success, |
| 736 bool is_new_profile) { | 736 bool is_new_profile) { |
| 737 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 737 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 738 | 738 |
| 739 ProfilesInfoMap::iterator iter = profiles_info_.find(profile->GetPath()); | 739 ProfilesInfoMap::iterator iter = profiles_info_.find(profile->GetPath()); |
| 740 DCHECK(iter != profiles_info_.end()); | 740 DCHECK(iter != profiles_info_.end()); |
| 741 ProfileInfo* info = iter->second.get(); | 741 ProfileInfo* info = iter->second.get(); |
| 742 | 742 |
| 743 std::vector<CreateCallback> callbacks; | 743 std::vector<CreateCallback> callbacks; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 824 return false; | 824 return false; |
| 825 } else { | 825 } else { |
| 826 return pair1.first < pair2.first; | 826 return pair1.first < pair2.first; |
| 827 } | 827 } |
| 828 } | 828 } |
| 829 | 829 |
| 830 ProfileInfoCache& ProfileManager::GetProfileInfoCache() { | 830 ProfileInfoCache& ProfileManager::GetProfileInfoCache() { |
| 831 if (!profile_info_cache_.get()) { | 831 if (!profile_info_cache_.get()) { |
| 832 profile_info_cache_.reset(new ProfileInfoCache( | 832 profile_info_cache_.reset(new ProfileInfoCache( |
| 833 g_browser_process->local_state(), user_data_dir_)); | 833 g_browser_process->local_state(), user_data_dir_)); |
| 834 #if defined(OS_WIN) | |
| 835 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
| 836 ProfileShortcutManagerWin* shortcut_manager = CreateShortcutManager(); | |
| 837 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 838 if (dist && dist->CanCreateDesktopShortcuts() && shortcut_manager && | |
| 839 !command_line.HasSwitch(switches::kDisableDesktopShortcuts)) { | |
| 840 profile_shortcut_manager_.reset(shortcut_manager); | |
| 841 profile_info_cache_->AddObserver(profile_shortcut_manager_.get()); | |
| 842 } | |
| 843 #endif | |
| 844 } | 834 } |
| 845 return *profile_info_cache_.get(); | 835 return *profile_info_cache_.get(); |
| 846 } | 836 } |
| 847 | 837 |
| 848 void ProfileManager::AddProfileToCache(Profile* profile) { | 838 void ProfileManager::AddProfileToCache(Profile* profile) { |
| 849 ProfileInfoCache& cache = GetProfileInfoCache(); | 839 ProfileInfoCache& cache = GetProfileInfoCache(); |
| 850 if (profile->GetPath().DirName() != cache.GetUserDataDir()) | 840 if (profile->GetPath().DirName() != cache.GetUserDataDir()) |
| 851 return; | 841 return; |
| 852 | 842 |
| 853 if (cache.GetIndexOfProfileWithPath(profile->GetPath()) != std::string::npos) | 843 if (cache.GetIndexOfProfileWithPath(profile->GetPath()) != std::string::npos) |
| 854 return; | 844 return; |
| 855 | 845 |
| 856 string16 username = UTF8ToUTF16(profile->GetPrefs()->GetString( | 846 string16 username = UTF8ToUTF16(profile->GetPrefs()->GetString( |
| 857 prefs::kGoogleServicesUsername)); | 847 prefs::kGoogleServicesUsername)); |
| 858 | 848 |
| 859 // Profile name and avatar are set by InitProfileUserPrefs and stored in the | 849 // Profile name and avatar are set by InitProfileUserPrefs and stored in the |
| 860 // profile. Use those values to setup the cache entry. | 850 // profile. Use those values to setup the cache entry. |
| 861 string16 profile_name = UTF8ToUTF16(profile->GetPrefs()->GetString( | 851 string16 profile_name = UTF8ToUTF16(profile->GetPrefs()->GetString( |
| 862 prefs::kProfileName)); | 852 prefs::kProfileName)); |
| 863 | 853 |
| 864 size_t icon_index = profile->GetPrefs()->GetInteger( | 854 size_t icon_index = profile->GetPrefs()->GetInteger( |
| 865 prefs::kProfileAvatarIndex); | 855 prefs::kProfileAvatarIndex); |
| 866 | 856 |
| 867 cache.AddProfileToCache(profile->GetPath(), | 857 cache.AddProfileToCache(profile->GetPath(), |
| 868 profile_name, | 858 profile_name, |
| 869 username, | 859 username, |
| 870 icon_index); | 860 icon_index); |
| 871 } | 861 } |
| 872 | 862 |
| 873 #if defined(OS_WIN) | |
| 874 void ProfileManager::CreateDesktopShortcut(Profile* profile) { | |
| 875 // TODO(sail): Disable creating new shortcuts for now. | |
| 876 return; | |
| 877 | |
| 878 // Some distributions and tests cannot create desktop shortcuts, in which case | |
| 879 // profile_shortcut_manager_ will not be set. | |
| 880 if (!profile_shortcut_manager_.get()) | |
| 881 return; | |
| 882 | |
| 883 bool shortcut_created = | |
| 884 profile->GetPrefs()->GetBoolean(prefs::kProfileShortcutCreated); | |
| 885 if (!shortcut_created && GetNumberOfProfiles() > 1) { | |
| 886 profile_shortcut_manager_->AddProfileShortcut(profile->GetPath()); | |
| 887 | |
| 888 // We only ever create the shortcut for a profile once, so set a pref | |
| 889 // reminding us to skip this in the future. | |
| 890 profile->GetPrefs()->SetBoolean(prefs::kProfileShortcutCreated, true); | |
| 891 } | |
| 892 } | |
| 893 #endif | |
| 894 | |
| 895 void ProfileManager::InitProfileUserPrefs(Profile* profile) { | 863 void ProfileManager::InitProfileUserPrefs(Profile* profile) { |
| 896 ProfileInfoCache& cache = GetProfileInfoCache(); | 864 ProfileInfoCache& cache = GetProfileInfoCache(); |
| 897 | 865 |
| 898 if (profile->GetPath().DirName() != cache.GetUserDataDir()) | 866 if (profile->GetPath().DirName() != cache.GetUserDataDir()) |
| 899 return; | 867 return; |
| 900 | 868 |
| 901 // Initialize the user preferences (name and avatar) only if the profile | 869 // Initialize the user preferences (name and avatar) only if the profile |
| 902 // doesn't have default preferenc values for them. | 870 // doesn't have default preferenc values for them. |
| 903 if (HasAnyDefaultUserPrefs(profile)) { | 871 if (HasAnyDefaultUserPrefs(profile)) { |
| 904 size_t profile_cache_index = | 872 size_t profile_cache_index = |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 975 if (ProfileSyncServiceFactory::GetInstance()->HasProfileSyncService( | 943 if (ProfileSyncServiceFactory::GetInstance()->HasProfileSyncService( |
| 976 profile)) { | 944 profile)) { |
| 977 ProfileSyncServiceFactory::GetInstance()->GetForProfile( | 945 ProfileSyncServiceFactory::GetInstance()->GetForProfile( |
| 978 profile)->DisableForUser(); | 946 profile)->DisableForUser(); |
| 979 } | 947 } |
| 980 } | 948 } |
| 981 | 949 |
| 982 QueueProfileDirectoryForDeletion(profile_dir); | 950 QueueProfileDirectoryForDeletion(profile_dir); |
| 983 cache.DeleteProfileFromCache(profile_dir); | 951 cache.DeleteProfileFromCache(profile_dir); |
| 984 | 952 |
| 953 // Delete possible shortcuts for this profile |
| 954 if (profile_shortcut_manager_.get()) |
| 955 profile_shortcut_manager_->DeleteChromeDesktopShortcut(profile_dir); |
| 956 |
| 985 ProfileMetrics::LogNumberOfProfiles(this, | 957 ProfileMetrics::LogNumberOfProfiles(this, |
| 986 ProfileMetrics::DELETE_PROFILE_EVENT); | 958 ProfileMetrics::DELETE_PROFILE_EVENT); |
| 987 } | 959 } |
| 988 | 960 |
| 989 // static | 961 // static |
| 990 bool ProfileManager::IsMultipleProfilesEnabled() { | 962 bool ProfileManager::IsMultipleProfilesEnabled() { |
| 991 #if defined(OS_CHROMEOS) | 963 #if defined(OS_CHROMEOS) |
| 992 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kMultiProfiles)) | 964 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kMultiProfiles)) |
| 993 return false; | 965 return false; |
| 994 #endif | 966 #endif |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1030 void ProfileManager::RunCallbacks(const std::vector<CreateCallback>& callbacks, | 1002 void ProfileManager::RunCallbacks(const std::vector<CreateCallback>& callbacks, |
| 1031 Profile* profile, | 1003 Profile* profile, |
| 1032 Profile::CreateStatus status) { | 1004 Profile::CreateStatus status) { |
| 1033 for (size_t i = 0; i < callbacks.size(); ++i) | 1005 for (size_t i = 0; i < callbacks.size(); ++i) |
| 1034 callbacks[i].Run(profile, status); | 1006 callbacks[i].Run(profile, status); |
| 1035 } | 1007 } |
| 1036 | 1008 |
| 1037 ProfileManager::ProfileInfo::~ProfileInfo() { | 1009 ProfileManager::ProfileInfo::~ProfileInfo() { |
| 1038 ProfileDestroyer::DestroyProfileWhenAppropriate(profile.release()); | 1010 ProfileDestroyer::DestroyProfileWhenAppropriate(profile.release()); |
| 1039 } | 1011 } |
| OLD | NEW |