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

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: 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..23f590cf880b66634d7c8ab0fa6087444bbc6498 100644
--- a/components/omnibox/search_suggestion_parser.cc
+++ b/components/omnibox/search_suggestion_parser.cc
@@ -65,6 +65,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 +84,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());
@@ -440,6 +442,7 @@ bool SearchSuggestionParser::ParseSuggestResults(
base::string16 annotation;
base::string16 answer_contents;
base::string16 answer_type;
+ SuggestionAnswer answer;
std::string suggest_query_params;
if (suggestion_details) {
@@ -456,12 +459,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);
+ 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);
+ suggestion_detail->GetString("ansb", &answer_type);
+ answer.SetType(answer_type);
+
+ answer.GetImageURLs(&results->answers_image_urls);
+ } else {
+ answer.Clear();
Peter Kasting 2014/10/24 22:38:09 Nit: Perhaps it would be nicer for ParseAnswer() t
Justin Donnelly 2014/10/27 21:41:15 Done.
+ DLOG(ERROR) << "Invalid suggestion answer json: "
Peter Kasting 2014/10/24 22:38:09 Nit: I'm never a fan of logging statements unless
Justin Donnelly 2014/10/27 21:41:15 Done. (In another change, I plan to add a UMA sta
+ << answer_contents;
+ }
}
}
}
@@ -472,42 +485,10 @@ bool SearchSuggestionParser::ParseSuggestResults(
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));
+ 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