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

Migrate TokenHandleFetcher to the IdentityManager

Move away from OAuth2TokenService and SigninManagerBase by implementing
IdentityManager::Observer and using Identity Service's APIs only (i.e.
IdentityManager, PrimaryAccessTokenFetcher).

Bug: 905689
Change-Id: I68e971ddf8a23987abfdadb8e2daf7b17a4a6ddb
Reviewed-on: https://chromium-review.googlesource.com/c/1340315Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Commit-Queue: Mario Sanchez Prada <mario@igalia.com>
Cr-Commit-Position: refs/heads/master@{#611044}
parent 8730008c
...@@ -9,16 +9,16 @@ ...@@ -9,16 +9,16 @@
#include "chrome/browser/chromeos/login/signin/token_handle_util.h" #include "chrome/browser/chromeos/login/signin/token_handle_util.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h" #include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h" #include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/signin/signin_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 "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_manager.h"
#include "google_apis/gaia/gaia_constants.h" #include "google_apis/gaia/gaia_constants.h"
#include "services/identity/public/cpp/primary_account_access_token_fetcher.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"
namespace { namespace {
const int kMaxRetries = 3; const int kMaxRetries = 3;
const char kAccessTokenFetchId[] = "token_handle_fetcher";
class TokenHandleFetcherShutdownNotifierFactory class TokenHandleFetcherShutdownNotifierFactory
: public BrowserContextKeyedServiceShutdownNotifierFactory { : public BrowserContextKeyedServiceShutdownNotifierFactory {
...@@ -34,7 +34,7 @@ class TokenHandleFetcherShutdownNotifierFactory ...@@ -34,7 +34,7 @@ class TokenHandleFetcherShutdownNotifierFactory
TokenHandleFetcherShutdownNotifierFactory() TokenHandleFetcherShutdownNotifierFactory()
: BrowserContextKeyedServiceShutdownNotifierFactory( : BrowserContextKeyedServiceShutdownNotifierFactory(
"TokenHandleFetcher") { "TokenHandleFetcher") {
DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); DependsOn(IdentityManagerFactory::GetInstance());
} }
~TokenHandleFetcherShutdownNotifierFactory() override {} ~TokenHandleFetcherShutdownNotifierFactory() override {}
...@@ -45,13 +45,11 @@ class TokenHandleFetcherShutdownNotifierFactory ...@@ -45,13 +45,11 @@ class TokenHandleFetcherShutdownNotifierFactory
TokenHandleFetcher::TokenHandleFetcher(TokenHandleUtil* util, TokenHandleFetcher::TokenHandleFetcher(TokenHandleUtil* util,
const AccountId& account_id) const AccountId& account_id)
: OAuth2TokenService::Consumer("user_session_manager"), : 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_) if (waiting_for_refresh_token_)
token_service_->RemoveObserver(this); identity_manager_->RemoveObserver(this);
} }
void TokenHandleFetcher::BackfillToken(Profile* profile, void TokenHandleFetcher::BackfillToken(Profile* profile,
...@@ -59,11 +57,9 @@ void TokenHandleFetcher::BackfillToken(Profile* profile, ...@@ -59,11 +57,9 @@ void TokenHandleFetcher::BackfillToken(Profile* profile,
profile_ = profile; profile_ = profile;
callback_ = callback; callback_ = callback;
token_service_ = ProfileOAuth2TokenServiceFactory::GetForProfile(profile); identity_manager_ = IdentityManagerFactory::GetForProfile(profile);
SigninManagerBase* signin_manager = const std::string user_email = identity_manager_->GetPrimaryAccountId();
SigninManagerFactory::GetForProfile(profile); if (!identity_manager_->HasAccountWithRefreshToken(user_email)) {
const std::string user_email = signin_manager->GetAuthenticatedAccountId();
if (!token_service_->RefreshTokenIsAvailable(user_email)) {
account_without_token_ = user_email; account_without_token_ = user_email;
profile_shutdown_notification_ = profile_shutdown_notification_ =
TokenHandleFetcherShutdownNotifierFactory::GetInstance() TokenHandleFetcherShutdownNotifierFactory::GetInstance()
...@@ -71,43 +67,52 @@ void TokenHandleFetcher::BackfillToken(Profile* profile, ...@@ -71,43 +67,52 @@ void TokenHandleFetcher::BackfillToken(Profile* profile,
->Subscribe(base::Bind(&TokenHandleFetcher::OnProfileDestroyed, ->Subscribe(base::Bind(&TokenHandleFetcher::OnProfileDestroyed,
base::Unretained(this))); base::Unretained(this)));
token_service_->AddObserver(this); identity_manager_->AddObserver(this);
waiting_for_refresh_token_ = true; waiting_for_refresh_token_ = true;
return; return;
} }
RequestAccessToken(user_email); RequestAccessToken(user_email);
} }
void TokenHandleFetcher::OnRefreshTokenAvailable( void TokenHandleFetcher::OnRefreshTokenUpdatedForAccount(
const std::string& user_email) { const AccountInfo& account_info,
if (account_without_token_ != user_email) bool is_valid) {
if (account_without_token_ != account_info.email)
return; return;
waiting_for_refresh_token_ = false; waiting_for_refresh_token_ = false;
token_service_->RemoveObserver(this); identity_manager_->RemoveObserver(this);
RequestAccessToken(user_email); RequestAccessToken(account_info.email);
} }
void TokenHandleFetcher::RequestAccessToken(const std::string& user_email) { void TokenHandleFetcher::RequestAccessToken(const std::string& user_email) {
OAuth2TokenService::ScopeSet scopes; identity::ScopeSet scopes;
scopes.insert(GaiaConstants::kOAuth1LoginScope); scopes.insert(GaiaConstants::kOAuth1LoginScope);
oauth2_access_token_request_ =
token_service_->StartRequest(user_email, scopes, this);
}
void TokenHandleFetcher::OnGetTokenSuccess( // We can use base::Unretained(this) below because |access_token_fetcher_| is
const OAuth2TokenService::Request* request, // owned by this object (thus destroyed when this object is destroyed) and
const OAuth2AccessTokenConsumer::TokenResponse& token_response) { // PrimaryAccountAccessTokenFetcher guarantees that it doesn't invoke its
oauth2_access_token_request_.reset(); // callback after it is destroyed.
FillForAccessToken(token_response.access_token); access_token_fetcher_ =
std::make_unique<identity::PrimaryAccountAccessTokenFetcher>(
kAccessTokenFetchId, identity_manager_, scopes,
base::BindOnce(&TokenHandleFetcher::OnAccessTokenFetchComplete,
base::Unretained(this)),
identity::PrimaryAccountAccessTokenFetcher::Mode::kImmediate);
} }
void TokenHandleFetcher::OnGetTokenFailure( void TokenHandleFetcher::OnAccessTokenFetchComplete(
const OAuth2TokenService::Request* request, GoogleServiceAuthError error,
const GoogleServiceAuthError& error) { identity::AccessTokenInfo token_info) {
oauth2_access_token_request_.reset(); access_token_fetcher_.reset();
if (error.state() != GoogleServiceAuthError::NONE) {
LOG(ERROR) << "Could not get access token to backfill token handler" LOG(ERROR) << "Could not get access token to backfill token handler"
<< error.ToString(); << error.ToString();
callback_.Run(account_id_, false); callback_.Run(account_id_, false);
return;
}
FillForAccessToken(token_info.token);
} }
void TokenHandleFetcher::FillForNewUser(const std::string& access_token, void TokenHandleFetcher::FillForNewUser(const std::string& access_token,
......
...@@ -14,7 +14,11 @@ ...@@ -14,7 +14,11 @@
#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 "google_apis/gaia/oauth2_token_service.h" #include "services/identity/public/cpp/identity_manager.h"
namespace identity {
class PrimaryAccountAccessTokenFetcher;
}
class TokenHandleUtil; class TokenHandleUtil;
class Profile; class Profile;
...@@ -25,8 +29,7 @@ class Profile; ...@@ -25,8 +29,7 @@ class Profile;
// 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 OAuth2TokenService::Consumer, public identity::IdentityManager::Observer {
public OAuth2TokenService::Observer {
public: public:
TokenHandleFetcher(TokenHandleUtil* util, const AccountId& account_id); TokenHandleFetcher(TokenHandleUtil* util, const AccountId& account_id);
~TokenHandleFetcher() override; ~TokenHandleFetcher() override;
...@@ -43,15 +46,13 @@ class TokenHandleFetcher : public gaia::GaiaOAuthClient::Delegate, ...@@ -43,15 +46,13 @@ class TokenHandleFetcher : public gaia::GaiaOAuthClient::Delegate,
void BackfillToken(Profile* profile, const TokenFetchingCallback& callback); void BackfillToken(Profile* profile, const TokenFetchingCallback& callback);
private: private:
// OAuth2TokenService::Observer override: // identity::IdentityManager::Observer override:
void OnRefreshTokenAvailable(const std::string& user_email) override; void OnRefreshTokenUpdatedForAccount(const AccountInfo& account_info,
bool is_valid) override;
// OAuth2TokenService::Consumer overrides: // AccessTokenFetcher::TokenCallback for PrimaryAccountAccessTokenFetcher.
void OnGetTokenSuccess( void OnAccessTokenFetchComplete(GoogleServiceAuthError error,
const OAuth2TokenService::Request* request, identity::AccessTokenInfo token_info);
const OAuth2AccessTokenConsumer::TokenResponse& token_response) override;
void OnGetTokenFailure(const OAuth2TokenService::Request* request,
const GoogleServiceAuthError& error) override;
// GaiaOAuthClient::Delegate overrides: // GaiaOAuthClient::Delegate overrides:
void OnOAuthError() override; void OnOAuthError() override;
...@@ -67,7 +68,7 @@ class TokenHandleFetcher : public gaia::GaiaOAuthClient::Delegate, ...@@ -67,7 +68,7 @@ class TokenHandleFetcher : public gaia::GaiaOAuthClient::Delegate,
TokenHandleUtil* token_handle_util_ = nullptr; TokenHandleUtil* token_handle_util_ = nullptr;
AccountId account_id_; AccountId account_id_;
OAuth2TokenService* token_service_ = nullptr; identity::IdentityManager* identity_manager_ = nullptr;
bool waiting_for_refresh_token_ = false; bool waiting_for_refresh_token_ = false;
std::string account_without_token_; std::string account_without_token_;
...@@ -75,7 +76,8 @@ class TokenHandleFetcher : public gaia::GaiaOAuthClient::Delegate, ...@@ -75,7 +76,8 @@ class TokenHandleFetcher : public gaia::GaiaOAuthClient::Delegate,
base::TimeTicks tokeninfo_response_start_time_ = base::TimeTicks(); base::TimeTicks tokeninfo_response_start_time_ = base::TimeTicks();
TokenFetchingCallback callback_; TokenFetchingCallback callback_;
std::unique_ptr<gaia::GaiaOAuthClient> gaia_client_; std::unique_ptr<gaia::GaiaOAuthClient> gaia_client_;
std::unique_ptr<OAuth2TokenService::Request> oauth2_access_token_request_; std::unique_ptr<identity::PrimaryAccountAccessTokenFetcher>
access_token_fetcher_;
std::unique_ptr<KeyedServiceShutdownNotifier::Subscription> std::unique_ptr<KeyedServiceShutdownNotifier::Subscription>
profile_shutdown_notification_; profile_shutdown_notification_;
......
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