#include "google_apis/gaia/oauth2_access_token_manager.h"
#include <memory>
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_macros.h"
#include "base/observer_list.h"
#include "base/rand_util.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "base/types/expected.h"
#include "google_apis/gaia/core_account_id.h"
#include "google_apis/gaia/gaia_urls.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "google_apis/gaia/oauth2_access_token_fetcher.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace {
void RecordOAuth2TokenFetchResult(GoogleServiceAuthError::State state) { … }
}
int OAuth2AccessTokenManager::max_fetch_retry_num_ = …;
OAuth2AccessTokenManager::Delegate::Delegate() = default;
OAuth2AccessTokenManager::Delegate::~Delegate() = default;
scoped_refptr<network::SharedURLLoaderFactory>
OAuth2AccessTokenManager::Delegate::GetURLLoaderFactory() const { … }
bool OAuth2AccessTokenManager::Delegate::HandleAccessTokenFetch(
RequestImpl* request,
const CoreAccountId& account_id,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const std::string& client_id,
const std::string& client_secret,
const ScopeSet& scopes) { … }
OAuth2AccessTokenManager::Request::Request() { … }
OAuth2AccessTokenManager::Request::~Request() { … }
OAuth2AccessTokenManager::Consumer::Consumer(const std::string& id) : … { … }
OAuth2AccessTokenManager::Consumer::~Consumer() { … }
OAuth2AccessTokenManager::RequestImpl::RequestImpl(
const CoreAccountId& account_id,
Consumer* consumer)
: … { … }
OAuth2AccessTokenManager::RequestImpl::~RequestImpl() { … }
CoreAccountId OAuth2AccessTokenManager::RequestImpl::GetAccountId() const { … }
std::string OAuth2AccessTokenManager::RequestImpl::GetConsumerId() const { … }
void OAuth2AccessTokenManager::RequestImpl::InformConsumer(
const GoogleServiceAuthError& error,
const OAuth2AccessTokenConsumer::TokenResponse& token_response) { … }
OAuth2AccessTokenManager::RequestParameters::RequestParameters(
const std::string& client_id,
const CoreAccountId& account_id,
const ScopeSet& scopes)
: … { … }
OAuth2AccessTokenManager::RequestParameters::RequestParameters(
const RequestParameters& other) = default;
OAuth2AccessTokenManager::RequestParameters::~RequestParameters() { … }
bool OAuth2AccessTokenManager::RequestParameters::operator<(
const RequestParameters& p) const { … }
class OAuth2AccessTokenManager::Fetcher : public OAuth2AccessTokenConsumer { … };
std::unique_ptr<OAuth2AccessTokenManager::Fetcher>
OAuth2AccessTokenManager::Fetcher::CreateAndStart(
OAuth2AccessTokenManager* oauth2_access_token_manager,
const CoreAccountId& account_id,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const std::string& client_id,
const std::string& client_secret,
const ScopeSet& scopes,
const std::string& consumer_name,
base::WeakPtr<RequestImpl> waiting_request) { … }
OAuth2AccessTokenManager::Fetcher::Fetcher(
OAuth2AccessTokenManager* oauth2_access_token_manager,
const CoreAccountId& account_id,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const std::string& client_id,
const std::string& client_secret,
const ScopeSet& scopes,
const std::string& consumer_name,
base::WeakPtr<RequestImpl> waiting_request)
: … { … }
OAuth2AccessTokenManager::Fetcher::~Fetcher() { … }
void OAuth2AccessTokenManager::Fetcher::Start() { … }
void OAuth2AccessTokenManager::Fetcher::OnGetTokenSuccess(
const OAuth2AccessTokenConsumer::TokenResponse& token_response) { … }
void OAuth2AccessTokenManager::Fetcher::OnGetTokenFailure(
const GoogleServiceAuthError& error) { … }
std::string OAuth2AccessTokenManager::Fetcher::GetConsumerName() const { … }
int64_t
OAuth2AccessTokenManager::Fetcher::ComputeExponentialBackOffMilliseconds(
int retry_num) { … }
bool OAuth2AccessTokenManager::Fetcher::RetryIfPossible(
const GoogleServiceAuthError& error) { … }
void OAuth2AccessTokenManager::Fetcher::InformFetchCompleted(
base::expected<OAuth2AccessTokenConsumer::TokenResponse,
GoogleServiceAuthError> response) { … }
std::vector<base::WeakPtr<OAuth2AccessTokenManager::RequestImpl>>
OAuth2AccessTokenManager::Fetcher::TakeWaitingRequests() { … }
void OAuth2AccessTokenManager::Fetcher::AddWaitingRequest(
base::WeakPtr<RequestImpl> waiting_request) { … }
size_t OAuth2AccessTokenManager::Fetcher::GetWaitingRequestCount() const { … }
OAuth2AccessTokenManager::OAuth2AccessTokenManager(
OAuth2AccessTokenManager::Delegate* delegate)
: … { … }
OAuth2AccessTokenManager::~OAuth2AccessTokenManager() { … }
OAuth2AccessTokenManager::Delegate* OAuth2AccessTokenManager::GetDelegate() { … }
const OAuth2AccessTokenManager::Delegate*
OAuth2AccessTokenManager::GetDelegate() const { … }
void OAuth2AccessTokenManager::AddDiagnosticsObserver(
DiagnosticsObserver* observer) { … }
void OAuth2AccessTokenManager::RemoveDiagnosticsObserver(
DiagnosticsObserver* observer) { … }
std::unique_ptr<OAuth2AccessTokenManager::Request>
OAuth2AccessTokenManager::StartRequest(const CoreAccountId& account_id,
const ScopeSet& scopes,
Consumer* consumer) { … }
std::unique_ptr<OAuth2AccessTokenManager::Request>
OAuth2AccessTokenManager::StartRequestForClient(
const CoreAccountId& account_id,
const std::string& client_id,
const std::string& client_secret,
const ScopeSet& scopes,
Consumer* consumer) { … }
std::unique_ptr<OAuth2AccessTokenManager::Request>
OAuth2AccessTokenManager::StartRequestWithContext(
const CoreAccountId& account_id,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const ScopeSet& scopes,
Consumer* consumer) { … }
void OAuth2AccessTokenManager::FetchOAuth2Token(
RequestImpl* request,
const CoreAccountId& account_id,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const std::string& client_id,
const std::string& client_secret,
const std::string& consumer_name,
const ScopeSet& scopes) { … }
const OAuth2AccessTokenConsumer::TokenResponse*
OAuth2AccessTokenManager::GetCachedTokenResponse(
const RequestParameters& request_parameters) { … }
void OAuth2AccessTokenManager::ClearCacheForAccount(
const CoreAccountId& account_id) { … }
void OAuth2AccessTokenManager::CancelAllRequests(
const GoogleServiceAuthError& error) { … }
void OAuth2AccessTokenManager::CancelRequestsForAccount(
const CoreAccountId& account_id,
const GoogleServiceAuthError& error) { … }
void OAuth2AccessTokenManager::CancelRequestIfMatch(
const GoogleServiceAuthError& error,
base::RepeatingCallback<bool(const RequestParameters&)> match_request) { … }
void OAuth2AccessTokenManager::InvalidateAccessToken(
const CoreAccountId& account_id,
const ScopeSet& scopes,
const std::string& access_token) { … }
void OAuth2AccessTokenManager::InvalidateAccessTokenImpl(
const CoreAccountId& account_id,
const std::string& client_id,
const ScopeSet& scopes,
const std::string& access_token) { … }
void OAuth2AccessTokenManager::
set_max_authorization_token_fetch_retries_for_testing(int max_retries) { … }
size_t OAuth2AccessTokenManager::GetNumPendingRequestsForTesting(
const std::string& client_id,
const CoreAccountId& account_id,
const ScopeSet& scopes) const { … }
const base::ObserverList<OAuth2AccessTokenManager::DiagnosticsObserver,
true>::Unchecked&
OAuth2AccessTokenManager::GetDiagnosticsObserversForTesting() { … }
std::unique_ptr<OAuth2AccessTokenFetcher>
OAuth2AccessTokenManager::CreateAccessTokenFetcher(
const CoreAccountId& account_id,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
OAuth2AccessTokenConsumer* consumer,
const std::string& token_binding_challenge) { … }
std::unique_ptr<OAuth2AccessTokenManager::Request>
OAuth2AccessTokenManager::StartRequestForClientWithContext(
const CoreAccountId& account_id,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const std::string& client_id,
const std::string& client_secret,
const ScopeSet& scopes,
Consumer* consumer) { … }
void OAuth2AccessTokenManager::InformConsumerWithCachedTokenResponse(
const OAuth2AccessTokenConsumer::TokenResponse* cache_token_response,
RequestImpl* request,
const RequestParameters& request_parameters) { … }
bool OAuth2AccessTokenManager::RemoveCachedTokenResponse(
const RequestParameters& request_parameters,
const std::string& token_to_remove) { … }
void OAuth2AccessTokenManager::OnFetchComplete(
const std::string& client_id,
const CoreAccountId& account_id,
const ScopeSet& scopes,
base::expected<OAuth2AccessTokenConsumer::TokenResponse,
GoogleServiceAuthError> response) { … }
void OAuth2AccessTokenManager::ProcessOnFetchComplete(
const RequestParameters& request_parameters,
base::expected<OAuth2AccessTokenConsumer::TokenResponse,
GoogleServiceAuthError> response,
const std::vector<base::WeakPtr<OAuth2AccessTokenManager::RequestImpl>>&
waiting_requests) { … }