| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/omnibox/browser/suggestion_answer.h" | 5 #include "components/omnibox/browser/suggestion_answer.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 } | 60 } |
| 61 | 61 |
| 62 bool SuggestionAnswer::TextField::Equals(const TextField& field) const { | 62 bool SuggestionAnswer::TextField::Equals(const TextField& field) const { |
| 63 return type_ == field.type_ && text_ == field.text_ && | 63 return type_ == field.type_ && text_ == field.text_ && |
| 64 has_num_lines_ == field.has_num_lines_ && | 64 has_num_lines_ == field.has_num_lines_ && |
| 65 (!has_num_lines_ || num_lines_ == field.num_lines_); | 65 (!has_num_lines_ || num_lines_ == field.num_lines_); |
| 66 } | 66 } |
| 67 | 67 |
| 68 // SuggestionAnswer::ImageLine ------------------------------------------------- | 68 // SuggestionAnswer::ImageLine ------------------------------------------------- |
| 69 | 69 |
| 70 SuggestionAnswer::ImageLine::ImageLine() {} | 70 SuggestionAnswer::ImageLine::ImageLine() |
| 71 : text_num_lines_(1) {} |
| 71 SuggestionAnswer::ImageLine::ImageLine(const ImageLine& line) | 72 SuggestionAnswer::ImageLine::ImageLine(const ImageLine& line) |
| 72 : text_fields_(line.text_fields_), | 73 : text_fields_(line.text_fields_), |
| 74 text_num_lines_(line.text_num_lines_), |
| 73 additional_text_(line.additional_text_ ? | 75 additional_text_(line.additional_text_ ? |
| 74 new TextField(*line.additional_text_) : nullptr), | 76 new TextField(*line.additional_text_) : nullptr), |
| 75 status_text_(line.status_text_ ? | 77 status_text_(line.status_text_ ? |
| 76 new TextField(*line.status_text_) : nullptr), | 78 new TextField(*line.status_text_) : nullptr), |
| 77 image_url_(line.image_url_) {} | 79 image_url_(line.image_url_) {} |
| 78 | 80 |
| 79 SuggestionAnswer::ImageLine::~ImageLine() {} | 81 SuggestionAnswer::ImageLine::~ImageLine() {} |
| 80 | 82 |
| 81 // static | 83 // static |
| 82 bool SuggestionAnswer::ImageLine::ParseImageLine( | 84 bool SuggestionAnswer::ImageLine::ParseImageLine( |
| 83 const base::DictionaryValue* line_json, ImageLine* image_line) { | 85 const base::DictionaryValue* line_json, ImageLine* image_line) { |
| 84 const base::DictionaryValue* inner_json; | 86 const base::DictionaryValue* inner_json; |
| 85 if (!line_json->GetDictionary(kAnswerJsonImageLine, &inner_json)) | 87 if (!line_json->GetDictionary(kAnswerJsonImageLine, &inner_json)) |
| 86 return false; | 88 return false; |
| 87 | 89 |
| 88 const base::ListValue* fields_json; | 90 const base::ListValue* fields_json; |
| 89 if (!inner_json->GetList(kAnswerJsonText, &fields_json) || | 91 if (!inner_json->GetList(kAnswerJsonText, &fields_json) || |
| 90 fields_json->GetSize() == 0) | 92 fields_json->GetSize() == 0) |
| 91 return false; | 93 return false; |
| 92 | 94 |
| 95 bool found_num_lines = false; |
| 93 for (size_t i = 0; i < fields_json->GetSize(); ++i) { | 96 for (size_t i = 0; i < fields_json->GetSize(); ++i) { |
| 94 const base::DictionaryValue* field_json; | 97 const base::DictionaryValue* field_json; |
| 95 TextField text_field; | 98 TextField text_field; |
| 96 if (!fields_json->GetDictionary(i, &field_json) || | 99 if (!fields_json->GetDictionary(i, &field_json) || |
| 97 !TextField::ParseTextField(field_json, &text_field)) | 100 !TextField::ParseTextField(field_json, &text_field)) |
| 98 return false; | 101 return false; |
| 99 image_line->text_fields_.push_back(text_field); | 102 image_line->text_fields_.push_back(text_field); |
| 103 if (!found_num_lines && text_field.has_num_lines()) { |
| 104 found_num_lines = true; |
| 105 image_line->text_num_lines_ = text_field.num_lines(); |
| 106 } |
| 100 } | 107 } |
| 101 | 108 |
| 102 if (inner_json->HasKey(kAnswerJsonAdditionalText)) { | 109 if (inner_json->HasKey(kAnswerJsonAdditionalText)) { |
| 103 image_line->additional_text_.reset(new TextField()); | 110 image_line->additional_text_.reset(new TextField()); |
| 104 const base::DictionaryValue* field_json; | 111 const base::DictionaryValue* field_json; |
| 105 if (!inner_json->GetDictionary(kAnswerJsonAdditionalText, &field_json) || | 112 if (!inner_json->GetDictionary(kAnswerJsonAdditionalText, &field_json) || |
| 106 !TextField::ParseTextField(field_json, | 113 !TextField::ParseTextField(field_json, |
| 107 image_line->additional_text_.get())) | 114 image_line->additional_text_.get())) |
| 108 return false; | 115 return false; |
| 109 } | 116 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 } | 148 } |
| 142 | 149 |
| 143 bool SuggestionAnswer::ImageLine::Equals(const ImageLine& line) const { | 150 bool SuggestionAnswer::ImageLine::Equals(const ImageLine& line) const { |
| 144 if (text_fields_.size() != line.text_fields_.size()) | 151 if (text_fields_.size() != line.text_fields_.size()) |
| 145 return false; | 152 return false; |
| 146 for (size_t i = 0; i < text_fields_.size(); ++i) { | 153 for (size_t i = 0; i < text_fields_.size(); ++i) { |
| 147 if (!text_fields_[i].Equals(line.text_fields_[i])) | 154 if (!text_fields_[i].Equals(line.text_fields_[i])) |
| 148 return false; | 155 return false; |
| 149 } | 156 } |
| 150 | 157 |
| 158 if (text_num_lines_ != line.text_num_lines_) |
| 159 return false; |
| 160 |
| 151 if (additional_text_ || line.additional_text_) { | 161 if (additional_text_ || line.additional_text_) { |
| 152 if (!additional_text_ || !line.additional_text_) | 162 if (!additional_text_ || !line.additional_text_) |
| 153 return false; | 163 return false; |
| 154 if (!additional_text_->Equals(*line.additional_text_)) | 164 if (!additional_text_->Equals(*line.additional_text_)) |
| 155 return false; | 165 return false; |
| 156 } | 166 } |
| 157 | 167 |
| 158 if (status_text_ || line.status_text_) { | 168 if (status_text_ || line.status_text_) { |
| 159 if (!status_text_ || !line.status_text_) | 169 if (!status_text_ || !line.status_text_) |
| 160 return false; | 170 return false; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 172 for (const TextField& text_field : text_fields_) | 182 for (const TextField& text_field : text_fields_) |
| 173 AppendWithSpace(&text_field, &result); | 183 AppendWithSpace(&text_field, &result); |
| 174 AppendWithSpace(additional_text_.get(), &result); | 184 AppendWithSpace(additional_text_.get(), &result); |
| 175 AppendWithSpace(status_text_.get(), &result); | 185 AppendWithSpace(status_text_.get(), &result); |
| 176 return result; | 186 return result; |
| 177 } | 187 } |
| 178 | 188 |
| 179 // SuggestionAnswer ------------------------------------------------------------ | 189 // SuggestionAnswer ------------------------------------------------------------ |
| 180 | 190 |
| 181 SuggestionAnswer::SuggestionAnswer() : type_(-1) {} | 191 SuggestionAnswer::SuggestionAnswer() : type_(-1) {} |
| 182 SuggestionAnswer::SuggestionAnswer(const SuggestionAnswer& answer) | |
| 183 : first_line_(answer.first_line_), | |
| 184 second_line_(answer.second_line_), | |
| 185 type_(answer.type_) {} | |
| 186 | 192 |
| 187 SuggestionAnswer::~SuggestionAnswer() {} | 193 SuggestionAnswer::~SuggestionAnswer() {} |
| 188 | 194 |
| 189 // static | 195 // static |
| 190 std::unique_ptr<SuggestionAnswer> SuggestionAnswer::ParseAnswer( | 196 std::unique_ptr<SuggestionAnswer> SuggestionAnswer::ParseAnswer( |
| 191 const base::DictionaryValue* answer_json) { | 197 const base::DictionaryValue* answer_json) { |
| 192 auto result = base::WrapUnique(new SuggestionAnswer); | 198 auto result = base::WrapUnique(new SuggestionAnswer); |
| 193 | 199 |
| 194 const base::ListValue* lines_json; | 200 const base::ListValue* lines_json; |
| 195 if (!answer_json->GetList(kAnswerJsonLines, &lines_json) || | 201 if (!answer_json->GetList(kAnswerJsonLines, &lines_json) || |
| (...skipping 18 matching lines...) Expand all Loading... |
| 214 first_line_.Equals(answer.first_line_) && | 220 first_line_.Equals(answer.first_line_) && |
| 215 second_line_.Equals(answer.second_line_); | 221 second_line_.Equals(answer.second_line_); |
| 216 } | 222 } |
| 217 | 223 |
| 218 void SuggestionAnswer::AddImageURLsTo(std::vector<GURL>* urls) const { | 224 void SuggestionAnswer::AddImageURLsTo(std::vector<GURL>* urls) const { |
| 219 if (first_line_.image_url().is_valid()) | 225 if (first_line_.image_url().is_valid()) |
| 220 urls->push_back(first_line_.image_url()); | 226 urls->push_back(first_line_.image_url()); |
| 221 if (second_line_.image_url().is_valid()) | 227 if (second_line_.image_url().is_valid()) |
| 222 urls->push_back(second_line_.image_url()); | 228 urls->push_back(second_line_.image_url()); |
| 223 } | 229 } |
| OLD | NEW |