Commit 61fe0044 authored by Sylvain Defresne's avatar Sylvain Defresne Committed by Commit Bot

Add FindExtendedAccountInfoForAccount to IdentityManager

FindExtendedAccountInfoForAccount returns the extended information
for an account using the CoreAccountInfo as a key. This will be
used in preparation to converting GetPrimaryAccountInfo to return
a CoreAccountInfo.

Bug: 930126
Change-Id: Iabd3058cb4f5851dc753a45527c67135eb304a55
Reviewed-on: https://chromium-review.googlesource.com/c/1481332
Commit-Queue: David Roger <droger@chromium.org>
Auto-Submit: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#635477}
parent c0748e6e
...@@ -138,6 +138,25 @@ bool IdentityManager::AreRefreshTokensLoaded() const { ...@@ -138,6 +138,25 @@ bool IdentityManager::AreRefreshTokensLoaded() const {
return token_service_->AreAllCredentialsLoaded(); return token_service_->AreAllCredentialsLoaded();
} }
base::Optional<AccountInfo> IdentityManager::FindExtendedAccountInfoForAccount(
const CoreAccountInfo& account_info) const {
AccountInfo extended_account_info =
account_tracker_service_->GetAccountInfo(account_info.account_id);
// AccountTrackerService always returns an AccountInfo, even on failure. In
// case of failure, the AccountInfo will be unpopulated, thus we should not
// be able to find a valid refresh token.
if (!HasAccountWithRefreshToken(extended_account_info.account_id))
return base::nullopt;
// If the extended information is not available, AccountInfo::IsValid() will
// return false. If this is the case, return base::nullopt.
if (!extended_account_info.IsValid())
return base::nullopt;
return GetAccountInfoForAccountWithRefreshToken(account_info.account_id);
}
base::Optional<AccountInfo> base::Optional<AccountInfo>
IdentityManager::FindAccountInfoForAccountWithRefreshTokenByAccountId( IdentityManager::FindAccountInfoForAccountWithRefreshTokenByAccountId(
const std::string& account_id) const { const std::string& account_id) const {
......
...@@ -277,6 +277,12 @@ class IdentityManager : public SigninManagerBase::Observer, ...@@ -277,6 +277,12 @@ class IdentityManager : public SigninManagerBase::Observer,
// Returns true if all refresh tokens have been loaded from disk. // Returns true if all refresh tokens have been loaded from disk.
bool AreRefreshTokensLoaded() const; bool AreRefreshTokensLoaded() const;
// Returns extended information for account identified by |account_info|.
// The information will be returned if the information is available and
// refresh token is available for account.
base::Optional<AccountInfo> FindExtendedAccountInfoForAccount(
const CoreAccountInfo& account_info) const;
// Looks up and returns information for account with given |account_id|. If // Looks up and returns information for account with given |account_id|. If
// the account cannot be found, return an empty optional. This is equivalent // the account cannot be found, return an empty optional. This is equivalent
// to searching on the vector returned by GetAccountsWithRefreshTokens() but // to searching on the vector returned by GetAccountsWithRefreshTokens() but
......
...@@ -2417,4 +2417,71 @@ TEST_F(IdentityManagerTest, TestLegacyPickAccountIdForAccount) { ...@@ -2417,4 +2417,71 @@ TEST_F(IdentityManagerTest, TestLegacyPickAccountIdForAccount) {
} }
} }
// Check that FindExtendedAccountInfoForAccount returns a valid account info
// iff the account is known, has refresh token and all the extended information
// is available.
TEST_F(IdentityManagerTest, FindExtendedAccountInfoForAccount) {
CoreAccountInfo account_info;
account_info.email = kTestEmail;
account_info.gaia = kTestGaiaId;
account_info.account_id = identity_manager()->LegacyPickAccountIdForAccount(
kTestGaiaId, kTestEmail);
// FindExtendedAccountInfoForAccount() returns empty optional if the
// account_info is invalid.
EXPECT_FALSE(identity_manager()
->FindExtendedAccountInfoForAccount(CoreAccountInfo{})
.has_value());
// FindExtendedAccountInfoForAccount() returns empty optional if the
// account_info is unknown.
EXPECT_FALSE(identity_manager()
->FindExtendedAccountInfoForAccount(account_info)
.has_value());
// Insert the core account information in the AccountTrackerService.
const std::string account_id =
account_tracker()->SeedAccountInfo(kTestGaiaId, kTestEmail);
ASSERT_EQ(account_info.account_id, account_id);
// FindExtendedAccountInfoForAccount() returns empty optional if the account
// has no refresh token.
EXPECT_FALSE(identity_manager()
->FindExtendedAccountInfoForAccount(account_info)
.has_value());
// Insert refresh token for account.
SetRefreshTokenForAccount(identity_manager(), account_info.account_id,
"refresh-token");
// FindExtendedAccountInfoForAccount() returns empty optional if the account
// has not extended information.
EXPECT_FALSE(identity_manager()
->FindExtendedAccountInfoForAccount(account_info)
.has_value());
// Populate the extended information of the account.
base::DictionaryValue user_info;
user_info.SetString("id", account_info.gaia);
user_info.SetString("email", account_info.email);
user_info.SetString("hd", "hosted_domain");
user_info.SetString("name", "full_name");
user_info.SetString("given_name", "given_name");
user_info.SetString("locale", "locale");
user_info.SetString("picture", "picture_url");
account_tracker()->SetAccountInfoFromUserInfo(account_info.account_id,
&user_info);
// FindExtendedAccountInfoForAccount() returns extended account information if
// the account is know, has valid refresh token and has extended account
// information.
const base::Optional<AccountInfo> extended_account_info =
identity_manager()->FindExtendedAccountInfoForAccount(account_info);
ASSERT_TRUE(extended_account_info.has_value());
EXPECT_EQ(account_info.gaia, extended_account_info.value().gaia);
EXPECT_EQ(account_info.email, extended_account_info.value().email);
EXPECT_EQ(account_info.account_id, extended_account_info.value().account_id);
}
} // namespace identity } // namespace identity
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment