| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 CHROME_COMMON_NET_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_H_ | |
| 6 #define CHROME_COMMON_NET_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/gtest_prod_util.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "chrome/common/net/gaia/oauth2_access_token_consumer.h" | |
| 14 #include "content/public/common/url_fetcher.h" | |
| 15 #include "content/public/common/url_fetcher_delegate.h" | |
| 16 #include "googleurl/src/gurl.h" | |
| 17 | |
| 18 class OAuth2AccessTokenFetcherTest; | |
| 19 | |
| 20 namespace net { | |
| 21 class URLRequestContextGetter; | |
| 22 class URLRequestStatus; | |
| 23 } | |
| 24 | |
| 25 // Abstracts the details to get OAuth2 access token token from | |
| 26 // OAuth2 refresh token. | |
| 27 // | |
| 28 // This class should be used on a single thread, but it can be whichever thread | |
| 29 // that you like. | |
| 30 // Also, do not reuse the same instance. Once Start() is called, the instance | |
| 31 // should not be reused. | |
| 32 // | |
| 33 // Usage: | |
| 34 // * Create an instance with a consumer. | |
| 35 // * Call Start() | |
| 36 // * The consumer passed in the constructor will be called on the same | |
| 37 // thread Start was called with the results. | |
| 38 // | |
| 39 // This class can handle one request at a time. To parallelize requests, | |
| 40 // create multiple instances. | |
| 41 class OAuth2AccessTokenFetcher : public content::URLFetcherDelegate { | |
| 42 public: | |
| 43 OAuth2AccessTokenFetcher(OAuth2AccessTokenConsumer* consumer, | |
| 44 net::URLRequestContextGetter* getter, | |
| 45 const std::string& source); | |
| 46 virtual ~OAuth2AccessTokenFetcher(); | |
| 47 | |
| 48 void Start(const std::string& refresh_token); | |
| 49 | |
| 50 void CancelRequest(); | |
| 51 | |
| 52 // Implementation of content::URLFetcherDelegate | |
| 53 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; | |
| 54 | |
| 55 private: | |
| 56 enum State { | |
| 57 INITIAL, | |
| 58 GET_ACCESS_TOKEN_STARTED, | |
| 59 GET_ACCESS_TOKEN_DONE, | |
| 60 ERROR_STATE, | |
| 61 }; | |
| 62 | |
| 63 // Helper methods for the flow. | |
| 64 void StartGetAccessToken(); | |
| 65 void EndGetAccessToken(const content::URLFetcher* source); | |
| 66 | |
| 67 // Helper mehtods for reporting back results. | |
| 68 void ReportSuccess(const std::string& access_token); | |
| 69 void ReportFailure(GoogleServiceAuthError error); | |
| 70 | |
| 71 // Other helpers. | |
| 72 static GURL MakeGetAccessTokenUrl(); | |
| 73 static std::string MakeGetAccessTokenBody(const std::string& refresh_token); | |
| 74 static bool ParseGetAccessTokenResponse(const content::URLFetcher* source, | |
| 75 std::string* access_token); | |
| 76 | |
| 77 // State that is set during construction. | |
| 78 OAuth2AccessTokenConsumer* const consumer_; | |
| 79 net::URLRequestContextGetter* const getter_; | |
| 80 std::string source_; | |
| 81 State state_; | |
| 82 | |
| 83 // While a fetch is in progress. | |
| 84 scoped_ptr<content::URLFetcher> fetcher_; | |
| 85 std::string refresh_token_; | |
| 86 | |
| 87 friend class OAuth2AccessTokenFetcherTest; | |
| 88 FRIEND_TEST_ALL_PREFIXES(OAuth2AccessTokenFetcherTest, | |
| 89 ParseGetAccessTokenResponse); | |
| 90 | |
| 91 DISALLOW_COPY_AND_ASSIGN(OAuth2AccessTokenFetcher); | |
| 92 }; | |
| 93 | |
| 94 #endif // CHROME_COMMON_NET_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_H_ | |
| OLD | NEW |