Chromium Code Reviews| OLD | NEW |
|---|---|
| (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_util.h" | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "base/values.h" | |
| 11 #include "url/url_constants.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // All of these are defined here (even though they're only used once each) so | |
|
Peter Kasting
2014/10/30 03:43:29
Nit: Maybe "even if they're used sparingly below"
Justin Donnelly
2014/10/30 19:23:13
Done.
| |
| 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"; | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 // SuggestionAnswer::TextField ------------------------------------------------- | |
| 29 | |
| 30 SuggestionAnswer::TextField::TextField() : type_(-1) {} | |
| 31 SuggestionAnswer::TextField::~TextField() {} | |
| 32 | |
| 33 // static | |
| 34 bool SuggestionAnswer::TextField::ParseTextField( | |
| 35 const base::DictionaryValue* field_json, TextField* text_field) { | |
| 36 return field_json->GetString(kAnswerJsonText, &text_field->text_) && | |
| 37 !text_field->text_.empty() && | |
| 38 field_json->GetInteger(kAnswerJsonTextType, &text_field->type_); | |
| 39 } | |
| 40 | |
| 41 bool SuggestionAnswer::TextField::Equals(const TextField& field) const { | |
| 42 return type_ == field.type_ && text_ == field.text_; | |
| 43 } | |
| 44 | |
| 45 // SuggestionAnswer::ImageLine ------------------------------------------------- | |
| 46 | |
| 47 SuggestionAnswer::ImageLine::ImageLine() {} | |
| 48 SuggestionAnswer::ImageLine::ImageLine(const ImageLine& line) | |
| 49 : text_fields_(line.text_fields_), | |
| 50 additional_text_(line.additional_text_.get() ? | |
|
Peter Kasting
2014/10/30 03:43:29
Nit: No get() on scoped_ptrs used as bools (6 plac
Justin Donnelly
2014/10/30 19:23:14
Done.
| |
| 51 new TextField(*line.additional_text_) : nullptr), | |
| 52 status_text_(line.status_text_.get() ? | |
| 53 new TextField(*line.status_text_) : nullptr) {} | |
| 54 | |
| 55 SuggestionAnswer::ImageLine::~ImageLine() {} | |
| 56 | |
| 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; | |
| 63 | |
| 64 const base::ListValue* fields_json; | |
| 65 if (!inner_json->GetList(kAnswerJsonText, &fields_json) || | |
| 66 fields_json->GetSize() == 0) | |
| 67 return false; | |
| 68 | |
| 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); | |
| 76 } | |
| 77 | |
| 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; | |
| 85 } | |
| 86 | |
| 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; | |
| 93 } | |
| 94 | |
| 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); | |
| 110 | |
| 111 if (!image_line->image_url_.is_valid()) | |
| 112 return false; | |
| 113 } | |
| 114 | |
| 115 return true; | |
| 116 } | |
| 117 | |
| 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; | |
| 124 } | |
| 125 | |
| 126 if (additional_text_.get()) { | |
|
Peter Kasting
2014/10/30 03:43:29
This conditional isn't quite correct, because it w
Justin Donnelly
2014/10/30 19:23:14
Done and done.
| |
| 127 if (!line.additional_text_.get() || | |
| 128 additional_text_->Equals(*line.additional_text_)) | |
| 129 return false; | |
| 130 } | |
| 131 | |
| 132 if (status_text_.get()) { | |
| 133 if (!line.status_text_.get() || status_text_->Equals(*line.status_text_)) | |
| 134 return false; | |
| 135 } | |
| 136 | |
| 137 return image_url_ == line.image_url_; | |
| 138 } | |
| 139 | |
| 140 // SuggestionAnswer ------------------------------------------------------------ | |
| 141 | |
| 142 SuggestionAnswer::SuggestionAnswer() : type_(-1) {} | |
| 143 SuggestionAnswer::SuggestionAnswer(const SuggestionAnswer& answer) | |
| 144 : first_line_(answer.first_line_), | |
| 145 second_line_(answer.second_line_), | |
| 146 type_(answer.type_) {} | |
| 147 | |
| 148 SuggestionAnswer::~SuggestionAnswer() {} | |
| 149 | |
| 150 // static | |
| 151 bool SuggestionAnswer::ParseAnswer( | |
|
Peter Kasting
2014/10/30 03:43:29
Hmm. I think it would be slightly nicer if this f
Justin Donnelly
2014/10/30 19:23:13
Done.
I agree that this is better. To be honest,
groby-ooo-7-16
2014/10/30 20:28:29
Uh-oh. Please step back from the computer - excite
| |
| 152 const std::string& answer_json, scoped_ptr<SuggestionAnswer>* answer) { | |
| 153 scoped_ptr<base::Value> value(base::JSONReader::Read(answer_json)); | |
| 154 base::DictionaryValue* dict; | |
| 155 if (!value.get() || !value->GetAsDictionary(&dict)) | |
| 156 return false; | |
| 157 | |
| 158 return ParseAnswer(dict, answer); | |
| 159 } | |
| 160 | |
| 161 // static | |
| 162 bool SuggestionAnswer::ParseAnswer( | |
| 163 const base::DictionaryValue* answer_json, | |
| 164 scoped_ptr<SuggestionAnswer>* answer) { | |
| 165 scoped_ptr<SuggestionAnswer> result(new SuggestionAnswer()); | |
| 166 | |
| 167 const base::ListValue* lines_json; | |
| 168 if (!answer_json->GetList(kAnswerJsonLines, &lines_json) || | |
| 169 lines_json->GetSize() != 2) | |
| 170 return false; | |
| 171 | |
| 172 const base::DictionaryValue* first_line_json; | |
| 173 if (!lines_json->GetDictionary(0, &first_line_json) || | |
| 174 !ImageLine::ParseImageLine(first_line_json, &result->first_line_)) | |
| 175 return false; | |
| 176 | |
| 177 const base::DictionaryValue* second_line_json; | |
| 178 if (!lines_json->GetDictionary(1, &second_line_json) || | |
| 179 !ImageLine::ParseImageLine(second_line_json, &result->second_line_)) | |
| 180 return false; | |
| 181 | |
| 182 (*answer) = result.Pass(); | |
| 183 return true; | |
| 184 } | |
| 185 | |
| 186 bool SuggestionAnswer::Equals(const SuggestionAnswer& answer) const { | |
| 187 return type_ == answer.type_ && | |
| 188 first_line_.Equals(answer.first_line_) && | |
| 189 second_line_.Equals(answer.second_line_); | |
| 190 } | |
| 191 | |
| 192 void SuggestionAnswer::GetImageURLs(std::vector<GURL>* urls) const { | |
| 193 urls->clear(); | |
| 194 if (first_line_.image_url().is_valid()) | |
| 195 urls->push_back(first_line_.image_url()); | |
| 196 if (second_line_.image_url().is_valid()) | |
| 197 urls->push_back(second_line_.image_url()); | |
| 198 } | |
| OLD | NEW |