Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: cloud_print/gcp20/prototype/cloud_print_response_parser.cc

Issue 19866002: GCP2.0 Device: Receiving printjobs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@confirmation
Patch Set: static_cast<int> Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "cloud_print/gcp20/prototype/cloud_print_response_parser.h" 5 #include "cloud_print/gcp20/prototype/cloud_print_response_parser.h"
6 6
7 #include "base/json/json_reader.h" 7 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h" 8 #include "base/json/json_writer.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 10
11 namespace cloud_print_response_parser { 11 namespace cloud_print_response_parser {
12 12
13 Job::Job() {
14 }
15
16 Job::~Job() {
17 }
18
13 // Parses json base::Value to base::DictionaryValue and checks |success| status. 19 // Parses json base::Value to base::DictionaryValue and checks |success| status.
14 // Returns |true| on success. 20 // Returns |true| on success.
15 bool GetJsonDictinaryAndCheckSuccess(base::Value* json, 21 bool GetJsonDictinaryAndCheckSuccess(base::Value* json,
16 const ErrorCallback error_callback, 22 std::string* error_description,
17 base::DictionaryValue** dictionary) { 23 base::DictionaryValue** dictionary) {
18 base::DictionaryValue* response_dictionary = NULL; 24 base::DictionaryValue* response_dictionary = NULL;
19 25
20 if (!json || !json->GetAsDictionary(&response_dictionary)) { 26 if (!json || !json->GetAsDictionary(&response_dictionary)) {
21 error_callback.Run("No JSON dictionary response received."); 27 *error_description = "No JSON dictionary response received.";
22 return false; 28 return false;
23 } 29 }
24 30
25 bool success = false; 31 bool success = false;
26 if (!response_dictionary->GetBoolean("success", &success)) { 32 if (!response_dictionary->GetBoolean("success", &success)) {
27 error_callback.Run("Cannot parse success state."); 33 *error_description = "Cannot parse success state.";
28 return false; 34 return false;
29 } 35 }
30 36
31 if (!success) { 37 if (!success) {
32 std::string message; 38 std::string message;
33 response_dictionary->GetString("message", &message); 39 response_dictionary->GetString("message", &message);
34 error_callback.Run(message); 40 *error_description = message;
35 return false; 41 return false;
36 } 42 }
37 43
38 *dictionary = response_dictionary; 44 *dictionary = response_dictionary;
39 return true; 45 return true;
40 } 46 }
41 47
42 bool ParseRegisterStartResponse(const std::string& response, 48 bool ParseRegisterStartResponse(const std::string& response,
43 const ErrorCallback error_callback, 49 std::string* error_description,
44 std::string* polling_url_result, 50 std::string* polling_url_result,
45 std::string* registration_token_result, 51 std::string* registration_token_result,
46 std::string* complete_invite_url_result, 52 std::string* complete_invite_url_result,
47 std::string* device_id_result) { 53 std::string* device_id_result) {
48 scoped_ptr<base::Value> json(base::JSONReader::Read(response)); 54 scoped_ptr<base::Value> json(base::JSONReader::Read(response));
49 base::DictionaryValue* response_dictionary = NULL; 55 base::DictionaryValue* response_dictionary = NULL;
50 if (!GetJsonDictinaryAndCheckSuccess(json.get(), error_callback, 56 if (!GetJsonDictinaryAndCheckSuccess(json.get(), error_description,
51 &response_dictionary)) { 57 &response_dictionary)) {
52 return false; 58 return false;
53 } 59 }
54 60
55 std::string registration_token; 61 std::string registration_token;
56 if (!response_dictionary->GetString("registration_token", 62 if (!response_dictionary->GetString("registration_token",
57 &registration_token)) { 63 &registration_token)) {
58 error_callback.Run("No registration_token specified."); 64 *error_description = "No registration_token specified.";
59 return false; 65 return false;
60 } 66 }
61 67
62 std::string complete_invite_url; 68 std::string complete_invite_url;
63 if (!response_dictionary->GetString("complete_invite_url", 69 if (!response_dictionary->GetString("complete_invite_url",
64 &complete_invite_url)) { 70 &complete_invite_url)) {
65 error_callback.Run("No complete_invite_url specified."); 71 *error_description = "No complete_invite_url specified.";
66 return false; 72 return false;
67 } 73 }
68 74
69 std::string polling_url; 75 std::string polling_url;
70 if (!response_dictionary->GetString("polling_url", &polling_url)) { 76 if (!response_dictionary->GetString("polling_url", &polling_url)) {
71 error_callback.Run("No polling_url specified."); 77 *error_description = "No polling_url specified.";
72 return false; 78 return false;
73 } 79 }
74 80
75 base::ListValue* list = NULL; 81 base::ListValue* list = NULL;
76 if (!response_dictionary->GetList("printers", &list)) { 82 if (!response_dictionary->GetList("printers", &list)) {
77 error_callback.Run("No printers list specified."); 83 *error_description = "No printers list specified.";
78 return false; 84 return false;
79 } 85 }
80 86
81 base::Value* printer_value = NULL; 87 base::Value* printer_value = NULL;
82 if (!list->Get(0, &printer_value)) { 88 if (!list->Get(0, &printer_value)) {
83 error_callback.Run("Printers list is empty."); 89 *error_description = "Printers list is empty.";
84 return false; 90 return false;
85 } 91 }
86 92
87 base::DictionaryValue* printer = NULL; 93 base::DictionaryValue* printer = NULL;
88 if (!printer_value->GetAsDictionary(&printer)) { 94 if (!printer_value->GetAsDictionary(&printer)) {
89 error_callback.Run("Printer is not a json-dictionary."); 95 *error_description = "Printer is not a json-dictionary.";
90 return false; 96 return false;
91 } 97 }
92 98
93 std::string device_id; 99 std::string device_id;
94 if (!printer->GetString("id", &device_id)) { 100 if (!printer->GetString("id", &device_id)) {
95 error_callback.Run("No id specified."); 101 *error_description = "No id specified.";
96 return false; 102 return false;
97 } 103 }
98 104
99 if (device_id.empty()) { 105 if (device_id.empty()) {
100 error_callback.Run("id is empty."); 106 *error_description = "id is empty.";
101 return false; 107 return false;
102 } 108 }
103 109
104 *polling_url_result = polling_url; 110 *polling_url_result = polling_url;
105 *registration_token_result = registration_token; 111 *registration_token_result = registration_token;
106 *complete_invite_url_result = complete_invite_url; 112 *complete_invite_url_result = complete_invite_url;
107 *device_id_result = device_id; 113 *device_id_result = device_id;
108 return true; 114 return true;
109 } 115 }
110 116
111 bool ParseRegisterCompleteResponse(const std::string& response, 117 bool ParseRegisterCompleteResponse(const std::string& response,
112 const ErrorCallback error_callback, 118 std::string* error_description,
113 std::string* authorization_code_result) { 119 std::string* authorization_code_result) {
114 scoped_ptr<base::Value> json(base::JSONReader::Read(response)); 120 scoped_ptr<base::Value> json(base::JSONReader::Read(response));
115 base::DictionaryValue* response_dictionary = NULL; 121 base::DictionaryValue* response_dictionary = NULL;
116 if (!GetJsonDictinaryAndCheckSuccess(json.get(), error_callback, 122 if (!GetJsonDictinaryAndCheckSuccess(json.get(), error_description,
117 &response_dictionary)) { 123 &response_dictionary)) {
118 return false; 124 return false;
119 } 125 }
120 126
121 std::string authorization_code; 127 std::string authorization_code;
122 if (!response_dictionary->GetString("authorization_code", 128 if (!response_dictionary->GetString("authorization_code",
123 &authorization_code)) { 129 &authorization_code)) {
124 error_callback.Run("Cannot parse authorization_code."); 130 *error_description = "Cannot parse authorization_code.";
125 return false; 131 return false;
126 } 132 }
127 133
128 *authorization_code_result = authorization_code; 134 *authorization_code_result = authorization_code;
129 return true; 135 return true;
130 } 136 }
131 137
138 bool ParseFetchResponse(const std::string& response,
139 std::string* error_description,
140 std::vector<Job>* list_result) {
141 scoped_ptr<base::Value> json(base::JSONReader::Read(response));
142 base::DictionaryValue* response_dictionary = NULL;
143
144 if (!json || !json->GetAsDictionary(&response_dictionary)) {
145 *error_description = "No JSON dictionary response received.";
146 return false;
147 }
148
149 bool success = false;
150 if (!response_dictionary->GetBoolean("success", &success)) {
151 *error_description = "Cannot parse success state.";
152 return false;
153 }
154
155 if (!success) { // Let's suppose we have no jobs to proceed.
156 list_result->clear();
157 return true;
158 }
159
160 if (!GetJsonDictinaryAndCheckSuccess(json.get(), error_description,
161 &response_dictionary)) {
162 return false;
163 }
164
165 base::ListValue* jobs = NULL;
166 if (!response_dictionary->GetList("jobs", &jobs)) {
167 *error_description = "Cannot parse jobs list.";
168 return false;
169 }
170
171 std::vector<Job> list(jobs->GetSize());
172 for (size_t idx = 0; idx < list.size(); ++idx) {
173 base::DictionaryValue* job = NULL;
174 jobs->GetDictionary(idx, &job);
175 if (!job->GetString("id", &list[idx].job_id) ||
176 !job->GetString("createTime", &list[idx].create_time) ||
177 !job->GetString("fileUrl", &list[idx].file_url) ||
178 !job->GetString("ticketUrl", &list[idx].ticket_url) ||
179 !job->GetString("title", &list[idx].title)) {
180 *error_description = "Cannot parse job info.";
181 return false;
182 }
183 }
184
185 *list_result = list;
186
187 return true;
188 }
189
132 } // namespace cloud_print_response_parser 190 } // namespace cloud_print_response_parser
133 191
OLDNEW
« no previous file with comments | « cloud_print/gcp20/prototype/cloud_print_response_parser.h ('k') | cloud_print/gcp20/prototype/dns_sd_server.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698