Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_operations.cc

Issue 10808027: gdrive: Get JSON feeds parsing off the UI thread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: making OnDataParsed() a private method of GetDataOperation Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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(const URLFetcher* source) {
265 const URLFetcher* source) {
266 GDataErrorCode code = GetErrorCode(source); 265 GDataErrorCode code = GetErrorCode(source);
267 266
268 // Take over the ownership of the the downloaded temp file. 267 // Take over the ownership of the the downloaded temp file.
269 FilePath temp_file; 268 FilePath temp_file;
270 if (code == HTTP_SUCCESS && 269 if (code == HTTP_SUCCESS &&
271 !source->GetResponseAsFilePath(true, // take_ownership 270 !source->GetResponseAsFilePath(true, // take_ownership
272 &temp_file)) { 271 &temp_file)) {
273 code = GDATA_FILE_ERROR; 272 code = GDATA_FILE_ERROR;
274 } 273 }
275 274
276 if (!download_action_callback_.is_null()) 275 if (!download_action_callback_.is_null())
277 download_action_callback_.Run(code, document_url_, temp_file); 276 download_action_callback_.Run(code, document_url_, temp_file);
278 return code == HTTP_SUCCESS; 277 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS);
279 } 278 }
280 279
281 void DownloadFileOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) { 280 void DownloadFileOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) {
282 if (!download_action_callback_.is_null()) 281 if (!download_action_callback_.is_null())
283 download_action_callback_.Run(code, document_url_, FilePath()); 282 download_action_callback_.Run(code, document_url_, FilePath());
284 } 283 }
285 284
286 //=========================== DeleteDocumentOperation ========================== 285 //=========================== DeleteDocumentOperation ==========================
287 286
288 DeleteDocumentOperation::DeleteDocumentOperation( 287 DeleteDocumentOperation::DeleteDocumentOperation(
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 return URLFetcher::PUT; 469 return URLFetcher::PUT;
471 } 470 }
472 471
473 std::vector<std::string> 472 std::vector<std::string>
474 AuthorizeAppsOperation::GetExtraRequestHeaders() const { 473 AuthorizeAppsOperation::GetExtraRequestHeaders() const {
475 std::vector<std::string> headers; 474 std::vector<std::string> headers;
476 headers.push_back(kIfMatchAllHeader); 475 headers.push_back(kIfMatchAllHeader);
477 return headers; 476 return headers;
478 } 477 }
479 478
480 bool AuthorizeAppsOperation::ProcessURLFetchResults( 479 void AuthorizeAppsOperation::ProcessURLFetchResults(const URLFetcher* source) {
481 const URLFetcher* source) {
482 std::string data; 480 std::string data;
483 source->GetResponseAsString(&data); 481 source->GetResponseAsString(&data);
484 return GetDataOperation::ProcessURLFetchResults(source); 482 GetDataOperation::ProcessURLFetchResults(source);
485 } 483 }
486 484
487 bool AuthorizeAppsOperation::GetContentData(std::string* upload_content_type, 485 bool AuthorizeAppsOperation::GetContentData(std::string* upload_content_type,
488 std::string* upload_content) { 486 std::string* upload_content) {
489 upload_content_type->assign("application/atom+xml"); 487 upload_content_type->assign("application/atom+xml");
490 XmlWriter xml_writer; 488 XmlWriter xml_writer;
491 xml_writer.StartWriting(); 489 xml_writer.StartWriting();
492 xml_writer.StartElement("entry"); 490 xml_writer.StartElement("entry");
493 xml_writer.AddAttribute("xmlns", "http://www.w3.org/2005/Atom"); 491 xml_writer.AddAttribute("xmlns", "http://www.w3.org/2005/Atom");
494 xml_writer.AddAttribute("xmlns:docs", "http://schemas.google.com/docs/2007"); 492 xml_writer.AddAttribute("xmlns:docs", "http://schemas.google.com/docs/2007");
495 xml_writer.WriteElement("docs:authorizedApp", app_id_); 493 xml_writer.WriteElement("docs:authorizedApp", app_id_);
496 494
497 xml_writer.EndElement(); // Ends "entry" element. 495 xml_writer.EndElement(); // Ends "entry" element.
498 xml_writer.StopWriting(); 496 xml_writer.StopWriting();
499 upload_content->assign(xml_writer.GetWrittenString()); 497 upload_content->assign(xml_writer.GetWrittenString());
500 DVLOG(1) << "AuthorizeAppOperation data: " << *upload_content_type << ", [" 498 DVLOG(1) << "AuthorizeAppOperation data: " << *upload_content_type << ", ["
501 << *upload_content << "]"; 499 << *upload_content << "]";
502 return true; 500 return true;
503 } 501 }
504 502
505 base::Value* AuthorizeAppsOperation::ParseResponse(const std::string& data) { 503 void AuthorizeAppsOperation::ParseResponse(
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 return link_list.release(); 534 RunCallback(fetch_error_code, link_list.PassAs<base::Value>());
535 const bool success = true;
536 OnProcessURLFetchResultsComplete(success);
535 } 537 }
536 538
537 GURL AuthorizeAppsOperation::GetURL() const { 539 GURL AuthorizeAppsOperation::GetURL() const {
538 return document_url_; 540 return document_url_;
539 } 541 }
540 542
541 //======================= AddResourceToDirectoryOperation ====================== 543 //======================= AddResourceToDirectoryOperation ======================
542 544
543 AddResourceToDirectoryOperation::AddResourceToDirectoryOperation( 545 AddResourceToDirectoryOperation::AddResourceToDirectoryOperation(
544 GDataOperationRegistry* registry, 546 GDataOperationRegistry* registry,
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
637 kUploadParamConvertKey, 639 kUploadParamConvertKey,
638 kUploadParamConvertValue)) { 640 kUploadParamConvertValue)) {
639 } 641 }
640 642
641 InitiateUploadOperation::~InitiateUploadOperation() {} 643 InitiateUploadOperation::~InitiateUploadOperation() {}
642 644
643 GURL InitiateUploadOperation::GetURL() const { 645 GURL InitiateUploadOperation::GetURL() const {
644 return initiate_upload_url_; 646 return initiate_upload_url_;
645 } 647 }
646 648
647 bool InitiateUploadOperation::ProcessURLFetchResults( 649 void InitiateUploadOperation::ProcessURLFetchResults(
648 const URLFetcher* source) { 650 const URLFetcher* source) {
649 GDataErrorCode code = GetErrorCode(source); 651 GDataErrorCode code = GetErrorCode(source);
650 652
651 std::string upload_location; 653 std::string upload_location;
652 if (code == HTTP_SUCCESS) { 654 if (code == HTTP_SUCCESS) {
653 // Retrieve value of the first "Location" header. 655 // Retrieve value of the first "Location" header.
654 source->GetResponseHeaders()->EnumerateHeader(NULL, 656 source->GetResponseHeaders()->EnumerateHeader(NULL,
655 kUploadResponseLocation, 657 kUploadResponseLocation,
656 &upload_location); 658 &upload_location);
657 } 659 }
658 VLOG(1) << "Got response for [" << params_.title 660 VLOG(1) << "Got response for [" << params_.title
659 << "]: code=" << code 661 << "]: code=" << code
660 << ", location=[" << upload_location << "]"; 662 << ", location=[" << upload_location << "]";
661 663
662 if (!callback_.is_null()) 664 if (!callback_.is_null())
663 callback_.Run(code, GURL(upload_location)); 665 callback_.Run(code, GURL(upload_location));
664 return code == HTTP_SUCCESS; 666 OnProcessURLFetchResultsComplete(code == HTTP_SUCCESS);
665 } 667 }
666 668
667 void InitiateUploadOperation::NotifySuccessToOperationRegistry() { 669 void InitiateUploadOperation::NotifySuccessToOperationRegistry() {
668 NotifySuspend(); 670 NotifySuspend();
669 } 671 }
670 672
671 void InitiateUploadOperation::RunCallbackOnPrematureFailure( 673 void InitiateUploadOperation::RunCallbackOnPrematureFailure(
672 GDataErrorCode code) { 674 GDataErrorCode code) {
673 if (!callback_.is_null()) 675 if (!callback_.is_null())
674 callback_.Run(code, GURL()); 676 callback_.Run(code, GURL());
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
741 params_(params), 743 params_(params),
742 last_chunk_completed_(false) { 744 last_chunk_completed_(false) {
743 } 745 }
744 746
745 ResumeUploadOperation::~ResumeUploadOperation() {} 747 ResumeUploadOperation::~ResumeUploadOperation() {}
746 748
747 GURL ResumeUploadOperation::GetURL() const { 749 GURL ResumeUploadOperation::GetURL() const {
748 return params_.upload_location; 750 return params_.upload_location;
749 } 751 }
750 752
751 bool ResumeUploadOperation::ProcessURLFetchResults( 753 void ResumeUploadOperation::ProcessURLFetchResults(const URLFetcher* source) {
752 const URLFetcher* source) {
753 GDataErrorCode code = GetErrorCode(source); 754 GDataErrorCode code = GetErrorCode(source);
754 net::HttpResponseHeaders* hdrs = source->GetResponseHeaders(); 755 net::HttpResponseHeaders* hdrs = source->GetResponseHeaders();
755 int64 start_range_received = -1; 756 int64 start_range_received = -1;
756 int64 end_range_received = -1; 757 int64 end_range_received = -1;
757 scoped_ptr<DocumentEntry> entry; 758 scoped_ptr<DocumentEntry> entry;
758 759
759 if (code == HTTP_RESUME_INCOMPLETE) { 760 if (code == HTTP_RESUME_INCOMPLETE) {
760 // Retrieve value of the first "Range" header. 761 // Retrieve value of the first "Range" header.
761 std::string range_received; 762 std::string range_received;
762 hdrs->EnumerateHeader(NULL, kUploadResponseRange, &range_received); 763 hdrs->EnumerateHeader(NULL, kUploadResponseRange, &range_received);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
803 entry.Pass()); 804 entry.Pass());
804 } 805 }
805 806
806 // For a new file, HTTP_CREATED is returned. 807 // For a new file, HTTP_CREATED is returned.
807 // For an existing file, HTTP_SUCCESS is returned. 808 // For an existing file, HTTP_SUCCESS is returned.
808 if ((params_.upload_mode == UPLOAD_NEW_FILE && code == HTTP_CREATED) || 809 if ((params_.upload_mode == UPLOAD_NEW_FILE && code == HTTP_CREATED) ||
809 (params_.upload_mode == UPLOAD_EXISTING_FILE && code == HTTP_SUCCESS)) { 810 (params_.upload_mode == UPLOAD_EXISTING_FILE && code == HTTP_SUCCESS)) {
810 last_chunk_completed_ = true; 811 last_chunk_completed_ = true;
811 } 812 }
812 813
813 return last_chunk_completed_ || code == HTTP_RESUME_INCOMPLETE; 814 OnProcessURLFetchResultsComplete(
815 last_chunk_completed_ || code == HTTP_RESUME_INCOMPLETE);
814 } 816 }
815 817
816 void ResumeUploadOperation::NotifyStartToOperationRegistry() { 818 void ResumeUploadOperation::NotifyStartToOperationRegistry() {
817 NotifyResume(); 819 NotifyResume();
818 } 820 }
819 821
820 void ResumeUploadOperation::NotifySuccessToOperationRegistry() { 822 void ResumeUploadOperation::NotifySuccessToOperationRegistry() {
821 if (last_chunk_completed_) 823 if (last_chunk_completed_)
822 NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED); 824 NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED);
823 else 825 else
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
869 return true; 871 return true;
870 } 872 }
871 873
872 void ResumeUploadOperation::OnURLFetchUploadProgress( 874 void ResumeUploadOperation::OnURLFetchUploadProgress(
873 const URLFetcher* source, int64 current, int64 total) { 875 const URLFetcher* source, int64 current, int64 total) {
874 // Adjust the progress values according to the range currently uploaded. 876 // Adjust the progress values according to the range currently uploaded.
875 NotifyProgress(params_.start_range + current, params_.content_length); 877 NotifyProgress(params_.start_range + current, params_.content_length);
876 } 878 }
877 879
878 } // namespace gdata 880 } // namespace gdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698