| 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 net::URLRequestContextGetter* url_request_context_getter) | 97 net::URLRequestContextGetter* url_request_context_getter) |
| 98 : RequestRegistry::Request(runner->request_registry()), | 98 : RequestRegistry::Request(runner->request_registry()), |
| 99 url_request_context_getter_(url_request_context_getter), | 99 url_request_context_getter_(url_request_context_getter), |
| 100 re_authenticate_count_(0), | 100 re_authenticate_count_(0), |
| 101 started_(false), | 101 started_(false), |
| 102 save_temp_file_(false), | 102 save_temp_file_(false), |
| 103 weak_ptr_factory_(this) { | 103 weak_ptr_factory_(this) { |
| 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 105 } | 105 } |
| 106 | 106 |
| 107 UrlFetchRequestBase::UrlFetchRequestBase( | |
| 108 RequestSender* runner, | |
| 109 net::URLRequestContextGetter* url_request_context_getter, | |
| 110 const base::FilePath& path) | |
| 111 : RequestRegistry::Request(runner->request_registry(), path), | |
| 112 url_request_context_getter_(url_request_context_getter), | |
| 113 re_authenticate_count_(0), | |
| 114 started_(false), | |
| 115 save_temp_file_(false), | |
| 116 weak_ptr_factory_(this) { | |
| 117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 118 } | |
| 119 | |
| 120 UrlFetchRequestBase::~UrlFetchRequestBase() {} | 107 UrlFetchRequestBase::~UrlFetchRequestBase() {} |
| 121 | 108 |
| 122 void UrlFetchRequestBase::Start(const std::string& access_token, | 109 void UrlFetchRequestBase::Start(const std::string& access_token, |
| 123 const std::string& custom_user_agent, | 110 const std::string& custom_user_agent, |
| 124 const ReAuthenticateCallback& callback) { | 111 const ReAuthenticateCallback& callback) { |
| 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 126 DCHECK(url_request_context_getter_); | 113 DCHECK(url_request_context_getter_); |
| 127 DCHECK(!access_token.empty()); | 114 DCHECK(!access_token.empty()); |
| 128 DCHECK(!callback.is_null()); | 115 DCHECK(!callback.is_null()); |
| 129 DCHECK(re_authenticate_callback_.is_null()); | 116 DCHECK(re_authenticate_callback_.is_null()); |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 382 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 396 callback_.Run(fetch_error_code, value.Pass()); | 383 callback_.Run(fetch_error_code, value.Pass()); |
| 397 } | 384 } |
| 398 | 385 |
| 399 //========================= InitiateUploadRequestBase ======================== | 386 //========================= InitiateUploadRequestBase ======================== |
| 400 | 387 |
| 401 InitiateUploadRequestBase::InitiateUploadRequestBase( | 388 InitiateUploadRequestBase::InitiateUploadRequestBase( |
| 402 RequestSender* runner, | 389 RequestSender* runner, |
| 403 net::URLRequestContextGetter* url_request_context_getter, | 390 net::URLRequestContextGetter* url_request_context_getter, |
| 404 const InitiateUploadCallback& callback, | 391 const InitiateUploadCallback& callback, |
| 405 const base::FilePath& drive_file_path, | |
| 406 const std::string& content_type, | 392 const std::string& content_type, |
| 407 int64 content_length) | 393 int64 content_length) |
| 408 : UrlFetchRequestBase(runner, | 394 : UrlFetchRequestBase(runner, url_request_context_getter), |
| 409 url_request_context_getter, | |
| 410 drive_file_path), | |
| 411 callback_(callback), | 395 callback_(callback), |
| 412 drive_file_path_(drive_file_path), | |
| 413 content_type_(content_type), | 396 content_type_(content_type), |
| 414 content_length_(content_length) { | 397 content_length_(content_length) { |
| 415 DCHECK(!callback_.is_null()); | 398 DCHECK(!callback_.is_null()); |
| 416 DCHECK(!content_type_.empty()); | 399 DCHECK(!content_type_.empty()); |
| 417 DCHECK_GE(content_length_, 0); | 400 DCHECK_GE(content_length_, 0); |
| 418 } | 401 } |
| 419 | 402 |
| 420 InitiateUploadRequestBase::~InitiateUploadRequestBase() {} | 403 InitiateUploadRequestBase::~InitiateUploadRequestBase() {} |
| 421 | 404 |
| 422 void InitiateUploadRequestBase::ProcessURLFetchResults( | 405 void InitiateUploadRequestBase::ProcessURLFetchResults( |
| 423 const URLFetcher* source) { | 406 const URLFetcher* source) { |
| 424 GDataErrorCode code = GetErrorCode(source); | 407 GDataErrorCode code = GetErrorCode(source); |
| 425 | 408 |
| 426 std::string upload_location; | 409 std::string upload_location; |
| 427 if (code == HTTP_SUCCESS) { | 410 if (code == HTTP_SUCCESS) { |
| 428 // Retrieve value of the first "Location" header. | 411 // Retrieve value of the first "Location" header. |
| 429 source->GetResponseHeaders()->EnumerateHeader(NULL, | 412 source->GetResponseHeaders()->EnumerateHeader(NULL, |
| 430 kUploadResponseLocation, | 413 kUploadResponseLocation, |
| 431 &upload_location); | 414 &upload_location); |
| 432 } | 415 } |
| 433 VLOG(1) << "Got response for [" << drive_file_path_.value() | |
| 434 << "]: code=" << code | |
| 435 << ", location=[" << upload_location << "]"; | |
| 436 | 416 |
| 437 callback_.Run(code, GURL(upload_location)); | 417 callback_.Run(code, GURL(upload_location)); |
| 438 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); | 418 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); |
| 439 } | 419 } |
| 440 | 420 |
| 441 void InitiateUploadRequestBase::RunCallbackOnPrematureFailure( | 421 void InitiateUploadRequestBase::RunCallbackOnPrematureFailure( |
| 442 GDataErrorCode code) { | 422 GDataErrorCode code) { |
| 443 callback_.Run(code, GURL()); | 423 callback_.Run(code, GURL()); |
| 444 } | 424 } |
| 445 | 425 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 469 } | 449 } |
| 470 | 450 |
| 471 UploadRangeResponse::~UploadRangeResponse() { | 451 UploadRangeResponse::~UploadRangeResponse() { |
| 472 } | 452 } |
| 473 | 453 |
| 474 //========================== UploadRangeRequestBase ========================== | 454 //========================== UploadRangeRequestBase ========================== |
| 475 | 455 |
| 476 UploadRangeRequestBase::UploadRangeRequestBase( | 456 UploadRangeRequestBase::UploadRangeRequestBase( |
| 477 RequestSender* runner, | 457 RequestSender* runner, |
| 478 net::URLRequestContextGetter* url_request_context_getter, | 458 net::URLRequestContextGetter* url_request_context_getter, |
| 479 const base::FilePath& drive_file_path, | |
| 480 const GURL& upload_url) | 459 const GURL& upload_url) |
| 481 : UrlFetchRequestBase(runner, | 460 : UrlFetchRequestBase(runner, url_request_context_getter), |
| 482 url_request_context_getter, | |
| 483 drive_file_path), | |
| 484 drive_file_path_(drive_file_path), | |
| 485 upload_url_(upload_url), | 461 upload_url_(upload_url), |
| 486 weak_ptr_factory_(this) { | 462 weak_ptr_factory_(this) { |
| 487 } | 463 } |
| 488 | 464 |
| 489 UploadRangeRequestBase::~UploadRangeRequestBase() {} | 465 UploadRangeRequestBase::~UploadRangeRequestBase() {} |
| 490 | 466 |
| 491 GURL UploadRangeRequestBase::GetURL() const { | 467 GURL UploadRangeRequestBase::GetURL() const { |
| 492 // This is very tricky to get json from this request. To do that, &alt=json | 468 // This is very tricky to get json from this request. To do that, &alt=json |
| 493 // has to be appended not here but in InitiateUploadRequestBase::GetURL(). | 469 // has to be appended not here but in InitiateUploadRequestBase::GetURL(). |
| 494 return upload_url_; | 470 return upload_url_; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 521 // Range header represents the range inclusively, while we are treating | 497 // Range header represents the range inclusively, while we are treating |
| 522 // ranges exclusively (i.e., end_position_received should be one passed | 498 // ranges exclusively (i.e., end_position_received should be one passed |
| 523 // the last valid index). So "+ 1" is added. | 499 // the last valid index). So "+ 1" is added. |
| 524 start_position_received = ranges[0].first_byte_position(); | 500 start_position_received = ranges[0].first_byte_position(); |
| 525 end_position_received = ranges[0].last_byte_position() + 1; | 501 end_position_received = ranges[0].last_byte_position() + 1; |
| 526 } | 502 } |
| 527 } | 503 } |
| 528 // The Range header has the received data range, so the start position | 504 // The Range header has the received data range, so the start position |
| 529 // should be always 0. | 505 // should be always 0. |
| 530 DCHECK_EQ(start_position_received, 0); | 506 DCHECK_EQ(start_position_received, 0); |
| 531 DVLOG(1) << "Got response for [" << drive_file_path_.value() | |
| 532 << "]: code=" << code | |
| 533 << ", range_hdr=[" << range_received | |
| 534 << "], range_parsed=" << start_position_received | |
| 535 << "," << end_position_received; | |
| 536 | 507 |
| 537 OnRangeRequestComplete(UploadRangeResponse(code, | 508 OnRangeRequestComplete(UploadRangeResponse(code, |
| 538 start_position_received, | 509 start_position_received, |
| 539 end_position_received), | 510 end_position_received), |
| 540 scoped_ptr<base::Value>()); | 511 scoped_ptr<base::Value>()); |
| 541 | 512 |
| 542 OnProcessURLFetchResultsComplete(true); | 513 OnProcessURLFetchResultsComplete(true); |
| 543 } else { | 514 } else { |
| 544 // There might be explanation of unexpected error code in response. | 515 // There might be explanation of unexpected error code in response. |
| 545 std::string response_content; | 516 std::string response_content; |
| 546 source->GetResponseAsString(&response_content); | 517 source->GetResponseAsString(&response_content); |
| 547 DVLOG(1) << "Got response for [" << drive_file_path_.value() | |
| 548 << "]: code=" << code | |
| 549 << ", content=[\n" << response_content << "\n]"; | |
| 550 | 518 |
| 551 ParseJson(response_content, | 519 ParseJson(response_content, |
| 552 base::Bind(&UploadRangeRequestBase::OnDataParsed, | 520 base::Bind(&UploadRangeRequestBase::OnDataParsed, |
| 553 weak_ptr_factory_.GetWeakPtr(), | 521 weak_ptr_factory_.GetWeakPtr(), |
| 554 code)); | 522 code)); |
| 555 } | 523 } |
| 556 } | 524 } |
| 557 | 525 |
| 558 void UploadRangeRequestBase::OnDataParsed(GDataErrorCode code, | 526 void UploadRangeRequestBase::OnDataParsed(GDataErrorCode code, |
| 559 scoped_ptr<base::Value> value) { | 527 scoped_ptr<base::Value> value) { |
| 560 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 528 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 561 | 529 |
| 562 OnRangeRequestComplete(UploadRangeResponse(code, -1, -1), value.Pass()); | 530 OnRangeRequestComplete(UploadRangeResponse(code, -1, -1), value.Pass()); |
| 563 OnProcessURLFetchResultsComplete( | 531 OnProcessURLFetchResultsComplete( |
| 564 code == HTTP_CREATED || code == HTTP_SUCCESS); | 532 code == HTTP_CREATED || code == HTTP_SUCCESS); |
| 565 } | 533 } |
| 566 | 534 |
| 567 void UploadRangeRequestBase::RunCallbackOnPrematureFailure( | 535 void UploadRangeRequestBase::RunCallbackOnPrematureFailure( |
| 568 GDataErrorCode code) { | 536 GDataErrorCode code) { |
| 569 OnRangeRequestComplete( | 537 OnRangeRequestComplete( |
| 570 UploadRangeResponse(code, 0, 0), scoped_ptr<base::Value>()); | 538 UploadRangeResponse(code, 0, 0), scoped_ptr<base::Value>()); |
| 571 } | 539 } |
| 572 | 540 |
| 573 //========================== ResumeUploadRequestBase ========================= | 541 //========================== ResumeUploadRequestBase ========================= |
| 574 | 542 |
| 575 ResumeUploadRequestBase::ResumeUploadRequestBase( | 543 ResumeUploadRequestBase::ResumeUploadRequestBase( |
| 576 RequestSender* runner, | 544 RequestSender* runner, |
| 577 net::URLRequestContextGetter* url_request_context_getter, | 545 net::URLRequestContextGetter* url_request_context_getter, |
| 578 const base::FilePath& drive_file_path, | |
| 579 const GURL& upload_location, | 546 const GURL& upload_location, |
| 580 int64 start_position, | 547 int64 start_position, |
| 581 int64 end_position, | 548 int64 end_position, |
| 582 int64 content_length, | 549 int64 content_length, |
| 583 const std::string& content_type, | 550 const std::string& content_type, |
| 584 const base::FilePath& local_file_path) | 551 const base::FilePath& local_file_path) |
| 585 : UploadRangeRequestBase(runner, | 552 : UploadRangeRequestBase(runner, |
| 586 url_request_context_getter, | 553 url_request_context_getter, |
| 587 drive_file_path, | |
| 588 upload_location), | 554 upload_location), |
| 589 start_position_(start_position), | 555 start_position_(start_position), |
| 590 end_position_(end_position), | 556 end_position_(end_position), |
| 591 content_length_(content_length), | 557 content_length_(content_length), |
| 592 content_type_(content_type), | 558 content_type_(content_type), |
| 593 local_file_path_(local_file_path) { | 559 local_file_path_(local_file_path) { |
| 594 DCHECK_LE(start_position_, end_position_); | 560 DCHECK_LE(start_position_, end_position_); |
| 595 } | 561 } |
| 596 | 562 |
| 597 ResumeUploadRequestBase::~ResumeUploadRequestBase() {} | 563 ResumeUploadRequestBase::~ResumeUploadRequestBase() {} |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 638 *range_length = end_position_ - start_position_; | 604 *range_length = end_position_ - start_position_; |
| 639 *upload_content_type = content_type_; | 605 *upload_content_type = content_type_; |
| 640 return true; | 606 return true; |
| 641 } | 607 } |
| 642 | 608 |
| 643 //======================== GetUploadStatusRequestBase ======================== | 609 //======================== GetUploadStatusRequestBase ======================== |
| 644 | 610 |
| 645 GetUploadStatusRequestBase::GetUploadStatusRequestBase( | 611 GetUploadStatusRequestBase::GetUploadStatusRequestBase( |
| 646 RequestSender* runner, | 612 RequestSender* runner, |
| 647 net::URLRequestContextGetter* url_request_context_getter, | 613 net::URLRequestContextGetter* url_request_context_getter, |
| 648 const base::FilePath& drive_file_path, | |
| 649 const GURL& upload_url, | 614 const GURL& upload_url, |
| 650 int64 content_length) | 615 int64 content_length) |
| 651 : UploadRangeRequestBase(runner, | 616 : UploadRangeRequestBase(runner, |
| 652 url_request_context_getter, | 617 url_request_context_getter, |
| 653 drive_file_path, | |
| 654 upload_url), | 618 upload_url), |
| 655 content_length_(content_length) {} | 619 content_length_(content_length) {} |
| 656 | 620 |
| 657 GetUploadStatusRequestBase::~GetUploadStatusRequestBase() {} | 621 GetUploadStatusRequestBase::~GetUploadStatusRequestBase() {} |
| 658 | 622 |
| 659 std::vector<std::string> | 623 std::vector<std::string> |
| 660 GetUploadStatusRequestBase::GetExtraRequestHeaders() const { | 624 GetUploadStatusRequestBase::GetExtraRequestHeaders() const { |
| 661 // The header looks like | 625 // The header looks like |
| 662 // Content-Range: bytes */<content_length> | 626 // Content-Range: bytes */<content_length> |
| 663 // for example: | 627 // for example: |
| 664 // Content-Range: bytes */13851821 | 628 // Content-Range: bytes */13851821 |
| 665 DCHECK_GE(content_length_, 0); | 629 DCHECK_GE(content_length_, 0); |
| 666 | 630 |
| 667 std::vector<std::string> headers; | 631 std::vector<std::string> headers; |
| 668 headers.push_back( | 632 headers.push_back( |
| 669 std::string(kUploadContentRange) + "*/" + | 633 std::string(kUploadContentRange) + "*/" + |
| 670 base::Int64ToString(content_length_)); | 634 base::Int64ToString(content_length_)); |
| 671 return headers; | 635 return headers; |
| 672 } | 636 } |
| 673 | 637 |
| 674 //============================ DownloadFileRequest =========================== | 638 //============================ DownloadFileRequest =========================== |
| 675 | 639 |
| 676 DownloadFileRequest::DownloadFileRequest( | 640 DownloadFileRequest::DownloadFileRequest( |
| 677 RequestSender* runner, | 641 RequestSender* runner, |
| 678 net::URLRequestContextGetter* url_request_context_getter, | 642 net::URLRequestContextGetter* url_request_context_getter, |
| 679 const DownloadActionCallback& download_action_callback, | 643 const DownloadActionCallback& download_action_callback, |
| 680 const GetContentCallback& get_content_callback, | 644 const GetContentCallback& get_content_callback, |
| 681 const ProgressCallback& progress_callback, | 645 const ProgressCallback& progress_callback, |
| 682 const GURL& download_url, | 646 const GURL& download_url, |
| 683 const base::FilePath& drive_file_path, | |
| 684 const base::FilePath& output_file_path) | 647 const base::FilePath& output_file_path) |
| 685 : UrlFetchRequestBase(runner, | 648 : UrlFetchRequestBase(runner, url_request_context_getter), |
| 686 url_request_context_getter, | |
| 687 drive_file_path), | |
| 688 download_action_callback_(download_action_callback), | 649 download_action_callback_(download_action_callback), |
| 689 get_content_callback_(get_content_callback), | 650 get_content_callback_(get_content_callback), |
| 690 progress_callback_(progress_callback), | 651 progress_callback_(progress_callback), |
| 691 download_url_(download_url) { | 652 download_url_(download_url) { |
| 692 DCHECK(!download_action_callback_.is_null()); | 653 DCHECK(!download_action_callback_.is_null()); |
| 693 // get_content_callback may be null. | 654 // get_content_callback may be null. |
| 694 | 655 |
| 695 // Make sure we download the content into a temp file. | 656 // Make sure we download the content into a temp file. |
| 696 if (output_file_path.empty()) | 657 if (output_file_path.empty()) |
| 697 set_save_temp_file(true); | 658 set_save_temp_file(true); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 737 | 698 |
| 738 download_action_callback_.Run(code, temp_file); | 699 download_action_callback_.Run(code, temp_file); |
| 739 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); | 700 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); |
| 740 } | 701 } |
| 741 | 702 |
| 742 void DownloadFileRequest::RunCallbackOnPrematureFailure(GDataErrorCode code) { | 703 void DownloadFileRequest::RunCallbackOnPrematureFailure(GDataErrorCode code) { |
| 743 download_action_callback_.Run(code, base::FilePath()); | 704 download_action_callback_.Run(code, base::FilePath()); |
| 744 } | 705 } |
| 745 | 706 |
| 746 } // namespace google_apis | 707 } // namespace google_apis |
| OLD | NEW |