GetProfileByUser deprecated and renamed to GetProfileByUserUnsafe.
Created safe alternative which is called GetProfileByUser.
The plan is to investigate all usages of GetProfileByUserUnsafe one by one and
replace them with a safe version, if possible.
Also warning message added for cases when GetProfileByUserUnsafe returns profile
of a wrong user or signin profile. That will help us to detect suspicious calls
to GetProfileByUserUnsafe while work in progress.
BUG=361528
TBR=yoz,dcheng,agl,dewittj,skuhne,derat
Review URL: https://codereview.chromium.org/445353004
Cr-Commit-Position: refs/heads/master@{#289011}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@289011 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/chromeos/profiles/profile_helper.cc b/chrome/browser/chromeos/profiles/profile_helper.cc
index 155084e..f960630 100644
--- a/chrome/browser/chromeos/profiles/profile_helper.cc
+++ b/chrome/browser/chromeos/profiles/profile_helper.cc
@@ -177,7 +177,7 @@
return user->email() == chromeos::UserManager::Get()->GetOwnerEmail();
}
-//static
+// static
bool ProfileHelper::IsPrimaryProfile(Profile* profile) {
if (!profile)
return false;
@@ -242,11 +242,37 @@
return it == user_to_profile_for_testing_.end() ? NULL : it->second;
}
+ if (!user->is_profile_created())
+ return NULL;
+ Profile* profile =
+ ProfileHelper::GetProfileByUserIdHash(user->username_hash());
+
+ // GetActiveUserProfile() or GetProfileByUserIdHash() returns a new instance
+ // of ProfileImpl(), but actually its OffTheRecordProfile() should be used.
+ if (UserManager::Get()->IsLoggedInAsGuest())
+ profile = profile->GetOffTheRecordProfile();
+
+ return profile;
+}
+
+Profile* ProfileHelper::GetProfileByUserUnsafe(const user_manager::User* user) {
+ // This map is non-empty only in tests.
+ if (!user_to_profile_for_testing_.empty()) {
+ std::map<const user_manager::User*, Profile*>::const_iterator it =
+ user_to_profile_for_testing_.find(user);
+ return it == user_to_profile_for_testing_.end() ? NULL : it->second;
+ }
+
Profile* profile = NULL;
- if (user->is_profile_created())
+ if (user->is_profile_created()) {
profile = ProfileHelper::GetProfileByUserIdHash(user->username_hash());
- else
+ } else {
+ LOG(WARNING) << "ProfileHelper::GetProfileByUserUnsafe is called when "
+ "|user|'s profile is not created. It probably means that "
+ "something is wrong with a calling code. Please report in "
+ "http://crbug.com/361528 if you see this message.";
profile = ProfileManager::GetActiveUserProfile();
+ }
// GetActiveUserProfile() or GetProfileByUserIdHash() returns a new instance
// of ProfileImpl(), but actually its OffTheRecordProfile() should be used.