Skip to content

Commit

Permalink
Move |token_cache_| management to OAuth2AccessTokenManager
Browse files Browse the repository at this point in the history
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: Colin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#670045}
  • Loading branch information
jkim-julie authored and Commit Bot committed Jun 18, 2019
1 parent 55ba834 commit 0afbcd3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 49 deletions.
50 changes: 49 additions & 1 deletion google_apis/gaia/oauth2_access_token_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
22 changes: 21 additions & 1 deletion google_apis/gaia/oauth2_access_token_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -32,15 +35,32 @@ 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.
friend class OAuth2TokenService;

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_);

Expand Down
42 changes: 5 additions & 37 deletions google_apis/gaia/oauth2_token_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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() {
Expand Down
15 changes: 5 additions & 10 deletions google_apis/gaia/oauth2_token_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 0afbcd3

Please sign in to comment.