Chromium Code Reviews| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 1295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1306 if (profile) { | 1306 if (profile) { |
| 1307 bool result = AddProfile(profile); | 1307 bool result = AddProfile(profile); |
| 1308 DCHECK(result); | 1308 DCHECK(result); |
| 1309 } | 1309 } |
| 1310 return profile; | 1310 return profile; |
| 1311 } | 1311 } |
| 1312 | 1312 |
| 1313 #if !defined(OS_ANDROID) | 1313 #if !defined(OS_ANDROID) |
| 1314 void ProfileManager::EnsureActiveProfileExistsBeforeDeletion( | 1314 void ProfileManager::EnsureActiveProfileExistsBeforeDeletion( |
| 1315 const CreateCallback& callback, const base::FilePath& profile_dir) { | 1315 const CreateCallback& callback, const base::FilePath& profile_dir) { |
| 1316 // In case we delete non-active profile, just proceed. | |
| 1317 const base::FilePath last_used_profile = | |
| 1318 GetLastUsedProfileDir(user_data_dir_); | |
| 1319 if (last_used_profile != profile_dir && | |
| 1320 last_used_profile != GetGuestProfilePath()) { | |
| 1321 FinishDeletingProfile(profile_dir, last_used_profile); | |
| 1322 return; | |
| 1323 } | |
| 1324 | |
| 1325 // Serch for an active browser and use its profile as active if possible. | |
|
anthonyvd
2016/11/23 21:03:17
nit: Search
| |
| 1326 for (Browser* browser : *BrowserList::GetInstance()) { | |
| 1327 Profile* profile = browser->profile(); | |
| 1328 base::FilePath cur_path = profile->GetPath(); | |
| 1329 if (cur_path != profile_dir && | |
| 1330 !profile->IsLegacySupervised() && | |
| 1331 !IsProfileDirectoryMarkedForDeletion(cur_path)) { | |
| 1332 OnNewActiveProfileLoaded(profile_dir, cur_path, callback, profile, | |
| 1333 Profile::CREATE_STATUS_INITIALIZED); | |
| 1334 return; | |
| 1335 } | |
| 1336 } | |
| 1337 | |
| 1338 // There no valid browsers to fallback, search for any existing valid profile. | |
| 1316 ProfileAttributesStorage& storage = GetProfileAttributesStorage(); | 1339 ProfileAttributesStorage& storage = GetProfileAttributesStorage(); |
| 1317 // If we're deleting the last (non-legacy-supervised) profile, then create a | 1340 base::FilePath fallback_profile_path; |
| 1318 // new profile in its place. | |
| 1319 base::FilePath last_non_supervised_profile_path; | |
| 1320 std::vector<ProfileAttributesEntry*> entries = | 1341 std::vector<ProfileAttributesEntry*> entries = |
| 1321 storage.GetAllProfilesAttributes(); | 1342 storage.GetAllProfilesAttributes(); |
| 1322 for (ProfileAttributesEntry* entry : entries) { | 1343 for (ProfileAttributesEntry* entry : entries) { |
| 1323 base::FilePath cur_path = entry->GetPath(); | 1344 base::FilePath cur_path = entry->GetPath(); |
| 1324 // Make sure that this profile is not pending deletion, and is not | 1345 // Make sure that this profile is not pending deletion, and is not |
| 1325 // legacy-supervised. | 1346 // legacy-supervised. |
| 1326 if (cur_path != profile_dir && | 1347 if (cur_path != profile_dir && |
| 1327 !entry->IsLegacySupervised() && | 1348 !entry->IsLegacySupervised() && |
| 1328 !IsProfileDirectoryMarkedForDeletion(cur_path)) { | 1349 !IsProfileDirectoryMarkedForDeletion(cur_path)) { |
| 1329 last_non_supervised_profile_path = cur_path; | 1350 fallback_profile_path = cur_path; |
| 1330 break; | 1351 break; |
| 1331 } | 1352 } |
| 1332 } | 1353 } |
| 1333 | 1354 |
| 1334 if (last_non_supervised_profile_path.empty()) { | 1355 // If we're deleting the last (non-legacy-supervised) profile, then create a |
| 1335 std::string new_avatar_url; | 1356 // new profile in its place. Load existing profile otherwise. |
| 1336 base::string16 new_profile_name; | 1357 std::string new_avatar_url; |
| 1337 | 1358 base::string16 new_profile_name; |
| 1338 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) | 1359 if (fallback_profile_path.empty()) { |
| 1360 fallback_profile_path = GenerateNextProfileDirectoryPath(); | |
| 1361 #if !defined(OS_CHROMEOS) | |
| 1339 int avatar_index = profiles::GetPlaceholderAvatarIndex(); | 1362 int avatar_index = profiles::GetPlaceholderAvatarIndex(); |
| 1340 new_avatar_url = profiles::GetDefaultAvatarIconUrl(avatar_index); | 1363 new_avatar_url = profiles::GetDefaultAvatarIconUrl(avatar_index); |
| 1341 new_profile_name = storage.ChooseNameForNewProfile(avatar_index); | 1364 new_profile_name = storage.ChooseNameForNewProfile(avatar_index); |
| 1342 #endif | 1365 #endif |
| 1343 | 1366 // A new profile about to be created. |
| 1344 base::FilePath new_path(GenerateNextProfileDirectoryPath()); | |
| 1345 CreateProfileAsync(new_path, | |
| 1346 base::Bind(&ProfileManager::OnNewActiveProfileLoaded, | |
| 1347 base::Unretained(this), | |
| 1348 profile_dir, | |
| 1349 new_path, | |
| 1350 callback), | |
| 1351 new_profile_name, | |
| 1352 new_avatar_url, | |
| 1353 std::string()); | |
| 1354 | |
| 1355 ProfileMetrics::LogProfileAddNewUser( | 1367 ProfileMetrics::LogProfileAddNewUser( |
| 1356 ProfileMetrics::ADD_NEW_USER_LAST_DELETED); | 1368 ProfileMetrics::ADD_NEW_USER_LAST_DELETED); |
| 1357 return; | |
| 1358 } | 1369 } |
| 1359 | 1370 |
| 1360 #if defined(OS_MACOSX) | 1371 // Create and/or load fallback profile. |
| 1361 // On the Mac, the browser process is not killed when all browser windows are | 1372 CreateProfileAsync(fallback_profile_path, |
| 1362 // closed, so just in case we are deleting the active profile, and no other | 1373 base::Bind(&ProfileManager::OnNewActiveProfileLoaded, |
| 1363 // profile has been loaded, we must pre-load a next one. | 1374 base::Unretained(this), profile_dir, |
| 1364 const base::FilePath last_used_profile = | 1375 fallback_profile_path, callback), |
| 1365 GetLastUsedProfileDir(user_data_dir_); | 1376 new_profile_name, new_avatar_url, std::string()); |
| 1366 if (last_used_profile == profile_dir || | |
| 1367 last_used_profile == GetGuestProfilePath()) { | |
| 1368 CreateProfileAsync(last_non_supervised_profile_path, | |
| 1369 base::Bind(&ProfileManager::OnNewActiveProfileLoaded, | |
| 1370 base::Unretained(this), | |
| 1371 profile_dir, | |
| 1372 last_non_supervised_profile_path, | |
| 1373 callback), | |
| 1374 base::string16(), | |
| 1375 std::string(), | |
| 1376 std::string()); | |
| 1377 return; | |
| 1378 } | |
| 1379 #endif // defined(OS_MACOSX) | |
| 1380 | |
| 1381 FinishDeletingProfile(profile_dir, last_non_supervised_profile_path); | |
| 1382 } | 1377 } |
| 1383 | 1378 |
| 1384 void ProfileManager::FinishDeletingProfile( | 1379 void ProfileManager::FinishDeletingProfile( |
| 1385 const base::FilePath& profile_dir, | 1380 const base::FilePath& profile_dir, |
| 1386 const base::FilePath& new_active_profile_dir) { | 1381 const base::FilePath& new_active_profile_dir) { |
| 1387 // Update the last used profile pref before closing browser windows. This | 1382 // Update the last used profile pref before closing browser windows. This |
| 1388 // way the correct last used profile is set for any notification observers. | 1383 // way the correct last used profile is set for any notification observers. |
| 1389 profiles::SetLastUsedProfile( | 1384 profiles::SetLastUsedProfile( |
| 1390 new_active_profile_dir.BaseName().MaybeAsASCII()); | 1385 new_active_profile_dir.BaseName().MaybeAsASCII()); |
| 1391 | 1386 |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1669 | 1664 |
| 1670 FinishDeletingProfile(profile_to_delete_path, new_active_profile_path); | 1665 FinishDeletingProfile(profile_to_delete_path, new_active_profile_path); |
| 1671 if (!original_callback.is_null()) | 1666 if (!original_callback.is_null()) |
| 1672 original_callback.Run(loaded_profile, status); | 1667 original_callback.Run(loaded_profile, status); |
| 1673 } | 1668 } |
| 1674 #endif // !defined(OS_ANDROID) | 1669 #endif // !defined(OS_ANDROID) |
| 1675 | 1670 |
| 1676 ProfileManagerWithoutInit::ProfileManagerWithoutInit( | 1671 ProfileManagerWithoutInit::ProfileManagerWithoutInit( |
| 1677 const base::FilePath& user_data_dir) : ProfileManager(user_data_dir) { | 1672 const base::FilePath& user_data_dir) : ProfileManager(user_data_dir) { |
| 1678 } | 1673 } |
| OLD | NEW |