| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/autocomplete/autocomplete_controller.h" | 5 #include "chrome/browser/autocomplete/autocomplete_controller.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" | 16 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" |
| 17 #include "chrome/browser/autocomplete/bookmark_provider.h" | 17 #include "chrome/browser/autocomplete/bookmark_provider.h" |
| 18 #include "chrome/browser/autocomplete/builtin_provider.h" | 18 #include "chrome/browser/autocomplete/builtin_provider.h" |
| 19 #include "chrome/browser/autocomplete/history_quick_provider.h" | 19 #include "chrome/browser/autocomplete/history_quick_provider.h" |
| 20 #include "chrome/browser/autocomplete/history_url_provider.h" | 20 #include "chrome/browser/autocomplete/history_url_provider.h" |
| 21 #include "chrome/browser/autocomplete/keyword_provider.h" | 21 #include "chrome/browser/autocomplete/keyword_provider.h" |
| 22 #include "chrome/browser/autocomplete/search_provider.h" | 22 #include "chrome/browser/autocomplete/search_provider.h" |
| 23 #include "chrome/browser/autocomplete/shortcuts_provider.h" | 23 #include "chrome/browser/autocomplete/shortcuts_provider.h" |
| 24 #include "chrome/browser/autocomplete/zero_suggest_provider.h" | 24 #include "chrome/browser/autocomplete/zero_suggest_provider.h" |
| 25 #include "chrome/browser/chrome_notification_types.h" | 25 #include "chrome/browser/chrome_notification_types.h" |
| 26 #include "chrome/browser/omnibox/omnibox_field_trial.h" | 26 #include "chrome/browser/omnibox/omnibox_field_trial.h" |
| 27 #include "chrome/browser/search_engines/template_url_service.h" | 27 #include "chrome/browser/profiles/profile.h" |
| 28 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" |
| 28 #include "components/search_engines/template_url.h" | 29 #include "components/search_engines/template_url.h" |
| 29 #include "content/public/browser/notification_service.h" | 30 #include "content/public/browser/notification_service.h" |
| 30 #include "grit/generated_resources.h" | 31 #include "grit/generated_resources.h" |
| 31 #include "grit/theme_resources.h" | 32 #include "grit/theme_resources.h" |
| 32 #include "ui/base/l10n/l10n_util.h" | 33 #include "ui/base/l10n/l10n_util.h" |
| 33 | 34 |
| 34 namespace { | 35 namespace { |
| 35 | 36 |
| 36 // Converts the given match to a type (and possibly subtype) based on the AQS | 37 // Converts the given match to a type (and possibly subtype) based on the AQS |
| 37 // specification. For more details, see | 38 // specification. For more details, see |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 // Whether this autocomplete match type supports custom descriptions. | 163 // Whether this autocomplete match type supports custom descriptions. |
| 163 bool AutocompleteMatchHasCustomDescription(const AutocompleteMatch& match) { | 164 bool AutocompleteMatchHasCustomDescription(const AutocompleteMatch& match) { |
| 164 return match.type == AutocompleteMatchType::SEARCH_SUGGEST_ENTITY || | 165 return match.type == AutocompleteMatchType::SEARCH_SUGGEST_ENTITY || |
| 165 match.type == AutocompleteMatchType::SEARCH_SUGGEST_PROFILE; | 166 match.type == AutocompleteMatchType::SEARCH_SUGGEST_PROFILE; |
| 166 } | 167 } |
| 167 | 168 |
| 168 } // namespace | 169 } // namespace |
| 169 | 170 |
| 170 AutocompleteController::AutocompleteController( | 171 AutocompleteController::AutocompleteController( |
| 171 Profile* profile, | 172 Profile* profile, |
| 172 TemplateURLService* template_url_service, | |
| 173 AutocompleteControllerDelegate* delegate, | 173 AutocompleteControllerDelegate* delegate, |
| 174 int provider_types) | 174 int provider_types) |
| 175 : delegate_(delegate), | 175 : delegate_(delegate), |
| 176 history_url_provider_(NULL), | 176 history_url_provider_(NULL), |
| 177 keyword_provider_(NULL), | 177 keyword_provider_(NULL), |
| 178 search_provider_(NULL), | 178 search_provider_(NULL), |
| 179 zero_suggest_provider_(NULL), | 179 zero_suggest_provider_(NULL), |
| 180 stop_timer_duration_(OmniboxFieldTrial::StopTimerFieldTrialDuration()), | 180 stop_timer_duration_(OmniboxFieldTrial::StopTimerFieldTrialDuration()), |
| 181 done_(true), | 181 done_(true), |
| 182 in_start_(false), | 182 in_start_(false), |
| 183 template_url_service_(template_url_service) { | 183 profile_(profile) { |
| 184 provider_types &= ~OmniboxFieldTrial::GetDisabledProviderTypes(); | 184 provider_types &= ~OmniboxFieldTrial::GetDisabledProviderTypes(); |
| 185 if (provider_types & AutocompleteProvider::TYPE_BOOKMARK) | 185 if (provider_types & AutocompleteProvider::TYPE_BOOKMARK) |
| 186 providers_.push_back(new BookmarkProvider(this, profile)); | 186 providers_.push_back(new BookmarkProvider(this, profile)); |
| 187 if (provider_types & AutocompleteProvider::TYPE_BUILTIN) | 187 if (provider_types & AutocompleteProvider::TYPE_BUILTIN) |
| 188 providers_.push_back(new BuiltinProvider(this, profile)); | 188 providers_.push_back(new BuiltinProvider(this, profile)); |
| 189 if (provider_types & AutocompleteProvider::TYPE_HISTORY_QUICK) | 189 if (provider_types & AutocompleteProvider::TYPE_HISTORY_QUICK) |
| 190 providers_.push_back(new HistoryQuickProvider(this, profile)); | 190 providers_.push_back(new HistoryQuickProvider(this, profile)); |
| 191 if (provider_types & AutocompleteProvider::TYPE_HISTORY_URL) { | 191 if (provider_types & AutocompleteProvider::TYPE_HISTORY_URL) { |
| 192 history_url_provider_ = new HistoryURLProvider(this, profile); | 192 history_url_provider_ = new HistoryURLProvider(this, profile); |
| 193 providers_.push_back(history_url_provider_); | 193 providers_.push_back(history_url_provider_); |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 | 395 |
| 396 void AutocompleteController::ResetSession() { | 396 void AutocompleteController::ResetSession() { |
| 397 for (ACProviders::const_iterator i(providers_.begin()); i != providers_.end(); | 397 for (ACProviders::const_iterator i(providers_.begin()); i != providers_.end(); |
| 398 ++i) | 398 ++i) |
| 399 (*i)->ResetSession(); | 399 (*i)->ResetSession(); |
| 400 } | 400 } |
| 401 | 401 |
| 402 void AutocompleteController::UpdateMatchDestinationURL( | 402 void AutocompleteController::UpdateMatchDestinationURL( |
| 403 base::TimeDelta query_formulation_time, | 403 base::TimeDelta query_formulation_time, |
| 404 AutocompleteMatch* match) const { | 404 AutocompleteMatch* match) const { |
| 405 TemplateURL* template_url = match->GetTemplateURL( | 405 TemplateURL* template_url = match->GetTemplateURL(profile_, false); |
| 406 template_url_service_, false); | |
| 407 if (!template_url || !match->search_terms_args.get() || | 406 if (!template_url || !match->search_terms_args.get() || |
| 408 match->search_terms_args->assisted_query_stats.empty()) | 407 match->search_terms_args->assisted_query_stats.empty()) |
| 409 return; | 408 return; |
| 410 | 409 |
| 411 // Append the query formulation time (time from when the user first typed a | 410 // Append the query formulation time (time from when the user first typed a |
| 412 // character into the omnibox to when the user selected a query) and whether | 411 // character into the omnibox to when the user selected a query) and whether |
| 413 // a field trial has triggered to the AQS parameter. | 412 // a field trial has triggered to the AQS parameter. |
| 414 TemplateURLRef::SearchTermsArgs search_terms_args(*match->search_terms_args); | 413 TemplateURLRef::SearchTermsArgs search_terms_args(*match->search_terms_args); |
| 415 search_terms_args.assisted_query_stats += base::StringPrintf( | 414 search_terms_args.assisted_query_stats += base::StringPrintf( |
| 416 ".%" PRId64 "j%dj%d", | 415 ".%" PRId64 "j%dj%d", |
| 417 query_formulation_time.InMilliseconds(), | 416 query_formulation_time.InMilliseconds(), |
| 418 (search_provider_ && | 417 (search_provider_ && |
| 419 search_provider_->field_trial_triggered_in_session()) || | 418 search_provider_->field_trial_triggered_in_session()) || |
| 420 (zero_suggest_provider_ && | 419 (zero_suggest_provider_ && |
| 421 zero_suggest_provider_->field_trial_triggered_in_session()), | 420 zero_suggest_provider_->field_trial_triggered_in_session()), |
| 422 input_.current_page_classification()); | 421 input_.current_page_classification()); |
| 423 match->destination_url = GURL(template_url->url_ref().ReplaceSearchTerms( | 422 match->destination_url = GURL(template_url->url_ref().ReplaceSearchTerms( |
| 424 search_terms_args, template_url_service_->search_terms_data())); | 423 search_terms_args, UIThreadSearchTermsData(profile_))); |
| 425 } | 424 } |
| 426 | 425 |
| 427 void AutocompleteController::UpdateResult( | 426 void AutocompleteController::UpdateResult( |
| 428 bool regenerate_result, | 427 bool regenerate_result, |
| 429 bool force_notify_default_match_changed) { | 428 bool force_notify_default_match_changed) { |
| 430 const bool last_default_was_valid = result_.default_match() != result_.end(); | 429 const bool last_default_was_valid = result_.default_match() != result_.end(); |
| 431 // The following three variables are only set and used if | 430 // The following three variables are only set and used if |
| 432 // |last_default_was_valid|. | 431 // |last_default_was_valid|. |
| 433 base::string16 last_default_fill_into_edit, last_default_keyword, | 432 base::string16 last_default_fill_into_edit, last_default_keyword, |
| 434 last_default_associated_keyword; | 433 last_default_associated_keyword; |
| 435 if (last_default_was_valid) { | 434 if (last_default_was_valid) { |
| 436 last_default_fill_into_edit = result_.default_match()->fill_into_edit; | 435 last_default_fill_into_edit = result_.default_match()->fill_into_edit; |
| 437 last_default_keyword = result_.default_match()->keyword; | 436 last_default_keyword = result_.default_match()->keyword; |
| 438 if (result_.default_match()->associated_keyword != NULL) | 437 if (result_.default_match()->associated_keyword != NULL) |
| 439 last_default_associated_keyword = | 438 last_default_associated_keyword = |
| 440 result_.default_match()->associated_keyword->keyword; | 439 result_.default_match()->associated_keyword->keyword; |
| 441 } | 440 } |
| 442 | 441 |
| 443 if (regenerate_result) | 442 if (regenerate_result) |
| 444 result_.Reset(); | 443 result_.Reset(); |
| 445 | 444 |
| 446 AutocompleteResult last_result; | 445 AutocompleteResult last_result; |
| 447 last_result.Swap(&result_); | 446 last_result.Swap(&result_); |
| 448 | 447 |
| 449 for (ACProviders::const_iterator i(providers_.begin()); | 448 for (ACProviders::const_iterator i(providers_.begin()); |
| 450 i != providers_.end(); ++i) | 449 i != providers_.end(); ++i) |
| 451 result_.AppendMatches((*i)->matches()); | 450 result_.AppendMatches((*i)->matches()); |
| 452 | 451 |
| 453 // Sort the matches and trim to a small number of "best" matches. | 452 // Sort the matches and trim to a small number of "best" matches. |
| 454 result_.SortAndCull(input_, template_url_service_); | 453 result_.SortAndCull(input_, profile_); |
| 455 | 454 |
| 456 // Need to validate before invoking CopyOldMatches as the old matches are not | 455 // Need to validate before invoking CopyOldMatches as the old matches are not |
| 457 // valid against the current input. | 456 // valid against the current input. |
| 458 #ifndef NDEBUG | 457 #ifndef NDEBUG |
| 459 result_.Validate(); | 458 result_.Validate(); |
| 460 #endif | 459 #endif |
| 461 | 460 |
| 462 if (!done_) { | 461 if (!done_) { |
| 463 // This conditional needs to match the conditional in Start that invokes | 462 // This conditional needs to match the conditional in Start that invokes |
| 464 // StartExpireTimer. | 463 // StartExpireTimer. |
| 465 result_.CopyOldMatches(input_, last_result, template_url_service_); | 464 result_.CopyOldMatches(input_, last_result, profile_); |
| 466 } | 465 } |
| 467 | 466 |
| 468 UpdateKeywordDescriptions(&result_); | 467 UpdateKeywordDescriptions(&result_); |
| 469 UpdateAssociatedKeywords(&result_); | 468 UpdateAssociatedKeywords(&result_); |
| 470 UpdateAssistedQueryStats(&result_); | 469 UpdateAssistedQueryStats(&result_); |
| 471 | 470 |
| 472 const bool default_is_valid = result_.default_match() != result_.end(); | 471 const bool default_is_valid = result_.default_match() != result_.end(); |
| 473 base::string16 default_associated_keyword; | 472 base::string16 default_associated_keyword; |
| 474 if (default_is_valid && | 473 if (default_is_valid && |
| 475 (result_.default_match()->associated_keyword != NULL)) { | 474 (result_.default_match()->associated_keyword != NULL)) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 500 | 499 |
| 501 void AutocompleteController::UpdateAssociatedKeywords( | 500 void AutocompleteController::UpdateAssociatedKeywords( |
| 502 AutocompleteResult* result) { | 501 AutocompleteResult* result) { |
| 503 if (!keyword_provider_) | 502 if (!keyword_provider_) |
| 504 return; | 503 return; |
| 505 | 504 |
| 506 std::set<base::string16> keywords; | 505 std::set<base::string16> keywords; |
| 507 for (ACMatches::iterator match(result->begin()); match != result->end(); | 506 for (ACMatches::iterator match(result->begin()); match != result->end(); |
| 508 ++match) { | 507 ++match) { |
| 509 base::string16 keyword( | 508 base::string16 keyword( |
| 510 match->GetSubstitutingExplicitlyInvokedKeyword(template_url_service_)); | 509 match->GetSubstitutingExplicitlyInvokedKeyword(profile_)); |
| 511 if (!keyword.empty()) { | 510 if (!keyword.empty()) { |
| 512 keywords.insert(keyword); | 511 keywords.insert(keyword); |
| 513 continue; | 512 continue; |
| 514 } | 513 } |
| 515 | 514 |
| 516 // Only add the keyword if the match does not have a duplicate keyword with | 515 // Only add the keyword if the match does not have a duplicate keyword with |
| 517 // a more relevant match. | 516 // a more relevant match. |
| 518 keyword = match->associated_keyword.get() ? | 517 keyword = match->associated_keyword.get() ? |
| 519 match->associated_keyword->keyword : | 518 match->associated_keyword->keyword : |
| 520 keyword_provider_->GetKeywordForText(match->fill_into_edit); | 519 keyword_provider_->GetKeywordForText(match->fill_into_edit); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 536 base::string16 last_keyword; | 535 base::string16 last_keyword; |
| 537 for (AutocompleteResult::iterator i(result->begin()); i != result->end(); | 536 for (AutocompleteResult::iterator i(result->begin()); i != result->end(); |
| 538 ++i) { | 537 ++i) { |
| 539 if (AutocompleteMatch::IsSearchType(i->type)) { | 538 if (AutocompleteMatch::IsSearchType(i->type)) { |
| 540 if (AutocompleteMatchHasCustomDescription(*i)) | 539 if (AutocompleteMatchHasCustomDescription(*i)) |
| 541 continue; | 540 continue; |
| 542 i->description.clear(); | 541 i->description.clear(); |
| 543 i->description_class.clear(); | 542 i->description_class.clear(); |
| 544 DCHECK(!i->keyword.empty()); | 543 DCHECK(!i->keyword.empty()); |
| 545 if (i->keyword != last_keyword) { | 544 if (i->keyword != last_keyword) { |
| 546 const TemplateURL* template_url = | 545 const TemplateURL* template_url = i->GetTemplateURL(profile_, false); |
| 547 i->GetTemplateURL(template_url_service_, false); | |
| 548 if (template_url) { | 546 if (template_url) { |
| 549 // For extension keywords, just make the description the extension | 547 // For extension keywords, just make the description the extension |
| 550 // name -- don't assume that the normal search keyword description is | 548 // name -- don't assume that the normal search keyword description is |
| 551 // applicable. | 549 // applicable. |
| 552 i->description = template_url->AdjustedShortNameForLocaleDirection(); | 550 i->description = template_url->AdjustedShortNameForLocaleDirection(); |
| 553 if (template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) { | 551 if (template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) { |
| 554 i->description = l10n_util::GetStringFUTF16( | 552 i->description = l10n_util::GetStringFUTF16( |
| 555 IDS_AUTOCOMPLETE_SEARCH_DESCRIPTION, i->description); | 553 IDS_AUTOCOMPLETE_SEARCH_DESCRIPTION, i->description); |
| 556 } | 554 } |
| 557 i->description_class.push_back( | 555 i->description_class.push_back( |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 590 count++; | 588 count++; |
| 591 } | 589 } |
| 592 last_type = type; | 590 last_type = type; |
| 593 last_subtype = subtype; | 591 last_subtype = subtype; |
| 594 } | 592 } |
| 595 AppendAvailableAutocompletion( | 593 AppendAvailableAutocompletion( |
| 596 last_type, last_subtype, count, &autocompletions); | 594 last_type, last_subtype, count, &autocompletions); |
| 597 // Go over all matches and set AQS if the match supports it. | 595 // Go over all matches and set AQS if the match supports it. |
| 598 for (size_t index = 0; index < result->size(); ++index) { | 596 for (size_t index = 0; index < result->size(); ++index) { |
| 599 AutocompleteMatch* match = result->match_at(index); | 597 AutocompleteMatch* match = result->match_at(index); |
| 600 const TemplateURL* template_url = | 598 const TemplateURL* template_url = match->GetTemplateURL(profile_, false); |
| 601 match->GetTemplateURL(template_url_service_, false); | |
| 602 if (!template_url || !match->search_terms_args.get()) | 599 if (!template_url || !match->search_terms_args.get()) |
| 603 continue; | 600 continue; |
| 604 std::string selected_index; | 601 std::string selected_index; |
| 605 // Prevent trivial suggestions from getting credit for being selected. | 602 // Prevent trivial suggestions from getting credit for being selected. |
| 606 if (!IsTrivialAutocompletion(*match)) | 603 if (!IsTrivialAutocompletion(*match)) |
| 607 selected_index = base::StringPrintf("%" PRIuS, index); | 604 selected_index = base::StringPrintf("%" PRIuS, index); |
| 608 match->search_terms_args->assisted_query_stats = | 605 match->search_terms_args->assisted_query_stats = |
| 609 base::StringPrintf("chrome.%s.%s", | 606 base::StringPrintf("chrome.%s.%s", |
| 610 selected_index.c_str(), | 607 selected_index.c_str(), |
| 611 autocompletions.c_str()); | 608 autocompletions.c_str()); |
| 612 match->destination_url = GURL(template_url->url_ref().ReplaceSearchTerms( | 609 match->destination_url = GURL(template_url->url_ref().ReplaceSearchTerms( |
| 613 *match->search_terms_args, template_url_service_->search_terms_data())); | 610 *match->search_terms_args, UIThreadSearchTermsData(profile_))); |
| 614 } | 611 } |
| 615 } | 612 } |
| 616 | 613 |
| 617 void AutocompleteController::NotifyChanged(bool notify_default_match) { | 614 void AutocompleteController::NotifyChanged(bool notify_default_match) { |
| 618 if (delegate_) | 615 if (delegate_) |
| 619 delegate_->OnResultChanged(notify_default_match); | 616 delegate_->OnResultChanged(notify_default_match); |
| 620 if (done_) { | 617 if (done_) { |
| 621 content::NotificationService::current()->Notify( | 618 content::NotificationService::current()->Notify( |
| 622 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY, | 619 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY, |
| 623 content::Source<AutocompleteController>(this), | 620 content::Source<AutocompleteController>(this), |
| (...skipping 25 matching lines...) Expand all Loading... |
| 649 this, &AutocompleteController::ExpireCopiedEntries); | 646 this, &AutocompleteController::ExpireCopiedEntries); |
| 650 } | 647 } |
| 651 | 648 |
| 652 void AutocompleteController::StartStopTimer() { | 649 void AutocompleteController::StartStopTimer() { |
| 653 stop_timer_.Start(FROM_HERE, | 650 stop_timer_.Start(FROM_HERE, |
| 654 stop_timer_duration_, | 651 stop_timer_duration_, |
| 655 base::Bind(&AutocompleteController::Stop, | 652 base::Bind(&AutocompleteController::Stop, |
| 656 base::Unretained(this), | 653 base::Unretained(this), |
| 657 false)); | 654 false)); |
| 658 } | 655 } |
| OLD | NEW |