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/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 // URL for fetching documents in a particular directory. | 152 // URL for fetching documents in a particular directory. |
153 GURL FormatDocumentListURL(const std::string& directory_resource_id) { | 153 GURL FormatDocumentListURL(const std::string& directory_resource_id) { |
154 if (directory_resource_id.empty()) | 154 if (directory_resource_id.empty()) |
155 return GURL(kGetDocumentListURLForAllDocuments); | 155 return GURL(kGetDocumentListURLForAllDocuments); |
156 | 156 |
157 return GURL(base::StringPrintf(kGetDocumentListURLForDirectoryFormat, | 157 return GURL(base::StringPrintf(kGetDocumentListURLForDirectoryFormat, |
158 net::EscapePath( | 158 net::EscapePath( |
159 directory_resource_id).c_str())); | 159 directory_resource_id).c_str())); |
160 } | 160 } |
161 | 161 |
162 // Parse JSON string to base::Value object. | |
163 void ParseJsonOnBlockingPool(const std::string& data, | |
164 scoped_ptr<base::Value>* value) { | |
165 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
166 | |
167 int error_code = -1; | |
168 std::string error_message; | |
169 value->reset(base::JSONReader::ReadAndReturnError(data, | |
170 base::JSON_PARSE_RFC, | |
171 &error_code, | |
172 &error_message)); | |
173 | |
174 if (!value->get()) { | |
175 LOG(ERROR) << "Error while parsing entry response: " | |
176 << error_message | |
177 << ", code: " | |
178 << error_code | |
179 << ", data:\n" | |
180 << data; | |
181 } | |
182 } | |
183 | |
184 } // namespace | 162 } // namespace |
185 | 163 |
186 namespace gdata { | 164 namespace gdata { |
187 | 165 |
188 //================================ AuthOperation =============================== | 166 //================================ AuthOperation =============================== |
189 | 167 |
190 AuthOperation::AuthOperation(GDataOperationRegistry* registry, | 168 AuthOperation::AuthOperation(GDataOperationRegistry* registry, |
191 Profile* profile, | 169 Profile* profile, |
192 const AuthStatusCallback& callback, | 170 const AuthStatusCallback& callback, |
193 const std::string& refresh_token) | 171 const std::string& refresh_token) |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 void EntryActionOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) { | 440 void EntryActionOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) { |
463 if (!callback_.is_null()) | 441 if (!callback_.is_null()) |
464 callback_.Run(code, document_url_); | 442 callback_.Run(code, document_url_); |
465 } | 443 } |
466 | 444 |
467 //============================== GetDataOperation ============================== | 445 //============================== GetDataOperation ============================== |
468 | 446 |
469 GetDataOperation::GetDataOperation(GDataOperationRegistry* registry, | 447 GetDataOperation::GetDataOperation(GDataOperationRegistry* registry, |
470 Profile* profile, | 448 Profile* profile, |
471 const GetDataCallback& callback) | 449 const GetDataCallback& callback) |
472 : UrlFetchOperationBase(registry, profile), | 450 : UrlFetchOperationBase(registry, profile), callback_(callback) { |
473 callback_(callback), | |
474 weak_ptr_factory_(this) { | |
475 } | 451 } |
476 | 452 |
477 GetDataOperation::~GetDataOperation() {} | 453 GetDataOperation::~GetDataOperation() {} |
478 | 454 |
479 bool GetDataOperation::ProcessURLFetchResults(const net::URLFetcher* source) { | 455 bool GetDataOperation::ProcessURLFetchResults(const net::URLFetcher* source) { |
480 std::string data; | 456 std::string data; |
481 source->GetResponseAsString(&data); | 457 source->GetResponseAsString(&data); |
482 GDataErrorCode fetch_error_code = GetErrorCode(source); | 458 scoped_ptr<base::Value> root_value; |
483 bool ret = false; | 459 GDataErrorCode code = GetErrorCode(source); |
484 | 460 |
485 switch (fetch_error_code) { | 461 switch (code) { |
486 case HTTP_SUCCESS: | 462 case HTTP_SUCCESS: |
487 case HTTP_CREATED: | 463 case HTTP_CREATED: { |
488 ret = ParseResponse(fetch_error_code, data); | 464 root_value.reset(ParseResponse(data)); |
| 465 if (!root_value.get()) |
| 466 code = GDATA_PARSE_ERROR; |
| 467 |
489 break; | 468 break; |
| 469 } |
490 default: | 470 default: |
491 RunCallback(fetch_error_code, scoped_ptr<base::Value>()); | |
492 break; | 471 break; |
493 } | 472 } |
494 | 473 |
495 return ret; | 474 if (!callback_.is_null()) |
| 475 callback_.Run(code, root_value.Pass()); |
| 476 return root_value.get() != NULL; |
496 } | 477 } |
497 | 478 |
498 void GetDataOperation::RunCallbackOnPrematureFailure( | 479 void GetDataOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) { |
499 GDataErrorCode fetch_error_code) { | |
500 if (!callback_.is_null()) { | 480 if (!callback_.is_null()) { |
501 scoped_ptr<base::Value> root_value; | 481 scoped_ptr<base::Value> root_value; |
502 callback_.Run(fetch_error_code, root_value.Pass()); | 482 callback_.Run(code, root_value.Pass()); |
503 } | 483 } |
504 } | 484 } |
505 | 485 |
506 bool GetDataOperation::ParseResponse(GDataErrorCode fetch_error_code, | 486 base::Value* GetDataOperation::ParseResponse(const std::string& data) { |
507 const std::string& data) { | 487 int error_code = -1; |
508 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 488 std::string error_message; |
509 | 489 scoped_ptr<base::Value> root_value(base::JSONReader::ReadAndReturnError( |
510 // Uses this hack to avoid deep-copy of json object because json might be so | 490 data, base::JSON_PARSE_RFC, &error_code, &error_message)); |
511 // big. | 491 if (!root_value.get()) { |
512 // This pointer of scped_ptr is to ensure a deletion of the parsed json value | 492 LOG(ERROR) << "Error while parsing entry response: " |
513 // object, even when OnDataParsed() is not called. | 493 << error_message |
514 scoped_ptr<base::Value>* parsed_value = new scoped_ptr<base::Value>(); | 494 << ", code: " |
515 | 495 << error_code |
516 return BrowserThread::PostBlockingPoolTaskAndReply( | 496 << ", data:\n" |
517 FROM_HERE, | 497 << data; |
518 base::Bind(&ParseJsonOnBlockingPool, | 498 return NULL; |
519 data, | 499 } |
520 parsed_value), | 500 return root_value.release(); |
521 base::Bind(&GetDataOperation::OnDataParsed, | |
522 weak_ptr_factory_.GetWeakPtr(), | |
523 fetch_error_code, | |
524 base::Owned(parsed_value))); | |
525 } | |
526 | |
527 void GetDataOperation::RunCallback(GDataErrorCode fetch_error_code, | |
528 scoped_ptr<base::Value> value) { | |
529 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
530 if (!callback_.is_null()) | |
531 callback_.Run(fetch_error_code, value.Pass()); | |
532 } | |
533 | |
534 void GetDataOperation::OnDataParsed(GDataErrorCode fetch_error_code, | |
535 scoped_ptr<base::Value>* value) { | |
536 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
537 if (!value->get()) | |
538 fetch_error_code = GDATA_PARSE_ERROR; | |
539 | |
540 // The ownership of the parsed json object is transfered to RunCallBack(), | |
541 // keeping the ownership of the |value| here. | |
542 RunCallback(fetch_error_code, value->Pass()); | |
543 DCHECK(!value->get()); | |
544 | |
545 // |value| will be deleted after return beause it is base::Owned()'d. | |
546 } | 501 } |
547 | 502 |
548 //============================ GetDocumentsOperation =========================== | 503 //============================ GetDocumentsOperation =========================== |
549 | 504 |
550 GetDocumentsOperation::GetDocumentsOperation( | 505 GetDocumentsOperation::GetDocumentsOperation( |
551 GDataOperationRegistry* registry, | 506 GDataOperationRegistry* registry, |
552 Profile* profile, | 507 Profile* profile, |
553 int start_changestamp, | 508 int start_changestamp, |
554 const std::string& search_string, | 509 const std::string& search_string, |
555 const std::string& directory_resource_id, | 510 const std::string& directory_resource_id, |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
896 xml_writer.WriteElement("docs:authorizedApp", app_id_); | 851 xml_writer.WriteElement("docs:authorizedApp", app_id_); |
897 | 852 |
898 xml_writer.EndElement(); // Ends "entry" element. | 853 xml_writer.EndElement(); // Ends "entry" element. |
899 xml_writer.StopWriting(); | 854 xml_writer.StopWriting(); |
900 upload_content->assign(xml_writer.GetWrittenString()); | 855 upload_content->assign(xml_writer.GetWrittenString()); |
901 DVLOG(1) << "AuthorizeAppOperation data: " << *upload_content_type << ", [" | 856 DVLOG(1) << "AuthorizeAppOperation data: " << *upload_content_type << ", [" |
902 << *upload_content << "]"; | 857 << *upload_content << "]"; |
903 return true; | 858 return true; |
904 } | 859 } |
905 | 860 |
906 bool AuthorizeAppsOperation::ParseResponse(GDataErrorCode code, | 861 base::Value* AuthorizeAppsOperation::ParseResponse(const std::string& data) { |
907 const std::string& data) { | |
908 // Parse entry XML. | 862 // Parse entry XML. |
909 XmlReader xml_reader; | 863 XmlReader xml_reader; |
910 scoped_ptr<DocumentEntry> entry; | 864 scoped_ptr<DocumentEntry> entry; |
911 if (xml_reader.Load(data)) { | 865 if (xml_reader.Load(data)) { |
912 while (xml_reader.Read()) { | 866 while (xml_reader.Read()) { |
913 if (xml_reader.NodeName() == DocumentEntry::GetEntryNodeName()) { | 867 if (xml_reader.NodeName() == DocumentEntry::GetEntryNodeName()) { |
914 entry.reset(DocumentEntry::CreateFromXml(&xml_reader)); | 868 entry.reset(DocumentEntry::CreateFromXml(&xml_reader)); |
915 break; | 869 break; |
916 } | 870 } |
917 } | 871 } |
918 } | 872 } |
919 | 873 |
920 // From the response, we create a list of the links returned, since those | 874 // From the response, we create a list of the links returned, since those |
921 // are the only things we are interested in. | 875 // are the only things we are interested in. |
922 scoped_ptr<base::ListValue> link_list(new ListValue); | 876 scoped_ptr<base::ListValue> link_list(new ListValue); |
923 const ScopedVector<Link>& feed_links = entry->links(); | 877 const ScopedVector<Link>& feed_links = entry->links(); |
924 for (ScopedVector<Link>::const_iterator iter = feed_links.begin(); | 878 for (ScopedVector<Link>::const_iterator iter = feed_links.begin(); |
925 iter != feed_links.end(); ++iter) { | 879 iter != feed_links.end(); ++iter) { |
926 if ((*iter)->type() == Link::OPEN_WITH) { | 880 if ((*iter)->type() == Link::OPEN_WITH) { |
927 base::DictionaryValue* link = new DictionaryValue; | 881 base::DictionaryValue* link = new DictionaryValue; |
928 link->SetString(std::string("href"), (*iter)->href().spec()); | 882 link->SetString(std::string("href"), (*iter)->href().spec()); |
929 link->SetString(std::string("mime_type"), (*iter)->mime_type()); | 883 link->SetString(std::string("mime_type"), (*iter)->mime_type()); |
930 link->SetString(std::string("title"), (*iter)->title()); | 884 link->SetString(std::string("title"), (*iter)->title()); |
931 link->SetString(std::string("app_id"), (*iter)->app_id()); | 885 link->SetString(std::string("app_id"), (*iter)->app_id()); |
932 link_list->Append(link); | 886 link_list->Append(link); |
933 } | 887 } |
934 } | 888 } |
935 | 889 |
936 RunCallback(code, link_list.PassAs<base::Value>()); | 890 return link_list.release(); |
937 | |
938 return true; | |
939 } | 891 } |
940 | 892 |
941 GURL AuthorizeAppsOperation::GetURL() const { | 893 GURL AuthorizeAppsOperation::GetURL() const { |
942 return document_url_; | 894 return document_url_; |
943 } | 895 } |
944 | 896 |
945 //======================= AddResourceToDirectoryOperation ====================== | 897 //======================= AddResourceToDirectoryOperation ====================== |
946 | 898 |
947 AddResourceToDirectoryOperation::AddResourceToDirectoryOperation( | 899 AddResourceToDirectoryOperation::AddResourceToDirectoryOperation( |
948 GDataOperationRegistry* registry, | 900 GDataOperationRegistry* registry, |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1273 return true; | 1225 return true; |
1274 } | 1226 } |
1275 | 1227 |
1276 void ResumeUploadOperation::OnURLFetchUploadProgress( | 1228 void ResumeUploadOperation::OnURLFetchUploadProgress( |
1277 const net::URLFetcher* source, int64 current, int64 total) { | 1229 const net::URLFetcher* source, int64 current, int64 total) { |
1278 // Adjust the progress values according to the range currently uploaded. | 1230 // Adjust the progress values according to the range currently uploaded. |
1279 NotifyProgress(params_.start_range + current, params_.content_length); | 1231 NotifyProgress(params_.start_range + current, params_.content_length); |
1280 } | 1232 } |
1281 | 1233 |
1282 } // namespace gdata | 1234 } // namespace gdata |
OLD | NEW |