Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: chrome/browser/autocomplete/history_url_provider.cc

Issue 333273009: Don't call AutocompleteInput::Parse() on a background thread, part 2. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review comments Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/history_url_provider.h" 5 #include "chrome/browser/autocomplete/history_url_provider.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 const AutocompleteMatch& what_you_typed_match, 410 const AutocompleteMatch& what_you_typed_match,
411 const std::string& languages, 411 const std::string& languages,
412 TemplateURL* default_search_provider, 412 TemplateURL* default_search_provider,
413 const SearchTermsData& search_terms_data) 413 const SearchTermsData& search_terms_data)
414 : message_loop(base::MessageLoop::current()), 414 : message_loop(base::MessageLoop::current()),
415 input(input), 415 input(input),
416 prevent_inline_autocomplete(input.prevent_inline_autocomplete()), 416 prevent_inline_autocomplete(input.prevent_inline_autocomplete()),
417 trim_http(trim_http), 417 trim_http(trim_http),
418 what_you_typed_match(what_you_typed_match), 418 what_you_typed_match(what_you_typed_match),
419 failed(false), 419 failed(false),
420 exact_suggestion_is_in_history(false),
421 promote_type(NEITHER),
420 languages(languages), 422 languages(languages),
421 dont_suggest_exact_input(false),
422 default_search_provider(default_search_provider ? 423 default_search_provider(default_search_provider ?
423 new TemplateURL(default_search_provider->data()) : NULL), 424 new TemplateURL(default_search_provider->data()) : NULL),
424 search_terms_data(new SearchTermsDataSnapshot(search_terms_data)) { 425 search_terms_data(new SearchTermsDataSnapshot(search_terms_data)) {
425 } 426 }
426 427
427 HistoryURLProviderParams::~HistoryURLProviderParams() { 428 HistoryURLProviderParams::~HistoryURLProviderParams() {
428 } 429 }
429 430
430 HistoryURLProvider::HistoryURLProvider(AutocompleteProviderListener* listener, 431 HistoryURLProvider::HistoryURLProvider(AutocompleteProviderListener* listener,
431 Profile* profile) 432 Profile* profile)
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
520 history::URLDatabase* url_db = history_service->InMemoryDatabase(); 521 history::URLDatabase* url_db = history_service->InMemoryDatabase();
521 // url_db can be NULL if it hasn't finished initializing (or failed to 522 // url_db can be NULL if it hasn't finished initializing (or failed to
522 // initialize). In this case all we can do is fall back on the second 523 // initialize). In this case all we can do is fall back on the second
523 // pass. 524 // pass.
524 // 525 //
525 // TODO(pkasting): We should just block here until this loads. Any time 526 // TODO(pkasting): We should just block here until this loads. Any time
526 // someone unloads the history backend, we'll get inconsistent inline 527 // someone unloads the history backend, we'll get inconsistent inline
527 // autocomplete behavior here. 528 // autocomplete behavior here.
528 if (url_db) { 529 if (url_db) {
529 DoAutocomplete(NULL, url_db, params.get()); 530 DoAutocomplete(NULL, url_db, params.get());
530 // params->matches now has the matches we should expose to the provider.
531 // Pass 2 expects a "clean slate" set of matches.
532 matches_.clear(); 531 matches_.clear();
533 matches_.swap(params->matches); 532 PromoteMatchIfNecessary(*params);
534 UpdateStarredStateOfMatches(); 533 UpdateStarredStateOfMatches();
535 // Reset the WYT match in |params| so that both passes get the same input 534 // Reset the WYT match in |params| so that both passes get the same input
536 // state, since DoAutocomplete() may have modified it. 535 // state, since DoAutocomplete() may have modified it.
537 params->what_you_typed_match = what_you_typed_match; 536 params->what_you_typed_match = what_you_typed_match;
Mark P 2014/06/19 03:53:14 I think you should explicitly comment something li
Peter Kasting 2014/06/19 20:55:22 I removed this as discussed in person. I also add
538 } 537 }
539 538
540 // Pass 2: Ask the history service to call us back on the history thread, 539 // Pass 2: Ask the history service to call us back on the history thread,
541 // where we can read the full on-disk DB. 540 // where we can read the full on-disk DB.
542 if (input.want_asynchronous_matches()) { 541 if (input.want_asynchronous_matches()) {
543 done_ = false; 542 done_ = false;
544 params_ = params.release(); // This object will be destroyed in 543 params_ = params.release(); // This object will be destroyed in
545 // QueryComplete() once we're done with it. 544 // QueryComplete() once we're done with it.
546 history_service->ScheduleAutocomplete(this, params_); 545 history_service->ScheduleAutocomplete(this, params_);
547 } 546 }
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
682 description_matches, offsets, description_word_starts, 0, 681 description_matches, offsets, description_word_starts, 0,
683 std::string::npos); 682 std::string::npos);
684 return SpansFromTermMatch( 683 return SpansFromTermMatch(
685 description_matches, clean_description.length(), false); 684 description_matches, clean_description.length(), false);
686 } 685 }
687 686
688 void HistoryURLProvider::DoAutocomplete(history::HistoryBackend* backend, 687 void HistoryURLProvider::DoAutocomplete(history::HistoryBackend* backend,
689 history::URLDatabase* db, 688 history::URLDatabase* db,
690 HistoryURLProviderParams* params) { 689 HistoryURLProviderParams* params) {
691 // Get the matching URLs from the DB. 690 // Get the matching URLs from the DB.
691 params->matches.clear();
692 history::URLRows url_matches; 692 history::URLRows url_matches;
693 history::HistoryMatches history_matches;
694
695 const URLPrefixes& prefixes = URLPrefix::GetURLPrefixes(); 693 const URLPrefixes& prefixes = URLPrefix::GetURLPrefixes();
696 for (URLPrefixes::const_iterator i(prefixes.begin()); i != prefixes.end(); 694 for (URLPrefixes::const_iterator i(prefixes.begin()); i != prefixes.end();
697 ++i) { 695 ++i) {
698 if (params->cancel_flag.IsSet()) 696 if (params->cancel_flag.IsSet())
699 return; // Canceled in the middle of a query, give up. 697 return; // Canceled in the middle of a query, give up.
700 698
701 // We only need kMaxMatches results in the end, but before we get there we 699 // We only need kMaxMatches results in the end, but before we get there we
702 // need to promote lower-quality matches that are prefixes of higher-quality 700 // need to promote lower-quality matches that are prefixes of higher-quality
703 // matches, and remove lower-quality redirects. So we ask for more results 701 // matches, and remove lower-quality redirects. So we ask for more results
704 // than we need, of every prefix type, in hopes this will give us far more 702 // than we need, of every prefix type, in hopes this will give us far more
705 // than enough to work with. CullRedirects() will then reduce the list to 703 // than enough to work with. CullRedirects() will then reduce the list to
706 // the best kMaxMatches results. 704 // the best kMaxMatches results.
707 db->AutocompleteForPrefix( 705 db->AutocompleteForPrefix(
708 base::UTF16ToUTF8(i->prefix + params->input.text()), kMaxMatches * 2, 706 base::UTF16ToUTF8(i->prefix + params->input.text()), kMaxMatches * 2,
709 !backend, &url_matches); 707 !backend, &url_matches);
710 for (history::URLRows::const_iterator j(url_matches.begin()); 708 for (history::URLRows::const_iterator j(url_matches.begin());
711 j != url_matches.end(); ++j) { 709 j != url_matches.end(); ++j) {
712 const URLPrefix* best_prefix = URLPrefix::BestURLPrefix( 710 const URLPrefix* best_prefix = URLPrefix::BestURLPrefix(
713 base::UTF8ToUTF16(j->url().spec()), base::string16()); 711 base::UTF8ToUTF16(j->url().spec()), base::string16());
714 DCHECK(best_prefix); 712 DCHECK(best_prefix);
715 history_matches.push_back(history::HistoryMatch( 713 params->matches.push_back(history::HistoryMatch(
716 *j, i->prefix.length(), !i->num_components, 714 *j, i->prefix.length(), !i->num_components,
717 i->num_components >= best_prefix->num_components)); 715 i->num_components >= best_prefix->num_components));
718 } 716 }
719 } 717 }
720 718
721 // Create sorted list of suggestions. 719 // Create sorted list of suggestions.
722 CullPoorMatches(*params, &history_matches); 720 CullPoorMatches(params);
723 SortAndDedupMatches(&history_matches); 721 SortAndDedupMatches(&params->matches);
724 722
725 // Try to create a shorter suggestion from the best match. 723 // Try to create a shorter suggestion from the best match.
726 // We allow the what you typed match to be displayed when there's a reasonable 724 // We allow the what you typed match to be displayed when there's a reasonable
727 // chance the user actually cares: 725 // chance the user actually cares:
728 // * Their input can be opened as a URL, and 726 // * Their input can be opened as a URL, and
729 // * We parsed the input as a URL, or it starts with an explicit "http:" or 727 // * We parsed the input as a URL, or it starts with an explicit "http:" or
730 // "https:". 728 // "https:".
731 // Otherwise, this is just low-quality noise. In the cases where we've parsed 729 // Otherwise, this is just low-quality noise. In the cases where we've parsed
732 // as UNKNOWN, we'll still show an accidental search infobar if need be. 730 // as UNKNOWN, we'll still show an accidental search infobar if need be.
733 VisitClassifier classifier(this, params->input, db); 731 VisitClassifier classifier(this, params->input, db);
734 bool have_what_you_typed_match = 732 bool have_what_you_typed_match =
735 (params->input.type() != metrics::OmniboxInputType::QUERY) && 733 (params->input.type() != metrics::OmniboxInputType::QUERY) &&
736 ((params->input.type() != metrics::OmniboxInputType::UNKNOWN) || 734 ((params->input.type() != metrics::OmniboxInputType::UNKNOWN) ||
737 (classifier.type() == VisitClassifier::UNVISITED_INTRANET) || 735 (classifier.type() == VisitClassifier::UNVISITED_INTRANET) ||
738 !params->trim_http || 736 !params->trim_http ||
739 (AutocompleteInput::NumNonHostComponents(params->input.parts()) > 0)); 737 (AutocompleteInput::NumNonHostComponents(params->input.parts()) > 0));
740 PromoteOrCreateShorterSuggestion(db, *params, have_what_you_typed_match, 738 PromoteOrCreateShorterSuggestion(db, have_what_you_typed_match, params);
741 &history_matches);
742 739
743 // Try to promote a match as an exact/inline autocomplete match. This also 740 // Try to promote a match as an exact/inline autocomplete match.
744 // moves it to the front of |history_matches|, so skip over it when 741 // Checking what_you_typed_match.is_history_what_you_typed_match tells us
Mark P 2014/06/19 03:53:14 How about inserting a sentence here to give contex
Peter Kasting 2014/06/19 20:55:22 I reorganized the comments here a bit. I took the
745 // converting the rest of the matches. 742 // whether SuggestExactInput() succeeded in constructing a valid match.
746 size_t first_match = 1; 743 // Additionally, in the case where the user has typed "foo.com" and visited
747 size_t exact_suggestion = 0; 744 // (but not typed) "foo/", and the input is "foo", the first pass will fall
748 // Checking params->what_you_typed_match.is_history_what_you_typed_match tells 745 // into the FRONT_HISTORY_MATCH case for "foo.com" but the second pass can
Mark P 2014/06/19 03:53:14 nit: case -> case below to make it obvious this l
749 // us whether SuggestExactInput() succeeded in constructing a valid match. 746 // suggest the exact input as a better URL. Since we need both passes to
750 if (params->what_you_typed_match.is_history_what_you_typed_match && 747 // agree, and since during the first pass there's no way to know about "foo/",
751 (!backend || !params->dont_suggest_exact_input) && 748 // ensure that if the promote type was set to FRONT_HISTORY_MATCH during the
752 FixupExactSuggestion(db, classifier, params, &history_matches)) { 749 // first pass, that should prevent this code from suggesting the exact input
750 // as a better match (during the second pass).
751 params->exact_suggestion_is_in_history =
752 params->what_you_typed_match.is_history_what_you_typed_match &&
753 (!backend ||
Mark P 2014/06/19 03:53:14 consider comment: // on first pass
754 (params->promote_type !=
755 HistoryURLProviderParams::FRONT_HISTORY_MATCH)) &&
Mark P 2014/06/19 03:53:14 consider comment: // first pass did not inline au
756 FixupExactSuggestion(db, classifier, params);
757 if (params->exact_suggestion_is_in_history) {
753 // Got an exact match for the user's input. Treat it as the best match 758 // Got an exact match for the user's input. Treat it as the best match
754 // regardless of the input type. 759 // regardless of the input type.
755 exact_suggestion = 1; 760 params->promote_type = HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH;
756 params->matches.push_back(params->what_you_typed_match); 761 } else if (!params->prevent_inline_autocomplete && !params->matches.empty() &&
757 } else if (params->prevent_inline_autocomplete || 762 CanPromoteMatchForInlineAutocomplete(params->matches[0])) {
758 history_matches.empty() || 763 params->promote_type = HistoryURLProviderParams::FRONT_HISTORY_MATCH;
759 !PromoteMatchForInlineAutocomplete(history_matches.front(), params)) { 764 } else if (have_what_you_typed_match) {
760 // Failed to promote any URLs for inline autocompletion. Use the What You 765 // Failed to promote any URLs for inline autocompletion. Use the What You
761 // Typed match, if we have it. 766 // Typed match, if we have it.
762 first_match = 0; 767 params->promote_type = have_what_you_typed_match ?
763 if (have_what_you_typed_match) 768 HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH :
764 params->matches.push_back(params->what_you_typed_match); 769 HistoryURLProviderParams::NEITHER;
765 } 770 }
766 771
767 // This is the end of the synchronous pass. 772 const size_t max_results =
768 if (!backend) 773 kMaxMatches + (params->exact_suggestion_is_in_history ? 1 : 0);
769 return; 774 if (backend && cull_redirects_) {
770
771 // Determine relevancy of highest scoring match, if any.
772 int relevance = -1;
773 for (ACMatches::const_iterator it = params->matches.begin();
774 it != params->matches.end(); ++it) {
775 relevance = std::max(relevance, it->relevance);
776 }
777
778 if (cull_redirects_) {
779 // Remove redirects and trim list to size. We want to provide up to 775 // Remove redirects and trim list to size. We want to provide up to
780 // kMaxMatches results plus the What You Typed result, if it was added to 776 // kMaxMatches results plus the What You Typed result, if it was added to
781 // |history_matches| above. 777 // params->matches above.
782 CullRedirects(backend, &history_matches, kMaxMatches + exact_suggestion); 778 CullRedirects(backend, &params->matches, max_results);
783 } else { 779 } else if (params->matches.size() > max_results) {
784 // Simply trim the list to size. 780 // Simply trim the list to size.
785 if (history_matches.size() > kMaxMatches + exact_suggestion) 781 params->matches.resize(max_results);
786 history_matches.resize(kMaxMatches + exact_suggestion);
787 }
788
789 // Convert the history matches to autocomplete matches.
790 for (size_t i = first_match; i < history_matches.size(); ++i) {
791 const history::HistoryMatch& match = history_matches[i];
792 DCHECK(!have_what_you_typed_match ||
793 (match.url_info.url() !=
794 GURL(params->matches.front().destination_url)));
795 // If we've assigned a score already, all later matches score one
796 // less than the previous match.
797 relevance = (relevance > 0) ?
798 (relevance - 1) :
799 CalculateRelevance(NORMAL,
800 static_cast<int>(history_matches.size() - 1 - i));
801 AutocompleteMatch ac_match = HistoryMatchToACMatch(*params, match,
802 NORMAL, relevance);
803 // The experimental scoring must not change the top result's score.
804 if (!params->matches.empty()) {
805 relevance = CalculateRelevanceScoreUsingScoringParams(match, relevance,
806 scoring_params_);
807 ac_match.relevance = relevance;
808 }
809 params->matches.push_back(ac_match);
810 } 782 }
811 } 783 }
812 784
785 void HistoryURLProvider::PromoteMatchIfNecessary(
786 const HistoryURLProviderParams& params) {
787 if (params.promote_type == HistoryURLProviderParams::NEITHER)
788 return;
789 matches_.push_back(
790 (params.promote_type == HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH) ?
791 params.what_you_typed_match :
792 HistoryMatchToACMatch(params, 0, INLINE_AUTOCOMPLETE,
793 CalculateRelevance(INLINE_AUTOCOMPLETE, 0)));
794 }
795
813 void HistoryURLProvider::QueryComplete( 796 void HistoryURLProvider::QueryComplete(
814 HistoryURLProviderParams* params_gets_deleted) { 797 HistoryURLProviderParams* params_gets_deleted) {
815 // Ensure |params_gets_deleted| gets deleted on exit. 798 // Ensure |params_gets_deleted| gets deleted on exit.
816 scoped_ptr<HistoryURLProviderParams> params(params_gets_deleted); 799 scoped_ptr<HistoryURLProviderParams> params(params_gets_deleted);
817 800
818 // If the user hasn't already started another query, clear our member pointer 801 // If the user hasn't already started another query, clear our member pointer
819 // so we can't write into deleted memory. 802 // so we can't write into deleted memory.
820 if (params_ == params_gets_deleted) 803 if (params_ == params_gets_deleted)
821 params_ = NULL; 804 params_ = NULL;
822 805
823 // Don't send responses for queries that have been canceled. 806 // Don't send responses for queries that have been canceled.
824 if (params->cancel_flag.IsSet()) 807 if (params->cancel_flag.IsSet())
825 return; // Already set done_ when we canceled, no need to set it again. 808 return; // Already set done_ when we canceled, no need to set it again.
826 809
827 // Don't modify |matches_| if the query failed, since it might have a default 810 // Don't modify |matches_| if the query failed, since it might have a default
828 // match in it, whereas |params->matches| will be empty. 811 // match in it, whereas |params->matches| will be empty.
829 if (!params->failed) { 812 if (!params->failed) {
830 matches_.swap(params->matches); 813 matches_.clear();
814 PromoteMatchIfNecessary(*params);
815
816 // If we promoted the first match, skip over it when converting the rest of
817 // the matches.
Mark P 2014/06/19 03:53:14 side comment: I really wish we didn't have the .pr
Peter Kasting 2014/06/19 20:55:22 I found a way to eliminate this member by adding a
818 const size_t first_match =
819 (params->exact_suggestion_is_in_history ||
820 (params->promote_type ==
821 HistoryURLProviderParams::FRONT_HISTORY_MATCH)) ? 1 : 0;
822
823 // Determine relevance of highest scoring match, if any.
824 int relevance = matches_.empty() ?
825 CalculateRelevance(
826 NORMAL,
827 static_cast<int>(params->matches.size() - 1 - first_match)) :
Mark P 2014/06/19 03:53:14 I think "- first_match" is unnecessary. If matche
Peter Kasting 2014/06/19 20:55:22 You're right, though it took me some examination t
828 matches_[0].relevance;
829
830 // Convert the history matches to autocomplete matches.
831 for (size_t i = first_match; i < params->matches.size(); ++i) {
832 // All matches score one less than the previous match.
833 --relevance;
834 // The experimental scoring must not change the top result's score.
835 if (!matches_.empty()) {
836 relevance = CalculateRelevanceScoreUsingScoringParams(
837 params->matches[i], relevance, scoring_params_);
838 }
839 matches_.push_back(HistoryMatchToACMatch(*params, i, NORMAL, relevance));
840 }
841
831 UpdateStarredStateOfMatches(); 842 UpdateStarredStateOfMatches();
832 } 843 }
833 844
834 done_ = true; 845 done_ = true;
835 listener_->OnProviderUpdate(true); 846 listener_->OnProviderUpdate(true);
836 } 847 }
837 848
838 bool HistoryURLProvider::FixupExactSuggestion( 849 bool HistoryURLProvider::FixupExactSuggestion(
839 history::URLDatabase* db, 850 history::URLDatabase* db,
840 const VisitClassifier& classifier, 851 const VisitClassifier& classifier,
841 HistoryURLProviderParams* params, 852 HistoryURLProviderParams* params) const {
842 history::HistoryMatches* matches) const {
843 DCHECK(matches != NULL);
844
845 MatchType type = INLINE_AUTOCOMPLETE; 853 MatchType type = INLINE_AUTOCOMPLETE;
846 switch (classifier.type()) { 854 switch (classifier.type()) {
847 case VisitClassifier::INVALID: 855 case VisitClassifier::INVALID:
848 return false; 856 return false;
849 case VisitClassifier::UNVISITED_INTRANET: 857 case VisitClassifier::UNVISITED_INTRANET:
850 type = UNVISITED_INTRANET; 858 type = UNVISITED_INTRANET;
851 break; 859 break;
852 default: 860 default:
853 DCHECK_EQ(VisitClassifier::VISITED, classifier.type()); 861 DCHECK_EQ(VisitClassifier::VISITED, classifier.type());
854 // We have data for this match, use it. 862 // We have data for this match, use it.
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
897 return false; 905 return false;
898 } 906 }
899 907
900 params->what_you_typed_match.relevance = CalculateRelevance(type, 0); 908 params->what_you_typed_match.relevance = CalculateRelevance(type, 0);
901 909
902 // If there are any other matches, then don't promote this match here, in 910 // If there are any other matches, then don't promote this match here, in
903 // hopes the caller will be able to inline autocomplete a better suggestion. 911 // hopes the caller will be able to inline autocomplete a better suggestion.
904 // DoAutocomplete() will fall back on this match if inline autocompletion 912 // DoAutocomplete() will fall back on this match if inline autocompletion
905 // fails. This matches how we react to never-visited URL inputs in the non- 913 // fails. This matches how we react to never-visited URL inputs in the non-
906 // intranet case. 914 // intranet case.
907 if (type == UNVISITED_INTRANET && !matches->empty()) 915 if (type == UNVISITED_INTRANET && !params->matches.empty())
908 return false; 916 return false;
909 917
910 // Put it on the front of the HistoryMatches for redirect culling. 918 // Put it on the front of the HistoryMatches for redirect culling.
911 CreateOrPromoteMatch(classifier.url_row(), base::string16::npos, false, 919 CreateOrPromoteMatch(classifier.url_row(), base::string16::npos, false,
912 matches, true, true); 920 &params->matches, true, true);
913 return true; 921 return true;
914 } 922 }
915 923
916 bool HistoryURLProvider::CanFindIntranetURL( 924 bool HistoryURLProvider::CanFindIntranetURL(
917 history::URLDatabase* db, 925 history::URLDatabase* db,
918 const AutocompleteInput& input) const { 926 const AutocompleteInput& input) const {
919 // Normally passing the first two conditions below ought to guarantee the 927 // Normally passing the first two conditions below ought to guarantee the
920 // third condition, but because FixupUserInput() can run and modify the 928 // third condition, but because FixupUserInput() can run and modify the
921 // input's text and parts between Parse() and here, it seems better to be 929 // input's text and parts between Parse() and here, it seems better to be
922 // paranoid and check. 930 // paranoid and check.
923 if ((input.type() != metrics::OmniboxInputType::UNKNOWN) || 931 if ((input.type() != metrics::OmniboxInputType::UNKNOWN) ||
924 !LowerCaseEqualsASCII(input.scheme(), url::kHttpScheme) || 932 !LowerCaseEqualsASCII(input.scheme(), url::kHttpScheme) ||
925 !input.parts().host.is_nonempty()) 933 !input.parts().host.is_nonempty())
926 return false; 934 return false;
927 const std::string host(base::UTF16ToUTF8( 935 const std::string host(base::UTF16ToUTF8(
928 input.text().substr(input.parts().host.begin, input.parts().host.len))); 936 input.text().substr(input.parts().host.begin, input.parts().host.len)));
929 const size_t registry_length = 937 const size_t registry_length =
930 net::registry_controlled_domains::GetRegistryLength( 938 net::registry_controlled_domains::GetRegistryLength(
931 host, 939 host,
932 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, 940 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
933 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); 941 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
934 return registry_length == 0 && db->IsTypedHost(host); 942 return registry_length == 0 && db->IsTypedHost(host);
935 } 943 }
936 944
937 bool HistoryURLProvider::PromoteMatchForInlineAutocomplete(
938 const history::HistoryMatch& match,
939 HistoryURLProviderParams* params) {
940 if (!CanPromoteMatchForInlineAutocomplete(match))
941 return false;
942
943 // In the case where the user has typed "foo.com" and visited (but not typed)
944 // "foo/", and the input is "foo", we can reach here for "foo.com" during the
945 // first pass but have the second pass suggest the exact input as a better
946 // URL. Since we need both passes to agree, and since during the first pass
947 // there's no way to know about "foo/", make reaching this point prevent any
948 // future pass from suggesting the exact input as a better match.
949 params->dont_suggest_exact_input = true;
950 params->matches.push_back(HistoryMatchToACMatch(
951 *params, match, INLINE_AUTOCOMPLETE,
952 CalculateRelevance(INLINE_AUTOCOMPLETE, 0)));
953 return true;
954 }
955
956 void HistoryURLProvider::PromoteOrCreateShorterSuggestion( 945 void HistoryURLProvider::PromoteOrCreateShorterSuggestion(
957 history::URLDatabase* db, 946 history::URLDatabase* db,
958 const HistoryURLProviderParams& params,
959 bool have_what_you_typed_match, 947 bool have_what_you_typed_match,
960 history::HistoryMatches* matches) { 948 HistoryURLProviderParams* params) {
961 if (matches->empty()) 949 if (params->matches.empty())
962 return; // No matches, nothing to do. 950 return; // No matches, nothing to do.
963 951
964 // Determine the base URL from which to search, and whether that URL could 952 // Determine the base URL from which to search, and whether that URL could
965 // itself be added as a match. We can add the base iff it's not "effectively 953 // itself be added as a match. We can add the base iff it's not "effectively
966 // the same" as any "what you typed" match. 954 // the same" as any "what you typed" match.
967 const history::HistoryMatch& match = matches->front(); 955 const history::HistoryMatch& match = params->matches[0];
968 GURL search_base = ConvertToHostOnly(match, params.input.text()); 956 GURL search_base = ConvertToHostOnly(match, params->input.text());
969 bool can_add_search_base_to_matches = !have_what_you_typed_match; 957 bool can_add_search_base_to_matches = !have_what_you_typed_match;
970 if (search_base.is_empty()) { 958 if (search_base.is_empty()) {
971 // Search from what the user typed when we couldn't reduce the best match 959 // Search from what the user typed when we couldn't reduce the best match
972 // to a host. Careful: use a substring of |match| here, rather than the 960 // to a host. Careful: use a substring of |match| here, rather than the
973 // first match in |params|, because they might have different prefixes. If 961 // first match in |params|, because they might have different prefixes. If
974 // the user typed "google.com", params->what_you_typed_match will hold 962 // the user typed "google.com", params->what_you_typed_match will hold
975 // "http://google.com/", but |match| might begin with 963 // "http://google.com/", but |match| might begin with
976 // "http://www.google.com/". 964 // "http://www.google.com/".
977 // TODO: this should be cleaned up, and is probably incorrect for IDN. 965 // TODO: this should be cleaned up, and is probably incorrect for IDN.
978 std::string new_match = match.url_info.url().possibly_invalid_spec(). 966 std::string new_match = match.url_info.url().possibly_invalid_spec().
979 substr(0, match.input_location + params.input.text().length()); 967 substr(0, match.input_location + params->input.text().length());
980 search_base = GURL(new_match); 968 search_base = GURL(new_match);
981 if (search_base.is_empty()) 969 if (search_base.is_empty())
982 return; // Can't construct a valid URL from which to start a search. 970 return; // Can't construct a valid URL from which to start a search.
983 } else if (!can_add_search_base_to_matches) { 971 } else if (!can_add_search_base_to_matches) {
984 can_add_search_base_to_matches = 972 can_add_search_base_to_matches =
985 (search_base != params.what_you_typed_match.destination_url); 973 (search_base != params->what_you_typed_match.destination_url);
986 } 974 }
987 if (search_base == match.url_info.url()) 975 if (search_base == match.url_info.url())
988 return; // Couldn't shorten |match|, so no range of URLs to search over. 976 return; // Couldn't shorten |match|, so no range of URLs to search over.
989 977
990 // Search the DB for short URLs between our base and |match|. 978 // Search the DB for short URLs between our base and |match|.
991 history::URLRow info(search_base); 979 history::URLRow info(search_base);
992 bool promote = true; 980 bool promote = true;
993 // A short URL is only worth suggesting if it's been visited at least a third 981 // A short URL is only worth suggesting if it's been visited at least a third
994 // as often as the longer URL. 982 // as often as the longer URL.
995 const int min_visit_count = ((match.url_info.visit_count() - 1) / 3) + 1; 983 const int min_visit_count = ((match.url_info.visit_count() - 1) / 3) + 1;
(...skipping 13 matching lines...) Expand all
1009 // Try to get info on the search base itself. Promote it to the top if the 997 // Try to get info on the search base itself. Promote it to the top if the
1010 // original best match isn't good enough to autocomplete. 998 // original best match isn't good enough to autocomplete.
1011 db->GetRowForURL(search_base, &info); 999 db->GetRowForURL(search_base, &info);
1012 promote = match.url_info.typed_count() <= 1; 1000 promote = match.url_info.typed_count() <= 1;
1013 } 1001 }
1014 1002
1015 // Promote or add the desired URL to the list of matches. 1003 // Promote or add the desired URL to the list of matches.
1016 bool ensure_can_inline = 1004 bool ensure_can_inline =
1017 promote && CanPromoteMatchForInlineAutocomplete(match); 1005 promote && CanPromoteMatchForInlineAutocomplete(match);
1018 ensure_can_inline &= CreateOrPromoteMatch(info, match.input_location, 1006 ensure_can_inline &= CreateOrPromoteMatch(info, match.input_location,
1019 match.match_in_scheme, matches, create_shorter_match_, promote); 1007 match.match_in_scheme, &params->matches, create_shorter_match_, promote);
1020 if (ensure_can_inline) 1008 if (ensure_can_inline)
1021 matches->front().promoted = true; 1009 params->matches[0].promoted = true;
1022 } 1010 }
1023 1011
1024 void HistoryURLProvider::CullPoorMatches( 1012 void HistoryURLProvider::CullPoorMatches(
1025 const HistoryURLProviderParams& params, 1013 HistoryURLProviderParams* params) const {
1026 history::HistoryMatches* matches) const {
1027 const base::Time& threshold(history::AutocompleteAgeThreshold()); 1014 const base::Time& threshold(history::AutocompleteAgeThreshold());
1028 for (history::HistoryMatches::iterator i(matches->begin()); 1015 for (history::HistoryMatches::iterator i(params->matches.begin());
1029 i != matches->end(); ) { 1016 i != params->matches.end(); ) {
1030 if (RowQualifiesAsSignificant(i->url_info, threshold) && 1017 if (RowQualifiesAsSignificant(i->url_info, threshold) &&
1031 !(params.default_search_provider && 1018 (!params->default_search_provider ||
1032 params.default_search_provider->IsSearchURL( 1019 !params->default_search_provider->IsSearchURL(
1033 i->url_info.url(), *params.search_terms_data.get()))) { 1020 i->url_info.url(), *params->search_terms_data))) {
1034 ++i; 1021 ++i;
1035 } else { 1022 } else {
1036 i = matches->erase(i); 1023 i = params->matches.erase(i);
1037 } 1024 }
1038 } 1025 }
1039 } 1026 }
1040 1027
1041 void HistoryURLProvider::CullRedirects(history::HistoryBackend* backend, 1028 void HistoryURLProvider::CullRedirects(history::HistoryBackend* backend,
1042 history::HistoryMatches* matches, 1029 history::HistoryMatches* matches,
1043 size_t max_results) const { 1030 size_t max_results) const {
1044 for (size_t source = 0; 1031 for (size_t source = 0;
1045 (source < matches->size()) && (source < max_results); ) { 1032 (source < matches->size()) && (source < max_results); ) {
1046 const GURL& url = (*matches)[source].url_info.url(); 1033 const GURL& url = (*matches)[source].url_info.url();
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1093 // need to shift it to the right and remember that so we can return it. 1080 // need to shift it to the right and remember that so we can return it.
1094 next = matches->erase(next); 1081 next = matches->erase(next);
1095 if (static_cast<size_t>(next - matches->begin()) < next_index) 1082 if (static_cast<size_t>(next - matches->begin()) < next_index)
1096 --next_index; 1083 --next_index;
1097 } 1084 }
1098 return next_index; 1085 return next_index;
1099 } 1086 }
1100 1087
1101 AutocompleteMatch HistoryURLProvider::HistoryMatchToACMatch( 1088 AutocompleteMatch HistoryURLProvider::HistoryMatchToACMatch(
1102 const HistoryURLProviderParams& params, 1089 const HistoryURLProviderParams& params,
1103 const history::HistoryMatch& history_match, 1090 size_t match_number,
1104 MatchType match_type, 1091 MatchType match_type,
1105 int relevance) { 1092 int relevance) {
1093 // The FormattedStringWithEquivalentMeaning() call below requires callers to
1094 // be on the UI thread.
1095 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI) ||
1096 !content::BrowserThread::IsThreadInitialized(content::BrowserThread::UI));
1097
1098 const history::HistoryMatch& history_match = params.matches[match_number];
1106 const history::URLRow& info = history_match.url_info; 1099 const history::URLRow& info = history_match.url_info;
1107 AutocompleteMatch match(this, relevance, 1100 AutocompleteMatch match(this, relevance,
1108 !!info.visit_count(), AutocompleteMatchType::HISTORY_URL); 1101 !!info.visit_count(), AutocompleteMatchType::HISTORY_URL);
1109 match.typed_count = info.typed_count(); 1102 match.typed_count = info.typed_count();
1110 match.destination_url = info.url(); 1103 match.destination_url = info.url();
1111 DCHECK(match.destination_url.is_valid()); 1104 DCHECK(match.destination_url.is_valid());
1112 size_t inline_autocomplete_offset = 1105 size_t inline_autocomplete_offset =
1113 history_match.input_location + params.input.text().length(); 1106 history_match.input_location + params.input.text().length();
1114 std::string languages = (match_type == WHAT_YOU_TYPED) ? 1107 std::string languages = (match_type == WHAT_YOU_TYPED) ?
1115 std::string() : params.languages; 1108 std::string() : params.languages;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1147 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, 1140 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
1148 match.contents.length(), ACMatchClassification::URL, 1141 match.contents.length(), ACMatchClassification::URL,
1149 &match.contents_class); 1142 &match.contents_class);
1150 } 1143 }
1151 match.description = info.title(); 1144 match.description = info.title();
1152 match.description_class = 1145 match.description_class =
1153 ClassifyDescription(params.input.text(), match.description); 1146 ClassifyDescription(params.input.text(), match.description);
1154 RecordAdditionalInfoFromUrlRow(info, &match); 1147 RecordAdditionalInfoFromUrlRow(info, &match);
1155 return match; 1148 return match;
1156 } 1149 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698