Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(402)

Unified Diff: components/omnibox/search_suggestion_parser.cc

Issue 669573005: Add a class to parse answer json. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync and respond to comments Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..9a005c83a2b1e515f16456e9a37df0a2251a7482 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,
+ const SuggestionAnswer& answer,
const std::string& suggest_query_params,
const std::string& deletion_url,
bool from_keyword_provider,
@@ -83,6 +85,7 @@ SearchSuggestionParser::SuggestResult::SuggestResult(
suggest_query_params_(suggest_query_params),
answer_contents_(answer_contents),
answer_type_(answer_type),
+ answer_(answer),
should_prefetch_(should_prefetch) {
match_contents_ = match_contents;
DCHECK(!match_contents_.empty());
@@ -439,7 +442,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;
+ SuggestionAnswer answer;
std::string suggest_query_params;
if (suggestion_details) {
@@ -456,12 +460,22 @@ bool SearchSuggestionParser::ParseSuggestResults(
// Extract Answers, if provided.
const base::DictionaryValue* answer_json = NULL;
if (suggestion_detail->GetDictionary("ansa", &answer_json)) {
- match_type = AutocompleteMatchType::SEARCH_SUGGEST_ANSWER;
- GetAnswersImageURLs(answer_json, &results->answers_image_urls);
- std::string contents;
- base::JSONWriter::Write(answer_json, &contents);
- answer_contents = base::UTF8ToUTF16(contents);
- suggestion_detail->GetString("ansb", &answer_type);
+ suggestion_detail->GetString("ansb", &answer_type_str);
+ int answer_type;
+ if (!base::StringToInt(answer_type_str, &answer_type))
+ answer_type = -1;
+ answer.set_type(answer_type);
+
+ SuggestionAnswer::ParseAnswer(answer_json, &answer);
+ if (answer.is_valid()) {
+ match_type = AutocompleteMatchType::SEARCH_SUGGEST_ANSWER;
+
+ std::string contents;
+ base::JSONWriter::Write(answer_json, &contents);
+ answer_contents = base::UTF8ToUTF16(contents);
+
+ answer.GetImageURLs(&results->answers_image_urls);
+ }
}
}
}
@@ -471,43 +485,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, 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);
- }
-}

Powered by Google App Engine
This is Rietveld 408576698