Chromium Code Reviews| 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 "chrome/browser/autocomplete/search_provider.h" | 5 #include "chrome/browser/autocomplete/search_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 765 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 776 } | 776 } |
| 777 | 777 |
| 778 void SearchProvider::ConvertResultsToAutocompleteMatches() { | 778 void SearchProvider::ConvertResultsToAutocompleteMatches() { |
| 779 // Convert all the results to matches and add them to a map, so we can keep | 779 // Convert all the results to matches and add them to a map, so we can keep |
| 780 // the most relevant match for each result. | 780 // the most relevant match for each result. |
| 781 MatchMap map; | 781 MatchMap map; |
| 782 const Time no_time; | 782 const Time no_time; |
| 783 int did_not_accept_keyword_suggestion = keyword_suggest_results_.empty() ? | 783 int did_not_accept_keyword_suggestion = keyword_suggest_results_.empty() ? |
| 784 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : | 784 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
| 785 TemplateURLRef::NO_SUGGESTION_CHOSEN; | 785 TemplateURLRef::NO_SUGGESTION_CHOSEN; |
| 786 // Keyword what you typed results are handled by the KeywordProvider. | |
| 787 | 786 |
| 788 int verbatim_relevance = GetVerbatimRelevance(); | 787 int verbatim_relevance = GetVerbatimRelevance(); |
| 789 int did_not_accept_default_suggestion = default_suggest_results_.empty() ? | 788 int did_not_accept_default_suggestion = default_suggest_results_.empty() ? |
| 790 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : | 789 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
| 791 TemplateURLRef::NO_SUGGESTION_CHOSEN; | 790 TemplateURLRef::NO_SUGGESTION_CHOSEN; |
| 792 if (verbatim_relevance > 0) { | 791 if (verbatim_relevance > 0) { |
| 793 AddMatchToMap(input_.text(), input_.text(), verbatim_relevance, | 792 AddMatchToMap(input_.text(), input_.text(), verbatim_relevance, |
| 794 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, | 793 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, |
| 795 did_not_accept_default_suggestion, false, &map); | 794 did_not_accept_default_suggestion, false, &map); |
| 796 } | 795 } |
| 797 const size_t what_you_typed_size = map.size(); | 796 if (!keyword_input_text_.empty()) { |
| 797 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); | |
| 798 // We only create the verbatim search query match for a keyword | |
| 799 // if it's not an extension keyword. Extension keywords are handled | |
| 800 // in KeywordProvider::Start(). (Extensions are complicated...) | |
| 801 // Note: in this provider, SEARCH_OTHER_ENGINE must correspond | |
| 802 // to the keyword verbatim search query. Do not create other matches | |
| 803 // of type SEARCH_OTHER_ENGINE. | |
| 804 if (keyword_url && !keyword_url->IsExtensionKeyword()) { | |
| 805 AddMatchToMap(keyword_input_text_, keyword_input_text_, | |
| 806 CalculateRelevanceForKeywordVerbatim( | |
| 807 input_.type(), input_.prefer_keyword()), | |
| 808 AutocompleteMatch::SEARCH_OTHER_ENGINE, | |
| 809 did_not_accept_keyword_suggestion, true, &map); | |
| 810 } | |
| 811 } | |
| 812 const size_t verbatim_matches_size = map.size(); | |
| 798 if (!default_provider_suggestion_.text.empty() && | 813 if (!default_provider_suggestion_.text.empty() && |
| 799 default_provider_suggestion_.type == INSTANT_SUGGESTION_SEARCH && | 814 default_provider_suggestion_.type == INSTANT_SUGGESTION_SEARCH && |
| 800 !input_.prevent_inline_autocomplete()) | 815 !input_.prevent_inline_autocomplete()) |
| 801 AddMatchToMap(input_.text() + default_provider_suggestion_.text, | 816 AddMatchToMap(input_.text() + default_provider_suggestion_.text, |
| 802 input_.text(), verbatim_relevance + 1, | 817 input_.text(), verbatim_relevance + 1, |
| 803 AutocompleteMatch::SEARCH_SUGGEST, | 818 AutocompleteMatch::SEARCH_SUGGEST, |
| 804 did_not_accept_default_suggestion, false, &map); | 819 did_not_accept_default_suggestion, false, &map); |
| 805 | 820 |
| 806 AddHistoryResultsToMap(keyword_history_results_, true, | 821 AddHistoryResultsToMap(keyword_history_results_, true, |
| 807 did_not_accept_keyword_suggestion, &map); | 822 did_not_accept_keyword_suggestion, &map); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 823 // |verbatim_relevance| here. | 838 // |verbatim_relevance| here. |
| 824 matches_.push_back(NavigationToMatch( | 839 matches_.push_back(NavigationToMatch( |
| 825 NavigationResult(GURL(UTF16ToUTF8(default_provider_suggestion_.text)), | 840 NavigationResult(GURL(UTF16ToUTF8(default_provider_suggestion_.text)), |
| 826 string16(), | 841 string16(), |
| 827 kNonURLVerbatimRelevance + 1), | 842 kNonURLVerbatimRelevance + 1), |
| 828 false)); | 843 false)); |
| 829 } | 844 } |
| 830 AddNavigationResultsToMatches(keyword_navigation_results_, true); | 845 AddNavigationResultsToMatches(keyword_navigation_results_, true); |
| 831 AddNavigationResultsToMatches(default_navigation_results_, false); | 846 AddNavigationResultsToMatches(default_navigation_results_, false); |
| 832 | 847 |
| 833 // Allow an additional match for "what you typed" if it's present. | 848 // Allow additional match(es) for verbatim results if present. |
| 834 const size_t max_total_matches = kMaxMatches + what_you_typed_size; | 849 const size_t max_total_matches = kMaxMatches + verbatim_matches_size; |
| 835 std::partial_sort(matches_.begin(), | 850 std::partial_sort(matches_.begin(), |
| 836 matches_.begin() + std::min(max_total_matches, matches_.size()), | 851 matches_.begin() + std::min(max_total_matches, matches_.size()), |
| 837 matches_.end(), &AutocompleteMatch::MoreRelevant); | 852 matches_.end(), &AutocompleteMatch::MoreRelevant); |
| 838 | 853 |
| 839 // If the top match is effectively 'verbatim' but exceeds the calculated | 854 // If the top match is effectively 'verbatim' but exceeds the calculated |
| 840 // verbatim relevance, and REQUESTED_URL |input_| has a |desired_tld| | 855 // verbatim relevance, and REQUESTED_URL |input_| has a |desired_tld| |
| 841 // (for example ".com" when the CTRL key is pressed for REQUESTED_URL input), | 856 // (for example ".com" when the CTRL key is pressed for REQUESTED_URL input), |
| 842 // promote a URL_WHAT_YOU_TYPED match to the top. Otherwise, these matches can | 857 // promote a URL_WHAT_YOU_TYPED match to the top. Otherwise, these matches can |
| 843 // stomp the HistoryURLProvider's similar transient URL_WHAT_YOU_TYPED match, | 858 // stomp the HistoryURLProvider's similar transient URL_WHAT_YOU_TYPED match, |
| 844 // and CTRL+ENTER will invoke the search instead of the expected navigation. | 859 // and CTRL+ENTER will invoke the search instead of the expected navigation. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 858 } | 873 } |
| 859 | 874 |
| 860 bool SearchProvider::IsTopMatchScoreTooLow() const { | 875 bool SearchProvider::IsTopMatchScoreTooLow() const { |
| 861 return matches_.front().relevance < CalculateRelevanceForVerbatim(); | 876 return matches_.front().relevance < CalculateRelevanceForVerbatim(); |
| 862 } | 877 } |
| 863 | 878 |
| 864 bool SearchProvider::IsTopMatchHighRankSearchForURL() const { | 879 bool SearchProvider::IsTopMatchHighRankSearchForURL() const { |
| 865 return input_.type() == AutocompleteInput::URL && | 880 return input_.type() == AutocompleteInput::URL && |
| 866 matches_.front().relevance > CalculateRelevanceForVerbatim() && | 881 matches_.front().relevance > CalculateRelevanceForVerbatim() && |
| 867 (matches_.front().type == AutocompleteMatch::SEARCH_SUGGEST || | 882 (matches_.front().type == AutocompleteMatch::SEARCH_SUGGEST || |
| 868 matches_.front().type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED); | 883 matches_.front().type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED || |
| 884 matches_.front().type == AutocompleteMatch::SEARCH_OTHER_ENGINE); | |
| 869 } | 885 } |
| 870 | 886 |
| 871 bool SearchProvider::IsTopMatchNotInlinable() const { | 887 bool SearchProvider::IsTopMatchNotInlinable() const { |
| 888 // Note: this test assumes the SEARCH_OTHER_ENGINE match corresponds to | |
| 889 // the verbatim search query on the keyword engine. SearchProvider should | |
| 890 // not create any other match of type SEARCH_OTHER_ENGINE. | |
| 872 return matches_.front().type != AutocompleteMatch::SEARCH_WHAT_YOU_TYPED && | 891 return matches_.front().type != AutocompleteMatch::SEARCH_WHAT_YOU_TYPED && |
| 873 matches_.front().type != AutocompleteMatch::URL_WHAT_YOU_TYPED && | 892 matches_.front().type != AutocompleteMatch::URL_WHAT_YOU_TYPED && |
| 893 matches_.front().type != AutocompleteMatch::SEARCH_OTHER_ENGINE && | |
| 874 matches_.front().inline_autocomplete_offset == string16::npos && | 894 matches_.front().inline_autocomplete_offset == string16::npos && |
| 875 matches_.front().fill_into_edit != input_.text(); | 895 matches_.front().fill_into_edit != input_.text(); |
| 876 } | 896 } |
| 877 | 897 |
| 878 void SearchProvider::UpdateMatches() { | 898 void SearchProvider::UpdateMatches() { |
| 879 ConvertResultsToAutocompleteMatches(); | 899 ConvertResultsToAutocompleteMatches(); |
| 880 | 900 |
| 881 // Check constraints that may be violated by suggested relevances. | 901 // Check constraints that may be violated by suggested relevances. |
| 882 if (!matches_.empty() && | 902 if (!matches_.empty() && |
| 883 (has_suggested_relevance_ || verbatim_relevance_ >= 0)) { | 903 (has_suggested_relevance_ || verbatim_relevance_ >= 0)) { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 895 // Disregard the suggested search and verbatim relevances if the input | 915 // Disregard the suggested search and verbatim relevances if the input |
| 896 // type is URL and the top match is a highly-ranked search suggestion. | 916 // type is URL and the top match is a highly-ranked search suggestion. |
| 897 // For example, prevent a search for "foo.com" from outranking another | 917 // For example, prevent a search for "foo.com" from outranking another |
| 898 // provider's navigation for "foo.com" or "foo.com/url_from_history". | 918 // provider's navigation for "foo.com" or "foo.com/url_from_history". |
| 899 ApplyCalculatedSuggestRelevance(&keyword_suggest_results_, true); | 919 ApplyCalculatedSuggestRelevance(&keyword_suggest_results_, true); |
| 900 ApplyCalculatedSuggestRelevance(&default_suggest_results_, false); | 920 ApplyCalculatedSuggestRelevance(&default_suggest_results_, false); |
| 901 verbatim_relevance_ = -1; | 921 verbatim_relevance_ = -1; |
| 902 ConvertResultsToAutocompleteMatches(); | 922 ConvertResultsToAutocompleteMatches(); |
| 903 } | 923 } |
| 904 if (IsTopMatchNotInlinable()) { | 924 if (IsTopMatchNotInlinable()) { |
| 905 // Disregard suggested relevances if the top match is not SWYT, inlinable, | 925 // Disregard suggested relevances if the top match is not a verbatim |
| 906 // or URL_WHAT_YOU_TYPED (which may be top match regardless of inlining). | 926 // match, inlinable, or URL_WHAT_YOU_TYPED (which may be top match |
| 907 // For example, input "foo" should not invoke a search for "bar", which | 927 // regardless of inlining). For example, input "foo" should not |
| 908 // would happen if the "bar" search match outranked all other matches. | 928 // invoke a search for "bar", which would happen if the "bar" search |
| 929 // match outranked all other matches. | |
| 909 ApplyCalculatedRelevance(); | 930 ApplyCalculatedRelevance(); |
| 910 ConvertResultsToAutocompleteMatches(); | 931 ConvertResultsToAutocompleteMatches(); |
| 911 } | 932 } |
| 912 DCHECK(!IsTopMatchScoreTooLow()); | 933 DCHECK(!IsTopMatchScoreTooLow()); |
| 913 DCHECK(!IsTopMatchHighRankSearchForURL()); | 934 DCHECK(!IsTopMatchHighRankSearchForURL()); |
| 914 DCHECK(!IsTopMatchNotInlinable()); | 935 DCHECK(!IsTopMatchNotInlinable()); |
| 915 } | 936 } |
| 916 | 937 |
| 917 UpdateStarredStateOfMatches(); | 938 UpdateStarredStateOfMatches(); |
| 918 UpdateDone(); | 939 UpdateDone(); |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1083 | 1104 |
| 1084 case AutocompleteInput::URL: | 1105 case AutocompleteInput::URL: |
| 1085 return 850; | 1106 return 850; |
| 1086 | 1107 |
| 1087 default: | 1108 default: |
| 1088 NOTREACHED(); | 1109 NOTREACHED(); |
| 1089 return 0; | 1110 return 0; |
| 1090 } | 1111 } |
| 1091 } | 1112 } |
| 1092 | 1113 |
| 1114 // static | |
| 1115 int SearchProvider::CalculateRelevanceForKeywordVerbatim( | |
| 1116 AutocompleteInput::Type type, | |
| 1117 bool prefer_keyword) { | |
| 1118 // Perhaps this should be kept similar to | |
|
Peter Kasting
2013/02/05 23:17:48
I'm not a huge fan of the "perhaps" comments here
Mark P
2013/02/06 01:31:59
I don't like these comments either. I think they
Peter Kasting
2013/02/06 02:01:41
My hope was either:
* Make the functions match an
Mark P
2013/02/06 04:31:14
My judgment call is that they don't need to match.
Peter Kasting
2013/02/06 18:47:34
Honestly, looking at these, it sure looks to me li
Mark P
2013/02/06 18:54:11
You are correct; that's how I made the function.
Peter Kasting
2013/02/06 19:00:09
That seems like the right comment, then:
// This
Mark P
2013/02/06 19:50:22
That comment sounds good to me. I copied and past
| |
| 1119 // KeywordProvider::CalculateRelevance(). That function calculates, | |
| 1120 // among other things, the verbatim query score for search keywords | |
| 1121 // but only extension-based ones. It would be a bit odd if the score | |
| 1122 // for a verbatim query for an extension keyword and for a | |
| 1123 // non-extension keyword differed dramatically (though no immediate | |
| 1124 // harm would come from it). | |
| 1125 if (prefer_keyword) | |
| 1126 return 1500; | |
| 1127 return type == AutocompleteInput::QUERY ? 1450 : 1100; | |
|
Peter Kasting
2013/02/05 23:17:48
Nit: I'd put parens around binary subexpression fo
Mark P
2013/02/06 01:31:59
Done.
| |
| 1128 } | |
| 1129 | |
| 1093 int SearchProvider::CalculateRelevanceForHistory( | 1130 int SearchProvider::CalculateRelevanceForHistory( |
| 1094 const Time& time, | 1131 const Time& time, |
| 1095 bool is_keyword, | 1132 bool is_keyword, |
| 1096 bool prevent_inline_autocomplete) const { | 1133 bool prevent_inline_autocomplete) const { |
| 1097 // The relevance of past searches falls off over time. There are two distinct | 1134 // The relevance of past searches falls off over time. There are two distinct |
| 1098 // equations used. If the first equation is used (searches to the primary | 1135 // equations used. If the first equation is used (searches to the primary |
| 1099 // provider that we want to inline autocomplete), the score starts at 1399 and | 1136 // provider that we want to inline autocomplete), the score starts at 1399 and |
| 1100 // falls to 1300. If the second equation is used the relevance of a search 15 | 1137 // falls to 1300. If the second equation is used the relevance of a search 15 |
| 1101 // minutes ago is discounted 50 points, while the relevance of a search two | 1138 // minutes ago is discounted 50 points, while the relevance of a search two |
| 1102 // weeks ago is discounted 450 points. | 1139 // weeks ago is discounted 450 points. |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1301 } | 1338 } |
| 1302 | 1339 |
| 1303 void SearchProvider::UpdateDone() { | 1340 void SearchProvider::UpdateDone() { |
| 1304 // We're done when the timer isn't running, there are no suggest queries | 1341 // We're done when the timer isn't running, there are no suggest queries |
| 1305 // pending, and we're not waiting on instant. | 1342 // pending, and we're not waiting on instant. |
| 1306 done_ = (!timer_.IsRunning() && (suggest_results_pending_ == 0) && | 1343 done_ = (!timer_.IsRunning() && (suggest_results_pending_ == 0) && |
| 1307 (instant_finalized_ || | 1344 (instant_finalized_ || |
| 1308 (!chrome::BrowserInstantController::IsInstantEnabled(profile_) && | 1345 (!chrome::BrowserInstantController::IsInstantEnabled(profile_) && |
| 1309 !chrome::search::IsInstantExtendedAPIEnabled(profile_)))); | 1346 !chrome::search::IsInstantExtendedAPIEnabled(profile_)))); |
| 1310 } | 1347 } |
| OLD | NEW |