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.
5 #include "components/omnibox/suggestion_answer.h"
7 #include "base/strings/string_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.h"
10 #include "url/url_constants.h"
14 // All of these are defined here (even though most are only used once each) so
15 // the format details are easy to locate and update or compare to the spec doc.
16 static const char kAnswerJsonLines
[] = "l";
17 static const char kAnswerJsonImageLine
[] = "il";
18 static const char kAnswerJsonText
[] = "t";
19 static const char kAnswerJsonAdditionalText
[] = "at";
20 static const char kAnswerJsonStatusText
[] = "st";
21 static const char kAnswerJsonTextType
[] = "tt";
22 static const char kAnswerJsonImage
[] = "i";
23 static const char kAnswerJsonImageData
[] = "i.d";
27 // SuggestionAnswer::TextField -------------------------------------------------
29 SuggestionAnswer::TextField::TextField() : type_(-1) {}
30 SuggestionAnswer::TextField::~TextField() {}
33 bool SuggestionAnswer::TextField::ParseTextField(
34 const base::DictionaryValue
* field_json
, TextField
* text_field
) {
35 return field_json
->GetString(kAnswerJsonText
, &text_field
->text_
) &&
36 !text_field
->text_
.empty() &&
37 field_json
->GetInteger(kAnswerJsonTextType
, &text_field
->type_
);
40 bool SuggestionAnswer::TextField::Equals(const TextField
& field
) const {
41 return type_
== field
.type_
&& text_
== field
.text_
;
44 // SuggestionAnswer::ImageLine -------------------------------------------------
46 SuggestionAnswer::ImageLine::ImageLine() {}
47 SuggestionAnswer::ImageLine::ImageLine(const ImageLine
& line
)
48 : text_fields_(line
.text_fields_
),
49 additional_text_(line
.additional_text_
?
50 new TextField(*line
.additional_text_
) : nullptr),
51 status_text_(line
.status_text_
?
52 new TextField(*line
.status_text_
) : nullptr),
53 image_url_(line
.image_url_
) {}
55 SuggestionAnswer::ImageLine::~ImageLine() {}
58 bool SuggestionAnswer::ImageLine::ParseImageLine(
59 const base::DictionaryValue
* line_json
, ImageLine
* image_line
) {
60 const base::DictionaryValue
* inner_json
;
61 if (!line_json
->GetDictionary(kAnswerJsonImageLine
, &inner_json
))
64 const base::ListValue
* fields_json
;
65 if (!inner_json
->GetList(kAnswerJsonText
, &fields_json
) ||
66 fields_json
->GetSize() == 0)
69 for (size_t i
= 0; i
< fields_json
->GetSize(); ++i
) {
70 const base::DictionaryValue
* field_json
;
72 if (!fields_json
->GetDictionary(i
, &field_json
) ||
73 !TextField::ParseTextField(field_json
, &text_field
))
75 image_line
->text_fields_
.push_back(text_field
);
78 if (inner_json
->HasKey(kAnswerJsonAdditionalText
)) {
79 image_line
->additional_text_
.reset(new TextField());
80 const base::DictionaryValue
* field_json
;
81 if (!inner_json
->GetDictionary(kAnswerJsonAdditionalText
, &field_json
) ||
82 !TextField::ParseTextField(field_json
,
83 image_line
->additional_text_
.get()))
87 if (inner_json
->HasKey(kAnswerJsonStatusText
)) {
88 image_line
->status_text_
.reset(new TextField());
89 const base::DictionaryValue
* field_json
;
90 if (!inner_json
->GetDictionary(kAnswerJsonStatusText
, &field_json
) ||
91 !TextField::ParseTextField(field_json
, image_line
->status_text_
.get()))
95 if (inner_json
->HasKey(kAnswerJsonImage
)) {
96 base::string16 url_string
;
97 if (!inner_json
->GetString(kAnswerJsonImageData
, &url_string
) ||
100 // If necessary, concatenate scheme and host/path using only ':' as
101 // separator. This is due to the results delivering strings of the form
102 // "//host/path", which is web-speak for "use the enclosing page's scheme",
103 // but not a valid path of an URL. The GWS frontend commonly (always?)
104 // redirects to HTTPS so we just default to that here.
105 image_line
->image_url_
= GURL(
106 StartsWith(url_string
, base::ASCIIToUTF16("//"), false) ?
107 (base::ASCIIToUTF16(url::kHttpsScheme
) + base::ASCIIToUTF16(":") +
111 if (!image_line
->image_url_
.is_valid())
118 bool SuggestionAnswer::ImageLine::Equals(const ImageLine
& line
) const {
119 if (text_fields_
.size() != line
.text_fields_
.size())
121 for (size_t i
= 0; i
< text_fields_
.size(); ++i
) {
122 if (!text_fields_
[i
].Equals(line
.text_fields_
[i
]))
126 if (additional_text_
|| line
.additional_text_
) {
127 if (!additional_text_
|| !line
.additional_text_
)
129 if (!additional_text_
->Equals(*line
.additional_text_
))
133 if (status_text_
|| line
.status_text_
) {
134 if (!status_text_
|| !line
.status_text_
)
136 if (!status_text_
->Equals(*line
.status_text_
))
140 return image_url_
== line
.image_url_
;
143 // SuggestionAnswer ------------------------------------------------------------
145 SuggestionAnswer::SuggestionAnswer() : type_(-1) {}
146 SuggestionAnswer::SuggestionAnswer(const SuggestionAnswer
& answer
)
147 : first_line_(answer
.first_line_
),
148 second_line_(answer
.second_line_
),
149 type_(answer
.type_
) {}
151 SuggestionAnswer::~SuggestionAnswer() {}
154 scoped_ptr
<SuggestionAnswer
> SuggestionAnswer::ParseAnswer(
155 const base::DictionaryValue
* answer_json
) {
156 auto result
= make_scoped_ptr(new SuggestionAnswer
);
158 const base::ListValue
* lines_json
;
159 if (!answer_json
->GetList(kAnswerJsonLines
, &lines_json
) ||
160 lines_json
->GetSize() != 2)
163 const base::DictionaryValue
* first_line_json
;
164 if (!lines_json
->GetDictionary(0, &first_line_json
) ||
165 !ImageLine::ParseImageLine(first_line_json
, &result
->first_line_
))
168 const base::DictionaryValue
* second_line_json
;
169 if (!lines_json
->GetDictionary(1, &second_line_json
) ||
170 !ImageLine::ParseImageLine(second_line_json
, &result
->second_line_
))
173 return result
.Pass();
176 bool SuggestionAnswer::Equals(const SuggestionAnswer
& answer
) const {
177 return type_
== answer
.type_
&&
178 first_line_
.Equals(answer
.first_line_
) &&
179 second_line_
.Equals(answer
.second_line_
);
182 void SuggestionAnswer::AddImageURLsTo(std::vector
<GURL
>* urls
) const {
183 if (first_line_
.image_url().is_valid())
184 urls
->push_back(first_line_
.image_url());
185 if (second_line_
.image_url().is_valid())
186 urls
->push_back(second_line_
.image_url());