1 // Copyright (c) 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/autocomplete_controller.h"
10 #include "base/format_macros.h"
11 #include "base/logging.h"
12 #include "base/metrics/histogram.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/time/time.h"
16 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h"
17 #include "chrome/browser/autocomplete/bookmark_provider.h"
18 #include "chrome/browser/autocomplete/builtin_provider.h"
19 #include "chrome/browser/autocomplete/extension_app_provider.h"
20 #include "chrome/browser/autocomplete/history_quick_provider.h"
21 #include "chrome/browser/autocomplete/history_url_provider.h"
22 #include "chrome/browser/autocomplete/keyword_provider.h"
23 #include "chrome/browser/autocomplete/search_provider.h"
24 #include "chrome/browser/autocomplete/shortcuts_provider.h"
25 #include "chrome/browser/autocomplete/zero_suggest_provider.h"
26 #include "chrome/browser/chrome_notification_types.h"
27 #include "chrome/browser/omnibox/omnibox_field_trial.h"
28 #include "chrome/browser/profiles/profile.h"
29 #include "chrome/browser/search/search.h"
30 #include "chrome/browser/search_engines/template_url.h"
31 #include "content/public/browser/notification_service.h"
32 #include "grit/generated_resources.h"
33 #include "grit/theme_resources.h"
34 #include "ui/base/l10n/l10n_util.h"
38 // Converts the given match to a type (and possibly subtype) based on the AQS
39 // specification. For more details, see
40 // http://goto.google.com/binary-clients-logging.
41 void AutocompleteMatchToAssistedQuery(
42 const AutocompleteMatch::Type
& match
, size_t* type
, size_t* subtype
) {
43 // This type indicates a native chrome suggestion.
45 // Default value, indicating no subtype.
46 *subtype
= base::string16::npos
;
49 case AutocompleteMatchType::SEARCH_SUGGEST
: {
53 case AutocompleteMatchType::SEARCH_SUGGEST_ENTITY
: {
57 case AutocompleteMatchType::SEARCH_SUGGEST_INFINITE
: {
61 case AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED
: {
65 case AutocompleteMatchType::SEARCH_SUGGEST_PROFILE
: {
69 case AutocompleteMatchType::NAVSUGGEST
: {
73 case AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
: {
77 case AutocompleteMatchType::URL_WHAT_YOU_TYPED
: {
81 case AutocompleteMatchType::SEARCH_HISTORY
: {
85 case AutocompleteMatchType::HISTORY_URL
: {
89 case AutocompleteMatchType::HISTORY_TITLE
: {
93 case AutocompleteMatchType::HISTORY_BODY
: {
97 case AutocompleteMatchType::HISTORY_KEYWORD
: {
101 case AutocompleteMatchType::BOOKMARK_TITLE
: {
106 // This value indicates a native chrome suggestion with no named subtype
113 // Appends available autocompletion of the given type, subtype, and number to
114 // the existing available autocompletions string, encoding according to the
116 void AppendAvailableAutocompletion(size_t type
,
119 std::string
* autocompletions
) {
120 if (!autocompletions
->empty())
121 autocompletions
->append("j");
122 base::StringAppendF(autocompletions
, "%" PRIuS
, type
);
123 // Subtype is optional - base::string16::npos indicates no subtype.
124 if (subtype
!= base::string16::npos
)
125 base::StringAppendF(autocompletions
, "i%" PRIuS
, subtype
);
127 base::StringAppendF(autocompletions
, "l%d", count
);
130 // Returns whether the autocompletion is trivial enough that we consider it
131 // an autocompletion for which the omnibox autocompletion code did not add
133 bool IsTrivialAutocompletion(const AutocompleteMatch
& match
) {
134 return match
.type
== AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED
||
135 match
.type
== AutocompleteMatchType::URL_WHAT_YOU_TYPED
||
136 match
.type
== AutocompleteMatchType::SEARCH_OTHER_ENGINE
;
139 // Whether this autocomplete match type supports custom descriptions.
140 bool AutocompleteMatchHasCustomDescription(const AutocompleteMatch
& match
) {
141 return match
.type
== AutocompleteMatchType::SEARCH_SUGGEST_ENTITY
||
142 match
.type
== AutocompleteMatchType::SEARCH_SUGGEST_PROFILE
;
147 AutocompleteController::AutocompleteController(
149 AutocompleteControllerDelegate
* delegate
,
151 : delegate_(delegate
),
152 history_url_provider_(NULL
),
153 keyword_provider_(NULL
),
154 search_provider_(NULL
),
155 zero_suggest_provider_(NULL
),
156 stop_timer_duration_(OmniboxFieldTrial::StopTimerFieldTrialDuration()),
160 provider_types
&= ~OmniboxFieldTrial::GetDisabledProviderTypes();
161 if (provider_types
& AutocompleteProvider::TYPE_BOOKMARK
)
162 providers_
.push_back(new BookmarkProvider(this, profile
));
163 if (provider_types
& AutocompleteProvider::TYPE_BUILTIN
)
164 providers_
.push_back(new BuiltinProvider(this, profile
));
165 if (provider_types
& AutocompleteProvider::TYPE_EXTENSION_APP
)
166 providers_
.push_back(new ExtensionAppProvider(this, profile
));
167 if (provider_types
& AutocompleteProvider::TYPE_HISTORY_QUICK
)
168 providers_
.push_back(new HistoryQuickProvider(this, profile
));
169 if (provider_types
& AutocompleteProvider::TYPE_HISTORY_URL
) {
170 history_url_provider_
= new HistoryURLProvider(this, profile
);
171 providers_
.push_back(history_url_provider_
);
173 // "Tab to search" can be used on all platforms other than Android.
174 #if !defined(OS_ANDROID)
175 if (provider_types
& AutocompleteProvider::TYPE_KEYWORD
) {
176 keyword_provider_
= new KeywordProvider(this, profile
);
177 providers_
.push_back(keyword_provider_
);
180 if (provider_types
& AutocompleteProvider::TYPE_SEARCH
) {
181 search_provider_
= new SearchProvider(this, profile
);
182 providers_
.push_back(search_provider_
);
184 if (provider_types
& AutocompleteProvider::TYPE_SHORTCUTS
)
185 providers_
.push_back(new ShortcutsProvider(this, profile
));
186 if (provider_types
& AutocompleteProvider::TYPE_ZERO_SUGGEST
) {
187 zero_suggest_provider_
= ZeroSuggestProvider::Create(this, profile
);
188 if (zero_suggest_provider_
)
189 providers_
.push_back(zero_suggest_provider_
);
192 for (ACProviders::iterator
i(providers_
.begin()); i
!= providers_
.end(); ++i
)
196 AutocompleteController::~AutocompleteController() {
197 // The providers may have tasks outstanding that hold refs to them. We need
198 // to ensure they won't call us back if they outlive us. (Practically,
199 // calling Stop() should also cancel those tasks and make it so that we hold
200 // the only refs.) We also don't want to bother notifying anyone of our
201 // result changes here, because the notification observer is in the midst of
202 // shutdown too, so we don't ask Stop() to clear |result_| (and notify).
203 result_
.Reset(); // Not really necessary.
206 for (ACProviders::iterator
i(providers_
.begin()); i
!= providers_
.end(); ++i
)
209 providers_
.clear(); // Not really necessary.
212 void AutocompleteController::Start(const AutocompleteInput
& input
) {
213 const base::string16
old_input_text(input_
.text());
214 const bool old_want_asynchronous_matches
= input_
.want_asynchronous_matches();
217 // See if we can avoid rerunning autocomplete when the query hasn't changed
218 // much. When the user presses or releases the ctrl key, the desired_tld
219 // changes, and when the user finishes an IME composition, inline autocomplete
220 // may no longer be prevented. In both these cases the text itself hasn't
221 // changed since the last query, and some providers can do much less work (and
222 // get matches back more quickly). Taking advantage of this reduces flicker.
224 // NOTE: This comes after constructing |input_| above since that construction
225 // can change the text string (e.g. by stripping off a leading '?').
226 const bool minimal_changes
= (input_
.text() == old_input_text
) &&
227 (input_
.want_asynchronous_matches() == old_want_asynchronous_matches
);
229 expire_timer_
.Stop();
232 // Start the new query.
234 base::TimeTicks start_time
= base::TimeTicks::Now();
235 for (ACProviders::iterator
i(providers_
.begin()); i
!= providers_
.end();
237 // TODO(mpearson): Remove timing code once bugs 178705 / 237703 / 168933
239 base::TimeTicks provider_start_time
= base::TimeTicks::Now();
241 // Call Start() on ZeroSuggestProvider with an INVALID AutocompleteInput
242 // to clear out zero-suggest |matches_|.
243 if (*i
== zero_suggest_provider_
)
244 (*i
)->Start(AutocompleteInput(), minimal_changes
);
246 (*i
)->Start(input_
, minimal_changes
);
248 if (!input
.want_asynchronous_matches())
249 DCHECK((*i
)->done());
250 base::TimeTicks provider_end_time
= base::TimeTicks::Now();
251 std::string name
= std::string("Omnibox.ProviderTime.") + (*i
)->GetName();
252 base::HistogramBase
* counter
= base::Histogram::FactoryGet(
253 name
, 1, 5000, 20, base::Histogram::kUmaTargetedHistogramFlag
);
254 counter
->Add(static_cast<int>(
255 (provider_end_time
- provider_start_time
).InMilliseconds()));
257 if (input
.want_asynchronous_matches() && (input
.text().length() < 6)) {
258 base::TimeTicks end_time
= base::TimeTicks::Now();
259 std::string name
= "Omnibox.QueryTime." + base::IntToString(
260 input
.text().length());
261 base::HistogramBase
* counter
= base::Histogram::FactoryGet(
262 name
, 1, 1000, 50, base::Histogram::kUmaTargetedHistogramFlag
);
263 counter
->Add(static_cast<int>((end_time
- start_time
).InMilliseconds()));
267 // The second true forces saying the default match has changed.
268 // This triggers the edit model to update things such as the inline
269 // autocomplete state. In particular, if the user has typed a key
270 // since the last notification, and we're now re-running
271 // autocomplete, then we need to update the inline autocompletion
272 // even if the current match is for the same URL as the last run's
273 // default match. Likewise, the controller doesn't know what's
274 // happened in the edit since the last time it ran autocomplete.
275 // The user might have selected all the text and hit delete, then
276 // typed a new character. The selection and delete won't send any
277 // signals to the controller so it doesn't realize that anything was
278 // cleared or changed. Even if the default match hasn't changed, we
279 // need the edit model to update the display.
280 UpdateResult(false, true);
288 void AutocompleteController::Stop(bool clear_result
) {
289 for (ACProviders::const_iterator
i(providers_
.begin()); i
!= providers_
.end();
291 (*i
)->Stop(clear_result
);
294 expire_timer_
.Stop();
297 if (clear_result
&& !result_
.empty()) {
299 // NOTE: We pass in false since we're trying to only clear the popup, not
300 // touch the edit... this is all a mess and should be cleaned up :(
301 NotifyChanged(false);
305 void AutocompleteController::StartZeroSuggest(const AutocompleteInput
& input
) {
306 if (zero_suggest_provider_
!= NULL
) {
307 DCHECK(!in_start_
); // We should not be already running a query.
309 // Call Start() on all prefix-based providers with an INVALID
310 // AutocompleteInput to clear out cached |matches_|, which ensures that
311 // they aren't used with zero suggest.
312 for (ACProviders::iterator
i(providers_
.begin()); i
!= providers_
.end();
314 if (*i
== zero_suggest_provider_
)
315 (*i
)->Start(input
, false);
317 (*i
)->Start(AutocompleteInput(), false);
322 void AutocompleteController::DeleteMatch(const AutocompleteMatch
& match
) {
323 DCHECK(match
.SupportsDeletion());
325 // Delete duplicate matches attached to the main match first.
326 for (ACMatches::const_iterator
it(match
.duplicate_matches
.begin());
327 it
!= match
.duplicate_matches
.end(); ++it
) {
329 it
->provider
->DeleteMatch(*it
);
333 match
.provider
->DeleteMatch(match
);
335 OnProviderUpdate(true);
337 // If we're not done, we might attempt to redisplay the deleted match. Make
338 // sure we aren't displaying it by removing any old entries.
339 ExpireCopiedEntries();
342 void AutocompleteController::ExpireCopiedEntries() {
343 // The first true makes UpdateResult() clear out the results and
344 // regenerate them, thus ensuring that no results from the previous
345 // result set remain.
346 UpdateResult(true, false);
349 void AutocompleteController::OnProviderUpdate(bool updated_matches
) {
351 // Multiple providers may provide synchronous results, so we only update the
352 // results if we're not in Start().
353 if (!in_start_
&& (updated_matches
|| done_
))
354 UpdateResult(false, false);
357 void AutocompleteController::AddProvidersInfo(
358 ProvidersInfo
* provider_info
) const {
359 provider_info
->clear();
360 for (ACProviders::const_iterator
i(providers_
.begin()); i
!= providers_
.end();
362 // Add per-provider info, if any.
363 (*i
)->AddProviderInfo(provider_info
);
365 // This is also a good place to put code to add info that you want to
366 // add for every provider.
370 void AutocompleteController::ResetSession() {
371 for (ACProviders::const_iterator
i(providers_
.begin()); i
!= providers_
.end();
373 (*i
)->ResetSession();
376 void AutocompleteController::UpdateMatchDestinationURL(
377 base::TimeDelta query_formulation_time
,
378 AutocompleteMatch
* match
) const {
379 TemplateURL
* template_url
= match
->GetTemplateURL(profile_
, false);
380 if (!template_url
|| !match
->search_terms_args
.get() ||
381 match
->search_terms_args
->assisted_query_stats
.empty())
384 // Append the query formulation time (time from when the user first typed a
385 // character into the omnibox to when the user selected a query) and whether
386 // a field trial has triggered to the AQS parameter.
387 TemplateURLRef::SearchTermsArgs
search_terms_args(*match
->search_terms_args
);
388 search_terms_args
.assisted_query_stats
+= base::StringPrintf(
389 ".%" PRId64
"j%dj%d",
390 query_formulation_time
.InMilliseconds(),
392 search_provider_
->field_trial_triggered_in_session()) ||
393 (zero_suggest_provider_
&&
394 zero_suggest_provider_
->field_trial_triggered_in_session()),
395 input_
.current_page_classification());
396 match
->destination_url
=
397 GURL(template_url
->url_ref().ReplaceSearchTerms(search_terms_args
));
400 void AutocompleteController::UpdateResult(
401 bool regenerate_result
,
402 bool force_notify_default_match_changed
) {
403 const bool last_default_was_valid
= result_
.default_match() != result_
.end();
404 // The following three variables are only set and used if
405 // |last_default_was_valid|.
406 base::string16 last_default_fill_into_edit
, last_default_keyword
,
407 last_default_associated_keyword
;
408 if (last_default_was_valid
) {
409 last_default_fill_into_edit
= result_
.default_match()->fill_into_edit
;
410 last_default_keyword
= result_
.default_match()->keyword
;
411 if (result_
.default_match()->associated_keyword
!= NULL
)
412 last_default_associated_keyword
=
413 result_
.default_match()->associated_keyword
->keyword
;
416 if (regenerate_result
)
419 AutocompleteResult last_result
;
420 last_result
.Swap(&result_
);
422 for (ACProviders::const_iterator
i(providers_
.begin());
423 i
!= providers_
.end(); ++i
)
424 result_
.AppendMatches((*i
)->matches());
426 // Sort the matches and trim to a small number of "best" matches.
427 result_
.SortAndCull(input_
, profile_
);
429 // Need to validate before invoking CopyOldMatches as the old matches are not
430 // valid against the current input.
436 // This conditional needs to match the conditional in Start that invokes
438 result_
.CopyOldMatches(input_
, last_result
, profile_
);
441 UpdateKeywordDescriptions(&result_
);
442 UpdateAssociatedKeywords(&result_
);
443 UpdateAssistedQueryStats(&result_
);
445 const bool default_is_valid
= result_
.default_match() != result_
.end();
446 base::string16 default_associated_keyword
;
447 if (default_is_valid
&&
448 (result_
.default_match()->associated_keyword
!= NULL
)) {
449 default_associated_keyword
=
450 result_
.default_match()->associated_keyword
->keyword
;
452 // We've gotten async results. Send notification that the default match
453 // updated if fill_into_edit, associated_keyword, or keyword differ. (The
454 // second can change if we've just started Chrome and the keyword database
455 // finishes loading while processing this request. The third can change
456 // if we swapped from interpreting the input as a search--which gets
457 // labeled with the default search provider's keyword--to a URL.)
458 // We don't check the URL as that may change for the default match
459 // even though the fill into edit hasn't changed (see SearchProvider
460 // for one case of this).
461 const bool notify_default_match
=
462 (last_default_was_valid
!= default_is_valid
) ||
463 (last_default_was_valid
&&
464 ((result_
.default_match()->fill_into_edit
!=
465 last_default_fill_into_edit
) ||
466 (default_associated_keyword
!= last_default_associated_keyword
) ||
467 (result_
.default_match()->keyword
!= last_default_keyword
)));
468 if (notify_default_match
)
469 last_time_default_match_changed_
= base::TimeTicks::Now();
471 NotifyChanged(force_notify_default_match_changed
|| notify_default_match
);
474 void AutocompleteController::UpdateAssociatedKeywords(
475 AutocompleteResult
* result
) {
476 if (!keyword_provider_
)
479 std::set
<base::string16
> keywords
;
480 for (ACMatches::iterator
match(result
->begin()); match
!= result
->end();
482 base::string16
keyword(
483 match
->GetSubstitutingExplicitlyInvokedKeyword(profile_
));
484 if (!keyword
.empty()) {
485 keywords
.insert(keyword
);
489 // Only add the keyword if the match does not have a duplicate keyword with
490 // a more relevant match.
491 keyword
= match
->associated_keyword
.get() ?
492 match
->associated_keyword
->keyword
:
493 keyword_provider_
->GetKeywordForText(match
->fill_into_edit
);
494 if (!keyword
.empty() && !keywords
.count(keyword
)) {
495 keywords
.insert(keyword
);
497 if (!match
->associated_keyword
.get())
498 match
->associated_keyword
.reset(new AutocompleteMatch(
499 keyword_provider_
->CreateVerbatimMatch(match
->fill_into_edit
,
502 match
->associated_keyword
.reset();
507 void AutocompleteController::UpdateKeywordDescriptions(
508 AutocompleteResult
* result
) {
509 base::string16 last_keyword
;
510 for (AutocompleteResult::iterator
i(result
->begin()); i
!= result
->end();
512 if (AutocompleteMatch::IsSearchType(i
->type
)) {
513 if (AutocompleteMatchHasCustomDescription(*i
))
515 i
->description
.clear();
516 i
->description_class
.clear();
517 DCHECK(!i
->keyword
.empty());
518 if (i
->keyword
!= last_keyword
) {
519 const TemplateURL
* template_url
= i
->GetTemplateURL(profile_
, false);
521 // For extension keywords, just make the description the extension
522 // name -- don't assume that the normal search keyword description is
524 i
->description
= template_url
->AdjustedShortNameForLocaleDirection();
525 if (template_url
->GetType() != TemplateURL::OMNIBOX_API_EXTENSION
) {
526 i
->description
= l10n_util::GetStringFUTF16(
527 IDS_AUTOCOMPLETE_SEARCH_DESCRIPTION
, i
->description
);
529 i
->description_class
.push_back(
530 ACMatchClassification(0, ACMatchClassification::DIM
));
532 last_keyword
= i
->keyword
;
535 last_keyword
.clear();
540 void AutocompleteController::UpdateAssistedQueryStats(
541 AutocompleteResult
* result
) {
545 // Build the impressions string (the AQS part after ".").
546 std::string autocompletions
;
548 size_t last_type
= base::string16::npos
;
549 size_t last_subtype
= base::string16::npos
;
550 for (ACMatches::iterator
match(result
->begin()); match
!= result
->end();
552 size_t type
= base::string16::npos
;
553 size_t subtype
= base::string16::npos
;
554 AutocompleteMatchToAssistedQuery(match
->type
, &type
, &subtype
);
555 if (last_type
!= base::string16::npos
&&
556 (type
!= last_type
|| subtype
!= last_subtype
)) {
557 AppendAvailableAutocompletion(
558 last_type
, last_subtype
, count
, &autocompletions
);
564 last_subtype
= subtype
;
566 AppendAvailableAutocompletion(
567 last_type
, last_subtype
, count
, &autocompletions
);
568 // Go over all matches and set AQS if the match supports it.
569 for (size_t index
= 0; index
< result
->size(); ++index
) {
570 AutocompleteMatch
* match
= result
->match_at(index
);
571 const TemplateURL
* template_url
= match
->GetTemplateURL(profile_
, false);
572 if (!template_url
|| !match
->search_terms_args
.get())
574 std::string selected_index
;
575 // Prevent trivial suggestions from getting credit for being selected.
576 if (!IsTrivialAutocompletion(*match
))
577 selected_index
= base::StringPrintf("%" PRIuS
, index
);
578 match
->search_terms_args
->assisted_query_stats
=
579 base::StringPrintf("chrome.%s.%s",
580 selected_index
.c_str(),
581 autocompletions
.c_str());
582 match
->destination_url
= GURL(template_url
->url_ref().ReplaceSearchTerms(
583 *match
->search_terms_args
));
587 void AutocompleteController::NotifyChanged(bool notify_default_match
) {
589 delegate_
->OnResultChanged(notify_default_match
);
591 content::NotificationService::current()->Notify(
592 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY
,
593 content::Source
<AutocompleteController
>(this),
594 content::NotificationService::NoDetails());
598 void AutocompleteController::CheckIfDone() {
599 for (ACProviders::const_iterator
i(providers_
.begin()); i
!= providers_
.end();
609 void AutocompleteController::StartExpireTimer() {
610 // Amount of time (in ms) between when the user stops typing and
611 // when we remove any copied entries. We do this from the time the
612 // user stopped typing as some providers (such as SearchProvider)
613 // wait for the user to stop typing before they initiate a query.
614 const int kExpireTimeMS
= 500;
616 if (result_
.HasCopiedMatches())
617 expire_timer_
.Start(FROM_HERE
,
618 base::TimeDelta::FromMilliseconds(kExpireTimeMS
),
619 this, &AutocompleteController::ExpireCopiedEntries
);
622 void AutocompleteController::StartStopTimer() {
623 stop_timer_
.Start(FROM_HERE
,
624 stop_timer_duration_
,
625 base::Bind(&AutocompleteController::Stop
,
626 base::Unretained(this),