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