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