Chromium Code Reviews| Index: chrome/browser/autocomplete/search_provider.cc |
| diff --git a/chrome/browser/autocomplete/search_provider.cc b/chrome/browser/autocomplete/search_provider.cc |
| index f1e02da9162e21b9aee5ff4151960043caa7cd30..b722ab4f4e64d1ebd5b38c7450724f780f75c3e1 100644 |
| --- a/chrome/browser/autocomplete/search_provider.cc |
| +++ b/chrome/browser/autocomplete/search_provider.cc |
| @@ -783,7 +783,6 @@ void SearchProvider::ConvertResultsToAutocompleteMatches() { |
| int did_not_accept_keyword_suggestion = keyword_suggest_results_.empty() ? |
| TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
| TemplateURLRef::NO_SUGGESTION_CHOSEN; |
| - // Keyword what you typed results are handled by the KeywordProvider. |
| int verbatim_relevance = GetVerbatimRelevance(); |
| int did_not_accept_default_suggestion = default_suggest_results_.empty() ? |
| @@ -794,7 +793,23 @@ void SearchProvider::ConvertResultsToAutocompleteMatches() { |
| AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, |
| did_not_accept_default_suggestion, false, &map); |
| } |
| - const size_t what_you_typed_size = map.size(); |
| + if (!keyword_input_text_.empty()) { |
| + const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
| + // We only create the verbatim search query match for a keyword |
| + // if it's not an extension keyword. Extension keywords are handled |
| + // in KeywordProvider::Start(). (Extensions are complicated...) |
| + // Note: in this provider, SEARCH_OTHER_ENGINE must correspond |
| + // to the keyword verbatim search query. Do not create other matches |
| + // of type SEARCH_OTHER_ENGINE. |
| + if (keyword_url && !keyword_url->IsExtensionKeyword()) { |
| + AddMatchToMap(keyword_input_text_, keyword_input_text_, |
| + CalculateRelevanceForKeywordVerbatim( |
| + input_.type(), input_.prefer_keyword()), |
| + AutocompleteMatch::SEARCH_OTHER_ENGINE, |
| + did_not_accept_keyword_suggestion, true, &map); |
| + } |
| + } |
| + const size_t verbatim_matches_size = map.size(); |
| if (!default_provider_suggestion_.text.empty() && |
| default_provider_suggestion_.type == INSTANT_SUGGESTION_SEARCH && |
| !input_.prevent_inline_autocomplete()) |
| @@ -830,8 +845,8 @@ void SearchProvider::ConvertResultsToAutocompleteMatches() { |
| AddNavigationResultsToMatches(keyword_navigation_results_, true); |
| AddNavigationResultsToMatches(default_navigation_results_, false); |
| - // Allow an additional match for "what you typed" if it's present. |
| - const size_t max_total_matches = kMaxMatches + what_you_typed_size; |
| + // Allow additional match(es) for verbatim results if present. |
| + const size_t max_total_matches = kMaxMatches + verbatim_matches_size; |
| std::partial_sort(matches_.begin(), |
| matches_.begin() + std::min(max_total_matches, matches_.size()), |
| matches_.end(), &AutocompleteMatch::MoreRelevant); |
| @@ -865,12 +880,18 @@ bool SearchProvider::IsTopMatchHighRankSearchForURL() const { |
| return input_.type() == AutocompleteInput::URL && |
| matches_.front().relevance > CalculateRelevanceForVerbatim() && |
| (matches_.front().type == AutocompleteMatch::SEARCH_SUGGEST || |
| - matches_.front().type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED); |
| + matches_.front().type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED || |
| + matches_.front().type == AutocompleteMatch::SEARCH_OTHER_ENGINE); |
| } |
| bool SearchProvider::IsTopMatchNotInlinable() const { |
| + // Note: this test assumes the SEARCH_OTHER_ENGINE match corresponds to |
| + // the verbatim search query on the keyword engine. SearchProvider should |
| + // not create any other match of type SEARCH_OTHER_ENGINE. We attempt to |
| + // CHECK this assumption in UpdateMatches(). |
| return matches_.front().type != AutocompleteMatch::SEARCH_WHAT_YOU_TYPED && |
| matches_.front().type != AutocompleteMatch::URL_WHAT_YOU_TYPED && |
| + matches_.front().type != AutocompleteMatch::SEARCH_OTHER_ENGINE && |
| matches_.front().inline_autocomplete_offset == string16::npos && |
| matches_.front().fill_into_edit != input_.text(); |
| } |
| @@ -878,6 +899,37 @@ bool SearchProvider::IsTopMatchNotInlinable() const { |
| void SearchProvider::UpdateMatches() { |
| ConvertResultsToAutocompleteMatches(); |
| +#ifdef DEBUG |
|
msw
2013/02/05 01:35:19
Shoot, I thought you were going to put this into a
Mark P
2013/02/05 19:50:55
Removed.
At least it's now in the code review his
|
| + // Check that SEARCH_OTHER_ENGINE match is actually the verbatim search |
| + // query on the keyword provider. |
| + for (int i = 0; i < matches_.size(); ++i) { |
| + const AutocompleteMatch& m = matches_[i]; |
| + if (m.type == AutocompleteMatch::SEARCH_OTHER_ENGINE) { |
| + // Verify that input_.text() begins with the keyword (possibly |
| + // with an ignorable prefix before it) and ends with the text |
| + // displayed in this match and that the only text in between is |
| + // entirely whitespace. |
| + const URLPrefix* prefix = |
| + URLPrefix::BestURLPrefix(input_.text(), ""); |
| + // There will always be a valid prefix; it may be empty. |
| + DCHECK(prefix); |
| + string16 input_after_prefix = input_.text().substr(prefix.length()); |
| + // case-insensitive for keyword name |
| + DCHECK(base::StartsWith(input_after_prefix, m.keyword, false)); |
| + string16 input_after_keyword = |
| + input_after_prefix.substr(m.keyword.length()); |
| + // case-sensitive match for remaining text |
| + DCHECK(base::EndsWith(input_after_keyword, m.contents, true)); |
| + string16::size_t pos_of_remaining_text = |
| + input_after_keyword.find(m.contents); |
| + DCHECK_NE(pos_of_remaining_text, string16::npos); |
| + DCHECK_GT(pos_of_remaining_text, 0u); |
| + DCHECK(base::ContainsOnlyWhitespace( |
| + input_after_keyword.substr(0, pos_of_remaining_text))); |
| + } |
| + } |
| +#endif |
| + |
| // Check constraints that may be violated by suggested relevances. |
| if (!matches_.empty() && |
| (has_suggested_relevance_ || verbatim_relevance_ >= 0)) { |
| @@ -902,10 +954,11 @@ void SearchProvider::UpdateMatches() { |
| ConvertResultsToAutocompleteMatches(); |
| } |
| if (IsTopMatchNotInlinable()) { |
| - // Disregard suggested relevances if the top match is not SWYT, inlinable, |
| - // or URL_WHAT_YOU_TYPED (which may be top match regardless of inlining). |
| - // For example, input "foo" should not invoke a search for "bar", which |
| - // would happen if the "bar" search match outranked all other matches. |
| + // Disregard suggested relevances if the top match is not a verbatim |
| + // match, inlinable, or URL_WHAT_YOU_TYPED (which may be top match |
| + // regardless of inlining). For example, input "foo" should not |
| + // invoke a search for "bar", which would happen if the "bar" search |
| + // match outranked all other matches. |
| ApplyCalculatedRelevance(); |
| ConvertResultsToAutocompleteMatches(); |
| } |
| @@ -1090,6 +1143,22 @@ int SearchProvider::CalculateRelevanceForVerbatim() const { |
| } |
| } |
| +// static |
| +int SearchProvider::CalculateRelevanceForKeywordVerbatim( |
| + AutocompleteInput::Type type, |
| + bool prefer_keyword) { |
| + // Perhaps this should be kept similar to |
| + // KeywordProvider::CalculateRelevance(). That function calculates, |
| + // among other things, the verbatim query score for search keywords |
| + // but only extension-based ones. It would be a bit odd if the score |
| + // for a verbatim query for an extension keyword and for a |
| + // non-extension keyword differed dramatically (though no immediate |
| + // harm would come from it). |
| + if (prefer_keyword) |
| + return 1500; |
| + return type == AutocompleteInput::QUERY ? 1450 : 1100; |
| +} |
| + |
| int SearchProvider::CalculateRelevanceForHistory( |
| const Time& time, |
| bool is_keyword, |