Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / components / omnibox / browser / suggestion_answer.cc
blob8e8b62f5fb18247e818763108a398f51eb19b888
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/browser/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 "net/base/escape.h"
11 #include "url/url_constants.h"
13 namespace {
15 // All of these are defined here (even though most are only used once each) so
16 // the format details are easy to locate and update or compare to the spec doc.
17 static const char kAnswerJsonLines[] = "l";
18 static const char kAnswerJsonImageLine[] = "il";
19 static const char kAnswerJsonText[] = "t";
20 static const char kAnswerJsonAdditionalText[] = "at";
21 static const char kAnswerJsonStatusText[] = "st";
22 static const char kAnswerJsonTextType[] = "tt";
23 static const char kAnswerJsonImage[] = "i";
24 static const char kAnswerJsonImageData[] = "i.d";
26 } // namespace
28 // SuggestionAnswer::TextField -------------------------------------------------
30 SuggestionAnswer::TextField::TextField() : type_(-1) {}
31 SuggestionAnswer::TextField::~TextField() {}
33 // static
34 bool SuggestionAnswer::TextField::ParseTextField(
35 const base::DictionaryValue* field_json, TextField* text_field) {
36 bool parsed = field_json->GetString(kAnswerJsonText, &text_field->text_) &&
37 !text_field->text_.empty() &&
38 field_json->GetInteger(kAnswerJsonTextType, &text_field->type_);
39 if (parsed)
40 text_field->text_ = net::UnescapeForHTML(text_field->text_);
41 return parsed;
44 bool SuggestionAnswer::TextField::Equals(const TextField& field) const {
45 return type_ == field.type_ && text_ == field.text_;
48 // SuggestionAnswer::ImageLine -------------------------------------------------
50 SuggestionAnswer::ImageLine::ImageLine() {}
51 SuggestionAnswer::ImageLine::ImageLine(const ImageLine& line)
52 : text_fields_(line.text_fields_),
53 additional_text_(line.additional_text_ ?
54 new TextField(*line.additional_text_) : nullptr),
55 status_text_(line.status_text_ ?
56 new TextField(*line.status_text_) : nullptr),
57 image_url_(line.image_url_) {}
59 SuggestionAnswer::ImageLine::~ImageLine() {}
61 // static
62 bool SuggestionAnswer::ImageLine::ParseImageLine(
63 const base::DictionaryValue* line_json, ImageLine* image_line) {
64 const base::DictionaryValue* inner_json;
65 if (!line_json->GetDictionary(kAnswerJsonImageLine, &inner_json))
66 return false;
68 const base::ListValue* fields_json;
69 if (!inner_json->GetList(kAnswerJsonText, &fields_json) ||
70 fields_json->GetSize() == 0)
71 return false;
73 for (size_t i = 0; i < fields_json->GetSize(); ++i) {
74 const base::DictionaryValue* field_json;
75 TextField text_field;
76 if (!fields_json->GetDictionary(i, &field_json) ||
77 !TextField::ParseTextField(field_json, &text_field))
78 return false;
79 image_line->text_fields_.push_back(text_field);
82 if (inner_json->HasKey(kAnswerJsonAdditionalText)) {
83 image_line->additional_text_.reset(new TextField());
84 const base::DictionaryValue* field_json;
85 if (!inner_json->GetDictionary(kAnswerJsonAdditionalText, &field_json) ||
86 !TextField::ParseTextField(field_json,
87 image_line->additional_text_.get()))
88 return false;
91 if (inner_json->HasKey(kAnswerJsonStatusText)) {
92 image_line->status_text_.reset(new TextField());
93 const base::DictionaryValue* field_json;
94 if (!inner_json->GetDictionary(kAnswerJsonStatusText, &field_json) ||
95 !TextField::ParseTextField(field_json, image_line->status_text_.get()))
96 return false;
99 if (inner_json->HasKey(kAnswerJsonImage)) {
100 base::string16 url_string;
101 if (!inner_json->GetString(kAnswerJsonImageData, &url_string) ||
102 url_string.empty())
103 return false;
104 // If necessary, concatenate scheme and host/path using only ':' as
105 // separator. This is due to the results delivering strings of the form
106 // "//host/path", which is web-speak for "use the enclosing page's scheme",
107 // but not a valid path of an URL. The GWS frontend commonly (always?)
108 // redirects to HTTPS so we just default to that here.
109 image_line->image_url_ =
110 GURL(base::StartsWith(url_string, base::ASCIIToUTF16("//"),
111 base::CompareCase::SENSITIVE)
112 ? (base::ASCIIToUTF16(url::kHttpsScheme) +
113 base::ASCIIToUTF16(":") + url_string)
114 : url_string);
116 if (!image_line->image_url_.is_valid())
117 return false;
120 return true;
123 bool SuggestionAnswer::ImageLine::Equals(const ImageLine& line) const {
124 if (text_fields_.size() != line.text_fields_.size())
125 return false;
126 for (size_t i = 0; i < text_fields_.size(); ++i) {
127 if (!text_fields_[i].Equals(line.text_fields_[i]))
128 return false;
131 if (additional_text_ || line.additional_text_) {
132 if (!additional_text_ || !line.additional_text_)
133 return false;
134 if (!additional_text_->Equals(*line.additional_text_))
135 return false;
138 if (status_text_ || line.status_text_) {
139 if (!status_text_ || !line.status_text_)
140 return false;
141 if (!status_text_->Equals(*line.status_text_))
142 return false;
145 return image_url_ == line.image_url_;
148 // SuggestionAnswer ------------------------------------------------------------
150 SuggestionAnswer::SuggestionAnswer() : type_(-1) {}
151 SuggestionAnswer::SuggestionAnswer(const SuggestionAnswer& answer)
152 : first_line_(answer.first_line_),
153 second_line_(answer.second_line_),
154 type_(answer.type_) {}
156 SuggestionAnswer::~SuggestionAnswer() {}
158 // static
159 scoped_ptr<SuggestionAnswer> SuggestionAnswer::ParseAnswer(
160 const base::DictionaryValue* answer_json) {
161 auto result = make_scoped_ptr(new SuggestionAnswer);
163 const base::ListValue* lines_json;
164 if (!answer_json->GetList(kAnswerJsonLines, &lines_json) ||
165 lines_json->GetSize() != 2)
166 return nullptr;
168 const base::DictionaryValue* first_line_json;
169 if (!lines_json->GetDictionary(0, &first_line_json) ||
170 !ImageLine::ParseImageLine(first_line_json, &result->first_line_))
171 return nullptr;
173 const base::DictionaryValue* second_line_json;
174 if (!lines_json->GetDictionary(1, &second_line_json) ||
175 !ImageLine::ParseImageLine(second_line_json, &result->second_line_))
176 return nullptr;
178 return result.Pass();
181 bool SuggestionAnswer::Equals(const SuggestionAnswer& answer) const {
182 return type_ == answer.type_ &&
183 first_line_.Equals(answer.first_line_) &&
184 second_line_.Equals(answer.second_line_);
187 void SuggestionAnswer::AddImageURLsTo(std::vector<GURL>* urls) const {
188 if (first_line_.image_url().is_valid())
189 urls->push_back(first_line_.image_url());
190 if (second_line_.image_url().is_valid())
191 urls->push_back(second_line_.image_url());