Chromium Code Reviews| 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. This | |
|
msarda
2017/01/27 10:38:58
ss/access token/access token for the authenticated
Marc Treib
2017/01/27 11:16:55
Done.
| |
| 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 // Callback for when a request completes (successful or not). On successful | |
| 26 // requests, |error| is NONE and |access_token| contains the obtained OAuth2 | |
| 27 // access token. On failed requests, |error| contains the actual error and | |
| 28 // |access_token| is empty. | |
| 29 using TokenCallback = | |
| 30 base::OnceCallback<void(const GoogleServiceAuthError& error, | |
| 31 const std::string& access_token)>; | |
| 32 | |
| 33 // Instantiates a fetcher and immediately starts the process of obtaining an | |
| 34 // OAuth2 access token for the given |scopes|. The |callback| is called once | |
| 35 // the request completes (successful or not). If the AccessTokenFetcher is | |
| 36 // destroyed before the process completes, the callback is not called. | |
| 37 AccessTokenFetcher(const std::string& oauth_consumer_name, | |
| 38 SigninManagerBase* signin_manager, | |
| 39 OAuth2TokenService* token_service, | |
| 40 const OAuth2TokenService::ScopeSet& scopes, | |
| 41 TokenCallback callback); | |
| 42 | |
| 43 ~AccessTokenFetcher() override; | |
| 44 | |
| 45 private: | |
| 46 void Start(); | |
| 47 | |
| 48 void WaitForRefreshToken(); | |
| 49 void StartAccessTokenRequest(); | |
| 50 | |
| 51 // SigninManagerBase::Observer implementation. | |
| 52 void GoogleSigninSucceeded(const std::string& account_id, | |
| 53 const std::string& username, | |
| 54 const std::string& password) override; | |
| 55 void GoogleSigninFailed(const GoogleServiceAuthError& error) override; | |
| 56 | |
| 57 // OAuth2TokenService::Observer implementation. | |
| 58 void OnRefreshTokenAvailable(const std::string& account_id) override; | |
| 59 void OnRefreshTokensLoaded() override; | |
| 60 | |
| 61 // OAuth2TokenService::Consumer implementation. | |
| 62 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | |
| 63 const std::string& access_token, | |
| 64 const base::Time& expiration_time) override; | |
| 65 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | |
| 66 const GoogleServiceAuthError& error) override; | |
| 67 | |
| 68 SigninManagerBase* signin_manager_; | |
| 69 OAuth2TokenService* token_service_; | |
| 70 OAuth2TokenService::ScopeSet scopes_; | |
| 71 | |
|
msarda
2017/01/27 10:38:58
Nit: Kill empty line.
Marc Treib
2017/01/27 11:16:55
Done.
| |
| 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 |