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 @@
#include "base/time/time.h"
OAuth2AccessTokenManager::OAuth2AccessTokenManager() = default;
OAuth2AccessTokenManager::OAuth2AccessTokenManager(
OAuth2TokenService* token_service)
: token_service_(token_service) {
DCHECK(token_service_);
}
OAuth2AccessTokenManager::~OAuth2AccessTokenManager() = default;
......@@ -35,3 +39,47 @@ OAuth2AccessTokenManager::GetCachedTokenResponse(
}
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 @@
// Class that manages requests for OAuth2 access tokens.
class OAuth2AccessTokenManager {
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();
// Add a new entry to the cache.
......@@ -32,6 +35,14 @@ class OAuth2AccessTokenManager {
const OAuth2AccessTokenConsumer::TokenResponse* GetCachedTokenResponse(
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:
// TODO(https://crbug.com/967598): Remove this once |token_cache_| management
// is moved to OAuth2AccessTokenManager.
......@@ -39,8 +50,17 @@ class OAuth2AccessTokenManager {
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.
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_);
......
......@@ -394,7 +394,7 @@ OAuth2TokenService::OAuth2TokenService(
: delegate_(std::move(delegate)), all_credentials_loaded_(false) {
DCHECK(delegate_);
AddObserver(this);
token_manager_ = std::make_unique<OAuth2AccessTokenManager>();
token_manager_ = std::make_unique<OAuth2AccessTokenManager>(this);
}
OAuth2TokenService::~OAuth2TokenService() {
......@@ -657,8 +657,8 @@ void OAuth2TokenService::InvalidateAccessTokenImpl(
const ScopeSet& scopes,
const std::string& access_token) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
RemoveCachedTokenResponse(RequestParameters(client_id, account_id, scopes),
access_token);
token_manager_->RemoveCachedTokenResponse(
RequestParameters(client_id, account_id, scopes), access_token);
delegate_->InvalidateAccessToken(account_id, client_id, scopes, access_token);
}
......@@ -727,23 +727,6 @@ OAuth2TokenService::GetCachedTokenResponse(
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() {
all_credentials_loaded_ = true;
}
......@@ -765,27 +748,12 @@ void OAuth2TokenService::RegisterTokenResponse(
void OAuth2TokenService::ClearCache() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (const auto& entry : token_cache()) {
for (auto& observer : diagnostics_observer_list_)
observer.OnAccessTokenRemoved(entry.first.account_id, entry.first.scopes);
}
token_cache().clear();
token_manager_->ClearCache();
}
void OAuth2TokenService::ClearCacheForAccount(const CoreAccountId& account_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (TokenCache::iterator iter = token_cache().begin();
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;
}
}
token_manager_->ClearCacheForAccount(account_id);
}
void OAuth2TokenService::CancelAllRequests() {
......
......@@ -259,6 +259,11 @@ class OAuth2TokenService : public OAuth2TokenServiceObserver {
// OAuth2TokenServiceTest.
OAuth2TokenService::TokenCache& token_cache();
const base::ObserverList<DiagnosticsObserver, true>::Unchecked&
GetDiagnicsObservers() {
return diagnostics_observer_list_;
}
protected:
// Implements a cancelable |OAuth2TokenService::Request|, which should be
// operated on the UI thread.
......@@ -344,11 +349,6 @@ class OAuth2TokenService : public OAuth2TokenServiceObserver {
const ScopeSet& scopes,
const std::string& access_token);
const base::ObserverList<DiagnosticsObserver, true>::Unchecked&
GetDiagnicsObservers() {
return diagnostics_observer_list_;
}
private:
class Fetcher;
friend class Fetcher;
......@@ -382,11 +382,6 @@ class OAuth2TokenService : public OAuth2TokenServiceObserver {
const OAuth2AccessTokenConsumer::TokenResponse* GetCachedTokenResponse(
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.
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