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

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

Issue 320713002: Revert of Omnibox: Combine Two Input Type Enums into One (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 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/base64.h" 10 #include "base/base64.h"
(...skipping 21 matching lines...) Expand all
32 #include "chrome/browser/metrics/variations/variations_http_header_provider.h" 32 #include "chrome/browser/metrics/variations/variations_http_header_provider.h"
33 #include "chrome/browser/omnibox/omnibox_field_trial.h" 33 #include "chrome/browser/omnibox/omnibox_field_trial.h"
34 #include "chrome/browser/profiles/profile.h" 34 #include "chrome/browser/profiles/profile.h"
35 #include "chrome/browser/search/search.h" 35 #include "chrome/browser/search/search.h"
36 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" 36 #include "chrome/browser/search_engines/template_url_prepopulate_data.h"
37 #include "chrome/browser/search_engines/template_url_service.h" 37 #include "chrome/browser/search_engines/template_url_service.h"
38 #include "chrome/browser/search_engines/template_url_service_factory.h" 38 #include "chrome/browser/search_engines/template_url_service_factory.h"
39 #include "chrome/browser/ui/search/instant_controller.h" 39 #include "chrome/browser/ui/search/instant_controller.h"
40 #include "chrome/common/chrome_switches.h" 40 #include "chrome/common/chrome_switches.h"
41 #include "chrome/common/pref_names.h" 41 #include "chrome/common/pref_names.h"
42 #include "components/metrics/proto/omnibox_input_type.pb.h"
43 #include "content/public/browser/user_metrics.h" 42 #include "content/public/browser/user_metrics.h"
44 #include "grit/generated_resources.h" 43 #include "grit/generated_resources.h"
45 #include "net/base/escape.h" 44 #include "net/base/escape.h"
46 #include "net/base/load_flags.h" 45 #include "net/base/load_flags.h"
47 #include "net/base/net_util.h" 46 #include "net/base/net_util.h"
48 #include "net/http/http_request_headers.h" 47 #include "net/http/http_request_headers.h"
49 #include "net/url_request/url_fetcher.h" 48 #include "net/url_request/url_fetcher.h"
50 #include "net/url_request/url_request_status.h" 49 #include "net/url_request/url_request_status.h"
51 #include "ui/base/l10n/l10n_util.h" 50 #include "ui/base/l10n/l10n_util.h"
52 #include "url/url_constants.h" 51 #include "url/url_constants.h"
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 // scores verbatim query matches for extension keywords, as well as 169 // scores verbatim query matches for extension keywords, as well as
171 // for keyword matches (i.e., suggestions of a keyword itself, not a 170 // for keyword matches (i.e., suggestions of a keyword itself, not a
172 // suggestion of a query on a keyword search engine). These two 171 // suggestion of a query on a keyword search engine). These two
173 // functions are currently in sync, but there's no reason we 172 // functions are currently in sync, but there's no reason we
174 // couldn't decide in the future to score verbatim matches 173 // couldn't decide in the future to score verbatim matches
175 // differently for extension and non-extension keywords. If you 174 // differently for extension and non-extension keywords. If you
176 // make such a change, however, you should update this comment to 175 // make such a change, however, you should update this comment to
177 // describe it, so it's clear why the functions diverge. 176 // describe it, so it's clear why the functions diverge.
178 if (prefer_keyword) 177 if (prefer_keyword)
179 return 1500; 178 return 1500;
180 return (type == metrics::OmniboxInputType::QUERY) ? 1450 : 1100; 179 return (type == AutocompleteInput::QUERY) ? 1450 : 1100;
181 } 180 }
182 181
183 void SearchProvider::Start(const AutocompleteInput& input, 182 void SearchProvider::Start(const AutocompleteInput& input,
184 bool minimal_changes) { 183 bool minimal_changes) {
185 // Do our best to load the model as early as possible. This will reduce 184 // Do our best to load the model as early as possible. This will reduce
186 // odds of having the model not ready when really needed (a non-empty input). 185 // odds of having the model not ready when really needed (a non-empty input).
187 TemplateURLService* model = providers_.template_url_service(); 186 TemplateURLService* model = providers_.template_url_service();
188 DCHECK(model); 187 DCHECK(model);
189 model->Load(); 188 model->Load();
190 189
191 matches_.clear(); 190 matches_.clear();
192 field_trial_triggered_ = false; 191 field_trial_triggered_ = false;
193 192
194 // Can't return search/suggest results for bogus input or without a profile. 193 // Can't return search/suggest results for bogus input or without a profile.
195 if (!profile_ || (input.type() == metrics::OmniboxInputType::INVALID)) { 194 if (!profile_ || (input.type() == AutocompleteInput::INVALID)) {
196 Stop(true); 195 Stop(true);
197 return; 196 return;
198 } 197 }
199 198
200 keyword_input_ = input; 199 keyword_input_ = input;
201 const TemplateURL* keyword_provider = 200 const TemplateURL* keyword_provider =
202 KeywordProvider::GetSubstitutingTemplateURLForInput(model, 201 KeywordProvider::GetSubstitutingTemplateURLForInput(model,
203 &keyword_input_); 202 &keyword_input_);
204 if (keyword_provider == NULL) 203 if (keyword_provider == NULL)
205 keyword_input_.Clear(); 204 keyword_input_.Clear();
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
543 ((!default_url || default_url->suggestions_url().empty()) && 542 ((!default_url || default_url->suggestions_url().empty()) &&
544 (!keyword_url || keyword_url->suggestions_url().empty())) || 543 (!keyword_url || keyword_url->suggestions_url().empty())) ||
545 !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled)) 544 !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled))
546 return false; 545 return false;
547 546
548 // If the input type might be a URL, we take extra care so that private data 547 // If the input type might be a URL, we take extra care so that private data
549 // isn't sent to the server. 548 // isn't sent to the server.
550 549
551 // FORCED_QUERY means the user is explicitly asking us to search for this, so 550 // FORCED_QUERY means the user is explicitly asking us to search for this, so
552 // we assume it isn't a URL and/or there isn't private data. 551 // we assume it isn't a URL and/or there isn't private data.
553 if (input_.type() == metrics::OmniboxInputType::FORCED_QUERY) 552 if (input_.type() == AutocompleteInput::FORCED_QUERY)
554 return true; 553 return true;
555 554
556 // Next we check the scheme. If this is UNKNOWN/URL with a scheme that isn't 555 // Next we check the scheme. If this is UNKNOWN/URL with a scheme that isn't
557 // http/https/ftp, we shouldn't send it. Sending things like file: and data: 556 // http/https/ftp, we shouldn't send it. Sending things like file: and data:
558 // is both a waste of time and a disclosure of potentially private, local 557 // is both a waste of time and a disclosure of potentially private, local
559 // data. Other "schemes" may actually be usernames, and we don't want to send 558 // data. Other "schemes" may actually be usernames, and we don't want to send
560 // passwords. If the scheme is OK, we still need to check other cases below. 559 // passwords. If the scheme is OK, we still need to check other cases below.
561 // If this is QUERY, then the presence of these schemes means the user 560 // If this is QUERY, then the presence of these schemes means the user
562 // explicitly typed one, and thus this is probably a URL that's being entered 561 // explicitly typed one, and thus this is probably a URL that's being entered
563 // and happens to currently be invalid -- in which case we again want to run 562 // and happens to currently be invalid -- in which case we again want to run
564 // our checks below. Other QUERY cases are less likely to be URLs and thus we 563 // our checks below. Other QUERY cases are less likely to be URLs and thus we
565 // assume we're OK. 564 // assume we're OK.
566 if (!LowerCaseEqualsASCII(input_.scheme(), url::kHttpScheme) && 565 if (!LowerCaseEqualsASCII(input_.scheme(), url::kHttpScheme) &&
567 !LowerCaseEqualsASCII(input_.scheme(), url::kHttpsScheme) && 566 !LowerCaseEqualsASCII(input_.scheme(), url::kHttpsScheme) &&
568 !LowerCaseEqualsASCII(input_.scheme(), url::kFtpScheme)) 567 !LowerCaseEqualsASCII(input_.scheme(), url::kFtpScheme))
569 return (input_.type() == metrics::OmniboxInputType::QUERY); 568 return (input_.type() == AutocompleteInput::QUERY);
570 569
571 // Don't send URLs with usernames, queries or refs. Some of these are 570 // Don't send URLs with usernames, queries or refs. Some of these are
572 // private, and the Suggest server is unlikely to have any useful results 571 // private, and the Suggest server is unlikely to have any useful results
573 // for any of them. Also don't send URLs with ports, as we may initially 572 // for any of them. Also don't send URLs with ports, as we may initially
574 // think that a username + password is a host + port (and we don't want to 573 // think that a username + password is a host + port (and we don't want to
575 // send usernames/passwords), and even if the port really is a port, the 574 // send usernames/passwords), and even if the port really is a port, the
576 // server is once again unlikely to have and useful results. 575 // server is once again unlikely to have and useful results.
577 // Note that we only block based on refs if the input is URL-typed, as search 576 // Note that we only block based on refs if the input is URL-typed, as search
578 // queries can legitimately have #s in them which the URL parser 577 // queries can legitimately have #s in them which the URL parser
579 // overaggressively categorizes as a url with a ref. 578 // overaggressively categorizes as a url with a ref.
580 const url::Parsed& parts = input_.parts(); 579 const url::Parsed& parts = input_.parts();
581 if (parts.username.is_nonempty() || parts.port.is_nonempty() || 580 if (parts.username.is_nonempty() || parts.port.is_nonempty() ||
582 parts.query.is_nonempty() || 581 parts.query.is_nonempty() ||
583 (parts.ref.is_nonempty() && 582 (parts.ref.is_nonempty() && (input_.type() == AutocompleteInput::URL)))
584 (input_.type() == metrics::OmniboxInputType::URL)))
585 return false; 583 return false;
586 584
587 // Don't send anything for https except the hostname. Hostnames are OK 585 // Don't send anything for https except the hostname. Hostnames are OK
588 // because they are visible when the TCP connection is established, but the 586 // because they are visible when the TCP connection is established, but the
589 // specific path may reveal private information. 587 // specific path may reveal private information.
590 if (LowerCaseEqualsASCII(input_.scheme(), url::kHttpsScheme) && 588 if (LowerCaseEqualsASCII(input_.scheme(), url::kHttpsScheme) &&
591 parts.path.is_nonempty()) 589 parts.path.is_nonempty())
592 return false; 590 return false;
593 591
594 return true; 592 return true;
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
807 ++it) { 805 ++it) {
808 if ((it->keyword == keyword_url->keyword()) && 806 if ((it->keyword == keyword_url->keyword()) &&
809 it->allowed_to_be_default_match) 807 it->allowed_to_be_default_match)
810 return true; 808 return true;
811 } 809 }
812 return false; 810 return false;
813 } 811 }
814 812
815 bool SearchProvider::IsTopMatchSearchWithURLInput() const { 813 bool SearchProvider::IsTopMatchSearchWithURLInput() const {
816 ACMatches::const_iterator first_match = FindTopMatch(); 814 ACMatches::const_iterator first_match = FindTopMatch();
817 return (input_.type() == metrics::OmniboxInputType::URL) && 815 return (input_.type() == AutocompleteInput::URL) &&
818 (first_match != matches_.end()) && 816 (first_match != matches_.end()) &&
819 (first_match->relevance > CalculateRelevanceForVerbatim()) && 817 (first_match->relevance > CalculateRelevanceForVerbatim()) &&
820 (first_match->type != AutocompleteMatchType::NAVSUGGEST) && 818 (first_match->type != AutocompleteMatchType::NAVSUGGEST) &&
821 (first_match->type != AutocompleteMatchType::NAVSUGGEST_PERSONALIZED); 819 (first_match->type != AutocompleteMatchType::NAVSUGGEST_PERSONALIZED);
822 } 820 }
823 821
824 void SearchProvider::AddNavigationResultsToMatches( 822 void SearchProvider::AddNavigationResultsToMatches(
825 const NavigationResults& navigation_results, 823 const NavigationResults& navigation_results,
826 ACMatches* matches) { 824 ACMatches* matches) {
827 for (NavigationResults::const_iterator it = navigation_results.begin(); 825 for (NavigationResults::const_iterator it = navigation_results.begin();
828 it != navigation_results.end(); ++it) { 826 it != navigation_results.end(); ++it) {
829 matches->push_back(NavigationToMatch(*it)); 827 matches->push_back(NavigationToMatch(*it));
830 // In the absence of suggested relevance scores, use only the single 828 // In the absence of suggested relevance scores, use only the single
831 // highest-scoring result. (The results are already sorted by relevance.) 829 // highest-scoring result. (The results are already sorted by relevance.)
832 if (!it->relevance_from_server()) 830 if (!it->relevance_from_server())
833 return; 831 return;
834 } 832 }
835 } 833 }
836 834
837 void SearchProvider::AddHistoryResultsToMap(const HistoryResults& results, 835 void SearchProvider::AddHistoryResultsToMap(const HistoryResults& results,
838 bool is_keyword, 836 bool is_keyword,
839 int did_not_accept_suggestion, 837 int did_not_accept_suggestion,
840 MatchMap* map) { 838 MatchMap* map) {
841 if (results.empty()) 839 if (results.empty())
842 return; 840 return;
843 841
844 base::TimeTicks start_time(base::TimeTicks::Now()); 842 base::TimeTicks start_time(base::TimeTicks::Now());
845 bool prevent_inline_autocomplete = input_.prevent_inline_autocomplete() || 843 bool prevent_inline_autocomplete = input_.prevent_inline_autocomplete() ||
846 (input_.type() == metrics::OmniboxInputType::URL); 844 (input_.type() == AutocompleteInput::URL);
847 const base::string16& input_text = 845 const base::string16& input_text =
848 is_keyword ? keyword_input_.text() : input_.text(); 846 is_keyword ? keyword_input_.text() : input_.text();
849 bool input_multiple_words = HasMultipleWords(input_text); 847 bool input_multiple_words = HasMultipleWords(input_text);
850 848
851 SuggestResults scored_results; 849 SuggestResults scored_results;
852 if (!prevent_inline_autocomplete && input_multiple_words) { 850 if (!prevent_inline_autocomplete && input_multiple_words) {
853 // ScoreHistoryResults() allows autocompletion of multi-word, 1-visit 851 // ScoreHistoryResults() allows autocompletion of multi-word, 1-visit
854 // queries if the input also has multiple words. But if we were already 852 // queries if the input also has multiple words. But if we were already
855 // scoring a multi-word, multi-visit query aggressively, and the current 853 // scoring a multi-word, multi-visit query aggressively, and the current
856 // input is still a prefix of it, then changing the suggestion suddenly 854 // input is still a prefix of it, then changing the suggestion suddenly
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
982 980
983 int SearchProvider::CalculateRelevanceForVerbatim() const { 981 int SearchProvider::CalculateRelevanceForVerbatim() const {
984 if (!providers_.keyword_provider().empty()) 982 if (!providers_.keyword_provider().empty())
985 return 250; 983 return 250;
986 return CalculateRelevanceForVerbatimIgnoringKeywordModeState(); 984 return CalculateRelevanceForVerbatimIgnoringKeywordModeState();
987 } 985 }
988 986
989 int SearchProvider:: 987 int SearchProvider::
990 CalculateRelevanceForVerbatimIgnoringKeywordModeState() const { 988 CalculateRelevanceForVerbatimIgnoringKeywordModeState() const {
991 switch (input_.type()) { 989 switch (input_.type()) {
992 case metrics::OmniboxInputType::UNKNOWN: 990 case AutocompleteInput::UNKNOWN:
993 case metrics::OmniboxInputType::QUERY: 991 case AutocompleteInput::QUERY:
994 case metrics::OmniboxInputType::FORCED_QUERY: 992 case AutocompleteInput::FORCED_QUERY:
995 return kNonURLVerbatimRelevance; 993 return kNonURLVerbatimRelevance;
996 994
997 case metrics::OmniboxInputType::URL: 995 case AutocompleteInput::URL:
998 return 850; 996 return 850;
999 997
1000 default: 998 default:
1001 NOTREACHED(); 999 NOTREACHED();
1002 return 0; 1000 return 0;
1003 } 1001 }
1004 } 1002 }
1005 1003
1006 int SearchProvider::GetKeywordVerbatimRelevance( 1004 int SearchProvider::GetKeywordVerbatimRelevance(
1007 bool* relevance_from_server) const { 1005 bool* relevance_from_server) const {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1054 elapsed_time -= autocomplete_time; 1052 elapsed_time -= autocomplete_time;
1055 } 1053 }
1056 1054
1057 const int score_discount = 1055 const int score_discount =
1058 static_cast<int>(6.5 * std::pow(elapsed_time, 0.3)); 1056 static_cast<int>(6.5 * std::pow(elapsed_time, 0.3));
1059 1057
1060 // Don't let scores go below 0. Negative relevance scores are meaningful in 1058 // Don't let scores go below 0. Negative relevance scores are meaningful in
1061 // a different way. 1059 // a different way.
1062 int base_score; 1060 int base_score;
1063 if (is_primary_provider) 1061 if (is_primary_provider)
1064 base_score = (input_.type() == metrics::OmniboxInputType::URL) ? 750 : 1050; 1062 base_score = (input_.type() == AutocompleteInput::URL) ? 750 : 1050;
1065 else 1063 else
1066 base_score = 200; 1064 base_score = 200;
1067 return std::max(0, base_score - score_discount); 1065 return std::max(0, base_score - score_discount);
1068 } 1066 }
1069 1067
1070 AutocompleteMatch SearchProvider::NavigationToMatch( 1068 AutocompleteMatch SearchProvider::NavigationToMatch(
1071 const NavigationResult& navigation) { 1069 const NavigationResult& navigation) {
1072 base::string16 input; 1070 base::string16 input;
1073 const bool trimmed_whitespace = base::TrimWhitespace( 1071 const bool trimmed_whitespace = base::TrimWhitespace(
1074 navigation.from_keyword_provider() ? 1072 navigation.from_keyword_provider() ?
(...skipping 19 matching lines...) Expand all
1094 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); 1092 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
1095 size_t inline_autocomplete_offset = (prefix == NULL) ? 1093 size_t inline_autocomplete_offset = (prefix == NULL) ?
1096 base::string16::npos : (match_start + input.length()); 1094 base::string16::npos : (match_start + input.length());
1097 match.fill_into_edit += 1095 match.fill_into_edit +=
1098 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(), 1096 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(),
1099 net::FormatUrl(navigation.url(), languages, format_types, 1097 net::FormatUrl(navigation.url(), languages, format_types,
1100 net::UnescapeRule::SPACES, NULL, NULL, 1098 net::UnescapeRule::SPACES, NULL, NULL,
1101 &inline_autocomplete_offset)); 1099 &inline_autocomplete_offset));
1102 // Preserve the forced query '?' prefix in |match.fill_into_edit|. 1100 // Preserve the forced query '?' prefix in |match.fill_into_edit|.
1103 // Otherwise, user edits to a suggestion would show non-Search results. 1101 // Otherwise, user edits to a suggestion would show non-Search results.
1104 if (input_.type() == metrics::OmniboxInputType::FORCED_QUERY) { 1102 if (input_.type() == AutocompleteInput::FORCED_QUERY) {
1105 match.fill_into_edit.insert(0, base::ASCIIToUTF16("?")); 1103 match.fill_into_edit.insert(0, base::ASCIIToUTF16("?"));
1106 if (inline_autocomplete_offset != base::string16::npos) 1104 if (inline_autocomplete_offset != base::string16::npos)
1107 ++inline_autocomplete_offset; 1105 ++inline_autocomplete_offset;
1108 } 1106 }
1109 if (inline_autocomplete_offset != base::string16::npos) { 1107 if (inline_autocomplete_offset != base::string16::npos) {
1110 DCHECK(inline_autocomplete_offset <= match.fill_into_edit.length()); 1108 DCHECK(inline_autocomplete_offset <= match.fill_into_edit.length());
1111 match.inline_autocompletion = 1109 match.inline_autocompletion =
1112 match.fill_into_edit.substr(inline_autocomplete_offset); 1110 match.fill_into_edit.substr(inline_autocomplete_offset);
1113 } 1111 }
1114 // An inlineable navsuggestion can only be the default match when there 1112 // An inlineable navsuggestion can only be the default match when there
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1156 // Make the base64 encoded value URL and filename safe(see RFC 3548). 1154 // Make the base64 encoded value URL and filename safe(see RFC 3548).
1157 std::replace(current_token_.begin(), current_token_.end(), '+', '-'); 1155 std::replace(current_token_.begin(), current_token_.end(), '+', '-');
1158 std::replace(current_token_.begin(), current_token_.end(), '/', '_'); 1156 std::replace(current_token_.begin(), current_token_.end(), '/', '_');
1159 } 1157 }
1160 1158
1161 // Extend expiration time another 60 seconds. 1159 // Extend expiration time another 60 seconds.
1162 token_expiration_time_ = current_time + base::TimeDelta::FromSeconds(60); 1160 token_expiration_time_ = current_time + base::TimeDelta::FromSeconds(60);
1163 1161
1164 return current_token_; 1162 return current_token_;
1165 } 1163 }
OLDNEW
« no previous file with comments | « chrome/browser/autocomplete/search_provider.h ('k') | chrome/browser/autocomplete/shortcuts_provider.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698