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
3 // found in the LICENSE file.
5 #include "chrome/browser/autocomplete/search_provider.h"
10 #include "base/callback.h"
11 #include "base/i18n/break_iterator.h"
12 #include "base/i18n/case_conversion.h"
13 #include "base/json/json_string_value_serializer.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/metrics/histogram.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "chrome/browser/autocomplete/autocomplete_classifier.h"
20 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
21 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h"
22 #include "chrome/browser/autocomplete/autocomplete_result.h"
23 #include "chrome/browser/autocomplete/keyword_provider.h"
24 #include "chrome/browser/autocomplete/url_prefix.h"
25 #include "chrome/browser/google/google_util.h"
26 #include "chrome/browser/history/history_service.h"
27 #include "chrome/browser/history/history_service_factory.h"
28 #include "chrome/browser/history/in_memory_database.h"
29 #include "chrome/browser/metrics/variations/variations_http_header_provider.h"
30 #include "chrome/browser/omnibox/omnibox_field_trial.h"
31 #include "chrome/browser/profiles/profile.h"
32 #include "chrome/browser/search/search.h"
33 #include "chrome/browser/search_engines/template_url_prepopulate_data.h"
34 #include "chrome/browser/search_engines/template_url_service.h"
35 #include "chrome/browser/search_engines/template_url_service_factory.h"
36 #include "chrome/browser/ui/search/instant_controller.h"
37 #include "chrome/common/pref_names.h"
38 #include "chrome/common/url_constants.h"
39 #include "content/public/browser/user_metrics.h"
40 #include "grit/generated_resources.h"
41 #include "net/base/escape.h"
42 #include "net/base/load_flags.h"
43 #include "net/base/net_util.h"
44 #include "net/http/http_request_headers.h"
45 #include "net/url_request/url_fetcher.h"
46 #include "net/url_request/url_request_status.h"
47 #include "ui/base/l10n/l10n_util.h"
48 #include "url/url_util.h"
51 // Helpers --------------------------------------------------------------------
55 // We keep track in a histogram how many suggest requests we send, how
56 // many suggest requests we invalidate (e.g., due to a user typing
57 // another character), and how many replies we receive.
58 // *** ADD NEW ENUMS AFTER ALL PREVIOUSLY DEFINED ONES! ***
59 // (excluding the end-of-list enum value)
60 // We do not want values of existing enums to change or else it screws
62 enum SuggestRequestsHistogramValue
{
66 MAX_SUGGEST_REQUEST_HISTOGRAM_VALUE
69 // The verbatim score for an input which is not an URL.
70 const int kNonURLVerbatimRelevance
= 1300;
72 // Increments the appropriate value in the histogram by one.
73 void LogOmniboxSuggestRequest(
74 SuggestRequestsHistogramValue request_value
) {
75 UMA_HISTOGRAM_ENUMERATION("Omnibox.SuggestRequests", request_value
,
76 MAX_SUGGEST_REQUEST_HISTOGRAM_VALUE
);
79 bool HasMultipleWords(const base::string16
& text
) {
80 base::i18n::BreakIterator
i(text
, base::i18n::BreakIterator::BREAK_WORD
);
81 bool found_word
= false;
96 // SearchProvider::Providers --------------------------------------------------
98 SearchProvider::Providers::Providers(TemplateURLService
* template_url_service
)
99 : template_url_service_(template_url_service
) {}
101 const TemplateURL
* SearchProvider::Providers::GetDefaultProviderURL() const {
102 return default_provider_
.empty() ? NULL
:
103 template_url_service_
->GetTemplateURLForKeyword(default_provider_
);
106 const TemplateURL
* SearchProvider::Providers::GetKeywordProviderURL() const {
107 return keyword_provider_
.empty() ? NULL
:
108 template_url_service_
->GetTemplateURLForKeyword(keyword_provider_
);
112 // SearchProvider::CompareScoredResults ---------------------------------------
114 class SearchProvider::CompareScoredResults
{
116 bool operator()(const Result
& a
, const Result
& b
) {
117 // Sort in descending relevance order.
118 return a
.relevance() > b
.relevance();
123 // SearchProvider -------------------------------------------------------------
126 int SearchProvider::kMinimumTimeBetweenSuggestQueriesMs
= 100;
128 SearchProvider::SearchProvider(AutocompleteProviderListener
* listener
,
130 : BaseSearchProvider(listener
, profile
, AutocompleteProvider::TYPE_SEARCH
),
131 providers_(TemplateURLServiceFactory::GetForProfile(profile
)) {
135 std::string
SearchProvider::GetSuggestMetadata(const AutocompleteMatch
& match
) {
136 return match
.GetAdditionalInfo(kSuggestMetadataKey
);
139 void SearchProvider::ResetSession() {
140 field_trial_triggered_in_session_
= false;
143 SearchProvider::~SearchProvider() {
146 void SearchProvider::UpdateMatchContentsClass(const base::string16
& input_text
,
148 for (SuggestResults::iterator sug_it
= results
->suggest_results
.begin();
149 sug_it
!= results
->suggest_results
.end(); ++sug_it
) {
150 sug_it
->ClassifyMatchContents(false, input_text
);
152 const std::string
languages(
153 profile_
->GetPrefs()->GetString(prefs::kAcceptLanguages
));
154 for (NavigationResults::iterator nav_it
= results
->navigation_results
.begin();
155 nav_it
!= results
->navigation_results
.end(); ++nav_it
) {
156 nav_it
->CalculateAndClassifyMatchContents(false, input_text
, languages
);
161 int SearchProvider::CalculateRelevanceForKeywordVerbatim(
162 AutocompleteInput::Type type
,
163 bool prefer_keyword
) {
164 // This function is responsible for scoring verbatim query matches
165 // for non-extension keywords. KeywordProvider::CalculateRelevance()
166 // scores verbatim query matches for extension keywords, as well as
167 // for keyword matches (i.e., suggestions of a keyword itself, not a
168 // suggestion of a query on a keyword search engine). These two
169 // functions are currently in sync, but there's no reason we
170 // couldn't decide in the future to score verbatim matches
171 // differently for extension and non-extension keywords. If you
172 // make such a change, however, you should update this comment to
173 // describe it, so it's clear why the functions diverge.
176 return (type
== AutocompleteInput::QUERY
) ? 1450 : 1100;
179 void SearchProvider::Start(const AutocompleteInput
& input
,
180 bool minimal_changes
) {
181 // Do our best to load the model as early as possible. This will reduce
182 // odds of having the model not ready when really needed (a non-empty input).
183 TemplateURLService
* model
= providers_
.template_url_service();
188 field_trial_triggered_
= false;
190 // Can't return search/suggest results for bogus input or without a profile.
191 if (!profile_
|| (input
.type() == AutocompleteInput::INVALID
)) {
196 keyword_input_
= input
;
197 const TemplateURL
* keyword_provider
=
198 KeywordProvider::GetSubstitutingTemplateURLForInput(model
,
200 if (keyword_provider
== NULL
)
201 keyword_input_
.Clear();
202 else if (keyword_input_
.text().empty())
203 keyword_provider
= NULL
;
205 const TemplateURL
* default_provider
= model
->GetDefaultSearchProvider();
206 if (default_provider
&& !default_provider
->SupportsReplacement())
207 default_provider
= NULL
;
209 if (keyword_provider
== default_provider
)
210 default_provider
= NULL
; // No use in querying the same provider twice.
212 if (!default_provider
&& !keyword_provider
) {
213 // No valid providers.
218 // If we're still running an old query but have since changed the query text
219 // or the providers, abort the query.
220 base::string16
default_provider_keyword(default_provider
?
221 default_provider
->keyword() : base::string16());
222 base::string16
keyword_provider_keyword(keyword_provider
?
223 keyword_provider
->keyword() : base::string16());
224 if (!minimal_changes
||
225 !providers_
.equal(default_provider_keyword
, keyword_provider_keyword
)) {
226 // Cancel any in-flight suggest requests.
231 providers_
.set(default_provider_keyword
, keyword_provider_keyword
);
233 if (input
.text().empty()) {
234 // User typed "?" alone. Give them a placeholder result indicating what
236 if (default_provider
) {
237 AutocompleteMatch match
;
238 match
.provider
= this;
239 match
.contents
.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE
));
240 match
.contents_class
.push_back(
241 ACMatchClassification(0, ACMatchClassification::NONE
));
242 match
.keyword
= providers_
.default_provider();
243 match
.allowed_to_be_default_match
= true;
244 matches_
.push_back(match
);
252 DoHistoryQuery(minimal_changes
);
253 StartOrStopSuggestQuery(minimal_changes
);
257 void SearchProvider::SortResults(bool is_keyword
,
258 const base::ListValue
* relevances
,
260 // Ignore suggested scores for non-keyword matches in keyword mode; if the
261 // server is allowed to score these, it could interfere with the user's
262 // ability to get good keyword results.
263 const bool abandon_suggested_scores
=
264 !is_keyword
&& !providers_
.keyword_provider().empty();
265 // Apply calculated relevance scores to suggestions if a valid list was
266 // not provided or we're abandoning suggested scores entirely.
267 if ((relevances
== NULL
) || abandon_suggested_scores
) {
268 ApplyCalculatedSuggestRelevance(&results
->suggest_results
);
269 ApplyCalculatedNavigationRelevance(&results
->navigation_results
);
270 // If abandoning scores entirely, also abandon the verbatim score.
271 if (abandon_suggested_scores
)
272 results
->verbatim_relevance
= -1;
275 // Keep the result lists sorted.
276 const CompareScoredResults comparator
= CompareScoredResults();
277 std::stable_sort(results
->suggest_results
.begin(),
278 results
->suggest_results
.end(),
280 std::stable_sort(results
->navigation_results
.begin(),
281 results
->navigation_results
.end(),
285 const TemplateURL
* SearchProvider::GetTemplateURL(bool is_keyword
) const {
286 return is_keyword
? providers_
.GetKeywordProviderURL()
287 : providers_
.GetDefaultProviderURL();
290 const AutocompleteInput
SearchProvider::GetInput(bool is_keyword
) const {
291 return is_keyword
? keyword_input_
: input_
;
294 BaseSearchProvider::Results
* SearchProvider::GetResultsToFill(bool is_keyword
) {
295 return is_keyword
? &keyword_results_
: &default_results_
;
298 bool SearchProvider::ShouldAppendExtraParams(
299 const SuggestResult
& result
) const {
300 return !result
.from_keyword_provider() ||
301 providers_
.default_provider().empty();
304 void SearchProvider::StopSuggest() {
305 // Increment the appropriate field in the histogram by the number of
306 // pending requests that were invalidated.
307 for (int i
= 0; i
< suggest_results_pending_
; ++i
)
308 LogOmniboxSuggestRequest(REQUEST_INVALIDATED
);
309 suggest_results_pending_
= 0;
311 // Stop any in-progress URL fetches.
312 keyword_fetcher_
.reset();
313 default_fetcher_
.reset();
316 void SearchProvider::ClearAllResults() {
317 keyword_results_
.Clear();
318 default_results_
.Clear();
321 int SearchProvider::GetDefaultResultRelevance() const {
325 void SearchProvider::RecordDeletionResult(bool success
) {
327 content::RecordAction(
328 base::UserMetricsAction("Omnibox.ServerSuggestDelete.Success"));
330 content::RecordAction(
331 base::UserMetricsAction("Omnibox.ServerSuggestDelete.Failure"));
335 void SearchProvider::LogFetchComplete(bool success
, bool is_keyword
) {
336 LogOmniboxSuggestRequest(REPLY_RECEIVED
);
337 // Record response time for suggest requests sent to Google. We care
338 // only about the common case: the Google default provider used in
340 const TemplateURL
* default_url
= providers_
.GetDefaultProviderURL();
341 if (!is_keyword
&& default_url
&&
342 (TemplateURLPrepopulateData::GetEngineType(*default_url
) ==
343 SEARCH_ENGINE_GOOGLE
)) {
344 const base::TimeDelta elapsed_time
=
345 base::TimeTicks::Now() - time_suggest_request_sent_
;
347 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime",
350 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime",
356 bool SearchProvider::IsKeywordFetcher(const net::URLFetcher
* fetcher
) const {
357 return fetcher
== keyword_fetcher_
.get();
360 void SearchProvider::UpdateMatches() {
361 ConvertResultsToAutocompleteMatches();
363 // Check constraints that may be violated by suggested relevances.
364 if (!matches_
.empty() &&
365 (default_results_
.HasServerProvidedScores() ||
366 keyword_results_
.HasServerProvidedScores())) {
367 // These blocks attempt to repair undesirable behavior by suggested
368 // relevances with minimal impact, preserving other suggested relevances.
370 if (!HasKeywordDefaultMatchInKeywordMode()) {
371 // In keyword mode, disregard the keyword verbatim suggested relevance
372 // if necessary so there at least one keyword match that's allowed to
373 // be the default match.
374 keyword_results_
.verbatim_relevance
= -1;
375 ConvertResultsToAutocompleteMatches();
377 if (IsTopMatchSearchWithURLInput()) {
378 // Disregard the suggested search and verbatim relevances if the input
379 // type is URL and the top match is a highly-ranked search suggestion.
380 // For example, prevent a search for "foo.com" from outranking another
381 // provider's navigation for "foo.com" or "foo.com/url_from_history".
382 ApplyCalculatedSuggestRelevance(&keyword_results_
.suggest_results
);
383 ApplyCalculatedSuggestRelevance(&default_results_
.suggest_results
);
384 default_results_
.verbatim_relevance
= -1;
385 keyword_results_
.verbatim_relevance
= -1;
386 ConvertResultsToAutocompleteMatches();
388 if (FindTopMatch() == matches_
.end()) {
389 // Guarantee that SearchProvider returns a legal default match. (The
390 // omnibox always needs at least one legal default match, and it relies
391 // on SearchProvider to always return one.)
392 ApplyCalculatedRelevance();
393 ConvertResultsToAutocompleteMatches();
395 DCHECK(HasKeywordDefaultMatchInKeywordMode());
396 DCHECK(!IsTopMatchSearchWithURLInput());
397 DCHECK(FindTopMatch() != matches_
.end());
399 UMA_HISTOGRAM_CUSTOM_COUNTS(
400 "Omnibox.SearchProviderMatches", matches_
.size(), 1, 6, 7);
402 const TemplateURL
* keyword_url
= providers_
.GetKeywordProviderURL();
403 if ((keyword_url
!= NULL
) && HasKeywordDefaultMatchInKeywordMode()) {
404 // If there is a keyword match that is allowed to be the default match,
405 // then prohibit default provider matches from being the default match lest
406 // such matches cause the user to break out of keyword mode.
407 for (ACMatches::iterator it
= matches_
.begin(); it
!= matches_
.end();
409 if (it
->keyword
!= keyword_url
->keyword())
410 it
->allowed_to_be_default_match
= false;
414 base::TimeTicks
update_starred_start_time(base::TimeTicks::Now());
415 UpdateStarredStateOfMatches();
416 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.UpdateStarredTime",
417 base::TimeTicks::Now() - update_starred_start_time
);
421 void SearchProvider::Run() {
422 // Start a new request with the current input.
423 suggest_results_pending_
= 0;
424 time_suggest_request_sent_
= base::TimeTicks::Now();
426 default_fetcher_
.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID
,
427 providers_
.GetDefaultProviderURL(), input_
));
428 keyword_fetcher_
.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID
,
429 providers_
.GetKeywordProviderURL(), keyword_input_
));
431 // Both the above can fail if the providers have been modified or deleted
432 // since the query began.
433 if (suggest_results_pending_
== 0) {
435 // We only need to update the listener if we're actually done.
437 listener_
->OnProviderUpdate(false);
441 void SearchProvider::DoHistoryQuery(bool minimal_changes
) {
442 // The history query results are synchronous, so if minimal_changes is true,
443 // we still have the last results and don't need to do anything.
447 keyword_history_results_
.clear();
448 default_history_results_
.clear();
450 if (OmniboxFieldTrial::SearchHistoryDisable(
451 input_
.current_page_classification()))
454 HistoryService
* const history_service
=
455 HistoryServiceFactory::GetForProfile(profile_
, Profile::EXPLICIT_ACCESS
);
456 history::URLDatabase
* url_db
= history_service
?
457 history_service
->InMemoryDatabase() : NULL
;
461 // Request history for both the keyword and default provider. We grab many
462 // more matches than we'll ultimately clamp to so that if there are several
463 // recent multi-word matches who scores are lowered (see
464 // AddHistoryResultsToMap()), they won't crowd out older, higher-scoring
465 // matches. Note that this doesn't fix the problem entirely, but merely
466 // limits it to cases with a very large number of such multi-word matches; for
467 // now, this seems OK compared with the complexity of a real fix, which would
468 // require multiple searches and tracking of "single- vs. multi-word" in the
470 int num_matches
= kMaxMatches
* 5;
471 const TemplateURL
* default_url
= providers_
.GetDefaultProviderURL();
473 const base::TimeTicks start_time
= base::TimeTicks::Now();
474 url_db
->GetMostRecentKeywordSearchTerms(default_url
->id(), input_
.text(),
475 num_matches
, &default_history_results_
);
477 "Omnibox.SearchProvider.GetMostRecentKeywordTermsDefaultProviderTime",
478 base::TimeTicks::Now() - start_time
);
480 const TemplateURL
* keyword_url
= providers_
.GetKeywordProviderURL();
482 url_db
->GetMostRecentKeywordSearchTerms(keyword_url
->id(),
483 keyword_input_
.text(), num_matches
, &keyword_history_results_
);
487 void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes
) {
488 if (!IsQuerySuitableForSuggest()) {
494 // For the minimal_changes case, if we finished the previous query and still
495 // have its results, or are allowed to keep running it, just do that, rather
496 // than starting a new query.
497 if (minimal_changes
&&
498 (!default_results_
.suggest_results
.empty() ||
499 !default_results_
.navigation_results
.empty() ||
500 !keyword_results_
.suggest_results
.empty() ||
501 !keyword_results_
.navigation_results
.empty() ||
502 (!done_
&& input_
.want_asynchronous_matches())))
505 // We can't keep running any previous query, so halt it.
508 // Remove existing results that cannot inline autocomplete the new input.
509 RemoveAllStaleResults();
511 // Update the content classifications of remaining results so they look good
512 // against the current input.
513 UpdateMatchContentsClass(input_
.text(), &default_results_
);
514 if (!keyword_input_
.text().empty())
515 UpdateMatchContentsClass(keyword_input_
.text(), &keyword_results_
);
517 // We can't start a new query if we're only allowed synchronous results.
518 if (!input_
.want_asynchronous_matches())
521 // To avoid flooding the suggest server, don't send a query until at
522 // least 100 ms since the last query.
523 base::TimeTicks
next_suggest_time(time_suggest_request_sent_
+
524 base::TimeDelta::FromMilliseconds(kMinimumTimeBetweenSuggestQueriesMs
));
525 base::TimeTicks
now(base::TimeTicks::Now());
526 if (now
>= next_suggest_time
) {
530 timer_
.Start(FROM_HERE
, next_suggest_time
- now
, this, &SearchProvider::Run
);
533 bool SearchProvider::IsQuerySuitableForSuggest() const {
534 // Don't run Suggest in incognito mode, if the engine doesn't support it, or
535 // if the user has disabled it.
536 const TemplateURL
* default_url
= providers_
.GetDefaultProviderURL();
537 const TemplateURL
* keyword_url
= providers_
.GetKeywordProviderURL();
538 if (profile_
->IsOffTheRecord() ||
539 ((!default_url
|| default_url
->suggestions_url().empty()) &&
540 (!keyword_url
|| keyword_url
->suggestions_url().empty())) ||
541 !profile_
->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled
))
544 // If the input type might be a URL, we take extra care so that private data
545 // isn't sent to the server.
547 // FORCED_QUERY means the user is explicitly asking us to search for this, so
548 // we assume it isn't a URL and/or there isn't private data.
549 if (input_
.type() == AutocompleteInput::FORCED_QUERY
)
552 // Next we check the scheme. If this is UNKNOWN/URL with a scheme that isn't
553 // http/https/ftp, we shouldn't send it. Sending things like file: and data:
554 // is both a waste of time and a disclosure of potentially private, local
555 // data. Other "schemes" may actually be usernames, and we don't want to send
556 // passwords. If the scheme is OK, we still need to check other cases below.
557 // If this is QUERY, then the presence of these schemes means the user
558 // explicitly typed one, and thus this is probably a URL that's being entered
559 // and happens to currently be invalid -- in which case we again want to run
560 // our checks below. Other QUERY cases are less likely to be URLs and thus we
562 if (!LowerCaseEqualsASCII(input_
.scheme(), content::kHttpScheme
) &&
563 !LowerCaseEqualsASCII(input_
.scheme(), content::kHttpsScheme
) &&
564 !LowerCaseEqualsASCII(input_
.scheme(), content::kFtpScheme
))
565 return (input_
.type() == AutocompleteInput::QUERY
);
567 // Don't send URLs with usernames, queries or refs. Some of these are
568 // private, and the Suggest server is unlikely to have any useful results
569 // for any of them. Also don't send URLs with ports, as we may initially
570 // think that a username + password is a host + port (and we don't want to
571 // send usernames/passwords), and even if the port really is a port, the
572 // server is once again unlikely to have and useful results.
573 // Note that we only block based on refs if the input is URL-typed, as search
574 // queries can legitimately have #s in them which the URL parser
575 // overaggressively categorizes as a url with a ref.
576 const url::Parsed
& parts
= input_
.parts();
577 if (parts
.username
.is_nonempty() || parts
.port
.is_nonempty() ||
578 parts
.query
.is_nonempty() ||
579 (parts
.ref
.is_nonempty() && (input_
.type() == AutocompleteInput::URL
)))
582 // Don't send anything for https except the hostname. Hostnames are OK
583 // because they are visible when the TCP connection is established, but the
584 // specific path may reveal private information.
585 if (LowerCaseEqualsASCII(input_
.scheme(), content::kHttpsScheme
) &&
586 parts
.path
.is_nonempty())
592 void SearchProvider::RemoveAllStaleResults() {
593 if (keyword_input_
.text().empty()) {
594 // User is either in keyword mode with a blank input or out of
595 // keyword mode entirely.
596 keyword_results_
.Clear();
600 void SearchProvider::ApplyCalculatedRelevance() {
601 ApplyCalculatedSuggestRelevance(&keyword_results_
.suggest_results
);
602 ApplyCalculatedSuggestRelevance(&default_results_
.suggest_results
);
603 ApplyCalculatedNavigationRelevance(&keyword_results_
.navigation_results
);
604 ApplyCalculatedNavigationRelevance(&default_results_
.navigation_results
);
605 default_results_
.verbatim_relevance
= -1;
606 keyword_results_
.verbatim_relevance
= -1;
609 void SearchProvider::ApplyCalculatedSuggestRelevance(SuggestResults
* list
) {
610 for (size_t i
= 0; i
< list
->size(); ++i
) {
611 SuggestResult
& result
= (*list
)[i
];
612 result
.set_relevance(
613 result
.CalculateRelevance(input_
, providers_
.has_keyword_provider()) +
614 (list
->size() - i
- 1));
615 result
.set_relevance_from_server(false);
619 void SearchProvider::ApplyCalculatedNavigationRelevance(
620 NavigationResults
* list
) {
621 for (size_t i
= 0; i
< list
->size(); ++i
) {
622 NavigationResult
& result
= (*list
)[i
];
623 result
.set_relevance(
624 result
.CalculateRelevance(input_
, providers_
.has_keyword_provider()) +
625 (list
->size() - i
- 1));
626 result
.set_relevance_from_server(false);
630 net::URLFetcher
* SearchProvider::CreateSuggestFetcher(
632 const TemplateURL
* template_url
,
633 const AutocompleteInput
& input
) {
634 if (!template_url
|| template_url
->suggestions_url().empty())
637 // Bail if the suggestion URL is invalid with the given replacements.
638 TemplateURLRef::SearchTermsArgs
search_term_args(input
.text());
639 search_term_args
.cursor_position
= input
.cursor_position();
640 search_term_args
.page_classification
= input
.current_page_classification();
641 GURL
suggest_url(template_url
->suggestions_url_ref().ReplaceSearchTerms(
643 if (!suggest_url
.is_valid())
645 // Send the current page URL if user setting and URL requirements are met and
646 // the user is in the field trial.
647 if (CanSendURL(current_page_url_
, suggest_url
, template_url
,
648 input
.current_page_classification(), profile_
) &&
649 OmniboxFieldTrial::InZeroSuggestAfterTypingFieldTrial()) {
650 search_term_args
.current_page_url
= current_page_url_
.spec();
651 // Create the suggest URL again with the current page URL.
652 suggest_url
= GURL(template_url
->suggestions_url_ref().ReplaceSearchTerms(
656 suggest_results_pending_
++;
657 LogOmniboxSuggestRequest(REQUEST_SENT
);
659 net::URLFetcher
* fetcher
=
660 net::URLFetcher::Create(id
, suggest_url
, net::URLFetcher::GET
, this);
661 fetcher
->SetRequestContext(profile_
->GetRequestContext());
662 fetcher
->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES
);
663 // Add Chrome experiment state to the request headers.
664 net::HttpRequestHeaders headers
;
665 chrome_variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders(
666 fetcher
->GetOriginalURL(), profile_
->IsOffTheRecord(), false, &headers
);
667 fetcher
->SetExtraRequestHeaders(headers
.ToString());
672 void SearchProvider::ConvertResultsToAutocompleteMatches() {
673 // Convert all the results to matches and add them to a map, so we can keep
674 // the most relevant match for each result.
675 base::TimeTicks
start_time(base::TimeTicks::Now());
677 const base::Time no_time
;
678 int did_not_accept_keyword_suggestion
=
679 keyword_results_
.suggest_results
.empty() ?
680 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE
:
681 TemplateURLRef::NO_SUGGESTION_CHOSEN
;
683 bool relevance_from_server
;
684 int verbatim_relevance
= GetVerbatimRelevance(&relevance_from_server
);
685 int did_not_accept_default_suggestion
=
686 default_results_
.suggest_results
.empty() ?
687 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE
:
688 TemplateURLRef::NO_SUGGESTION_CHOSEN
;
689 if (verbatim_relevance
> 0) {
690 const base::string16
& trimmed_verbatim
=
691 base::CollapseWhitespace(input_
.text(), false);
692 SuggestResult
verbatim(
693 trimmed_verbatim
, AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
,
694 trimmed_verbatim
, base::string16(), base::string16(), std::string(),
695 std::string(), false, verbatim_relevance
, relevance_from_server
, false,
697 AddMatchToMap(verbatim
, std::string(), did_not_accept_default_suggestion
,
700 if (!keyword_input_
.text().empty()) {
701 const TemplateURL
* keyword_url
= providers_
.GetKeywordProviderURL();
702 // We only create the verbatim search query match for a keyword
703 // if it's not an extension keyword. Extension keywords are handled
704 // in KeywordProvider::Start(). (Extensions are complicated...)
705 // Note: in this provider, SEARCH_OTHER_ENGINE must correspond
706 // to the keyword verbatim search query. Do not create other matches
707 // of type SEARCH_OTHER_ENGINE.
709 (keyword_url
->GetType() != TemplateURL::OMNIBOX_API_EXTENSION
)) {
710 bool keyword_relevance_from_server
;
711 const int keyword_verbatim_relevance
=
712 GetKeywordVerbatimRelevance(&keyword_relevance_from_server
);
713 if (keyword_verbatim_relevance
> 0) {
714 const base::string16
& trimmed_verbatim
=
715 base::CollapseWhitespace(keyword_input_
.text(), false);
716 SuggestResult
verbatim(
717 trimmed_verbatim
, AutocompleteMatchType::SEARCH_OTHER_ENGINE
,
718 trimmed_verbatim
, base::string16(), base::string16(),
719 std::string(), std::string(), true, keyword_verbatim_relevance
,
720 keyword_relevance_from_server
, false, trimmed_verbatim
);
721 AddMatchToMap(verbatim
, std::string(),
722 did_not_accept_keyword_suggestion
, false, &map
);
726 AddHistoryResultsToMap(keyword_history_results_
, true,
727 did_not_accept_keyword_suggestion
, &map
);
728 AddHistoryResultsToMap(default_history_results_
, false,
729 did_not_accept_default_suggestion
, &map
);
731 AddSuggestResultsToMap(keyword_results_
.suggest_results
,
732 keyword_results_
.metadata
, &map
);
733 AddSuggestResultsToMap(default_results_
.suggest_results
,
734 default_results_
.metadata
, &map
);
737 for (MatchMap::const_iterator
i(map
.begin()); i
!= map
.end(); ++i
)
738 matches
.push_back(i
->second
);
740 AddNavigationResultsToMatches(keyword_results_
.navigation_results
, &matches
);
741 AddNavigationResultsToMatches(default_results_
.navigation_results
, &matches
);
743 // Now add the most relevant matches to |matches_|. We take up to kMaxMatches
744 // suggest/navsuggest matches, regardless of origin. If Instant Extended is
745 // enabled and we have server-provided (and thus hopefully more accurate)
746 // scores for some suggestions, we allow more of those, until we reach
747 // AutocompleteResult::kMaxMatches total matches (that is, enough to fill the
750 // We will always return any verbatim matches, no matter how we obtained their
751 // scores, unless we have already accepted AutocompleteResult::kMaxMatches
752 // higher-scoring matches under the conditions above.
753 std::sort(matches
.begin(), matches
.end(), &AutocompleteMatch::MoreRelevant
);
756 size_t num_suggestions
= 0;
757 for (ACMatches::const_iterator
i(matches
.begin());
758 (i
!= matches
.end()) &&
759 (matches_
.size() < AutocompleteResult::kMaxMatches
);
761 // SEARCH_OTHER_ENGINE is only used in the SearchProvider for the keyword
762 // verbatim result, so this condition basically means "if this match is a
763 // suggestion of some sort".
764 if ((i
->type
!= AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
) &&
765 (i
->type
!= AutocompleteMatchType::SEARCH_OTHER_ENGINE
)) {
766 // If we've already hit the limit on non-server-scored suggestions, and
767 // this isn't a server-scored suggestion we can add, skip it.
768 if ((num_suggestions
>= kMaxMatches
) &&
769 (!chrome::IsInstantExtendedAPIEnabled() ||
770 (i
->GetAdditionalInfo(kRelevanceFromServerKey
) != kTrue
))) {
777 matches_
.push_back(*i
);
779 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.ConvertResultsTime",
780 base::TimeTicks::Now() - start_time
);
783 ACMatches::const_iterator
SearchProvider::FindTopMatch() const {
784 ACMatches::const_iterator it
= matches_
.begin();
785 while ((it
!= matches_
.end()) && !it
->allowed_to_be_default_match
)
790 bool SearchProvider::HasKeywordDefaultMatchInKeywordMode() const {
791 const TemplateURL
* keyword_url
= providers_
.GetKeywordProviderURL();
792 // If the user is not in keyword mode, return true to say that this
793 // constraint is not violated.
794 if (keyword_url
== NULL
)
796 for (ACMatches::const_iterator it
= matches_
.begin(); it
!= matches_
.end();
798 if ((it
->keyword
== keyword_url
->keyword()) &&
799 it
->allowed_to_be_default_match
)
805 bool SearchProvider::IsTopMatchSearchWithURLInput() const {
806 ACMatches::const_iterator first_match
= FindTopMatch();
807 return (input_
.type() == AutocompleteInput::URL
) &&
808 (first_match
!= matches_
.end()) &&
809 (first_match
->relevance
> CalculateRelevanceForVerbatim()) &&
810 (first_match
->type
!= AutocompleteMatchType::NAVSUGGEST
);
813 void SearchProvider::AddNavigationResultsToMatches(
814 const NavigationResults
& navigation_results
,
815 ACMatches
* matches
) {
816 for (NavigationResults::const_iterator it
= navigation_results
.begin();
817 it
!= navigation_results
.end(); ++it
) {
818 matches
->push_back(NavigationToMatch(*it
));
819 // In the absence of suggested relevance scores, use only the single
820 // highest-scoring result. (The results are already sorted by relevance.)
821 if (!it
->relevance_from_server())
826 void SearchProvider::AddHistoryResultsToMap(const HistoryResults
& results
,
828 int did_not_accept_suggestion
,
833 base::TimeTicks
start_time(base::TimeTicks::Now());
834 bool prevent_inline_autocomplete
= input_
.prevent_inline_autocomplete() ||
835 (input_
.type() == AutocompleteInput::URL
);
836 const base::string16
& input_text
=
837 is_keyword
? keyword_input_
.text() : input_
.text();
838 bool input_multiple_words
= HasMultipleWords(input_text
);
840 SuggestResults scored_results
;
841 if (!prevent_inline_autocomplete
&& input_multiple_words
) {
842 // ScoreHistoryResults() allows autocompletion of multi-word, 1-visit
843 // queries if the input also has multiple words. But if we were already
844 // scoring a multi-word, multi-visit query aggressively, and the current
845 // input is still a prefix of it, then changing the suggestion suddenly
846 // feels wrong. To detect this case, first score as if only one word has
847 // been typed, then check if the best result came from aggressive search
848 // history scoring. If it did, then just keep that score set. This
849 // 1200 the lowest possible score in CalculateRelevanceForHistory()'s
850 // aggressive-scoring curve.
851 scored_results
= ScoreHistoryResults(results
, prevent_inline_autocomplete
,
852 false, input_text
, is_keyword
);
853 if ((scored_results
.front().relevance() < 1200) ||
854 !HasMultipleWords(scored_results
.front().suggestion()))
855 scored_results
.clear(); // Didn't detect the case above, score normally.
857 if (scored_results
.empty())
858 scored_results
= ScoreHistoryResults(results
, prevent_inline_autocomplete
,
859 input_multiple_words
, input_text
,
861 for (SuggestResults::const_iterator
i(scored_results
.begin());
862 i
!= scored_results
.end(); ++i
) {
863 AddMatchToMap(*i
, std::string(), did_not_accept_suggestion
, true, map
);
865 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.AddHistoryResultsTime",
866 base::TimeTicks::Now() - start_time
);
869 SearchProvider::SuggestResults
SearchProvider::ScoreHistoryResults(
870 const HistoryResults
& results
,
871 bool base_prevent_inline_autocomplete
,
872 bool input_multiple_words
,
873 const base::string16
& input_text
,
875 AutocompleteClassifier
* classifier
=
876 AutocompleteClassifierFactory::GetForProfile(profile_
);
877 SuggestResults scored_results
;
878 const bool prevent_search_history_inlining
=
879 OmniboxFieldTrial::SearchHistoryPreventInlining(
880 input_
.current_page_classification());
881 const base::string16
& trimmed_input
=
882 base::CollapseWhitespace(input_text
, false);
883 for (HistoryResults::const_iterator
i(results
.begin()); i
!= results
.end();
885 const base::string16
& trimmed_suggestion
=
886 base::CollapseWhitespace(i
->term
, false);
888 // Don't autocomplete multi-word queries that have only been seen once
889 // unless the user has typed more than one word.
890 bool prevent_inline_autocomplete
= base_prevent_inline_autocomplete
||
891 (!input_multiple_words
&& (i
->visits
< 2) &&
892 HasMultipleWords(trimmed_suggestion
));
894 // Don't autocomplete search terms that would normally be treated as URLs
895 // when typed. For example, if the user searched for "google.com" and types
896 // "goog", don't autocomplete to the search term "google.com". Otherwise,
897 // the input will look like a URL but act like a search, which is confusing.
898 // NOTE: We don't check this in the following cases:
899 // * When inline autocomplete is disabled, we won't be inline
900 // autocompleting this term, so we don't need to worry about confusion as
901 // much. This also prevents calling Classify() again from inside the
902 // classifier (which will corrupt state and likely crash), since the
903 // classifier always disables inline autocomplete.
904 // * When the user has typed the whole term, the "what you typed" history
905 // match will outrank us for URL-like inputs anyway, so we need not do
907 if (!prevent_inline_autocomplete
&& classifier
&&
908 (trimmed_suggestion
!= trimmed_input
)) {
909 AutocompleteMatch match
;
910 classifier
->Classify(trimmed_suggestion
, false, false,
911 input_
.current_page_classification(), &match
, NULL
);
912 prevent_inline_autocomplete
=
913 !AutocompleteMatch::IsSearchType(match
.type
);
916 int relevance
= CalculateRelevanceForHistory(
917 i
->time
, is_keyword
, !prevent_inline_autocomplete
,
918 prevent_search_history_inlining
);
919 scored_results
.push_back(SuggestResult(
920 trimmed_suggestion
, AutocompleteMatchType::SEARCH_HISTORY
,
921 trimmed_suggestion
, base::string16(), base::string16(), std::string(),
922 std::string(), is_keyword
, relevance
, false, false, trimmed_input
));
925 // History returns results sorted for us. However, we may have docked some
926 // results' scores, so things are no longer in order. Do a stable sort to get
927 // things back in order without otherwise disturbing results with equal
928 // scores, then force the scores to be unique, so that the order in which
929 // they're shown is deterministic.
930 std::stable_sort(scored_results
.begin(), scored_results
.end(),
931 CompareScoredResults());
932 int last_relevance
= 0;
933 for (SuggestResults::iterator
i(scored_results
.begin());
934 i
!= scored_results
.end(); ++i
) {
935 if ((i
!= scored_results
.begin()) && (i
->relevance() >= last_relevance
))
936 i
->set_relevance(last_relevance
- 1);
937 last_relevance
= i
->relevance();
940 return scored_results
;
943 void SearchProvider::AddSuggestResultsToMap(const SuggestResults
& results
,
944 const std::string
& metadata
,
946 for (size_t i
= 0; i
< results
.size(); ++i
)
947 AddMatchToMap(results
[i
], metadata
, i
, false, map
);
950 int SearchProvider::GetVerbatimRelevance(bool* relevance_from_server
) const {
951 // Use the suggested verbatim relevance score if it is non-negative (valid),
952 // if inline autocomplete isn't prevented (always show verbatim on backspace),
953 // and if it won't suppress verbatim, leaving no default provider matches.
954 // Otherwise, if the default provider returned no matches and was still able
955 // to suppress verbatim, the user would have no search/nav matches and may be
956 // left unable to search using their default provider from the omnibox.
957 // Check for results on each verbatim calculation, as results from older
958 // queries (on previous input) may be trimmed for failing to inline new input.
959 bool use_server_relevance
=
960 (default_results_
.verbatim_relevance
>= 0) &&
961 !input_
.prevent_inline_autocomplete() &&
962 ((default_results_
.verbatim_relevance
> 0) ||
963 !default_results_
.suggest_results
.empty() ||
964 !default_results_
.navigation_results
.empty());
965 if (relevance_from_server
)
966 *relevance_from_server
= use_server_relevance
;
967 return use_server_relevance
?
968 default_results_
.verbatim_relevance
: CalculateRelevanceForVerbatim();
971 int SearchProvider::CalculateRelevanceForVerbatim() const {
972 if (!providers_
.keyword_provider().empty())
974 return CalculateRelevanceForVerbatimIgnoringKeywordModeState();
978 CalculateRelevanceForVerbatimIgnoringKeywordModeState() const {
979 switch (input_
.type()) {
980 case AutocompleteInput::UNKNOWN
:
981 case AutocompleteInput::QUERY
:
982 case AutocompleteInput::FORCED_QUERY
:
983 return kNonURLVerbatimRelevance
;
985 case AutocompleteInput::URL
:
994 int SearchProvider::GetKeywordVerbatimRelevance(
995 bool* relevance_from_server
) const {
996 // Use the suggested verbatim relevance score if it is non-negative (valid),
997 // if inline autocomplete isn't prevented (always show verbatim on backspace),
998 // and if it won't suppress verbatim, leaving no keyword provider matches.
999 // Otherwise, if the keyword provider returned no matches and was still able
1000 // to suppress verbatim, the user would have no search/nav matches and may be
1001 // left unable to search using their keyword provider from the omnibox.
1002 // Check for results on each verbatim calculation, as results from older
1003 // queries (on previous input) may be trimmed for failing to inline new input.
1004 bool use_server_relevance
=
1005 (keyword_results_
.verbatim_relevance
>= 0) &&
1006 !input_
.prevent_inline_autocomplete() &&
1007 ((keyword_results_
.verbatim_relevance
> 0) ||
1008 !keyword_results_
.suggest_results
.empty() ||
1009 !keyword_results_
.navigation_results
.empty());
1010 if (relevance_from_server
)
1011 *relevance_from_server
= use_server_relevance
;
1012 return use_server_relevance
?
1013 keyword_results_
.verbatim_relevance
:
1014 CalculateRelevanceForKeywordVerbatim(keyword_input_
.type(),
1015 keyword_input_
.prefer_keyword());
1018 int SearchProvider::CalculateRelevanceForHistory(
1019 const base::Time
& time
,
1021 bool use_aggressive_method
,
1022 bool prevent_search_history_inlining
) const {
1023 // The relevance of past searches falls off over time. There are two distinct
1024 // equations used. If the first equation is used (searches to the primary
1025 // provider that we want to score aggressively), the score is in the range
1026 // 1300-1599 (unless |prevent_search_history_inlining|, in which case
1027 // it's in the range 1200-1299). If the second equation is used the
1028 // relevance of a search 15 minutes ago is discounted 50 points, while the
1029 // relevance of a search two weeks ago is discounted 450 points.
1030 double elapsed_time
= std::max((base::Time::Now() - time
).InSecondsF(), 0.0);
1031 bool is_primary_provider
= is_keyword
|| !providers_
.has_keyword_provider();
1032 if (is_primary_provider
&& use_aggressive_method
) {
1033 // Searches with the past two days get a different curve.
1034 const double autocomplete_time
= 2 * 24 * 60 * 60;
1035 if (elapsed_time
< autocomplete_time
) {
1036 int max_score
= is_keyword
? 1599 : 1399;
1037 if (prevent_search_history_inlining
)
1039 return max_score
- static_cast<int>(99 *
1040 std::pow(elapsed_time
/ autocomplete_time
, 2.5));
1042 elapsed_time
-= autocomplete_time
;
1045 const int score_discount
=
1046 static_cast<int>(6.5 * std::pow(elapsed_time
, 0.3));
1048 // Don't let scores go below 0. Negative relevance scores are meaningful in
1051 if (is_primary_provider
)
1052 base_score
= (input_
.type() == AutocompleteInput::URL
) ? 750 : 1050;
1055 return std::max(0, base_score
- score_discount
);
1058 AutocompleteMatch
SearchProvider::NavigationToMatch(
1059 const NavigationResult
& navigation
) {
1060 base::string16 input
;
1061 const bool trimmed_whitespace
= base::TrimWhitespace(
1062 navigation
.from_keyword_provider() ?
1063 keyword_input_
.text() : input_
.text(),
1064 base::TRIM_TRAILING
, &input
) != base::TRIM_NONE
;
1065 AutocompleteMatch
match(this, navigation
.relevance(), false,
1066 AutocompleteMatchType::NAVSUGGEST
);
1067 match
.destination_url
= navigation
.url();
1069 // First look for the user's input inside the formatted url as it would be
1070 // without trimming the scheme, so we can find matches at the beginning of the
1072 const URLPrefix
* prefix
=
1073 URLPrefix::BestURLPrefix(navigation
.formatted_url(), input
);
1074 size_t match_start
= (prefix
== NULL
) ?
1075 navigation
.formatted_url().find(input
) : prefix
->prefix
.length();
1076 bool trim_http
= !AutocompleteInput::HasHTTPScheme(input
) &&
1077 (!prefix
|| (match_start
!= 0));
1078 const net::FormatUrlTypes format_types
=
1079 net::kFormatUrlOmitAll
& ~(trim_http
? 0 : net::kFormatUrlOmitHTTP
);
1081 const std::string
languages(
1082 profile_
->GetPrefs()->GetString(prefs::kAcceptLanguages
));
1083 size_t inline_autocomplete_offset
= (prefix
== NULL
) ?
1084 base::string16::npos
: (match_start
+ input
.length());
1085 match
.fill_into_edit
+=
1086 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation
.url(),
1087 net::FormatUrl(navigation
.url(), languages
, format_types
,
1088 net::UnescapeRule::SPACES
, NULL
, NULL
,
1089 &inline_autocomplete_offset
));
1090 // Preserve the forced query '?' prefix in |match.fill_into_edit|.
1091 // Otherwise, user edits to a suggestion would show non-Search results.
1092 if (input_
.type() == AutocompleteInput::FORCED_QUERY
) {
1093 match
.fill_into_edit
.insert(0, base::ASCIIToUTF16("?"));
1094 if (inline_autocomplete_offset
!= base::string16::npos
)
1095 ++inline_autocomplete_offset
;
1097 if (inline_autocomplete_offset
!= base::string16::npos
) {
1098 DCHECK(inline_autocomplete_offset
<= match
.fill_into_edit
.length());
1099 match
.inline_autocompletion
=
1100 match
.fill_into_edit
.substr(inline_autocomplete_offset
);
1102 // An inlineable navsuggestion can only be the default match when there
1103 // is no keyword provider active, lest it appear first and break the user
1104 // out of keyword mode. It can also only be default if either the inline
1105 // autocompletion is empty or we're not preventing inline autocompletion.
1106 // Finally, if we have an inlineable navsuggestion with an inline completion
1107 // that we're not preventing, make sure we didn't trim any whitespace.
1108 // We don't want to claim http://foo.com/bar is inlineable against the
1109 // input "foo.com/b ".
1110 match
.allowed_to_be_default_match
= navigation
.IsInlineable(input
) &&
1111 (providers_
.GetKeywordProviderURL() == NULL
) &&
1112 (match
.inline_autocompletion
.empty() ||
1113 (!input_
.prevent_inline_autocomplete() && !trimmed_whitespace
));
1115 match
.contents
= navigation
.match_contents();
1116 match
.contents_class
= navigation
.match_contents_class();
1117 match
.description
= navigation
.description();
1118 AutocompleteMatch::ClassifyMatchInString(input
, match
.description
,
1119 ACMatchClassification::NONE
, &match
.description_class
);
1121 match
.RecordAdditionalInfo(
1122 kRelevanceFromServerKey
,
1123 navigation
.relevance_from_server() ? kTrue
: kFalse
);
1124 match
.RecordAdditionalInfo(kShouldPrefetchKey
, kFalse
);
1129 void SearchProvider::UpdateDone() {
1130 // We're done when the timer isn't running, there are no suggest queries
1131 // pending, and we're not waiting on Instant.
1132 done_
= !timer_
.IsRunning() && (suggest_results_pending_
== 0);