Chromium Code Reviews| 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/extension_app_provider.h" | 19 #include "chrome/browser/autocomplete/extension_app_provider.h" |
| 20 #include "chrome/browser/autocomplete/history_quick_provider.h" | 20 #include "chrome/browser/autocomplete/history_quick_provider.h" |
| 21 #include "chrome/browser/autocomplete/history_url_provider.h" | 21 #include "chrome/browser/autocomplete/history_url_provider.h" |
| 22 #include "chrome/browser/autocomplete/keyword_provider.h" | 22 #include "chrome/browser/autocomplete/keyword_provider.h" |
| 23 #include "chrome/browser/autocomplete/search_provider.h" | 23 #include "chrome/browser/autocomplete/search_provider.h" |
| 24 #include "chrome/browser/autocomplete/shortcuts_provider.h" | 24 #include "chrome/browser/autocomplete/shortcuts_provider.h" |
| 25 #include "chrome/browser/autocomplete/zero_suggest_provider.h" | 25 #include "chrome/browser/autocomplete/zero_suggest_provider.h" |
| 26 #include "chrome/browser/chrome_notification_types.h" | 26 #include "chrome/browser/chrome_notification_types.h" |
| 27 #include "chrome/browser/omnibox/omnibox_field_trial.h" | 27 #include "chrome/browser/omnibox/omnibox_field_trial.h" |
| 28 #include "chrome/browser/profiles/profile.h" | 28 #include "chrome/browser/profiles/profile.h" |
| 29 #include "chrome/browser/search/search.h" | 29 #include "chrome/browser/search/search.h" |
| 30 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 30 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" | 31 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" |
| 31 #include "components/search_engines/template_url.h" | 32 #include "components/search_engines/template_url.h" |
| 32 #include "content/public/browser/notification_service.h" | 33 #include "content/public/browser/notification_service.h" |
| 33 #include "grit/generated_resources.h" | 34 #include "grit/generated_resources.h" |
| 34 #include "grit/theme_resources.h" | 35 #include "grit/theme_resources.h" |
| 35 #include "ui/base/l10n/l10n_util.h" | 36 #include "ui/base/l10n/l10n_util.h" |
| 36 | 37 |
| 37 namespace { | 38 namespace { |
| 38 | 39 |
| 39 // Converts the given match to a type (and possibly subtype) based on the AQS | 40 // Converts the given match to a type (and possibly subtype) based on the AQS |
| (...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 399 | 400 |
| 400 void AutocompleteController::ResetSession() { | 401 void AutocompleteController::ResetSession() { |
| 401 for (ACProviders::const_iterator i(providers_.begin()); i != providers_.end(); | 402 for (ACProviders::const_iterator i(providers_.begin()); i != providers_.end(); |
| 402 ++i) | 403 ++i) |
| 403 (*i)->ResetSession(); | 404 (*i)->ResetSession(); |
| 404 } | 405 } |
| 405 | 406 |
| 406 void AutocompleteController::UpdateMatchDestinationURL( | 407 void AutocompleteController::UpdateMatchDestinationURL( |
| 407 base::TimeDelta query_formulation_time, | 408 base::TimeDelta query_formulation_time, |
| 408 AutocompleteMatch* match) const { | 409 AutocompleteMatch* match) const { |
| 409 TemplateURL* template_url = match->GetTemplateURL(profile_, false); | 410 TemplateURLService* service = |
| 411 TemplateURLServiceFactory::GetForProfile(profile_); | |
|
blundell
2014/06/26 09:00:00
This class will eventually need to have the Templa
Jun Mukai
2014/06/26 21:01:19
Agree, it would be better to pass TemplateURLServi
| |
| 412 TemplateURL* template_url = match->GetTemplateURL(service, false); | |
| 410 if (!template_url || !match->search_terms_args.get() || | 413 if (!template_url || !match->search_terms_args.get() || |
| 411 match->search_terms_args->assisted_query_stats.empty()) | 414 match->search_terms_args->assisted_query_stats.empty()) |
| 412 return; | 415 return; |
| 413 | 416 |
| 414 // Append the query formulation time (time from when the user first typed a | 417 // Append the query formulation time (time from when the user first typed a |
| 415 // character into the omnibox to when the user selected a query) and whether | 418 // character into the omnibox to when the user selected a query) and whether |
| 416 // a field trial has triggered to the AQS parameter. | 419 // a field trial has triggered to the AQS parameter. |
| 417 TemplateURLRef::SearchTermsArgs search_terms_args(*match->search_terms_args); | 420 TemplateURLRef::SearchTermsArgs search_terms_args(*match->search_terms_args); |
| 418 search_terms_args.assisted_query_stats += base::StringPrintf( | 421 search_terms_args.assisted_query_stats += base::StringPrintf( |
| 419 ".%" PRId64 "j%dj%d", | 422 ".%" PRId64 "j%dj%d", |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 446 if (regenerate_result) | 449 if (regenerate_result) |
| 447 result_.Reset(); | 450 result_.Reset(); |
| 448 | 451 |
| 449 AutocompleteResult last_result; | 452 AutocompleteResult last_result; |
| 450 last_result.Swap(&result_); | 453 last_result.Swap(&result_); |
| 451 | 454 |
| 452 for (ACProviders::const_iterator i(providers_.begin()); | 455 for (ACProviders::const_iterator i(providers_.begin()); |
| 453 i != providers_.end(); ++i) | 456 i != providers_.end(); ++i) |
| 454 result_.AppendMatches((*i)->matches()); | 457 result_.AppendMatches((*i)->matches()); |
| 455 | 458 |
| 459 TemplateURLService* template_url_service = | |
| 460 TemplateURLServiceFactory::GetForProfile(profile_); | |
| 461 | |
| 456 // Sort the matches and trim to a small number of "best" matches. | 462 // Sort the matches and trim to a small number of "best" matches. |
| 457 result_.SortAndCull(input_, profile_); | 463 result_.SortAndCull(input_, template_url_service); |
| 458 | 464 |
| 459 // Need to validate before invoking CopyOldMatches as the old matches are not | 465 // Need to validate before invoking CopyOldMatches as the old matches are not |
| 460 // valid against the current input. | 466 // valid against the current input. |
| 461 #ifndef NDEBUG | 467 #ifndef NDEBUG |
| 462 result_.Validate(); | 468 result_.Validate(); |
| 463 #endif | 469 #endif |
| 464 | 470 |
| 465 if (!done_) { | 471 if (!done_) { |
| 466 // This conditional needs to match the conditional in Start that invokes | 472 // This conditional needs to match the conditional in Start that invokes |
| 467 // StartExpireTimer. | 473 // StartExpireTimer. |
| 468 result_.CopyOldMatches(input_, last_result, profile_); | 474 result_.CopyOldMatches(input_, last_result, template_url_service); |
| 469 } | 475 } |
| 470 | 476 |
| 471 UpdateKeywordDescriptions(&result_); | 477 UpdateKeywordDescriptions(&result_); |
| 472 UpdateAssociatedKeywords(&result_); | 478 UpdateAssociatedKeywords(&result_); |
| 473 UpdateAssistedQueryStats(&result_); | 479 UpdateAssistedQueryStats(&result_); |
| 474 | 480 |
| 475 const bool default_is_valid = result_.default_match() != result_.end(); | 481 const bool default_is_valid = result_.default_match() != result_.end(); |
| 476 base::string16 default_associated_keyword; | 482 base::string16 default_associated_keyword; |
| 477 if (default_is_valid && | 483 if (default_is_valid && |
| 478 (result_.default_match()->associated_keyword != NULL)) { | 484 (result_.default_match()->associated_keyword != NULL)) { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 500 | 506 |
| 501 NotifyChanged(force_notify_default_match_changed || notify_default_match); | 507 NotifyChanged(force_notify_default_match_changed || notify_default_match); |
| 502 } | 508 } |
| 503 | 509 |
| 504 void AutocompleteController::UpdateAssociatedKeywords( | 510 void AutocompleteController::UpdateAssociatedKeywords( |
| 505 AutocompleteResult* result) { | 511 AutocompleteResult* result) { |
| 506 if (!keyword_provider_) | 512 if (!keyword_provider_) |
| 507 return; | 513 return; |
| 508 | 514 |
| 509 std::set<base::string16> keywords; | 515 std::set<base::string16> keywords; |
| 516 TemplateURLService* service = | |
| 517 TemplateURLServiceFactory::GetForProfile(profile_); | |
| 510 for (ACMatches::iterator match(result->begin()); match != result->end(); | 518 for (ACMatches::iterator match(result->begin()); match != result->end(); |
| 511 ++match) { | 519 ++match) { |
| 512 base::string16 keyword( | 520 base::string16 keyword( |
| 513 match->GetSubstitutingExplicitlyInvokedKeyword(profile_)); | 521 match->GetSubstitutingExplicitlyInvokedKeyword(service)); |
| 514 if (!keyword.empty()) { | 522 if (!keyword.empty()) { |
| 515 keywords.insert(keyword); | 523 keywords.insert(keyword); |
| 516 continue; | 524 continue; |
| 517 } | 525 } |
| 518 | 526 |
| 519 // Only add the keyword if the match does not have a duplicate keyword with | 527 // Only add the keyword if the match does not have a duplicate keyword with |
| 520 // a more relevant match. | 528 // a more relevant match. |
| 521 keyword = match->associated_keyword.get() ? | 529 keyword = match->associated_keyword.get() ? |
| 522 match->associated_keyword->keyword : | 530 match->associated_keyword->keyword : |
| 523 keyword_provider_->GetKeywordForText(match->fill_into_edit); | 531 keyword_provider_->GetKeywordForText(match->fill_into_edit); |
| 524 if (!keyword.empty() && !keywords.count(keyword)) { | 532 if (!keyword.empty() && !keywords.count(keyword)) { |
| 525 keywords.insert(keyword); | 533 keywords.insert(keyword); |
| 526 | 534 |
| 527 if (!match->associated_keyword.get()) | 535 if (!match->associated_keyword.get()) |
| 528 match->associated_keyword.reset(new AutocompleteMatch( | 536 match->associated_keyword.reset(new AutocompleteMatch( |
| 529 keyword_provider_->CreateVerbatimMatch(match->fill_into_edit, | 537 keyword_provider_->CreateVerbatimMatch(match->fill_into_edit, |
| 530 keyword, input_))); | 538 keyword, input_))); |
| 531 } else { | 539 } else { |
| 532 match->associated_keyword.reset(); | 540 match->associated_keyword.reset(); |
| 533 } | 541 } |
| 534 } | 542 } |
| 535 } | 543 } |
| 536 | 544 |
| 537 void AutocompleteController::UpdateKeywordDescriptions( | 545 void AutocompleteController::UpdateKeywordDescriptions( |
| 538 AutocompleteResult* result) { | 546 AutocompleteResult* result) { |
| 539 base::string16 last_keyword; | 547 base::string16 last_keyword; |
| 548 TemplateURLService* service = | |
| 549 TemplateURLServiceFactory::GetForProfile(profile_); | |
| 540 for (AutocompleteResult::iterator i(result->begin()); i != result->end(); | 550 for (AutocompleteResult::iterator i(result->begin()); i != result->end(); |
| 541 ++i) { | 551 ++i) { |
| 542 if (AutocompleteMatch::IsSearchType(i->type)) { | 552 if (AutocompleteMatch::IsSearchType(i->type)) { |
| 543 if (AutocompleteMatchHasCustomDescription(*i)) | 553 if (AutocompleteMatchHasCustomDescription(*i)) |
| 544 continue; | 554 continue; |
| 545 i->description.clear(); | 555 i->description.clear(); |
| 546 i->description_class.clear(); | 556 i->description_class.clear(); |
| 547 DCHECK(!i->keyword.empty()); | 557 DCHECK(!i->keyword.empty()); |
| 548 if (i->keyword != last_keyword) { | 558 if (i->keyword != last_keyword) { |
| 549 const TemplateURL* template_url = i->GetTemplateURL(profile_, false); | 559 const TemplateURL* template_url = i->GetTemplateURL(service, false); |
| 550 if (template_url) { | 560 if (template_url) { |
| 551 // For extension keywords, just make the description the extension | 561 // For extension keywords, just make the description the extension |
| 552 // name -- don't assume that the normal search keyword description is | 562 // name -- don't assume that the normal search keyword description is |
| 553 // applicable. | 563 // applicable. |
| 554 i->description = template_url->AdjustedShortNameForLocaleDirection(); | 564 i->description = template_url->AdjustedShortNameForLocaleDirection(); |
| 555 if (template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) { | 565 if (template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) { |
| 556 i->description = l10n_util::GetStringFUTF16( | 566 i->description = l10n_util::GetStringFUTF16( |
| 557 IDS_AUTOCOMPLETE_SEARCH_DESCRIPTION, i->description); | 567 IDS_AUTOCOMPLETE_SEARCH_DESCRIPTION, i->description); |
| 558 } | 568 } |
| 559 i->description_class.push_back( | 569 i->description_class.push_back( |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 590 count = 1; | 600 count = 1; |
| 591 } else { | 601 } else { |
| 592 count++; | 602 count++; |
| 593 } | 603 } |
| 594 last_type = type; | 604 last_type = type; |
| 595 last_subtype = subtype; | 605 last_subtype = subtype; |
| 596 } | 606 } |
| 597 AppendAvailableAutocompletion( | 607 AppendAvailableAutocompletion( |
| 598 last_type, last_subtype, count, &autocompletions); | 608 last_type, last_subtype, count, &autocompletions); |
| 599 // Go over all matches and set AQS if the match supports it. | 609 // Go over all matches and set AQS if the match supports it. |
| 610 TemplateURLService* service = | |
| 611 TemplateURLServiceFactory::GetForProfile(profile_); | |
| 600 for (size_t index = 0; index < result->size(); ++index) { | 612 for (size_t index = 0; index < result->size(); ++index) { |
| 601 AutocompleteMatch* match = result->match_at(index); | 613 AutocompleteMatch* match = result->match_at(index); |
| 602 const TemplateURL* template_url = match->GetTemplateURL(profile_, false); | 614 const TemplateURL* template_url = match->GetTemplateURL(service, false); |
| 603 if (!template_url || !match->search_terms_args.get()) | 615 if (!template_url || !match->search_terms_args.get()) |
| 604 continue; | 616 continue; |
| 605 std::string selected_index; | 617 std::string selected_index; |
| 606 // Prevent trivial suggestions from getting credit for being selected. | 618 // Prevent trivial suggestions from getting credit for being selected. |
| 607 if (!IsTrivialAutocompletion(*match)) | 619 if (!IsTrivialAutocompletion(*match)) |
| 608 selected_index = base::StringPrintf("%" PRIuS, index); | 620 selected_index = base::StringPrintf("%" PRIuS, index); |
| 609 match->search_terms_args->assisted_query_stats = | 621 match->search_terms_args->assisted_query_stats = |
| 610 base::StringPrintf("chrome.%s.%s", | 622 base::StringPrintf("chrome.%s.%s", |
| 611 selected_index.c_str(), | 623 selected_index.c_str(), |
| 612 autocompletions.c_str()); | 624 autocompletions.c_str()); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 650 this, &AutocompleteController::ExpireCopiedEntries); | 662 this, &AutocompleteController::ExpireCopiedEntries); |
| 651 } | 663 } |
| 652 | 664 |
| 653 void AutocompleteController::StartStopTimer() { | 665 void AutocompleteController::StartStopTimer() { |
| 654 stop_timer_.Start(FROM_HERE, | 666 stop_timer_.Start(FROM_HERE, |
| 655 stop_timer_duration_, | 667 stop_timer_duration_, |
| 656 base::Bind(&AutocompleteController::Stop, | 668 base::Bind(&AutocompleteController::Stop, |
| 657 base::Unretained(this), | 669 base::Unretained(this), |
| 658 false)); | 670 false)); |
| 659 } | 671 } |
| OLD | NEW |