Revert of Telemetry, Mac: Use the actual keychain for performance tests. (patchset...
[chromium-blink-merge.git] / components / omnibox / suggestion_answer.cc
blob9642fa7f27d0035a9c1e018f6ee9034d0b98a178
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"
12 namespace {
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";
25 } // namespace
27 // SuggestionAnswer::TextField -------------------------------------------------
29 SuggestionAnswer::TextField::TextField() : type_(-1) {}
30 SuggestionAnswer::TextField::~TextField() {}
32 // static
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() {}
57 // static
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))
62 return false;
64 const base::ListValue* fields_json;
65 if (!inner_json->GetList(kAnswerJsonText, &fields_json) ||
66 fields_json->GetSize() == 0)
67 return false;
69 for (size_t i = 0; i < fields_json->GetSize(); ++i) {
70 const base::DictionaryValue* field_json;
71 TextField text_field;
72 if (!fields_json->GetDictionary(i, &field_json) ||
73 !TextField::ParseTextField(field_json, &text_field))
74 return false;
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()))
84 return false;
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()))
92 return false;
95 if (inner_json->HasKey(kAnswerJsonImage)) {
96 base::string16 url_string;
97 if (!inner_json->GetString(kAnswerJsonImageData, &url_string) ||
98 url_string.empty())
99 return false;
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(":") +
108 url_string) :
109 url_string);
111 if (!image_line->image_url_.is_valid())
112 return false;
115 return true;
118 bool SuggestionAnswer::ImageLine::Equals(const ImageLine& line) const {
119 if (text_fields_.size() != line.text_fields_.size())
120 return false;
121 for (size_t i = 0; i < text_fields_.size(); ++i) {
122 if (!text_fields_[i].Equals(line.text_fields_[i]))
123 return false;
126 if (additional_text_ || line.additional_text_) {
127 if (!additional_text_ || !line.additional_text_)
128 return false;
129 if (!additional_text_->Equals(*line.additional_text_))
130 return false;
133 if (status_text_ || line.status_text_) {
134 if (!status_text_ || !line.status_text_)
135 return false;
136 if (!status_text_->Equals(*line.status_text_))
137 return false;
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() {}
153 // static
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)
161 return nullptr;
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_))
166 return nullptr;
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_))
171 return nullptr;
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());