Commit 0afbcd35 authored by Julie Jeongeun Kim's avatar Julie Jeongeun Kim Committed by Commit Bot

Move |token_cache_| management to OAuth2AccessTokenManager

This CL is a part of moving access token management to
OAuth2AccessTokenManager. It moves APIs which access to
|token_cache_| in OAuth2AccessTokenManager to
OAuth2AccessTokenManager from OAuth2TokenService.

Bug: 967598
Change-Id: If07c87263571871ab380feb8e1be2975c2133f6a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1663932
Commit-Queue: Julie Jeongeun Kim <jkim@igalia.com>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#670045}
parent 55ba8340
...@@ -6,7 +6,11 @@ ...@@ -6,7 +6,11 @@
#include "base/time/time.h" #include "base/time/time.h"
OAuth2AccessTokenManager::OAuth2AccessTokenManager() = default; OAuth2AccessTokenManager::OAuth2AccessTokenManager(
OAuth2TokenService* token_service)
: token_service_(token_service) {
DCHECK(token_service_);
}
OAuth2AccessTokenManager::~OAuth2AccessTokenManager() = default; OAuth2AccessTokenManager::~OAuth2AccessTokenManager() = default;
...@@ -35,3 +39,47 @@ OAuth2AccessTokenManager::GetCachedTokenResponse( ...@@ -35,3 +39,47 @@ OAuth2AccessTokenManager::GetCachedTokenResponse(
} }
return &token_iterator->second; return &token_iterator->second;
} }
void OAuth2AccessTokenManager::ClearCache() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (const auto& entry : token_cache_) {
for (auto& observer : token_service_->GetDiagnicsObservers())
observer.OnAccessTokenRemoved(entry.first.account_id, entry.first.scopes);
}
token_cache_.clear();
}
void OAuth2AccessTokenManager::ClearCacheForAccount(
const CoreAccountId& account_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (OAuth2TokenService::TokenCache::iterator iter = token_cache_.begin();
iter != token_cache_.end();
/* iter incremented in body */) {
if (iter->first.account_id == account_id) {
for (auto& observer : token_service_->GetDiagnicsObservers())
observer.OnAccessTokenRemoved(account_id, iter->first.scopes);
token_cache_.erase(iter++);
} else {
++iter;
}
}
}
bool OAuth2AccessTokenManager::RemoveCachedTokenResponse(
const OAuth2TokenService::RequestParameters& request_parameters,
const std::string& token_to_remove) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
OAuth2TokenService::OAuth2TokenService::TokenCache::iterator token_iterator =
token_cache_.find(request_parameters);
if (token_iterator != token_cache_.end() &&
token_iterator->second.access_token == token_to_remove) {
for (auto& observer : token_service_->GetDiagnicsObservers()) {
observer.OnAccessTokenRemoved(request_parameters.account_id,
request_parameters.scopes);
}
token_cache_.erase(token_iterator);
return true;
}
return false;
}
...@@ -15,7 +15,10 @@ ...@@ -15,7 +15,10 @@
// Class that manages requests for OAuth2 access tokens. // Class that manages requests for OAuth2 access tokens.
class OAuth2AccessTokenManager { class OAuth2AccessTokenManager {
public: public:
OAuth2AccessTokenManager(); // TODO(https://crbug.com/967598): Remove |token_service| parameter once
// OAuth2AccessTokenManager fully manages access tokens independently of
// OAuth2TokenService.
explicit OAuth2AccessTokenManager(OAuth2TokenService* token_service);
virtual ~OAuth2AccessTokenManager(); virtual ~OAuth2AccessTokenManager();
// Add a new entry to the cache. // Add a new entry to the cache.
...@@ -32,6 +35,14 @@ class OAuth2AccessTokenManager { ...@@ -32,6 +35,14 @@ class OAuth2AccessTokenManager {
const OAuth2AccessTokenConsumer::TokenResponse* GetCachedTokenResponse( const OAuth2AccessTokenConsumer::TokenResponse* GetCachedTokenResponse(
const OAuth2TokenService::RequestParameters& client_scopes); const OAuth2TokenService::RequestParameters& client_scopes);
// Clears the internal token cache.
void ClearCache();
// Clears all of the tokens belonging to |account_id| from the internal token
// cache. It does not matter what other parameters, like |client_id| were
// used to request the tokens.
void ClearCacheForAccount(const CoreAccountId& account_id);
private: private:
// TODO(https://crbug.com/967598): Remove this once |token_cache_| management // TODO(https://crbug.com/967598): Remove this once |token_cache_| management
// is moved to OAuth2AccessTokenManager. // is moved to OAuth2AccessTokenManager.
...@@ -39,8 +50,17 @@ class OAuth2AccessTokenManager { ...@@ -39,8 +50,17 @@ class OAuth2AccessTokenManager {
OAuth2TokenService::TokenCache& token_cache() { return token_cache_; } OAuth2TokenService::TokenCache& token_cache() { return token_cache_; }
// Removes an access token for the given set of scopes from the cache.
// Returns true if the entry was removed, otherwise false.
bool RemoveCachedTokenResponse(
const OAuth2TokenService::RequestParameters& client_scopes,
const std::string& token_to_remove);
// The cache of currently valid tokens. // The cache of currently valid tokens.
OAuth2TokenService::TokenCache token_cache_; OAuth2TokenService::TokenCache token_cache_;
// TODO(https://crbug.com/967598): Remove this once OAuth2AccessTokenManager
// fully manages access tokens independently of OAuth2TokenService.
OAuth2TokenService* token_service_;
SEQUENCE_CHECKER(sequence_checker_); SEQUENCE_CHECKER(sequence_checker_);
......
...@@ -394,7 +394,7 @@ OAuth2TokenService::OAuth2TokenService( ...@@ -394,7 +394,7 @@ OAuth2TokenService::OAuth2TokenService(
: delegate_(std::move(delegate)), all_credentials_loaded_(false) { : delegate_(std::move(delegate)), all_credentials_loaded_(false) {
DCHECK(delegate_); DCHECK(delegate_);
AddObserver(this); AddObserver(this);
token_manager_ = std::make_unique<OAuth2AccessTokenManager>(); token_manager_ = std::make_unique<OAuth2AccessTokenManager>(this);
} }
OAuth2TokenService::~OAuth2TokenService() { OAuth2TokenService::~OAuth2TokenService() {
...@@ -657,8 +657,8 @@ void OAuth2TokenService::InvalidateAccessTokenImpl( ...@@ -657,8 +657,8 @@ void OAuth2TokenService::InvalidateAccessTokenImpl(
const ScopeSet& scopes, const ScopeSet& scopes,
const std::string& access_token) { const std::string& access_token) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
RemoveCachedTokenResponse(RequestParameters(client_id, account_id, scopes), token_manager_->RemoveCachedTokenResponse(
access_token); RequestParameters(client_id, account_id, scopes), access_token);
delegate_->InvalidateAccessToken(account_id, client_id, scopes, access_token); delegate_->InvalidateAccessToken(account_id, client_id, scopes, access_token);
} }
...@@ -727,23 +727,6 @@ OAuth2TokenService::GetCachedTokenResponse( ...@@ -727,23 +727,6 @@ OAuth2TokenService::GetCachedTokenResponse(
return token_manager_->GetCachedTokenResponse(request_parameters); return token_manager_->GetCachedTokenResponse(request_parameters);
} }
bool OAuth2TokenService::RemoveCachedTokenResponse(
const RequestParameters& request_parameters,
const std::string& token_to_remove) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
TokenCache::iterator token_iterator = token_cache().find(request_parameters);
if (token_iterator != token_cache().end() &&
token_iterator->second.access_token == token_to_remove) {
for (auto& observer : diagnostics_observer_list_) {
observer.OnAccessTokenRemoved(request_parameters.account_id,
request_parameters.scopes);
}
token_cache().erase(token_iterator);
return true;
}
return false;
}
void OAuth2TokenService::OnRefreshTokensLoaded() { void OAuth2TokenService::OnRefreshTokensLoaded() {
all_credentials_loaded_ = true; all_credentials_loaded_ = true;
} }
...@@ -765,27 +748,12 @@ void OAuth2TokenService::RegisterTokenResponse( ...@@ -765,27 +748,12 @@ void OAuth2TokenService::RegisterTokenResponse(
void OAuth2TokenService::ClearCache() { void OAuth2TokenService::ClearCache() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (const auto& entry : token_cache()) { token_manager_->ClearCache();
for (auto& observer : diagnostics_observer_list_)
observer.OnAccessTokenRemoved(entry.first.account_id, entry.first.scopes);
}
token_cache().clear();
} }
void OAuth2TokenService::ClearCacheForAccount(const CoreAccountId& account_id) { void OAuth2TokenService::ClearCacheForAccount(const CoreAccountId& account_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (TokenCache::iterator iter = token_cache().begin(); token_manager_->ClearCacheForAccount(account_id);
iter != token_cache().end();
/* iter incremented in body */) {
if (iter->first.account_id == account_id) {
for (auto& observer : diagnostics_observer_list_)
observer.OnAccessTokenRemoved(account_id, iter->first.scopes);
token_cache().erase(iter++);
} else {
++iter;
}
}
} }
void OAuth2TokenService::CancelAllRequests() { void OAuth2TokenService::CancelAllRequests() {
......
...@@ -259,6 +259,11 @@ class OAuth2TokenService : public OAuth2TokenServiceObserver { ...@@ -259,6 +259,11 @@ class OAuth2TokenService : public OAuth2TokenServiceObserver {
// OAuth2TokenServiceTest. // OAuth2TokenServiceTest.
OAuth2TokenService::TokenCache& token_cache(); OAuth2TokenService::TokenCache& token_cache();
const base::ObserverList<DiagnosticsObserver, true>::Unchecked&
GetDiagnicsObservers() {
return diagnostics_observer_list_;
}
protected: protected:
// Implements a cancelable |OAuth2TokenService::Request|, which should be // Implements a cancelable |OAuth2TokenService::Request|, which should be
// operated on the UI thread. // operated on the UI thread.
...@@ -344,11 +349,6 @@ class OAuth2TokenService : public OAuth2TokenServiceObserver { ...@@ -344,11 +349,6 @@ class OAuth2TokenService : public OAuth2TokenServiceObserver {
const ScopeSet& scopes, const ScopeSet& scopes,
const std::string& access_token); const std::string& access_token);
const base::ObserverList<DiagnosticsObserver, true>::Unchecked&
GetDiagnicsObservers() {
return diagnostics_observer_list_;
}
private: private:
class Fetcher; class Fetcher;
friend class Fetcher; friend class Fetcher;
...@@ -382,11 +382,6 @@ class OAuth2TokenService : public OAuth2TokenServiceObserver { ...@@ -382,11 +382,6 @@ class OAuth2TokenService : public OAuth2TokenServiceObserver {
const OAuth2AccessTokenConsumer::TokenResponse* GetCachedTokenResponse( const OAuth2AccessTokenConsumer::TokenResponse* GetCachedTokenResponse(
const RequestParameters& client_scopes); const RequestParameters& client_scopes);
// Removes an access token for the given set of scopes from the cache.
// Returns true if the entry was removed, otherwise false.
bool RemoveCachedTokenResponse(const RequestParameters& client_scopes,
const std::string& token_to_remove);
// Called when |fetcher| finishes fetching. // Called when |fetcher| finishes fetching.
void OnFetchComplete(Fetcher* fetcher); void OnFetchComplete(Fetcher* fetcher);
......
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