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

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

Issue 1113333003: Don't create a new profile when cleaning up stale ephemeral profiles. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: fix Created 5 years, 7 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_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
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
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(BrowserThread::FILE, FROM_HERE,
782 base::Bind(&NukeProfileFromDisk, profile_path));
783
784 profile_cache.DeleteProfileFromCache(profile_path);
785 }
786 }
787
760 void ProfileManager::InitProfileUserPrefs(Profile* profile) { 788 void ProfileManager::InitProfileUserPrefs(Profile* profile) {
761 TRACE_EVENT0("browser", "ProfileManager::InitProfileUserPrefs"); 789 TRACE_EVENT0("browser", "ProfileManager::InitProfileUserPrefs");
762 ProfileInfoCache& cache = GetProfileInfoCache(); 790 ProfileInfoCache& cache = GetProfileInfoCache();
763 791
764 if (profile->GetPath().DirName() != cache.GetUserDataDir()) 792 if (profile->GetPath().DirName() != cache.GetUserDataDir())
765 return; 793 return;
766 794
767 size_t avatar_index; 795 size_t avatar_index;
768 std::string profile_name; 796 std::string profile_name;
769 std::string supervised_user_id; 797 std::string supervised_user_id;
(...skipping 565 matching lines...) Expand 10 before | Expand all | Expand 10 after
1335 } 1363 }
1336 1364
1337 #if !defined(OS_ANDROID) && !defined(OS_IOS) 1365 #if !defined(OS_ANDROID) && !defined(OS_IOS)
1338 void ProfileManager::UpdateLastUser(Profile* last_active) { 1366 void ProfileManager::UpdateLastUser(Profile* last_active) {
1339 PrefService* local_state = g_browser_process->local_state(); 1367 PrefService* local_state = g_browser_process->local_state();
1340 DCHECK(local_state); 1368 DCHECK(local_state);
1341 // Only keep track of profiles that we are managing; tests may create others. 1369 // Only keep track of profiles that we are managing; tests may create others.
1342 if (profiles_info_.find(last_active->GetPath()) != profiles_info_.end()) { 1370 if (profiles_info_.find(last_active->GetPath()) != profiles_info_.end()) {
1343 std::string profile_path_base = 1371 std::string profile_path_base =
1344 last_active->GetPath().BaseName().MaybeAsASCII(); 1372 last_active->GetPath().BaseName().MaybeAsASCII();
1345 if (profile_path_base != local_state->GetString(prefs::kProfileLastUsed)) 1373 if (profile_path_base != GetLastUsedProfileName())
1346 local_state->SetString(prefs::kProfileLastUsed, profile_path_base); 1374 local_state->SetString(prefs::kProfileLastUsed, profile_path_base);
1347 1375
1348 ProfileInfoCache& cache = GetProfileInfoCache(); 1376 ProfileInfoCache& cache = GetProfileInfoCache();
1349 size_t profile_index = 1377 size_t profile_index =
1350 cache.GetIndexOfProfileWithPath(last_active->GetPath()); 1378 cache.GetIndexOfProfileWithPath(last_active->GetPath());
1351 if (profile_index != std::string::npos) 1379 if (profile_index != std::string::npos)
1352 cache.SetProfileActiveTimeAtIndex(profile_index); 1380 cache.SetProfileActiveTimeAtIndex(profile_index);
1353 } 1381 }
1354 } 1382 }
1355 1383
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
1427 } 1455 }
1428 1456
1429 FinishDeletingProfile(profile_to_delete_path, new_active_profile_path); 1457 FinishDeletingProfile(profile_to_delete_path, new_active_profile_path);
1430 if (!original_callback.is_null()) 1458 if (!original_callback.is_null())
1431 original_callback.Run(loaded_profile, status); 1459 original_callback.Run(loaded_profile, status);
1432 } 1460 }
1433 1461
1434 ProfileManagerWithoutInit::ProfileManagerWithoutInit( 1462 ProfileManagerWithoutInit::ProfileManagerWithoutInit(
1435 const base::FilePath& user_data_dir) : ProfileManager(user_data_dir) { 1463 const base::FilePath& user_data_dir) : ProfileManager(user_data_dir) {
1436 } 1464 }
OLDNEW
« no previous file with comments | « chrome/browser/profiles/profile_manager.h ('k') | chrome/browser/profiles/profile_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698