Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Side by Side Diff: components/signin/core/browser/access_token_fetcher.h

Issue 2582573002: Signin/OAuth: Create an AccessTokenFetcher helper class (Closed)
Patch Set: review2 Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/oauth2_token_service.h"
15
16 // Helper class to ease the task of obtaining an OAuth2 access token. This
17 // handles various special cases, e.g. when the refresh token isn't loaded yet
18 // (during startup).
19 // May only be used on the UI thread.
20 class AccessTokenFetcher : public SigninManagerBase::Observer,
21 public OAuth2TokenService::Observer,
22 public OAuth2TokenService::Consumer {
23 public:
24 using TokenCallback = base::OnceCallback<void(const std::string&)>;
25
26 // Instantiates a fetcher and immediately starts the process of obtaining an
27 // OAuth2 access token for the given |scopes|. The |callback| will be called
28 // with the access token on success, or with an empty string on failure. If
29 // the AccessTokenFetcher is destroyed before the process completes, the
30 // callback will not be called.
31 AccessTokenFetcher(const std::string& oauth_consumer_name,
32 SigninManagerBase* signin_manager,
33 OAuth2TokenService* token_service,
34 const OAuth2TokenService::ScopeSet& scopes,
35 TokenCallback callback);
36
37 ~AccessTokenFetcher() override;
38
39 private:
40 void Start();
41
42 void WaitForRefreshToken();
43 void StartAccessTokenRequest();
44
45 // SigninManagerBase::Observer implementation.
46 void GoogleSigninSucceeded(const std::string& account_id,
47 const std::string& username,
48 const std::string& password) override;
49 void GoogleSigninFailed(const GoogleServiceAuthError& error) override;
50
51 // OAuth2TokenService::Observer implementation.
52 void OnRefreshTokenAvailable(const std::string& account_id) override;
53 void OnRefreshTokensLoaded() override;
54
55 // OAuth2TokenService::Consumer implementation.
56 void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
57 const std::string& access_token,
58 const base::Time& expiration_time) override;
59 void OnGetTokenFailure(const OAuth2TokenService::Request* request,
60 const GoogleServiceAuthError& error) override;
61
62 SigninManagerBase* signin_manager_;
63 OAuth2TokenService* token_service_;
64 OAuth2TokenService::ScopeSet scopes_;
65
66 TokenCallback callback_;
67
68 bool waiting_for_sign_in_;
69 bool waiting_for_refresh_token_;
70
71 std::unique_ptr<OAuth2TokenService::Request> access_token_request_;
72
73 // When a token request gets canceled, we want to retry once.
74 bool access_token_retried_;
75
76 DISALLOW_COPY_AND_ASSIGN(AccessTokenFetcher);
77 };
78
79 #endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCESS_TOKEN_FETCHER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698