Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1950)

Unified Diff: google_apis/gaia/oauth2_token_service.h

Issue 1143323005: Refactor AO2TS to make it easier to componentize. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: works for all platforms commit e75a498951318d4deb65d40ce8b2def44cd5abc0 Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: google_apis/gaia/oauth2_token_service.h
diff --git a/google_apis/gaia/oauth2_token_service.h b/google_apis/gaia/oauth2_token_service.h
index 4decbf4c2a0eb48af455ed0b334fdd0e9c0c1cc8..879570f469c3468aeb8a16e99caf9a6686c85e83 100644
--- a/google_apis/gaia/oauth2_token_service.h
+++ b/google_apis/gaia/oauth2_token_service.h
@@ -26,6 +26,7 @@ class URLRequestContextGetter;
class GoogleServiceAuthError;
class OAuth2AccessTokenFetcher;
+class OAuth2TokenServiceDelegate;
// Abstract base class for a service that fetches and caches OAuth2 access
// tokens. Concrete subclasses should implement GetRefreshToken to return
@@ -129,7 +130,7 @@ class OAuth2TokenService : public base::NonThreadSafe {
const ScopeSet& scopes) = 0;
};
- OAuth2TokenService();
+ OAuth2TokenService(OAuth2TokenServiceDelegate* delegate);
xiyuan 2015/06/26 18:15:53 nit: explicit and use scoped_ptr to make ownership
gogerald1 2015/07/01 17:58:41 Acknowledged.
virtual ~OAuth2TokenService();
// Add or remove observers of this token service.
@@ -172,31 +173,30 @@ class OAuth2TokenService : public base::NonThreadSafe {
// Lists account IDs of all accounts with a refresh token maintained by this
// instance.
- virtual std::vector<std::string> GetAccounts();
+ std::vector<std::string> GetAccounts() const;
// Returns true if a refresh token exists for |account_id|. If false, calls to
// |StartRequest| will result in a Consumer::OnGetTokenFailure callback.
- virtual bool RefreshTokenIsAvailable(const std::string& account_id) const = 0;
+ bool RefreshTokenIsAvailable(const std::string& account_id) const;
+
+ void RevokeAllCredentials();
Mattias Nissler (ping if slow) 2015/06/29 08:20:06 Would be good to put a comment stating what this d
gogerald1 2015/07/01 17:58:41 Done.
// Mark an OAuth2 |access_token| issued for |account_id| and |scopes| as
// invalid. This should be done if the token was received from this class,
// but was not accepted by the server (e.g., the server returned
// 401 Unauthorized). The token will be removed from the cache for the given
// scopes.
- void InvalidateToken(const std::string& account_id,
- const ScopeSet& scopes,
- const std::string& access_token);
+ void InvalidateAccessToken(const std::string& account_id,
+ const ScopeSet& scopes,
+ const std::string& access_token);
// Like |InvalidateToken| except is uses |client_id| to identity OAuth2 client
// app that issued the request instead of Chrome's default values.
- void InvalidateTokenForClient(const std::string& account_id,
- const std::string& client_id,
- const ScopeSet& scopes,
- const std::string& access_token);
+ void InvalidateAccessTokenForClient(const std::string& account_id,
+ const std::string& client_id,
+ const ScopeSet& scopes,
+ const std::string& access_token);
-
- // Return the current number of entries in the cache.
- int cache_size_for_testing() const;
void set_max_authorization_token_fetch_retries_for_testing(int max_retries);
// Returns the current number of pending fetchers matching given params.
size_t GetNumPendingRequestsForTesting(
@@ -204,6 +204,8 @@ class OAuth2TokenService : public base::NonThreadSafe {
const std::string& account_id,
const ScopeSet& scopes) const;
+ OAuth2TokenServiceDelegate* GetDelegate();
Mattias Nissler (ping if slow) 2015/06/29 08:20:06 From an architectural perspective, it seems wrong
msarda 2015/06/29 08:49:44 I agree that from architectural perspective having
Mattias Nissler (ping if slow) 2015/06/29 09:01:29 Who calls that logic to reload credentials? If tha
Roger Tawa OOO till Jul 10th 2015/06/30 19:55:30 There are a few types of token services in chrome,
Mattias Nissler (ping if slow) 2015/06/30 20:42:51 That's awesome, and I'm very happy to hear this. D
gogerald1 2015/07/01 17:58:41 Acknowledged.
+
protected:
// Implements a cancelable |OAuth2TokenService::Request|, which should be
// operated on the UI thread.
@@ -232,20 +234,9 @@ class OAuth2TokenService : public base::NonThreadSafe {
Consumer* const consumer_;
};
- // Helper class to scope batch changes.
- class ScopedBatchChange {
- public:
- explicit ScopedBatchChange(OAuth2TokenService* token_service);
- ~ScopedBatchChange();
- private:
- OAuth2TokenService* token_service_; // Weak.
- DISALLOW_COPY_AND_ASSIGN(ScopedBatchChange);
- };
-
- // Subclasses can override if they want to report errors to the user.
- virtual void UpdateAuthError(
- const std::string& account_id,
- const GoogleServiceAuthError& error);
+ // Implement it in delegates if they want to report errors to the user.
+ void UpdateAuthError(const std::string& account_id,
+ const GoogleServiceAuthError& error);
// Add a new entry to the cache.
// Subclasses can override if there are implementation-specific reasons
@@ -270,16 +261,8 @@ class OAuth2TokenService : public base::NonThreadSafe {
// Cancels all requests related to a given |account_id|.
void CancelRequestsForAccount(const std::string& account_id);
- // Called by subclasses to notify observers.
- virtual void FireRefreshTokenAvailable(const std::string& account_id);
- virtual void FireRefreshTokenRevoked(const std::string& account_id);
- virtual void FireRefreshTokensLoaded();
-
- virtual void StartBatchChanges();
- virtual void EndBatchChanges();
-
// Fetches an OAuth token for the specified client/scopes. Virtual so it can
- // be overridden for tests and for platform-specific behavior on Android.
+ // be overridden for tests and for platform-specific behavior.
virtual void FetchOAuth2Token(RequestImpl* request,
const std::string& account_id,
net::URLRequestContextGetter* getter,
@@ -287,27 +270,24 @@ class OAuth2TokenService : public base::NonThreadSafe {
const std::string& client_secret,
const ScopeSet& scopes);
- // Creates an access token fetcher for the given account id.
- //
- // Subclasses should override to create an access token fetcher for the given
- // |account_id|. This method is only called if subclasses use the default
- // implementation of |FetchOAuth2Token|.
- virtual OAuth2AccessTokenFetcher* CreateAccessTokenFetcher(
+ // Create an access token fetcher for the given account id.
+ OAuth2AccessTokenFetcher* CreateAccessTokenFetcher(
const std::string& account_id,
net::URLRequestContextGetter* getter,
- OAuth2AccessTokenConsumer* consumer) = 0;
+ OAuth2AccessTokenConsumer* consumer);
// Invalidates the |access_token| issued for |account_id|, |client_id| and
// |scopes|. Virtual so it can be overriden for tests and for platform-
// specifc behavior.
- virtual void InvalidateOAuth2Token(const std::string& account_id,
- const std::string& client_id,
- const ScopeSet& scopes,
- const std::string& access_token);
+ virtual void InvalidateAccessTokenImpl(const std::string& account_id,
+ const std::string& client_id,
+ const ScopeSet& scopes,
+ const std::string& access_token);
private:
class Fetcher;
friend class Fetcher;
+ friend class OAuth2TokenServiceDelegate;
// The parameters used to fetch an OAuth2 access token.
struct RequestParameters {
@@ -327,9 +307,9 @@ class OAuth2TokenService : public base::NonThreadSafe {
typedef std::map<RequestParameters, Fetcher*> PendingFetcherMap;
- // Derived classes must provide a request context used for fetching access
- // tokens with the |StartRequest| method.
- virtual net::URLRequestContextGetter* GetRequestContext() = 0;
+ // Provide a request context used for fetching access tokens with the
+ // |StartRequest| method.
+ net::URLRequestContextGetter* GetRequestContext() const;
// Struct that contains the information of an OAuth2 access token.
struct CacheEntry {
@@ -379,14 +359,12 @@ class OAuth2TokenService : public base::NonThreadSafe {
typedef std::map<RequestParameters, CacheEntry> TokenCache;
TokenCache token_cache_;
+ scoped_ptr<OAuth2TokenServiceDelegate> delegate_;
+
// A map from fetch parameters to a fetcher that is fetching an OAuth2 access
// token using these parameters.
PendingFetcherMap pending_fetchers_;
- // List of observers to notify when refresh token availability changes.
- // Makes sure list is empty on destruction.
- base::ObserverList<Observer, true> observer_list_;
-
// List of observers to notify when access token status changes.
base::ObserverList<DiagnosticsObserver, true> diagnostics_observer_list_;
@@ -399,6 +377,7 @@ class OAuth2TokenService : public base::NonThreadSafe {
FRIEND_TEST_ALL_PREFIXES(OAuth2TokenServiceTest, RequestParametersOrderTest);
FRIEND_TEST_ALL_PREFIXES(OAuth2TokenServiceTest,
SameScopesRequestedForDifferentClients);
+ FRIEND_TEST_ALL_PREFIXES(OAuth2TokenServiceTest, UpdateClearsCache);
DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService);
};

Powered by Google App Engine
This is Rietveld 408576698