OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 // |
| 5 // OAuth2TokenService is used to retrieve OAuth2 access tokens for given |
| 6 // scopes. To use this service, a consumer will start a Request for a given |
| 7 // set of scopes. This request can be started from any thread. It will then be |
| 8 // called asynchronously, on the same thread, with either a valid OAuth2 access |
| 9 // token, or with an error. Deleting the request object will cancel an |
| 10 // in-flight request. |
| 11 |
| 12 #ifndef CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ |
| 13 #define CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ |
| 14 |
| 15 #include <set> |
| 16 #include <string> |
| 17 #include <vector> |
| 18 |
| 19 #include "base/memory/ref_counted.h" |
| 20 #include "base/memory/scoped_ptr.h" |
| 21 #include "chrome/browser/common/cancelable_request.h" |
| 22 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 23 #include "content/public/browser/notification_observer.h" |
| 24 #include "content/public/browser/notification_registrar.h" |
| 25 #include "net/url_request/url_request_context_getter.h" |
| 26 |
| 27 class GoogleServiceAuthError; |
| 28 class OAuth2AccessTokenConsumer; |
| 29 class Profile; |
| 30 |
| 31 // The OAuth2TokenService is a Profile keyed service. All calls but |
| 32 // |StartRequest()| are expected from the UI thread. CreateRequest can be |
| 33 // called from any thread. |
| 34 class OAuth2TokenService : public CancelableRequestProvider, |
| 35 public content::NotificationObserver, |
| 36 public ProfileKeyedService { |
| 37 public: |
| 38 // The class represents a request. Deleting an instance of this class will |
| 39 // stop the in-flight request. |
| 40 class Request { |
| 41 public: |
| 42 Request() {} |
| 43 virtual ~Request() {} |
| 44 }; |
| 45 |
| 46 OAuth2TokenService(); |
| 47 virtual ~OAuth2TokenService(); |
| 48 |
| 49 // Initializes this token service with the profile. |
| 50 void Initialize(Profile* profile); |
| 51 |
| 52 // Starts a request for an OAuth2 access token. The ownership of the Request |
| 53 // instance is passed to the caller. |scopes| is the list of scopes to get an |
| 54 // access token for, |consumer| is the object that will be called back with |
| 55 // results. |consumer| has to outlive the returned request. Deleting the |
| 56 // returned object will cancel the in-flight request. |
| 57 virtual Request* StartRequest(const std::vector<std::string>& scopes, |
| 58 OAuth2AccessTokenConsumer* consumer); |
| 59 |
| 60 // Clear the currently cached tokens. Can be used by consumers to force a new |
| 61 // token to be fetched. |
| 62 void ClearCache(); |
| 63 |
| 64 // content::NotificationObserver |
| 65 virtual void Observe(int type, |
| 66 const content::NotificationSource& source, |
| 67 const content::NotificationDetails& details) OVERRIDE; |
| 68 |
| 69 private: |
| 70 class RequestImpl; |
| 71 friend class RequestImpl; |
| 72 |
| 73 struct CacheEntry { |
| 74 std::string access_token; |
| 75 base::Time expiration_date; |
| 76 }; |
| 77 |
| 78 // Returns a currently valid |AccessToken| for the given set of scopes, or |
| 79 // NULL if none have been cached. |
| 80 CacheEntry const* GetCacheEntry(const std::set<std::string>& scopes); |
| 81 |
| 82 // Register a new access token in the cache. |
| 83 void RegisterCacheEntry(const std::set<std::string>& scopes, |
| 84 const std::string& access_token, |
| 85 const base::Time& expiration_date); |
| 86 |
| 87 // The profile with which this instance was initialized, or NULL. |
| 88 Profile* profile_; |
| 89 |
| 90 // Getter to use for fetchers. |
| 91 scoped_refptr<net::URLRequestContextGetter> getter_; |
| 92 |
| 93 // The cache of currently valid tokens. |
| 94 typedef std::map<std::set<std::string>, CacheEntry> TokensCache; |
| 95 TokensCache tokens_cache_; |
| 96 |
| 97 // Registrar for notifications from the TokenService. |
| 98 content::NotificationRegistrar registrar_; |
| 99 |
| 100 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenService); |
| 101 }; |
| 102 |
| 103 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_H_ |
OLD | NEW |