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 "google_apis/gaia/gaia_oauth_client.h" | 5 #include "google_apis/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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 int max_retries, | 118 int max_retries, |
119 Delegate* delegate) { | 119 Delegate* delegate) { |
120 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); | 120 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); |
121 DCHECK(!request_.get()); | 121 DCHECK(!request_.get()); |
122 request_type_ = USER_INFO; | 122 request_type_ = USER_INFO; |
123 delegate_ = delegate; | 123 delegate_ = delegate; |
124 num_retries_ = 0; | 124 num_retries_ = 0; |
125 request_.reset(net::URLFetcher::Create( | 125 request_.reset(net::URLFetcher::Create( |
126 0, GURL(GaiaUrls::GetInstance()->oauth_user_info_url()), | 126 0, GURL(GaiaUrls::GetInstance()->oauth_user_info_url()), |
127 net::URLFetcher::GET, this)); | 127 net::URLFetcher::GET, this)); |
128 request_->SetRequestContext(request_context_getter_); | 128 request_->SetRequestContext(request_context_getter_.get()); |
129 request_->AddExtraRequestHeader( | 129 request_->AddExtraRequestHeader("Authorization: OAuth " + oauth_access_token); |
130 "Authorization: OAuth " + oauth_access_token); | |
131 request_->SetMaxRetriesOn5xx(max_retries); | 130 request_->SetMaxRetriesOn5xx(max_retries); |
132 // Fetchers are sometimes cancelled because a network change was detected, | 131 // Fetchers are sometimes cancelled because a network change was detected, |
133 // especially at startup and after sign-in on ChromeOS. Retrying once should | 132 // especially at startup and after sign-in on ChromeOS. Retrying once should |
134 // be enough in those cases; let the fetcher retry up to 3 times just in case. | 133 // be enough in those cases; let the fetcher retry up to 3 times just in case. |
135 // http://crbug.com/163710 | 134 // http://crbug.com/163710 |
136 request_->SetAutomaticallyRetryOnNetworkChanges(3); | 135 request_->SetAutomaticallyRetryOnNetworkChanges(3); |
137 request_->Start(); | 136 request_->Start(); |
138 } | 137 } |
139 | 138 |
140 void GaiaOAuthClient::Core::MakeGaiaRequest( | 139 void GaiaOAuthClient::Core::MakeGaiaRequest( |
141 const std::string& post_body, | 140 const std::string& post_body, |
142 int max_retries, | 141 int max_retries, |
143 GaiaOAuthClient::Delegate* delegate) { | 142 GaiaOAuthClient::Delegate* delegate) { |
144 DCHECK(!request_.get()) << "Tried to fetch two things at once!"; | 143 DCHECK(!request_.get()) << "Tried to fetch two things at once!"; |
145 delegate_ = delegate; | 144 delegate_ = delegate; |
146 num_retries_ = 0; | 145 num_retries_ = 0; |
147 request_.reset(net::URLFetcher::Create( | 146 request_.reset(net::URLFetcher::Create( |
148 0, gaia_url_, net::URLFetcher::POST, this)); | 147 0, gaia_url_, net::URLFetcher::POST, this)); |
149 request_->SetRequestContext(request_context_getter_); | 148 request_->SetRequestContext(request_context_getter_.get()); |
150 request_->SetUploadData("application/x-www-form-urlencoded", post_body); | 149 request_->SetUploadData("application/x-www-form-urlencoded", post_body); |
151 request_->SetMaxRetriesOn5xx(max_retries); | 150 request_->SetMaxRetriesOn5xx(max_retries); |
152 // See comment on SetAutomaticallyRetryOnNetworkChanges() above. | 151 // See comment on SetAutomaticallyRetryOnNetworkChanges() above. |
153 request_->SetAutomaticallyRetryOnNetworkChanges(3); | 152 request_->SetAutomaticallyRetryOnNetworkChanges(3); |
154 request_->Start(); | 153 request_->Start(); |
155 } | 154 } |
156 | 155 |
157 // URLFetcher::Delegate implementation. | 156 // URLFetcher::Delegate implementation. |
158 void GaiaOAuthClient::Core::OnURLFetchComplete( | 157 void GaiaOAuthClient::Core::OnURLFetchComplete( |
159 const net::URLFetcher* source) { | 158 const net::URLFetcher* source) { |
160 bool should_retry = false; | 159 bool should_retry = false; |
161 HandleResponse(source, &should_retry); | 160 HandleResponse(source, &should_retry); |
162 if (should_retry) { | 161 if (should_retry) { |
163 // Explicitly call ReceivedContentWasMalformed() to ensure the current | 162 // Explicitly call ReceivedContentWasMalformed() to ensure the current |
164 // request gets counted as a failure for calculation of the back-off | 163 // request gets counted as a failure for calculation of the back-off |
165 // period. If it was already a failure by status code, this call will | 164 // period. If it was already a failure by status code, this call will |
166 // be ignored. | 165 // be ignored. |
167 request_->ReceivedContentWasMalformed(); | 166 request_->ReceivedContentWasMalformed(); |
168 num_retries_++; | 167 num_retries_++; |
169 // We must set our request_context_getter_ again because | 168 // We must set our request_context_getter_ again because |
170 // URLFetcher::Core::RetryOrCompleteUrlFetch resets it to NULL... | 169 // URLFetcher::Core::RetryOrCompleteUrlFetch resets it to NULL... |
171 request_->SetRequestContext(request_context_getter_); | 170 request_->SetRequestContext(request_context_getter_.get()); |
172 request_->Start(); | 171 request_->Start(); |
173 } | 172 } |
174 } | 173 } |
175 | 174 |
176 void GaiaOAuthClient::Core::HandleResponse( | 175 void GaiaOAuthClient::Core::HandleResponse( |
177 const net::URLFetcher* source, | 176 const net::URLFetcher* source, |
178 bool* should_retry_request) { | 177 bool* should_retry_request) { |
179 // Keep the URLFetcher object in case we need to reuse it. | 178 // Keep the URLFetcher object in case we need to reuse it. |
180 scoped_ptr<net::URLFetcher> old_request = request_.Pass(); | 179 scoped_ptr<net::URLFetcher> old_request = request_.Pass(); |
181 DCHECK_EQ(source, old_request.get()); | 180 DCHECK_EQ(source, old_request.get()); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 delegate); | 281 delegate); |
283 } | 282 } |
284 | 283 |
285 void GaiaOAuthClient::GetUserInfo(const std::string& access_token, | 284 void GaiaOAuthClient::GetUserInfo(const std::string& access_token, |
286 int max_retries, | 285 int max_retries, |
287 Delegate* delegate) { | 286 Delegate* delegate) { |
288 return core_->GetUserInfo(access_token, max_retries, delegate); | 287 return core_->GetUserInfo(access_token, max_retries, delegate); |
289 } | 288 } |
290 | 289 |
291 } // namespace gaia | 290 } // namespace gaia |
OLD | NEW |