Revert of Remove OneClickSigninHelper since it is no longer used. (patchset #5 id...
[chromium-blink-merge.git] / chrome / browser / autocomplete / autocomplete_controller.cc
blob65a53c7c6320361234fa5960cb9b0f76eb33135d
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_TAIL: {
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::NAVSUGGEST: {
88 // Do not set subtype here; subtype may have been set above.
89 *type = 5;
90 return;
92 case AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED: {
93 *subtype = 57;
94 return;
96 case AutocompleteMatchType::URL_WHAT_YOU_TYPED: {
97 *subtype = 58;
98 return;
100 case AutocompleteMatchType::SEARCH_HISTORY: {
101 *subtype = 59;
102 return;
104 case AutocompleteMatchType::HISTORY_URL: {
105 *subtype = 60;
106 return;
108 case AutocompleteMatchType::HISTORY_TITLE: {
109 *subtype = 61;
110 return;
112 case AutocompleteMatchType::HISTORY_BODY: {
113 *subtype = 62;
114 return;
116 case AutocompleteMatchType::HISTORY_KEYWORD: {
117 *subtype = 63;
118 return;
120 case AutocompleteMatchType::BOOKMARK_TITLE: {
121 *subtype = 65;
122 return;
124 case AutocompleteMatchType::NAVSUGGEST_PERSONALIZED: {
125 *subtype = 39;
126 return;
128 default: {
129 // This value indicates a native chrome suggestion with no named subtype
130 // (yet).
131 *subtype = 64;
136 // Appends available autocompletion of the given type, subtype, and number to
137 // the existing available autocompletions string, encoding according to the
138 // spec.
139 void 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);
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.
156 bool 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;
162 // Whether this autocomplete match type supports custom descriptions.
163 bool AutocompleteMatchHasCustomDescription(const AutocompleteMatch& match) {
164 return match.type == AutocompleteMatchType::SEARCH_SUGGEST_ENTITY ||
165 match.type == AutocompleteMatchType::SEARCH_SUGGEST_PROFILE;
168 } // namespace
170 AutocompleteController::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_);
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, template_url_service);
199 #if defined(ENABLE_EXTENSIONS)
200 keyword_provider_->set_extensions_delegate(
201 scoped_ptr<KeywordExtensionsDelegate>(
202 new KeywordExtensionsDelegateImpl(profile, keyword_provider_)));
203 #endif
204 providers_.push_back(keyword_provider_);
206 #endif
207 if (provider_types & AutocompleteProvider::TYPE_SEARCH) {
208 search_provider_ = new SearchProvider(
209 this, template_url_service, scoped_ptr<AutocompleteProviderClient>(
210 new ChromeAutocompleteProviderClient(profile)));
211 providers_.push_back(search_provider_);
213 if (provider_types & AutocompleteProvider::TYPE_SHORTCUTS)
214 providers_.push_back(new ShortcutsProvider(profile));
215 if (provider_types & AutocompleteProvider::TYPE_ZERO_SUGGEST) {
216 zero_suggest_provider_ = ZeroSuggestProvider::Create(
217 this, template_url_service, profile);
218 if (zero_suggest_provider_)
219 providers_.push_back(zero_suggest_provider_);
223 AutocompleteController::~AutocompleteController() {
224 // The providers may have tasks outstanding that hold refs to them. We need
225 // to ensure they won't call us back if they outlive us. (Practically,
226 // calling Stop() should also cancel those tasks and make it so that we hold
227 // the only refs.) We also don't want to bother notifying anyone of our
228 // result changes here, because the notification observer is in the midst of
229 // shutdown too, so we don't ask Stop() to clear |result_| (and notify).
230 result_.Reset(); // Not really necessary.
231 Stop(false);
234 void AutocompleteController::Start(const AutocompleteInput& input) {
235 const base::string16 old_input_text(input_.text());
236 const bool old_want_asynchronous_matches = input_.want_asynchronous_matches();
237 input_ = input;
239 // See if we can avoid rerunning autocomplete when the query hasn't changed
240 // much. When the user presses or releases the ctrl key, the desired_tld
241 // changes, and when the user finishes an IME composition, inline autocomplete
242 // may no longer be prevented. In both these cases the text itself hasn't
243 // changed since the last query, and some providers can do much less work (and
244 // get matches back more quickly). Taking advantage of this reduces flicker.
246 // NOTE: This comes after constructing |input_| above since that construction
247 // can change the text string (e.g. by stripping off a leading '?').
248 const bool minimal_changes = (input_.text() == old_input_text) &&
249 (input_.want_asynchronous_matches() == old_want_asynchronous_matches);
251 expire_timer_.Stop();
252 stop_timer_.Stop();
254 // Start the new query.
255 in_start_ = true;
256 base::TimeTicks start_time = base::TimeTicks::Now();
257 for (Providers::iterator i(providers_.begin()); i != providers_.end(); ++i) {
258 // TODO(mpearson): Remove timing code once bugs 178705 / 237703 / 168933
259 // are resolved.
260 base::TimeTicks provider_start_time = base::TimeTicks::Now();
261 (*i)->Start(input_, minimal_changes, false);
262 if (!input.want_asynchronous_matches())
263 DCHECK((*i)->done());
264 base::TimeTicks provider_end_time = base::TimeTicks::Now();
265 std::string name = std::string("Omnibox.ProviderTime.") + (*i)->GetName();
266 base::HistogramBase* counter = base::Histogram::FactoryGet(
267 name, 1, 5000, 20, base::Histogram::kUmaTargetedHistogramFlag);
268 counter->Add(static_cast<int>(
269 (provider_end_time - provider_start_time).InMilliseconds()));
271 if (input.want_asynchronous_matches() && (input.text().length() < 6)) {
272 base::TimeTicks end_time = base::TimeTicks::Now();
273 std::string name = "Omnibox.QueryTime." + base::IntToString(
274 input.text().length());
275 base::HistogramBase* counter = base::Histogram::FactoryGet(
276 name, 1, 1000, 50, base::Histogram::kUmaTargetedHistogramFlag);
277 counter->Add(static_cast<int>((end_time - start_time).InMilliseconds()));
279 in_start_ = false;
280 CheckIfDone();
281 // The second true forces saying the default match has changed.
282 // This triggers the edit model to update things such as the inline
283 // autocomplete state. In particular, if the user has typed a key
284 // since the last notification, and we're now re-running
285 // autocomplete, then we need to update the inline autocompletion
286 // even if the current match is for the same URL as the last run's
287 // default match. Likewise, the controller doesn't know what's
288 // happened in the edit since the last time it ran autocomplete.
289 // The user might have selected all the text and hit delete, then
290 // typed a new character. The selection and delete won't send any
291 // signals to the controller so it doesn't realize that anything was
292 // cleared or changed. Even if the default match hasn't changed, we
293 // need the edit model to update the display.
294 UpdateResult(false, true);
296 if (!done_) {
297 StartExpireTimer();
298 StartStopTimer();
302 void AutocompleteController::Stop(bool clear_result) {
303 StopHelper(clear_result, false);
306 void AutocompleteController::OnOmniboxFocused(const AutocompleteInput& input) {
307 DCHECK(!in_start_); // We should not be already running a query.
309 // Call Start() on all prefix-based providers with an INVALID
310 // AutocompleteInput to clear out cached |matches_|, which ensures that
311 // they aren't used with zero suggest.
312 for (Providers::iterator i(providers_.begin()); i != providers_.end(); ++i)
313 (*i)->Start(input, false, true);
315 UpdateResult(false, false);
318 void AutocompleteController::DeleteMatch(const AutocompleteMatch& match) {
319 DCHECK(match.SupportsDeletion());
321 // Delete duplicate matches attached to the main match first.
322 for (ACMatches::const_iterator it(match.duplicate_matches.begin());
323 it != match.duplicate_matches.end(); ++it) {
324 if (it->deletable)
325 it->provider->DeleteMatch(*it);
328 if (match.deletable)
329 match.provider->DeleteMatch(match);
331 OnProviderUpdate(true);
333 // If we're not done, we might attempt to redisplay the deleted match. Make
334 // sure we aren't displaying it by removing any old entries.
335 ExpireCopiedEntries();
338 void AutocompleteController::ExpireCopiedEntries() {
339 // The first true makes UpdateResult() clear out the results and
340 // regenerate them, thus ensuring that no results from the previous
341 // result set remain.
342 UpdateResult(true, false);
345 void AutocompleteController::OnProviderUpdate(bool updated_matches) {
346 CheckIfDone();
347 // Multiple providers may provide synchronous results, so we only update the
348 // results if we're not in Start().
349 if (!in_start_ && (updated_matches || done_))
350 UpdateResult(false, false);
353 void AutocompleteController::AddProvidersInfo(
354 ProvidersInfo* provider_info) const {
355 provider_info->clear();
356 for (Providers::const_iterator i(providers_.begin()); i != providers_.end();
357 ++i) {
358 // Add per-provider info, if any.
359 (*i)->AddProviderInfo(provider_info);
361 // This is also a good place to put code to add info that you want to
362 // add for every provider.
366 void AutocompleteController::ResetSession() {
367 for (Providers::const_iterator i(providers_.begin()); i != providers_.end();
368 ++i)
369 (*i)->ResetSession();
372 void AutocompleteController::UpdateMatchDestinationURLWithQueryFormulationTime(
373 base::TimeDelta query_formulation_time,
374 AutocompleteMatch* match) const {
375 if (!match->search_terms_args.get() ||
376 match->search_terms_args->assisted_query_stats.empty())
377 return;
379 // Append the query formulation time (time from when the user first typed a
380 // character into the omnibox to when the user selected a query) and whether
381 // a field trial has triggered to the AQS parameter.
382 TemplateURLRef::SearchTermsArgs search_terms_args(*match->search_terms_args);
383 search_terms_args.assisted_query_stats += base::StringPrintf(
384 ".%" PRId64 "j%dj%d",
385 query_formulation_time.InMilliseconds(),
386 (search_provider_ &&
387 search_provider_->field_trial_triggered_in_session()) ||
388 (zero_suggest_provider_ &&
389 zero_suggest_provider_->field_trial_triggered_in_session()),
390 input_.current_page_classification());
391 UpdateMatchDestinationURL(search_terms_args, match);
394 void AutocompleteController::UpdateMatchDestinationURL(
395 const TemplateURLRef::SearchTermsArgs& search_terms_args,
396 AutocompleteMatch* match) const {
397 TemplateURL* template_url = match->GetTemplateURL(
398 template_url_service_, false);
399 if (!template_url)
400 return;
402 match->destination_url = GURL(template_url->url_ref().ReplaceSearchTerms(
403 search_terms_args, template_url_service_->search_terms_data()));
406 void AutocompleteController::UpdateResult(
407 bool regenerate_result,
408 bool force_notify_default_match_changed) {
409 const bool last_default_was_valid = result_.default_match() != result_.end();
410 // The following three variables are only set and used if
411 // |last_default_was_valid|.
412 base::string16 last_default_fill_into_edit, last_default_keyword,
413 last_default_associated_keyword;
414 if (last_default_was_valid) {
415 last_default_fill_into_edit = result_.default_match()->fill_into_edit;
416 last_default_keyword = result_.default_match()->keyword;
417 if (result_.default_match()->associated_keyword != NULL)
418 last_default_associated_keyword =
419 result_.default_match()->associated_keyword->keyword;
422 if (regenerate_result)
423 result_.Reset();
425 AutocompleteResult last_result;
426 last_result.Swap(&result_);
428 for (Providers::const_iterator i(providers_.begin());
429 i != providers_.end(); ++i)
430 result_.AppendMatches((*i)->matches());
432 // Sort the matches and trim to a small number of "best" matches.
433 result_.SortAndCull(input_, template_url_service_);
435 // Need to validate before invoking CopyOldMatches as the old matches are not
436 // valid against the current input.
437 #ifndef NDEBUG
438 result_.Validate();
439 #endif
441 if (!done_) {
442 // This conditional needs to match the conditional in Start that invokes
443 // StartExpireTimer.
444 result_.CopyOldMatches(input_, last_result, template_url_service_);
447 UpdateKeywordDescriptions(&result_);
448 UpdateAssociatedKeywords(&result_);
449 UpdateAssistedQueryStats(&result_);
450 if (search_provider_)
451 search_provider_->RegisterDisplayedAnswers(result_);
453 const bool default_is_valid = result_.default_match() != result_.end();
454 base::string16 default_associated_keyword;
455 if (default_is_valid &&
456 (result_.default_match()->associated_keyword != NULL)) {
457 default_associated_keyword =
458 result_.default_match()->associated_keyword->keyword;
460 // We've gotten async results. Send notification that the default match
461 // updated if fill_into_edit, associated_keyword, or keyword differ. (The
462 // second can change if we've just started Chrome and the keyword database
463 // finishes loading while processing this request. The third can change
464 // if we swapped from interpreting the input as a search--which gets
465 // labeled with the default search provider's keyword--to a URL.)
466 // We don't check the URL as that may change for the default match
467 // even though the fill into edit hasn't changed (see SearchProvider
468 // for one case of this).
469 const bool notify_default_match =
470 (last_default_was_valid != default_is_valid) ||
471 (last_default_was_valid &&
472 ((result_.default_match()->fill_into_edit !=
473 last_default_fill_into_edit) ||
474 (default_associated_keyword != last_default_associated_keyword) ||
475 (result_.default_match()->keyword != last_default_keyword)));
476 if (notify_default_match)
477 last_time_default_match_changed_ = base::TimeTicks::Now();
479 NotifyChanged(force_notify_default_match_changed || notify_default_match);
482 void AutocompleteController::UpdateAssociatedKeywords(
483 AutocompleteResult* result) {
484 if (!keyword_provider_)
485 return;
487 // Determine if the user's input is an exact keyword match.
488 base::string16 exact_keyword = keyword_provider_->GetKeywordForText(
489 TemplateURLService::CleanUserInputKeyword(input_.text()));
491 std::set<base::string16> keywords;
492 for (ACMatches::iterator match(result->begin()); match != result->end();
493 ++match) {
494 base::string16 keyword(
495 match->GetSubstitutingExplicitlyInvokedKeyword(template_url_service_));
496 if (!keyword.empty()) {
497 keywords.insert(keyword);
498 continue;
501 // When the user has typed an exact keyword, we want tab-to-search on the
502 // default match to select that keyword, even if the match
503 // inline-autocompletes to a different keyword. (This prevents inline
504 // autocompletions from blocking a user's attempts to use an explicitly-set
505 // keyword of their own creation.) So use |exact_keyword| if it's
506 // available.
507 if (!exact_keyword.empty() && !keywords.count(exact_keyword)) {
508 keywords.insert(exact_keyword);
509 match->associated_keyword.reset(new AutocompleteMatch(
510 keyword_provider_->CreateVerbatimMatch(exact_keyword,
511 exact_keyword, input_)));
512 continue;
515 // Otherwise, set a match's associated keyword based on the match's
516 // fill_into_edit, which should take inline autocompletions into account.
517 keyword = keyword_provider_->GetKeywordForText(match->fill_into_edit);
519 // Only add the keyword if the match does not have a duplicate keyword with
520 // a more relevant match.
521 if (!keyword.empty() && !keywords.count(keyword)) {
522 keywords.insert(keyword);
523 match->associated_keyword.reset(new AutocompleteMatch(
524 keyword_provider_->CreateVerbatimMatch(match->fill_into_edit,
525 keyword, input_)));
526 } else {
527 match->associated_keyword.reset();
532 void AutocompleteController::UpdateKeywordDescriptions(
533 AutocompleteResult* result) {
534 base::string16 last_keyword;
535 for (AutocompleteResult::iterator i(result->begin()); i != result->end();
536 ++i) {
537 if (AutocompleteMatch::IsSearchType(i->type)) {
538 if (AutocompleteMatchHasCustomDescription(*i))
539 continue;
540 i->description.clear();
541 i->description_class.clear();
542 DCHECK(!i->keyword.empty());
543 if (i->keyword != last_keyword) {
544 const TemplateURL* template_url =
545 i->GetTemplateURL(template_url_service_, false);
546 if (template_url) {
547 // For extension keywords, just make the description the extension
548 // name -- don't assume that the normal search keyword description is
549 // applicable.
550 i->description = template_url->AdjustedShortNameForLocaleDirection();
551 if (template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) {
552 i->description = l10n_util::GetStringFUTF16(
553 IDS_AUTOCOMPLETE_SEARCH_DESCRIPTION, i->description);
555 i->description_class.push_back(
556 ACMatchClassification(0, ACMatchClassification::DIM));
558 last_keyword = i->keyword;
560 } else {
561 last_keyword.clear();
566 void AutocompleteController::UpdateAssistedQueryStats(
567 AutocompleteResult* result) {
568 if (result->empty())
569 return;
571 // Build the impressions string (the AQS part after ".").
572 std::string autocompletions;
573 int count = 0;
574 size_t last_type = base::string16::npos;
575 size_t last_subtype = base::string16::npos;
576 for (ACMatches::iterator match(result->begin()); match != result->end();
577 ++match) {
578 size_t type = base::string16::npos;
579 size_t subtype = base::string16::npos;
580 AutocompleteMatchToAssistedQuery(
581 match->type, match->provider, &type, &subtype);
582 if (last_type != base::string16::npos &&
583 (type != last_type || subtype != last_subtype)) {
584 AppendAvailableAutocompletion(
585 last_type, last_subtype, count, &autocompletions);
586 count = 1;
587 } else {
588 count++;
590 last_type = type;
591 last_subtype = subtype;
593 AppendAvailableAutocompletion(
594 last_type, last_subtype, count, &autocompletions);
595 // Go over all matches and set AQS if the match supports it.
596 for (size_t index = 0; index < result->size(); ++index) {
597 AutocompleteMatch* match = result->match_at(index);
598 const TemplateURL* template_url =
599 match->GetTemplateURL(template_url_service_, false);
600 if (!template_url || !match->search_terms_args.get())
601 continue;
602 std::string selected_index;
603 // Prevent trivial suggestions from getting credit for being selected.
604 if (!IsTrivialAutocompletion(*match))
605 selected_index = base::StringPrintf("%" PRIuS, index);
606 match->search_terms_args->assisted_query_stats =
607 base::StringPrintf("chrome.%s.%s",
608 selected_index.c_str(),
609 autocompletions.c_str());
610 match->destination_url = GURL(template_url->url_ref().ReplaceSearchTerms(
611 *match->search_terms_args, template_url_service_->search_terms_data()));
615 void AutocompleteController::NotifyChanged(bool notify_default_match) {
616 if (delegate_)
617 delegate_->OnResultChanged(notify_default_match);
618 if (done_) {
619 content::NotificationService::current()->Notify(
620 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY,
621 content::Source<AutocompleteController>(this),
622 content::NotificationService::NoDetails());
626 void AutocompleteController::CheckIfDone() {
627 for (Providers::const_iterator i(providers_.begin()); i != providers_.end();
628 ++i) {
629 if (!(*i)->done()) {
630 done_ = false;
631 return;
634 done_ = true;
637 void AutocompleteController::StartExpireTimer() {
638 // Amount of time (in ms) between when the user stops typing and
639 // when we remove any copied entries. We do this from the time the
640 // user stopped typing as some providers (such as SearchProvider)
641 // wait for the user to stop typing before they initiate a query.
642 const int kExpireTimeMS = 500;
644 if (result_.HasCopiedMatches())
645 expire_timer_.Start(FROM_HERE,
646 base::TimeDelta::FromMilliseconds(kExpireTimeMS),
647 this, &AutocompleteController::ExpireCopiedEntries);
650 void AutocompleteController::StartStopTimer() {
651 stop_timer_.Start(FROM_HERE,
652 stop_timer_duration_,
653 base::Bind(&AutocompleteController::StopHelper,
654 base::Unretained(this),
655 false, true));
658 void AutocompleteController::StopHelper(bool clear_result,
659 bool due_to_user_inactivity) {
660 for (Providers::const_iterator i(providers_.begin()); i != providers_.end();
661 ++i) {
662 (*i)->Stop(clear_result, due_to_user_inactivity);
665 expire_timer_.Stop();
666 stop_timer_.Stop();
667 done_ = true;
668 if (clear_result && !result_.empty()) {
669 result_.Reset();
670 // NOTE: We pass in false since we're trying to only clear the popup, not
671 // touch the edit... this is all a mess and should be cleaned up :(
672 NotifyChanged(false);