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/strings/string_util.h" | |
| 8 #include "base/strings/utf_string_conversions.h" | |
| 9 #include "base/values.h" | |
| 10 #include "url/url_constants.h" | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 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"; | |
|
groby-ooo-7-16
2014/10/30 20:28:29
text constants should always be const char arrays,
Justin Donnelly
2014/10/30 21:38:03
Done.
| |
| 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"; | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 // SuggestionAnswer::TextField ------------------------------------------------- | |
| 28 | |
| 29 SuggestionAnswer::TextField::TextField() : type_(-1) {} | |
| 30 SuggestionAnswer::TextField::~TextField() {} | |
| 31 | |
| 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_); | |
| 38 } | |
| 39 | |
| 40 bool SuggestionAnswer::TextField::Equals(const TextField& field) const { | |
| 41 return type_ == field.type_ && text_ == field.text_; | |
| 42 } | |
| 43 | |
| 44 // SuggestionAnswer::ImageLine ------------------------------------------------- | |
| 45 | |
| 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_) {} | |
| 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()) | |
|
groby-ooo-7-16
2014/10/30 20:28:29
I'd argue this is a valid reason why TextField nee
Peter Kasting
2014/10/30 20:32:56
I'm very sympathetic to that, and in fact in gener
groby-ooo-7-16
2014/10/30 20:46:12
Grumble mumble. I'd say that the code base _happil
Justin Donnelly
2014/10/30 21:38:03
Acknowledged.
| |
| 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_ || line.additional_text_) { | |
| 127 if (!additional_text_ || !line.additional_text_) | |
| 128 return false; | |
| 129 if (!additional_text_->Equals(*line.additional_text_)) | |
| 130 return false; | |
| 131 } | |
| 132 | |
| 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; | |
| 138 } | |
| 139 | |
| 140 return image_url_ == line.image_url_; | |
| 141 } | |
| 142 | |
| 143 // SuggestionAnswer ------------------------------------------------------------ | |
| 144 | |
| 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_) {} | |
| 150 | |
| 151 SuggestionAnswer::~SuggestionAnswer() {} | |
| 152 | |
| 153 // static | |
| 154 scoped_ptr<SuggestionAnswer> SuggestionAnswer::ParseAnswer( | |
| 155 const base::DictionaryValue* answer_json) { | |
| 156 scoped_ptr<SuggestionAnswer> result(new SuggestionAnswer()); | |
|
groby-ooo-7-16
2014/10/30 20:28:29
shorter:
auto result=make_scoped_ptr(new Suggesti
Justin Donnelly
2014/10/30 21:38:03
Done.
| |
| 157 | |
| 158 const base::ListValue* lines_json; | |
| 159 if (!answer_json->GetList(kAnswerJsonLines, &lines_json) || | |
| 160 lines_json->GetSize() != 2) | |
| 161 return nullptr; | |
| 162 | |
| 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; | |
| 167 | |
| 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; | |
| 172 | |
| 173 return result.Pass(); | |
| 174 } | |
| 175 | |
| 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_); | |
| 180 } | |
| 181 | |
| 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()); | |
| 187 } | |
| OLD | NEW |