diff options
author | maksymb@chromium.org <maksymb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-08-06 22:54:50 +0000 |
---|---|---|
committer | maksymb@chromium.org <maksymb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-08-06 22:54:50 +0000 |
commit | e3cb4b2b4005ba362cc8db6e6cc488fafa9177dd (patch) | |
tree | bb8e54350f4769a56d2099794d22d565f013cd54 /cloud_print | |
parent | 48e5cfcc957f3adfea6d630c3b6956275f9eb76f (diff) | |
download | chromium_src-e3cb4b2b4005ba362cc8db6e6cc488fafa9177dd.zip chromium_src-e3cb4b2b4005ba362cc8db6e6cc488fafa9177dd.tar.gz chromium_src-e3cb4b2b4005ba362cc8db6e6cc488fafa9177dd.tar.bz2 |
GCP2.0 Device: Response parser refactor.
BUG=
Review URL: https://chromiumcodereview.appspot.com/22337005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@216011 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'cloud_print')
-rw-r--r-- | cloud_print/gcp20/prototype/cloud_print_response_parser.cc | 142 | ||||
-rw-r--r-- | cloud_print/gcp20/prototype/cloud_print_response_parser.h | 14 |
2 files changed, 81 insertions, 75 deletions
diff --git a/cloud_print/gcp20/prototype/cloud_print_response_parser.cc b/cloud_print/gcp20/prototype/cloud_print_response_parser.cc index 367a994..bc2742b 100644 --- a/cloud_print/gcp20/prototype/cloud_print_response_parser.cc +++ b/cloud_print/gcp20/prototype/cloud_print_response_parser.cc @@ -6,6 +6,7 @@ #include "base/json/json_reader.h" #include "base/json/json_writer.h" +#include "base/logging.h" #include "base/values.h" namespace cloud_print_response_parser { @@ -16,64 +17,77 @@ Job::Job() { Job::~Job() { } -// Parses json base::Value to base::DictionaryValue and checks |success| status. -// Returns |true| on success. +// Parses json base::Value to base::DictionaryValue. +// If |success| value in JSON dictionary is |false| then |message| will +// contain |message| from the JSON dictionary. +// Returns |true| if no parsing error occurred. bool GetJsonDictinaryAndCheckSuccess(base::Value* json, std::string* error_description, + bool* json_success, + std::string* message, base::DictionaryValue** dictionary) { 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)) { + bool response_json_success = false; + if (!response_dictionary->GetBoolean("success", &response_json_success)) { *error_description = "Cannot parse success state."; return false; } - if (!success) { - std::string message; - response_dictionary->GetString("message", &message); - *error_description = message; - return false; + if (!response_json_success) { + std::string response_message; + if (!response_dictionary->GetString("message", &response_message)) { + *error_description = "Cannot parse message from response."; + return false; + } + *message = response_message; } + *json_success = response_json_success; *dictionary = response_dictionary; return true; } bool ParseRegisterStartResponse(const std::string& response, 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) { + std::string* polling_url, + std::string* registration_token, + std::string* complete_invite_url, + std::string* device_id) { scoped_ptr<base::Value> json(base::JSONReader::Read(response)); base::DictionaryValue* response_dictionary = NULL; + bool json_success; + std::string message; if (!GetJsonDictinaryAndCheckSuccess(json.get(), error_description, + &json_success, &message, &response_dictionary)) { return false; } + if (!json_success) { + *error_description = message; + return false; + } - std::string registration_token; + std::string response_registration_token; if (!response_dictionary->GetString("registration_token", - ®istration_token)) { + &response_registration_token)) { *error_description = "No registration_token specified."; return false; } - std::string complete_invite_url; + std::string response_complete_invite_url; if (!response_dictionary->GetString("complete_invite_url", - &complete_invite_url)) { + &response_complete_invite_url)) { *error_description = "No complete_invite_url specified."; return false; } - std::string polling_url; - if (!response_dictionary->GetString("polling_url", &polling_url)) { + std::string response_polling_url; + if (!response_dictionary->GetString("polling_url", &response_polling_url)) { *error_description = "No polling_url specified."; return false; } @@ -84,113 +98,105 @@ bool ParseRegisterStartResponse(const std::string& response, return false; } - base::Value* printer_value = NULL; - if (!list->Get(0, &printer_value)) { - *error_description = "Printers list is empty."; - return false; - } - base::DictionaryValue* printer = NULL; - if (!printer_value->GetAsDictionary(&printer)) { - *error_description = "Printer is not a json-dictionary."; + if (!list->GetDictionary(0, &printer)) { + *error_description = "Printers list is empty or printer is not dictionary."; return false; } - std::string device_id; - if (!printer->GetString("id", &device_id)) { + std::string id; + if (!printer->GetString("id", &id)) { *error_description = "No id specified."; return false; } - if (device_id.empty()) { + if (id.empty()) { *error_description = "id is empty."; return false; } - *polling_url_result = polling_url; - *registration_token_result = registration_token; - *complete_invite_url_result = complete_invite_url; - *device_id_result = device_id; + *polling_url = response_polling_url; + *registration_token = response_registration_token; + *complete_invite_url = response_complete_invite_url; + *device_id = id; return true; } bool ParseRegisterCompleteResponse(const std::string& response, std::string* error_description, - std::string* authorization_code_result, - std::string* xmpp_jid_result) { + std::string* authorization_code, + std::string* xmpp_jid) { scoped_ptr<base::Value> json(base::JSONReader::Read(response)); base::DictionaryValue* response_dictionary = NULL; + bool json_success; + std::string message; if (!GetJsonDictinaryAndCheckSuccess(json.get(), error_description, + &json_success, &message, &response_dictionary)) { return false; } + if (!json_success) { + *error_description = message; + return false; + } - std::string authorization_code; + std::string response_authorization_code; if (!response_dictionary->GetString("authorization_code", - &authorization_code)) { + &response_authorization_code)) { *error_description = "Cannot parse authorization_code."; return false; } - std::string xmpp_jid; - if (!response_dictionary->GetString("xmpp_jid", &xmpp_jid)) { + std::string response_xmpp_jid; + if (!response_dictionary->GetString("xmpp_jid", &response_xmpp_jid)) { *error_description = "Cannot parse xmpp jid."; return false; } - *authorization_code_result = authorization_code; - *xmpp_jid_result = xmpp_jid; + *authorization_code = response_authorization_code; + *xmpp_jid = response_xmpp_jid; return true; } bool ParseFetchResponse(const std::string& response, std::string* error_description, - std::vector<Job>* list_result) { + std::vector<Job>* list) { 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."; + bool json_success; + std::string message; + if (!GetJsonDictinaryAndCheckSuccess(json.get(), error_description, + &json_success, &message, + &response_dictionary)) { return false; } - if (!success) { // Let's suppose we have no jobs to proceed. - list_result->clear(); + if (!json_success) { // Let's suppose we have no jobs to proceed. + list->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) { + std::vector<Job> job_list(jobs->GetSize()); + for (size_t idx = 0; idx < job_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)) { + if (!job->GetString("id", &job_list[idx].job_id) || + !job->GetString("createTime", &job_list[idx].create_time) || + !job->GetString("fileUrl", &job_list[idx].file_url) || + !job->GetString("ticketUrl", &job_list[idx].ticket_url) || + !job->GetString("title", &job_list[idx].title)) { *error_description = "Cannot parse job info."; return false; } } - *list_result = list; + *list = job_list; return true; } diff --git a/cloud_print/gcp20/prototype/cloud_print_response_parser.h b/cloud_print/gcp20/prototype/cloud_print_response_parser.h index 5c7254f..97a4c26 100644 --- a/cloud_print/gcp20/prototype/cloud_print_response_parser.h +++ b/cloud_print/gcp20/prototype/cloud_print_response_parser.h @@ -38,25 +38,25 @@ struct Job { // when parsing is failed. bool ParseRegisterStartResponse(const std::string& response, 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); + std::string* polling_url, + std::string* registration_token, + std::string* complete_invite_url, + std::string* device_id); // Parses CloudPrint register complete response to out parameters. // Returns |true| on success. Callback is called with description as a parameter // when parsing is failed. bool ParseRegisterCompleteResponse(const std::string& response, std::string* error_description, - std::string* authorization_code_result, - std::string* xmpp_jid_result); + std::string* authorization_code, + std::string* xmpp_jid); // Parses CloudPrint fetch response to out parameters. // Returns |true| on success. Callback is called with description as a parameter // when parsing is failed. bool ParseFetchResponse(const std::string& response, std::string* error_description, - std::vector<Job>* list_result); + std::vector<Job>* list); } // namespace cloud_print_response_parser |