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 828 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
839 AutocompleteMatch match = HistoryURLProvider::SuggestExactInput( | 839 AutocompleteMatch match = HistoryURLProvider::SuggestExactInput( |
840 this, input_, !HasHTTPScheme(input_.text())); | 840 this, input_, !HasHTTPScheme(input_.text())); |
841 match.relevance = matches_.front().relevance + 1; | 841 match.relevance = matches_.front().relevance + 1; |
842 matches_.insert(matches_.begin(), match); | 842 matches_.insert(matches_.begin(), match); |
843 } | 843 } |
844 | 844 |
845 if (matches_.size() > max_total_matches) | 845 if (matches_.size() > max_total_matches) |
846 matches_.resize(max_total_matches); | 846 matches_.resize(max_total_matches); |
847 } | 847 } |
848 | 848 |
849 bool SearchProvider::IsTopMatchScoreTooLow() const { | 849 bool SearchProvider::IsTopMatchScoreTooLow( |
850 return matches_.front().relevance < CalculateRelevanceForVerbatim(); | 850 int keyword_search_what_you_typed_relevance) const { |
| 851 return std::max(matches_.front().relevance, |
| 852 keyword_search_what_you_typed_relevance) < |
| 853 CalculateRelevanceForVerbatim(); |
851 } | 854 } |
852 | 855 |
853 bool SearchProvider::IsTopMatchHighRankSearchForURL() const { | 856 bool SearchProvider::IsTopMatchHighRankSearchForURL( |
| 857 int keyword_search_what_you_typed_relevance) const { |
854 return input_.type() == AutocompleteInput::URL && | 858 return input_.type() == AutocompleteInput::URL && |
855 matches_.front().relevance > CalculateRelevanceForVerbatim() && | 859 ((keyword_search_what_you_typed_relevance > |
856 (matches_.front().type == AutocompleteMatch::SEARCH_SUGGEST || | 860 matches_.front().relevance) || |
857 matches_.front().type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED); | 861 (matches_.front().relevance > CalculateRelevanceForVerbatim() && |
| 862 (matches_.front().type == AutocompleteMatch::SEARCH_SUGGEST || |
| 863 matches_.front().type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED)))
; |
858 } | 864 } |
859 | 865 |
860 bool SearchProvider::IsTopMatchNotInlinable() const { | 866 bool SearchProvider::IsTopMatchNotInlinable( |
861 return matches_.front().type != AutocompleteMatch::SEARCH_WHAT_YOU_TYPED && | 867 int keyword_search_what_you_typed_relevance) const { |
| 868 return (keyword_search_what_you_typed_relevance < |
| 869 matches_.front().relevance) && |
| 870 matches_.front().type != AutocompleteMatch::SEARCH_WHAT_YOU_TYPED && |
862 matches_.front().type != AutocompleteMatch::URL_WHAT_YOU_TYPED && | 871 matches_.front().type != AutocompleteMatch::URL_WHAT_YOU_TYPED && |
863 matches_.front().inline_autocomplete_offset == string16::npos && | 872 matches_.front().inline_autocomplete_offset == string16::npos && |
864 matches_.front().fill_into_edit != input_.text(); | 873 matches_.front().fill_into_edit != input_.text(); |
865 } | 874 } |
866 | 875 |
867 void SearchProvider::UpdateMatches() { | 876 void SearchProvider::UpdateMatches() { |
868 ConvertResultsToAutocompleteMatches(); | 877 ConvertResultsToAutocompleteMatches(); |
869 | 878 |
| 879 const int keyword_search_what_you_typed_relevance = |
| 880 providers_.keyword_provider().empty() ? 0 : |
| 881 KeywordProvider::CalculateRelevance(input_.type(), true, true, |
| 882 input_.prefer_keyword(), input_.allow_exact_keyword_match()); |
| 883 |
870 // Check constraints that may be violated by suggested relevances. | 884 // Check constraints that may be violated by suggested relevances. |
871 if (!matches_.empty() && | 885 if (!matches_.empty() && |
872 (has_suggested_relevance_ || verbatim_relevance_ >= 0)) { | 886 (has_suggested_relevance_ || verbatim_relevance_ >= 0)) { |
873 // These two blocks attempt to repair undesriable behavior by suggested | 887 // These two blocks attempt to repair undesriable behavior by suggested |
874 // relevances with minimal impact, preserving other suggested relevances. | 888 // relevances with minimal impact, preserving other suggested relevances. |
875 if (IsTopMatchScoreTooLow()) { | 889 if (IsTopMatchScoreTooLow(keyword_search_what_you_typed_relevance)) { |
876 // Disregard the suggested verbatim relevance if the top score is below | 890 // Disregard the suggested verbatim relevance if the top score is below |
877 // the usual verbatim value. For example, a BarProvider may rely on | 891 // the usual verbatim value. For example, a BarProvider may rely on |
878 // SearchProvider's verbatim or inlineable matches for input "foo" to | 892 // SearchProvider's verbatim or inlineable matches for input "foo" to |
879 // always outrank its own lowly-ranked non-inlineable "bar" match. | 893 // always outrank its own lowly-ranked non-inlineable "bar" match. |
880 verbatim_relevance_ = -1; | 894 verbatim_relevance_ = -1; |
881 ConvertResultsToAutocompleteMatches(); | 895 ConvertResultsToAutocompleteMatches(); |
882 } | 896 } |
883 if (IsTopMatchHighRankSearchForURL()) { | 897 if (IsTopMatchHighRankSearchForURL( |
| 898 keyword_search_what_you_typed_relevance)) { |
884 // Disregard the suggested search and verbatim relevances if the input | 899 // Disregard the suggested search and verbatim relevances if the input |
885 // type is URL and the top match is a highly-ranked search suggestion. | 900 // type is URL and the top match is a highly-ranked search suggestion. |
886 // For example, prevent a search for "foo.com" from outranking another | 901 // For example, prevent a search for "foo.com" from outranking another |
887 // provider's navigation for "foo.com" or "foo.com/url_from_history". | 902 // provider's navigation for "foo.com" or "foo.com/url_from_history". |
888 ApplyCalculatedSuggestRelevance(&keyword_suggest_results_, true); | 903 ApplyCalculatedSuggestRelevance(&keyword_suggest_results_, true); |
889 ApplyCalculatedSuggestRelevance(&default_suggest_results_, false); | 904 ApplyCalculatedSuggestRelevance(&default_suggest_results_, false); |
890 verbatim_relevance_ = -1; | 905 verbatim_relevance_ = -1; |
891 ConvertResultsToAutocompleteMatches(); | 906 ConvertResultsToAutocompleteMatches(); |
892 } | 907 } |
893 if (IsTopMatchNotInlinable()) { | 908 if (IsTopMatchNotInlinable(keyword_search_what_you_typed_relevance)) { |
894 // Disregard suggested relevances if the top match is not SWYT, inlinable, | 909 // Disregard suggested relevances if the top match is not SWYT, inlinable, |
895 // or URL_WHAT_YOU_TYPED (which may be top match regardless of inlining). | 910 // or URL_WHAT_YOU_TYPED (which may be top match regardless of inlining). |
896 // For example, input "foo" should not invoke a search for "bar", which | 911 // For example, input "foo" should not invoke a search for "bar", which |
897 // would happen if the "bar" search match outranked all other matches. | 912 // would happen if the "bar" search match outranked all other matches. |
898 ApplyCalculatedRelevance(); | 913 ApplyCalculatedRelevance(); |
899 ConvertResultsToAutocompleteMatches(); | 914 ConvertResultsToAutocompleteMatches(); |
900 } | 915 } |
901 DCHECK(!IsTopMatchScoreTooLow()); | 916 DCHECK(!IsTopMatchScoreTooLow(keyword_search_what_you_typed_relevance)); |
902 DCHECK(!IsTopMatchHighRankSearchForURL()); | 917 DCHECK(!IsTopMatchHighRankSearchForURL( |
903 DCHECK(!IsTopMatchNotInlinable()); | 918 keyword_search_what_you_typed_relevance)); |
| 919 DCHECK(!IsTopMatchNotInlinable(keyword_search_what_you_typed_relevance)); |
904 } | 920 } |
905 | 921 |
906 UpdateStarredStateOfMatches(); | 922 UpdateStarredStateOfMatches(); |
907 UpdateDone(); | 923 UpdateDone(); |
908 } | 924 } |
909 | 925 |
910 void SearchProvider::AddNavigationResultsToMatches( | 926 void SearchProvider::AddNavigationResultsToMatches( |
911 const NavigationResults& navigation_results, | 927 const NavigationResults& navigation_results, |
912 bool is_keyword) { | 928 bool is_keyword) { |
913 if (navigation_results.empty()) | 929 if (navigation_results.empty()) |
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1290 } | 1306 } |
1291 | 1307 |
1292 void SearchProvider::UpdateDone() { | 1308 void SearchProvider::UpdateDone() { |
1293 // We're done when the timer isn't running, there are no suggest queries | 1309 // We're done when the timer isn't running, there are no suggest queries |
1294 // pending, and we're not waiting on instant. | 1310 // pending, and we're not waiting on instant. |
1295 done_ = (!timer_.IsRunning() && (suggest_results_pending_ == 0) && | 1311 done_ = (!timer_.IsRunning() && (suggest_results_pending_ == 0) && |
1296 (instant_finalized_ || | 1312 (instant_finalized_ || |
1297 (!chrome::BrowserInstantController::IsInstantEnabled(profile_) && | 1313 (!chrome::BrowserInstantController::IsInstantEnabled(profile_) && |
1298 !chrome::search::IsInstantExtendedAPIEnabled(profile_)))); | 1314 !chrome::search::IsInstantExtendedAPIEnabled(profile_)))); |
1299 } | 1315 } |
OLD | NEW |