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