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/browser/google_apis/base_requests.h" | 5 #include "chrome/browser/google_apis/base_requests.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/task_runner_util.h" | 10 #include "base/task_runner_util.h" |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 base::PostTaskAndReplyWithResult( | 86 base::PostTaskAndReplyWithResult( |
87 BrowserThread::GetBlockingPool(), | 87 BrowserThread::GetBlockingPool(), |
88 FROM_HERE, | 88 FROM_HERE, |
89 base::Bind(&ParseJsonOnBlockingPool, json), | 89 base::Bind(&ParseJsonOnBlockingPool, json), |
90 callback); | 90 callback); |
91 } | 91 } |
92 | 92 |
93 //============================ UrlFetchRequestBase =========================== | 93 //============================ UrlFetchRequestBase =========================== |
94 | 94 |
95 UrlFetchRequestBase::UrlFetchRequestBase( | 95 UrlFetchRequestBase::UrlFetchRequestBase( |
96 RequestSender* runner, | 96 RequestSender* sender, |
97 net::URLRequestContextGetter* url_request_context_getter) | 97 net::URLRequestContextGetter* url_request_context_getter) |
98 : RequestRegistry::Request(runner->request_registry()), | 98 : url_request_context_getter_(url_request_context_getter), |
99 url_request_context_getter_(url_request_context_getter), | |
100 re_authenticate_count_(0), | 99 re_authenticate_count_(0), |
101 started_(false), | 100 sender_(sender), |
102 save_temp_file_(false), | 101 save_temp_file_(false), |
103 weak_ptr_factory_(this) { | 102 weak_ptr_factory_(this) { |
104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
105 } | 104 } |
106 | 105 |
107 UrlFetchRequestBase::~UrlFetchRequestBase() {} | 106 UrlFetchRequestBase::~UrlFetchRequestBase() {} |
108 | 107 |
109 void UrlFetchRequestBase::Start(const std::string& access_token, | 108 void UrlFetchRequestBase::Start(const std::string& access_token, |
110 const std::string& custom_user_agent, | 109 const std::string& custom_user_agent, |
111 const ReAuthenticateCallback& callback) { | 110 const ReAuthenticateCallback& callback) { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 if (request_type == URLFetcher::POST || | 183 if (request_type == URLFetcher::POST || |
185 request_type == URLFetcher::PUT || | 184 request_type == URLFetcher::PUT || |
186 request_type == URLFetcher::PATCH) { | 185 request_type == URLFetcher::PATCH) { |
187 // Set empty upload content-type and upload content, so that | 186 // Set empty upload content-type and upload content, so that |
188 // the request will have no "Content-type: " header and no content. | 187 // the request will have no "Content-type: " header and no content. |
189 url_fetcher_->SetUploadData(std::string(), std::string()); | 188 url_fetcher_->SetUploadData(std::string(), std::string()); |
190 } | 189 } |
191 } | 190 } |
192 } | 191 } |
193 | 192 |
194 // Register to request registry. | |
195 NotifyStart(); | |
196 | |
197 url_fetcher_->Start(); | 193 url_fetcher_->Start(); |
198 started_ = true; | |
199 } | 194 } |
200 | 195 |
201 URLFetcher::RequestType UrlFetchRequestBase::GetRequestType() const { | 196 URLFetcher::RequestType UrlFetchRequestBase::GetRequestType() const { |
202 return URLFetcher::GET; | 197 return URLFetcher::GET; |
203 } | 198 } |
204 | 199 |
205 std::vector<std::string> UrlFetchRequestBase::GetExtraRequestHeaders() const { | 200 std::vector<std::string> UrlFetchRequestBase::GetExtraRequestHeaders() const { |
206 return std::vector<std::string>(); | 201 return std::vector<std::string>(); |
207 } | 202 } |
208 | 203 |
209 bool UrlFetchRequestBase::GetContentData(std::string* upload_content_type, | 204 bool UrlFetchRequestBase::GetContentData(std::string* upload_content_type, |
210 std::string* upload_content) { | 205 std::string* upload_content) { |
211 return false; | 206 return false; |
212 } | 207 } |
213 | 208 |
214 bool UrlFetchRequestBase::GetContentFile(base::FilePath* local_file_path, | 209 bool UrlFetchRequestBase::GetContentFile(base::FilePath* local_file_path, |
215 int64* range_offset, | 210 int64* range_offset, |
216 int64* range_length, | 211 int64* range_length, |
217 std::string* upload_content_type) { | 212 std::string* upload_content_type) { |
218 return false; | 213 return false; |
219 } | 214 } |
220 | 215 |
221 void UrlFetchRequestBase::Cancel() { | 216 void UrlFetchRequestBase::Cancel() { |
222 url_fetcher_.reset(NULL); | 217 url_fetcher_.reset(NULL); |
223 RunCallbackOnPrematureFailure(GDATA_CANCELLED); | 218 RunCallbackOnPrematureFailure(GDATA_CANCELLED); |
224 NotifyFinish(); | 219 sender_->RequestFinished(this); |
225 } | 220 } |
226 | 221 |
227 // static | 222 // static |
228 GDataErrorCode UrlFetchRequestBase::GetErrorCode(const URLFetcher* source) { | 223 GDataErrorCode UrlFetchRequestBase::GetErrorCode(const URLFetcher* source) { |
229 GDataErrorCode code = static_cast<GDataErrorCode>(source->GetResponseCode()); | 224 GDataErrorCode code = static_cast<GDataErrorCode>(source->GetResponseCode()); |
230 if (!source->GetStatus().is_success()) { | 225 if (!source->GetStatus().is_success()) { |
231 switch (source->GetStatus().error()) { | 226 switch (source->GetStatus().error()) { |
232 case net::ERR_NETWORK_CHANGED: | 227 case net::ERR_NETWORK_CHANGED: |
233 code = GDATA_NO_CONNECTION; | 228 code = GDATA_NO_CONNECTION; |
234 break; | 229 break; |
235 default: | 230 default: |
236 code = GDATA_OTHER_ERROR; | 231 code = GDATA_OTHER_ERROR; |
237 } | 232 } |
238 } | 233 } |
239 return code; | 234 return code; |
240 } | 235 } |
241 | 236 |
242 void UrlFetchRequestBase::OnProcessURLFetchResultsComplete(bool result) { | 237 void UrlFetchRequestBase::OnProcessURLFetchResultsComplete(bool result) { |
243 NotifyFinish(); | 238 sender_->RequestFinished(this); |
244 } | 239 } |
245 | 240 |
246 void UrlFetchRequestBase::OnURLFetchComplete(const URLFetcher* source) { | 241 void UrlFetchRequestBase::OnURLFetchComplete(const URLFetcher* source) { |
247 GDataErrorCode code = GetErrorCode(source); | 242 GDataErrorCode code = GetErrorCode(source); |
248 DVLOG(1) << "Response headers:\n" << GetResponseHeadersAsString(source); | 243 DVLOG(1) << "Response headers:\n" << GetResponseHeadersAsString(source); |
249 | 244 |
250 if (code == HTTP_UNAUTHORIZED) { | 245 if (code == HTTP_UNAUTHORIZED) { |
251 if (++re_authenticate_count_ <= kMaxReAuthenticateAttemptsPerRequest) { | 246 if (++re_authenticate_count_ <= kMaxReAuthenticateAttemptsPerRequest) { |
252 // Reset re_authenticate_callback_ so Start() can be called again. | 247 // Reset re_authenticate_callback_ so Start() can be called again. |
253 ReAuthenticateCallback callback = re_authenticate_callback_; | 248 ReAuthenticateCallback callback = re_authenticate_callback_; |
254 re_authenticate_callback_.Reset(); | 249 re_authenticate_callback_.Reset(); |
255 callback.Run(this); | 250 callback.Run(this); |
256 return; | 251 return; |
257 } | 252 } |
258 | 253 |
259 OnAuthFailed(code); | 254 OnAuthFailed(code); |
260 return; | 255 return; |
261 } | 256 } |
262 | 257 |
263 // Overridden by each specialization | 258 // Overridden by each specialization |
264 ProcessURLFetchResults(source); | 259 ProcessURLFetchResults(source); |
265 } | 260 } |
266 | 261 |
267 void UrlFetchRequestBase::OnAuthFailed(GDataErrorCode code) { | 262 void UrlFetchRequestBase::OnAuthFailed(GDataErrorCode code) { |
268 RunCallbackOnPrematureFailure(code); | 263 RunCallbackOnPrematureFailure(code); |
269 | 264 sender_->RequestFinished(this); |
270 // Check if this failed before we even started fetching. If so, register | |
271 // for start so we can properly unregister with finish. | |
272 if (!started_) | |
273 NotifyStart(); | |
274 | |
275 // Note: NotifyFinish() must be invoked at the end, after all other callbacks | |
276 // and notifications. Once NotifyFinish() is called, the current instance of | |
277 // request will be deleted from the RequestRegistry and become invalid. | |
278 NotifyFinish(); | |
279 } | 265 } |
280 | 266 |
281 base::WeakPtr<AuthenticatedRequestInterface> | 267 base::WeakPtr<AuthenticatedRequestInterface> |
282 UrlFetchRequestBase::GetWeakPtr() { | 268 UrlFetchRequestBase::GetWeakPtr() { |
283 return weak_ptr_factory_.GetWeakPtr(); | 269 return weak_ptr_factory_.GetWeakPtr(); |
284 } | 270 } |
285 | 271 |
286 //============================ EntryActionRequest ============================ | 272 //============================ EntryActionRequest ============================ |
287 | 273 |
288 EntryActionRequest::EntryActionRequest( | 274 EntryActionRequest::EntryActionRequest( |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
695 | 681 |
696 download_action_callback_.Run(code, temp_file); | 682 download_action_callback_.Run(code, temp_file); |
697 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); | 683 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); |
698 } | 684 } |
699 | 685 |
700 void DownloadFileRequest::RunCallbackOnPrematureFailure(GDataErrorCode code) { | 686 void DownloadFileRequest::RunCallbackOnPrematureFailure(GDataErrorCode code) { |
701 download_action_callback_.Run(code, base::FilePath()); | 687 download_action_callback_.Run(code, base::FilePath()); |
702 } | 688 } |
703 | 689 |
704 } // namespace google_apis | 690 } // namespace google_apis |
OLD | NEW |