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

Unified Diff: components/signin/core/browser/access_token_fetcher.h

Issue 2582573002: Signin/OAuth: Create an AccessTokenFetcher helper class (Closed)
Patch Set: ChromeOS, for realz now 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 side-by-side diff with in-line comments
Download patch
Index: components/signin/core/browser/access_token_fetcher.h
diff --git a/components/signin/core/browser/access_token_fetcher.h b/components/signin/core/browser/access_token_fetcher.h
new file mode 100644
index 0000000000000000000000000000000000000000..a054bce7e64d3120156c01f4ad1bbfc2ce4c7e7a
--- /dev/null
+++ b/components/signin/core/browser/access_token_fetcher.h
@@ -0,0 +1,83 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef COMPONENTS_SIGNIN_CORE_BROWSER_ACCESS_TOKEN_FETCHER_H_
+#define COMPONENTS_SIGNIN_CORE_BROWSER_ACCESS_TOKEN_FETCHER_H_
+
+#include <memory>
+#include <string>
+
+#include "base/callback.h"
+#include "base/macros.h"
+#include "base/memory/weak_ptr.h"
+#include "components/signin/core/browser/signin_manager_base.h"
+#include "google_apis/gaia/oauth2_token_service.h"
+
+// Helper class to ease the task of obtaining an OAuth2 access token. This
+// handles various special cases, e.g. when the refresh token isn't loaded yet
+// (during startup).
+// May only be used on the UI thread.
+class AccessTokenFetcher : public SigninManagerBase::Observer,
+ public OAuth2TokenService::Observer,
+ public OAuth2TokenService::Consumer {
+ public:
+ using TokenCallback = base::Callback<void(const std::string&)>;
+
+ // Instantiates a fetcher and immediately starts the process of obtaining an
+ // OAuth2 access token for the given |scopes|. The |callback| will be called
+ // with the access token on success, or with an empty string on failure. If
+ // the AccessTokenFetcher is destroyed before the process completes, the
+ // callback will not be called.
+ AccessTokenFetcher(const std::string& oauth_consumer_name,
+ SigninManagerBase* signin_manager,
+ OAuth2TokenService* token_service,
+ const OAuth2TokenService::ScopeSet& scopes,
+ const TokenCallback& callback);
+
+ ~AccessTokenFetcher() override;
+
+ private:
+ void Start();
+
+ void RunCallbackNotSignedIn();
+ void WaitForRefreshToken();
+ void StartAccessTokenRequest();
+
+ // SigninManagerBase::Observer implementation.
+ void GoogleSigninSucceeded(const std::string& account_id,
+ const std::string& username,
+ const std::string& password) override;
+ void GoogleSigninFailed(const GoogleServiceAuthError& error) override;
+
+ // OAuth2TokenService::Observer implementation.
+ void OnRefreshTokenAvailable(const std::string& account_id) override;
+ void OnRefreshTokensLoaded() override;
+
+ // OAuth2TokenService::Consumer implementation.
+ void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
+ const std::string& access_token,
+ const base::Time& expiration_time) override;
+ void OnGetTokenFailure(const OAuth2TokenService::Request* request,
+ const GoogleServiceAuthError& error) override;
+
+ SigninManagerBase* signin_manager_;
+ OAuth2TokenService* token_service_;
+ OAuth2TokenService::ScopeSet scopes_;
+
+ TokenCallback callback_;
+
+ bool waiting_for_sign_in_;
+ bool waiting_for_refresh_token_;
+
+ std::unique_ptr<OAuth2TokenService::Request> access_token_request_;
+
+ // When a token request gets canceled, we want to retry once.
+ bool access_token_retried_;
+
+ base::WeakPtrFactory<AccessTokenFetcher> weak_ptr_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(AccessTokenFetcher);
+};
+
+#endif // COMPONENTS_SIGNIN_CORE_BROWSER_ACCESS_TOKEN_FETCHER_H_

Powered by Google App Engine
This is Rietveld 408576698