OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 COMPONENTS_SIGNIN_CORE_BROWSER_ACCESS_TOKEN_FETCHER_H_ |
| 6 #define COMPONENTS_SIGNIN_CORE_BROWSER_ACCESS_TOKEN_FETCHER_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <string> |
| 10 |
| 11 #include "base/callback.h" |
| 12 #include "base/macros.h" |
| 13 #include "components/signin/core/browser/signin_manager_base.h" |
| 14 #include "google_apis/gaia/google_service_auth_error.h" |
| 15 #include "google_apis/gaia/oauth2_token_service.h" |
| 16 |
| 17 // Helper class to ease the task of obtaining an OAuth2 access token for the |
| 18 // authenticated account. This handles various special cases, e.g. when the |
| 19 // refresh token isn't loaded yet (during startup), or when there is some |
| 20 // transient error. |
| 21 // May only be used on the UI thread. |
| 22 class AccessTokenFetcher : public SigninManagerBase::Observer, |
| 23 public OAuth2TokenService::Observer, |
| 24 public OAuth2TokenService::Consumer { |
| 25 public: |
| 26 // Callback for when a request completes (successful or not). On successful |
| 27 // requests, |error| is NONE and |access_token| contains the obtained OAuth2 |
| 28 // access token. On failed requests, |error| contains the actual error and |
| 29 // |access_token| is empty. |
| 30 using TokenCallback = |
| 31 base::OnceCallback<void(const GoogleServiceAuthError& error, |
| 32 const std::string& access_token)>; |
| 33 |
| 34 // Instantiates a fetcher and immediately starts the process of obtaining an |
| 35 // OAuth2 access token for the given |scopes|. The |callback| is called once |
| 36 // the request completes (successful or not). If the AccessTokenFetcher is |
| 37 // destroyed before the process completes, the callback is not called. |
| 38 AccessTokenFetcher(const std::string& oauth_consumer_name, |
| 39 SigninManagerBase* signin_manager, |
| 40 OAuth2TokenService* token_service, |
| 41 const OAuth2TokenService::ScopeSet& scopes, |
| 42 TokenCallback callback); |
| 43 |
| 44 ~AccessTokenFetcher() override; |
| 45 |
| 46 private: |
| 47 void Start(); |
| 48 |
| 49 void WaitForRefreshToken(); |
| 50 void StartAccessTokenRequest(); |
| 51 |
| 52 // SigninManagerBase::Observer implementation. |
| 53 void GoogleSigninSucceeded(const std::string& account_id, |
| 54 const std::string& username, |
| 55 const std::string& password) override; |
| 56 void GoogleSigninFailed(const GoogleServiceAuthError& error) override; |
| 57 |
| 58 // OAuth2TokenService::Observer implementation. |
| 59 void OnRefreshTokenAvailable(const std::string& account_id) override; |
| 60 void OnRefreshTokensLoaded() override; |
| 61 |
| 62 // OAuth2TokenService::Consumer implementation. |
| 63 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 64 const std::string& access_token, |
| 65 const base::Time& expiration_time) override; |
| 66 void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 67 const GoogleServiceAuthError& error) override; |
| 68 |
| 69 SigninManagerBase* signin_manager_; |
| 70 OAuth2TokenService* token_service_; |
| 71 OAuth2TokenService::ScopeSet scopes_; |
| 72 TokenCallback callback_; |
| 73 |
| 74 bool waiting_for_sign_in_; |
| 75 bool waiting_for_refresh_token_; |
| 76 |
| 77 std::unique_ptr<OAuth2TokenService::Request> access_token_request_; |
| 78 |
| 79 // When a token request gets canceled, we want to retry once. |
| 80 bool access_token_retried_; |
| 81 |
| 82 DISALLOW_COPY_AND_ASSIGN(AccessTokenFetcher); |
| 83 }; |
| 84 |
| 85 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCESS_TOKEN_FETCHER_H_ |
OLD | NEW |