OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/net/gaia/gaia_oauth_client.h" | 5 #include "chrome/common/net/gaia/gaia_oauth_client.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 void GetTokensFromAuthCode(const OAuthClientInfo& oauth_client_info, | 37 void GetTokensFromAuthCode(const OAuthClientInfo& oauth_client_info, |
38 const std::string& auth_code, | 38 const std::string& auth_code, |
39 int max_retries, | 39 int max_retries, |
40 GaiaOAuthClient::Delegate* delegate); | 40 GaiaOAuthClient::Delegate* delegate); |
41 void RefreshToken(const OAuthClientInfo& oauth_client_info, | 41 void RefreshToken(const OAuthClientInfo& oauth_client_info, |
42 const std::string& refresh_token, | 42 const std::string& refresh_token, |
43 int max_retries, | 43 int max_retries, |
44 GaiaOAuthClient::Delegate* delegate); | 44 GaiaOAuthClient::Delegate* delegate); |
45 | 45 |
46 // content::URLFetcherDelegate implementation. | 46 // content::URLFetcherDelegate implementation. |
47 virtual void OnURLFetchComplete(const content::URLFetcher* source); | 47 virtual void OnURLFetchComplete(const net::URLFetcher* source); |
48 | 48 |
49 private: | 49 private: |
50 friend class base::RefCountedThreadSafe<Core>; | 50 friend class base::RefCountedThreadSafe<Core>; |
51 virtual ~Core() {} | 51 virtual ~Core() {} |
52 | 52 |
53 void MakeGaiaRequest(std::string post_body, | 53 void MakeGaiaRequest(std::string post_body, |
54 int max_retries, | 54 int max_retries, |
55 GaiaOAuthClient::Delegate* delegate); | 55 GaiaOAuthClient::Delegate* delegate); |
56 void HandleResponse(const content::URLFetcher* source, | 56 void HandleResponse(const net::URLFetcher* source, |
57 bool* should_retry_request); | 57 bool* should_retry_request); |
58 | 58 |
59 GURL gaia_url_; | 59 GURL gaia_url_; |
60 int num_retries_; | 60 int num_retries_; |
61 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 61 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
62 GaiaOAuthClient::Delegate* delegate_; | 62 GaiaOAuthClient::Delegate* delegate_; |
63 scoped_ptr<content::URLFetcher> request_; | 63 scoped_ptr<content::URLFetcher> request_; |
64 }; | 64 }; |
65 | 65 |
66 void GaiaOAuthClient::Core::GetTokensFromAuthCode( | 66 void GaiaOAuthClient::Core::GetTokensFromAuthCode( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 request_.reset(content::URLFetcher::Create( | 103 request_.reset(content::URLFetcher::Create( |
104 0, gaia_url_, content::URLFetcher::POST, this)); | 104 0, gaia_url_, content::URLFetcher::POST, this)); |
105 request_->SetRequestContext(request_context_getter_); | 105 request_->SetRequestContext(request_context_getter_); |
106 request_->SetUploadData("application/x-www-form-urlencoded", post_body); | 106 request_->SetUploadData("application/x-www-form-urlencoded", post_body); |
107 request_->SetMaxRetries(max_retries); | 107 request_->SetMaxRetries(max_retries); |
108 request_->Start(); | 108 request_->Start(); |
109 } | 109 } |
110 | 110 |
111 // URLFetcher::Delegate implementation. | 111 // URLFetcher::Delegate implementation. |
112 void GaiaOAuthClient::Core::OnURLFetchComplete( | 112 void GaiaOAuthClient::Core::OnURLFetchComplete( |
113 const content::URLFetcher* source) { | 113 const net::URLFetcher* source) { |
114 bool should_retry = false; | 114 bool should_retry = false; |
115 HandleResponse(source, &should_retry); | 115 HandleResponse(source, &should_retry); |
116 if (should_retry) { | 116 if (should_retry) { |
117 // Explicitly call ReceivedContentWasMalformed() to ensure the current | 117 // Explicitly call ReceivedContentWasMalformed() to ensure the current |
118 // request gets counted as a failure for calculation of the back-off | 118 // request gets counted as a failure for calculation of the back-off |
119 // period. If it was already a failure by status code, this call will | 119 // period. If it was already a failure by status code, this call will |
120 // be ignored. | 120 // be ignored. |
121 request_->ReceivedContentWasMalformed(); | 121 request_->ReceivedContentWasMalformed(); |
122 num_retries_++; | 122 num_retries_++; |
123 // We must set our request_context_getter_ again because | 123 // We must set our request_context_getter_ again because |
124 // URLFetcher::Core::RetryOrCompleteUrlFetch resets it to NULL... | 124 // URLFetcher::Core::RetryOrCompleteUrlFetch resets it to NULL... |
125 request_->SetRequestContext(request_context_getter_); | 125 request_->SetRequestContext(request_context_getter_); |
126 request_->Start(); | 126 request_->Start(); |
127 } else { | 127 } else { |
128 request_.reset(); | 128 request_.reset(); |
129 } | 129 } |
130 } | 130 } |
131 | 131 |
132 void GaiaOAuthClient::Core::HandleResponse( | 132 void GaiaOAuthClient::Core::HandleResponse( |
133 const content::URLFetcher* source, | 133 const net::URLFetcher* source, |
134 bool* should_retry_request) { | 134 bool* should_retry_request) { |
135 *should_retry_request = false; | 135 *should_retry_request = false; |
136 // RC_BAD_REQUEST means the arguments are invalid. No point retrying. We are | 136 // RC_BAD_REQUEST means the arguments are invalid. No point retrying. We are |
137 // done here. | 137 // done here. |
138 if (source->GetResponseCode() == net::HTTP_BAD_REQUEST) { | 138 if (source->GetResponseCode() == net::HTTP_BAD_REQUEST) { |
139 delegate_->OnOAuthError(); | 139 delegate_->OnOAuthError(); |
140 return; | 140 return; |
141 } | 141 } |
142 std::string access_token; | 142 std::string access_token; |
143 std::string refresh_token; | 143 std::string refresh_token; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 const std::string& refresh_token, | 199 const std::string& refresh_token, |
200 int max_retries, | 200 int max_retries, |
201 Delegate* delegate) { | 201 Delegate* delegate) { |
202 return core_->RefreshToken(oauth_client_info, | 202 return core_->RefreshToken(oauth_client_info, |
203 refresh_token, | 203 refresh_token, |
204 max_retries, | 204 max_retries, |
205 delegate); | 205 delegate); |
206 } | 206 } |
207 | 207 |
208 } // namespace gaia | 208 } // namespace gaia |
OLD | NEW |