Index: cloud_print/gcp20/prototype/cloud_print_response_parser.cc |
diff --git a/cloud_print/gcp20/prototype/cloud_print_response_parser.cc b/cloud_print/gcp20/prototype/cloud_print_response_parser.cc |
index fa2c5f7f83ffd0c95caaa09331becd953b6da3b4..288ceb5d207c25d410834c9b1ec9ff5413a5c29c 100644 |
--- a/cloud_print/gcp20/prototype/cloud_print_response_parser.cc |
+++ b/cloud_print/gcp20/prototype/cloud_print_response_parser.cc |
@@ -10,6 +10,12 @@ |
namespace cloud_print_response_parser { |
+Printjob::Printjob() { |
+} |
+ |
+Printjob::~Printjob() { |
+} |
+ |
// Parses json base::Value to base::DictionaryValue and checks |success| status. |
// Returns |true| on success. |
bool GetJsonDictinaryAndCheckSuccess(base::Value* json, |
@@ -129,5 +135,57 @@ bool ParseRegisterCompleteResponse(const std::string& response, |
return true; |
} |
+bool ParseFetchResponse(const std::string& response, |
+ const ErrorCallback error_callback, |
+ std::vector<Printjob>* list_result) { |
+ scoped_ptr<base::Value> json(base::JSONReader::Read(response)); |
+ base::DictionaryValue* response_dictionary = NULL; |
+ |
+ if (!json || !json->GetAsDictionary(&response_dictionary)) { |
+ error_callback.Run("No JSON dictionary response received."); |
+ return false; |
+ } |
+ |
+ bool success = false; |
+ if (!response_dictionary->GetBoolean("success", &success)) { |
+ error_callback.Run("Cannot parse success state."); |
+ return false; |
+ } |
+ |
+ if (!success) { // Let's suppose we have no jobs to proceed. |
+ list_result->clear(); |
+ return true; |
+ } |
+ |
+ if (!GetJsonDictinaryAndCheckSuccess(json.get(), error_callback, |
+ &response_dictionary)) { |
+ return false; |
+ } |
+ |
+ base::ListValue* jobs = NULL; |
+ if (!response_dictionary->GetList("jobs", &jobs)) { |
+ error_callback.Run("Cannot parse jobs list."); |
+ return false; |
+ } |
+ |
+ std::vector<Printjob> list(jobs->GetSize()); |
+ for (size_t idx = 0; idx < list.size(); ++idx) { |
+ base::DictionaryValue* job = NULL; |
+ jobs->GetDictionary(idx, &job); |
+ if (!job->GetString("id", &list[idx].jobid) || |
+ !job->GetString("createTime", &list[idx].create_time) || |
+ !job->GetString("fileUrl", &list[idx].file_url) || |
+ !job->GetString("ticketUrl", &list[idx].ticket_url) || |
+ !job->GetString("title", &list[idx].title)) { |
+ error_callback.Run("Cannot parse job info."); |
+ return false; |
+ } |
+ } |
+ |
+ *list_result = list; |
+ |
+ return true; |
+} |
+ |
} // namespace cloud_print_response_parser |