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

Side by Side Diff: components/omnibox/suggestion_answer.cc

Issue 669573005: Add a class to parse answer json. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Respond to comments Created 6 years, 2 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
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/omnibox/suggestion_answer.h"
6
7 #include "base/json/json_reader.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12
13 namespace {
14
15 static const char* kAnswerJsonLines = "l";
16 static const char* kAnswerJsonImageLine = "il";
17 static const char* kAnswerJsonText = "t";
18 static const char* kAnswerJsonAdditionalText = "at";
19 static const char* kAnswerJsonStatusText = "st";
20 static const char* kAnswerJsonTextType = "tt";
21 static const char* kAnswerJsonImage = "i";
22 static const char* kAnswerJsonImageData = "i.d";
Peter Kasting 2014/10/24 22:38:10 Nit: I tend to interpret the style guide's "Place
Justin Donnelly 2014/10/27 21:41:15 Makes sense, but these are a bit of a special case
Peter Kasting 2014/10/27 21:50:09 I don't care much. If you think there's a win in
Justin Donnelly 2014/10/28 00:39:34 Done.
23
24 }
25
26 // SuggestionAnswer::TextField -------------------------------------------------
27
28 SuggestionAnswer::TextField::TextField() : type_(-1) {}
29 SuggestionAnswer::TextField::~TextField() {}
30
31 // static
32 void SuggestionAnswer::TextField::ParseTextField(
Peter Kasting 2014/10/24 22:38:10 This function returns void and the caller checks v
Justin Donnelly 2014/10/27 21:41:15 The issue here is that the is_valid() functions ar
Peter Kasting 2014/10/27 21:50:09 Would passing by pointer muck things up too much?
Justin Donnelly 2014/10/28 00:39:34 Yeah, it creates a lot of slightly more complicate
Peter Kasting 2014/10/29 17:56:17 OK, I read through patch set 1 and looked at the d
33 const base::DictionaryValue* field_json, TextField* text_field) {
34 if (!field_json->GetString(kAnswerJsonText, &text_field->text_) ||
35 text_field->text_.empty())
36 return;
37 field_json->GetInteger(kAnswerJsonTextType, &text_field->type_);
38 }
39
40 bool SuggestionAnswer::TextField::operator==(const TextField& field) const {
41 return type_ == field.type_ && text_ == field.text_;
42 }
43
44 bool SuggestionAnswer::TextField::operator!=(const TextField& field) const {
45 return !operator==(field);
46 }
47
48 void SuggestionAnswer::TextField::Clear() {
49 text_.clear();
50 type_ = -1;
51 }
52
53 // SuggestionAnswer::ImageLine -------------------------------------------------
54
55 SuggestionAnswer::ImageLine::ImageLine() : is_valid_(false) {}
56 SuggestionAnswer::ImageLine::~ImageLine() {}
57
58 // static
59 void SuggestionAnswer::ImageLine::ParseImageLine(
60 const base::DictionaryValue* line_json, ImageLine* image_line) {
61 image_line->is_valid_ = false;
62
63 const base::DictionaryValue* inner_json;
64 if (!line_json->GetDictionary(kAnswerJsonImageLine, &inner_json))
65 return;
66
67 const base::ListValue* fields_json;
68 if (!inner_json->GetList(kAnswerJsonText, &fields_json) ||
69 fields_json->GetSize() == 0)
70 return;
71
72 for (size_t i = 0; i < fields_json->GetSize(); ++i) {
73 const base::DictionaryValue* field_json;
74 TextField text_field;
75 if (!fields_json->GetDictionary(i, &field_json))
76 return;
77 TextField::ParseTextField(field_json, &text_field);
78 if (!text_field.is_valid())
79 return;
80 image_line->text_fields_.push_back(text_field);
81 }
82
83 if (inner_json->HasKey(kAnswerJsonAdditionalText)) {
84 const base::DictionaryValue* field_json;
85 if (!inner_json->GetDictionary(kAnswerJsonAdditionalText, &field_json))
86 return;
87 TextField::ParseTextField(field_json, &image_line->additional_text_);
88 if (!image_line->additional_text_.is_valid())
89 return;
90 }
91
92 if (inner_json->HasKey(kAnswerJsonStatusText)) {
93 const base::DictionaryValue* field_json;
94 if (!inner_json->GetDictionary(kAnswerJsonStatusText, &field_json))
95 return;
96 TextField::ParseTextField(field_json, &image_line->status_text_);
97 if (!image_line->status_text_.is_valid())
98 return;
99 }
100
101 if (inner_json->HasKey(kAnswerJsonImage)) {
102 base::string16 url_string;
103 if (!inner_json->GetString(kAnswerJsonImageData, &url_string) ||
104 url_string.empty())
105 return;
106 // If necessary, concatenate scheme and host/path using only ':' as
107 // separator. This is due to the results delivering strings of the form
108 // "//host/path", which is web-speak for "use the enclosing page's scheme",
109 // but not a valid path of an URL.
110 if (StartsWith(url_string, base::ASCIIToUTF16("//"), false))
111 image_line->image_url_ = GURL(base::ASCIIToUTF16("https:") + url_string);
Peter Kasting 2014/10/24 22:38:10 Nit: Maybe we should use kHttpsScheme from url/url
Justin Donnelly 2014/10/27 21:41:15 Done.
112 else
113 image_line->image_url_ = GURL(url_string);
Peter Kasting 2014/10/24 22:38:10 Nit: Shorter: image_line->image_url_ = GURL(
Justin Donnelly 2014/10/27 21:41:15 Done.
114
115 if (!image_line->image_url_.is_valid())
116 return;
117 }
118
119 image_line->is_valid_ = true;
120 }
121
122 bool SuggestionAnswer::ImageLine::operator==(const ImageLine& line) const {
123 return text_fields_ == line.text_fields_ &&
124 additional_text_ == line.additional_text_ &&
125 status_text_ == line.status_text_ &&
126 image_url_ == line.image_url_;
127 }
128
129 bool SuggestionAnswer::ImageLine::operator!=(const ImageLine& line) const {
130 return !operator==(line);
131 }
132
133 void SuggestionAnswer::ImageLine::Clear() {
134 text_fields_.clear();
135 additional_text_.Clear();
136 status_text_.Clear();
137 image_url_ = GURL();
138 is_valid_ = false;
139 }
140
141 // SuggestionAnswer ------------------------------------------------------------
142
143 SuggestionAnswer::SuggestionAnswer() : type_(-1) {}
144 SuggestionAnswer::~SuggestionAnswer() {}
145
146 // static
147 void SuggestionAnswer::ParseAnswer(
148 const std::string& answer_json, SuggestionAnswer* answer) {
149 scoped_ptr<base::Value> value(base::JSONReader::Read(answer_json));
150 base::DictionaryValue* dict;
151 if (!value.get() || !value->GetAsDictionary(&dict))
152 return;
153
154 ParseAnswer(dict, answer);
155 }
156
157 // static
158 void SuggestionAnswer::ParseAnswer(
159 const base::DictionaryValue* answer_json, SuggestionAnswer* answer) {
160 const base::ListValue* lines_json;
161 if (!answer_json->GetList(kAnswerJsonLines, &lines_json) ||
162 lines_json->GetSize() != 2)
163 return;
164
165 const base::DictionaryValue* first_line_json;
166 if (!lines_json->GetDictionary(0, &first_line_json))
167 return;
168 ImageLine::ParseImageLine(first_line_json, &answer->first_line_);
169 if (!answer->first_line_.is_valid())
170 return;
171
172 const base::DictionaryValue* second_line_json;
173 if (!lines_json->GetDictionary(1, &second_line_json))
174 return;
175 ImageLine::ParseImageLine(second_line_json, &answer->second_line_);
176 }
177
178 bool SuggestionAnswer::operator==(const SuggestionAnswer& answer) const {
179 return type_ == answer.type_ &&
180 first_line_ == answer.first_line_ &&
181 second_line_ == answer.second_line_;
182 }
183
184 bool SuggestionAnswer::operator!=(const SuggestionAnswer& answer) const {
185 return !operator==(answer);
186 }
187
188 void SuggestionAnswer::SetType(const base::string16& type) {
189 if (!base::StringToInt(type, &type_))
190 type_ = -1;
191 }
192
193 void SuggestionAnswer::Clear() {
194 first_line_.Clear();
195 second_line_.Clear();
196 type_ = -1;
197 }
198
199 void SuggestionAnswer::GetImageURLs(std::vector<GURL>* urls) const {
200 urls->clear();
201
202 if(!is_valid())
203 return;
204
205 if (first_line_.has_image_url())
206 urls->push_back(first_line_.image_url());
207 if (second_line_.has_image_url())
208 urls->push_back(second_line_.image_url());
209 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698