Chromium Code Reviews| Index: components/omnibox/search_suggestion_parser.cc |
| diff --git a/components/omnibox/search_suggestion_parser.cc b/components/omnibox/search_suggestion_parser.cc |
| index 965662fd0e2cfb0f74453362c6bdf65bd48bb73c..622f7dbcc8be2b06a4ca8296c8f7875b5b9fca19 100644 |
| --- a/components/omnibox/search_suggestion_parser.cc |
| +++ b/components/omnibox/search_suggestion_parser.cc |
| @@ -8,6 +8,7 @@ |
| #include "base/json/json_string_value_serializer.h" |
| #include "base/json/json_writer.h" |
| #include "base/logging.h" |
| +#include "base/strings/string_number_conversions.h" |
| #include "base/strings/string_util.h" |
| #include "base/strings/utf_string_conversions.h" |
| #include "base/values.h" |
| @@ -65,6 +66,7 @@ SearchSuggestionParser::SuggestResult::SuggestResult( |
| const base::string16& annotation, |
| const base::string16& answer_contents, |
| const base::string16& answer_type, |
| + scoped_ptr<SuggestionAnswer> answer, |
| const std::string& suggest_query_params, |
| const std::string& deletion_url, |
| bool from_keyword_provider, |
| @@ -83,14 +85,46 @@ SearchSuggestionParser::SuggestResult::SuggestResult( |
| suggest_query_params_(suggest_query_params), |
| answer_contents_(answer_contents), |
| answer_type_(answer_type), |
| + answer_(answer.Pass()), |
| should_prefetch_(should_prefetch) { |
| match_contents_ = match_contents; |
| DCHECK(!match_contents_.empty()); |
| ClassifyMatchContents(true, input_text); |
| } |
| +SearchSuggestionParser::SuggestResult::SuggestResult( |
| + const SuggestResult& result) |
| + : Result(result), |
| + suggestion_(result.suggestion_), |
| + match_contents_prefix_(result.match_contents_prefix_), |
| + annotation_(result.annotation_), |
| + suggest_query_params_(result.suggest_query_params_), |
| + answer_contents_(result.answer_contents_), |
| + answer_type_(result.answer_type_), |
| + answer_(SuggestionAnswer::Copy(result.answer_.get())), |
| + should_prefetch_(result.should_prefetch_) { |
| +} |
| + |
| SearchSuggestionParser::SuggestResult::~SuggestResult() {} |
| +SearchSuggestionParser::SuggestResult& |
| + SearchSuggestionParser::SuggestResult::operator=(const SuggestResult& rhs) { |
| + if (this == &rhs) |
| + return *this; |
| + |
| + this->Result::operator=(rhs); |
|
Peter Kasting
2014/10/30 03:43:29
I don't understand why Rachel asked for "this->" h
groby-ooo-7-16
2014/10/30 16:21:14
It parses easier, to my brain. Since it doesn't to
Justin Donnelly
2014/10/30 19:23:13
Done.
|
| + suggestion_ = rhs.suggestion_; |
| + match_contents_prefix_ = rhs.match_contents_prefix_; |
| + annotation_ = rhs.annotation_; |
| + suggest_query_params_ = rhs.suggest_query_params_; |
| + answer_contents_ = rhs.answer_contents_; |
| + answer_type_ = rhs.answer_type_; |
| + answer_ = SuggestionAnswer::Copy(rhs.answer_.get()); |
| + should_prefetch_ = rhs.should_prefetch_; |
| + |
| + return *this; |
| +} |
| + |
| void SearchSuggestionParser::SuggestResult::ClassifyMatchContents( |
| const bool allow_bolding_all, |
| const base::string16& input_text) { |
| @@ -439,7 +473,8 @@ bool SearchSuggestionParser::ParseSuggestResults( |
| base::string16 match_contents_prefix; |
| base::string16 annotation; |
| base::string16 answer_contents; |
| - base::string16 answer_type; |
| + base::string16 answer_type_str; |
| + scoped_ptr<SuggestionAnswer> answer; |
| std::string suggest_query_params; |
| if (suggestion_details) { |
| @@ -455,13 +490,19 @@ bool SearchSuggestionParser::ParseSuggestResults( |
| // Extract Answers, if provided. |
| const base::DictionaryValue* answer_json = NULL; |
| - if (suggestion_detail->GetDictionary("ansa", &answer_json)) { |
| + int answer_type = 0; |
| + if (suggestion_detail->GetDictionary("ansa", &answer_json) && |
| + suggestion_detail->GetString("ansb", &answer_type_str) && |
| + base::StringToInt(answer_type_str, &answer_type) && |
| + SuggestionAnswer::ParseAnswer(answer_json, &answer)) { |
| match_type = AutocompleteMatchType::SEARCH_SUGGEST_ANSWER; |
| - GetAnswersImageURLs(answer_json, &results->answers_image_urls); |
| + answer->set_type(answer_type); |
| + |
| std::string contents; |
| base::JSONWriter::Write(answer_json, &contents); |
| answer_contents = base::UTF8ToUTF16(contents); |
| - suggestion_detail->GetString("ansb", &answer_type); |
| + |
| + answer->GetImageURLs(&results->answers_image_urls); |
| } |
| } |
| } |
| @@ -471,43 +512,11 @@ bool SearchSuggestionParser::ParseSuggestResults( |
| results->suggest_results.push_back(SuggestResult( |
| base::CollapseWhitespace(suggestion, false), match_type, |
| base::CollapseWhitespace(match_contents, false), |
| - match_contents_prefix, annotation, answer_contents, answer_type, |
| - suggest_query_params, deletion_url, is_keyword_result, relevance, |
| - relevances != NULL, should_prefetch, trimmed_input)); |
| + match_contents_prefix, annotation, answer_contents, answer_type_str, |
| + answer.Pass(), suggest_query_params, deletion_url, is_keyword_result, |
| + relevance, relevances != NULL, should_prefetch, trimmed_input)); |
| } |
| } |
| results->relevances_from_server = relevances != NULL; |
| return true; |
| } |
| - |
| -// static |
| -void SearchSuggestionParser::GetAnswersImageURLs( |
| - const base::DictionaryValue* answer_json, |
| - std::vector<GURL>* urls) { |
| - DCHECK(answer_json); |
| - |
| - const base::ListValue* lines = NULL; |
| - if (!answer_json->GetList("l", &lines) || !lines || lines->GetSize() == 0) |
| - return; |
| - |
| - for (base::ListValue::const_iterator iter = lines->begin(); |
| - iter != lines->end(); |
| - ++iter) { |
| - const base::DictionaryValue* line = NULL; |
| - if (!(*iter)->GetAsDictionary(&line) || !line) |
| - continue; |
| - |
| - std::string image_host_and_path; |
| - if (!line->GetString("il.i.d", &image_host_and_path) || |
| - image_host_and_path.empty()) |
| - continue; |
| - // Concatenate scheme and host/path using only ':' as separator. This is |
| - // due to the results delivering strings of the form '//host/path', which |
| - // is web-speak for "use the enclosing page's scheme", but not a valid path |
| - // of an URL. |
| - GURL image_url( |
| - GURL(std::string(url::kHttpsScheme) + ":" + image_host_and_path)); |
| - if (image_url.is_valid()) |
| - urls->push_back(image_url); |
| - } |
| -} |