Commit 5942a79f authored by Mario Sanchez Prada's avatar Mario Sanchez Prada Committed by Commit Bot

Add new API: IdentityManager::GetPrimaryAccountId()

This new API will provide access to the account ID of the latest cached
information for the user's primary account, which is conceptually not
the same than getting an AccountInfo with GetPrimaryAccountInfo() and
then accessing the account_id member, since that won't return any ID
when the account has been removed from the AccountTrackerService.

Bug: 887264
Change-Id: Id87e58fbe86e2a048067d4ede8d9621e2d430cff
Reviewed-on: https://chromium-review.googlesource.com/c/1264162
Commit-Queue: Mario Sanchez Prada <mario@igalia.com>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#597567}
parent 7c44412d
......@@ -68,6 +68,10 @@ AccountInfo IdentityManager::GetPrimaryAccountInfo() const {
return signin_manager_->GetAuthenticatedAccountInfo();
}
const std::string& IdentityManager::GetPrimaryAccountId() const {
return signin_manager_->GetAuthenticatedAccountId();
}
bool IdentityManager::HasPrimaryAccount() const {
return signin_manager_->IsAuthenticated();
}
......@@ -139,7 +143,7 @@ GoogleServiceAuthError IdentityManager::GetErrorStateOfRefreshTokenForAccount(
}
bool IdentityManager::HasPrimaryAccountWithRefreshToken() const {
return HasAccountWithRefreshToken(GetPrimaryAccountInfo().account_id);
return HasAccountWithRefreshToken(GetPrimaryAccountId());
}
std::unique_ptr<AccessTokenFetcher>
......@@ -198,8 +202,7 @@ void IdentityManager::SetPrimaryAccountSynchronously(
signin_manager_->SetAuthenticatedAccountInfo(gaia_id, email_address);
if (!refresh_token.empty()) {
token_service_->UpdateCredentials(GetPrimaryAccountInfo().account_id,
refresh_token);
token_service_->UpdateCredentials(GetPrimaryAccountId(), refresh_token);
}
}
......
......@@ -129,13 +129,23 @@ class IdentityManager : public SigninManagerBase::Observer,
GaiaCookieManagerService* gaia_cookie_manager_service);
~IdentityManager() override;
// Provides access to the latest cached information of the user's primary
// account.
// Provides access to the extended information of the user's primary account.
// Returns an empty struct if no such info is available, either because there
// is no primary account or because the extended information for the primary
// account has been removed (this happens when the refresh token is revoked,
// for example).
AccountInfo GetPrimaryAccountInfo() const;
// Returns whether the primary account is available, according to the latest
// cached information. Simple convenience wrapper over checking whether the
// primary account info has a valid account ID.
// Provides access to the account ID of the user's primary account. Note that
// this may return a valid string even in cases where GetPrimaryAccountInfo()
// returns an empty struct, as the extended information for the primary
// account is removed on certain events (e.g., when its refresh token is
// revoked).
const std::string& GetPrimaryAccountId() const;
// Returns whether the primary account is available. Simple convenience
// wrapper over checking whether GetPrimaryAccountId() returns a non-empty
// string.
bool HasPrimaryAccount() const;
// For ChromeOS, mutation of primary account state is not managed externally.
......
......@@ -600,6 +600,10 @@ TEST_F(IdentityManagerTest, PrimaryAccountInfoAfterSignin) {
identity_manager()->GetPrimaryAccountInfo();
EXPECT_EQ(kTestGaiaId, primary_account_info.gaia);
EXPECT_EQ(kTestEmail, primary_account_info.email);
std::string primary_account_id = identity_manager()->GetPrimaryAccountId();
EXPECT_EQ(primary_account_id, kTestGaiaId);
EXPECT_EQ(primary_account_id, primary_account_info.account_id);
}
TEST_F(IdentityManagerTest, ClearPrimaryAccount_RemoveAll) {
......@@ -666,7 +670,7 @@ TEST_F(IdentityManagerTest,
// Set primary account to have authentication error.
SetRefreshTokenForPrimaryAccount(token_service(), identity_manager());
token_service()->UpdateAuthErrorForTesting(
identity_manager()->GetPrimaryAccountInfo().account_id,
identity_manager()->GetPrimaryAccountId(),
GoogleServiceAuthError(
GoogleServiceAuthError::State::INVALID_GAIA_CREDENTIALS));
......@@ -759,6 +763,36 @@ TEST_F(IdentityManagerTest, PrimaryAccountInfoAfterSigninAndSignout) {
identity_manager()->GetPrimaryAccountInfo();
EXPECT_EQ("", primary_account_info.gaia);
EXPECT_EQ("", primary_account_info.email);
std::string primary_account_id = identity_manager()->GetPrimaryAccountId();
EXPECT_EQ("", primary_account_id);
EXPECT_EQ(primary_account_id, primary_account_info.account_id);
}
// Test that the primary account's ID remains tracked by the IdentityManager
// after signing in even after having removed the account without signing out.
TEST_F(IdentityManagerTest, PrimaryAccountInfoAfterSigninAndAccountRemoval) {
// First ensure that the user is signed in from the POV of the
// IdentityManager.
base::RunLoop run_loop;
identity_manager_observer()->set_on_primary_account_set_callback(
run_loop.QuitClosure());
signin_manager()->SignIn(kTestGaiaId, kTestEmail, "password");
run_loop.Run();
// Remove the account from the AccountTrackerService and check that
// the returned AccountInfo won't have a valid ID anymore, even if
// the IdentityManager is still storing the primary account's ID.
account_tracker()->RemoveAccount(kTestGaiaId);
AccountInfo primary_account_info =
identity_manager()->GetPrimaryAccountInfo();
EXPECT_EQ("", primary_account_info.gaia);
EXPECT_EQ("", primary_account_info.email);
EXPECT_EQ("", primary_account_info.account_id);
std::string primary_account_id = identity_manager()->GetPrimaryAccountId();
EXPECT_EQ(primary_account_id, kTestGaiaId);
}
#endif // !defined(OS_CHROMEOS)
......@@ -767,8 +801,7 @@ TEST_F(IdentityManagerTest, HasPrimaryAccount) {
// Removing the account from the AccountTrackerService should not cause
// IdentityManager to think that there is no longer a primary account.
account_tracker()->RemoveAccount(
identity_manager()->GetPrimaryAccountInfo().account_id);
account_tracker()->RemoveAccount(identity_manager()->GetPrimaryAccountId());
EXPECT_TRUE(identity_manager()->HasPrimaryAccount());
#if !defined(OS_CHROMEOS)
......@@ -1308,9 +1341,8 @@ TEST_F(IdentityManagerTest, CreateAccessTokenFetcher) {
[](GoogleServiceAuthError error, AccessTokenInfo access_token_info) {});
std::unique_ptr<AccessTokenFetcher> token_fetcher =
identity_manager()->CreateAccessTokenFetcherForAccount(
identity_manager()->GetPrimaryAccountInfo().account_id,
"dummy_consumer", scopes, std::move(callback),
AccessTokenFetcher::Mode::kImmediate);
identity_manager()->GetPrimaryAccountId(), "dummy_consumer", scopes,
std::move(callback), AccessTokenFetcher::Mode::kImmediate);
EXPECT_TRUE(token_fetcher);
}
......@@ -1328,9 +1360,8 @@ TEST_F(IdentityManagerTest, ObserveAccessTokenFetch) {
[](GoogleServiceAuthError error, AccessTokenInfo access_token_info) {});
std::unique_ptr<AccessTokenFetcher> token_fetcher =
identity_manager()->CreateAccessTokenFetcherForAccount(
identity_manager()->GetPrimaryAccountInfo().account_id,
"dummy_consumer", scopes, std::move(callback),
AccessTokenFetcher::Mode::kImmediate);
identity_manager()->GetPrimaryAccountId(), "dummy_consumer", scopes,
std::move(callback), AccessTokenFetcher::Mode::kImmediate);
run_loop.Run();
......
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