| 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..d4da3c33ed79598fc3337248d95cf54a47ac1e14 100644
|
| --- a/cloud_print/gcp20/prototype/cloud_print_response_parser.cc
|
| +++ b/cloud_print/gcp20/prototype/cloud_print_response_parser.cc
|
| @@ -10,28 +10,34 @@
|
|
|
| namespace cloud_print_response_parser {
|
|
|
| +Job::Job() {
|
| +}
|
| +
|
| +Job::~Job() {
|
| +}
|
| +
|
| // Parses json base::Value to base::DictionaryValue and checks |success| status.
|
| // Returns |true| on success.
|
| bool GetJsonDictinaryAndCheckSuccess(base::Value* json,
|
| - const ErrorCallback error_callback,
|
| + std::string* error_description,
|
| base::DictionaryValue** dictionary) {
|
| base::DictionaryValue* response_dictionary = NULL;
|
|
|
| if (!json || !json->GetAsDictionary(&response_dictionary)) {
|
| - error_callback.Run("No JSON dictionary response received.");
|
| + *error_description = "No JSON dictionary response received.";
|
| return false;
|
| }
|
|
|
| bool success = false;
|
| if (!response_dictionary->GetBoolean("success", &success)) {
|
| - error_callback.Run("Cannot parse success state.");
|
| + *error_description = "Cannot parse success state.";
|
| return false;
|
| }
|
|
|
| if (!success) {
|
| std::string message;
|
| response_dictionary->GetString("message", &message);
|
| - error_callback.Run(message);
|
| + *error_description = message;
|
| return false;
|
| }
|
|
|
| @@ -40,14 +46,14 @@ bool GetJsonDictinaryAndCheckSuccess(base::Value* json,
|
| }
|
|
|
| bool ParseRegisterStartResponse(const std::string& response,
|
| - const ErrorCallback error_callback,
|
| + std::string* error_description,
|
| std::string* polling_url_result,
|
| std::string* registration_token_result,
|
| std::string* complete_invite_url_result,
|
| std::string* device_id_result) {
|
| scoped_ptr<base::Value> json(base::JSONReader::Read(response));
|
| base::DictionaryValue* response_dictionary = NULL;
|
| - if (!GetJsonDictinaryAndCheckSuccess(json.get(), error_callback,
|
| + if (!GetJsonDictinaryAndCheckSuccess(json.get(), error_description,
|
| &response_dictionary)) {
|
| return false;
|
| }
|
| @@ -55,49 +61,49 @@ bool ParseRegisterStartResponse(const std::string& response,
|
| std::string registration_token;
|
| if (!response_dictionary->GetString("registration_token",
|
| ®istration_token)) {
|
| - error_callback.Run("No registration_token specified.");
|
| + *error_description = "No registration_token specified.";
|
| return false;
|
| }
|
|
|
| std::string complete_invite_url;
|
| if (!response_dictionary->GetString("complete_invite_url",
|
| &complete_invite_url)) {
|
| - error_callback.Run("No complete_invite_url specified.");
|
| + *error_description = "No complete_invite_url specified.";
|
| return false;
|
| }
|
|
|
| std::string polling_url;
|
| if (!response_dictionary->GetString("polling_url", &polling_url)) {
|
| - error_callback.Run("No polling_url specified.");
|
| + *error_description = "No polling_url specified.";
|
| return false;
|
| }
|
|
|
| base::ListValue* list = NULL;
|
| if (!response_dictionary->GetList("printers", &list)) {
|
| - error_callback.Run("No printers list specified.");
|
| + *error_description = "No printers list specified.";
|
| return false;
|
| }
|
|
|
| base::Value* printer_value = NULL;
|
| if (!list->Get(0, &printer_value)) {
|
| - error_callback.Run("Printers list is empty.");
|
| + *error_description = "Printers list is empty.";
|
| return false;
|
| }
|
|
|
| base::DictionaryValue* printer = NULL;
|
| if (!printer_value->GetAsDictionary(&printer)) {
|
| - error_callback.Run("Printer is not a json-dictionary.");
|
| + *error_description = "Printer is not a json-dictionary.";
|
| return false;
|
| }
|
|
|
| std::string device_id;
|
| if (!printer->GetString("id", &device_id)) {
|
| - error_callback.Run("No id specified.");
|
| + *error_description = "No id specified.";
|
| return false;
|
| }
|
|
|
| if (device_id.empty()) {
|
| - error_callback.Run("id is empty.");
|
| + *error_description = "id is empty.";
|
| return false;
|
| }
|
|
|
| @@ -109,11 +115,11 @@ bool ParseRegisterStartResponse(const std::string& response,
|
| }
|
|
|
| bool ParseRegisterCompleteResponse(const std::string& response,
|
| - const ErrorCallback error_callback,
|
| + std::string* error_description,
|
| std::string* authorization_code_result) {
|
| scoped_ptr<base::Value> json(base::JSONReader::Read(response));
|
| base::DictionaryValue* response_dictionary = NULL;
|
| - if (!GetJsonDictinaryAndCheckSuccess(json.get(), error_callback,
|
| + if (!GetJsonDictinaryAndCheckSuccess(json.get(), error_description,
|
| &response_dictionary)) {
|
| return false;
|
| }
|
| @@ -121,7 +127,7 @@ bool ParseRegisterCompleteResponse(const std::string& response,
|
| std::string authorization_code;
|
| if (!response_dictionary->GetString("authorization_code",
|
| &authorization_code)) {
|
| - error_callback.Run("Cannot parse authorization_code.");
|
| + *error_description = "Cannot parse authorization_code.";
|
| return false;
|
| }
|
|
|
| @@ -129,5 +135,57 @@ bool ParseRegisterCompleteResponse(const std::string& response,
|
| return true;
|
| }
|
|
|
| +bool ParseFetchResponse(const std::string& response,
|
| + std::string* error_description,
|
| + std::vector<Job>* list_result) {
|
| + scoped_ptr<base::Value> json(base::JSONReader::Read(response));
|
| + base::DictionaryValue* response_dictionary = NULL;
|
| +
|
| + if (!json || !json->GetAsDictionary(&response_dictionary)) {
|
| + *error_description = "No JSON dictionary response received.";
|
| + return false;
|
| + }
|
| +
|
| + bool success = false;
|
| + if (!response_dictionary->GetBoolean("success", &success)) {
|
| + *error_description = "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_description,
|
| + &response_dictionary)) {
|
| + return false;
|
| + }
|
| +
|
| + base::ListValue* jobs = NULL;
|
| + if (!response_dictionary->GetList("jobs", &jobs)) {
|
| + *error_description = "Cannot parse jobs list.";
|
| + return false;
|
| + }
|
| +
|
| + std::vector<Job> 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].job_id) ||
|
| + !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_description = "Cannot parse job info.";
|
| + return false;
|
| + }
|
| + }
|
| +
|
| + *list_result = list;
|
| +
|
| + return true;
|
| +}
|
| +
|
| } // namespace cloud_print_response_parser
|
|
|
|
|