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/command_line.h"
11 #include "base/format_macros.h"
12 #include "base/logging.h"
13 #include "base/metrics/histogram.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/time/time.h"
17 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h"
18 #include "chrome/browser/autocomplete/bookmark_provider.h"
19 #include "chrome/browser/autocomplete/builtin_provider.h"
20 #include "chrome/browser/autocomplete/extension_app_provider.h"
21 #include "chrome/browser/autocomplete/history_quick_provider.h"
22 #include "chrome/browser/autocomplete/history_url_provider.h"
23 #include "chrome/browser/autocomplete/keyword_provider.h"
24 #include "chrome/browser/autocomplete/search_provider.h"
25 #include "chrome/browser/autocomplete/shortcuts_provider.h"
26 #include "chrome/browser/autocomplete/zero_suggest_provider.h"
27 #include "chrome/browser/chrome_notification_types.h"
28 #include "chrome/browser/omnibox/omnibox_field_trial.h"
29 #include "chrome/browser/profiles/profile.h"
30 #include "chrome/browser/search/search.h"
31 #include "chrome/browser/search_engines/template_url.h"
32 #include "chrome/common/chrome_switches.h"
33 #include "content/public/browser/notification_service.h"
34 #include "grit/generated_resources.h"
35 #include "grit/theme_resources.h"
36 #include "ui/base/l10n/l10n_util.h"
38 #if defined(OS_CHROMEOS)
39 #include "chrome/browser/autocomplete/contact_provider_chromeos.h"
40 #include "chrome/browser/chromeos/contacts/contact_manager.h"
45 // Converts the given match to a type (and possibly subtype) based on the AQS
46 // specification. For more details, see
47 // http://goto.google.com/binary-clients-logging.
48 void AutocompleteMatchToAssistedQuery(
49 const AutocompleteMatch::Type& match, size_t* type, size_t* subtype) {
50 // This type indicates a native chrome suggestion.
52 // Default value, indicating no subtype.
53 *subtype = base::string16::npos;
56 case AutocompleteMatchType::SEARCH_SUGGEST: {
60 case AutocompleteMatchType::SEARCH_SUGGEST_ENTITY: {
64 case AutocompleteMatchType::SEARCH_SUGGEST_INFINITE: {
68 case AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED: {
72 case AutocompleteMatchType::SEARCH_SUGGEST_PROFILE: {
76 case AutocompleteMatchType::NAVSUGGEST: {
80 case AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED: {
84 case AutocompleteMatchType::URL_WHAT_YOU_TYPED: {
88 case AutocompleteMatchType::SEARCH_HISTORY: {
92 case AutocompleteMatchType::HISTORY_URL: {
96 case AutocompleteMatchType::HISTORY_TITLE: {
100 case AutocompleteMatchType::HISTORY_BODY: {
104 case AutocompleteMatchType::HISTORY_KEYWORD: {
108 case AutocompleteMatchType::BOOKMARK_TITLE: {
113 // This value indicates a native chrome suggestion with no named subtype
120 // Appends available autocompletion of the given type, subtype, and number to
121 // the existing available autocompletions string, encoding according to the
123 void AppendAvailableAutocompletion(size_t type,
126 std::string* autocompletions) {
127 if (!autocompletions->empty())
128 autocompletions->append("j");
129 base::StringAppendF(autocompletions, "%" PRIuS, type);
130 // Subtype is optional - base::string16::npos indicates no subtype.
131 if (subtype != base::string16::npos)
132 base::StringAppendF(autocompletions, "i%" PRIuS, subtype);
134 base::StringAppendF(autocompletions, "l%d", count);
137 // Returns whether the autocompletion is trivial enough that we consider it
138 // an autocompletion for which the omnibox autocompletion code did not add
140 bool IsTrivialAutocompletion(const AutocompleteMatch& match) {
141 return match.type == AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED ||
142 match.type == AutocompleteMatchType::URL_WHAT_YOU_TYPED ||
143 match.type == AutocompleteMatchType::SEARCH_OTHER_ENGINE;
146 // Whether this autocomplete match type supports custom descriptions.
147 bool AutocompleteMatchHasCustomDescription(const AutocompleteMatch& match) {
148 return match.type == AutocompleteMatchType::SEARCH_SUGGEST_ENTITY ||
149 match.type == AutocompleteMatchType::SEARCH_SUGGEST_PROFILE;
154 AutocompleteController::AutocompleteController(
156 AutocompleteControllerDelegate* delegate,
158 : delegate_(delegate),
159 history_url_provider_(NULL),
160 keyword_provider_(NULL),
161 search_provider_(NULL),
162 zero_suggest_provider_(NULL),
163 stop_timer_duration_(OmniboxFieldTrial::StopTimerFieldTrialDuration()),
166 in_zero_suggest_(false),
168 // AND with the disabled providers, if any.
169 provider_types &= ~OmniboxFieldTrial::GetDisabledProviderTypes();
170 bool use_hqp = !!(provider_types & AutocompleteProvider::TYPE_HISTORY_QUICK);
171 // TODO(mrossetti): Permanently modify the HistoryURLProvider to not search
172 // titles once HQP is turned on permanently.
174 if (provider_types & AutocompleteProvider::TYPE_BUILTIN)
175 providers_.push_back(new BuiltinProvider(this, profile));
176 #if defined(OS_CHROMEOS)
177 if (provider_types & AutocompleteProvider::TYPE_CONTACT)
178 providers_.push_back(new ContactProvider(this, profile,
179 contacts::ContactManager::GetInstance()->GetWeakPtr()));
181 if (provider_types & AutocompleteProvider::TYPE_EXTENSION_APP)
182 providers_.push_back(new ExtensionAppProvider(this, profile));
184 providers_.push_back(new HistoryQuickProvider(this, profile));
185 if (provider_types & AutocompleteProvider::TYPE_HISTORY_URL) {
186 history_url_provider_ = new HistoryURLProvider(this, profile);
187 providers_.push_back(history_url_provider_);
189 // Search provider/"tab to search" can be used on all platforms other than
191 #if !defined(OS_ANDROID)
192 if (provider_types & AutocompleteProvider::TYPE_KEYWORD) {
193 keyword_provider_ = new KeywordProvider(this, profile);
194 providers_.push_back(keyword_provider_);
197 if (provider_types & AutocompleteProvider::TYPE_SEARCH) {
198 search_provider_ = new SearchProvider(this, profile);
199 providers_.push_back(search_provider_);
201 if (provider_types & AutocompleteProvider::TYPE_SHORTCUTS)
202 providers_.push_back(new ShortcutsProvider(this, profile));
204 // Create ZeroSuggest if it is enabled.
205 if (provider_types & AutocompleteProvider::TYPE_ZERO_SUGGEST) {
206 zero_suggest_provider_ = ZeroSuggestProvider::Create(this, profile);
207 if (zero_suggest_provider_)
208 providers_.push_back(zero_suggest_provider_);
211 if ((provider_types & AutocompleteProvider::TYPE_BOOKMARK) &&
212 !CommandLine::ForCurrentProcess()->HasSwitch(
213 switches::kDisableBookmarkAutocompleteProvider))
214 providers_.push_back(new BookmarkProvider(this, profile));
216 for (ACProviders::iterator i(providers_.begin()); i != providers_.end(); ++i)
220 AutocompleteController::~AutocompleteController() {
221 // The providers may have tasks outstanding that hold refs to them. We need
222 // to ensure they won't call us back if they outlive us. (Practically,
223 // calling Stop() should also cancel those tasks and make it so that we hold
224 // the only refs.) We also don't want to bother notifying anyone of our
225 // result changes here, because the notification observer is in the midst of
226 // shutdown too, so we don't ask Stop() to clear |result_| (and notify).
227 result_.Reset(); // Not really necessary.
230 for (ACProviders::iterator i(providers_.begin()); i != providers_.end(); ++i)
233 providers_.clear(); // Not really necessary.
236 void AutocompleteController::Start(const AutocompleteInput& input) {
237 const base::string16 old_input_text(input_.text());
238 const AutocompleteInput::MatchesRequested old_matches_requested =
239 input_.matches_requested();
242 // See if we can avoid rerunning autocomplete when the query hasn't changed
243 // much. When the user presses or releases the ctrl key, the desired_tld
244 // changes, and when the user finishes an IME composition, inline autocomplete
245 // may no longer be prevented. In both these cases the text itself hasn't
246 // changed since the last query, and some providers can do much less work (and
247 // get matches back more quickly). Taking advantage of this reduces flicker.
249 // NOTE: This comes after constructing |input_| above since that construction
250 // can change the text string (e.g. by stripping off a leading '?').
251 const bool minimal_changes = (input_.text() == old_input_text) &&
252 (input_.matches_requested() == old_matches_requested);
254 expire_timer_.Stop();
257 // Start the new query.
258 in_zero_suggest_ = false;
260 base::TimeTicks start_time = base::TimeTicks::Now();
261 for (ACProviders::iterator i(providers_.begin()); i != providers_.end();
263 // TODO(mpearson): Remove timing code once bugs 178705 / 237703 / 168933
265 base::TimeTicks provider_start_time = base::TimeTicks::Now();
266 (*i)->Start(input_, minimal_changes);
267 if (input.matches_requested() != AutocompleteInput::ALL_MATCHES)
268 DCHECK((*i)->done());
269 base::TimeTicks provider_end_time = base::TimeTicks::Now();
270 std::string name = std::string("Omnibox.ProviderTime.") + (*i)->GetName();
271 base::HistogramBase* counter = base::Histogram::FactoryGet(
272 name, 1, 5000, 20, base::Histogram::kUmaTargetedHistogramFlag);
273 counter->Add(static_cast<int>(
274 (provider_end_time - provider_start_time).InMilliseconds()));
276 if (input.matches_requested() == AutocompleteInput::ALL_MATCHES &&
277 (input.text().length() < 6)) {
278 base::TimeTicks end_time = base::TimeTicks::Now();
279 std::string name = "Omnibox.QueryTime." + base::IntToString(
280 input.text().length());
281 base::HistogramBase* counter = base::Histogram::FactoryGet(
282 name, 1, 1000, 50, base::Histogram::kUmaTargetedHistogramFlag);
283 counter->Add(static_cast<int>((end_time - start_time).InMilliseconds()));
287 // The second true forces saying the default match has changed.
288 // This triggers the edit model to update things such as the inline
289 // autocomplete state. In particular, if the user has typed a key
290 // since the last notification, and we're now re-running
291 // autocomplete, then we need to update the inline autocompletion
292 // even if the current match is for the same URL as the last run's
293 // default match. Likewise, the controller doesn't know what's
294 // happened in the edit since the last time it ran autocomplete.
295 // The user might have selected all the text and hit delete, then
296 // typed a new character. The selection and delete won't send any
297 // signals to the controller so it doesn't realize that anything was
298 // cleared or changed. Even if the default match hasn't changed, we
299 // need the edit model to update the display.
300 UpdateResult(false, true);
308 void AutocompleteController::Stop(bool clear_result) {
309 for (ACProviders::const_iterator i(providers_.begin()); i != providers_.end();
311 (*i)->Stop(clear_result);
314 expire_timer_.Stop();
317 if (clear_result && !result_.empty()) {
319 // NOTE: We pass in false since we're trying to only clear the popup, not
320 // touch the edit... this is all a mess and should be cleaned up :(
321 NotifyChanged(false);
325 void AutocompleteController::StartZeroSuggest(
327 AutocompleteInput::PageClassification page_classification,
328 const base::string16& permanent_text) {
329 if (zero_suggest_provider_ != NULL) {
330 DCHECK(!in_start_); // We should not be already running a query.
331 in_zero_suggest_ = true;
332 zero_suggest_provider_->StartZeroSuggest(
333 url, page_classification, permanent_text);
337 void AutocompleteController::StopZeroSuggest() {
338 if (zero_suggest_provider_ != NULL) {
339 DCHECK(!in_start_); // We should not be already running a query.
340 zero_suggest_provider_->Stop(false);
344 void AutocompleteController::DeleteMatch(const AutocompleteMatch& match) {
345 DCHECK(match.deletable);
346 match.provider->DeleteMatch(match); // This may synchronously call back to
347 // OnProviderUpdate().
348 // If DeleteMatch resulted in a callback to OnProviderUpdate and we're
349 // not done, we might attempt to redisplay the deleted match. Make sure
350 // we aren't displaying it by removing any old entries.
351 ExpireCopiedEntries();
354 void AutocompleteController::ExpireCopiedEntries() {
355 // The first true makes UpdateResult() clear out the results and
356 // regenerate them, thus ensuring that no results from the previous
357 // result set remain.
358 UpdateResult(true, false);
361 void AutocompleteController::OnProviderUpdate(bool updated_matches) {
362 if (in_zero_suggest_) {
363 // We got ZeroSuggest results before Start(). Show only those results,
364 // because results from other providers are stale.
366 result_.AppendMatches(zero_suggest_provider_->matches());
367 result_.SortAndCull(input_, profile_);
368 UpdateAssistedQueryStats(&result_);
372 // Multiple providers may provide synchronous results, so we only update the
373 // results if we're not in Start().
374 if (!in_start_ && (updated_matches || done_))
375 UpdateResult(false, false);
379 void AutocompleteController::AddProvidersInfo(
380 ProvidersInfo* provider_info) const {
381 provider_info->clear();
382 for (ACProviders::const_iterator i(providers_.begin()); i != providers_.end();
384 // Add per-provider info, if any.
385 (*i)->AddProviderInfo(provider_info);
387 // This is also a good place to put code to add info that you want to
388 // add for every provider.
392 void AutocompleteController::ResetSession() {
393 for (ACProviders::const_iterator i(providers_.begin()); i != providers_.end();
395 (*i)->ResetSession();
396 in_zero_suggest_ = false;
399 void AutocompleteController::UpdateMatchDestinationURL(
400 base::TimeDelta query_formulation_time,
401 AutocompleteMatch* match) const {
402 TemplateURL* template_url = match->GetTemplateURL(profile_, false);
403 if (!template_url || !match->search_terms_args.get() ||
404 match->search_terms_args->assisted_query_stats.empty())
407 // Append the query formulation time (time from when the user first typed a
408 // character into the omnibox to when the user selected a query) and whether
409 // a field trial has triggered to the AQS parameter.
410 TemplateURLRef::SearchTermsArgs search_terms_args(*match->search_terms_args);
411 search_terms_args.assisted_query_stats += base::StringPrintf(
412 ".%" PRId64 "j%dj%d",
413 query_formulation_time.InMilliseconds(),
415 search_provider_->field_trial_triggered_in_session()) ||
416 (zero_suggest_provider_ &&
417 zero_suggest_provider_->field_trial_triggered_in_session()),
418 input_.current_page_classification());
419 match->destination_url =
420 GURL(template_url->url_ref().ReplaceSearchTerms(search_terms_args));
423 void AutocompleteController::UpdateResult(
424 bool regenerate_result,
425 bool force_notify_default_match_changed) {
426 const bool last_default_was_valid = result_.default_match() != result_.end();
427 // The following three variables are only set and used if
428 // |last_default_was_valid|.
429 base::string16 last_default_fill_into_edit, last_default_keyword,
430 last_default_associated_keyword;
431 if (last_default_was_valid) {
432 last_default_fill_into_edit = result_.default_match()->fill_into_edit;
433 last_default_keyword = result_.default_match()->keyword;
434 if (result_.default_match()->associated_keyword != NULL)
435 last_default_associated_keyword =
436 result_.default_match()->associated_keyword->keyword;
439 if (regenerate_result)
442 AutocompleteResult last_result;
443 last_result.Swap(&result_);
445 for (ACProviders::const_iterator i(providers_.begin()); i != providers_.end();
447 result_.AppendMatches((*i)->matches());
449 // Sort the matches and trim to a small number of "best" matches.
450 result_.SortAndCull(input_, profile_);
452 // Need to validate before invoking CopyOldMatches as the old matches are not
453 // valid against the current input.
459 // This conditional needs to match the conditional in Start that invokes
461 result_.CopyOldMatches(input_, last_result, profile_);
464 UpdateKeywordDescriptions(&result_);
465 UpdateAssociatedKeywords(&result_);
466 UpdateAssistedQueryStats(&result_);
468 const bool default_is_valid = result_.default_match() != result_.end();
469 base::string16 default_associated_keyword;
470 if (default_is_valid &&
471 (result_.default_match()->associated_keyword != NULL)) {
472 default_associated_keyword =
473 result_.default_match()->associated_keyword->keyword;
475 // We've gotten async results. Send notification that the default match
476 // updated if fill_into_edit, associated_keyword, or keyword differ. (The
477 // second can change if we've just started Chrome and the keyword database
478 // finishes loading while processing this request. The third can change
479 // if we swapped from interpreting the input as a search--which gets
480 // labeled with the default search provider's keyword--to a URL.)
481 // We don't check the URL as that may change for the default match
482 // even though the fill into edit hasn't changed (see SearchProvider
483 // for one case of this).
484 const bool notify_default_match =
485 (last_default_was_valid != default_is_valid) ||
486 (last_default_was_valid &&
487 ((result_.default_match()->fill_into_edit !=
488 last_default_fill_into_edit) ||
489 (default_associated_keyword != last_default_associated_keyword) ||
490 (result_.default_match()->keyword != last_default_keyword)));
491 if (notify_default_match)
492 last_time_default_match_changed_ = base::TimeTicks::Now();
494 NotifyChanged(force_notify_default_match_changed || notify_default_match);
497 void AutocompleteController::UpdateAssociatedKeywords(
498 AutocompleteResult* result) {
499 if (!keyword_provider_)
502 std::set<base::string16> keywords;
503 for (ACMatches::iterator match(result->begin()); match != result->end();
505 base::string16 keyword(
506 match->GetSubstitutingExplicitlyInvokedKeyword(profile_));
507 if (!keyword.empty()) {
508 keywords.insert(keyword);
512 // Only add the keyword if the match does not have a duplicate keyword with
513 // a more relevant match.
514 keyword = match->associated_keyword.get() ?
515 match->associated_keyword->keyword :
516 keyword_provider_->GetKeywordForText(match->fill_into_edit);
517 if (!keyword.empty() && !keywords.count(keyword)) {
518 keywords.insert(keyword);
520 if (!match->associated_keyword.get())
521 match->associated_keyword.reset(new AutocompleteMatch(
522 keyword_provider_->CreateVerbatimMatch(match->fill_into_edit,
525 match->associated_keyword.reset();
530 void AutocompleteController::UpdateKeywordDescriptions(
531 AutocompleteResult* result) {
532 base::string16 last_keyword;
533 for (AutocompleteResult::iterator i(result->begin()); i != result->end();
535 if ((i->provider->type() == AutocompleteProvider::TYPE_KEYWORD &&
536 !i->keyword.empty()) ||
537 (i->provider->type() == AutocompleteProvider::TYPE_SEARCH &&
538 AutocompleteMatch::IsSearchType(i->type))) {
539 if (AutocompleteMatchHasCustomDescription(*i))
541 i->description.clear();
542 i->description_class.clear();
543 DCHECK(!i->keyword.empty());
544 if (i->keyword != last_keyword) {
545 const TemplateURL* template_url = i->GetTemplateURL(profile_, false);
547 // For extension keywords, just make the description the extension
548 // name -- don't assume that the normal search keyword description is
550 i->description = template_url->AdjustedShortNameForLocaleDirection();
551 if (template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) {
552 i->description = l10n_util::GetStringFUTF16(
553 IDS_AUTOCOMPLETE_SEARCH_DESCRIPTION, i->description);
555 i->description_class.push_back(
556 ACMatchClassification(0, ACMatchClassification::DIM));
558 last_keyword = i->keyword;
561 last_keyword.clear();
566 void AutocompleteController::UpdateAssistedQueryStats(
567 AutocompleteResult* result) {
571 // Build the impressions string (the AQS part after ".").
572 std::string autocompletions;
574 size_t last_type = base::string16::npos;
575 size_t last_subtype = base::string16::npos;
576 for (ACMatches::iterator match(result->begin()); match != result->end();
578 size_t type = base::string16::npos;
579 size_t subtype = base::string16::npos;
580 AutocompleteMatchToAssistedQuery(match->type, &type, &subtype);
581 if (last_type != base::string16::npos &&
582 (type != last_type || subtype != last_subtype)) {
583 AppendAvailableAutocompletion(
584 last_type, last_subtype, count, &autocompletions);
590 last_subtype = subtype;
592 AppendAvailableAutocompletion(
593 last_type, last_subtype, count, &autocompletions);
594 // Go over all matches and set AQS if the match supports it.
595 for (size_t index = 0; index < result->size(); ++index) {
596 AutocompleteMatch* match = result->match_at(index);
597 const TemplateURL* template_url = match->GetTemplateURL(profile_, false);
598 if (!template_url || !match->search_terms_args.get())
600 std::string selected_index;
601 // Prevent trivial suggestions from getting credit for being selected.
602 if (!IsTrivialAutocompletion(*match))
603 selected_index = base::StringPrintf("%" PRIuS, index);
604 match->search_terms_args->assisted_query_stats =
605 base::StringPrintf("chrome.%s.%s",
606 selected_index.c_str(),
607 autocompletions.c_str());
608 match->destination_url = GURL(template_url->url_ref().ReplaceSearchTerms(
609 *match->search_terms_args));
613 void AutocompleteController::NotifyChanged(bool notify_default_match) {
615 delegate_->OnResultChanged(notify_default_match);
617 content::NotificationService::current()->Notify(
618 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY,
619 content::Source<AutocompleteController>(this),
620 content::NotificationService::NoDetails());
624 void AutocompleteController::CheckIfDone() {
625 for (ACProviders::const_iterator i(providers_.begin()); i != providers_.end();
635 void AutocompleteController::StartExpireTimer() {
636 // Amount of time (in ms) between when the user stops typing and
637 // when we remove any copied entries. We do this from the time the
638 // user stopped typing as some providers (such as SearchProvider)
639 // wait for the user to stop typing before they initiate a query.
640 const int kExpireTimeMS = 500;
642 if (result_.HasCopiedMatches())
643 expire_timer_.Start(FROM_HERE,
644 base::TimeDelta::FromMilliseconds(kExpireTimeMS),
645 this, &AutocompleteController::ExpireCopiedEntries);
648 void AutocompleteController::StartStopTimer() {
649 stop_timer_.Start(FROM_HERE,
650 stop_timer_duration_,
651 base::Bind(&AutocompleteController::Stop,
652 base::Unretained(this),