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