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/base64.h" | 10 #include "base/base64.h" |
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
374 void SearchProvider::UpdateMatches() { | 374 void SearchProvider::UpdateMatches() { |
375 ConvertResultsToAutocompleteMatches(); | 375 ConvertResultsToAutocompleteMatches(); |
376 | 376 |
377 // Check constraints that may be violated by suggested relevances. | 377 // Check constraints that may be violated by suggested relevances. |
378 if (!matches_.empty() && | 378 if (!matches_.empty() && |
379 (default_results_.HasServerProvidedScores() || | 379 (default_results_.HasServerProvidedScores() || |
380 keyword_results_.HasServerProvidedScores())) { | 380 keyword_results_.HasServerProvidedScores())) { |
381 // These blocks attempt to repair undesirable behavior by suggested | 381 // These blocks attempt to repair undesirable behavior by suggested |
382 // relevances with minimal impact, preserving other suggested relevances. | 382 // relevances with minimal impact, preserving other suggested relevances. |
383 | 383 |
384 if (!HasKeywordDefaultMatchInKeywordMode()) { | 384 if ((providers_.GetKeywordProviderURL() != NULL) && |
msw
2014/08/15 18:32:00
nit: remove extra parens here and below; "!= NULL"
Mark P
2014/08/15 18:47:08
Prefer to keep all of those, which (slightly) seem
| |
385 (FindTopMatch() == matches_.end())) { | |
385 // In keyword mode, disregard the keyword verbatim suggested relevance | 386 // In keyword mode, disregard the keyword verbatim suggested relevance |
386 // if necessary so there at least one keyword match that's allowed to | 387 // if necessary so there at least one match that's allowed to be the |
msw
2014/08/15 18:32:00
nit: "necessary, so", "there is", and consider: "i
Mark P
2014/08/15 18:47:07
Done.
| |
387 // be the default match. | 388 // default match. |
388 keyword_results_.verbatim_relevance = -1; | 389 keyword_results_.verbatim_relevance = -1; |
389 ConvertResultsToAutocompleteMatches(); | 390 ConvertResultsToAutocompleteMatches(); |
390 } | 391 } |
391 if (IsTopMatchSearchWithURLInput()) { | 392 if (IsTopMatchSearchWithURLInput()) { |
392 // Disregard the suggested search and verbatim relevances if the input | 393 // Disregard the suggested search and verbatim relevances if the input |
393 // type is URL and the top match is a highly-ranked search suggestion. | 394 // type is URL and the top match is a highly-ranked search suggestion. |
394 // For example, prevent a search for "foo.com" from outranking another | 395 // For example, prevent a search for "foo.com" from outranking another |
395 // provider's navigation for "foo.com" or "foo.com/url_from_history". | 396 // provider's navigation for "foo.com" or "foo.com/url_from_history". |
396 ApplyCalculatedSuggestRelevance(&keyword_results_.suggest_results); | 397 ApplyCalculatedSuggestRelevance(&keyword_results_.suggest_results); |
397 ApplyCalculatedSuggestRelevance(&default_results_.suggest_results); | 398 ApplyCalculatedSuggestRelevance(&default_results_.suggest_results); |
398 default_results_.verbatim_relevance = -1; | 399 default_results_.verbatim_relevance = -1; |
399 keyword_results_.verbatim_relevance = -1; | 400 keyword_results_.verbatim_relevance = -1; |
400 ConvertResultsToAutocompleteMatches(); | 401 ConvertResultsToAutocompleteMatches(); |
401 } | 402 } |
402 if (FindTopMatch() == matches_.end()) { | 403 if (FindTopMatch() == matches_.end()) { |
403 // Guarantee that SearchProvider returns a legal default match. (The | 404 // Guarantee that SearchProvider returns a legal default match. (The |
404 // omnibox always needs at least one legal default match, and it relies | 405 // omnibox always needs at least one legal default match, and it relies |
405 // on SearchProvider to always return one.) | 406 // on SearchProvider to always return one.) |
406 ApplyCalculatedRelevance(); | 407 ApplyCalculatedRelevance(); |
407 ConvertResultsToAutocompleteMatches(); | 408 ConvertResultsToAutocompleteMatches(); |
408 } | 409 } |
409 DCHECK(HasKeywordDefaultMatchInKeywordMode()); | |
410 DCHECK(!IsTopMatchSearchWithURLInput()); | 410 DCHECK(!IsTopMatchSearchWithURLInput()); |
411 DCHECK(FindTopMatch() != matches_.end()); | 411 DCHECK(FindTopMatch() != matches_.end()); |
412 } | 412 } |
413 UMA_HISTOGRAM_CUSTOM_COUNTS( | 413 UMA_HISTOGRAM_CUSTOM_COUNTS( |
414 "Omnibox.SearchProviderMatches", matches_.size(), 1, 6, 7); | 414 "Omnibox.SearchProviderMatches", matches_.size(), 1, 6, 7); |
415 | |
416 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); | |
417 if ((keyword_url != NULL) && HasKeywordDefaultMatchInKeywordMode()) { | |
418 // If there is a keyword match that is allowed to be the default match, | |
msw
2014/08/15 18:32:00
I'm a little worried that this might no longer be
msw
2014/08/15 18:38:24
Actually, your changes to CreateSearchSuggestion d
Mark P
2014/08/15 18:47:07
There is a link in the code review description to
msw
2014/08/15 19:06:06
Indeed, but I was looking for the specific test/ca
| |
419 // then prohibit default provider matches from being the default match lest | |
420 // such matches cause the user to break out of keyword mode. | |
421 for (ACMatches::iterator it = matches_.begin(); it != matches_.end(); | |
422 ++it) { | |
423 if (it->keyword != keyword_url->keyword()) | |
424 it->allowed_to_be_default_match = false; | |
425 } | |
426 } | |
427 UpdateDone(); | 415 UpdateDone(); |
428 } | 416 } |
429 | 417 |
430 void SearchProvider::Run() { | 418 void SearchProvider::Run() { |
431 // Start a new request with the current input. | 419 // Start a new request with the current input. |
432 suggest_results_pending_ = 0; | 420 suggest_results_pending_ = 0; |
433 time_suggest_request_sent_ = base::TimeTicks::Now(); | 421 time_suggest_request_sent_ = base::TimeTicks::Now(); |
434 | 422 |
435 default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID, | 423 default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID, |
436 providers_.GetDefaultProviderURL(), input_)); | 424 providers_.GetDefaultProviderURL(), input_)); |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
702 keyword_results_.suggest_results.empty() ? | 690 keyword_results_.suggest_results.empty() ? |
703 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : | 691 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
704 TemplateURLRef::NO_SUGGESTION_CHOSEN; | 692 TemplateURLRef::NO_SUGGESTION_CHOSEN; |
705 | 693 |
706 bool relevance_from_server; | 694 bool relevance_from_server; |
707 int verbatim_relevance = GetVerbatimRelevance(&relevance_from_server); | 695 int verbatim_relevance = GetVerbatimRelevance(&relevance_from_server); |
708 int did_not_accept_default_suggestion = | 696 int did_not_accept_default_suggestion = |
709 default_results_.suggest_results.empty() ? | 697 default_results_.suggest_results.empty() ? |
710 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : | 698 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
711 TemplateURLRef::NO_SUGGESTION_CHOSEN; | 699 TemplateURLRef::NO_SUGGESTION_CHOSEN; |
700 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); | |
712 if (verbatim_relevance > 0) { | 701 if (verbatim_relevance > 0) { |
713 const base::string16& trimmed_verbatim = | 702 const base::string16& trimmed_verbatim = |
714 base::CollapseWhitespace(input_.text(), false); | 703 base::CollapseWhitespace(input_.text(), false); |
715 SearchSuggestionParser::SuggestResult verbatim( | 704 SearchSuggestionParser::SuggestResult verbatim( |
716 trimmed_verbatim, AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | 705 trimmed_verbatim, AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
717 trimmed_verbatim, base::string16(), base::string16(), base::string16(), | 706 trimmed_verbatim, base::string16(), base::string16(), base::string16(), |
718 base::string16(), std::string(), std::string(), false, | 707 base::string16(), std::string(), std::string(), false, |
719 verbatim_relevance, relevance_from_server, false, | 708 verbatim_relevance, relevance_from_server, false, |
720 trimmed_verbatim); | 709 trimmed_verbatim); |
721 AddMatchToMap(verbatim, std::string(), did_not_accept_default_suggestion, | 710 AddMatchToMap(verbatim, std::string(), did_not_accept_default_suggestion, |
722 false, &map); | 711 false, keyword_url != NULL, &map); |
msw
2014/08/15 18:32:01
nit: "!= NULL" may not be needed.
Mark P
2014/08/15 18:47:08
Acknowledged.
However, I don't want readers to th
| |
723 } | 712 } |
724 if (!keyword_input_.text().empty()) { | 713 if (!keyword_input_.text().empty()) { |
725 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); | |
726 // We only create the verbatim search query match for a keyword | 714 // We only create the verbatim search query match for a keyword |
727 // if it's not an extension keyword. Extension keywords are handled | 715 // if it's not an extension keyword. Extension keywords are handled |
728 // in KeywordProvider::Start(). (Extensions are complicated...) | 716 // in KeywordProvider::Start(). (Extensions are complicated...) |
729 // Note: in this provider, SEARCH_OTHER_ENGINE must correspond | 717 // Note: in this provider, SEARCH_OTHER_ENGINE must correspond |
730 // to the keyword verbatim search query. Do not create other matches | 718 // to the keyword verbatim search query. Do not create other matches |
731 // of type SEARCH_OTHER_ENGINE. | 719 // of type SEARCH_OTHER_ENGINE. |
732 if (keyword_url && | 720 if (keyword_url && |
733 (keyword_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) { | 721 (keyword_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) { |
734 bool keyword_relevance_from_server; | 722 bool keyword_relevance_from_server; |
735 const int keyword_verbatim_relevance = | 723 const int keyword_verbatim_relevance = |
736 GetKeywordVerbatimRelevance(&keyword_relevance_from_server); | 724 GetKeywordVerbatimRelevance(&keyword_relevance_from_server); |
737 if (keyword_verbatim_relevance > 0) { | 725 if (keyword_verbatim_relevance > 0) { |
738 const base::string16& trimmed_verbatim = | 726 const base::string16& trimmed_verbatim = |
739 base::CollapseWhitespace(keyword_input_.text(), false); | 727 base::CollapseWhitespace(keyword_input_.text(), false); |
740 SearchSuggestionParser::SuggestResult verbatim( | 728 SearchSuggestionParser::SuggestResult verbatim( |
741 trimmed_verbatim, AutocompleteMatchType::SEARCH_OTHER_ENGINE, | 729 trimmed_verbatim, AutocompleteMatchType::SEARCH_OTHER_ENGINE, |
742 trimmed_verbatim, base::string16(), base::string16(), | 730 trimmed_verbatim, base::string16(), base::string16(), |
743 base::string16(), base::string16(), std::string(), std::string(), | 731 base::string16(), base::string16(), std::string(), std::string(), |
744 true, keyword_verbatim_relevance, keyword_relevance_from_server, | 732 true, keyword_verbatim_relevance, keyword_relevance_from_server, |
745 false, trimmed_verbatim); | 733 false, trimmed_verbatim); |
746 AddMatchToMap(verbatim, std::string(), | 734 AddMatchToMap(verbatim, std::string(), |
747 did_not_accept_keyword_suggestion, false, &map); | 735 did_not_accept_keyword_suggestion, false, true, &map); |
748 } | 736 } |
749 } | 737 } |
750 } | 738 } |
751 AddHistoryResultsToMap(keyword_history_results_, true, | 739 AddHistoryResultsToMap(keyword_history_results_, true, |
752 did_not_accept_keyword_suggestion, &map); | 740 did_not_accept_keyword_suggestion, &map); |
753 AddHistoryResultsToMap(default_history_results_, false, | 741 AddHistoryResultsToMap(default_history_results_, false, |
754 did_not_accept_default_suggestion, &map); | 742 did_not_accept_default_suggestion, &map); |
755 | 743 |
756 AddSuggestResultsToMap(keyword_results_.suggest_results, | 744 AddSuggestResultsToMap(keyword_results_.suggest_results, |
757 keyword_results_.metadata, &map); | 745 keyword_results_.metadata, &map); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
805 base::TimeTicks::Now() - start_time); | 793 base::TimeTicks::Now() - start_time); |
806 } | 794 } |
807 | 795 |
808 ACMatches::const_iterator SearchProvider::FindTopMatch() const { | 796 ACMatches::const_iterator SearchProvider::FindTopMatch() const { |
809 ACMatches::const_iterator it = matches_.begin(); | 797 ACMatches::const_iterator it = matches_.begin(); |
810 while ((it != matches_.end()) && !it->allowed_to_be_default_match) | 798 while ((it != matches_.end()) && !it->allowed_to_be_default_match) |
811 ++it; | 799 ++it; |
812 return it; | 800 return it; |
813 } | 801 } |
814 | 802 |
815 bool SearchProvider::HasKeywordDefaultMatchInKeywordMode() const { | |
msw
2014/08/15 18:32:00
Remove the decl in search_provider.h
Mark P
2014/08/15 18:47:07
Done.
| |
816 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); | |
817 // If the user is not in keyword mode, return true to say that this | |
818 // constraint is not violated. | |
819 if (keyword_url == NULL) | |
820 return true; | |
821 for (ACMatches::const_iterator it = matches_.begin(); it != matches_.end(); | |
822 ++it) { | |
823 if ((it->keyword == keyword_url->keyword()) && | |
824 it->allowed_to_be_default_match) | |
825 return true; | |
826 } | |
827 return false; | |
828 } | |
829 | |
830 bool SearchProvider::IsTopMatchSearchWithURLInput() const { | 803 bool SearchProvider::IsTopMatchSearchWithURLInput() const { |
831 ACMatches::const_iterator first_match = FindTopMatch(); | 804 ACMatches::const_iterator first_match = FindTopMatch(); |
832 return (input_.type() == metrics::OmniboxInputType::URL) && | 805 return (input_.type() == metrics::OmniboxInputType::URL) && |
833 (first_match != matches_.end()) && | 806 (first_match != matches_.end()) && |
834 (first_match->relevance > CalculateRelevanceForVerbatim()) && | 807 (first_match->relevance > CalculateRelevanceForVerbatim()) && |
835 (first_match->type != AutocompleteMatchType::NAVSUGGEST) && | 808 (first_match->type != AutocompleteMatchType::NAVSUGGEST) && |
836 (first_match->type != AutocompleteMatchType::NAVSUGGEST_PERSONALIZED); | 809 (first_match->type != AutocompleteMatchType::NAVSUGGEST_PERSONALIZED); |
837 } | 810 } |
838 | 811 |
839 void SearchProvider::AddNavigationResultsToMatches( | 812 void SearchProvider::AddNavigationResultsToMatches( |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
879 if ((scored_results.front().relevance() < 1200) || | 852 if ((scored_results.front().relevance() < 1200) || |
880 !HasMultipleWords(scored_results.front().suggestion())) | 853 !HasMultipleWords(scored_results.front().suggestion())) |
881 scored_results.clear(); // Didn't detect the case above, score normally. | 854 scored_results.clear(); // Didn't detect the case above, score normally. |
882 } | 855 } |
883 if (scored_results.empty()) | 856 if (scored_results.empty()) |
884 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete, | 857 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete, |
885 input_multiple_words, input_text, | 858 input_multiple_words, input_text, |
886 is_keyword); | 859 is_keyword); |
887 for (SearchSuggestionParser::SuggestResults::const_iterator i( | 860 for (SearchSuggestionParser::SuggestResults::const_iterator i( |
888 scored_results.begin()); i != scored_results.end(); ++i) { | 861 scored_results.begin()); i != scored_results.end(); ++i) { |
889 AddMatchToMap(*i, std::string(), did_not_accept_suggestion, true, map); | 862 AddMatchToMap(*i, std::string(), did_not_accept_suggestion, true, |
863 providers_.GetKeywordProviderURL() != NULL, map); | |
msw
2014/08/15 18:32:01
nit: "!= NULL" may not be needed.
Mark P
2014/08/15 18:47:07
same reply as above.
| |
890 } | 864 } |
891 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.AddHistoryResultsTime", | 865 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.AddHistoryResultsTime", |
892 base::TimeTicks::Now() - start_time); | 866 base::TimeTicks::Now() - start_time); |
893 } | 867 } |
894 | 868 |
895 SearchSuggestionParser::SuggestResults SearchProvider::ScoreHistoryResults( | 869 SearchSuggestionParser::SuggestResults SearchProvider::ScoreHistoryResults( |
896 const HistoryResults& results, | 870 const HistoryResults& results, |
897 bool base_prevent_inline_autocomplete, | 871 bool base_prevent_inline_autocomplete, |
898 bool input_multiple_words, | 872 bool input_multiple_words, |
899 const base::string16& input_text, | 873 const base::string16& input_text, |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1001 last_relevance = i->relevance(); | 975 last_relevance = i->relevance(); |
1002 } | 976 } |
1003 | 977 |
1004 return scored_results; | 978 return scored_results; |
1005 } | 979 } |
1006 | 980 |
1007 void SearchProvider::AddSuggestResultsToMap( | 981 void SearchProvider::AddSuggestResultsToMap( |
1008 const SearchSuggestionParser::SuggestResults& results, | 982 const SearchSuggestionParser::SuggestResults& results, |
1009 const std::string& metadata, | 983 const std::string& metadata, |
1010 MatchMap* map) { | 984 MatchMap* map) { |
1011 for (size_t i = 0; i < results.size(); ++i) | 985 for (size_t i = 0; i < results.size(); ++i) { |
1012 AddMatchToMap(results[i], metadata, i, false, map); | 986 AddMatchToMap(results[i], metadata, i, false, |
987 providers_.GetKeywordProviderURL() != NULL, map); | |
msw
2014/08/15 18:32:00
nit: "!= NULL" may not be needed.
Mark P
2014/08/15 18:47:07
same reply as above
| |
988 } | |
1013 } | 989 } |
1014 | 990 |
1015 int SearchProvider::GetVerbatimRelevance(bool* relevance_from_server) const { | 991 int SearchProvider::GetVerbatimRelevance(bool* relevance_from_server) const { |
1016 // Use the suggested verbatim relevance score if it is non-negative (valid), | 992 // Use the suggested verbatim relevance score if it is non-negative (valid), |
1017 // if inline autocomplete isn't prevented (always show verbatim on backspace), | 993 // if inline autocomplete isn't prevented (always show verbatim on backspace), |
1018 // and if it won't suppress verbatim, leaving no default provider matches. | 994 // and if it won't suppress verbatim, leaving no default provider matches. |
1019 // Otherwise, if the default provider returned no matches and was still able | 995 // Otherwise, if the default provider returned no matches and was still able |
1020 // to suppress verbatim, the user would have no search/nav matches and may be | 996 // to suppress verbatim, the user would have no search/nav matches and may be |
1021 // left unable to search using their default provider from the omnibox. | 997 // left unable to search using their default provider from the omnibox. |
1022 // Check for results on each verbatim calculation, as results from older | 998 // Check for results on each verbatim calculation, as results from older |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1241 last_answer_seen_.query_type = match->answer_type; | 1217 last_answer_seen_.query_type = match->answer_type; |
1242 } | 1218 } |
1243 | 1219 |
1244 void SearchProvider::DoAnswersQuery(const AutocompleteInput& input) { | 1220 void SearchProvider::DoAnswersQuery(const AutocompleteInput& input) { |
1245 // If the query text starts with trimmed input, this is valid prefetch data. | 1221 // If the query text starts with trimmed input, this is valid prefetch data. |
1246 prefetch_data_ = StartsWith(last_answer_seen_.full_query_text, | 1222 prefetch_data_ = StartsWith(last_answer_seen_.full_query_text, |
1247 base::CollapseWhitespace(input.text(), false), | 1223 base::CollapseWhitespace(input.text(), false), |
1248 false) ? | 1224 false) ? |
1249 last_answer_seen_ : AnswersQueryData(); | 1225 last_answer_seen_ : AnswersQueryData(); |
1250 } | 1226 } |
OLD | NEW |