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