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/search_suggestion_parser.h" | 5 #include "components/omnibox/search_suggestion_parser.h" |
6 | 6 |
7 #include "base/i18n/icu_string_conversions.h" | 7 #include "base/i18n/icu_string_conversions.h" |
8 #include "base/json/json_string_value_serializer.h" | 8 #include "base/json/json_string_value_serializer.h" |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/strings/string_number_conversions.h" | |
11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
13 #include "base/values.h" | 14 #include "base/values.h" |
14 #include "components/omnibox/autocomplete_input.h" | 15 #include "components/omnibox/autocomplete_input.h" |
15 #include "components/omnibox/url_prefix.h" | 16 #include "components/omnibox/url_prefix.h" |
16 #include "components/url_fixer/url_fixer.h" | 17 #include "components/url_fixer/url_fixer.h" |
17 #include "net/base/net_util.h" | 18 #include "net/base/net_util.h" |
18 #include "net/http/http_response_headers.h" | 19 #include "net/http/http_response_headers.h" |
19 #include "net/url_request/url_fetcher.h" | 20 #include "net/url_request/url_fetcher.h" |
20 #include "url/url_constants.h" | 21 #include "url/url_constants.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 // SearchSuggestionParser::SuggestResult --------------------------------------- | 59 // SearchSuggestionParser::SuggestResult --------------------------------------- |
59 | 60 |
60 SearchSuggestionParser::SuggestResult::SuggestResult( | 61 SearchSuggestionParser::SuggestResult::SuggestResult( |
61 const base::string16& suggestion, | 62 const base::string16& suggestion, |
62 AutocompleteMatchType::Type type, | 63 AutocompleteMatchType::Type type, |
63 const base::string16& match_contents, | 64 const base::string16& match_contents, |
64 const base::string16& match_contents_prefix, | 65 const base::string16& match_contents_prefix, |
65 const base::string16& annotation, | 66 const base::string16& annotation, |
66 const base::string16& answer_contents, | 67 const base::string16& answer_contents, |
67 const base::string16& answer_type, | 68 const base::string16& answer_type, |
69 scoped_ptr<SuggestionAnswer> answer, | |
68 const std::string& suggest_query_params, | 70 const std::string& suggest_query_params, |
69 const std::string& deletion_url, | 71 const std::string& deletion_url, |
70 bool from_keyword_provider, | 72 bool from_keyword_provider, |
71 int relevance, | 73 int relevance, |
72 bool relevance_from_server, | 74 bool relevance_from_server, |
73 bool should_prefetch, | 75 bool should_prefetch, |
74 const base::string16& input_text) | 76 const base::string16& input_text) |
75 : Result(from_keyword_provider, | 77 : Result(from_keyword_provider, |
76 relevance, | 78 relevance, |
77 relevance_from_server, | 79 relevance_from_server, |
78 type, | 80 type, |
79 deletion_url), | 81 deletion_url), |
80 suggestion_(suggestion), | 82 suggestion_(suggestion), |
81 match_contents_prefix_(match_contents_prefix), | 83 match_contents_prefix_(match_contents_prefix), |
82 annotation_(annotation), | 84 annotation_(annotation), |
83 suggest_query_params_(suggest_query_params), | 85 suggest_query_params_(suggest_query_params), |
84 answer_contents_(answer_contents), | 86 answer_contents_(answer_contents), |
85 answer_type_(answer_type), | 87 answer_type_(answer_type), |
88 answer_(answer.Pass()), | |
86 should_prefetch_(should_prefetch) { | 89 should_prefetch_(should_prefetch) { |
87 match_contents_ = match_contents; | 90 match_contents_ = match_contents; |
88 DCHECK(!match_contents_.empty()); | 91 DCHECK(!match_contents_.empty()); |
89 ClassifyMatchContents(true, input_text); | 92 ClassifyMatchContents(true, input_text); |
90 } | 93 } |
91 | 94 |
95 SearchSuggestionParser::SuggestResult::SuggestResult( | |
96 const SuggestResult& result) | |
97 : Result(result), | |
98 suggestion_(result.suggestion_), | |
99 match_contents_prefix_(result.match_contents_prefix_), | |
100 annotation_(result.annotation_), | |
101 suggest_query_params_(result.suggest_query_params_), | |
102 answer_contents_(result.answer_contents_), | |
103 answer_type_(result.answer_type_), | |
104 answer_(SuggestionAnswer::Copy(result.answer_.get())), | |
105 should_prefetch_(result.should_prefetch_) { | |
106 } | |
107 | |
92 SearchSuggestionParser::SuggestResult::~SuggestResult() {} | 108 SearchSuggestionParser::SuggestResult::~SuggestResult() {} |
93 | 109 |
110 SearchSuggestionParser::SuggestResult& | |
111 SearchSuggestionParser::SuggestResult::operator=(const SuggestResult& rhs) { | |
112 if (this == &rhs) | |
113 return *this; | |
114 | |
115 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.
| |
116 suggestion_ = rhs.suggestion_; | |
117 match_contents_prefix_ = rhs.match_contents_prefix_; | |
118 annotation_ = rhs.annotation_; | |
119 suggest_query_params_ = rhs.suggest_query_params_; | |
120 answer_contents_ = rhs.answer_contents_; | |
121 answer_type_ = rhs.answer_type_; | |
122 answer_ = SuggestionAnswer::Copy(rhs.answer_.get()); | |
123 should_prefetch_ = rhs.should_prefetch_; | |
124 | |
125 return *this; | |
126 } | |
127 | |
94 void SearchSuggestionParser::SuggestResult::ClassifyMatchContents( | 128 void SearchSuggestionParser::SuggestResult::ClassifyMatchContents( |
95 const bool allow_bolding_all, | 129 const bool allow_bolding_all, |
96 const base::string16& input_text) { | 130 const base::string16& input_text) { |
97 if (input_text.empty()) { | 131 if (input_text.empty()) { |
98 // In case of zero-suggest results, do not highlight matches. | 132 // In case of zero-suggest results, do not highlight matches. |
99 match_contents_class_.push_back( | 133 match_contents_class_.push_back( |
100 ACMatchClassification(0, ACMatchClassification::NONE)); | 134 ACMatchClassification(0, ACMatchClassification::NONE)); |
101 return; | 135 return; |
102 } | 136 } |
103 | 137 |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
432 results->navigation_results.push_back(NavigationResult( | 466 results->navigation_results.push_back(NavigationResult( |
433 scheme_classifier, url, match_type, title, deletion_url, | 467 scheme_classifier, url, match_type, title, deletion_url, |
434 is_keyword_result, relevance, relevances != NULL, input.text(), | 468 is_keyword_result, relevance, relevances != NULL, input.text(), |
435 languages)); | 469 languages)); |
436 } | 470 } |
437 } else { | 471 } else { |
438 base::string16 match_contents = suggestion; | 472 base::string16 match_contents = suggestion; |
439 base::string16 match_contents_prefix; | 473 base::string16 match_contents_prefix; |
440 base::string16 annotation; | 474 base::string16 annotation; |
441 base::string16 answer_contents; | 475 base::string16 answer_contents; |
442 base::string16 answer_type; | 476 base::string16 answer_type_str; |
477 scoped_ptr<SuggestionAnswer> answer; | |
443 std::string suggest_query_params; | 478 std::string suggest_query_params; |
444 | 479 |
445 if (suggestion_details) { | 480 if (suggestion_details) { |
446 suggestion_details->GetDictionary(index, &suggestion_detail); | 481 suggestion_details->GetDictionary(index, &suggestion_detail); |
447 if (suggestion_detail) { | 482 if (suggestion_detail) { |
448 suggestion_detail->GetString("t", &match_contents); | 483 suggestion_detail->GetString("t", &match_contents); |
449 suggestion_detail->GetString("mp", &match_contents_prefix); | 484 suggestion_detail->GetString("mp", &match_contents_prefix); |
450 // Error correction for bad data from server. | 485 // Error correction for bad data from server. |
451 if (match_contents.empty()) | 486 if (match_contents.empty()) |
452 match_contents = suggestion; | 487 match_contents = suggestion; |
453 suggestion_detail->GetString("a", &annotation); | 488 suggestion_detail->GetString("a", &annotation); |
454 suggestion_detail->GetString("q", &suggest_query_params); | 489 suggestion_detail->GetString("q", &suggest_query_params); |
455 | 490 |
456 // Extract Answers, if provided. | 491 // Extract Answers, if provided. |
457 const base::DictionaryValue* answer_json = NULL; | 492 const base::DictionaryValue* answer_json = NULL; |
458 if (suggestion_detail->GetDictionary("ansa", &answer_json)) { | 493 int answer_type = 0; |
494 if (suggestion_detail->GetDictionary("ansa", &answer_json) && | |
495 suggestion_detail->GetString("ansb", &answer_type_str) && | |
496 base::StringToInt(answer_type_str, &answer_type) && | |
497 SuggestionAnswer::ParseAnswer(answer_json, &answer)) { | |
459 match_type = AutocompleteMatchType::SEARCH_SUGGEST_ANSWER; | 498 match_type = AutocompleteMatchType::SEARCH_SUGGEST_ANSWER; |
460 GetAnswersImageURLs(answer_json, &results->answers_image_urls); | 499 answer->set_type(answer_type); |
500 | |
461 std::string contents; | 501 std::string contents; |
462 base::JSONWriter::Write(answer_json, &contents); | 502 base::JSONWriter::Write(answer_json, &contents); |
463 answer_contents = base::UTF8ToUTF16(contents); | 503 answer_contents = base::UTF8ToUTF16(contents); |
464 suggestion_detail->GetString("ansb", &answer_type); | 504 |
505 answer->GetImageURLs(&results->answers_image_urls); | |
465 } | 506 } |
466 } | 507 } |
467 } | 508 } |
468 | 509 |
469 bool should_prefetch = static_cast<int>(index) == prefetch_index; | 510 bool should_prefetch = static_cast<int>(index) == prefetch_index; |
470 // TODO(kochi): Improve calculator suggestion presentation. | 511 // TODO(kochi): Improve calculator suggestion presentation. |
471 results->suggest_results.push_back(SuggestResult( | 512 results->suggest_results.push_back(SuggestResult( |
472 base::CollapseWhitespace(suggestion, false), match_type, | 513 base::CollapseWhitespace(suggestion, false), match_type, |
473 base::CollapseWhitespace(match_contents, false), | 514 base::CollapseWhitespace(match_contents, false), |
474 match_contents_prefix, annotation, answer_contents, answer_type, | 515 match_contents_prefix, annotation, answer_contents, answer_type_str, |
475 suggest_query_params, deletion_url, is_keyword_result, relevance, | 516 answer.Pass(), suggest_query_params, deletion_url, is_keyword_result, |
476 relevances != NULL, should_prefetch, trimmed_input)); | 517 relevance, relevances != NULL, should_prefetch, trimmed_input)); |
477 } | 518 } |
478 } | 519 } |
479 results->relevances_from_server = relevances != NULL; | 520 results->relevances_from_server = relevances != NULL; |
480 return true; | 521 return true; |
481 } | 522 } |
482 | |
483 // static | |
484 void SearchSuggestionParser::GetAnswersImageURLs( | |
485 const base::DictionaryValue* answer_json, | |
486 std::vector<GURL>* urls) { | |
487 DCHECK(answer_json); | |
488 | |
489 const base::ListValue* lines = NULL; | |
490 if (!answer_json->GetList("l", &lines) || !lines || lines->GetSize() == 0) | |
491 return; | |
492 | |
493 for (base::ListValue::const_iterator iter = lines->begin(); | |
494 iter != lines->end(); | |
495 ++iter) { | |
496 const base::DictionaryValue* line = NULL; | |
497 if (!(*iter)->GetAsDictionary(&line) || !line) | |
498 continue; | |
499 | |
500 std::string image_host_and_path; | |
501 if (!line->GetString("il.i.d", &image_host_and_path) || | |
502 image_host_and_path.empty()) | |
503 continue; | |
504 // Concatenate scheme and host/path using only ':' as separator. This is | |
505 // due to the results delivering strings of the form '//host/path', which | |
506 // is web-speak for "use the enclosing page's scheme", but not a valid path | |
507 // of an URL. | |
508 GURL image_url( | |
509 GURL(std::string(url::kHttpsScheme) + ":" + image_host_and_path)); | |
510 if (image_url.is_valid()) | |
511 urls->push_back(image_url); | |
512 } | |
513 } | |
OLD | NEW |