Fix experimental app list search box disappearing on profile switch.
[chromium-blink-merge.git] / components / omnibox / suggestion_answer.cc
blobaec952e0222ae18710d66e50bbe58c8bdb15b33e
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 "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_ = GURL(
110 StartsWith(url_string, base::ASCIIToUTF16("//"), false) ?
111 (base::ASCIIToUTF16(url::kHttpsScheme) + base::ASCIIToUTF16(":") +
112 url_string) :
113 url_string);
115 if (!image_line->image_url_.is_valid())
116 return false;
119 return true;
122 bool SuggestionAnswer::ImageLine::Equals(const ImageLine& line) const {
123 if (text_fields_.size() != line.text_fields_.size())
124 return false;
125 for (size_t i = 0; i < text_fields_.size(); ++i) {
126 if (!text_fields_[i].Equals(line.text_fields_[i]))
127 return false;
130 if (additional_text_ || line.additional_text_) {
131 if (!additional_text_ || !line.additional_text_)
132 return false;
133 if (!additional_text_->Equals(*line.additional_text_))
134 return false;
137 if (status_text_ || line.status_text_) {
138 if (!status_text_ || !line.status_text_)
139 return false;
140 if (!status_text_->Equals(*line.status_text_))
141 return false;
144 return image_url_ == line.image_url_;
147 // SuggestionAnswer ------------------------------------------------------------
149 SuggestionAnswer::SuggestionAnswer() : type_(-1) {}
150 SuggestionAnswer::SuggestionAnswer(const SuggestionAnswer& answer)
151 : first_line_(answer.first_line_),
152 second_line_(answer.second_line_),
153 type_(answer.type_) {}
155 SuggestionAnswer::~SuggestionAnswer() {}
157 // static
158 scoped_ptr<SuggestionAnswer> SuggestionAnswer::ParseAnswer(
159 const base::DictionaryValue* answer_json) {
160 auto result = make_scoped_ptr(new SuggestionAnswer);
162 const base::ListValue* lines_json;
163 if (!answer_json->GetList(kAnswerJsonLines, &lines_json) ||
164 lines_json->GetSize() != 2)
165 return nullptr;
167 const base::DictionaryValue* first_line_json;
168 if (!lines_json->GetDictionary(0, &first_line_json) ||
169 !ImageLine::ParseImageLine(first_line_json, &result->first_line_))
170 return nullptr;
172 const base::DictionaryValue* second_line_json;
173 if (!lines_json->GetDictionary(1, &second_line_json) ||
174 !ImageLine::ParseImageLine(second_line_json, &result->second_line_))
175 return nullptr;
177 return result.Pass();
180 bool SuggestionAnswer::Equals(const SuggestionAnswer& answer) const {
181 return type_ == answer.type_ &&
182 first_line_.Equals(answer.first_line_) &&
183 second_line_.Equals(answer.second_line_);
186 void SuggestionAnswer::AddImageURLsTo(std::vector<GURL>* urls) const {
187 if (first_line_.image_url().is_valid())
188 urls->push_back(first_line_.image_url());
189 if (second_line_.image_url().is_valid())
190 urls->push_back(second_line_.image_url());