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/location.h" | 8 #include "base/location.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 } | 241 } |
242 | 242 |
243 bool UrlFetchRequestBase::CalledOnValidThread() { | 243 bool UrlFetchRequestBase::CalledOnValidThread() { |
244 return thread_checker_.CalledOnValidThread(); | 244 return thread_checker_.CalledOnValidThread(); |
245 } | 245 } |
246 | 246 |
247 base::TaskRunner* UrlFetchRequestBase::blocking_task_runner() const { | 247 base::TaskRunner* UrlFetchRequestBase::blocking_task_runner() const { |
248 return sender_->blocking_task_runner(); | 248 return sender_->blocking_task_runner(); |
249 } | 249 } |
250 | 250 |
251 void UrlFetchRequestBase::OnProcessURLFetchResultsComplete(bool result) { | 251 void UrlFetchRequestBase::OnProcessURLFetchResultsComplete() { |
252 sender_->RequestFinished(this); | 252 sender_->RequestFinished(this); |
253 } | 253 } |
254 | 254 |
255 void UrlFetchRequestBase::OnURLFetchComplete(const URLFetcher* source) { | 255 void UrlFetchRequestBase::OnURLFetchComplete(const URLFetcher* source) { |
256 GDataErrorCode code = GetErrorCode(source); | 256 GDataErrorCode code = GetErrorCode(source); |
257 DVLOG(1) << "Response headers:\n" << GetResponseHeadersAsString(source); | 257 DVLOG(1) << "Response headers:\n" << GetResponseHeadersAsString(source); |
258 | 258 |
259 if (code == HTTP_UNAUTHORIZED) { | 259 if (code == HTTP_UNAUTHORIZED) { |
260 if (++re_authenticate_count_ <= kMaxReAuthenticateAttemptsPerRequest) { | 260 if (++re_authenticate_count_ <= kMaxReAuthenticateAttemptsPerRequest) { |
261 // Reset re_authenticate_callback_ so Start() can be called again. | 261 // Reset re_authenticate_callback_ so Start() can be called again. |
(...skipping 26 matching lines...) Expand all Loading... |
288 EntryActionRequest::EntryActionRequest(RequestSender* sender, | 288 EntryActionRequest::EntryActionRequest(RequestSender* sender, |
289 const EntryActionCallback& callback) | 289 const EntryActionCallback& callback) |
290 : UrlFetchRequestBase(sender), | 290 : UrlFetchRequestBase(sender), |
291 callback_(callback) { | 291 callback_(callback) { |
292 DCHECK(!callback_.is_null()); | 292 DCHECK(!callback_.is_null()); |
293 } | 293 } |
294 | 294 |
295 EntryActionRequest::~EntryActionRequest() {} | 295 EntryActionRequest::~EntryActionRequest() {} |
296 | 296 |
297 void EntryActionRequest::ProcessURLFetchResults(const URLFetcher* source) { | 297 void EntryActionRequest::ProcessURLFetchResults(const URLFetcher* source) { |
298 GDataErrorCode code = GetErrorCode(source); | 298 callback_.Run(GetErrorCode(source)); |
299 callback_.Run(code); | 299 OnProcessURLFetchResultsComplete(); |
300 const bool success = true; | |
301 OnProcessURLFetchResultsComplete(success); | |
302 } | 300 } |
303 | 301 |
304 void EntryActionRequest::RunCallbackOnPrematureFailure(GDataErrorCode code) { | 302 void EntryActionRequest::RunCallbackOnPrematureFailure(GDataErrorCode code) { |
305 callback_.Run(code); | 303 callback_.Run(code); |
306 } | 304 } |
307 | 305 |
308 //============================== GetDataRequest ============================== | 306 //============================== GetDataRequest ============================== |
309 | 307 |
310 GetDataRequest::GetDataRequest(RequestSender* sender, | 308 GetDataRequest::GetDataRequest(RequestSender* sender, |
311 const GetDataCallback& callback) | 309 const GetDataCallback& callback) |
(...skipping 24 matching lines...) Expand all Loading... |
336 scoped_ptr<base::Value> root_value; | 334 scoped_ptr<base::Value> root_value; |
337 GDataErrorCode fetch_error_code = GetErrorCode(source); | 335 GDataErrorCode fetch_error_code = GetErrorCode(source); |
338 | 336 |
339 switch (fetch_error_code) { | 337 switch (fetch_error_code) { |
340 case HTTP_SUCCESS: | 338 case HTTP_SUCCESS: |
341 case HTTP_CREATED: | 339 case HTTP_CREATED: |
342 ParseResponse(fetch_error_code, data); | 340 ParseResponse(fetch_error_code, data); |
343 break; | 341 break; |
344 default: | 342 default: |
345 RunCallbackOnPrematureFailure(fetch_error_code); | 343 RunCallbackOnPrematureFailure(fetch_error_code); |
346 const bool success = false; | 344 OnProcessURLFetchResultsComplete(); |
347 OnProcessURLFetchResultsComplete(success); | |
348 break; | 345 break; |
349 } | 346 } |
350 } | 347 } |
351 | 348 |
352 void GetDataRequest::RunCallbackOnPrematureFailure( | 349 void GetDataRequest::RunCallbackOnPrematureFailure( |
353 GDataErrorCode fetch_error_code) { | 350 GDataErrorCode fetch_error_code) { |
354 callback_.Run(fetch_error_code, scoped_ptr<base::Value>()); | 351 callback_.Run(fetch_error_code, scoped_ptr<base::Value>()); |
355 } | 352 } |
356 | 353 |
357 void GetDataRequest::OnDataParsed(GDataErrorCode fetch_error_code, | 354 void GetDataRequest::OnDataParsed(GDataErrorCode fetch_error_code, |
358 scoped_ptr<base::Value> value) { | 355 scoped_ptr<base::Value> value) { |
359 DCHECK(CalledOnValidThread()); | 356 DCHECK(CalledOnValidThread()); |
360 | 357 |
361 bool success = true; | 358 if (!value.get()) |
362 if (!value.get()) { | |
363 fetch_error_code = GDATA_PARSE_ERROR; | 359 fetch_error_code = GDATA_PARSE_ERROR; |
364 success = false; | |
365 } | |
366 | 360 |
367 RunCallbackOnSuccess(fetch_error_code, value.Pass()); | |
368 | |
369 DCHECK(!value.get()); | |
370 OnProcessURLFetchResultsComplete(success); | |
371 } | |
372 | |
373 void GetDataRequest::RunCallbackOnSuccess(GDataErrorCode fetch_error_code, | |
374 scoped_ptr<base::Value> value) { | |
375 DCHECK(CalledOnValidThread()); | |
376 callback_.Run(fetch_error_code, value.Pass()); | 361 callback_.Run(fetch_error_code, value.Pass()); |
| 362 OnProcessURLFetchResultsComplete(); |
377 } | 363 } |
378 | 364 |
379 //========================= InitiateUploadRequestBase ======================== | 365 //========================= InitiateUploadRequestBase ======================== |
380 | 366 |
381 InitiateUploadRequestBase::InitiateUploadRequestBase( | 367 InitiateUploadRequestBase::InitiateUploadRequestBase( |
382 RequestSender* sender, | 368 RequestSender* sender, |
383 const InitiateUploadCallback& callback, | 369 const InitiateUploadCallback& callback, |
384 const std::string& content_type, | 370 const std::string& content_type, |
385 int64 content_length) | 371 int64 content_length) |
386 : UrlFetchRequestBase(sender), | 372 : UrlFetchRequestBase(sender), |
(...skipping 13 matching lines...) Expand all Loading... |
400 | 386 |
401 std::string upload_location; | 387 std::string upload_location; |
402 if (code == HTTP_SUCCESS) { | 388 if (code == HTTP_SUCCESS) { |
403 // Retrieve value of the first "Location" header. | 389 // Retrieve value of the first "Location" header. |
404 source->GetResponseHeaders()->EnumerateHeader(NULL, | 390 source->GetResponseHeaders()->EnumerateHeader(NULL, |
405 kUploadResponseLocation, | 391 kUploadResponseLocation, |
406 &upload_location); | 392 &upload_location); |
407 } | 393 } |
408 | 394 |
409 callback_.Run(code, GURL(upload_location)); | 395 callback_.Run(code, GURL(upload_location)); |
410 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); | 396 OnProcessURLFetchResultsComplete(); |
411 } | 397 } |
412 | 398 |
413 void InitiateUploadRequestBase::RunCallbackOnPrematureFailure( | 399 void InitiateUploadRequestBase::RunCallbackOnPrematureFailure( |
414 GDataErrorCode code) { | 400 GDataErrorCode code) { |
415 callback_.Run(code, GURL()); | 401 callback_.Run(code, GURL()); |
416 } | 402 } |
417 | 403 |
418 std::vector<std::string> | 404 std::vector<std::string> |
419 InitiateUploadRequestBase::GetExtraRequestHeaders() const { | 405 InitiateUploadRequestBase::GetExtraRequestHeaders() const { |
420 std::vector<std::string> headers; | 406 std::vector<std::string> headers; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 } | 479 } |
494 // The Range header has the received data range, so the start position | 480 // The Range header has the received data range, so the start position |
495 // should be always 0. | 481 // should be always 0. |
496 DCHECK_EQ(start_position_received, 0); | 482 DCHECK_EQ(start_position_received, 0); |
497 | 483 |
498 OnRangeRequestComplete(UploadRangeResponse(code, | 484 OnRangeRequestComplete(UploadRangeResponse(code, |
499 start_position_received, | 485 start_position_received, |
500 end_position_received), | 486 end_position_received), |
501 scoped_ptr<base::Value>()); | 487 scoped_ptr<base::Value>()); |
502 | 488 |
503 OnProcessURLFetchResultsComplete(true); | 489 OnProcessURLFetchResultsComplete(); |
504 } else if (code == HTTP_CREATED || code == HTTP_SUCCESS) { | 490 } else if (code == HTTP_CREATED || code == HTTP_SUCCESS) { |
505 // The upload is successfully done. Parse the response which should be | 491 // The upload is successfully done. Parse the response which should be |
506 // the entry's metadata. | 492 // the entry's metadata. |
507 std::string response_content; | 493 std::string response_content; |
508 source->GetResponseAsString(&response_content); | 494 source->GetResponseAsString(&response_content); |
509 | 495 |
510 ParseJson(blocking_task_runner(), | 496 ParseJson(blocking_task_runner(), |
511 response_content, | 497 response_content, |
512 base::Bind(&UploadRangeRequestBase::OnDataParsed, | 498 base::Bind(&UploadRangeRequestBase::OnDataParsed, |
513 weak_ptr_factory_.GetWeakPtr(), | 499 weak_ptr_factory_.GetWeakPtr(), |
514 code)); | 500 code)); |
515 } else { | 501 } else { |
516 // Failed to upload. Run callbacks to notify the error. | 502 // Failed to upload. Run callbacks to notify the error. |
517 OnRangeRequestComplete( | 503 OnRangeRequestComplete( |
518 UploadRangeResponse(code, -1, -1), scoped_ptr<base::Value>()); | 504 UploadRangeResponse(code, -1, -1), scoped_ptr<base::Value>()); |
519 OnProcessURLFetchResultsComplete(false); | 505 OnProcessURLFetchResultsComplete(); |
520 } | 506 } |
521 } | 507 } |
522 | 508 |
523 void UploadRangeRequestBase::OnDataParsed(GDataErrorCode code, | 509 void UploadRangeRequestBase::OnDataParsed(GDataErrorCode code, |
524 scoped_ptr<base::Value> value) { | 510 scoped_ptr<base::Value> value) { |
525 DCHECK(CalledOnValidThread()); | 511 DCHECK(CalledOnValidThread()); |
526 DCHECK(code == HTTP_CREATED || code == HTTP_SUCCESS); | 512 DCHECK(code == HTTP_CREATED || code == HTTP_SUCCESS); |
527 | 513 |
528 OnRangeRequestComplete(UploadRangeResponse(code, -1, -1), value.Pass()); | 514 OnRangeRequestComplete(UploadRangeResponse(code, -1, -1), value.Pass()); |
529 OnProcessURLFetchResultsComplete(true); | 515 OnProcessURLFetchResultsComplete(); |
530 } | 516 } |
531 | 517 |
532 void UploadRangeRequestBase::RunCallbackOnPrematureFailure( | 518 void UploadRangeRequestBase::RunCallbackOnPrematureFailure( |
533 GDataErrorCode code) { | 519 GDataErrorCode code) { |
534 OnRangeRequestComplete( | 520 OnRangeRequestComplete( |
535 UploadRangeResponse(code, 0, 0), scoped_ptr<base::Value>()); | 521 UploadRangeResponse(code, 0, 0), scoped_ptr<base::Value>()); |
536 } | 522 } |
537 | 523 |
538 //========================== ResumeUploadRequestBase ========================= | 524 //========================== ResumeUploadRequestBase ========================= |
539 | 525 |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
683 | 669 |
684 // Take over the ownership of the the downloaded temp file. | 670 // Take over the ownership of the the downloaded temp file. |
685 base::FilePath temp_file; | 671 base::FilePath temp_file; |
686 if (code == HTTP_SUCCESS && | 672 if (code == HTTP_SUCCESS && |
687 !source->GetResponseAsFilePath(true, // take_ownership | 673 !source->GetResponseAsFilePath(true, // take_ownership |
688 &temp_file)) { | 674 &temp_file)) { |
689 code = GDATA_FILE_ERROR; | 675 code = GDATA_FILE_ERROR; |
690 } | 676 } |
691 | 677 |
692 download_action_callback_.Run(code, temp_file); | 678 download_action_callback_.Run(code, temp_file); |
693 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); | 679 OnProcessURLFetchResultsComplete(); |
694 } | 680 } |
695 | 681 |
696 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( | 682 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( |
697 GDataErrorCode code) { | 683 GDataErrorCode code) { |
698 download_action_callback_.Run(code, base::FilePath()); | 684 download_action_callback_.Run(code, base::FilePath()); |
699 } | 685 } |
700 | 686 |
701 } // namespace google_apis | 687 } // namespace google_apis |
OLD | NEW |