Commit c352b1d6 authored by Mario Sanchez Prada's avatar Mario Sanchez Prada Committed by Commit Bot

Simplify token retrieval logic in TokenHandleFetcher

Instead of relying on an implementation of IdentityManager::Observer and
a private flag to know when a RefreshToken is available before requesting
using PrimaryAccountAccessTokenFetcher, use PAATF's mode to wait until the
primary account has a refresh token available (i.e. kWaitUntilAvailable)
whenever needed when TokenHandleFetcher::BackfillToken() is invoked, or
simply using kImmediate if such refresh token is already available.

Bug: 905689
Change-Id: Idf8e8b58a9bda518f2bd11d140b4e8776194a8f4
Reviewed-on: https://chromium-review.googlesource.com/c/1343264
Commit-Queue: Mario Sanchez Prada <mario@igalia.com>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#611079}
parent c8163c78
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "chrome/browser/signin/identity_manager_factory.h" #include "chrome/browser/signin/identity_manager_factory.h"
#include "components/keyed_service/content/browser_context_keyed_service_shutdown_notifier_factory.h" #include "components/keyed_service/content/browser_context_keyed_service_shutdown_notifier_factory.h"
#include "google_apis/gaia/gaia_constants.h" #include "google_apis/gaia/gaia_constants.h"
#include "services/identity/public/cpp/identity_manager.h"
#include "services/identity/public/cpp/primary_account_access_token_fetcher.h" #include "services/identity/public/cpp/primary_account_access_token_fetcher.h"
#include "services/identity/public/cpp/scope_set.h" #include "services/identity/public/cpp/scope_set.h"
#include "services/network/public/cpp/shared_url_loader_factory.h" #include "services/network/public/cpp/shared_url_loader_factory.h"
...@@ -47,10 +48,7 @@ TokenHandleFetcher::TokenHandleFetcher(TokenHandleUtil* util, ...@@ -47,10 +48,7 @@ TokenHandleFetcher::TokenHandleFetcher(TokenHandleUtil* util,
const AccountId& account_id) const AccountId& account_id)
: token_handle_util_(util), account_id_(account_id) {} : token_handle_util_(util), account_id_(account_id) {}
TokenHandleFetcher::~TokenHandleFetcher() { TokenHandleFetcher::~TokenHandleFetcher() {}
if (waiting_for_refresh_token_)
identity_manager_->RemoveObserver(this);
}
void TokenHandleFetcher::BackfillToken(Profile* profile, void TokenHandleFetcher::BackfillToken(Profile* profile,
const TokenFetchingCallback& callback) { const TokenFetchingCallback& callback) {
...@@ -58,33 +56,17 @@ void TokenHandleFetcher::BackfillToken(Profile* profile, ...@@ -58,33 +56,17 @@ void TokenHandleFetcher::BackfillToken(Profile* profile,
callback_ = callback; callback_ = callback;
identity_manager_ = IdentityManagerFactory::GetForProfile(profile); identity_manager_ = IdentityManagerFactory::GetForProfile(profile);
const std::string user_email = identity_manager_->GetPrimaryAccountId(); if (!identity_manager_->HasPrimaryAccountWithRefreshToken()) {
if (!identity_manager_->HasAccountWithRefreshToken(user_email)) {
account_without_token_ = user_email;
profile_shutdown_notification_ = profile_shutdown_notification_ =
TokenHandleFetcherShutdownNotifierFactory::GetInstance() TokenHandleFetcherShutdownNotifierFactory::GetInstance()
->Get(profile) ->Get(profile)
->Subscribe(base::Bind(&TokenHandleFetcher::OnProfileDestroyed, ->Subscribe(base::Bind(&TokenHandleFetcher::OnProfileDestroyed,
base::Unretained(this))); base::Unretained(this)));
identity_manager_->AddObserver(this);
waiting_for_refresh_token_ = true;
return;
} }
RequestAccessToken(user_email);
}
void TokenHandleFetcher::OnRefreshTokenUpdatedForAccount(
const AccountInfo& account_info,
bool is_valid) {
if (account_without_token_ != account_info.email)
return;
waiting_for_refresh_token_ = false;
identity_manager_->RemoveObserver(this);
RequestAccessToken(account_info.email);
}
void TokenHandleFetcher::RequestAccessToken(const std::string& user_email) { // Now we can request the token, knowing that it will be immediately requested
// if the refresh token is available, or that it will be requested once the
// refresh token is available for the primary account.
identity::ScopeSet scopes; identity::ScopeSet scopes;
scopes.insert(GaiaConstants::kOAuth1LoginScope); scopes.insert(GaiaConstants::kOAuth1LoginScope);
...@@ -92,12 +74,12 @@ void TokenHandleFetcher::RequestAccessToken(const std::string& user_email) { ...@@ -92,12 +74,12 @@ void TokenHandleFetcher::RequestAccessToken(const std::string& user_email) {
// owned by this object (thus destroyed when this object is destroyed) and // owned by this object (thus destroyed when this object is destroyed) and
// PrimaryAccountAccessTokenFetcher guarantees that it doesn't invoke its // PrimaryAccountAccessTokenFetcher guarantees that it doesn't invoke its
// callback after it is destroyed. // callback after it is destroyed.
access_token_fetcher_ = access_token_fetcher_ = std::make_unique<
std::make_unique<identity::PrimaryAccountAccessTokenFetcher>( identity::PrimaryAccountAccessTokenFetcher>(
kAccessTokenFetchId, identity_manager_, scopes, kAccessTokenFetchId, identity_manager_, scopes,
base::BindOnce(&TokenHandleFetcher::OnAccessTokenFetchComplete, base::BindOnce(&TokenHandleFetcher::OnAccessTokenFetchComplete,
base::Unretained(this)), base::Unretained(this)),
identity::PrimaryAccountAccessTokenFetcher::Mode::kImmediate); identity::PrimaryAccountAccessTokenFetcher::Mode::kWaitUntilAvailable);
} }
void TokenHandleFetcher::OnAccessTokenFetchComplete( void TokenHandleFetcher::OnAccessTokenFetchComplete(
......
...@@ -14,10 +14,10 @@ ...@@ -14,10 +14,10 @@
#include "components/account_id/account_id.h" #include "components/account_id/account_id.h"
#include "components/keyed_service/core/keyed_service_shutdown_notifier.h" #include "components/keyed_service/core/keyed_service_shutdown_notifier.h"
#include "google_apis/gaia/gaia_oauth_client.h" #include "google_apis/gaia/gaia_oauth_client.h"
#include "services/identity/public/cpp/identity_manager.h" #include "services/identity/public/cpp/primary_account_access_token_fetcher.h"
namespace identity { namespace identity {
class PrimaryAccountAccessTokenFetcher; class IdentityManager;
} }
class TokenHandleUtil; class TokenHandleUtil;
...@@ -28,8 +28,7 @@ class Profile; ...@@ -28,8 +28,7 @@ class Profile;
// an OAuth2 token available. If there is profile already loaded, then // an OAuth2 token available. If there is profile already loaded, then
// minting additional access token might be required. // minting additional access token might be required.
class TokenHandleFetcher : public gaia::GaiaOAuthClient::Delegate, class TokenHandleFetcher : public gaia::GaiaOAuthClient::Delegate {
public identity::IdentityManager::Observer {
public: public:
TokenHandleFetcher(TokenHandleUtil* util, const AccountId& account_id); TokenHandleFetcher(TokenHandleUtil* util, const AccountId& account_id);
~TokenHandleFetcher() override; ~TokenHandleFetcher() override;
...@@ -46,10 +45,6 @@ class TokenHandleFetcher : public gaia::GaiaOAuthClient::Delegate, ...@@ -46,10 +45,6 @@ class TokenHandleFetcher : public gaia::GaiaOAuthClient::Delegate,
void BackfillToken(Profile* profile, const TokenFetchingCallback& callback); void BackfillToken(Profile* profile, const TokenFetchingCallback& callback);
private: private:
// identity::IdentityManager::Observer override:
void OnRefreshTokenUpdatedForAccount(const AccountInfo& account_info,
bool is_valid) override;
// AccessTokenFetcher::TokenCallback for PrimaryAccountAccessTokenFetcher. // AccessTokenFetcher::TokenCallback for PrimaryAccountAccessTokenFetcher.
void OnAccessTokenFetchComplete(GoogleServiceAuthError error, void OnAccessTokenFetchComplete(GoogleServiceAuthError error,
identity::AccessTokenInfo token_info); identity::AccessTokenInfo token_info);
...@@ -60,7 +55,6 @@ class TokenHandleFetcher : public gaia::GaiaOAuthClient::Delegate, ...@@ -60,7 +55,6 @@ class TokenHandleFetcher : public gaia::GaiaOAuthClient::Delegate,
void OnGetTokenInfoResponse( void OnGetTokenInfoResponse(
std::unique_ptr<base::DictionaryValue> token_info) override; std::unique_ptr<base::DictionaryValue> token_info) override;
void RequestAccessToken(const std::string& user_email);
void FillForAccessToken(const std::string& access_token); void FillForAccessToken(const std::string& access_token);
// This is called before profile is detroyed. // This is called before profile is detroyed.
...@@ -70,8 +64,6 @@ class TokenHandleFetcher : public gaia::GaiaOAuthClient::Delegate, ...@@ -70,8 +64,6 @@ class TokenHandleFetcher : public gaia::GaiaOAuthClient::Delegate,
AccountId account_id_; AccountId account_id_;
identity::IdentityManager* identity_manager_ = nullptr; identity::IdentityManager* identity_manager_ = nullptr;
bool waiting_for_refresh_token_ = false;
std::string account_without_token_;
Profile* profile_ = nullptr; Profile* profile_ = nullptr;
base::TimeTicks tokeninfo_response_start_time_ = base::TimeTicks(); base::TimeTicks tokeninfo_response_start_time_ = base::TimeTicks();
TokenFetchingCallback callback_; TokenFetchingCallback callback_;
......
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