OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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_MINT_TOKEN_FETCHER_H_ | |
6 #define CHROME_COMMON_NET_GAIA_OAUTH2_MINT_TOKEN_FETCHER_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/gtest_prod_util.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "chrome/common/net/gaia/oauth2_mint_token_consumer.h" | |
14 #include "googleurl/src/gurl.h" | |
15 #include "net/url_request/url_fetcher_delegate.h" | |
16 | |
17 class OAuth2MintTokenFetcherTest; | |
18 | |
19 namespace net { | |
20 class URLFetcher; | |
21 class URLRequestContextGetter; | |
22 class URLRequestStatus; | |
23 } | |
24 | |
25 // Abstracts the details to mint new OAuth2 tokens from OAuth2 login scoped | |
26 // 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 OAuth2MintTokenFetcher : public net::URLFetcherDelegate { | |
42 public: | |
43 OAuth2MintTokenFetcher(OAuth2MintTokenConsumer* consumer, | |
44 net::URLRequestContextGetter* getter, | |
45 const std::string& source); | |
46 virtual ~OAuth2MintTokenFetcher(); | |
47 | |
48 // Start the flow. | |
49 virtual void Start(const std::string& oauth_login_access_token, | |
50 const std::string& client_id, | |
51 const std::vector<std::string>& scopes, | |
52 const std::string& origin); | |
53 | |
54 void CancelRequest(); | |
55 | |
56 // Implementation of net::URLFetcherDelegate | |
57 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
58 | |
59 private: | |
60 enum State { | |
61 INITIAL, | |
62 MINT_TOKEN_STARTED, | |
63 MINT_TOKEN_DONE, | |
64 ERROR_STATE, | |
65 }; | |
66 | |
67 // Helper methods for the flow. | |
68 void StartMintToken(); | |
69 void EndMintToken(const net::URLFetcher* source); | |
70 | |
71 // Helper methods for reporting back results. | |
72 void OnMintTokenSuccess(const std::string& access_token); | |
73 void OnMintTokenFailure(const GoogleServiceAuthError& error); | |
74 | |
75 // Other helpers. | |
76 static GURL MakeMintTokenUrl(); | |
77 static std::string MakeMintTokenHeader(const std::string& access_token); | |
78 static std::string MakeMintTokenBody(const std::string& client_id, | |
79 const std::vector<std::string>& scopes, | |
80 const std::string& origin); | |
81 static bool ParseMintTokenResponse(const net::URLFetcher* source, | |
82 std::string* access_token); | |
83 | |
84 // State that is set during construction. | |
85 OAuth2MintTokenConsumer* const consumer_; | |
86 net::URLRequestContextGetter* const getter_; | |
87 std::string source_; | |
88 State state_; | |
89 | |
90 // While a fetch is in progress. | |
91 scoped_ptr<net::URLFetcher> fetcher_; | |
92 std::string oauth_login_access_token_; | |
93 std::string client_id_; | |
94 std::vector<std::string> scopes_; | |
95 std::string origin_; | |
96 | |
97 friend class OAuth2MintTokenFetcherTest; | |
98 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFetcherTest, | |
99 ParseMintTokenResponse); | |
100 | |
101 DISALLOW_COPY_AND_ASSIGN(OAuth2MintTokenFetcher); | |
102 }; | |
103 | |
104 #endif // CHROME_COMMON_NET_GAIA_OAUTH2_MINT_TOKEN_FETCHER_H_ | |
OLD | NEW |