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