Chromium Code Reviews| Index: chrome/browser/profiles/profile_manager.cc |
| diff --git a/chrome/browser/profiles/profile_manager.cc b/chrome/browser/profiles/profile_manager.cc |
| index 3a69b5e042999d0a461ac0d6f890292d44b5057f..8c91ffa562d7a35341e4ca66071474de9d7fe4d7 100644 |
| --- a/chrome/browser/profiles/profile_manager.cc |
| +++ b/chrome/browser/profiles/profile_manager.cc |
| @@ -1313,10 +1313,31 @@ Profile* ProfileManager::CreateAndInitializeProfile( |
| #if !defined(OS_ANDROID) |
| void ProfileManager::EnsureActiveProfileExistsBeforeDeletion( |
| const CreateCallback& callback, const base::FilePath& profile_dir) { |
| + // In case we delete non-active profile, just proceed. |
| + const base::FilePath last_used_profile = |
| + GetLastUsedProfileDir(user_data_dir_); |
| + if (last_used_profile != profile_dir && |
| + last_used_profile != GetGuestProfilePath()) { |
| + FinishDeletingProfile(profile_dir, last_used_profile); |
| + return; |
| + } |
| + |
| + // Serch for an active browser and use its profile as active if possible. |
| + for (auto* browser : *BrowserList::GetInstance()) { |
|
Bernhard Bauer
2016/11/22 14:16:45
Use the real type instead of auto?
|
| + Profile* profile = browser->profile(); |
| + base::FilePath cur_path = profile->GetPath(); |
| + if (cur_path != profile_dir && |
| + !profile->IsLegacySupervised() && |
| + !IsProfileDirectoryMarkedForDeletion(cur_path)) { |
| + OnNewActiveProfileLoaded(profile_dir, cur_path, callback, profile, |
| + Profile::CREATE_STATUS_INITIALIZED); |
| + return; |
| + } |
| + } |
| + |
| + // There no valid browsers to fallback, search for any existing valid profile. |
| ProfileAttributesStorage& storage = GetProfileAttributesStorage(); |
| - // If we're deleting the last (non-legacy-supervised) profile, then create a |
| - // new profile in its place. |
| - base::FilePath last_non_supervised_profile_path; |
| + base::FilePath fallback_profile_path; |
| std::vector<ProfileAttributesEntry*> entries = |
| storage.GetAllProfilesAttributes(); |
| for (ProfileAttributesEntry* entry : entries) { |
| @@ -1326,59 +1347,33 @@ void ProfileManager::EnsureActiveProfileExistsBeforeDeletion( |
| if (cur_path != profile_dir && |
| !entry->IsLegacySupervised() && |
| !IsProfileDirectoryMarkedForDeletion(cur_path)) { |
| - last_non_supervised_profile_path = cur_path; |
| + fallback_profile_path = cur_path; |
| break; |
| } |
| } |
| - if (last_non_supervised_profile_path.empty()) { |
| - std::string new_avatar_url; |
| - base::string16 new_profile_name; |
| - |
| + // If we're deleting the last (non-legacy-supervised) profile, then create a |
| + // new profile in its place. Load existing profile otherwise. |
| + std::string new_avatar_url; |
| + base::string16 new_profile_name; |
| + if (fallback_profile_path.empty()) { |
| + fallback_profile_path = GenerateNextProfileDirectoryPath(); |
| #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) |
|
Bernhard Bauer
2016/11/22 14:16:45
As an aside, this is already inside of an !defined
|
| int avatar_index = profiles::GetPlaceholderAvatarIndex(); |
| new_avatar_url = profiles::GetDefaultAvatarIconUrl(avatar_index); |
| new_profile_name = storage.ChooseNameForNewProfile(avatar_index); |
| #endif |
| - |
| - base::FilePath new_path(GenerateNextProfileDirectoryPath()); |
| - CreateProfileAsync(new_path, |
| - base::Bind(&ProfileManager::OnNewActiveProfileLoaded, |
| - base::Unretained(this), |
| - profile_dir, |
| - new_path, |
| - callback), |
| - new_profile_name, |
| - new_avatar_url, |
| - std::string()); |
| - |
| + // A new profile about to be created. |
| ProfileMetrics::LogProfileAddNewUser( |
| ProfileMetrics::ADD_NEW_USER_LAST_DELETED); |
| - return; |
| - } |
| - |
| -#if defined(OS_MACOSX) |
| - // On the Mac, the browser process is not killed when all browser windows are |
| - // closed, so just in case we are deleting the active profile, and no other |
| - // profile has been loaded, we must pre-load a next one. |
| - const base::FilePath last_used_profile = |
| - GetLastUsedProfileDir(user_data_dir_); |
| - if (last_used_profile == profile_dir || |
| - last_used_profile == GetGuestProfilePath()) { |
| - CreateProfileAsync(last_non_supervised_profile_path, |
| - base::Bind(&ProfileManager::OnNewActiveProfileLoaded, |
| - base::Unretained(this), |
| - profile_dir, |
| - last_non_supervised_profile_path, |
| - callback), |
| - base::string16(), |
| - std::string(), |
| - std::string()); |
| - return; |
| } |
| -#endif // defined(OS_MACOSX) |
| - FinishDeletingProfile(profile_dir, last_non_supervised_profile_path); |
| + // Create and/or load fallback profile. |
| + CreateProfileAsync(fallback_profile_path, |
| + base::Bind(&ProfileManager::OnNewActiveProfileLoaded, |
| + base::Unretained(this), profile_dir, |
| + fallback_profile_path, callback), |
| + new_profile_name, new_avatar_url, std::string()); |
| } |
| void ProfileManager::FinishDeletingProfile( |