| 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" |
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/metrics/field_trial.h" | 13 #include "base/metrics/field_trial.h" |
| 14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 15 #include "base/string_number_conversions.h" | 15 #include "base/string_number_conversions.h" |
| 16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
| 18 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 19 #include "chrome/browser/content_settings/host_content_settings_map.h" | 19 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 20 #include "chrome/browser/extensions/default_apps_trial.h" | 20 #include "chrome/browser/extensions/default_apps_trial.h" |
| 21 #include "chrome/browser/extensions/extension_service.h" | 21 #include "chrome/browser/extensions/extension_service.h" |
| 22 #include "chrome/browser/extensions/extension_system.h" | 22 #include "chrome/browser/extensions/extension_system.h" |
| 23 #include "chrome/browser/managed_mode.h" | 23 #include "chrome/browser/managed_mode.h" |
| 24 #include "chrome/browser/prefs/pref_service.h" | 24 #include "chrome/browser/prefs/pref_service.h" |
| 25 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 25 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 26 #include "chrome/browser/profiles/profile_destroyer.h" | 26 #include "chrome/browser/profiles/profile_destroyer.h" |
| 27 #include "chrome/browser/profiles/profile_info_cache.h" | 27 #include "chrome/browser/profiles/profile_info_cache.h" |
| 28 #include "chrome/browser/profiles/profile_metrics.h" | 28 #include "chrome/browser/profiles/profile_metrics.h" |
| 29 #include "chrome/browser/profiles/profile_shortcut_manager.h" |
| 29 #include "chrome/browser/sessions/session_service_factory.h" | 30 #include "chrome/browser/sessions/session_service_factory.h" |
| 30 #include "chrome/browser/sync/profile_sync_service.h" | 31 #include "chrome/browser/sync/profile_sync_service.h" |
| 31 #include "chrome/browser/sync/profile_sync_service_factory.h" | 32 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 32 #include "chrome/browser/ui/browser.h" | 33 #include "chrome/browser/ui/browser.h" |
| 33 #include "chrome/browser/ui/browser_finder.h" | 34 #include "chrome/browser/ui/browser_finder.h" |
| 34 #include "chrome/browser/ui/browser_list.h" | 35 #include "chrome/browser/ui/browser_list.h" |
| 35 #include "chrome/browser/ui/browser_window.h" | 36 #include "chrome/browser/ui/browser_window.h" |
| 36 #include "chrome/browser/ui/startup/startup_browser_creator.h" | 37 #include "chrome/browser/ui/startup/startup_browser_creator.h" |
| 37 #include "chrome/browser/ui/webui/sync_promo/sync_promo_ui.h" | 38 #include "chrome/browser/ui/webui/sync_promo/sync_promo_ui.h" |
| 38 #include "chrome/common/chrome_constants.h" | 39 #include "chrome/common/chrome_constants.h" |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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()); |
| 269 } | 270 } |
| 270 | 271 |
| 271 ProfileManager::~ProfileManager() { | 272 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 } | 273 } |
| 277 | 274 |
| 278 FilePath ProfileManager::GetDefaultProfileDir( | 275 FilePath ProfileManager::GetDefaultProfileDir( |
| 279 const FilePath& user_data_dir) { | 276 const FilePath& user_data_dir) { |
| 280 FilePath default_profile_dir(user_data_dir); | 277 FilePath default_profile_dir(user_data_dir); |
| 281 default_profile_dir = | 278 default_profile_dir = |
| 282 default_profile_dir.AppendASCII(chrome::kInitialProfile); | 279 default_profile_dir.AppendASCII(chrome::kInitialProfile); |
| 283 return default_profile_dir; | 280 return default_profile_dir; |
| 284 } | 281 } |
| 285 | 282 |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 RegisterProfile(CreateProfileAsyncHelper(profile_path, this), false); | 444 RegisterProfile(CreateProfileAsyncHelper(profile_path, this), false); |
| 448 ProfileInfoCache& cache = GetProfileInfoCache(); | 445 ProfileInfoCache& cache = GetProfileInfoCache(); |
| 449 // Get the icon index from the user's icon url | 446 // Get the icon index from the user's icon url |
| 450 size_t icon_index; | 447 size_t icon_index; |
| 451 std::string icon_url_std = UTF16ToASCII(icon_url); | 448 std::string icon_url_std = UTF16ToASCII(icon_url); |
| 452 if (cache.IsDefaultAvatarIconUrl(icon_url_std, &icon_index)) { | 449 if (cache.IsDefaultAvatarIconUrl(icon_url_std, &icon_index)) { |
| 453 // add profile to cache with user selected name and avatar | 450 // add profile to cache with user selected name and avatar |
| 454 cache.AddProfileToCache(profile_path, name, string16(), icon_index); | 451 cache.AddProfileToCache(profile_path, name, string16(), icon_index); |
| 455 } | 452 } |
| 456 info->callbacks.push_back(callback); | 453 info->callbacks.push_back(callback); |
| 454 if (!name.empty() && !icon_url.empty()) { |
| 455 BrowserThread::PostTask( |
| 456 BrowserThread::FILE, FROM_HERE, |
| 457 base::Bind(&ProfileShortcutManager::CreateChromeDesktopShortcut, |
| 458 profile_path, name, |
| 459 ResourceBundle::GetSharedInstance().GetNativeImageNamed( |
| 460 cache.GetDefaultAvatarIconResourceIDAtIndex(icon_index)))); |
| 461 } |
| 457 } | 462 } |
| 458 } | 463 } |
| 459 | 464 |
| 460 // static | 465 // static |
| 461 void ProfileManager::CreateDefaultProfileAsync(const CreateCallback& callback) { | 466 void ProfileManager::CreateDefaultProfileAsync(const CreateCallback& callback) { |
| 462 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 467 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 463 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 468 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 464 | 469 |
| 465 FilePath default_profile_dir = profile_manager->user_data_dir_; | 470 FilePath default_profile_dir = profile_manager->user_data_dir_; |
| 466 // TODO(mirandac): current directory will not always be default in the future | 471 // 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()); | 671 last_active->GetPath().BaseName().MaybeAsASCII()); |
| 667 } | 672 } |
| 668 } | 673 } |
| 669 #endif // !defined(OS_ANDROID) | 674 #endif // !defined(OS_ANDROID) |
| 670 | 675 |
| 671 void ProfileManager::DoFinalInit(Profile* profile, bool go_off_the_record) { | 676 void ProfileManager::DoFinalInit(Profile* profile, bool go_off_the_record) { |
| 672 DoFinalInitForServices(profile, go_off_the_record); | 677 DoFinalInitForServices(profile, go_off_the_record); |
| 673 InitProfileUserPrefs(profile); | 678 InitProfileUserPrefs(profile); |
| 674 AddProfileToCache(profile); | 679 AddProfileToCache(profile); |
| 675 DoFinalInitLogging(profile); | 680 DoFinalInitLogging(profile); |
| 676 #if defined(OS_WIN) | |
| 677 CreateDesktopShortcut(profile); | |
| 678 #endif | |
| 679 | 681 |
| 680 ProfileMetrics::LogNumberOfProfiles(this, ProfileMetrics::ADD_PROFILE_EVENT); | 682 ProfileMetrics::LogNumberOfProfiles(this, ProfileMetrics::ADD_PROFILE_EVENT); |
| 681 content::NotificationService::current()->Notify( | 683 content::NotificationService::current()->Notify( |
| 682 chrome::NOTIFICATION_PROFILE_ADDED, | 684 chrome::NOTIFICATION_PROFILE_ADDED, |
| 683 content::Source<Profile>(profile), | 685 content::Source<Profile>(profile), |
| 684 content::NotificationService::NoDetails()); | 686 content::NotificationService::NoDetails()); |
| 685 | 687 |
| 686 } | 688 } |
| 687 | 689 |
| 688 void ProfileManager::DoFinalInitForServices(Profile* profile, | 690 void ProfileManager::DoFinalInitForServices(Profile* profile, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 718 return Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS); | 720 return Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS); |
| 719 } | 721 } |
| 720 | 722 |
| 721 Profile* ProfileManager::CreateProfileAsyncHelper(const FilePath& path, | 723 Profile* ProfileManager::CreateProfileAsyncHelper(const FilePath& path, |
| 722 Delegate* delegate) { | 724 Delegate* delegate) { |
| 723 return Profile::CreateProfile(path, | 725 return Profile::CreateProfile(path, |
| 724 delegate, | 726 delegate, |
| 725 Profile::CREATE_MODE_ASYNCHRONOUS); | 727 Profile::CREATE_MODE_ASYNCHRONOUS); |
| 726 } | 728 } |
| 727 | 729 |
| 728 #if defined(OS_WIN) | |
| 729 ProfileShortcutManagerWin* ProfileManager::CreateShortcutManager() { | |
| 730 return new ProfileShortcutManagerWin(); | |
| 731 } | |
| 732 #endif | |
| 733 | |
| 734 void ProfileManager::OnProfileCreated(Profile* profile, | 730 void ProfileManager::OnProfileCreated(Profile* profile, |
| 735 bool success, | 731 bool success, |
| 736 bool is_new_profile) { | 732 bool is_new_profile) { |
| 737 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 733 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 738 | 734 |
| 739 ProfilesInfoMap::iterator iter = profiles_info_.find(profile->GetPath()); | 735 ProfilesInfoMap::iterator iter = profiles_info_.find(profile->GetPath()); |
| 740 DCHECK(iter != profiles_info_.end()); | 736 DCHECK(iter != profiles_info_.end()); |
| 741 ProfileInfo* info = iter->second.get(); | 737 ProfileInfo* info = iter->second.get(); |
| 742 | 738 |
| 743 std::vector<CreateCallback> callbacks; | 739 std::vector<CreateCallback> callbacks; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 824 return false; | 820 return false; |
| 825 } else { | 821 } else { |
| 826 return pair1.first < pair2.first; | 822 return pair1.first < pair2.first; |
| 827 } | 823 } |
| 828 } | 824 } |
| 829 | 825 |
| 830 ProfileInfoCache& ProfileManager::GetProfileInfoCache() { | 826 ProfileInfoCache& ProfileManager::GetProfileInfoCache() { |
| 831 if (!profile_info_cache_.get()) { | 827 if (!profile_info_cache_.get()) { |
| 832 profile_info_cache_.reset(new ProfileInfoCache( | 828 profile_info_cache_.reset(new ProfileInfoCache( |
| 833 g_browser_process->local_state(), user_data_dir_)); | 829 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 } | 830 } |
| 845 return *profile_info_cache_.get(); | 831 return *profile_info_cache_.get(); |
| 846 } | 832 } |
| 847 | 833 |
| 848 void ProfileManager::AddProfileToCache(Profile* profile) { | 834 void ProfileManager::AddProfileToCache(Profile* profile) { |
| 849 ProfileInfoCache& cache = GetProfileInfoCache(); | 835 ProfileInfoCache& cache = GetProfileInfoCache(); |
| 850 if (profile->GetPath().DirName() != cache.GetUserDataDir()) | 836 if (profile->GetPath().DirName() != cache.GetUserDataDir()) |
| 851 return; | 837 return; |
| 852 | 838 |
| 853 if (cache.GetIndexOfProfileWithPath(profile->GetPath()) != std::string::npos) | 839 if (cache.GetIndexOfProfileWithPath(profile->GetPath()) != std::string::npos) |
| 854 return; | 840 return; |
| 855 | 841 |
| 856 string16 username = UTF8ToUTF16(profile->GetPrefs()->GetString( | 842 string16 username = UTF8ToUTF16(profile->GetPrefs()->GetString( |
| 857 prefs::kGoogleServicesUsername)); | 843 prefs::kGoogleServicesUsername)); |
| 858 | 844 |
| 859 // Profile name and avatar are set by InitProfileUserPrefs and stored in the | 845 // Profile name and avatar are set by InitProfileUserPrefs and stored in the |
| 860 // profile. Use those values to setup the cache entry. | 846 // profile. Use those values to setup the cache entry. |
| 861 string16 profile_name = UTF8ToUTF16(profile->GetPrefs()->GetString( | 847 string16 profile_name = UTF8ToUTF16(profile->GetPrefs()->GetString( |
| 862 prefs::kProfileName)); | 848 prefs::kProfileName)); |
| 863 | 849 |
| 864 size_t icon_index = profile->GetPrefs()->GetInteger( | 850 size_t icon_index = profile->GetPrefs()->GetInteger( |
| 865 prefs::kProfileAvatarIndex); | 851 prefs::kProfileAvatarIndex); |
| 866 | 852 |
| 867 cache.AddProfileToCache(profile->GetPath(), | 853 cache.AddProfileToCache(profile->GetPath(), |
| 868 profile_name, | 854 profile_name, |
| 869 username, | 855 username, |
| 870 icon_index); | 856 icon_index); |
| 871 } | 857 } |
| 872 | 858 |
| 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) { | 859 void ProfileManager::InitProfileUserPrefs(Profile* profile) { |
| 896 ProfileInfoCache& cache = GetProfileInfoCache(); | 860 ProfileInfoCache& cache = GetProfileInfoCache(); |
| 897 | 861 |
| 898 if (profile->GetPath().DirName() != cache.GetUserDataDir()) | 862 if (profile->GetPath().DirName() != cache.GetUserDataDir()) |
| 899 return; | 863 return; |
| 900 | 864 |
| 901 // Initialize the user preferences (name and avatar) only if the profile | 865 // Initialize the user preferences (name and avatar) only if the profile |
| 902 // doesn't have default preferenc values for them. | 866 // doesn't have default preferenc values for them. |
| 903 if (HasAnyDefaultUserPrefs(profile)) { | 867 if (HasAnyDefaultUserPrefs(profile)) { |
| 904 size_t profile_cache_index = | 868 size_t profile_cache_index = |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 975 if (ProfileSyncServiceFactory::GetInstance()->HasProfileSyncService( | 939 if (ProfileSyncServiceFactory::GetInstance()->HasProfileSyncService( |
| 976 profile)) { | 940 profile)) { |
| 977 ProfileSyncServiceFactory::GetInstance()->GetForProfile( | 941 ProfileSyncServiceFactory::GetInstance()->GetForProfile( |
| 978 profile)->DisableForUser(); | 942 profile)->DisableForUser(); |
| 979 } | 943 } |
| 980 } | 944 } |
| 981 | 945 |
| 982 QueueProfileDirectoryForDeletion(profile_dir); | 946 QueueProfileDirectoryForDeletion(profile_dir); |
| 983 cache.DeleteProfileFromCache(profile_dir); | 947 cache.DeleteProfileFromCache(profile_dir); |
| 984 | 948 |
| 949 // Delete possible shortcuts for this profile |
| 950 ProfileShortcutManager::DeleteChromeDesktopShortcut(profile_dir); |
| 951 |
| 985 ProfileMetrics::LogNumberOfProfiles(this, | 952 ProfileMetrics::LogNumberOfProfiles(this, |
| 986 ProfileMetrics::DELETE_PROFILE_EVENT); | 953 ProfileMetrics::DELETE_PROFILE_EVENT); |
| 987 } | 954 } |
| 988 | 955 |
| 989 // static | 956 // static |
| 990 bool ProfileManager::IsMultipleProfilesEnabled() { | 957 bool ProfileManager::IsMultipleProfilesEnabled() { |
| 991 #if defined(OS_CHROMEOS) | 958 #if defined(OS_CHROMEOS) |
| 992 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kMultiProfiles)) | 959 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kMultiProfiles)) |
| 993 return false; | 960 return false; |
| 994 #endif | 961 #endif |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1030 void ProfileManager::RunCallbacks(const std::vector<CreateCallback>& callbacks, | 997 void ProfileManager::RunCallbacks(const std::vector<CreateCallback>& callbacks, |
| 1031 Profile* profile, | 998 Profile* profile, |
| 1032 Profile::CreateStatus status) { | 999 Profile::CreateStatus status) { |
| 1033 for (size_t i = 0; i < callbacks.size(); ++i) | 1000 for (size_t i = 0; i < callbacks.size(); ++i) |
| 1034 callbacks[i].Run(profile, status); | 1001 callbacks[i].Run(profile, status); |
| 1035 } | 1002 } |
| 1036 | 1003 |
| 1037 ProfileManager::ProfileInfo::~ProfileInfo() { | 1004 ProfileManager::ProfileInfo::~ProfileInfo() { |
| 1038 ProfileDestroyer::DestroyProfileWhenAppropriate(profile.release()); | 1005 ProfileDestroyer::DestroyProfileWhenAppropriate(profile.release()); |
| 1039 } | 1006 } |
| OLD | NEW |