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/chromeos/gdata/gdata_operations.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_operations.h" |
6 | 6 |
7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "chrome/browser/chromeos/gdata/gdata_util.h" | 10 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 return !get_download_data_callback_.is_null(); | 267 return !get_download_data_callback_.is_null(); |
268 } | 268 } |
269 | 269 |
270 void DownloadFileOperation::OnURLFetchDownloadData( | 270 void DownloadFileOperation::OnURLFetchDownloadData( |
271 const URLFetcher* source, | 271 const URLFetcher* source, |
272 scoped_ptr<std::string> download_data) { | 272 scoped_ptr<std::string> download_data) { |
273 if (!get_download_data_callback_.is_null()) | 273 if (!get_download_data_callback_.is_null()) |
274 get_download_data_callback_.Run(HTTP_SUCCESS, download_data.Pass()); | 274 get_download_data_callback_.Run(HTTP_SUCCESS, download_data.Pass()); |
275 } | 275 } |
276 | 276 |
277 bool DownloadFileOperation::ProcessURLFetchResults( | 277 void DownloadFileOperation::ProcessURLFetchResults(const URLFetcher* source) { |
278 const URLFetcher* source) { | |
279 GDataErrorCode code = GetErrorCode(source); | 278 GDataErrorCode code = GetErrorCode(source); |
280 | 279 |
281 // Take over the ownership of the the downloaded temp file. | 280 // Take over the ownership of the the downloaded temp file. |
282 FilePath temp_file; | 281 FilePath temp_file; |
283 if (code == HTTP_SUCCESS && | 282 if (code == HTTP_SUCCESS && |
284 !source->GetResponseAsFilePath(true, // take_ownership | 283 !source->GetResponseAsFilePath(true, // take_ownership |
285 &temp_file)) { | 284 &temp_file)) { |
286 code = GDATA_FILE_ERROR; | 285 code = GDATA_FILE_ERROR; |
287 } | 286 } |
288 | 287 |
289 if (!download_action_callback_.is_null()) | 288 if (!download_action_callback_.is_null()) |
290 download_action_callback_.Run(code, document_url_, temp_file); | 289 download_action_callback_.Run(code, document_url_, temp_file); |
291 return code == HTTP_SUCCESS; | 290 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); |
292 } | 291 } |
293 | 292 |
294 void DownloadFileOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) { | 293 void DownloadFileOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) { |
295 if (!download_action_callback_.is_null()) | 294 if (!download_action_callback_.is_null()) |
296 download_action_callback_.Run(code, document_url_, FilePath()); | 295 download_action_callback_.Run(code, document_url_, FilePath()); |
297 } | 296 } |
298 | 297 |
299 //=========================== DeleteDocumentOperation ========================== | 298 //=========================== DeleteDocumentOperation ========================== |
300 | 299 |
301 DeleteDocumentOperation::DeleteDocumentOperation( | 300 DeleteDocumentOperation::DeleteDocumentOperation( |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 return URLFetcher::PUT; | 482 return URLFetcher::PUT; |
484 } | 483 } |
485 | 484 |
486 std::vector<std::string> | 485 std::vector<std::string> |
487 AuthorizeAppsOperation::GetExtraRequestHeaders() const { | 486 AuthorizeAppsOperation::GetExtraRequestHeaders() const { |
488 std::vector<std::string> headers; | 487 std::vector<std::string> headers; |
489 headers.push_back(kIfMatchAllHeader); | 488 headers.push_back(kIfMatchAllHeader); |
490 return headers; | 489 return headers; |
491 } | 490 } |
492 | 491 |
493 bool AuthorizeAppsOperation::ProcessURLFetchResults( | 492 void AuthorizeAppsOperation::ProcessURLFetchResults(const URLFetcher* source) { |
494 const URLFetcher* source) { | |
495 std::string data; | 493 std::string data; |
496 source->GetResponseAsString(&data); | 494 source->GetResponseAsString(&data); |
497 return GetDataOperation::ProcessURLFetchResults(source); | 495 GetDataOperation::ProcessURLFetchResults(source); |
498 } | 496 } |
499 | 497 |
500 bool AuthorizeAppsOperation::GetContentData(std::string* upload_content_type, | 498 bool AuthorizeAppsOperation::GetContentData(std::string* upload_content_type, |
501 std::string* upload_content) { | 499 std::string* upload_content) { |
502 upload_content_type->assign("application/atom+xml"); | 500 upload_content_type->assign("application/atom+xml"); |
503 XmlWriter xml_writer; | 501 XmlWriter xml_writer; |
504 xml_writer.StartWriting(); | 502 xml_writer.StartWriting(); |
505 xml_writer.StartElement("entry"); | 503 xml_writer.StartElement("entry"); |
506 xml_writer.AddAttribute("xmlns", "http://www.w3.org/2005/Atom"); | 504 xml_writer.AddAttribute("xmlns", "http://www.w3.org/2005/Atom"); |
507 xml_writer.AddAttribute("xmlns:docs", "http://schemas.google.com/docs/2007"); | 505 xml_writer.AddAttribute("xmlns:docs", "http://schemas.google.com/docs/2007"); |
508 xml_writer.WriteElement("docs:authorizedApp", app_id_); | 506 xml_writer.WriteElement("docs:authorizedApp", app_id_); |
509 | 507 |
510 xml_writer.EndElement(); // Ends "entry" element. | 508 xml_writer.EndElement(); // Ends "entry" element. |
511 xml_writer.StopWriting(); | 509 xml_writer.StopWriting(); |
512 upload_content->assign(xml_writer.GetWrittenString()); | 510 upload_content->assign(xml_writer.GetWrittenString()); |
513 DVLOG(1) << "AuthorizeAppOperation data: " << *upload_content_type << ", [" | 511 DVLOG(1) << "AuthorizeAppOperation data: " << *upload_content_type << ", [" |
514 << *upload_content << "]"; | 512 << *upload_content << "]"; |
515 return true; | 513 return true; |
516 } | 514 } |
517 | 515 |
518 base::Value* AuthorizeAppsOperation::ParseResponse(const std::string& data) { | 516 void AuthorizeAppsOperation::ParseResponse( |
| 517 GDataErrorCode fetch_error_code, |
| 518 const std::string& data) { |
519 // Parse entry XML. | 519 // Parse entry XML. |
520 XmlReader xml_reader; | 520 XmlReader xml_reader; |
521 scoped_ptr<DocumentEntry> entry; | 521 scoped_ptr<DocumentEntry> entry; |
522 if (xml_reader.Load(data)) { | 522 if (xml_reader.Load(data)) { |
523 while (xml_reader.Read()) { | 523 while (xml_reader.Read()) { |
524 if (xml_reader.NodeName() == DocumentEntry::GetEntryNodeName()) { | 524 if (xml_reader.NodeName() == DocumentEntry::GetEntryNodeName()) { |
525 entry.reset(DocumentEntry::CreateFromXml(&xml_reader)); | 525 entry.reset(DocumentEntry::CreateFromXml(&xml_reader)); |
526 break; | 526 break; |
527 } | 527 } |
528 } | 528 } |
529 } | 529 } |
530 | 530 |
531 // From the response, we create a list of the links returned, since those | 531 // From the response, we create a list of the links returned, since those |
532 // are the only things we are interested in. | 532 // are the only things we are interested in. |
533 scoped_ptr<base::ListValue> link_list(new ListValue); | 533 scoped_ptr<base::ListValue> link_list(new ListValue); |
534 const ScopedVector<Link>& feed_links = entry->links(); | 534 const ScopedVector<Link>& feed_links = entry->links(); |
535 for (ScopedVector<Link>::const_iterator iter = feed_links.begin(); | 535 for (ScopedVector<Link>::const_iterator iter = feed_links.begin(); |
536 iter != feed_links.end(); ++iter) { | 536 iter != feed_links.end(); ++iter) { |
537 if ((*iter)->type() == Link::OPEN_WITH) { | 537 if ((*iter)->type() == Link::OPEN_WITH) { |
538 base::DictionaryValue* link = new DictionaryValue; | 538 base::DictionaryValue* link = new DictionaryValue; |
539 link->SetString(std::string("href"), (*iter)->href().spec()); | 539 link->SetString(std::string("href"), (*iter)->href().spec()); |
540 link->SetString(std::string("mime_type"), (*iter)->mime_type()); | 540 link->SetString(std::string("mime_type"), (*iter)->mime_type()); |
541 link->SetString(std::string("title"), (*iter)->title()); | 541 link->SetString(std::string("title"), (*iter)->title()); |
542 link->SetString(std::string("app_id"), (*iter)->app_id()); | 542 link->SetString(std::string("app_id"), (*iter)->app_id()); |
543 link_list->Append(link); | 543 link_list->Append(link); |
544 } | 544 } |
545 } | 545 } |
546 | 546 |
547 return link_list.release(); | 547 RunCallback(fetch_error_code, link_list.PassAs<base::Value>()); |
| 548 const bool success = true; |
| 549 OnProcessURLFetchResultsComplete(success); |
548 } | 550 } |
549 | 551 |
550 GURL AuthorizeAppsOperation::GetURL() const { | 552 GURL AuthorizeAppsOperation::GetURL() const { |
551 return document_url_; | 553 return document_url_; |
552 } | 554 } |
553 | 555 |
554 //======================= AddResourceToDirectoryOperation ====================== | 556 //======================= AddResourceToDirectoryOperation ====================== |
555 | 557 |
556 AddResourceToDirectoryOperation::AddResourceToDirectoryOperation( | 558 AddResourceToDirectoryOperation::AddResourceToDirectoryOperation( |
557 GDataOperationRegistry* registry, | 559 GDataOperationRegistry* registry, |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
650 kUploadParamConvertKey, | 652 kUploadParamConvertKey, |
651 kUploadParamConvertValue)) { | 653 kUploadParamConvertValue)) { |
652 } | 654 } |
653 | 655 |
654 InitiateUploadOperation::~InitiateUploadOperation() {} | 656 InitiateUploadOperation::~InitiateUploadOperation() {} |
655 | 657 |
656 GURL InitiateUploadOperation::GetURL() const { | 658 GURL InitiateUploadOperation::GetURL() const { |
657 return initiate_upload_url_; | 659 return initiate_upload_url_; |
658 } | 660 } |
659 | 661 |
660 bool InitiateUploadOperation::ProcessURLFetchResults( | 662 void InitiateUploadOperation::ProcessURLFetchResults( |
661 const URLFetcher* source) { | 663 const URLFetcher* source) { |
662 GDataErrorCode code = GetErrorCode(source); | 664 GDataErrorCode code = GetErrorCode(source); |
663 | 665 |
664 std::string upload_location; | 666 std::string upload_location; |
665 if (code == HTTP_SUCCESS) { | 667 if (code == HTTP_SUCCESS) { |
666 // Retrieve value of the first "Location" header. | 668 // Retrieve value of the first "Location" header. |
667 source->GetResponseHeaders()->EnumerateHeader(NULL, | 669 source->GetResponseHeaders()->EnumerateHeader(NULL, |
668 kUploadResponseLocation, | 670 kUploadResponseLocation, |
669 &upload_location); | 671 &upload_location); |
670 } | 672 } |
671 VLOG(1) << "Got response for [" << params_.title | 673 VLOG(1) << "Got response for [" << params_.title |
672 << "]: code=" << code | 674 << "]: code=" << code |
673 << ", location=[" << upload_location << "]"; | 675 << ", location=[" << upload_location << "]"; |
674 | 676 |
675 if (!callback_.is_null()) | 677 if (!callback_.is_null()) |
676 callback_.Run(code, GURL(upload_location)); | 678 callback_.Run(code, GURL(upload_location)); |
677 return code == HTTP_SUCCESS; | 679 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); |
678 } | 680 } |
679 | 681 |
680 void InitiateUploadOperation::NotifySuccessToOperationRegistry() { | 682 void InitiateUploadOperation::NotifySuccessToOperationRegistry() { |
681 NotifySuspend(); | 683 NotifySuspend(); |
682 } | 684 } |
683 | 685 |
684 void InitiateUploadOperation::RunCallbackOnPrematureFailure( | 686 void InitiateUploadOperation::RunCallbackOnPrematureFailure( |
685 GDataErrorCode code) { | 687 GDataErrorCode code) { |
686 if (!callback_.is_null()) | 688 if (!callback_.is_null()) |
687 callback_.Run(code, GURL()); | 689 callback_.Run(code, GURL()); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
754 params_(params), | 756 params_(params), |
755 last_chunk_completed_(false) { | 757 last_chunk_completed_(false) { |
756 } | 758 } |
757 | 759 |
758 ResumeUploadOperation::~ResumeUploadOperation() {} | 760 ResumeUploadOperation::~ResumeUploadOperation() {} |
759 | 761 |
760 GURL ResumeUploadOperation::GetURL() const { | 762 GURL ResumeUploadOperation::GetURL() const { |
761 return params_.upload_location; | 763 return params_.upload_location; |
762 } | 764 } |
763 | 765 |
764 bool ResumeUploadOperation::ProcessURLFetchResults( | 766 void ResumeUploadOperation::ProcessURLFetchResults(const URLFetcher* source) { |
765 const URLFetcher* source) { | |
766 GDataErrorCode code = GetErrorCode(source); | 767 GDataErrorCode code = GetErrorCode(source); |
767 net::HttpResponseHeaders* hdrs = source->GetResponseHeaders(); | 768 net::HttpResponseHeaders* hdrs = source->GetResponseHeaders(); |
768 int64 start_range_received = -1; | 769 int64 start_range_received = -1; |
769 int64 end_range_received = -1; | 770 int64 end_range_received = -1; |
770 scoped_ptr<DocumentEntry> entry; | 771 scoped_ptr<DocumentEntry> entry; |
771 | 772 |
772 if (code == HTTP_RESUME_INCOMPLETE) { | 773 if (code == HTTP_RESUME_INCOMPLETE) { |
773 // Retrieve value of the first "Range" header. | 774 // Retrieve value of the first "Range" header. |
774 std::string range_received; | 775 std::string range_received; |
775 hdrs->EnumerateHeader(NULL, kUploadResponseRange, &range_received); | 776 hdrs->EnumerateHeader(NULL, kUploadResponseRange, &range_received); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
816 entry.Pass()); | 817 entry.Pass()); |
817 } | 818 } |
818 | 819 |
819 // For a new file, HTTP_CREATED is returned. | 820 // For a new file, HTTP_CREATED is returned. |
820 // For an existing file, HTTP_SUCCESS is returned. | 821 // For an existing file, HTTP_SUCCESS is returned. |
821 if ((params_.upload_mode == UPLOAD_NEW_FILE && code == HTTP_CREATED) || | 822 if ((params_.upload_mode == UPLOAD_NEW_FILE && code == HTTP_CREATED) || |
822 (params_.upload_mode == UPLOAD_EXISTING_FILE && code == HTTP_SUCCESS)) { | 823 (params_.upload_mode == UPLOAD_EXISTING_FILE && code == HTTP_SUCCESS)) { |
823 last_chunk_completed_ = true; | 824 last_chunk_completed_ = true; |
824 } | 825 } |
825 | 826 |
826 return last_chunk_completed_ || code == HTTP_RESUME_INCOMPLETE; | 827 OnProcessURLFetchResultsComplete( |
| 828 last_chunk_completed_ || code == HTTP_RESUME_INCOMPLETE); |
827 } | 829 } |
828 | 830 |
829 void ResumeUploadOperation::NotifyStartToOperationRegistry() { | 831 void ResumeUploadOperation::NotifyStartToOperationRegistry() { |
830 NotifyResume(); | 832 NotifyResume(); |
831 } | 833 } |
832 | 834 |
833 void ResumeUploadOperation::NotifySuccessToOperationRegistry() { | 835 void ResumeUploadOperation::NotifySuccessToOperationRegistry() { |
834 if (last_chunk_completed_) | 836 if (last_chunk_completed_) |
835 NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED); | 837 NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED); |
836 else | 838 else |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
924 photo_url_(photo_url), | 926 photo_url_(photo_url), |
925 callback_(callback) { | 927 callback_(callback) { |
926 } | 928 } |
927 | 929 |
928 GetContactPhotoOperation::~GetContactPhotoOperation() {} | 930 GetContactPhotoOperation::~GetContactPhotoOperation() {} |
929 | 931 |
930 GURL GetContactPhotoOperation::GetURL() const { | 932 GURL GetContactPhotoOperation::GetURL() const { |
931 return photo_url_; | 933 return photo_url_; |
932 } | 934 } |
933 | 935 |
934 bool GetContactPhotoOperation::ProcessURLFetchResults( | 936 void GetContactPhotoOperation::ProcessURLFetchResults( |
935 const net::URLFetcher* source) { | 937 const net::URLFetcher* source) { |
936 GDataErrorCode code = static_cast<GDataErrorCode>(source->GetResponseCode()); | 938 GDataErrorCode code = static_cast<GDataErrorCode>(source->GetResponseCode()); |
937 scoped_ptr<std::string> data(new std::string); | 939 scoped_ptr<std::string> data(new std::string); |
938 source->GetResponseAsString(data.get()); | 940 source->GetResponseAsString(data.get()); |
939 callback_.Run(code, data.Pass()); | 941 callback_.Run(code, data.Pass()); |
940 return code == HTTP_SUCCESS; | 942 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); |
941 } | 943 } |
942 | 944 |
943 void GetContactPhotoOperation::RunCallbackOnPrematureFailure( | 945 void GetContactPhotoOperation::RunCallbackOnPrematureFailure( |
944 GDataErrorCode code) { | 946 GDataErrorCode code) { |
945 scoped_ptr<std::string> data(new std::string); | 947 scoped_ptr<std::string> data(new std::string); |
946 callback_.Run(code, data.Pass()); | 948 callback_.Run(code, data.Pass()); |
947 } | 949 } |
948 | 950 |
949 } // namespace gdata | 951 } // namespace gdata |
OLD | NEW |