OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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_provider.h" | 5 #include "components/omnibox/search_provider.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
(...skipping 805 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
816 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); | 816 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
817 if (verbatim_relevance > 0) { | 817 if (verbatim_relevance > 0) { |
818 const base::string16& trimmed_verbatim = | 818 const base::string16& trimmed_verbatim = |
819 base::CollapseWhitespace(input_.text(), false); | 819 base::CollapseWhitespace(input_.text(), false); |
820 | 820 |
821 // Verbatim results don't get suggestions and hence, answers. | 821 // Verbatim results don't get suggestions and hence, answers. |
822 // Scan previous matches if the last answer-bearing suggestion matches | 822 // Scan previous matches if the last answer-bearing suggestion matches |
823 // verbatim, and if so, copy over answer contents. | 823 // verbatim, and if so, copy over answer contents. |
824 base::string16 answer_contents; | 824 base::string16 answer_contents; |
825 base::string16 answer_type; | 825 base::string16 answer_type; |
| 826 scoped_ptr<SuggestionAnswer> answer; |
826 for (ACMatches::iterator it = matches_.begin(); it != matches_.end(); | 827 for (ACMatches::iterator it = matches_.begin(); it != matches_.end(); |
827 ++it) { | 828 ++it) { |
828 if (!it->answer_contents.empty() && | 829 if (!it->answer_contents.empty() && |
829 it->fill_into_edit == trimmed_verbatim) { | 830 it->fill_into_edit == trimmed_verbatim) { |
830 answer_contents = it->answer_contents; | 831 answer_contents = it->answer_contents; |
831 answer_type = it->answer_type; | 832 answer_type = it->answer_type; |
| 833 answer.reset(it->answer.get() ? |
| 834 new SuggestionAnswer(*it->answer) : nullptr); |
832 break; | 835 break; |
833 } | 836 } |
834 } | 837 } |
835 | 838 |
836 SearchSuggestionParser::SuggestResult verbatim( | 839 SearchSuggestionParser::SuggestResult verbatim( |
837 trimmed_verbatim, AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | 840 trimmed_verbatim, AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
838 trimmed_verbatim, base::string16(), base::string16(), answer_contents, | 841 trimmed_verbatim, base::string16(), base::string16(), answer_contents, |
839 answer_type, std::string(), std::string(), false, verbatim_relevance, | 842 answer_type, answer.Pass(), std::string(), std::string(), false, |
840 relevance_from_server, false, trimmed_verbatim); | 843 verbatim_relevance, relevance_from_server, false, trimmed_verbatim); |
841 AddMatchToMap(verbatim, std::string(), did_not_accept_default_suggestion, | 844 AddMatchToMap(verbatim, std::string(), did_not_accept_default_suggestion, |
842 false, keyword_url != NULL, &map); | 845 false, keyword_url != NULL, &map); |
843 } | 846 } |
844 if (!keyword_input_.text().empty()) { | 847 if (!keyword_input_.text().empty()) { |
845 // We only create the verbatim search query match for a keyword | 848 // We only create the verbatim search query match for a keyword |
846 // if it's not an extension keyword. Extension keywords are handled | 849 // if it's not an extension keyword. Extension keywords are handled |
847 // in KeywordProvider::Start(). (Extensions are complicated...) | 850 // in KeywordProvider::Start(). (Extensions are complicated...) |
848 // Note: in this provider, SEARCH_OTHER_ENGINE must correspond | 851 // Note: in this provider, SEARCH_OTHER_ENGINE must correspond |
849 // to the keyword verbatim search query. Do not create other matches | 852 // to the keyword verbatim search query. Do not create other matches |
850 // of type SEARCH_OTHER_ENGINE. | 853 // of type SEARCH_OTHER_ENGINE. |
851 if (keyword_url && | 854 if (keyword_url && |
852 (keyword_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) { | 855 (keyword_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) { |
853 bool keyword_relevance_from_server; | 856 bool keyword_relevance_from_server; |
854 const int keyword_verbatim_relevance = | 857 const int keyword_verbatim_relevance = |
855 GetKeywordVerbatimRelevance(&keyword_relevance_from_server); | 858 GetKeywordVerbatimRelevance(&keyword_relevance_from_server); |
856 if (keyword_verbatim_relevance > 0) { | 859 if (keyword_verbatim_relevance > 0) { |
857 const base::string16& trimmed_verbatim = | 860 const base::string16& trimmed_verbatim = |
858 base::CollapseWhitespace(keyword_input_.text(), false); | 861 base::CollapseWhitespace(keyword_input_.text(), false); |
859 SearchSuggestionParser::SuggestResult verbatim( | 862 SearchSuggestionParser::SuggestResult verbatim( |
860 trimmed_verbatim, AutocompleteMatchType::SEARCH_OTHER_ENGINE, | 863 trimmed_verbatim, AutocompleteMatchType::SEARCH_OTHER_ENGINE, |
861 trimmed_verbatim, base::string16(), base::string16(), | 864 trimmed_verbatim, base::string16(), base::string16(), |
862 base::string16(), base::string16(), std::string(), std::string(), | 865 base::string16(), base::string16(), nullptr, std::string(), |
863 true, keyword_verbatim_relevance, keyword_relevance_from_server, | 866 std::string(), true, keyword_verbatim_relevance, |
864 false, trimmed_verbatim); | 867 keyword_relevance_from_server, false, trimmed_verbatim); |
865 AddMatchToMap(verbatim, std::string(), | 868 AddMatchToMap(verbatim, std::string(), |
866 did_not_accept_keyword_suggestion, false, true, &map); | 869 did_not_accept_keyword_suggestion, false, true, &map); |
867 } | 870 } |
868 } | 871 } |
869 } | 872 } |
870 AddRawHistoryResultsToMap(true, did_not_accept_keyword_suggestion, &map); | 873 AddRawHistoryResultsToMap(true, did_not_accept_keyword_suggestion, &map); |
871 AddRawHistoryResultsToMap(false, did_not_accept_default_suggestion, &map); | 874 AddRawHistoryResultsToMap(false, did_not_accept_default_suggestion, &map); |
872 | 875 |
873 AddSuggestResultsToMap(keyword_results_.suggest_results, | 876 AddSuggestResultsToMap(keyword_results_.suggest_results, |
874 keyword_results_.metadata, &map); | 877 keyword_results_.metadata, &map); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
934 | 937 |
935 matches_.push_back(*i); | 938 matches_.push_back(*i); |
936 } | 939 } |
937 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.ConvertResultsTime", | 940 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.ConvertResultsTime", |
938 base::TimeTicks::Now() - start_time); | 941 base::TimeTicks::Now() - start_time); |
939 } | 942 } |
940 | 943 |
941 void SearchProvider::RemoveExtraAnswers(ACMatches* matches) { | 944 void SearchProvider::RemoveExtraAnswers(ACMatches* matches) { |
942 bool answer_seen = false; | 945 bool answer_seen = false; |
943 for (ACMatches::iterator it = matches->begin(); it != matches->end(); ++it) { | 946 for (ACMatches::iterator it = matches->begin(); it != matches->end(); ++it) { |
944 if (!it->answer_contents.empty()) { | 947 if (!it->answer.get()) { |
945 if (!answer_seen) { | 948 if (!answer_seen) { |
946 answer_seen = true; | 949 answer_seen = true; |
947 } else { | 950 } else { |
948 it->answer_contents.clear(); | 951 it->answer_contents.clear(); |
949 it->answer_type.clear(); | 952 it->answer_type.clear(); |
| 953 it->answer.reset(); |
950 } | 954 } |
951 } | 955 } |
952 } | 956 } |
953 } | 957 } |
954 | 958 |
955 ACMatches::const_iterator SearchProvider::FindTopMatch() const { | 959 ACMatches::const_iterator SearchProvider::FindTopMatch() const { |
956 ACMatches::const_iterator it = matches_.begin(); | 960 ACMatches::const_iterator it = matches_.begin(); |
957 while ((it != matches_.end()) && !it->allowed_to_be_default_match) | 961 while ((it != matches_.end()) && !it->allowed_to_be_default_match) |
958 ++it; | 962 ++it; |
959 return it; | 963 return it; |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1058 // typed match to always be first. | 1062 // typed match to always be first. |
1059 SearchSuggestionParser::SuggestResults::iterator insertion_position = | 1063 SearchSuggestionParser::SuggestResults::iterator insertion_position = |
1060 scored_results.end(); | 1064 scored_results.end(); |
1061 if (trimmed_suggestion == trimmed_input) { | 1065 if (trimmed_suggestion == trimmed_input) { |
1062 found_what_you_typed_match = true; | 1066 found_what_you_typed_match = true; |
1063 insertion_position = scored_results.begin(); | 1067 insertion_position = scored_results.begin(); |
1064 } | 1068 } |
1065 SearchSuggestionParser::SuggestResult history_suggestion( | 1069 SearchSuggestionParser::SuggestResult history_suggestion( |
1066 trimmed_suggestion, AutocompleteMatchType::SEARCH_HISTORY, | 1070 trimmed_suggestion, AutocompleteMatchType::SEARCH_HISTORY, |
1067 trimmed_suggestion, base::string16(), base::string16(), | 1071 trimmed_suggestion, base::string16(), base::string16(), |
1068 base::string16(), base::string16(), std::string(), std::string(), | 1072 base::string16(), base::string16(), nullptr, std::string(), |
1069 is_keyword, relevance, false, false, trimmed_input); | 1073 std::string(), is_keyword, relevance, false, false, trimmed_input); |
1070 // History results are synchronous; they are received on the last keystroke. | 1074 // History results are synchronous; they are received on the last keystroke. |
1071 history_suggestion.set_received_after_last_keystroke(false); | 1075 history_suggestion.set_received_after_last_keystroke(false); |
1072 scored_results.insert(insertion_position, history_suggestion); | 1076 scored_results.insert(insertion_position, history_suggestion); |
1073 } | 1077 } |
1074 | 1078 |
1075 // History returns results sorted for us. However, we may have docked some | 1079 // History returns results sorted for us. However, we may have docked some |
1076 // results' scores, so things are no longer in order. While keeping the | 1080 // results' scores, so things are no longer in order. While keeping the |
1077 // what-you-typed match at the front (if it exists), do a stable sort to get | 1081 // what-you-typed match at the front (if it exists), do a stable sort to get |
1078 // things back in order without otherwise disturbing results with equal | 1082 // things back in order without otherwise disturbing results with equal |
1079 // scores, then force the scores to be unique, so that the order in which | 1083 // scores, then force the scores to be unique, so that the order in which |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1431 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i) | 1435 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i) |
1432 matches.push_back(i->second); | 1436 matches.push_back(i->second); |
1433 std::sort(matches.begin(), matches.end(), &AutocompleteMatch::MoreRelevant); | 1437 std::sort(matches.begin(), matches.end(), &AutocompleteMatch::MoreRelevant); |
1434 | 1438 |
1435 // If there is a top scoring entry, find the corresponding answer. | 1439 // If there is a top scoring entry, find the corresponding answer. |
1436 if (!matches.empty()) | 1440 if (!matches.empty()) |
1437 return answers_cache_.GetTopAnswerEntry(matches[0].contents); | 1441 return answers_cache_.GetTopAnswerEntry(matches[0].contents); |
1438 | 1442 |
1439 return AnswersQueryData(); | 1443 return AnswersQueryData(); |
1440 } | 1444 } |
OLD | NEW |