| 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_manager.h" | 5 #include "chrome/browser/profiles/profile_manager.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 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 487 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 return profile->IsGuestSession() ? profile->GetOffTheRecordProfile() : | 498 return profile->IsGuestSession() ? profile->GetOffTheRecordProfile() : |
| 499 profile; | 499 profile; |
| 500 } | 500 } |
| 501 #endif | 501 #endif |
| 502 | 502 |
| 503 return GetProfile(GetLastUsedProfileDir(user_data_dir)); | 503 return GetProfile(GetLastUsedProfileDir(user_data_dir)); |
| 504 } | 504 } |
| 505 | 505 |
| 506 base::FilePath ProfileManager::GetLastUsedProfileDir( | 506 base::FilePath ProfileManager::GetLastUsedProfileDir( |
| 507 const base::FilePath& user_data_dir) { | 507 const base::FilePath& user_data_dir) { |
| 508 base::FilePath last_used_profile_dir(user_data_dir); | 508 return user_data_dir.AppendASCII(GetLastUsedProfileName()); |
| 509 } |
| 510 |
| 511 std::string ProfileManager::GetLastUsedProfileName() { |
| 509 PrefService* local_state = g_browser_process->local_state(); | 512 PrefService* local_state = g_browser_process->local_state(); |
| 510 DCHECK(local_state); | 513 DCHECK(local_state); |
| 514 const std::string last_used_profile_name = |
| 515 local_state->GetString(prefs::kProfileLastUsed); |
| 516 if (!last_used_profile_name.empty()) |
| 517 return last_used_profile_name; |
| 511 | 518 |
| 512 if (local_state->HasPrefPath(prefs::kProfileLastUsed)) { | 519 return chrome::kInitialProfile; |
| 513 return last_used_profile_dir.AppendASCII( | |
| 514 local_state->GetString(prefs::kProfileLastUsed)); | |
| 515 } | |
| 516 | |
| 517 return last_used_profile_dir.AppendASCII(chrome::kInitialProfile); | |
| 518 } | 520 } |
| 519 | 521 |
| 520 std::vector<Profile*> ProfileManager::GetLastOpenedProfiles( | 522 std::vector<Profile*> ProfileManager::GetLastOpenedProfiles( |
| 521 const base::FilePath& user_data_dir) { | 523 const base::FilePath& user_data_dir) { |
| 522 PrefService* local_state = g_browser_process->local_state(); | 524 PrefService* local_state = g_browser_process->local_state(); |
| 523 DCHECK(local_state); | 525 DCHECK(local_state); |
| 524 | 526 |
| 525 std::vector<Profile*> to_return; | 527 std::vector<Profile*> to_return; |
| 526 if (local_state->HasPrefPath(prefs::kProfilesLastActive) && | 528 if (local_state->HasPrefPath(prefs::kProfilesLastActive) && |
| 527 local_state->GetList(prefs::kProfilesLastActive)) { | 529 local_state->GetList(prefs::kProfilesLastActive)) { |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 695 | 697 |
| 696 ProfileMetrics::LogProfileAddNewUser( | 698 ProfileMetrics::LogProfileAddNewUser( |
| 697 ProfileMetrics::ADD_NEW_USER_LAST_DELETED); | 699 ProfileMetrics::ADD_NEW_USER_LAST_DELETED); |
| 698 return; | 700 return; |
| 699 } | 701 } |
| 700 | 702 |
| 701 #if defined(OS_MACOSX) | 703 #if defined(OS_MACOSX) |
| 702 // On the Mac, the browser process is not killed when all browser windows are | 704 // On the Mac, the browser process is not killed when all browser windows are |
| 703 // closed, so just in case we are deleting the active profile, and no other | 705 // closed, so just in case we are deleting the active profile, and no other |
| 704 // profile has been loaded, we must pre-load a next one. | 706 // profile has been loaded, we must pre-load a next one. |
| 705 PrefService* local_state = g_browser_process->local_state(); | 707 const base::FilePath last_used_profile = |
| 706 DCHECK(local_state); | 708 GetLastUsedProfileDir(user_data_dir_); |
| 707 const std::string last_used_profile = | 709 if (last_used_profile == profile_dir || |
| 708 local_state->GetString(prefs::kProfileLastUsed); | 710 last_used_profile == GetGuestProfilePath()) { |
| 709 if (last_used_profile == profile_dir.BaseName().MaybeAsASCII() || | |
| 710 last_used_profile == GetGuestProfilePath().BaseName().MaybeAsASCII()) { | |
| 711 CreateProfileAsync(last_non_supervised_profile_path, | 711 CreateProfileAsync(last_non_supervised_profile_path, |
| 712 base::Bind(&ProfileManager::OnNewActiveProfileLoaded, | 712 base::Bind(&ProfileManager::OnNewActiveProfileLoaded, |
| 713 base::Unretained(this), | 713 base::Unretained(this), |
| 714 profile_dir, | 714 profile_dir, |
| 715 last_non_supervised_profile_path, | 715 last_non_supervised_profile_path, |
| 716 callback), | 716 callback), |
| 717 base::string16(), | 717 base::string16(), |
| 718 base::string16(), | 718 base::string16(), |
| 719 std::string()); | 719 std::string()); |
| 720 return; | 720 return; |
| 721 } | 721 } |
| 722 #endif // defined(OS_MACOSX) | 722 #endif // defined(OS_MACOSX) |
| 723 | 723 |
| 724 FinishDeletingProfile(profile_dir, last_non_supervised_profile_path); | 724 FinishDeletingProfile(profile_dir, last_non_supervised_profile_path); |
| 725 } | 725 } |
| 726 | 726 |
| 727 // static | |
| 728 void ProfileManager::CleanUpStaleProfiles( | |
| 729 const std::vector<base::FilePath>& profile_paths) { | |
| 730 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
| 731 | |
| 732 for (std::vector<base::FilePath>::const_iterator it = profile_paths.begin(); | |
| 733 it != profile_paths.end(); ++it) { | |
| 734 NukeProfileFromDisk(*it); | |
| 735 } | |
| 736 } | |
| 737 | |
| 738 void ProfileManager::AutoloadProfiles() { | 727 void ProfileManager::AutoloadProfiles() { |
| 739 // If running in the background is disabled for the browser, do not autoload | 728 // If running in the background is disabled for the browser, do not autoload |
| 740 // any profiles. | 729 // any profiles. |
| 741 PrefService* local_state = g_browser_process->local_state(); | 730 PrefService* local_state = g_browser_process->local_state(); |
| 742 DCHECK(local_state); | 731 DCHECK(local_state); |
| 743 if (!local_state->HasPrefPath(prefs::kBackgroundModeEnabled) || | 732 if (!local_state->HasPrefPath(prefs::kBackgroundModeEnabled) || |
| 744 !local_state->GetBoolean(prefs::kBackgroundModeEnabled)) { | 733 !local_state->GetBoolean(prefs::kBackgroundModeEnabled)) { |
| 745 return; | 734 return; |
| 746 } | 735 } |
| 747 | 736 |
| 748 ProfileInfoCache& cache = GetProfileInfoCache(); | 737 ProfileInfoCache& cache = GetProfileInfoCache(); |
| 749 size_t number_of_profiles = cache.GetNumberOfProfiles(); | 738 size_t number_of_profiles = cache.GetNumberOfProfiles(); |
| 750 for (size_t p = 0; p < number_of_profiles; ++p) { | 739 for (size_t p = 0; p < number_of_profiles; ++p) { |
| 751 if (cache.GetBackgroundStatusOfProfileAtIndex(p)) { | 740 if (cache.GetBackgroundStatusOfProfileAtIndex(p)) { |
| 752 // If status is true, that profile is running background apps. By calling | 741 // If status is true, that profile is running background apps. By calling |
| 753 // GetProfile, we automatically cause the profile to be loaded which will | 742 // GetProfile, we automatically cause the profile to be loaded which will |
| 754 // register it with the BackgroundModeManager. | 743 // register it with the BackgroundModeManager. |
| 755 GetProfile(cache.GetPathOfProfileAtIndex(p)); | 744 GetProfile(cache.GetPathOfProfileAtIndex(p)); |
| 756 } | 745 } |
| 757 } | 746 } |
| 758 } | 747 } |
| 759 | 748 |
| 749 void ProfileManager::CleanUpEphemeralProfiles() { |
| 750 const std::string last_used_profile = GetLastUsedProfileName(); |
| 751 |
| 752 bool last_active_profile_deleted = false; |
| 753 base::FilePath new_profile_path; |
| 754 std::vector<base::FilePath> profiles_to_delete; |
| 755 ProfileInfoCache& profile_cache = GetProfileInfoCache(); |
| 756 size_t profiles_count = profile_cache.GetNumberOfProfiles(); |
| 757 for (size_t i = 0; i < profiles_count; ++i) { |
| 758 base::FilePath profile_path = profile_cache.GetPathOfProfileAtIndex(i); |
| 759 if (profile_cache.ProfileIsEphemeralAtIndex(i)) { |
| 760 profiles_to_delete.push_back(profile_path); |
| 761 if (profile_path.BaseName().MaybeAsASCII() == last_used_profile) |
| 762 last_active_profile_deleted = true; |
| 763 } else if (new_profile_path.empty()) { |
| 764 new_profile_path = profile_path; |
| 765 } |
| 766 } |
| 767 |
| 768 // If the last active profile was ephemeral, set a new one. |
| 769 if (last_active_profile_deleted) { |
| 770 if (new_profile_path.empty()) |
| 771 new_profile_path = GenerateNextProfileDirectoryPath(); |
| 772 |
| 773 PrefService* local_state = g_browser_process->local_state(); |
| 774 local_state->SetString(prefs::kProfileLastUsed, |
| 775 new_profile_path.BaseName().MaybeAsASCII()); |
| 776 } |
| 777 |
| 778 // This uses a separate loop, because deleting the profile from the |
| 779 // ProfileInfoCache will modify indices. |
| 780 for (const base::FilePath& profile_path : profiles_to_delete) { |
| 781 BrowserThread::PostTask( |
| 782 BrowserThread::FILE, FROM_HERE, |
| 783 base::Bind(&NukeProfileFromDisk, profile_path)); |
| 784 |
| 785 profile_cache.DeleteProfileFromCache(profile_path); |
| 786 } |
| 787 } |
| 788 |
| 760 void ProfileManager::InitProfileUserPrefs(Profile* profile) { | 789 void ProfileManager::InitProfileUserPrefs(Profile* profile) { |
| 761 TRACE_EVENT0("browser", "ProfileManager::InitProfileUserPrefs"); | 790 TRACE_EVENT0("browser", "ProfileManager::InitProfileUserPrefs"); |
| 762 ProfileInfoCache& cache = GetProfileInfoCache(); | 791 ProfileInfoCache& cache = GetProfileInfoCache(); |
| 763 | 792 |
| 764 if (profile->GetPath().DirName() != cache.GetUserDataDir()) | 793 if (profile->GetPath().DirName() != cache.GetUserDataDir()) |
| 765 return; | 794 return; |
| 766 | 795 |
| 767 size_t avatar_index; | 796 size_t avatar_index; |
| 768 std::string profile_name; | 797 std::string profile_name; |
| 769 std::string supervised_user_id; | 798 std::string supervised_user_id; |
| (...skipping 560 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1330 } | 1359 } |
| 1331 | 1360 |
| 1332 #if !defined(OS_ANDROID) && !defined(OS_IOS) | 1361 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
| 1333 void ProfileManager::UpdateLastUser(Profile* last_active) { | 1362 void ProfileManager::UpdateLastUser(Profile* last_active) { |
| 1334 PrefService* local_state = g_browser_process->local_state(); | 1363 PrefService* local_state = g_browser_process->local_state(); |
| 1335 DCHECK(local_state); | 1364 DCHECK(local_state); |
| 1336 // Only keep track of profiles that we are managing; tests may create others. | 1365 // Only keep track of profiles that we are managing; tests may create others. |
| 1337 if (profiles_info_.find(last_active->GetPath()) != profiles_info_.end()) { | 1366 if (profiles_info_.find(last_active->GetPath()) != profiles_info_.end()) { |
| 1338 std::string profile_path_base = | 1367 std::string profile_path_base = |
| 1339 last_active->GetPath().BaseName().MaybeAsASCII(); | 1368 last_active->GetPath().BaseName().MaybeAsASCII(); |
| 1340 if (profile_path_base != local_state->GetString(prefs::kProfileLastUsed)) | 1369 if (profile_path_base != GetLastUsedProfileName()) |
| 1341 local_state->SetString(prefs::kProfileLastUsed, profile_path_base); | 1370 local_state->SetString(prefs::kProfileLastUsed, profile_path_base); |
| 1342 | 1371 |
| 1343 ProfileInfoCache& cache = GetProfileInfoCache(); | 1372 ProfileInfoCache& cache = GetProfileInfoCache(); |
| 1344 size_t profile_index = | 1373 size_t profile_index = |
| 1345 cache.GetIndexOfProfileWithPath(last_active->GetPath()); | 1374 cache.GetIndexOfProfileWithPath(last_active->GetPath()); |
| 1346 if (profile_index != std::string::npos) | 1375 if (profile_index != std::string::npos) |
| 1347 cache.SetProfileActiveTimeAtIndex(profile_index); | 1376 cache.SetProfileActiveTimeAtIndex(profile_index); |
| 1348 } | 1377 } |
| 1349 } | 1378 } |
| 1350 | 1379 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1422 } | 1451 } |
| 1423 | 1452 |
| 1424 FinishDeletingProfile(profile_to_delete_path, new_active_profile_path); | 1453 FinishDeletingProfile(profile_to_delete_path, new_active_profile_path); |
| 1425 if (!original_callback.is_null()) | 1454 if (!original_callback.is_null()) |
| 1426 original_callback.Run(loaded_profile, status); | 1455 original_callback.Run(loaded_profile, status); |
| 1427 } | 1456 } |
| 1428 | 1457 |
| 1429 ProfileManagerWithoutInit::ProfileManagerWithoutInit( | 1458 ProfileManagerWithoutInit::ProfileManagerWithoutInit( |
| 1430 const base::FilePath& user_data_dir) : ProfileManager(user_data_dir) { | 1459 const base::FilePath& user_data_dir) : ProfileManager(user_data_dir) { |
| 1431 } | 1460 } |
| OLD | NEW |