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

Side by Side Diff: chrome/browser/autocomplete/search_provider.cc

Issue 12090006: Omnibox: Create Keyword Verbatim Result in Search Provider (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Bart's final comments. Created 7 years, 10 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 if (keyword_url && !keyword_url->IsExtensionKeyword()) {
802 AddMatchToMap(keyword_input_text_, keyword_input_text_,
803 CalculateRelevanceForKeywordVerbatim(
804 input_.type(), input_.prefer_keyword()),
805 AutocompleteMatch::SEARCH_OTHER_ENGINE,
806 did_not_accept_keyword_suggestion, true, &map);
807 }
808 }
809 const size_t verbatim_matches_size = map.size();
798 if (!default_provider_suggestion_.text.empty() && 810 if (!default_provider_suggestion_.text.empty() &&
799 default_provider_suggestion_.type == INSTANT_SUGGESTION_SEARCH && 811 default_provider_suggestion_.type == INSTANT_SUGGESTION_SEARCH &&
800 !input_.prevent_inline_autocomplete()) 812 !input_.prevent_inline_autocomplete())
801 AddMatchToMap(input_.text() + default_provider_suggestion_.text, 813 AddMatchToMap(input_.text() + default_provider_suggestion_.text,
802 input_.text(), verbatim_relevance + 1, 814 input_.text(), verbatim_relevance + 1,
803 AutocompleteMatch::SEARCH_SUGGEST, 815 AutocompleteMatch::SEARCH_SUGGEST,
804 did_not_accept_default_suggestion, false, &map); 816 did_not_accept_default_suggestion, false, &map);
805 817
806 AddHistoryResultsToMap(keyword_history_results_, true, 818 AddHistoryResultsToMap(keyword_history_results_, true,
807 did_not_accept_keyword_suggestion, &map); 819 did_not_accept_keyword_suggestion, &map);
(...skipping 15 matching lines...) Expand all
823 // |verbatim_relevance| here. 835 // |verbatim_relevance| here.
824 matches_.push_back(NavigationToMatch( 836 matches_.push_back(NavigationToMatch(
825 NavigationResult(GURL(UTF16ToUTF8(default_provider_suggestion_.text)), 837 NavigationResult(GURL(UTF16ToUTF8(default_provider_suggestion_.text)),
826 string16(), 838 string16(),
827 kNonURLVerbatimRelevance + 1), 839 kNonURLVerbatimRelevance + 1),
828 false)); 840 false));
829 } 841 }
830 AddNavigationResultsToMatches(keyword_navigation_results_, true); 842 AddNavigationResultsToMatches(keyword_navigation_results_, true);
831 AddNavigationResultsToMatches(default_navigation_results_, false); 843 AddNavigationResultsToMatches(default_navigation_results_, false);
832 844
833 // Allow an additional match for "what you typed" if it's present. 845 // Allow additional match(es) for verbatim results if present.
834 const size_t max_total_matches = kMaxMatches + what_you_typed_size; 846 const size_t max_total_matches = kMaxMatches + verbatim_matches_size;
835 std::partial_sort(matches_.begin(), 847 std::partial_sort(matches_.begin(),
836 matches_.begin() + std::min(max_total_matches, matches_.size()), 848 matches_.begin() + std::min(max_total_matches, matches_.size()),
837 matches_.end(), &AutocompleteMatch::MoreRelevant); 849 matches_.end(), &AutocompleteMatch::MoreRelevant);
838 850
839 // If the top match is effectively 'verbatim' but exceeds the calculated 851 // If the top match is effectively 'verbatim' but exceeds the calculated
840 // verbatim relevance, and REQUESTED_URL |input_| has a |desired_tld| 852 // verbatim relevance, and REQUESTED_URL |input_| has a |desired_tld|
841 // (for example ".com" when the CTRL key is pressed for REQUESTED_URL input), 853 // (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 854 // 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, 855 // stomp the HistoryURLProvider's similar transient URL_WHAT_YOU_TYPED match,
844 // and CTRL+ENTER will invoke the search instead of the expected navigation. 856 // and CTRL+ENTER will invoke the search instead of the expected navigation.
(...skipping 13 matching lines...) Expand all
858 } 870 }
859 871
860 bool SearchProvider::IsTopMatchScoreTooLow() const { 872 bool SearchProvider::IsTopMatchScoreTooLow() const {
861 return matches_.front().relevance < CalculateRelevanceForVerbatim(); 873 return matches_.front().relevance < CalculateRelevanceForVerbatim();
862 } 874 }
863 875
864 bool SearchProvider::IsTopMatchHighRankSearchForURL() const { 876 bool SearchProvider::IsTopMatchHighRankSearchForURL() const {
865 return input_.type() == AutocompleteInput::URL && 877 return input_.type() == AutocompleteInput::URL &&
866 matches_.front().relevance > CalculateRelevanceForVerbatim() && 878 matches_.front().relevance > CalculateRelevanceForVerbatim() &&
867 (matches_.front().type == AutocompleteMatch::SEARCH_SUGGEST || 879 (matches_.front().type == AutocompleteMatch::SEARCH_SUGGEST ||
868 matches_.front().type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED); 880 matches_.front().type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED ||
881 matches_.front().type == AutocompleteMatch::SEARCH_OTHER_ENGINE);
869 } 882 }
870 883
871 bool SearchProvider::IsTopMatchNotInlinable() const { 884 bool SearchProvider::IsTopMatchNotInlinable() const {
872 return matches_.front().type != AutocompleteMatch::SEARCH_WHAT_YOU_TYPED && 885 return matches_.front().type != AutocompleteMatch::SEARCH_WHAT_YOU_TYPED &&
873 matches_.front().type != AutocompleteMatch::URL_WHAT_YOU_TYPED && 886 matches_.front().type != AutocompleteMatch::URL_WHAT_YOU_TYPED &&
887 matches_.front().type != AutocompleteMatch::SEARCH_OTHER_ENGINE &&
msw 2013/01/31 00:38:16 I realize the only SEARCH_OTHER_ENGINE match at th
Mark P 2013/01/31 23:09:27 I like this idea:
msw 2013/02/01 20:52:37 Hmm, that might be a decent DCHECK to add there, r
Mark P 2013/02/02 00:24:35 Agreed that writing the DCHECK is overkill (lots o
msw 2013/02/02 02:27:52 It might not be too tough if you use .EndsWith, et
Mark P 2013/02/04 19:46:09 I tried to write a test that would work (verify th
msw 2013/02/04 22:51:39 Please try using URLPrefix::BestURLPrefix.
Mark P 2013/02/05 00:54:36 I was hoping you'd let me get away without this ch
874 matches_.front().inline_autocomplete_offset == string16::npos && 888 matches_.front().inline_autocomplete_offset == string16::npos &&
875 matches_.front().fill_into_edit != input_.text(); 889 matches_.front().fill_into_edit != input_.text();
876 } 890 }
877 891
878 void SearchProvider::UpdateMatches() { 892 void SearchProvider::UpdateMatches() {
879 ConvertResultsToAutocompleteMatches(); 893 ConvertResultsToAutocompleteMatches();
880 894
881 // Check constraints that may be violated by suggested relevances. 895 // Check constraints that may be violated by suggested relevances.
882 if (!matches_.empty() && 896 if (!matches_.empty() &&
883 (has_suggested_relevance_ || verbatim_relevance_ >= 0)) { 897 (has_suggested_relevance_ || verbatim_relevance_ >= 0)) {
(...skipping 11 matching lines...) Expand all
895 // Disregard the suggested search and verbatim relevances if the input 909 // Disregard the suggested search and verbatim relevances if the input
896 // type is URL and the top match is a highly-ranked search suggestion. 910 // 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 911 // For example, prevent a search for "foo.com" from outranking another
898 // provider's navigation for "foo.com" or "foo.com/url_from_history". 912 // provider's navigation for "foo.com" or "foo.com/url_from_history".
899 ApplyCalculatedSuggestRelevance(&keyword_suggest_results_, true); 913 ApplyCalculatedSuggestRelevance(&keyword_suggest_results_, true);
900 ApplyCalculatedSuggestRelevance(&default_suggest_results_, false); 914 ApplyCalculatedSuggestRelevance(&default_suggest_results_, false);
901 verbatim_relevance_ = -1; 915 verbatim_relevance_ = -1;
902 ConvertResultsToAutocompleteMatches(); 916 ConvertResultsToAutocompleteMatches();
903 } 917 }
904 if (IsTopMatchNotInlinable()) { 918 if (IsTopMatchNotInlinable()) {
905 // Disregard suggested relevances if the top match is not SWYT, inlinable, 919 // 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). 920 // 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 921 // regardless of inlining). For example, input "foo" should not
908 // would happen if the "bar" search match outranked all other matches. 922 // invoke a search for "bar", which would happen if the "bar" search
923 // match outranked all other matches.
909 ApplyCalculatedRelevance(); 924 ApplyCalculatedRelevance();
910 ConvertResultsToAutocompleteMatches(); 925 ConvertResultsToAutocompleteMatches();
911 } 926 }
912 DCHECK(!IsTopMatchScoreTooLow()); 927 DCHECK(!IsTopMatchScoreTooLow());
913 DCHECK(!IsTopMatchHighRankSearchForURL()); 928 DCHECK(!IsTopMatchHighRankSearchForURL());
914 DCHECK(!IsTopMatchNotInlinable()); 929 DCHECK(!IsTopMatchNotInlinable());
915 } 930 }
916 931
917 UpdateStarredStateOfMatches(); 932 UpdateStarredStateOfMatches();
918 UpdateDone(); 933 UpdateDone();
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
1083 1098
1084 case AutocompleteInput::URL: 1099 case AutocompleteInput::URL:
1085 return 850; 1100 return 850;
1086 1101
1087 default: 1102 default:
1088 NOTREACHED(); 1103 NOTREACHED();
1089 return 0; 1104 return 0;
1090 } 1105 }
1091 } 1106 }
1092 1107
1108 // static
1109 int SearchProvider::CalculateRelevanceForKeywordVerbatim(
1110 AutocompleteInput::Type type,
1111 bool prefer_keyword) {
1112 if (prefer_keyword)
1113 return 1500;
1114 return type == AutocompleteInput::QUERY ? 1450 : 1100;
1115 }
1116
1093 int SearchProvider::CalculateRelevanceForHistory( 1117 int SearchProvider::CalculateRelevanceForHistory(
1094 const Time& time, 1118 const Time& time,
1095 bool is_keyword, 1119 bool is_keyword,
1096 bool prevent_inline_autocomplete) const { 1120 bool prevent_inline_autocomplete) const {
1097 // The relevance of past searches falls off over time. There are two distinct 1121 // 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 1122 // 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 1123 // 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 1124 // 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 1125 // minutes ago is discounted 50 points, while the relevance of a search two
1102 // weeks ago is discounted 450 points. 1126 // weeks ago is discounted 450 points.
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
1301 } 1325 }
1302 1326
1303 void SearchProvider::UpdateDone() { 1327 void SearchProvider::UpdateDone() {
1304 // We're done when the timer isn't running, there are no suggest queries 1328 // We're done when the timer isn't running, there are no suggest queries
1305 // pending, and we're not waiting on instant. 1329 // pending, and we're not waiting on instant.
1306 done_ = (!timer_.IsRunning() && (suggest_results_pending_ == 0) && 1330 done_ = (!timer_.IsRunning() && (suggest_results_pending_ == 0) &&
1307 (instant_finalized_ || 1331 (instant_finalized_ ||
1308 (!chrome::BrowserInstantController::IsInstantEnabled(profile_) && 1332 (!chrome::BrowserInstantController::IsInstantEnabled(profile_) &&
1309 !chrome::search::IsInstantExtendedAPIEnabled(profile_)))); 1333 !chrome::search::IsInstantExtendedAPIEnabled(profile_))));
1310 } 1334 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698