Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / google_apis / gaia / oauth2_access_token_fetcher_impl.h
blob6944d243e7d14a80e1eb807d662f6169d0b06e8f
1 // Copyright 2014 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.
5 #ifndef GOOGLE_APIS_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_IMPL_H_
6 #define GOOGLE_APIS_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_IMPL_H_
8 #include <string>
9 #include <vector>
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "google_apis/gaia/oauth2_access_token_consumer.h"
14 #include "google_apis/gaia/oauth2_access_token_fetcher.h"
15 #include "net/url_request/url_fetcher_delegate.h"
16 #include "url/gurl.h"
18 class OAuth2AccessTokenFetcherImplTest;
20 namespace base {
21 class Time;
24 namespace net {
25 class URLFetcher;
26 class URLRequestContextGetter;
27 class URLRequestStatus;
30 // Abstracts the details to get OAuth2 access token token from
31 // OAuth2 refresh token.
32 // See "Using the Refresh Token" section in:
33 // http://code.google.com/apis/accounts/docs/OAuth2WebServer.html
35 // This class should be used on a single thread, but it can be whichever thread
36 // that you like.
37 // Also, do not reuse the same instance. Once Start() is called, the instance
38 // should not be reused.
40 // Usage:
41 // * Create an instance with a consumer.
42 // * Call Start()
43 // * The consumer passed in the constructor will be called on the same
44 // thread Start was called with the results.
46 // This class can handle one request at a time. To parallelize requests,
47 // create multiple instances.
48 class OAuth2AccessTokenFetcherImpl : public OAuth2AccessTokenFetcher,
49 public net::URLFetcherDelegate {
50 public:
51 OAuth2AccessTokenFetcherImpl(OAuth2AccessTokenConsumer* consumer,
52 net::URLRequestContextGetter* getter,
53 const std::string& refresh_token);
54 ~OAuth2AccessTokenFetcherImpl() override;
56 // Implementation of OAuth2AccessTokenFetcher
57 void Start(const std::string& client_id,
58 const std::string& client_secret,
59 const std::vector<std::string>& scopes) override;
61 void CancelRequest() override;
63 // Implementation of net::URLFetcherDelegate
64 void OnURLFetchComplete(const net::URLFetcher* source) override;
66 private:
67 enum State {
68 INITIAL,
69 GET_ACCESS_TOKEN_STARTED,
70 GET_ACCESS_TOKEN_DONE,
71 ERROR_STATE,
74 // Helper methods for the flow.
75 void StartGetAccessToken();
76 void EndGetAccessToken(const net::URLFetcher* source);
78 // Helper mehtods for reporting back results.
79 void OnGetTokenSuccess(const std::string& access_token,
80 const base::Time& expiration_time);
81 void OnGetTokenFailure(const GoogleServiceAuthError& error);
83 // Other helpers.
84 static GURL MakeGetAccessTokenUrl();
85 static std::string MakeGetAccessTokenBody(
86 const std::string& client_id,
87 const std::string& client_secret,
88 const std::string& refresh_token,
89 const std::vector<std::string>& scopes);
91 static bool ParseGetAccessTokenSuccessResponse(const net::URLFetcher* source,
92 std::string* access_token,
93 int* expires_in);
95 static bool ParseGetAccessTokenFailureResponse(const net::URLFetcher* source,
96 std::string* error);
98 // State that is set during construction.
99 net::URLRequestContextGetter* const getter_;
100 std::string refresh_token_;
101 State state_;
103 // While a fetch is in progress.
104 scoped_ptr<net::URLFetcher> fetcher_;
105 std::string client_id_;
106 std::string client_secret_;
107 std::vector<std::string> scopes_;
109 friend class OAuth2AccessTokenFetcherImplTest;
110 FRIEND_TEST_ALL_PREFIXES(OAuth2AccessTokenFetcherImplTest,
111 ParseGetAccessTokenResponse);
112 FRIEND_TEST_ALL_PREFIXES(OAuth2AccessTokenFetcherImplTest,
113 MakeGetAccessTokenBody);
115 DISALLOW_COPY_AND_ASSIGN(OAuth2AccessTokenFetcherImpl);
118 #endif // GOOGLE_APIS_GAIA_OAUTH2_ACCESS_TOKEN_FETCHER_IMPL_H_