Add support for providers called when the omnibox is focused.
[chromium-blink-merge.git] / chrome / browser / autocomplete / autocomplete_controller.cc
blobb23c215c4b380903b0a564ce4acc63f74e81e679
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"
7 #include <set>
8 #include <string>
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/chrome_autocomplete_provider_client.h"
20 #include "chrome/browser/autocomplete/history_quick_provider.h"
21 #include "chrome/browser/autocomplete/history_url_provider.h"
22 #include "chrome/browser/autocomplete/shortcuts_provider.h"
23 #include "chrome/browser/autocomplete/zero_suggest_provider.h"
24 #include "chrome/browser/chrome_notification_types.h"
25 #include "components/omnibox/keyword_provider.h"
26 #include "components/omnibox/omnibox_field_trial.h"
27 #include "components/omnibox/search_provider.h"
28 #include "components/search_engines/template_url.h"
29 #include "components/search_engines/template_url_service.h"
30 #include "content/public/browser/notification_service.h"
31 #include "grit/components_strings.h"
32 #include "ui/base/l10n/l10n_util.h"
34 #if defined(ENABLE_EXTENSIONS)
35 #include "chrome/browser/autocomplete/keyword_extensions_delegate_impl.h"
36 #endif
38 namespace {
40 // Converts the given match to a type (and possibly subtype) based on the AQS
41 // specification. For more details, see
42 // http://goto.google.com/binary-clients-logging.
43 void AutocompleteMatchToAssistedQuery(
44 const AutocompleteMatch::Type& match,
45 const AutocompleteProvider* provider,
46 size_t* type,
47 size_t* subtype) {
48 // This type indicates a native chrome suggestion.
49 *type = 69;
50 // Default value, indicating no subtype.
51 *subtype = base::string16::npos;
53 // If provider is TYPE_ZERO_SUGGEST, set the subtype accordingly.
54 // Type will be set in the switch statement below where we'll enter one of
55 // SEARCH_SUGGEST or NAVSUGGEST. This subtype indicates context-aware zero
56 // suggest.
57 if (provider &&
58 (provider->type() == AutocompleteProvider::TYPE_ZERO_SUGGEST) &&
59 (match != AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED)) {
60 DCHECK((match == AutocompleteMatchType::SEARCH_SUGGEST) ||
61 (match == AutocompleteMatchType::NAVSUGGEST));
62 *subtype = 66;
65 switch (match) {
66 case AutocompleteMatchType::SEARCH_SUGGEST: {
67 // Do not set subtype here; subtype may have been set above.
68 *type = 0;
69 return;
71 case AutocompleteMatchType::SEARCH_SUGGEST_ENTITY: {
72 *subtype = 46;
73 return;
75 case AutocompleteMatchType::SEARCH_SUGGEST_INFINITE: {
76 *subtype = 33;
77 return;
79 case AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED: {
80 *subtype = 39;
81 return;
83 case AutocompleteMatchType::SEARCH_SUGGEST_PROFILE: {
84 *subtype = 44;
85 return;
87 case AutocompleteMatchType::SEARCH_SUGGEST_ANSWER: {
88 *subtype = 70;
89 return;
91 case AutocompleteMatchType::NAVSUGGEST: {
92 // Do not set subtype here; subtype may have been set above.
93 *type = 5;
94 return;
96 case AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED: {
97 *subtype = 57;
98 return;
100 case AutocompleteMatchType::URL_WHAT_YOU_TYPED: {
101 *subtype = 58;
102 return;
104 case AutocompleteMatchType::SEARCH_HISTORY: {
105 *subtype = 59;
106 return;
108 case AutocompleteMatchType::HISTORY_URL: {
109 *subtype = 60;
110 return;
112 case AutocompleteMatchType::HISTORY_TITLE: {
113 *subtype = 61;
114 return;
116 case AutocompleteMatchType::HISTORY_BODY: {
117 *subtype = 62;
118 return;
120 case AutocompleteMatchType::HISTORY_KEYWORD: {
121 *subtype = 63;
122 return;
124 case AutocompleteMatchType::BOOKMARK_TITLE: {
125 *subtype = 65;
126 return;
128 case AutocompleteMatchType::NAVSUGGEST_PERSONALIZED: {
129 *subtype = 39;
130 return;
132 default: {
133 // This value indicates a native chrome suggestion with no named subtype
134 // (yet).
135 *subtype = 64;
140 // Appends available autocompletion of the given type, subtype, and number to
141 // the existing available autocompletions string, encoding according to the
142 // spec.
143 void AppendAvailableAutocompletion(size_t type,
144 size_t subtype,
145 int count,
146 std::string* autocompletions) {
147 if (!autocompletions->empty())
148 autocompletions->append("j");
149 base::StringAppendF(autocompletions, "%" PRIuS, type);
150 // Subtype is optional - base::string16::npos indicates no subtype.
151 if (subtype != base::string16::npos)
152 base::StringAppendF(autocompletions, "i%" PRIuS, subtype);
153 if (count > 1)
154 base::StringAppendF(autocompletions, "l%d", count);
157 // Returns whether the autocompletion is trivial enough that we consider it
158 // an autocompletion for which the omnibox autocompletion code did not add
159 // any value.
160 bool IsTrivialAutocompletion(const AutocompleteMatch& match) {
161 return match.type == AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED ||
162 match.type == AutocompleteMatchType::URL_WHAT_YOU_TYPED ||
163 match.type == AutocompleteMatchType::SEARCH_OTHER_ENGINE;
166 // Whether this autocomplete match type supports custom descriptions.
167 bool AutocompleteMatchHasCustomDescription(const AutocompleteMatch& match) {
168 return match.type == AutocompleteMatchType::SEARCH_SUGGEST_ENTITY ||
169 match.type == AutocompleteMatchType::SEARCH_SUGGEST_PROFILE;
172 } // namespace
174 AutocompleteController::AutocompleteController(
175 Profile* profile,
176 TemplateURLService* template_url_service,
177 AutocompleteControllerDelegate* delegate,
178 int provider_types)
179 : delegate_(delegate),
180 history_url_provider_(NULL),
181 keyword_provider_(NULL),
182 search_provider_(NULL),
183 zero_suggest_provider_(NULL),
184 stop_timer_duration_(OmniboxFieldTrial::StopTimerFieldTrialDuration()),
185 done_(true),
186 in_start_(false),
187 template_url_service_(template_url_service) {
188 provider_types &= ~OmniboxFieldTrial::GetDisabledProviderTypes();
189 if (provider_types & AutocompleteProvider::TYPE_BOOKMARK)
190 providers_.push_back(new BookmarkProvider(profile));
191 if (provider_types & AutocompleteProvider::TYPE_BUILTIN)
192 providers_.push_back(new BuiltinProvider());
193 if (provider_types & AutocompleteProvider::TYPE_HISTORY_QUICK)
194 providers_.push_back(new HistoryQuickProvider(profile));
195 if (provider_types & AutocompleteProvider::TYPE_HISTORY_URL) {
196 history_url_provider_ = new HistoryURLProvider(this, profile);
197 providers_.push_back(history_url_provider_);
199 // "Tab to search" can be used on all platforms other than Android.
200 #if !defined(OS_ANDROID)
201 if (provider_types & AutocompleteProvider::TYPE_KEYWORD) {
202 keyword_provider_ = new KeywordProvider(this, template_url_service);
203 #if defined(ENABLE_EXTENSIONS)
204 keyword_provider_->set_extensions_delegate(
205 scoped_ptr<KeywordExtensionsDelegate>(
206 new KeywordExtensionsDelegateImpl(profile, keyword_provider_)));
207 #endif
208 providers_.push_back(keyword_provider_);
210 #endif
211 if (provider_types & AutocompleteProvider::TYPE_SEARCH) {
212 search_provider_ = new SearchProvider(
213 this, template_url_service, scoped_ptr<AutocompleteProviderClient>(
214 new ChromeAutocompleteProviderClient(profile)));
215 providers_.push_back(search_provider_);
217 if (provider_types & AutocompleteProvider::TYPE_SHORTCUTS)
218 providers_.push_back(new ShortcutsProvider(profile));
219 if (provider_types & AutocompleteProvider::TYPE_ZERO_SUGGEST) {
220 zero_suggest_provider_ = ZeroSuggestProvider::Create(
221 this, template_url_service, profile);
222 if (zero_suggest_provider_)
223 providers_.push_back(zero_suggest_provider_);
227 AutocompleteController::~AutocompleteController() {
228 // The providers may have tasks outstanding that hold refs to them. We need
229 // to ensure they won't call us back if they outlive us. (Practically,
230 // calling Stop() should also cancel those tasks and make it so that we hold
231 // the only refs.) We also don't want to bother notifying anyone of our
232 // result changes here, because the notification observer is in the midst of
233 // shutdown too, so we don't ask Stop() to clear |result_| (and notify).
234 result_.Reset(); // Not really necessary.
235 Stop(false);
238 void AutocompleteController::Start(const AutocompleteInput& input) {
239 const base::string16 old_input_text(input_.text());
240 const bool old_want_asynchronous_matches = input_.want_asynchronous_matches();
241 input_ = input;
243 // See if we can avoid rerunning autocomplete when the query hasn't changed
244 // much. When the user presses or releases the ctrl key, the desired_tld
245 // changes, and when the user finishes an IME composition, inline autocomplete
246 // may no longer be prevented. In both these cases the text itself hasn't
247 // changed since the last query, and some providers can do much less work (and
248 // get matches back more quickly). Taking advantage of this reduces flicker.
250 // NOTE: This comes after constructing |input_| above since that construction
251 // can change the text string (e.g. by stripping off a leading '?').
252 const bool minimal_changes = (input_.text() == old_input_text) &&
253 (input_.want_asynchronous_matches() == old_want_asynchronous_matches);
255 expire_timer_.Stop();
256 stop_timer_.Stop();
258 // Start the new query.
259 in_start_ = true;
260 base::TimeTicks start_time = base::TimeTicks::Now();
261 for (Providers::iterator i(providers_.begin()); i != providers_.end(); ++i) {
262 // TODO(mpearson): Remove timing code once bugs 178705 / 237703 / 168933
263 // are resolved.
264 base::TimeTicks provider_start_time = base::TimeTicks::Now();
266 // Call Start() on ZeroSuggestProvider with an INVALID AutocompleteInput
267 // to clear out zero-suggest |matches_|.
268 if (i->get() == zero_suggest_provider_)
269 (*i)->Start(AutocompleteInput(), minimal_changes);
270 else
271 (*i)->Start(input_, minimal_changes);
273 if (!input.want_asynchronous_matches())
274 DCHECK((*i)->done());
275 base::TimeTicks provider_end_time = base::TimeTicks::Now();
276 std::string name = std::string("Omnibox.ProviderTime.") + (*i)->GetName();
277 base::HistogramBase* counter = base::Histogram::FactoryGet(
278 name, 1, 5000, 20, base::Histogram::kUmaTargetedHistogramFlag);
279 counter->Add(static_cast<int>(
280 (provider_end_time - provider_start_time).InMilliseconds()));
282 if (input.want_asynchronous_matches() && (input.text().length() < 6)) {
283 base::TimeTicks end_time = base::TimeTicks::Now();
284 std::string name = "Omnibox.QueryTime." + base::IntToString(
285 input.text().length());
286 base::HistogramBase* counter = base::Histogram::FactoryGet(
287 name, 1, 1000, 50, base::Histogram::kUmaTargetedHistogramFlag);
288 counter->Add(static_cast<int>((end_time - start_time).InMilliseconds()));
290 in_start_ = false;
291 CheckIfDone();
292 // The second true forces saying the default match has changed.
293 // This triggers the edit model to update things such as the inline
294 // autocomplete state. In particular, if the user has typed a key
295 // since the last notification, and we're now re-running
296 // autocomplete, then we need to update the inline autocompletion
297 // even if the current match is for the same URL as the last run's
298 // default match. Likewise, the controller doesn't know what's
299 // happened in the edit since the last time it ran autocomplete.
300 // The user might have selected all the text and hit delete, then
301 // typed a new character. The selection and delete won't send any
302 // signals to the controller so it doesn't realize that anything was
303 // cleared or changed. Even if the default match hasn't changed, we
304 // need the edit model to update the display.
305 UpdateResult(false, true);
307 if (!done_) {
308 StartExpireTimer();
309 StartStopTimer();
313 void AutocompleteController::Stop(bool clear_result) {
314 for (Providers::const_iterator i(providers_.begin()); i != providers_.end();
315 ++i) {
316 (*i)->Stop(clear_result);
319 expire_timer_.Stop();
320 stop_timer_.Stop();
321 done_ = true;
322 if (clear_result && !result_.empty()) {
323 result_.Reset();
324 // NOTE: We pass in false since we're trying to only clear the popup, not
325 // touch the edit... this is all a mess and should be cleaned up :(
326 NotifyChanged(false);
330 void AutocompleteController::OnOmniboxFocused(const AutocompleteInput& input) {
331 DCHECK(!in_start_); // We should not be already running a query.
333 // Call Start() on all prefix-based providers with an INVALID
334 // AutocompleteInput to clear out cached |matches_|, which ensures that
335 // they aren't used with zero suggest.
336 for (Providers::iterator i(providers_.begin()); i != providers_.end(); ++i) {
337 if ((*i)->ProvidesMatchesOnOmniboxFocus())
338 (*i)->Start(input, false);
339 else
340 (*i)->Start(AutocompleteInput(), false);
343 UpdateResult(false, false);
346 void AutocompleteController::DeleteMatch(const AutocompleteMatch& match) {
347 DCHECK(match.SupportsDeletion());
349 // Delete duplicate matches attached to the main match first.
350 for (ACMatches::const_iterator it(match.duplicate_matches.begin());
351 it != match.duplicate_matches.end(); ++it) {
352 if (it->deletable)
353 it->provider->DeleteMatch(*it);
356 if (match.deletable)
357 match.provider->DeleteMatch(match);
359 OnProviderUpdate(true);
361 // If we're not done, we might attempt to redisplay the deleted match. Make
362 // sure we aren't displaying it by removing any old entries.
363 ExpireCopiedEntries();
366 void AutocompleteController::ExpireCopiedEntries() {
367 // The first true makes UpdateResult() clear out the results and
368 // regenerate them, thus ensuring that no results from the previous
369 // result set remain.
370 UpdateResult(true, false);
373 void AutocompleteController::OnProviderUpdate(bool updated_matches) {
374 CheckIfDone();
375 // Multiple providers may provide synchronous results, so we only update the
376 // results if we're not in Start().
377 if (!in_start_ && (updated_matches || done_))
378 UpdateResult(false, false);
381 void AutocompleteController::AddProvidersInfo(
382 ProvidersInfo* provider_info) const {
383 provider_info->clear();
384 for (Providers::const_iterator i(providers_.begin()); i != providers_.end();
385 ++i) {
386 // Add per-provider info, if any.
387 (*i)->AddProviderInfo(provider_info);
389 // This is also a good place to put code to add info that you want to
390 // add for every provider.
394 void AutocompleteController::ResetSession() {
395 for (Providers::const_iterator i(providers_.begin()); i != providers_.end();
396 ++i)
397 (*i)->ResetSession();
400 void AutocompleteController::UpdateMatchDestinationURLWithQueryFormulationTime(
401 base::TimeDelta query_formulation_time,
402 AutocompleteMatch* match) const {
403 if (!match->search_terms_args.get() ||
404 match->search_terms_args->assisted_query_stats.empty())
405 return;
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(),
414 (search_provider_ &&
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 UpdateMatchDestinationURL(search_terms_args, match);
422 void AutocompleteController::UpdateMatchDestinationURL(
423 const TemplateURLRef::SearchTermsArgs& search_terms_args,
424 AutocompleteMatch* match) const {
425 TemplateURL* template_url = match->GetTemplateURL(
426 template_url_service_, false);
427 if (!template_url)
428 return;
430 match->destination_url = GURL(template_url->url_ref().ReplaceSearchTerms(
431 search_terms_args, template_url_service_->search_terms_data()));
434 void AutocompleteController::UpdateResult(
435 bool regenerate_result,
436 bool force_notify_default_match_changed) {
437 const bool last_default_was_valid = result_.default_match() != result_.end();
438 // The following three variables are only set and used if
439 // |last_default_was_valid|.
440 base::string16 last_default_fill_into_edit, last_default_keyword,
441 last_default_associated_keyword;
442 if (last_default_was_valid) {
443 last_default_fill_into_edit = result_.default_match()->fill_into_edit;
444 last_default_keyword = result_.default_match()->keyword;
445 if (result_.default_match()->associated_keyword != NULL)
446 last_default_associated_keyword =
447 result_.default_match()->associated_keyword->keyword;
450 if (regenerate_result)
451 result_.Reset();
453 AutocompleteResult last_result;
454 last_result.Swap(&result_);
456 for (Providers::const_iterator i(providers_.begin());
457 i != providers_.end(); ++i)
458 result_.AppendMatches((*i)->matches());
460 // Sort the matches and trim to a small number of "best" matches.
461 result_.SortAndCull(input_, template_url_service_);
463 // Need to validate before invoking CopyOldMatches as the old matches are not
464 // valid against the current input.
465 #ifndef NDEBUG
466 result_.Validate();
467 #endif
469 if (!done_) {
470 // This conditional needs to match the conditional in Start that invokes
471 // StartExpireTimer.
472 result_.CopyOldMatches(input_, last_result, template_url_service_);
475 UpdateKeywordDescriptions(&result_);
476 UpdateAssociatedKeywords(&result_);
477 UpdateAssistedQueryStats(&result_);
478 if (search_provider_)
479 search_provider_->RegisterDisplayedAnswers(result_);
481 const bool default_is_valid = result_.default_match() != result_.end();
482 base::string16 default_associated_keyword;
483 if (default_is_valid &&
484 (result_.default_match()->associated_keyword != NULL)) {
485 default_associated_keyword =
486 result_.default_match()->associated_keyword->keyword;
488 // We've gotten async results. Send notification that the default match
489 // updated if fill_into_edit, associated_keyword, or keyword differ. (The
490 // second can change if we've just started Chrome and the keyword database
491 // finishes loading while processing this request. The third can change
492 // if we swapped from interpreting the input as a search--which gets
493 // labeled with the default search provider's keyword--to a URL.)
494 // We don't check the URL as that may change for the default match
495 // even though the fill into edit hasn't changed (see SearchProvider
496 // for one case of this).
497 const bool notify_default_match =
498 (last_default_was_valid != default_is_valid) ||
499 (last_default_was_valid &&
500 ((result_.default_match()->fill_into_edit !=
501 last_default_fill_into_edit) ||
502 (default_associated_keyword != last_default_associated_keyword) ||
503 (result_.default_match()->keyword != last_default_keyword)));
504 if (notify_default_match)
505 last_time_default_match_changed_ = base::TimeTicks::Now();
507 NotifyChanged(force_notify_default_match_changed || notify_default_match);
510 void AutocompleteController::UpdateAssociatedKeywords(
511 AutocompleteResult* result) {
512 if (!keyword_provider_)
513 return;
515 // Determine if the user's input is an exact keyword match.
516 base::string16 exact_keyword = keyword_provider_->GetKeywordForText(
517 TemplateURLService::CleanUserInputKeyword(input_.text()));
519 std::set<base::string16> keywords;
520 for (ACMatches::iterator match(result->begin()); match != result->end();
521 ++match) {
522 base::string16 keyword(
523 match->GetSubstitutingExplicitlyInvokedKeyword(template_url_service_));
524 if (!keyword.empty()) {
525 keywords.insert(keyword);
526 continue;
529 // When the user has typed an exact keyword, we want tab-to-search on the
530 // default match to select that keyword, even if the match
531 // inline-autocompletes to a different keyword. (This prevents inline
532 // autocompletions from blocking a user's attempts to use an explicitly-set
533 // keyword of their own creation.) So use |exact_keyword| if it's
534 // available.
535 if (!exact_keyword.empty() && !keywords.count(exact_keyword)) {
536 keywords.insert(exact_keyword);
537 match->associated_keyword.reset(new AutocompleteMatch(
538 keyword_provider_->CreateVerbatimMatch(exact_keyword,
539 exact_keyword, input_)));
540 continue;
543 // Otherwise, set a match's associated keyword based on the match's
544 // fill_into_edit, which should take inline autocompletions into account.
545 keyword = keyword_provider_->GetKeywordForText(match->fill_into_edit);
547 // Only add the keyword if the match does not have a duplicate keyword with
548 // a more relevant match.
549 if (!keyword.empty() && !keywords.count(keyword)) {
550 keywords.insert(keyword);
551 match->associated_keyword.reset(new AutocompleteMatch(
552 keyword_provider_->CreateVerbatimMatch(match->fill_into_edit,
553 keyword, input_)));
554 } else {
555 match->associated_keyword.reset();
560 void AutocompleteController::UpdateKeywordDescriptions(
561 AutocompleteResult* result) {
562 base::string16 last_keyword;
563 for (AutocompleteResult::iterator i(result->begin()); i != result->end();
564 ++i) {
565 if (AutocompleteMatch::IsSearchType(i->type)) {
566 if (AutocompleteMatchHasCustomDescription(*i))
567 continue;
568 i->description.clear();
569 i->description_class.clear();
570 DCHECK(!i->keyword.empty());
571 if (i->keyword != last_keyword) {
572 const TemplateURL* template_url =
573 i->GetTemplateURL(template_url_service_, false);
574 if (template_url) {
575 // For extension keywords, just make the description the extension
576 // name -- don't assume that the normal search keyword description is
577 // applicable.
578 i->description = template_url->AdjustedShortNameForLocaleDirection();
579 if (template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) {
580 i->description = l10n_util::GetStringFUTF16(
581 IDS_AUTOCOMPLETE_SEARCH_DESCRIPTION, i->description);
583 i->description_class.push_back(
584 ACMatchClassification(0, ACMatchClassification::DIM));
586 last_keyword = i->keyword;
588 } else {
589 last_keyword.clear();
594 void AutocompleteController::UpdateAssistedQueryStats(
595 AutocompleteResult* result) {
596 if (result->empty())
597 return;
599 // Build the impressions string (the AQS part after ".").
600 std::string autocompletions;
601 int count = 0;
602 size_t last_type = base::string16::npos;
603 size_t last_subtype = base::string16::npos;
604 for (ACMatches::iterator match(result->begin()); match != result->end();
605 ++match) {
606 size_t type = base::string16::npos;
607 size_t subtype = base::string16::npos;
608 AutocompleteMatchToAssistedQuery(
609 match->type, match->provider, &type, &subtype);
610 if (last_type != base::string16::npos &&
611 (type != last_type || subtype != last_subtype)) {
612 AppendAvailableAutocompletion(
613 last_type, last_subtype, count, &autocompletions);
614 count = 1;
615 } else {
616 count++;
618 last_type = type;
619 last_subtype = subtype;
621 AppendAvailableAutocompletion(
622 last_type, last_subtype, count, &autocompletions);
623 // Go over all matches and set AQS if the match supports it.
624 for (size_t index = 0; index < result->size(); ++index) {
625 AutocompleteMatch* match = result->match_at(index);
626 const TemplateURL* template_url =
627 match->GetTemplateURL(template_url_service_, false);
628 if (!template_url || !match->search_terms_args.get())
629 continue;
630 std::string selected_index;
631 // Prevent trivial suggestions from getting credit for being selected.
632 if (!IsTrivialAutocompletion(*match))
633 selected_index = base::StringPrintf("%" PRIuS, index);
634 match->search_terms_args->assisted_query_stats =
635 base::StringPrintf("chrome.%s.%s",
636 selected_index.c_str(),
637 autocompletions.c_str());
638 match->destination_url = GURL(template_url->url_ref().ReplaceSearchTerms(
639 *match->search_terms_args, template_url_service_->search_terms_data()));
643 void AutocompleteController::NotifyChanged(bool notify_default_match) {
644 if (delegate_)
645 delegate_->OnResultChanged(notify_default_match);
646 if (done_) {
647 content::NotificationService::current()->Notify(
648 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY,
649 content::Source<AutocompleteController>(this),
650 content::NotificationService::NoDetails());
654 void AutocompleteController::CheckIfDone() {
655 for (Providers::const_iterator i(providers_.begin()); i != providers_.end();
656 ++i) {
657 if (!(*i)->done()) {
658 done_ = false;
659 return;
662 done_ = true;
665 void AutocompleteController::StartExpireTimer() {
666 // Amount of time (in ms) between when the user stops typing and
667 // when we remove any copied entries. We do this from the time the
668 // user stopped typing as some providers (such as SearchProvider)
669 // wait for the user to stop typing before they initiate a query.
670 const int kExpireTimeMS = 500;
672 if (result_.HasCopiedMatches())
673 expire_timer_.Start(FROM_HERE,
674 base::TimeDelta::FromMilliseconds(kExpireTimeMS),
675 this, &AutocompleteController::ExpireCopiedEntries);
678 void AutocompleteController::StartStopTimer() {
679 stop_timer_.Start(FROM_HERE,
680 stop_timer_duration_,
681 base::Bind(&AutocompleteController::Stop,
682 base::Unretained(this),
683 false));