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 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
190 | 190 |
191 // Physically remove deleted profile directories from disk. | 191 // Physically remove deleted profile directories from disk. |
192 void NukeProfileFromDisk(const base::FilePath& profile_path) { | 192 void NukeProfileFromDisk(const base::FilePath& profile_path) { |
193 // Delete both the profile directory and its corresponding cache. | 193 // Delete both the profile directory and its corresponding cache. |
194 base::FilePath cache_path; | 194 base::FilePath cache_path; |
195 chrome::GetUserCacheDirectory(profile_path, &cache_path); | 195 chrome::GetUserCacheDirectory(profile_path, &cache_path); |
196 base::DeleteFile(profile_path, true); | 196 base::DeleteFile(profile_path, true); |
197 base::DeleteFile(cache_path, true); | 197 base::DeleteFile(cache_path, true); |
198 } | 198 } |
199 | 199 |
200 std::string GetLastUsedProfileName() { | |
201 PrefService* local_state = g_browser_process->local_state(); | |
Mike Lerman
2015/05/11 15:06:22
nit: can we DCHECK(local_state)?
Bernhard Bauer
2015/05/11 16:52:08
Done.
| |
202 std::string last_used_profile_name = | |
Mike Lerman
2015/05/11 15:06:22
tiny: can this be a const std::string&?
Bernhard Bauer
2015/05/11 16:52:08
Done.
| |
203 local_state->GetString(prefs::kProfileLastUsed); | |
204 if (!last_used_profile_name.empty()) | |
205 return last_used_profile_name; | |
206 | |
207 return chrome::kInitialProfile; | |
208 } | |
209 | |
200 #if defined(OS_CHROMEOS) | 210 #if defined(OS_CHROMEOS) |
201 void CheckCryptohomeIsMounted(chromeos::DBusMethodCallStatus call_status, | 211 void CheckCryptohomeIsMounted(chromeos::DBusMethodCallStatus call_status, |
202 bool is_mounted) { | 212 bool is_mounted) { |
203 if (call_status != chromeos::DBUS_METHOD_CALL_SUCCESS) { | 213 if (call_status != chromeos::DBUS_METHOD_CALL_SUCCESS) { |
204 LOG(ERROR) << "IsMounted call failed."; | 214 LOG(ERROR) << "IsMounted call failed."; |
205 return; | 215 return; |
206 } | 216 } |
207 if (!is_mounted) | 217 if (!is_mounted) |
208 LOG(ERROR) << "Cryptohome is not mounted."; | 218 LOG(ERROR) << "Cryptohome is not mounted."; |
209 } | 219 } |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
496 base::FilePath profile_path(user_data_dir); | 506 base::FilePath profile_path(user_data_dir); |
497 Profile* profile = GetProfile(profile_path.Append(profile_dir)); | 507 Profile* profile = GetProfile(profile_path.Append(profile_dir)); |
498 return profile->IsGuestSession() ? profile->GetOffTheRecordProfile() : | 508 return profile->IsGuestSession() ? profile->GetOffTheRecordProfile() : |
499 profile; | 509 profile; |
500 } | 510 } |
501 #endif | 511 #endif |
502 | 512 |
503 return GetProfile(GetLastUsedProfileDir(user_data_dir)); | 513 return GetProfile(GetLastUsedProfileDir(user_data_dir)); |
504 } | 514 } |
505 | 515 |
506 base::FilePath ProfileManager::GetLastUsedProfileDir( | 516 base::FilePath ProfileManager::GetLastUsedProfileDir( |
Mike Lerman
2015/05/11 15:06:22
AppListServiceImpl.cc::GetProfilePath(), especiall
Bernhard Bauer
2015/05/11 16:52:08
Sure, done.
FYI, I also came up with some more cl
| |
507 const base::FilePath& user_data_dir) { | 517 const base::FilePath& user_data_dir) { |
508 base::FilePath last_used_profile_dir(user_data_dir); | 518 return user_data_dir.AppendASCII(GetLastUsedProfileName()); |
509 PrefService* local_state = g_browser_process->local_state(); | |
510 DCHECK(local_state); | |
511 | |
512 if (local_state->HasPrefPath(prefs::kProfileLastUsed)) { | |
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 } | 519 } |
519 | 520 |
520 std::vector<Profile*> ProfileManager::GetLastOpenedProfiles( | 521 std::vector<Profile*> ProfileManager::GetLastOpenedProfiles( |
521 const base::FilePath& user_data_dir) { | 522 const base::FilePath& user_data_dir) { |
522 PrefService* local_state = g_browser_process->local_state(); | 523 PrefService* local_state = g_browser_process->local_state(); |
523 DCHECK(local_state); | 524 DCHECK(local_state); |
524 | 525 |
525 std::vector<Profile*> to_return; | 526 std::vector<Profile*> to_return; |
526 if (local_state->HasPrefPath(prefs::kProfilesLastActive) && | 527 if (local_state->HasPrefPath(prefs::kProfilesLastActive) && |
527 local_state->GetList(prefs::kProfilesLastActive)) { | 528 local_state->GetList(prefs::kProfilesLastActive)) { |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
695 | 696 |
696 ProfileMetrics::LogProfileAddNewUser( | 697 ProfileMetrics::LogProfileAddNewUser( |
697 ProfileMetrics::ADD_NEW_USER_LAST_DELETED); | 698 ProfileMetrics::ADD_NEW_USER_LAST_DELETED); |
698 return; | 699 return; |
699 } | 700 } |
700 | 701 |
701 #if defined(OS_MACOSX) | 702 #if defined(OS_MACOSX) |
702 // On the Mac, the browser process is not killed when all browser windows are | 703 // 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 | 704 // 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. | 705 // profile has been loaded, we must pre-load a next one. |
705 PrefService* local_state = g_browser_process->local_state(); | 706 base::FilePath last_used_profile = GetLastUsedProfileDir(user_data_dir_); |
Mike Lerman
2015/05/11 15:06:22
nit: const
Bernhard Bauer
2015/05/11 16:52:08
Done.
| |
706 DCHECK(local_state); | 707 if (last_used_profile == profile_dir || |
707 const std::string last_used_profile = | 708 last_used_profile == GetGuestProfilePath()) { |
708 local_state->GetString(prefs::kProfileLastUsed); | |
709 if (last_used_profile == profile_dir.BaseName().MaybeAsASCII() || | |
710 last_used_profile == GetGuestProfilePath().BaseName().MaybeAsASCII()) { | |
711 CreateProfileAsync(last_non_supervised_profile_path, | 709 CreateProfileAsync(last_non_supervised_profile_path, |
712 base::Bind(&ProfileManager::OnNewActiveProfileLoaded, | 710 base::Bind(&ProfileManager::OnNewActiveProfileLoaded, |
713 base::Unretained(this), | 711 base::Unretained(this), |
714 profile_dir, | 712 profile_dir, |
715 last_non_supervised_profile_path, | 713 last_non_supervised_profile_path, |
716 callback), | 714 callback), |
717 base::string16(), | 715 base::string16(), |
718 base::string16(), | 716 base::string16(), |
719 std::string()); | 717 std::string()); |
720 return; | 718 return; |
721 } | 719 } |
722 #endif // defined(OS_MACOSX) | 720 #endif // defined(OS_MACOSX) |
723 | 721 |
724 FinishDeletingProfile(profile_dir, last_non_supervised_profile_path); | 722 FinishDeletingProfile(profile_dir, last_non_supervised_profile_path); |
725 } | 723 } |
726 | 724 |
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() { | 725 void ProfileManager::AutoloadProfiles() { |
739 // If running in the background is disabled for the browser, do not autoload | 726 // If running in the background is disabled for the browser, do not autoload |
740 // any profiles. | 727 // any profiles. |
741 PrefService* local_state = g_browser_process->local_state(); | 728 PrefService* local_state = g_browser_process->local_state(); |
742 DCHECK(local_state); | 729 DCHECK(local_state); |
743 if (!local_state->HasPrefPath(prefs::kBackgroundModeEnabled) || | 730 if (!local_state->HasPrefPath(prefs::kBackgroundModeEnabled) || |
744 !local_state->GetBoolean(prefs::kBackgroundModeEnabled)) { | 731 !local_state->GetBoolean(prefs::kBackgroundModeEnabled)) { |
745 return; | 732 return; |
746 } | 733 } |
747 | 734 |
748 ProfileInfoCache& cache = GetProfileInfoCache(); | 735 ProfileInfoCache& cache = GetProfileInfoCache(); |
749 size_t number_of_profiles = cache.GetNumberOfProfiles(); | 736 size_t number_of_profiles = cache.GetNumberOfProfiles(); |
750 for (size_t p = 0; p < number_of_profiles; ++p) { | 737 for (size_t p = 0; p < number_of_profiles; ++p) { |
751 if (cache.GetBackgroundStatusOfProfileAtIndex(p)) { | 738 if (cache.GetBackgroundStatusOfProfileAtIndex(p)) { |
752 // If status is true, that profile is running background apps. By calling | 739 // If status is true, that profile is running background apps. By calling |
753 // GetProfile, we automatically cause the profile to be loaded which will | 740 // GetProfile, we automatically cause the profile to be loaded which will |
754 // register it with the BackgroundModeManager. | 741 // register it with the BackgroundModeManager. |
755 GetProfile(cache.GetPathOfProfileAtIndex(p)); | 742 GetProfile(cache.GetPathOfProfileAtIndex(p)); |
756 } | 743 } |
757 } | 744 } |
758 } | 745 } |
759 | 746 |
747 void ProfileManager::CleanUpEphemeralProfiles() { | |
748 std::string last_used_profile = GetLastUsedProfileName(); | |
749 | |
750 bool last_active_profile_deleted = false; | |
751 base::FilePath new_profile_path; | |
752 std::vector<base::FilePath> profiles_to_delete; | |
753 ProfileInfoCache& profile_cache = GetProfileInfoCache(); | |
754 size_t profiles_count = profile_cache.GetNumberOfProfiles(); | |
755 for (size_t i = 0; i < profiles_count; ++i) { | |
756 base::FilePath profile_path = profile_cache.GetPathOfProfileAtIndex(i); | |
757 bool profile_is_active = | |
758 profile_path.BaseName().MaybeAsASCII() == last_used_profile; | |
Mike Lerman
2015/05/11 15:06:22
can you just inline this equality check within the
Bernhard Bauer
2015/05/11 16:52:08
Yes, I've been meaning to do that :)
| |
759 if (profile_cache.ProfileIsEphemeralAtIndex(i)) { | |
760 profiles_to_delete.push_back(profile_path); | |
761 if (profile_is_active) | |
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 |