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

Unified Diff: chrome/browser/signin/oauth2_token_service.h

Issue 12647008: Refactor OAuth2TokenService to have profile- and device-based implementations. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Addressed final review comments Created 7 years, 9 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: chrome/browser/signin/oauth2_token_service.h
diff --git a/chrome/browser/signin/oauth2_token_service.h b/chrome/browser/signin/oauth2_token_service.h
index 3f19b3c0817954e516767854af99d7beceb27cc1..6ee353e5667783a3fbaa9c23b626d79d7603d996 100644
--- a/chrome/browser/signin/oauth2_token_service.h
+++ b/chrome/browser/signin/oauth2_token_service.h
@@ -9,23 +9,25 @@
#include <set>
#include <string>
-#include "base/memory/ref_counted.h"
+#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
-#include "base/time.h"
-#include "chrome/browser/profiles/profile_keyed_service.h"
-#include "chrome/browser/signin/signin_global_error.h"
-#include "content/public/browser/notification_observer.h"
-#include "content/public/browser/notification_registrar.h"
-#include "net/url_request/url_request_context_getter.h"
+
+namespace base {
+class Time;
+}
+
+namespace net {
+class URLRequestContextGetter;
+}
class GoogleServiceAuthError;
-class OAuth2AccessTokenConsumer;
-class Profile;
-// OAuth2TokenService is a ProfileKeyedService that retrieves OAuth2 access
-// tokens for a given set of scopes using the OAuth2 refresh token maintained by
-// TokenService. All calls are expected from the UI thread.
+// Abstract base class for a service that fetches and caches OAuth2 access
+// tokens. Concrete subclasses should implement GetRefreshToken to return
+// the appropriate refresh token.
+//
+// All calls are expected from the UI thread.
//
// To use this service, call StartRequest() with a given set of scopes and a
// consumer of the request results. The consumer is required to outlive the
@@ -43,12 +45,7 @@ class Profile;
//
// The caller of StartRequest() owns the returned request and is responsible to
// delete the request even once the callback has been invoked.
-//
-// Note the request should be started from the UI thread. To start a request
-// from other thread, please use OAuth2TokenServiceRequest.
-class OAuth2TokenService : public content::NotificationObserver,
- public SigninGlobalError::AuthStatusProvider,
- public ProfileKeyedService {
+class OAuth2TokenService {
public:
// Class representing a request that fetches an OAuth2 access token.
class Request {
@@ -76,36 +73,43 @@ class OAuth2TokenService : public content::NotificationObserver,
// A set of scopes in OAuth2 authentication.
typedef std::set<std::string> ScopeSet;
- OAuth2TokenService();
+ explicit OAuth2TokenService(net::URLRequestContextGetter* getter);
virtual ~OAuth2TokenService();
- // Initializes this token service with the profile.
- void Initialize(Profile* profile);
-
- // ProfileKeyedService implementation.
- virtual void Shutdown() OVERRIDE;
-
- // Starts a request for an OAuth2 access token using the OAuth2 refresh token
- // maintained by TokenService. The caller owns the returned Request. |scopes|
- // is the set of scopes to get an access token for, |consumer| is the object
- // that will be called back with results if the returned request is not
- // deleted.
- // Note the refresh token has been collected from TokenService when this
- // method returns, and the request can continue even if TokenService clears
- // its tokens after this method returns. This means that outstanding
- // StartRequest actions will still complete even if the user signs out in the
- // meantime.
- virtual scoped_ptr<Request> StartRequest(
- const ScopeSet& scopes,
- OAuth2TokenService::Consumer* consumer);
-
- // content::NotificationObserver
- virtual void Observe(int type,
- const content::NotificationSource& source,
- const content::NotificationDetails& details) OVERRIDE;
-
- // SigninGlobalError::AuthStatusProvider implementation.
- virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE;
+ // Checks in the cache for a valid access token, and if not found starts
+ // a request for an OAuth2 access token using the OAuth2 refresh token
+ // maintained by this instance. The caller owns the returned Request.
+ // |scopes| is the set of scopes to get an access token for, |consumer| is
+ // the object that will be called back with results if the returned request
+ // is not deleted.
+ virtual scoped_ptr<Request> StartRequest(const ScopeSet& scopes,
+ Consumer* consumer);
+
+ // Returns true if a refresh token exists. If false, calls to
+ // |StartRequest| will result in a Consumer::OnGetTokenFailure callback.
+ bool RefreshTokenIsAvailable();
+
+ // Return the current number of entries in the cache.
+ int cache_size_for_testing() const;
+
+ protected:
+ // Subclasses should return the refresh token maintained.
+ // If no token is available, return an empty string.
+ virtual std::string GetRefreshToken() = 0;
+
+ // Subclasses can override if they want to report errors to the user.
+ virtual void UpdateAuthError(const GoogleServiceAuthError& error);
+
+ // Add a new entry to the cache.
+ // Subclasses can override if there are implementation-specific reasons
+ // that an access token should ever not be cached.
+ virtual void RegisterCacheEntry(const std::string& refresh_token,
+ const ScopeSet& scopes,
+ const std::string& access_token,
+ const base::Time& expiration_date);
+
+ // Clears the internal token cache.
+ void ClearCache();
private:
// Class that fetches an OAuth2 access token for a given set of scopes and
@@ -133,28 +137,12 @@ class OAuth2TokenService : public content::NotificationObserver,
// ensure no entry with the same |scopes| is added before the usage of the
// returned entry is done.
const CacheEntry* GetCacheEntry(const ScopeSet& scopes);
- // Registers a new access token in the cache if |refresh_token| is the one
- // currently held by TokenService.
- void RegisterCacheEntry(const std::string& refresh_token,
- const ScopeSet& scopes,
- const std::string& access_token,
- const base::Time& expiration_date);
// Called when |fetcher| finishes fetching.
void OnFetchComplete(Fetcher* fetcher);
- // Updates the internal cache of the result from the most-recently-completed
- // auth request (used for reporting errors to the user).
- void UpdateAuthError(const GoogleServiceAuthError& error);
-
- // The profile with which this instance was initialized, or NULL.
- Profile* profile_;
-
- // The auth status from the most-recently-completed request.
- GoogleServiceAuthError last_auth_error_;
-
// Getter to use for fetchers.
- scoped_refptr<net::URLRequestContextGetter> getter_;
+ scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
// The cache of currently valid tokens.
typedef std::map<ScopeSet, CacheEntry> TokenCache;
@@ -167,9 +155,6 @@ class OAuth2TokenService : public content::NotificationObserver,
// token using these parameters.
std::map<FetchParameters, Fetcher*> pending_fetchers_;
- // Registrar for notifications from the TokenService.
- content::NotificationRegistrar registrar_;
-
DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService);
};

Powered by Google App Engine
This is Rietveld 408576698