Commit 08b77943 authored by Mihai Sardarescu's avatar Mihai Sardarescu Committed by Commit Bot

Fetch the user info when the account info in invalid.

This CL removes some Android-only logic that avoided downloading the
account info when the account info was invalid. This ensures that the
account info is correctly updated when the user signs in to Chrome.

This CL also fixes a subtle bug that lead to the avatar image not being
downloaded on start-up if the previous attempt to download the avatar
image failed.

Bug: 1135958
Change-Id: Ia31993f969aac2fc6b7c6f1ed325eb012ad33dd0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2461310
Commit-Queue: Mihai Sardarescu <msarda@chromium.org>
Reviewed-by: default avatarAlice Wang <aliceywang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#815632}
parent a0966b4c
...@@ -79,7 +79,7 @@ void AccountFetcherService::Initialize( ...@@ -79,7 +79,7 @@ void AccountFetcherService::Initialize(
signin_client_->GetPrefs(), AccountFetcherService::kLastUpdatePref, signin_client_->GetPrefs(), AccountFetcherService::kLastUpdatePref,
kRefreshFromTokenServiceDelay, kRefreshFromTokenServiceDelay,
base::Bind(&AccountFetcherService::RefreshAllAccountInfo, base::Bind(&AccountFetcherService::RefreshAllAccountInfo,
base::Unretained(this), false)); base::Unretained(this), /*only_fetch_if_invalid=*/false));
// Tokens may have already been loaded and we will not receive a // Tokens may have already been loaded and we will not receive a
// notification-on-registration for |token_service_->AddObserver(this)| few // notification-on-registration for |token_service_->AddObserver(this)| few
...@@ -105,7 +105,7 @@ bool AccountFetcherService::IsAllUserInfoFetched() const { ...@@ -105,7 +105,7 @@ bool AccountFetcherService::IsAllUserInfoFetched() const {
void AccountFetcherService::ForceRefreshOfAccountInfo( void AccountFetcherService::ForceRefreshOfAccountInfo(
const CoreAccountId& account_id) { const CoreAccountId& account_id) {
DCHECK(network_fetches_enabled_); DCHECK(network_fetches_enabled_);
RefreshAccountInfo(account_id, false); RefreshAccountInfo(account_id, /*only_fetch_if_invalid=*/false);
} }
void AccountFetcherService::OnNetworkInitialized() { void AccountFetcherService::OnNetworkInitialized() {
...@@ -167,7 +167,7 @@ void AccountFetcherService::MaybeEnableNetworkFetches() { ...@@ -167,7 +167,7 @@ void AccountFetcherService::MaybeEnableNetworkFetches() {
network_fetches_enabled_ = true; network_fetches_enabled_ = true;
repeating_timer_->Start(); repeating_timer_->Start();
} }
RefreshAllAccountInfo(true); RefreshAllAccountInfo(/*only_fetch_if_invalid=*/true);
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
UpdateChildInfo(); UpdateChildInfo();
#endif #endif
...@@ -221,15 +221,21 @@ void AccountFetcherService::RefreshAccountInfo(const CoreAccountId& account_id, ...@@ -221,15 +221,21 @@ void AccountFetcherService::RefreshAccountInfo(const CoreAccountId& account_id,
const AccountInfo& info = const AccountInfo& info =
account_tracker_service_->GetAccountInfo(account_id); account_tracker_service_->GetAccountInfo(account_id);
// |only_fetch_if_invalid| is false when the service is due for a timed update. // |only_fetch_if_invalid| is false when the service is due for a timed
#if defined(OS_ANDROID) // update.
// TODO(mlerman): Change this condition back to info.IsValid() and ensure the if (!only_fetch_if_invalid || !info.IsValid()) {
// Fetch doesn't occur until after ProfileImpl::OnPrefsLoaded(). // Fetching the user info will also fetch the account image.
if (!only_fetch_if_invalid || info.gaia.empty())
#else
if (!only_fetch_if_invalid || !info.IsValid())
#endif
StartFetchingUserInfo(account_id); StartFetchingUserInfo(account_id);
return;
}
// User info is already valid and does not need to be downloaded again.
// Fetch the account image in case it was not fetched previously.
//
// Note: |FetchAccountImage()| does not fetch the account image if the
// account image was already downloaded. So it is fine to call this method
// even when |only_fetch_if_invalid| is true.
FetchAccountImage(account_id);
} }
void AccountFetcherService::OnUserInfoFetchSuccess( void AccountFetcherService::OnUserInfoFetchSuccess(
...@@ -323,7 +329,7 @@ void AccountFetcherService::OnRefreshTokenAvailable( ...@@ -323,7 +329,7 @@ void AccountFetcherService::OnRefreshTokenAvailable(
if (!network_fetches_enabled_) if (!network_fetches_enabled_)
return; return;
RefreshAccountInfo(account_id, true); RefreshAccountInfo(account_id, /*only_fetch_if_invalid=*/true);
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
UpdateChildInfo(); UpdateChildInfo();
#endif #endif
......
...@@ -107,11 +107,10 @@ std::string AccountKeyToPictureURL(AccountKey account_key) { ...@@ -107,11 +107,10 @@ std::string AccountKeyToPictureURL(AccountKey account_key) {
account_key.value); account_key.value);
} }
std::string AccountKeyToPictureURLWithSize(AccountKey account_key) { GURL AccountKeyToPictureURLWithSize(AccountKey account_key) {
return signin::GetAvatarImageURLWithOptions( return signin::GetAvatarImageURLWithOptions(
GURL(AccountKeyToPictureURL(account_key)), GURL(AccountKeyToPictureURL(account_key)), signin::kAccountInfoImageSize,
signin::kAccountInfoImageSize, true /* no_silhouette */) true /* no_silhouette */);
.spec();
} }
class TrackingEvent { class TrackingEvent {
...@@ -315,7 +314,8 @@ class AccountTrackerServiceTest : public testing::Test { ...@@ -315,7 +314,8 @@ class AccountTrackerServiceTest : public testing::Test {
} }
protected: protected:
void ReturnFetchResults(net::HttpStatusCode response_code, void ReturnFetchResults(const GURL& url,
net::HttpStatusCode response_code,
const std::string& response_string); const std::string& response_string);
base::test::TaskEnvironment task_environment_; base::test::TaskEnvironment task_environment_;
...@@ -371,9 +371,9 @@ class AccountTrackerServiceTest : public testing::Test { ...@@ -371,9 +371,9 @@ class AccountTrackerServiceTest : public testing::Test {
}; };
void AccountTrackerServiceTest::ReturnFetchResults( void AccountTrackerServiceTest::ReturnFetchResults(
const GURL& url,
net::HttpStatusCode response_code, net::HttpStatusCode response_code,
const std::string& response_string) { const std::string& response_string) {
GURL url = GaiaUrls::GetInstance()->oauth_user_info_url();
EXPECT_TRUE(GetTestURLLoaderFactory()->IsPending(url.spec())); EXPECT_TRUE(GetTestURLLoaderFactory()->IsPending(url.spec()));
// It's possible for multiple requests to be pending. Respond to all of them. // It's possible for multiple requests to be pending. Respond to all of them.
...@@ -388,35 +388,35 @@ void AccountTrackerServiceTest::ReturnFetchResults( ...@@ -388,35 +388,35 @@ void AccountTrackerServiceTest::ReturnFetchResults(
void AccountTrackerServiceTest::ReturnAccountInfoFetchSuccess( void AccountTrackerServiceTest::ReturnAccountInfoFetchSuccess(
AccountKey account_key) { AccountKey account_key) {
IssueAccessToken(account_key); IssueAccessToken(account_key);
ReturnFetchResults(net::HTTP_OK, GenerateValidTokenInfoResponse(account_key)); ReturnFetchResults(GaiaUrls::GetInstance()->oauth_user_info_url(),
net::HTTP_OK, GenerateValidTokenInfoResponse(account_key));
} }
void AccountTrackerServiceTest::ReturnAccountInfoFetchSuccessIncomplete( void AccountTrackerServiceTest::ReturnAccountInfoFetchSuccessIncomplete(
AccountKey account_key) { AccountKey account_key) {
IssueAccessToken(account_key); IssueAccessToken(account_key);
ReturnFetchResults(net::HTTP_OK, ReturnFetchResults(GaiaUrls::GetInstance()->oauth_user_info_url(),
net::HTTP_OK,
GenerateIncompleteTokenInfoResponse(account_key)); GenerateIncompleteTokenInfoResponse(account_key));
} }
void AccountTrackerServiceTest::ReturnAccountInfoFetchFailure( void AccountTrackerServiceTest::ReturnAccountInfoFetchFailure(
AccountKey account_key) { AccountKey account_key) {
IssueAccessToken(account_key); IssueAccessToken(account_key);
ReturnFetchResults(net::HTTP_BAD_REQUEST, std::string()); ReturnFetchResults(GaiaUrls::GetInstance()->oauth_user_info_url(),
net::HTTP_BAD_REQUEST, std::string());
} }
void AccountTrackerServiceTest::ReturnAccountImageFetchSuccess( void AccountTrackerServiceTest::ReturnAccountImageFetchSuccess(
AccountKey account_key) { AccountKey account_key) {
GetTestURLLoaderFactory()->AddResponse( ReturnFetchResults(AccountKeyToPictureURLWithSize(account_key), net::HTTP_OK,
AccountKeyToPictureURLWithSize(account_key), "image data"); "image data");
task_environment_.RunUntilIdle();
} }
void AccountTrackerServiceTest::ReturnAccountImageFetchFailure( void AccountTrackerServiceTest::ReturnAccountImageFetchFailure(
AccountKey account_key) { AccountKey account_key) {
GetTestURLLoaderFactory()->AddResponse( ReturnFetchResults(AccountKeyToPictureURLWithSize(account_key),
AccountKeyToPictureURLWithSize(account_key), std::string(), net::HTTP_BAD_REQUEST, "image data");
net::HTTP_BAD_REQUEST);
task_environment_.RunUntilIdle();
} }
TEST_F(AccountTrackerServiceTest, Basic) {} TEST_F(AccountTrackerServiceTest, Basic) {}
...@@ -563,6 +563,44 @@ TEST_F(AccountTrackerServiceTest, TwoTokenAvailable_OneUserInfo) { ...@@ -563,6 +563,44 @@ TEST_F(AccountTrackerServiceTest, TwoTokenAvailable_OneUserInfo) {
})); }));
} }
// Regression test for http://crbug.com/1135958
TEST_F(AccountTrackerServiceTest,
AccountSeeded_TokenAvailable_UserInfoSuccess) {
// Setup: Seed the account before simulating that the refresh token is
// available.
account_tracker()->SeedAccountInfo(AccountKeyToGaiaId(kAccountKeyAlpha),
AccountKeyToEmail(kAccountKeyAlpha));
// Account fetcher service should fetch user info for accounts that were
// seeded.
SimulateTokenAvailable(kAccountKeyAlpha);
ReturnAccountInfoFetchSuccess(kAccountKeyAlpha);
EXPECT_TRUE(account_fetcher()->IsAllUserInfoFetched());
}
// Regression test for http://crbug.com/1135958
TEST_F(AccountTrackerServiceTest, RefreshAccount_FetchImageSuccess) {
CoreAccountId account_id = AccountKeyToAccountId(kAccountKeyAlpha);
// Setup: User info fetched successfully, but missing the account image.
SimulateTokenAvailable(kAccountKeyAlpha);
ReturnAccountInfoFetchSuccess(kAccountKeyAlpha);
ReturnAccountImageFetchFailure(kAccountKeyAlpha);
ASSERT_TRUE(account_tracker()->GetAccountInfo(account_id).IsValid());
ASSERT_TRUE(
account_tracker()->GetAccountInfo(account_id).account_image.IsEmpty());
// Account fetcher should fetch the account image even when user info if
// the account image was not fetched before.
SimulateTokenAvailable(kAccountKeyAlpha);
ReturnAccountImageFetchSuccess(kAccountKeyAlpha);
AccountInfo account_info = account_tracker()->GetAccountInfo(
AccountKeyToAccountId(kAccountKeyAlpha));
EXPECT_FALSE(account_info.account_image.IsEmpty());
EXPECT_EQ(account_info.last_downloaded_image_url_with_size,
AccountKeyToPictureURLWithSize(kAccountKeyAlpha));
}
TEST_F(AccountTrackerServiceTest, GetAccounts) { TEST_F(AccountTrackerServiceTest, GetAccounts) {
SimulateTokenAvailable(kAccountKeyAlpha); SimulateTokenAvailable(kAccountKeyAlpha);
SimulateTokenAvailable(kAccountKeyBeta); SimulateTokenAvailable(kAccountKeyBeta);
...@@ -1139,7 +1177,8 @@ TEST_F(AccountTrackerServiceTest, ChildAccountUpdatedAndRevoked) { ...@@ -1139,7 +1177,8 @@ TEST_F(AccountTrackerServiceTest, ChildAccountUpdatedAndRevoked) {
account_tracker()->SetIsChildAccount(AccountKeyToAccountId(kAccountKeyChild), account_tracker()->SetIsChildAccount(AccountKeyToAccountId(kAccountKeyChild),
false); false);
#endif #endif
ReturnFetchResults(net::HTTP_OK, ReturnFetchResults(GaiaUrls::GetInstance()->oauth_user_info_url(),
net::HTTP_OK,
GenerateValidTokenInfoResponse(kAccountKeyChild)); GenerateValidTokenInfoResponse(kAccountKeyChild));
EXPECT_TRUE(CheckAccountTrackerEvents({ EXPECT_TRUE(CheckAccountTrackerEvents({
TrackingEvent(UPDATED, AccountKeyToAccountId(kAccountKeyChild), TrackingEvent(UPDATED, AccountKeyToAccountId(kAccountKeyChild),
...@@ -1167,7 +1206,8 @@ TEST_F(AccountTrackerServiceTest, ChildAccountUpdatedAndRevokedWithUpdate) { ...@@ -1167,7 +1206,8 @@ TEST_F(AccountTrackerServiceTest, ChildAccountUpdatedAndRevokedWithUpdate) {
account_tracker()->SetIsChildAccount(AccountKeyToAccountId(kAccountKeyChild), account_tracker()->SetIsChildAccount(AccountKeyToAccountId(kAccountKeyChild),
true); true);
#endif #endif
ReturnFetchResults(net::HTTP_OK, ReturnFetchResults(GaiaUrls::GetInstance()->oauth_user_info_url(),
net::HTTP_OK,
GenerateValidTokenInfoResponse(kAccountKeyChild)); GenerateValidTokenInfoResponse(kAccountKeyChild));
EXPECT_TRUE(CheckAccountTrackerEvents({ EXPECT_TRUE(CheckAccountTrackerEvents({
TrackingEvent(UPDATED, AccountKeyToAccountId(kAccountKeyChild), TrackingEvent(UPDATED, AccountKeyToAccountId(kAccountKeyChild),
...@@ -1253,7 +1293,8 @@ TEST_F(AccountTrackerServiceTest, ChildAccountGraduation) { ...@@ -1253,7 +1293,8 @@ TEST_F(AccountTrackerServiceTest, ChildAccountGraduation) {
AccountInfo info = account_tracker()->GetAccountInfo( AccountInfo info = account_tracker()->GetAccountInfo(
AccountKeyToAccountId(kAccountKeyChild)); AccountKeyToAccountId(kAccountKeyChild));
EXPECT_TRUE(info.is_child_account); EXPECT_TRUE(info.is_child_account);
ReturnFetchResults(net::HTTP_OK, ReturnFetchResults(GaiaUrls::GetInstance()->oauth_user_info_url(),
net::HTTP_OK,
GenerateValidTokenInfoResponse(kAccountKeyChild)); GenerateValidTokenInfoResponse(kAccountKeyChild));
EXPECT_TRUE(CheckAccountTrackerEvents({ EXPECT_TRUE(CheckAccountTrackerEvents({
TrackingEvent(UPDATED, AccountKeyToAccountId(kAccountKeyChild), TrackingEvent(UPDATED, AccountKeyToAccountId(kAccountKeyChild),
......
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