Index: chrome/browser/net/gaia/oauth2_login_token_fetcher.h |
=================================================================== |
--- chrome/browser/net/gaia/oauth2_login_token_fetcher.h (revision 0) |
+++ chrome/browser/net/gaia/oauth2_login_token_fetcher.h (revision 0) |
@@ -0,0 +1,112 @@ |
+// Copyright (c) 2011 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 CHROME_BROWSER_NET_GAIA_OAUTH2_LOGIN_TOKEN_FETCHER_H_ |
+#define CHROME_BROWSER_NET_GAIA_OAUTH2_LOGIN_TOKEN_FETCHER_H_ |
+#pragma once |
+ |
+#include <string> |
+ |
+#include "base/gtest_prod_util.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "chrome/browser/net/gaia/oauth2_login_token_consumer.h" |
+#include "content/public/common/url_fetcher.h" |
+#include "content/public/common/url_fetcher_delegate.h" |
+#include "googleurl/src/gurl.h" |
+ |
+class OAuth2LoginTokenFetcherTest; |
+ |
+namespace net { |
+class URLRequestContextGetter; |
+class URLRequestStatus; |
+} |
+ |
+// Abstracts the details to get OAuth2 login scoped token from |
+// ClientLogin credentails. |
+// |
+// This class should be used on a single thread, but it can be whichever thread |
+// that you like. |
+// Also, do not reuse the same instance. Once Start() is called, the instance |
+// should not be reused. |
+// |
+// Usage: |
+// * Create an instance with a consumer. |
+// * Call Start() |
+// * The consumer passed in the constructor will be called on the same |
+// thread Start was called with the results. |
+// |
+// This class can handle one request at a time. To parallelize requests, |
+// create multiple instances. |
+class OAuth2LoginTokenFetcher : public content::URLFetcherDelegate { |
+ public: |
+ OAuth2LoginTokenFetcher(OAuth2LoginTokenConsumer* consumer, |
+ net::URLRequestContextGetter* getter, |
+ const std::string& source); |
+ virtual ~OAuth2LoginTokenFetcher(); |
+ |
+ void Start(const std::string& auth_token); |
+ |
+ void CancelRequest(); |
+ |
+ // Implementation of content::URLFetcherDelegate |
+ virtual void OnURLFetchComplete(const content::URLFetcher* source); |
+ |
+ private: |
+ // There are two steps to getting a tokeen pair: |
+ // 1. Get authorization code. |
+ // 2. Exchange authorization code for a token pair. |
+ enum State { |
+ INITIAL, |
+ GET_AUTH_CODE_STARTED, |
+ GET_AUTH_CODE_DONE, |
+ GET_TOKEN_PAIR_STARTED, |
+ GET_TOKEN_PAIR_DONE, |
+ ERROR_STATE, |
+ }; |
+ |
+ // Helper methods for the flow. |
+ void StartGetAuthCode(); |
+ void EndGetAuthCode(const content::URLFetcher* source); |
+ void StartGetTokenPair(); |
+ void EndGetTokenPair(const content::URLFetcher* source); |
+ |
+ // Helper mehtods for reporting back results. |
+ void ReportSuccess(const std::string& refresh_token, |
+ const std::string& access_token); |
+ void ReportFailure(GoogleServiceAuthError error); |
+ |
+ // Other helpers. |
+ static GURL MakeGetAuthCodeUrl(); |
+ static std::string MakeGetAuthCodeHeader(const std::string& auth_token); |
+ static std::string MakeGetAuthCodeBody(); |
+ static bool ParseGetAuthCodeResponse(const content::URLFetcher* source, |
+ std::string* auth_code); |
+ |
+ static GURL MakeGetTokenPairUrl(); |
+ static std::string MakeGetTokenPairBody(const std::string& auth_code); |
+ static bool ParseGetTokenPairResponse(const content::URLFetcher* source, |
+ std::string* refresh_token, |
+ std::string* access_token); |
+ |
+ // State that is set during construction. |
+ OAuth2LoginTokenConsumer* const consumer_; |
+ net::URLRequestContextGetter* const getter_; |
+ std::string source_; |
+ State state_; |
+ |
+ // While a fetch is in progress. |
+ scoped_ptr<content::URLFetcher> fetcher_; |
+ std::string auth_token_; |
+ std::string auth_code_; |
+ |
+ friend class OAuth2LoginTokenFetcherTest; |
+ FRIEND_TEST_ALL_PREFIXES(OAuth2LoginTokenFetcherTest, |
+ ParseGetAuthCodeResponse); |
+ FRIEND_TEST_ALL_PREFIXES(OAuth2LoginTokenFetcherTest, |
+ ParseGetTokenPairResponse); |
+ |
+ DISALLOW_COPY_AND_ASSIGN(OAuth2LoginTokenFetcher); |
+}; |
+ |
+#endif // CHROME_BROWSER_NET_GAIA_OAUTH2_LOGIN_TOKEN_FETCHER_H_ |