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_wapi_parser.h" | 10 #include "chrome/browser/chromeos/gdata/gdata_wapi_parser.h" |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 return !get_download_data_callback_.is_null(); | 254 return !get_download_data_callback_.is_null(); |
255 } | 255 } |
256 | 256 |
257 void DownloadFileOperation::OnURLFetchDownloadData( | 257 void DownloadFileOperation::OnURLFetchDownloadData( |
258 const URLFetcher* source, | 258 const URLFetcher* source, |
259 scoped_ptr<std::string> download_data) { | 259 scoped_ptr<std::string> download_data) { |
260 if (!get_download_data_callback_.is_null()) | 260 if (!get_download_data_callback_.is_null()) |
261 get_download_data_callback_.Run(HTTP_SUCCESS, download_data.Pass()); | 261 get_download_data_callback_.Run(HTTP_SUCCESS, download_data.Pass()); |
262 } | 262 } |
263 | 263 |
264 void DownloadFileOperation::ProcessURLFetchResults(const URLFetcher* source) { | 264 bool DownloadFileOperation::ProcessURLFetchResults( |
| 265 const URLFetcher* source) { |
265 GDataErrorCode code = GetErrorCode(source); | 266 GDataErrorCode code = GetErrorCode(source); |
266 | 267 |
267 // Take over the ownership of the the downloaded temp file. | 268 // Take over the ownership of the the downloaded temp file. |
268 FilePath temp_file; | 269 FilePath temp_file; |
269 if (code == HTTP_SUCCESS && | 270 if (code == HTTP_SUCCESS && |
270 !source->GetResponseAsFilePath(true, // take_ownership | 271 !source->GetResponseAsFilePath(true, // take_ownership |
271 &temp_file)) { | 272 &temp_file)) { |
272 code = GDATA_FILE_ERROR; | 273 code = GDATA_FILE_ERROR; |
273 } | 274 } |
274 | 275 |
275 if (!download_action_callback_.is_null()) | 276 if (!download_action_callback_.is_null()) |
276 download_action_callback_.Run(code, document_url_, temp_file); | 277 download_action_callback_.Run(code, document_url_, temp_file); |
277 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); | 278 return code == HTTP_SUCCESS; |
278 } | 279 } |
279 | 280 |
280 void DownloadFileOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) { | 281 void DownloadFileOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) { |
281 if (!download_action_callback_.is_null()) | 282 if (!download_action_callback_.is_null()) |
282 download_action_callback_.Run(code, document_url_, FilePath()); | 283 download_action_callback_.Run(code, document_url_, FilePath()); |
283 } | 284 } |
284 | 285 |
285 //=========================== DeleteDocumentOperation ========================== | 286 //=========================== DeleteDocumentOperation ========================== |
286 | 287 |
287 DeleteDocumentOperation::DeleteDocumentOperation( | 288 DeleteDocumentOperation::DeleteDocumentOperation( |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 return URLFetcher::PUT; | 470 return URLFetcher::PUT; |
470 } | 471 } |
471 | 472 |
472 std::vector<std::string> | 473 std::vector<std::string> |
473 AuthorizeAppsOperation::GetExtraRequestHeaders() const { | 474 AuthorizeAppsOperation::GetExtraRequestHeaders() const { |
474 std::vector<std::string> headers; | 475 std::vector<std::string> headers; |
475 headers.push_back(kIfMatchAllHeader); | 476 headers.push_back(kIfMatchAllHeader); |
476 return headers; | 477 return headers; |
477 } | 478 } |
478 | 479 |
479 void AuthorizeAppsOperation::ProcessURLFetchResults(const URLFetcher* source) { | 480 bool AuthorizeAppsOperation::ProcessURLFetchResults( |
| 481 const URLFetcher* source) { |
480 std::string data; | 482 std::string data; |
481 source->GetResponseAsString(&data); | 483 source->GetResponseAsString(&data); |
482 GetDataOperation::ProcessURLFetchResults(source); | 484 return GetDataOperation::ProcessURLFetchResults(source); |
483 } | 485 } |
484 | 486 |
485 bool AuthorizeAppsOperation::GetContentData(std::string* upload_content_type, | 487 bool AuthorizeAppsOperation::GetContentData(std::string* upload_content_type, |
486 std::string* upload_content) { | 488 std::string* upload_content) { |
487 upload_content_type->assign("application/atom+xml"); | 489 upload_content_type->assign("application/atom+xml"); |
488 XmlWriter xml_writer; | 490 XmlWriter xml_writer; |
489 xml_writer.StartWriting(); | 491 xml_writer.StartWriting(); |
490 xml_writer.StartElement("entry"); | 492 xml_writer.StartElement("entry"); |
491 xml_writer.AddAttribute("xmlns", "http://www.w3.org/2005/Atom"); | 493 xml_writer.AddAttribute("xmlns", "http://www.w3.org/2005/Atom"); |
492 xml_writer.AddAttribute("xmlns:docs", "http://schemas.google.com/docs/2007"); | 494 xml_writer.AddAttribute("xmlns:docs", "http://schemas.google.com/docs/2007"); |
493 xml_writer.WriteElement("docs:authorizedApp", app_id_); | 495 xml_writer.WriteElement("docs:authorizedApp", app_id_); |
494 | 496 |
495 xml_writer.EndElement(); // Ends "entry" element. | 497 xml_writer.EndElement(); // Ends "entry" element. |
496 xml_writer.StopWriting(); | 498 xml_writer.StopWriting(); |
497 upload_content->assign(xml_writer.GetWrittenString()); | 499 upload_content->assign(xml_writer.GetWrittenString()); |
498 DVLOG(1) << "AuthorizeAppOperation data: " << *upload_content_type << ", [" | 500 DVLOG(1) << "AuthorizeAppOperation data: " << *upload_content_type << ", [" |
499 << *upload_content << "]"; | 501 << *upload_content << "]"; |
500 return true; | 502 return true; |
501 } | 503 } |
502 | 504 |
503 void AuthorizeAppsOperation::ParseResponse( | 505 base::Value* AuthorizeAppsOperation::ParseResponse(const std::string& data) { |
504 GDataErrorCode fetch_error_code, | |
505 const std::string& data) { | |
506 // Parse entry XML. | 506 // Parse entry XML. |
507 XmlReader xml_reader; | 507 XmlReader xml_reader; |
508 scoped_ptr<DocumentEntry> entry; | 508 scoped_ptr<DocumentEntry> entry; |
509 if (xml_reader.Load(data)) { | 509 if (xml_reader.Load(data)) { |
510 while (xml_reader.Read()) { | 510 while (xml_reader.Read()) { |
511 if (xml_reader.NodeName() == DocumentEntry::GetEntryNodeName()) { | 511 if (xml_reader.NodeName() == DocumentEntry::GetEntryNodeName()) { |
512 entry.reset(DocumentEntry::CreateFromXml(&xml_reader)); | 512 entry.reset(DocumentEntry::CreateFromXml(&xml_reader)); |
513 break; | 513 break; |
514 } | 514 } |
515 } | 515 } |
516 } | 516 } |
517 | 517 |
518 // From the response, we create a list of the links returned, since those | 518 // From the response, we create a list of the links returned, since those |
519 // are the only things we are interested in. | 519 // are the only things we are interested in. |
520 scoped_ptr<base::ListValue> link_list(new ListValue); | 520 scoped_ptr<base::ListValue> link_list(new ListValue); |
521 const ScopedVector<Link>& feed_links = entry->links(); | 521 const ScopedVector<Link>& feed_links = entry->links(); |
522 for (ScopedVector<Link>::const_iterator iter = feed_links.begin(); | 522 for (ScopedVector<Link>::const_iterator iter = feed_links.begin(); |
523 iter != feed_links.end(); ++iter) { | 523 iter != feed_links.end(); ++iter) { |
524 if ((*iter)->type() == Link::OPEN_WITH) { | 524 if ((*iter)->type() == Link::OPEN_WITH) { |
525 base::DictionaryValue* link = new DictionaryValue; | 525 base::DictionaryValue* link = new DictionaryValue; |
526 link->SetString(std::string("href"), (*iter)->href().spec()); | 526 link->SetString(std::string("href"), (*iter)->href().spec()); |
527 link->SetString(std::string("mime_type"), (*iter)->mime_type()); | 527 link->SetString(std::string("mime_type"), (*iter)->mime_type()); |
528 link->SetString(std::string("title"), (*iter)->title()); | 528 link->SetString(std::string("title"), (*iter)->title()); |
529 link->SetString(std::string("app_id"), (*iter)->app_id()); | 529 link->SetString(std::string("app_id"), (*iter)->app_id()); |
530 link_list->Append(link); | 530 link_list->Append(link); |
531 } | 531 } |
532 } | 532 } |
533 | 533 |
534 RunCallback(fetch_error_code, link_list.PassAs<base::Value>()); | 534 return link_list.release(); |
535 const bool success = true; | |
536 OnProcessURLFetchResultsComplete(success); | |
537 } | 535 } |
538 | 536 |
539 GURL AuthorizeAppsOperation::GetURL() const { | 537 GURL AuthorizeAppsOperation::GetURL() const { |
540 return document_url_; | 538 return document_url_; |
541 } | 539 } |
542 | 540 |
543 //======================= AddResourceToDirectoryOperation ====================== | 541 //======================= AddResourceToDirectoryOperation ====================== |
544 | 542 |
545 AddResourceToDirectoryOperation::AddResourceToDirectoryOperation( | 543 AddResourceToDirectoryOperation::AddResourceToDirectoryOperation( |
546 GDataOperationRegistry* registry, | 544 GDataOperationRegistry* registry, |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
639 kUploadParamConvertKey, | 637 kUploadParamConvertKey, |
640 kUploadParamConvertValue)) { | 638 kUploadParamConvertValue)) { |
641 } | 639 } |
642 | 640 |
643 InitiateUploadOperation::~InitiateUploadOperation() {} | 641 InitiateUploadOperation::~InitiateUploadOperation() {} |
644 | 642 |
645 GURL InitiateUploadOperation::GetURL() const { | 643 GURL InitiateUploadOperation::GetURL() const { |
646 return initiate_upload_url_; | 644 return initiate_upload_url_; |
647 } | 645 } |
648 | 646 |
649 void InitiateUploadOperation::ProcessURLFetchResults( | 647 bool InitiateUploadOperation::ProcessURLFetchResults( |
650 const URLFetcher* source) { | 648 const URLFetcher* source) { |
651 GDataErrorCode code = GetErrorCode(source); | 649 GDataErrorCode code = GetErrorCode(source); |
652 | 650 |
653 std::string upload_location; | 651 std::string upload_location; |
654 if (code == HTTP_SUCCESS) { | 652 if (code == HTTP_SUCCESS) { |
655 // Retrieve value of the first "Location" header. | 653 // Retrieve value of the first "Location" header. |
656 source->GetResponseHeaders()->EnumerateHeader(NULL, | 654 source->GetResponseHeaders()->EnumerateHeader(NULL, |
657 kUploadResponseLocation, | 655 kUploadResponseLocation, |
658 &upload_location); | 656 &upload_location); |
659 } | 657 } |
660 VLOG(1) << "Got response for [" << params_.title | 658 VLOG(1) << "Got response for [" << params_.title |
661 << "]: code=" << code | 659 << "]: code=" << code |
662 << ", location=[" << upload_location << "]"; | 660 << ", location=[" << upload_location << "]"; |
663 | 661 |
664 if (!callback_.is_null()) | 662 if (!callback_.is_null()) |
665 callback_.Run(code, GURL(upload_location)); | 663 callback_.Run(code, GURL(upload_location)); |
666 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS); | 664 return code == HTTP_SUCCESS; |
667 } | 665 } |
668 | 666 |
669 void InitiateUploadOperation::NotifySuccessToOperationRegistry() { | 667 void InitiateUploadOperation::NotifySuccessToOperationRegistry() { |
670 NotifySuspend(); | 668 NotifySuspend(); |
671 } | 669 } |
672 | 670 |
673 void InitiateUploadOperation::RunCallbackOnPrematureFailure( | 671 void InitiateUploadOperation::RunCallbackOnPrematureFailure( |
674 GDataErrorCode code) { | 672 GDataErrorCode code) { |
675 if (!callback_.is_null()) | 673 if (!callback_.is_null()) |
676 callback_.Run(code, GURL()); | 674 callback_.Run(code, GURL()); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
743 params_(params), | 741 params_(params), |
744 last_chunk_completed_(false) { | 742 last_chunk_completed_(false) { |
745 } | 743 } |
746 | 744 |
747 ResumeUploadOperation::~ResumeUploadOperation() {} | 745 ResumeUploadOperation::~ResumeUploadOperation() {} |
748 | 746 |
749 GURL ResumeUploadOperation::GetURL() const { | 747 GURL ResumeUploadOperation::GetURL() const { |
750 return params_.upload_location; | 748 return params_.upload_location; |
751 } | 749 } |
752 | 750 |
753 void ResumeUploadOperation::ProcessURLFetchResults(const URLFetcher* source) { | 751 bool ResumeUploadOperation::ProcessURLFetchResults( |
| 752 const URLFetcher* source) { |
754 GDataErrorCode code = GetErrorCode(source); | 753 GDataErrorCode code = GetErrorCode(source); |
755 net::HttpResponseHeaders* hdrs = source->GetResponseHeaders(); | 754 net::HttpResponseHeaders* hdrs = source->GetResponseHeaders(); |
756 int64 start_range_received = -1; | 755 int64 start_range_received = -1; |
757 int64 end_range_received = -1; | 756 int64 end_range_received = -1; |
758 scoped_ptr<DocumentEntry> entry; | 757 scoped_ptr<DocumentEntry> entry; |
759 | 758 |
760 if (code == HTTP_RESUME_INCOMPLETE) { | 759 if (code == HTTP_RESUME_INCOMPLETE) { |
761 // Retrieve value of the first "Range" header. | 760 // Retrieve value of the first "Range" header. |
762 std::string range_received; | 761 std::string range_received; |
763 hdrs->EnumerateHeader(NULL, kUploadResponseRange, &range_received); | 762 hdrs->EnumerateHeader(NULL, kUploadResponseRange, &range_received); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
804 entry.Pass()); | 803 entry.Pass()); |
805 } | 804 } |
806 | 805 |
807 // For a new file, HTTP_CREATED is returned. | 806 // For a new file, HTTP_CREATED is returned. |
808 // For an existing file, HTTP_SUCCESS is returned. | 807 // For an existing file, HTTP_SUCCESS is returned. |
809 if ((params_.upload_mode == UPLOAD_NEW_FILE && code == HTTP_CREATED) || | 808 if ((params_.upload_mode == UPLOAD_NEW_FILE && code == HTTP_CREATED) || |
810 (params_.upload_mode == UPLOAD_EXISTING_FILE && code == HTTP_SUCCESS)) { | 809 (params_.upload_mode == UPLOAD_EXISTING_FILE && code == HTTP_SUCCESS)) { |
811 last_chunk_completed_ = true; | 810 last_chunk_completed_ = true; |
812 } | 811 } |
813 | 812 |
814 OnProcessURLFetchResultsComplete( | 813 return last_chunk_completed_ || code == HTTP_RESUME_INCOMPLETE; |
815 last_chunk_completed_ || code == HTTP_RESUME_INCOMPLETE); | |
816 } | 814 } |
817 | 815 |
818 void ResumeUploadOperation::NotifyStartToOperationRegistry() { | 816 void ResumeUploadOperation::NotifyStartToOperationRegistry() { |
819 NotifyResume(); | 817 NotifyResume(); |
820 } | 818 } |
821 | 819 |
822 void ResumeUploadOperation::NotifySuccessToOperationRegistry() { | 820 void ResumeUploadOperation::NotifySuccessToOperationRegistry() { |
823 if (last_chunk_completed_) | 821 if (last_chunk_completed_) |
824 NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED); | 822 NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED); |
825 else | 823 else |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
871 return true; | 869 return true; |
872 } | 870 } |
873 | 871 |
874 void ResumeUploadOperation::OnURLFetchUploadProgress( | 872 void ResumeUploadOperation::OnURLFetchUploadProgress( |
875 const URLFetcher* source, int64 current, int64 total) { | 873 const URLFetcher* source, int64 current, int64 total) { |
876 // Adjust the progress values according to the range currently uploaded. | 874 // Adjust the progress values according to the range currently uploaded. |
877 NotifyProgress(params_.start_range + current, params_.content_length); | 875 NotifyProgress(params_.start_range + current, params_.content_length); |
878 } | 876 } |
879 | 877 |
880 } // namespace gdata | 878 } // namespace gdata |
OLD | NEW |