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