| 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 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH2_POLICY_FETCHER_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH2_POLICY_FETCHER_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/callback_forward.h" |
| 12 #include "base/compiler_specific.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "google_apis/gaia/gaia_auth_consumer.h" |
| 16 #include "google_apis/gaia/oauth2_access_token_consumer.h" |
| 17 |
| 18 class GaiaAuthFetcher; |
| 19 class OAuth2AccessTokenFetcher; |
| 20 |
| 21 namespace net { |
| 22 class URLRequestContextGetter; |
| 23 } |
| 24 |
| 25 namespace chromeos { |
| 26 |
| 27 // Fetches the OAuth2 token for the device management service. Since Profile |
| 28 // creation might be blocking on a user policy fetch, this fetcher must always |
| 29 // send a (possibly empty) token to the BrowserPolicyConnector, which will then |
| 30 // let the policy subsystem proceed and resume Profile creation. |
| 31 // Sending the token even when no Profile is pending is also OK. |
| 32 class OAuth2PolicyFetcher : public base::SupportsWeakPtr<OAuth2PolicyFetcher>, |
| 33 public GaiaAuthConsumer, |
| 34 public OAuth2AccessTokenConsumer { |
| 35 public: |
| 36 // Fetches the device management service's OAuth2 token using |
| 37 // |oauth2_tokens.refresh_token|. |
| 38 OAuth2PolicyFetcher(net::URLRequestContextGetter* system_context_getter, |
| 39 const std::string& oauth2_refresh_token); |
| 40 // Fetches the device management service's oauth2 token, after also retrieving |
| 41 // the OAuth2 refresh tokens. |
| 42 OAuth2PolicyFetcher( |
| 43 net::URLRequestContextGetter* auth_context_getter, |
| 44 net::URLRequestContextGetter* system_context_getter); |
| 45 |
| 46 virtual ~OAuth2PolicyFetcher(); |
| 47 |
| 48 // Starts process of minting device management service OAuth2 access token. |
| 49 void Start(); |
| 50 |
| 51 // Returns OAuth2 tokens fetched through an authenticated cookie jar. |
| 52 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens() const { |
| 53 return oauth2_tokens_; |
| 54 } |
| 55 |
| 56 // True if we have OAuth2 tokens that were fetched through an authenticated |
| 57 // cookie jar. |
| 58 bool has_oauth2_tokens() const { |
| 59 return !oauth2_tokens_.refresh_token.empty(); |
| 60 } |
| 61 |
| 62 // Returns true if we have previously attempted to fetch tokens with this |
| 63 // class and failed. |
| 64 bool failed() const { |
| 65 return failed_; |
| 66 } |
| 67 |
| 68 private: |
| 69 // GaiaAuthConsumer overrides. |
| 70 virtual void OnClientOAuthSuccess( |
| 71 const GaiaAuthConsumer::ClientOAuthResult& oauth_tokens) OVERRIDE; |
| 72 virtual void OnClientOAuthFailure( |
| 73 const GoogleServiceAuthError& error) OVERRIDE; |
| 74 |
| 75 // OAuth2AccessTokenConsumer overrides. |
| 76 virtual void OnGetTokenSuccess(const std::string& access_token, |
| 77 const base::Time& expiration_time) OVERRIDE; |
| 78 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; |
| 79 |
| 80 // Starts fetching OAuth2 refresh token. |
| 81 void StartFetchingRefreshToken(); |
| 82 // Starts fetching OAuth2 access token for the device management service. |
| 83 void StartFetchingAccessToken(); |
| 84 |
| 85 void SetPolicyToken(const std::string& token); |
| 86 // Decides how to proceed on GAIA |error|. If the error looks temporary, |
| 87 // retries |task| until max retry count is reached. |
| 88 // If retry count runs out, or error condition is unrecoverable, it calls |
| 89 // Delegate::OnOAuth2TokenFetchFailed(). |
| 90 void RetryOnError(const GoogleServiceAuthError& error, |
| 91 const base::Closure& task); |
| 92 |
| 93 scoped_ptr<GaiaAuthFetcher> refresh_token_fetcher_; |
| 94 scoped_ptr<OAuth2AccessTokenFetcher> access_token_fetcher_; |
| 95 std::string policy_token_; |
| 96 GaiaAuthConsumer::ClientOAuthResult oauth2_tokens_; |
| 97 // OAuth2 refresh token. Could come either from the outside or through |
| 98 // refresh token fetching flow within this class. |
| 99 std::string oauth2_refresh_token_; |
| 100 // The retry counter. Increment this only when failure happened. |
| 101 int retry_count_; |
| 102 // True if we have already failed to fetch the policy. |
| 103 bool failed_; |
| 104 |
| 105 DISALLOW_COPY_AND_ASSIGN(OAuth2PolicyFetcher); |
| 106 }; |
| 107 |
| 108 } // namespace chromeos |
| 109 |
| 110 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_OAUTH2_POLICY_FETCHER_H_ |
| OLD | NEW |